From 7fc367c32b65b98995add238635574b1b1cc4870 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Wed, 14 Oct 2026 14:56:41 -0700 Subject: [PATCH 1/4] F-6452 - Correct AES-CCM error logging --- src/we_aes_ccm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/we_aes_ccm.c b/src/we_aes_ccm.c index d5e17ac7..d2065ca3 100644 --- a/src/we_aes_ccm.c +++ b/src/we_aes_ccm.c @@ -370,7 +370,7 @@ static int we_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, rc = wc_AesCcmSetNonce(&aes->aes, aes->iv, aes->ivLen); if (rc != 0) { WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, - "wc_AesCcmSetExtIV", rc); + "wc_AesCcmSetNonce", rc); ret = -1; } } @@ -404,7 +404,7 @@ static int we_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, aes->ivLen, aes->tag, aes->tagLen, aes->aad, aes->aadLen); if (rc != 0) { - WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesCcmDecrypt_ex", rc); + WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesCcmDecrypt", rc); ret = -1; } if (ret == 1) { From c709fb333ef08281bd14c7048cd2dad2b7e2042b Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Wed, 14 Oct 2026 15:04:42 -0700 Subject: [PATCH 2/4] F-11321 - Use the standard AES-CCM default tag size --- src/we_aes_ccm.c | 4 +-- test/test_aestag.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/we_aes_ccm.c b/src/we_aes_ccm.c index d2065ca3..44839ab6 100644 --- a/src/we_aes_ccm.c +++ b/src/we_aes_ccm.c @@ -360,8 +360,8 @@ static int we_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, } else if ((ret == 1) && (len > 0)) { if (aes->tagLen == 0) { - /* Default to full size tag. */ - aes->tagLen = EVP_CCM_TLS_TAG_LEN; + /* OpenSSL defaults non-TLS CCM tags to 12 bytes. */ + aes->tagLen = 12; } if (aes->enc) { if (!aes->ivSet) { diff --git a/test/test_aestag.c b/test/test_aestag.c index d223cc2c..b8bed88e 100644 --- a/test/test_aestag.c +++ b/test/test_aestag.c @@ -27,6 +27,7 @@ #ifndef EVP_CCM_TLS_TAG_LEN #define EVP_CCM_TLS_TAG_LEN EVP_GCM_TLS_TAG_LEN #endif +#define CCM_TAG_DEFAULT_LEN 12 #if defined(WE_HAVE_AESGCM) || defined(WE_HAVE_AESCCM) @@ -1179,12 +1180,82 @@ int test_aes128_gcm_iv_gen_bounds(ENGINE *e, void *data) #ifdef WE_HAVE_AESCCM +static int test_aes_ccm_default_tag_encrypt(ENGINE *e, unsigned char *enc, + unsigned char *tag) +{ + int err = 0; + int encLen = 0; + unsigned char key[16]; + unsigned char iv[13]; + unsigned char msg[] = "Test pattern"; + EVP_CIPHER_CTX *ctx = NULL; + + XMEMSET(key, 0, sizeof(key)); + XMEMSET(iv, 0, sizeof(iv)); + + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) { + err = EVP_EncryptInit_ex(ctx, EVP_aes_128_ccm(), e, NULL, NULL) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, + sizeof(iv), NULL) != 1; + } + if (err == 0) { + err = EVP_EncryptInit_ex(ctx, NULL, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_EncryptUpdate(ctx, NULL, &encLen, NULL, sizeof(msg)) != 1; + } + if (err == 0) { + err = EVP_EncryptUpdate(ctx, enc, &encLen, msg, sizeof(msg)) != 1; + } + if (err == 0) { + err = encLen != (int)sizeof(msg); + } + if (err == 0) { + err = EVP_EncryptFinal_ex(ctx, enc + encLen, &encLen) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, + CCM_TAG_DEFAULT_LEN, tag) != 1; + } + + EVP_CIPHER_CTX_free(ctx); + return err; +} + +static int test_aes_ccm_default_tag(ENGINE *e) +{ + int err = 0; + unsigned char enc[sizeof("Test pattern")]; + unsigned char encOSSL[sizeof("Test pattern")]; + unsigned char tag[CCM_TAG_DEFAULT_LEN]; + unsigned char tagOSSL[CCM_TAG_DEFAULT_LEN]; + + err = test_aes_ccm_default_tag_encrypt(NULL, encOSSL, tagOSSL); + if (err == 0) { + err = test_aes_ccm_default_tag_encrypt(e, enc, tag); + } + if (err == 0) { + err = XMEMCMP(enc, encOSSL, sizeof(enc)) != 0; + } + if (err == 0) { + err = XMEMCMP(tag, tagOSSL, sizeof(tag)) != 0; + } + + return err; +} + int test_aes128_ccm(ENGINE *e, void *data) { int err = 0; /* test with default length field (L) */ err = test_aes_tag(e, data, EVP_aes_128_ccm(), 16, 13, 1, 0); + if (err == 0) { + err = test_aes_ccm_default_tag(e); + } #if OPENSSL_VERSION_NUMBER < 0x30000000L /* test with modified length field (L) of 7 */ From 935ad94f50c84dd8a96a5de6c8f0f58d1f4a4d5f Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Wed, 14 Oct 2026 15:08:42 -0700 Subject: [PATCH 3/4] F-10136 - Return zero for empty AES-CCM updates --- src/we_aes_ccm.c | 9 +++--- test/test_aestag.c | 78 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/we_aes_ccm.c b/src/we_aes_ccm.c index 44839ab6..ea2313aa 100644 --- a/src/we_aes_ccm.c +++ b/src/we_aes_ccm.c @@ -358,7 +358,7 @@ static int we_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, ret = (int)len; } } - else if ((ret == 1) && (len > 0)) { + else if ((ret == 1) && ((len > 0) || (in != NULL))) { if (aes->tagLen == 0) { /* OpenSSL defaults non-TLS CCM tags to 12 bytes. */ aes->tagLen = 12; @@ -425,10 +425,9 @@ static int we_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, OPENSSL_free(aes->aad); aes->aad = NULL; aes->aadLen = 0; - } else if ((ret == 1) && (in == NULL)) { - /* no error, but no input data or AAD to process, return 0 length */ - WOLFENGINE_MSG(WE_LOG_CIPHER, "No input data or AAd to process, " - "returning 0"); + } + else if (ret == 1) { + WOLFENGINE_MSG(WE_LOG_CIPHER, "No input data to process, returning 0"); ret = 0; } diff --git a/test/test_aestag.c b/test/test_aestag.c index b8bed88e..dd7e0cb4 100644 --- a/test/test_aestag.c +++ b/test/test_aestag.c @@ -92,8 +92,8 @@ static int test_aes_tag_enc(ENGINE *e, const EVP_CIPHER *cipher, err = EVP_EncryptUpdate(ctx, NULL, &encLen, aad + 1, (int)strlen((char *)aad) - 1) != 1; } - if (err == 0 && len > 0) { - /* Update with msg, if len > 0 (not GMAC) */ + if (err == 0 && (len > 0 || ccm)) { + /* CCM authenticates empty messages during the payload update. */ err = EVP_EncryptUpdate(ctx, enc, &encLen, msg, len) != 1; if (encLen != len) { err = 1; @@ -166,9 +166,11 @@ static int test_aes_tag_dec(ENGINE *e, const EVP_CIPHER *cipher, err = 1; } } - if (err == 0 && len > 0) { - /* Not used in GMAC test (len == 0) */ + if (err == 0 && (len > 0 || ccm)) { err = EVP_DecryptUpdate(ctx, dec, &decLen, enc, len) != 1; + if (err == 0 && ccm && len == 0) { + err = decLen != 0; + } } else { /* Reset decLen, represented AAD length above */ decLen = 0; @@ -177,7 +179,7 @@ static int test_aes_tag_dec(ENGINE *e, const EVP_CIPHER *cipher, /* Bug in older versions has tag_set cleared and causes failure. */ if (err == 0) { #else - if (err == 0 && (!ccm || len == 0)) { + if (err == 0 && !ccm) { #endif err = EVP_DecryptFinal_ex(ctx, dec + decLen, &decLen) != 1; } @@ -1207,6 +1209,19 @@ static int test_aes_ccm_default_tag_encrypt(ENGINE *e, unsigned char *enc, if (err == 0) { err = EVP_EncryptUpdate(ctx, NULL, &encLen, NULL, sizeof(msg)) != 1; } + if ((err == 0) && (e != NULL)) { + encLen = -1; + err = EVP_EncryptUpdate(ctx, enc, &encLen, msg, 0) != 1; + } + if ((err == 0) && (e != NULL)) { + err = encLen != 0; + } + if ((err == 0) && (e != NULL)) { + err = EVP_EncryptInit_ex(ctx, NULL, e, key, iv) != 1; + } + if ((err == 0) && (e != NULL)) { + err = EVP_EncryptUpdate(ctx, NULL, &encLen, NULL, sizeof(msg)) != 1; + } if (err == 0) { err = EVP_EncryptUpdate(ctx, enc, &encLen, msg, sizeof(msg)) != 1; } @@ -1247,6 +1262,50 @@ static int test_aes_ccm_default_tag(ENGINE *e) return err; } +static int test_aes_ccm_empty(ENGINE *e, const EVP_CIPHER *cipher) +{ + int err; + unsigned char key[32] = {0}; + unsigned char iv[13] = {0}; + unsigned char aad[] = "AAD"; + unsigned char msg[1] = {0}; + unsigned char enc[1] = {0}; + unsigned char dec[1] = {0}; + unsigned char tag[16]; + unsigned char expectedTag[sizeof(tag)]; + + PRINT_MSG("CCM authenticates empty messages with AAD"); + err = test_aes_tag_enc(NULL, cipher, key, iv, sizeof(iv), aad, msg, 0, + enc, expectedTag, 1, 0); + if (err == 0) { + err = test_aes_tag_dec(NULL, cipher, key, iv, sizeof(iv), aad, msg, 0, + enc, expectedTag, dec, 1, 0); + } + if (err == 0) { + expectedTag[0] ^= 1; + err = test_aes_tag_dec(NULL, cipher, key, iv, sizeof(iv), aad, msg, 0, + enc, expectedTag, dec, 1, 0) == 0; + } + if (err == 0) { + err = test_aes_tag_dec(e, cipher, key, iv, sizeof(iv), aad, msg, 0, + enc, expectedTag, dec, 1, 0) == 0; + } + if (err == 0) { + expectedTag[0] ^= 1; + err = test_aes_tag_dec(e, cipher, key, iv, sizeof(iv), aad, msg, 0, + enc, expectedTag, dec, 1, 0); + } + if (err == 0) { + err = test_aes_tag_enc(e, cipher, key, iv, sizeof(iv), aad, msg, 0, + enc, tag, 1, 0); + } + if (err == 0) { + err = XMEMCMP(tag, expectedTag, sizeof(tag)) != 0; + } + + return err; +} + int test_aes128_ccm(ENGINE *e, void *data) { int err = 0; @@ -1256,6 +1315,9 @@ int test_aes128_ccm(ENGINE *e, void *data) if (err == 0) { err = test_aes_ccm_default_tag(e); } + if (err == 0) { + err = test_aes_ccm_empty(e, EVP_aes_128_ccm()); + } #if OPENSSL_VERSION_NUMBER < 0x30000000L /* test with modified length field (L) of 7 */ @@ -1275,6 +1337,9 @@ int test_aes192_ccm(ENGINE *e, void *data) /* test with default length field (L) */ err = test_aes_tag(e, data, EVP_aes_192_ccm(), 24, 13, 1, 0); + if (err == 0) { + err = test_aes_ccm_empty(e, EVP_aes_192_ccm()); + } #if OPENSSL_VERSION_NUMBER < 0x30000000L /* test with modified length field (L) of 7 */ @@ -1294,6 +1359,9 @@ int test_aes256_ccm(ENGINE *e, void *data) /* test with default length field (L) */ err = test_aes_tag(e, data, EVP_aes_256_ccm(), 32, 13, 1, 0); + if (err == 0) { + err = test_aes_ccm_empty(e, EVP_aes_256_ccm()); + } #if OPENSSL_VERSION_NUMBER < 0x30000000L /* test with modified length field (L) of 7 */ From d4e59fbcf3b90a52ae269d08b615f3732827a4a7 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Wed, 14 Oct 2026 15:11:50 -0700 Subject: [PATCH 4/4] F-13038 - Preserve the AES-CCM decrypt nonce --- src/we_aes_ccm.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/we_aes_ccm.c b/src/we_aes_ccm.c index ea2313aa..504d2937 100644 --- a/src/we_aes_ccm.c +++ b/src/we_aes_ccm.c @@ -412,10 +412,6 @@ static int we_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER, "Decrypted %zu bytes " "(AES-CCM):", len); WOLFENGINE_BUFFER(WE_LOG_CIPHER, out, (unsigned int)len); - WOLFENGINE_MSG(WE_LOG_CIPHER, "Caching nonce/IV in aes->iv"); - - /* Cache nonce/IV. */ - XMEMCPY(aes->iv, aes->aes.reg, aes->ivLen); /* Return decrypted data length */ ret = (int)len; }