diff --git a/sys/crypto/modes/ccm.c b/sys/crypto/modes/ccm.c index 657c76b47e5deda6037fa1aa9beae59ce7393a87..6c7d1e7ba47929243b48221e54371450e35eb013 100644 --- a/sys/crypto/modes/ccm.c +++ b/sys/crypto/modes/ccm.c @@ -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; } @@ -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; @@ -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; diff --git a/tests/unittests/tests-crypto/tests-crypto-modes-ccm.c b/tests/unittests/tests-crypto/tests-crypto-modes-ccm.c index 1fe20e7972d9a238e5649e22c465183ff3c382bc..c9151d9ef57a8124ac8b5f7aa7066524186807c9 100644 --- a/tests/unittests/tests-crypto/tests-crypto-modes-ccm.c +++ b/tests/unittests/tests-crypto/tests-crypto-modes-ccm.c @@ -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); }