Skip to content

Commit

Permalink
Merge pull request #10377 from cladmi/backport/2018.10/pr/crypto/ccm/…
Browse files Browse the repository at this point in the history
…auth_data_len_upper_bound

crypto/ccm: fix auth_data_len check [backport 2018.10]
  • Loading branch information
jia200x authored Nov 12, 2018
2 parents 3ff78e1 + c947815 commit 4c810c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 14 additions & 4 deletions sys/crypto/modes/ccm.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,17 @@ int ccm_compute_adata_mac(cipher_t* cipher, uint8_t* auth_data,
/* 16 octet block size + max. 10 len encoding */
uint8_t auth_data_encoded[26], len_encoding = 0;

if ( auth_data_len < (((uint32_t) 2) << 16)) { /* length (0x0001 ... 0xFEFF) */
/* If 0 < l(a) < (2^16 - 2^8), then the length field is encoded as two
* octets. (RFC3610 page 2)
*/
if (auth_data_len <= 0xFEFF) {
/* length (0x0001 ... 0xFEFF) */
len_encoding = 2;

auth_data_encoded[1] = auth_data_len & 0xFF;
auth_data_encoded[0] = (auth_data_len >> 8) & 0xFF;
} else {
DEBUG("UNSUPPORTED Adata length\n");
DEBUG("UNSUPPORTED Adata length: %" PRIu32 "\n", auth_data_len);
return -1;
}

Expand Down Expand Up @@ -167,7 +171,10 @@ int cipher_encrypt_ccm(cipher_t* cipher, uint8_t* auth_data, uint32_t auth_data_
}

/* MAC calulation (T) with additional data and plaintext */
ccm_compute_adata_mac(cipher, auth_data, auth_data_len, mac_iv);
len = ccm_compute_adata_mac(cipher, auth_data, auth_data_len, mac_iv);
if (len < 0) {
return len;
}
len = ccm_compute_cbc_mac(cipher, mac_iv, input, input_len, mac);
if (len < 0) {
return len;
Expand Down Expand Up @@ -245,7 +252,10 @@ int cipher_decrypt_ccm(cipher_t* cipher, uint8_t* auth_data,
}

/* MAC calulation (T) with additional data and plaintext */
ccm_compute_adata_mac(cipher, auth_data, auth_data_len, mac_iv);
len = ccm_compute_adata_mac(cipher, auth_data, auth_data_len, mac_iv);
if (len < 0) {
return len;
}
len = ccm_compute_cbc_mac(cipher, mac_iv, plain, plain_len, mac);
if (len < 0) {
return len;
Expand Down
4 changes: 4 additions & 0 deletions tests/unittests/tests-crypto/tests-crypto-modes-ccm.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ static void test_crypto_modes_ccm_check_len(void)

ret = _test_ccm_len(cipher_decrypt_ccm, 8, einput, 16, 0);
TEST_ASSERT_MESSAGE(ret > 0, "Decryption : failed with valid input_len");

/* ccm library does not support auth_data_len > 0xFEFF */
ret = _test_ccm_len(cipher_encrypt_ccm, 2, NULL, 0, 0xFEFF + 1);
TEST_ASSERT_EQUAL_INT(-1, ret);
}


Expand Down

0 comments on commit 4c810c2

Please sign in to comment.