Skip to content
Snippets Groups Projects
Unverified Commit ee311a33 authored by Gaëtan Harter's avatar Gaëtan Harter
Browse files

crypto/ccm: refactor input_len check in a function

Refactorg checking that length can be stored in a number of bytes to a
subfunction.
parent a47e5e42
No related branches found
No related tags found
No related merge requests found
......@@ -129,6 +129,14 @@ int ccm_compute_adata_mac(cipher_t* cipher, uint8_t* auth_data,
}
/* Check if 'value' can be stored in 'num_bytes' */
static inline int _fits_in_nbytes(size_t value, uint8_t num_bytes)
{
uint32_t length_max = 1 << (8 * num_bytes);
return value < length_max;
}
int cipher_encrypt_ccm(cipher_t* cipher, uint8_t* auth_data, uint32_t auth_data_len,
uint8_t mac_length, uint8_t length_encoding,
uint8_t* nonce, size_t nonce_len,
......@@ -136,7 +144,6 @@ int cipher_encrypt_ccm(cipher_t* cipher, uint8_t* auth_data, uint32_t auth_data_
uint8_t* output)
{
int len = -1;
uint32_t length_max;
uint8_t nonce_counter[16] = {0}, mac_iv[16] = {0}, mac[16] = {0},
stream_block[16] = {0}, zero_block[16] = {0}, block_size;
......@@ -144,8 +151,8 @@ int cipher_encrypt_ccm(cipher_t* cipher, uint8_t* auth_data, uint32_t auth_data_
return CCM_ERR_INVALID_MAC_LENGTH;
}
length_max = 1 << (8 * length_encoding);
if (length_encoding < 2 || length_encoding > 8 || input_len >= length_max) {
if (length_encoding < 2 || length_encoding > 8 ||
!_fits_in_nbytes(input_len, length_encoding)) {
return CCM_ERR_INVALID_LENGTH_ENCODING;
}
......@@ -196,7 +203,6 @@ int cipher_decrypt_ccm(cipher_t* cipher, uint8_t* auth_data,
uint8_t* input, size_t input_len, uint8_t* plain)
{
int len = -1;
uint32_t length_max;
uint8_t nonce_counter[16] = {0}, mac_iv[16] = {0}, mac[16] = {0},
mac_recv[16] = {0}, stream_block[16] = {0}, zero_block[16] = {0},
plain_len, block_size;
......@@ -205,8 +211,8 @@ int cipher_decrypt_ccm(cipher_t* cipher, uint8_t* auth_data,
return CCM_ERR_INVALID_MAC_LENGTH;
}
length_max = 1 << (8 * length_encoding);
if (length_encoding < 2 || length_encoding > 8 || input_len >= length_max) {
if (length_encoding < 2 || length_encoding > 8 ||
!_fits_in_nbytes(input_len, length_encoding)) {
return CCM_ERR_INVALID_LENGTH_ENCODING;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment