diff --git a/src/we_aes_ccm.c b/src/we_aes_ccm.c index d5e17ac7..504d2937 100644 --- a/src/we_aes_ccm.c +++ b/src/we_aes_ccm.c @@ -358,10 +358,10 @@ 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) { - /* 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) { @@ -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) { @@ -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; } @@ -425,10 +421,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 d223cc2c..dd7e0cb4 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) @@ -91,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; @@ -165,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; @@ -176,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; } @@ -1179,12 +1182,142 @@ 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) && (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; + } + 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; +} + +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; /* 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 (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 */ @@ -1204,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 */ @@ -1223,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 */