From a195b7cd83710240e406502be21ea6f51d803bf1 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Fri, 20 Nov 2026 11:47:09 -0700 Subject: [PATCH 01/10] Take AES-GCM TLS and IV-gen IVs from the wolfCrypt module counter --- src/wp_aes_aead.c | 90 ++++++++++++++++++++++++++++++++++------------ test/test_aestag.c | 83 ++++++++++++++++++++++++++++++++++++++++++ test/unit.c | 1 + test/unit.h | 1 + 4 files changed, 153 insertions(+), 22 deletions(-) diff --git a/src/wp_aes_aead.c b/src/wp_aes_aead.c index ea1acb58..527e07dd 100644 --- a/src/wp_aes_aead.c +++ b/src/wp_aes_aead.c @@ -86,6 +86,8 @@ typedef struct wp_AeadCtx { unsigned int ivSet:1; /** GCM needs to know if key has been set. */ unsigned int keySet:1; + /** GCM IV was generated by wolfCrypt, which counts each use. */ + unsigned int ivMod:1; /** Cache of authentication status. */ unsigned int authErr:1; /** AAD set with call to update. */ @@ -1061,6 +1063,34 @@ static int wp_aesgcm_iv_inc(wp_AeadCtx* ctx) return ok; } +/* Set up the next IV: wolfCrypt's next counted IV when it generated the IV, + * copied to ctx->iv, else the caller's IV in ctx->iv. Returns 1 on success. */ +static int wp_aesgcm_init_iv(wp_AeadCtx* ctx) +{ + int ok = 1; +#ifdef WOLFSSL_AESGCM_STREAM + int rc; + + if (ctx->ivMod) { + rc = wc_AesGcmEncryptInit_ex(&ctx->aes, NULL, 0, ctx->iv, + (word32)ctx->ivLen); + } else { + rc = wc_AesGcmInit(&ctx->aes, NULL, 0, ctx->iv, (word32)ctx->ivLen); + } + if (rc != 0) { + WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, + "wc_AesGcmEncryptInit_ex/wc_AesGcmInit", rc); + ok = 0; + } +#else + /* wc_AesGcmEncrypt_ex() encrypts with the IV held in aes.reg. */ + if (ctx->ivMod) { + XMEMCPY(ctx->iv, ctx->aes.reg, ctx->ivLen); + } +#endif + return ok; +} + /** * Get the random part of the IV/nonce. * @@ -1088,15 +1118,7 @@ static int wp_aesgcm_get_rand_iv(wp_AeadCtx* ctx, unsigned char* out, ok = 0; } if (ok) { - #ifdef WOLFSSL_AESGCM_STREAM - int rc; - - rc = wc_AesGcmInit(&ctx->aes, NULL, 0, ctx->iv, (word32)ctx->ivLen); - if (rc != 0) { - WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmInit", rc); - ok = 0; - } - #endif + ok = wp_aesgcm_init_iv(ctx); } if (ok) { /* Use all the IV/nonce length if none specified or too much. */ @@ -1107,7 +1129,8 @@ static int wp_aesgcm_get_rand_iv(wp_AeadCtx* ctx, unsigned char* out, #ifndef WOLFSSL_AESGCM_STREAM XMEMCPY(ctx->origIv, ctx->iv, ctx->ivLen); #endif - if (inc && (!wp_aesgcm_iv_inc(ctx))) { + /* wolfCrypt advances its own IV. */ + if (inc && (!ctx->ivMod) && (!wp_aesgcm_iv_inc(ctx))) { ok = 0; } if (ok) { @@ -1176,6 +1199,7 @@ static int wp_aesgcm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv, if (len == (size_t)-1) { XMEMCPY(ctx->iv, iv, ctx->ivLen); ctx->ivGen = 1; + ctx->ivMod = 0; ctx->ivState = IV_STATE_BUFFERED; } else { @@ -1216,6 +1240,7 @@ static int wp_aesgcm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv, if (ok && len > 0) { XMEMCPY(ctx->iv, ctx->aes.reg, ctx->ivLen); ctx->ivSet = 1; + ctx->ivMod = 1; } } else { @@ -1311,6 +1336,10 @@ static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key, ctx->enc = 1; ctx->keySet |= (key != NULL); ctx->tlsAadLen = UNINITIALISED_SIZET; + /* A new key or caller IV replaces a generated IV. */ + if ((key != NULL) || (iv != NULL)) { + ctx->ivMod = 0; + } /* A fresh IV starts a new operation and clears per-operation state. * Without one, a context that already produced output is made terminal * so its IV/nonce cannot be reused. */ @@ -1396,6 +1425,7 @@ static int wp_aesgcm_dinit(wp_AeadCtx *ctx, const unsigned char *key, #endif if (ok) { ctx->enc = 0; + ctx->ivMod = 0; ctx->keySet |= (key != NULL); ctx->tlsAadLen = UNINITIALISED_SIZET; /* A fresh IV starts a new operation and clears per-operation state. @@ -1457,7 +1487,7 @@ static int wp_aesgcm_tls_cipher(wp_AeadCtx* ctx, unsigned char* out, ok = 0; } - if (ok) { + if (ok && (!ctx->ivMod)) { if (ctx->enc) { if (!wp_aesgcm_get_rand_iv(ctx, out, EVP_GCM_TLS_EXPLICIT_IV_LEN, 0)) { @@ -1478,7 +1508,21 @@ static int wp_aesgcm_tls_cipher(wp_AeadCtx* ctx, unsigned char* out, out += EVP_GCM_TLS_EXPLICIT_IV_LEN; len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; - if (ctx->enc) { + if (ctx->ivMod) { + /* wolfCrypt picks the IV; send its invocation field as the + * record's explicit nonce (RFC 5288 3). */ + rc = wc_AesGcmEncrypt_ex(&ctx->aes, out, in, (word32)len, ctx->iv, + (word32)ctx->ivLen, out + len, EVP_GCM_TLS_TAG_LEN, + ctx->buf, EVP_AEAD_TLS1_AAD_LEN); + if (rc != 0) { + WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmEncrypt_ex", rc); + ok = 0; + } else { + XMEMCPY(out - EVP_GCM_TLS_EXPLICIT_IV_LEN, + ctx->iv + ctx->ivLen - EVP_GCM_TLS_EXPLICIT_IV_LEN, + EVP_GCM_TLS_EXPLICIT_IV_LEN); + } + } else if (ctx->enc) { rc = wc_AesGcmEncrypt(&ctx->aes, out, in, (word32)len, ctx->iv, (word32)ctx->ivLen, out + len, EVP_GCM_TLS_TAG_LEN, ctx->buf, EVP_AEAD_TLS1_AAD_LEN); @@ -1562,12 +1606,7 @@ static int wp_aesgcm_stream_update(wp_AeadCtx *ctx, unsigned char *out, if ((!done) && ok) { if (ctx->ivState == IV_STATE_BUFFERED) { - rc = wc_AesGcmInit(&ctx->aes, NULL, 0, ctx->iv, (word32)ctx->ivLen); - if (rc != 0) { - WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmInit", rc); - ok = 0; - } - + ok = wp_aesgcm_init_iv(ctx); ctx->ivState = IV_STATE_COPIED; } } @@ -1780,6 +1819,8 @@ static int wp_aesgcm_encdec(wp_AeadCtx *ctx, unsigned char *out, size_t* outLen, int rc; unsigned char *tmp = NULL; byte *iv = NULL; + /* The first encrypt takes wolfCrypt's IV, written to origIv. */ + int modIv = ctx->ivMod && (offset == 0); WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_encdec"); @@ -1814,11 +1855,16 @@ static int wp_aesgcm_encdec(wp_AeadCtx *ctx, unsigned char *out, size_t* outLen, if (ctx->ivState == IV_STATE_COPIED) { iv = ctx->origIv; } + if (modIv) { + iv = ctx->origIv; + } if (ok) { - rc = wc_AesGcmSetExtIV(&ctx->aes, iv, (word32)ctx->ivLen); - if (rc != 0) { - WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmSetExtIV", rc); - ok = 0; + if (!modIv) { + rc = wc_AesGcmSetExtIV(&ctx->aes, iv, (word32)ctx->ivLen); + if (rc != 0) { + WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmSetExtIV", rc); + ok = 0; + } } if (ok && ctx->ivState == IV_STATE_BUFFERED) { diff --git a/test/test_aestag.c b/test/test_aestag.c index 38214eba..ad3c948c 100644 --- a/test/test_aestag.c +++ b/test/test_aestag.c @@ -1341,6 +1341,89 @@ int test_aes_gcm_tls_multi_record(void *data) /******************************************************************************/ +/* IV-fixed then IV-gen per message on one context: each IV keeps the fixed + * field, differs from every earlier one and decrypts with OpenSSL. */ +int test_aes_gcm_iv_gen_multi(void *data) +{ + int err; + int i; + int j; + int outLen; + unsigned char msg[] = "Test pattern"; + unsigned char aad[] = "AAD"; + unsigned char key[16]; + unsigned char fixed[EVP_GCM_TLS_FIXED_IV_LEN]; + unsigned char iv[8][12]; + unsigned char enc[sizeof(msg)]; + unsigned char tag[AES_BLOCK_SIZE]; + unsigned char dec[sizeof(msg)]; + int numIv = (int)(sizeof(iv) / sizeof(iv[0])); + EVP_CIPHER* ocipher = NULL; + EVP_CIPHER* wcipher = NULL; + EVP_CIPHER_CTX* ctx = NULL; + + (void)data; + + ocipher = EVP_CIPHER_fetch(osslLibCtx, "AES-128-GCM", ""); + wcipher = EVP_CIPHER_fetch(wpLibCtx, "AES-128-GCM", ""); + err = (ocipher == NULL) || (wcipher == NULL); + if (err == 0) { + err = (RAND_bytes(key, (int)sizeof(key)) != 1) || + (RAND_bytes(fixed, (int)sizeof(fixed)) != 1); + } + if (err == 0) { + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + } + if (err == 0) { + err = EVP_EncryptInit(ctx, wcipher, key, NULL) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IV_FIXED, + (int)sizeof(fixed), fixed) != 1; + } + + for (i = 0; (err == 0) && (i < numIv); i++) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_IV_GEN, + (int)sizeof(iv[i]), iv[i]) != 1; + if (err == 0) { + PRINT_BUFFER("IV", iv[i], sizeof(iv[i])); + err = memcmp(iv[i], fixed, sizeof(fixed)) != 0; + } + for (j = 0; (err == 0) && (j < i); j++) { + err = memcmp(iv[i], iv[j], sizeof(iv[i])) == 0; + } + if (err == 0) { + err = EVP_EncryptUpdate(ctx, NULL, &outLen, aad, + (int)strlen((char *)aad)) != 1; + } + if (err == 0) { + err = EVP_EncryptUpdate(ctx, enc, &outLen, msg, + (int)sizeof(msg)) != 1; + } + if (err == 0) { + err = EVP_EncryptFinal_ex(ctx, enc + outLen, &outLen) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, + (int)sizeof(tag), tag) != 1; + } + if (err == 0) { + PRINT_MSG("Decrypt with OpenSSL - message %d", i); + err = test_aes_tag_dec(ocipher, key, iv[i], (int)sizeof(iv[i]), + aad, msg, (int)sizeof(msg), enc, tag, dec, + 0, 0); + } + } + + EVP_CIPHER_CTX_free(ctx); + EVP_CIPHER_free(wcipher); + EVP_CIPHER_free(ocipher); + + return err; +} + +/******************************************************************************/ + /* Test that OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV correctly sets the * explicit/random portion of the IV on the decrypt side via the OSSL_PARAM * interface. This exercises the fix in wp_aead_set_ctx_params where the diff --git a/test/unit.c b/test/unit.c index 453fa72d..7c8f2977 100644 --- a/test/unit.c +++ b/test/unit.c @@ -323,6 +323,7 @@ TEST_CASE test_case[] = { TEST_DECL(test_aes128_gcm_fixed, NULL), TEST_DECL(test_aes128_gcm_tls, NULL), TEST_DECL(test_aes_gcm_tls_multi_record, NULL), + TEST_DECL(test_aes_gcm_iv_gen_multi, NULL), TEST_DECL(test_aes128_gcm_set_iv_inv, NULL), TEST_DECL(test_aes128_gcm_key_then_iv, NULL), TEST_DECL(test_aes_gcm_bad_tag, NULL), diff --git a/test/unit.h b/test/unit.h index 5a0ce806..8614790f 100644 --- a/test/unit.h +++ b/test/unit.h @@ -284,6 +284,7 @@ int test_aes256_gcm(void *data); int test_aes128_gcm_fixed(void *data); int test_aes128_gcm_tls(void *data); int test_aes_gcm_tls_multi_record(void *data); +int test_aes_gcm_iv_gen_multi(void *data); int test_aes128_gcm_set_iv_inv(void *data); int test_aes128_gcm_key_then_iv(void *data); int test_aes_gcm_bad_tag(void *data); From e7a40d09eb148295d3975112cb94aaa358ef1eb8 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Fri, 20 Nov 2026 14:38:14 -0700 Subject: [PATCH 02/10] Finish abandoned GCM streams and drop IV-gen on decrypt init --- src/wp_aes_aead.c | 52 +++++++++++++++++-- test/test_aestag.c | 126 +++++++++++++++++++++++++++++++++++++++++++++ test/unit.c | 1 + test/unit.h | 1 + 4 files changed, 177 insertions(+), 3 deletions(-) diff --git a/src/wp_aes_aead.c b/src/wp_aes_aead.c index 527e07dd..041f28a3 100644 --- a/src/wp_aes_aead.c +++ b/src/wp_aes_aead.c @@ -88,6 +88,8 @@ typedef struct wp_AeadCtx { unsigned int keySet:1; /** GCM IV was generated by wolfCrypt, which counts each use. */ unsigned int ivMod:1; + /** GCM stream holds wolfCrypt's IV and has not been finalized. */ + unsigned int ivModOpen:1; /** Cache of authentication status. */ unsigned int authErr:1; /** AAD set with call to update. */ @@ -1063,17 +1065,47 @@ static int wp_aesgcm_iv_inc(wp_AeadCtx* ctx) return ok; } +/* Finalize a stream left open on wolfCrypt's IV so wolfCrypt moves to its + * next IV; older wolfCrypt advances only in final. Returns 1 on success. */ +static int wp_aesgcm_close_iv(wp_AeadCtx* ctx) +{ + int ok = 1; +#ifdef WOLFSSL_AESGCM_STREAM + int rc; + unsigned char tag[AES_BLOCK_SIZE]; + + if (ctx->ivModOpen) { + rc = wc_AesGcmEncryptFinal(&ctx->aes, tag, (word32)sizeof(tag)); + OPENSSL_cleanse(tag, sizeof(tag)); + if (rc != 0) { + WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, + "wc_AesGcmEncryptFinal", rc); + ok = 0; + } else { + ctx->ivModOpen = 0; + } + } +#else + (void)ctx; +#endif + return ok; +} + /* Set up the next IV: wolfCrypt's next counted IV when it generated the IV, * copied to ctx->iv, else the caller's IV in ctx->iv. Returns 1 on success. */ static int wp_aesgcm_init_iv(wp_AeadCtx* ctx) { int ok = 1; #ifdef WOLFSSL_AESGCM_STREAM - int rc; + int rc = 0; if (ctx->ivMod) { - rc = wc_AesGcmEncryptInit_ex(&ctx->aes, NULL, 0, ctx->iv, - (word32)ctx->ivLen); + ok = wp_aesgcm_close_iv(ctx); + if (ok) { + rc = wc_AesGcmEncryptInit_ex(&ctx->aes, NULL, 0, ctx->iv, + (word32)ctx->ivLen); + ctx->ivModOpen = (rc == 0); + } } else { rc = wc_AesGcmInit(&ctx->aes, NULL, 0, ctx->iv, (word32)ctx->ivLen); } @@ -1200,6 +1232,7 @@ static int wp_aesgcm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv, XMEMCPY(ctx->iv, iv, ctx->ivLen); ctx->ivGen = 1; ctx->ivMod = 0; + ctx->ivModOpen = 0; ctx->ivState = IV_STATE_BUFFERED; } else { @@ -1241,6 +1274,7 @@ static int wp_aesgcm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv, XMEMCPY(ctx->iv, ctx->aes.reg, ctx->ivLen); ctx->ivSet = 1; ctx->ivMod = 1; + ctx->ivModOpen = 0; } } else { @@ -1339,6 +1373,7 @@ static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key, /* A new key or caller IV replaces a generated IV. */ if ((key != NULL) || (iv != NULL)) { ctx->ivMod = 0; + ctx->ivModOpen = 0; } /* A fresh IV starts a new operation and clears per-operation state. * Without one, a context that already produced output is made terminal @@ -1425,7 +1460,12 @@ static int wp_aesgcm_dinit(wp_AeadCtx *ctx, const unsigned char *key, #endif if (ok) { ctx->enc = 0; + /* wolfCrypt's IV is for encryption; IV-gen needs a new fixed IV. */ + if (ctx->ivMod) { + ctx->ivGen = 0; + } ctx->ivMod = 0; + ctx->ivModOpen = 0; ctx->keySet |= (key != NULL); ctx->tlsAadLen = UNINITIALISED_SIZET; /* A fresh IV starts a new operation and clears per-operation state. @@ -1487,6 +1527,9 @@ static int wp_aesgcm_tls_cipher(wp_AeadCtx* ctx, unsigned char* out, ok = 0; } + if (ok && ctx->ivMod && (!wp_aesgcm_close_iv(ctx))) { + ok = 0; + } if (ok && (!ctx->ivMod)) { if (ctx->enc) { if (!wp_aesgcm_get_rand_iv(ctx, out, EVP_GCM_TLS_EXPLICIT_IV_LEN, @@ -1697,6 +1740,9 @@ static int wp_aesgcm_stream_final(wp_AeadCtx *ctx, unsigned char *out, if (ctx->enc) { rc = wc_AesGcmEncryptFinal(aes, ctx->buf, sizeof(ctx->buf)); + if (rc == 0) { + ctx->ivModOpen = 0; + } } else { rc = wc_AesGcmDecryptFinal(aes, ctx->buf, (word32)ctx->tagLen); diff --git a/test/test_aestag.c b/test/test_aestag.c index ad3c948c..5c992088 100644 --- a/test/test_aestag.c +++ b/test/test_aestag.c @@ -1424,6 +1424,132 @@ int test_aes_gcm_iv_gen_multi(void *data) /******************************************************************************/ +/* An IV from IV-gen is not handed out again after an abandoned message or a + * switch to decrypt and back; after the switch the call may fail instead. */ +int test_aes_gcm_iv_gen_reuse(void *data) +{ + int err; + int i; + int outLen; + unsigned char msg[] = "Test pattern"; + unsigned char aad[] = "AAD"; + unsigned char tlsAad[EVP_AEAD_TLS1_AAD_LEN] = {0,}; + unsigned char key[16]; + unsigned char fixed[EVP_GCM_TLS_FIXED_IV_LEN]; + unsigned char iv[3][12]; + unsigned char rec[48]; + unsigned char enc[sizeof(msg)]; + unsigned char tag[AES_BLOCK_SIZE]; + unsigned char dec[sizeof(msg)]; + EVP_CIPHER* ocipher = NULL; + EVP_CIPHER* wcipher = NULL; + EVP_CIPHER_CTX* ctx = NULL; + + (void)data; + + tlsAad[8] = 23; + tlsAad[9] = 3; + tlsAad[10] = 3; + tlsAad[12] = sizeof(rec) - EVP_GCM_TLS_TAG_LEN; + + ocipher = EVP_CIPHER_fetch(osslLibCtx, "AES-128-GCM", ""); + wcipher = EVP_CIPHER_fetch(wpLibCtx, "AES-128-GCM", ""); + err = (ocipher == NULL) || (wcipher == NULL); + if (err == 0) { + err = (RAND_bytes(key, (int)sizeof(key)) != 1) || + (RAND_bytes(fixed, (int)sizeof(fixed)) != 1); + } + if (err == 0) { + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + } + if (err == 0) { + err = EVP_EncryptInit(ctx, wcipher, key, NULL) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IV_FIXED, + (int)sizeof(fixed), fixed) != 1; + } + + /* Message 0 produces ciphertext and is abandoned before final. */ + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_IV_GEN, + (int)sizeof(iv[0]), iv[0]) != 1; + } + if (err == 0) { + err = EVP_EncryptUpdate(ctx, enc, &outLen, msg, (int)sizeof(msg)) != 1; + } + if (err == 0) { + err = EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL) != 1; + } + + /* Message 1 needs a new IV and still decrypts. */ + if (err == 0) { + PRINT_MSG("IV-gen after an abandoned message"); + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_IV_GEN, + (int)sizeof(iv[1]), iv[1]) != 1; + } + if (err == 0) { + PRINT_BUFFER("IV 0", iv[0], sizeof(iv[0])); + PRINT_BUFFER("IV 1", iv[1], sizeof(iv[1])); + err = memcmp(iv[0], iv[1], sizeof(iv[0])) == 0; + } + if (err == 0) { + err = EVP_EncryptUpdate(ctx, NULL, &outLen, aad, + (int)strlen((char *)aad)) != 1; + } + if (err == 0) { + err = EVP_EncryptUpdate(ctx, enc, &outLen, msg, (int)sizeof(msg)) != 1; + } + if (err == 0) { + err = EVP_EncryptFinal_ex(ctx, enc + outLen, &outLen) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, + (int)sizeof(tag), tag) != 1; + } + if (err == 0) { + err = test_aes_tag_dec(ocipher, key, iv[1], (int)sizeof(iv[1]), aad, + msg, (int)sizeof(msg), enc, tag, dec, 0, 0); + } + + /* Switch to decrypt and back with no new key or IV. */ + if (err == 0) { + PRINT_MSG("IV-gen and TLS record after decrypt init"); + err = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 0) != 1; + } + if (err == 0) { + err = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 1) != 1; + } + if ((err == 0) && (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_IV_GEN, + (int)sizeof(iv[2]), iv[2]) == 1)) { + PRINT_BUFFER("IV 2", iv[2], sizeof(iv[2])); + for (i = 0; (err == 0) && (i < 2); i++) { + err = memcmp(iv[2], iv[i], sizeof(iv[2])) == 0; + } + } + if (err == 0) { + memset(rec, 0, sizeof(rec)); + memcpy(rec + EVP_GCM_TLS_EXPLICIT_IV_LEN, msg, sizeof(msg)); + } + if ((err == 0) && (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + EVP_AEAD_TLS1_AAD_LEN, tlsAad) == EVP_GCM_TLS_TAG_LEN) && + (EVP_CipherUpdate(ctx, rec, &outLen, rec, (int)sizeof(rec)) == 1)) { + PRINT_BUFFER("Explicit IV", rec, EVP_GCM_TLS_EXPLICIT_IV_LEN); + for (i = 0; (err == 0) && (i < 2); i++) { + err = memcmp(rec, iv[i] + EVP_GCM_TLS_FIXED_IV_LEN, + EVP_GCM_TLS_EXPLICIT_IV_LEN) == 0; + } + } + + EVP_CIPHER_CTX_free(ctx); + EVP_CIPHER_free(wcipher); + EVP_CIPHER_free(ocipher); + + return err; +} + +/******************************************************************************/ + /* Test that OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV correctly sets the * explicit/random portion of the IV on the decrypt side via the OSSL_PARAM * interface. This exercises the fix in wp_aead_set_ctx_params where the diff --git a/test/unit.c b/test/unit.c index 7c8f2977..00649dd3 100644 --- a/test/unit.c +++ b/test/unit.c @@ -324,6 +324,7 @@ TEST_CASE test_case[] = { TEST_DECL(test_aes128_gcm_tls, NULL), TEST_DECL(test_aes_gcm_tls_multi_record, NULL), TEST_DECL(test_aes_gcm_iv_gen_multi, NULL), + TEST_DECL(test_aes_gcm_iv_gen_reuse, NULL), TEST_DECL(test_aes128_gcm_set_iv_inv, NULL), TEST_DECL(test_aes128_gcm_key_then_iv, NULL), TEST_DECL(test_aes_gcm_bad_tag, NULL), diff --git a/test/unit.h b/test/unit.h index 8614790f..fc19b73e 100644 --- a/test/unit.h +++ b/test/unit.h @@ -285,6 +285,7 @@ int test_aes128_gcm_fixed(void *data); int test_aes128_gcm_tls(void *data); int test_aes_gcm_tls_multi_record(void *data); int test_aes_gcm_iv_gen_multi(void *data); +int test_aes_gcm_iv_gen_reuse(void *data); int test_aes128_gcm_set_iv_inv(void *data); int test_aes128_gcm_key_then_iv(void *data); int test_aes_gcm_bad_tag(void *data); From e8d35d5c3b7a50b64453ae41785967cfdf429cb2 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Fri, 20 Nov 2026 15:27:36 -0700 Subject: [PATCH 03/10] Add AES-GCM module IV counter entry to ChangeLog --- ChangeLog.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 562ee9e2..ac4fd6e7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,15 @@ +# wolfProvider Unreleased + +## Enhancements and Optimizations +* Take AES-GCM IVs for TLS 1.2 records and EVP IV generation + (EVP_CTRL_GCM_SET_IV_FIXED with EVP_CTRL_GCM_IV_GEN) from the wolfCrypt + module's own counter instead of advancing them in the provider (FIPS 140-3 + IG C.H). The TLS explicit nonce on the wire is unchanged. Two IV_GEN calls in + a row now return distinct IVs, an unfinished encrypt stream is closed before + the next IV is handed out, and a decrypt init on a context with a generated + IV requires a new SET_IV_FIXED (and on FIPS v7 a new key) before encrypting + again (PR 531). + # wolfProvider version 1.2.2 (August 27, 2026) Release 1.2.2 has been developed according to wolfSSL's development and QA From 8035c8ed0d3e1ae31e9735bdaa29aaadb21dd141 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Sat, 21 Nov 2026 06:54:58 -0700 Subject: [PATCH 04/10] Unlock private key reads around RSA, ECC and ECX private key DER encoding --- src/wp_ecc_kmgmt.c | 2 ++ src/wp_ecx_kmgmt.c | 2 ++ src/wp_rsa_kmgmt.c | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/src/wp_ecc_kmgmt.c b/src/wp_ecc_kmgmt.c index e6a7761b..494ff23f 100644 --- a/src/wp_ecc_kmgmt.c +++ b/src/wp_ecc_kmgmt.c @@ -2856,7 +2856,9 @@ static int wp_ecc_encode_priv(const wp_Ecc *ecc, unsigned char* keyData, WOLFPROV_ENTER(WP_LOG_COMP_ECC, "wp_ecc_encode_priv"); + PRIVATE_KEY_UNLOCK(); rc = wc_EccKeyToDer((ecc_key*)&ecc->key, keyData, len); + PRIVATE_KEY_LOCK(); if (rc <= 0) { ok = 0; } diff --git a/src/wp_ecx_kmgmt.c b/src/wp_ecx_kmgmt.c index 99947824..19e58bda 100644 --- a/src/wp_ecx_kmgmt.c +++ b/src/wp_ecx_kmgmt.c @@ -2412,7 +2412,9 @@ static int wp_ecx_encode(wp_EcxEncDecCtx* ctx, OSSL_CORE_BIO *cBio, } if (ok) { + PRIVATE_KEY_UNLOCK(); rc = ctx->encode((void*)&ecx->key, derData, sizeof(derData)); + PRIVATE_KEY_LOCK(); if (rc <= 0) { ok = 0; } diff --git a/src/wp_rsa_kmgmt.c b/src/wp_rsa_kmgmt.c index 73baff32..40d38c62 100644 --- a/src/wp_rsa_kmgmt.c +++ b/src/wp_rsa_kmgmt.c @@ -3443,7 +3443,9 @@ static int wp_rsa_encode_pki(const wp_Rsa* rsa, unsigned char* keyData, } } if (ok) { + PRIVATE_KEY_UNLOCK(); ret = wc_RsaKeyToDer((RsaKey*)&rsa->key, pkcs1Data, (word32)pkcs1Len); + PRIVATE_KEY_LOCK(); if (ret <= 0) { ok = 0; } @@ -3537,7 +3539,9 @@ static int wp_rsa_encode_priv(const wp_Rsa* rsa, unsigned char* keyData, WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_encode_priv"); + PRIVATE_KEY_UNLOCK(); ret = wc_RsaKeyToDer((RsaKey*)&rsa->key, keyData, (word32)*keyLen); + PRIVATE_KEY_LOCK(); if (ret <= 0) { ok = 0; } From 38de7bfa20566baf45f419361e783b3b2880ccc5 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Sat, 21 Nov 2026 07:33:25 -0700 Subject: [PATCH 05/10] Keep the module GCM IV sequence when only the key is set again --- src/wp_aes_aead.c | 16 ++++++++++++++-- test/test_aestag.c | 47 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/wp_aes_aead.c b/src/wp_aes_aead.c index 041f28a3..55a58ceb 100644 --- a/src/wp_aes_aead.c +++ b/src/wp_aes_aead.c @@ -1308,6 +1308,8 @@ static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key, { Aes *aes = &ctx->aes; int ok = 1; + int keepIv; + byte ivNext[AES_BLOCK_SIZE]; WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_einit"); @@ -1317,6 +1319,13 @@ static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key, if (ok) { WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_AES); } + /* A new key alone keeps wolfCrypt's IV sequence; older wolfCrypt clears + * aes.reg when the key is set, so the next IV is put back after. */ + keepIv = ok && (key != NULL) && (iv == NULL) && ctx->ivMod; + if (keepIv) { + ok = wp_aesgcm_close_iv(ctx); + XMEMCPY(ivNext, aes->reg, ctx->ivLen); + } #ifdef WOLFSSL_AESGCM_STREAM if (ok) { int rc; @@ -1366,12 +1375,15 @@ static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key, } } #endif + if (ok && keepIv) { + XMEMCPY(aes->reg, ivNext, ctx->ivLen); + } if (ok) { ctx->enc = 1; ctx->keySet |= (key != NULL); ctx->tlsAadLen = UNINITIALISED_SIZET; - /* A new key or caller IV replaces a generated IV. */ - if ((key != NULL) || (iv != NULL)) { + /* A caller IV replaces a generated IV. */ + if (iv != NULL) { ctx->ivMod = 0; ctx->ivModOpen = 0; } diff --git a/test/test_aestag.c b/test/test_aestag.c index 5c992088..2d24514e 100644 --- a/test/test_aestag.c +++ b/test/test_aestag.c @@ -1424,8 +1424,8 @@ int test_aes_gcm_iv_gen_multi(void *data) /******************************************************************************/ -/* An IV from IV-gen is not handed out again after an abandoned message or a - * switch to decrypt and back; after the switch the call may fail instead. */ +/* An IV from IV-gen is not handed out again after an abandoned message, the + * same key set again, or a switch to decrypt and back (which may fail). */ int test_aes_gcm_iv_gen_reuse(void *data) { int err; @@ -1436,7 +1436,7 @@ int test_aes_gcm_iv_gen_reuse(void *data) unsigned char tlsAad[EVP_AEAD_TLS1_AAD_LEN] = {0,}; unsigned char key[16]; unsigned char fixed[EVP_GCM_TLS_FIXED_IV_LEN]; - unsigned char iv[3][12]; + unsigned char iv[4][12]; unsigned char rec[48]; unsigned char enc[sizeof(msg)]; unsigned char tag[AES_BLOCK_SIZE]; @@ -1512,6 +1512,37 @@ int test_aes_gcm_iv_gen_reuse(void *data) msg, (int)sizeof(msg), enc, tag, dec, 0, 0); } + /* Message 2 after the same key is set again needs a new IV. */ + if (err == 0) { + PRINT_MSG("IV-gen after the same key is set again"); + err = EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_IV_GEN, + (int)sizeof(iv[2]), iv[2]) != 1; + } + for (i = 0; (err == 0) && (i < 2); i++) { + err = memcmp(iv[2], iv[i], sizeof(iv[2])) == 0; + } + if (err == 0) { + err = EVP_EncryptUpdate(ctx, NULL, &outLen, aad, + (int)strlen((char *)aad)) != 1; + } + if (err == 0) { + err = EVP_EncryptUpdate(ctx, enc, &outLen, msg, (int)sizeof(msg)) != 1; + } + if (err == 0) { + err = EVP_EncryptFinal_ex(ctx, enc + outLen, &outLen) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, + (int)sizeof(tag), tag) != 1; + } + if (err == 0) { + err = test_aes_tag_dec(ocipher, key, iv[2], (int)sizeof(iv[2]), aad, + msg, (int)sizeof(msg), enc, tag, dec, 0, 0); + } + /* Switch to decrypt and back with no new key or IV. */ if (err == 0) { PRINT_MSG("IV-gen and TLS record after decrypt init"); @@ -1521,10 +1552,10 @@ int test_aes_gcm_iv_gen_reuse(void *data) err = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 1) != 1; } if ((err == 0) && (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_IV_GEN, - (int)sizeof(iv[2]), iv[2]) == 1)) { - PRINT_BUFFER("IV 2", iv[2], sizeof(iv[2])); - for (i = 0; (err == 0) && (i < 2); i++) { - err = memcmp(iv[2], iv[i], sizeof(iv[2])) == 0; + (int)sizeof(iv[3]), iv[3]) == 1)) { + PRINT_BUFFER("IV 3", iv[3], sizeof(iv[3])); + for (i = 0; (err == 0) && (i < 3); i++) { + err = memcmp(iv[3], iv[i], sizeof(iv[3])) == 0; } } if (err == 0) { @@ -1535,7 +1566,7 @@ int test_aes_gcm_iv_gen_reuse(void *data) EVP_AEAD_TLS1_AAD_LEN, tlsAad) == EVP_GCM_TLS_TAG_LEN) && (EVP_CipherUpdate(ctx, rec, &outLen, rec, (int)sizeof(rec)) == 1)) { PRINT_BUFFER("Explicit IV", rec, EVP_GCM_TLS_EXPLICIT_IV_LEN); - for (i = 0; (err == 0) && (i < 2); i++) { + for (i = 0; (err == 0) && (i < 3); i++) { err = memcmp(rec, iv[i] + EVP_GCM_TLS_FIXED_IV_LEN, EVP_GCM_TLS_EXPLICIT_IV_LEN) == 0; } From 06dfe298049154e1d5cfea314fedaa401564e228 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Sun, 22 Nov 2026 16:44:59 -0700 Subject: [PATCH 06/10] Guard the X25519 KAT vectors when curve25519 blinding is on --- test/test_ecc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_ecc.c b/test/test_ecc.c index 87003a73..b1cfeaac 100644 --- a/test/test_ecc.c +++ b/test/test_ecc.c @@ -1507,6 +1507,7 @@ int test_ecdh_p521(void *data) #endif /* WP_HAVE_EC_P521 */ #ifdef WP_HAVE_X25519 +#ifndef WOLFSSL_CURVE25519_BLINDING /* RFC 7748 Section 6.1 X25519 test vectors. */ static const unsigned char x25519_alice_priv[] = { 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, @@ -1526,6 +1527,7 @@ static const unsigned char x25519_shared_secret[] = { 0xe0, 0x7e, 0x21, 0xc9, 0x47, 0xd1, 0x9e, 0x33, 0x76, 0xf0, 0x9b, 0x3c, 0x1e, 0x16, 0x17, 0x42 }; +#endif /* * X25519 known-answer ECDH test using RFC 7748 Section 6.1 vectors. From 2ec40fd71d75a76e8f49e2788b97a634ba307a32 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Sun, 22 Nov 2026 16:44:59 -0700 Subject: [PATCH 07/10] Expect FIPS v7 refusals in the RSA, ECDSA and X25519 tests --- test/test_ecc.c | 47 ++++++++++++++++++++++++++++++++++------------- test/test_rsa.c | 19 ++++++++++++------- test/unit.h | 6 ++++++ 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/test/test_ecc.c b/test/test_ecc.c index b1cfeaac..c40fab1e 100644 --- a/test/test_ecc.c +++ b/test/test_ecc.c @@ -33,6 +33,9 @@ #ifndef WC_MIN_DIGEST_SIZE #define WC_MIN_DIGEST_SIZE 20 #endif +#ifndef WC_MIN_DIGEST_SIZE_FOR_SIGN +#define WC_MIN_DIGEST_SIZE_FOR_SIGN WC_MIN_DIGEST_SIZE +#endif #ifdef WP_HAVE_ECC @@ -879,7 +882,13 @@ int test_ecdh_x25519_keygen(void *data) { (void)data; +#ifdef WP_TEST_FIPS_V7 + /* FIPS v7 allows X25519 only inside X25519MLKEM768. */ + PRINT_MSG("FIPS v7 refuses a standalone X25519 derive."); + return test_ecdh_keygen(NID_undef, "X25519", 32) == 0; +#else return test_ecdh_keygen(NID_undef, "X25519", 32); +#endif } #endif /* WP_HAVE_X25519 */ @@ -1883,6 +1892,10 @@ int test_ecdsa_p192_pkey(void *data) PRINT_MSG("Verify with wolfprovider"); err = test_pkey_verify_ecc(pkey, wpLibCtx, buf, sizeof(buf), ecdsaSig, ecdsaSigLen); +#ifdef WP_TEST_FIPS_V7 + PRINT_MSG("FIPS v7 refuses P-192 verify."); + err = err == 0; +#endif } if (err == 0) { PRINT_MSG("Verify bad signature with wolfprovider"); @@ -1930,7 +1943,7 @@ int test_ecdsa_p224_pkey(void *data) EVP_PKEY *pkey = NULL; unsigned char ecdsaSig[64]; size_t ecdsaSigLen; - unsigned char buf[20]; + unsigned char buf[32]; const unsigned char *p = ecc_key_der_224; (void)data; @@ -1985,7 +1998,7 @@ int test_ecdsa_p256_pkey(void *data) EVP_PKEY *pkey = NULL; unsigned char ecdsaSig[80]; size_t ecdsaSigLen; - unsigned char buf[20]; + unsigned char buf[32]; const unsigned char *p = ecc_key_der_256; (void)data; @@ -2034,13 +2047,16 @@ int test_ecdsa_p256_pkey(void *data) /* Raw EVP_PKEY_verify must reject inputs below WC_MIN_DIGEST_SIZE. */ int test_ecdsa_verify_undersized_hash(void *data) { - /* Boundaries track WC_MIN_DIGEST_SIZE so the assertions stay valid for - * whatever hash set the wolfSSL build enabled (typically 20 for SHA-1, - * but 16 when MD5 is on or 28+ in FIPS 186-5 builds). */ + /* Verify tracks WC_MIN_DIGEST_SIZE (20, or 16 with MD5). Sign tracks + * WC_MIN_DIGEST_SIZE_FOR_SIGN, which FIPS 186-5 builds raise to 28. */ static const size_t sizes[] = { 0, WC_MIN_DIGEST_SIZE - 1, WC_MIN_DIGEST_SIZE, WC_MAX_DIGEST_SIZE }; + static const size_t signSizes[] = { 0, + WC_MIN_DIGEST_SIZE_FOR_SIGN - 1, + WC_MIN_DIGEST_SIZE_FOR_SIGN, + WC_MAX_DIGEST_SIZE }; static const int expectFail[] = { 1, 1, 0, 0 }; int err; size_t i; @@ -2070,14 +2086,15 @@ int test_ecdsa_verify_undersized_hash(void *data) } } - /* Same minimum applies on the sign side: wolfProvider must refuse to - * produce a raw signature it would later refuse to verify. */ - for (i = 0; (err == 0) && (i < sizeof(sizes)/sizeof(sizes[0])); i++) { + /* Sign side: wolfProvider must refuse a raw input below the sign + * minimum. */ + for (i = 0; (err == 0) && (i < sizeof(signSizes)/sizeof(signSizes[0])); + i++) { int signErr; - PRINT_MSG("sign tbsLen=%zu", sizes[i]); + PRINT_MSG("sign tbsLen=%zu", signSizes[i]); ecdsaSigLen = sizeof(ecdsaSig); - signErr = test_pkey_sign_ecc(pkey, wpLibCtx, buf, sizes[i], ecdsaSig, - &ecdsaSigLen); + signErr = test_pkey_sign_ecc(pkey, wpLibCtx, buf, signSizes[i], + ecdsaSig, &ecdsaSigLen); err = (signErr != 0) != expectFail[i]; } @@ -2182,7 +2199,7 @@ int test_ecdsa_p384_pkey(void *data) EVP_PKEY *pkey = NULL; unsigned char ecdsaSig[120]; size_t ecdsaSigLen; - unsigned char buf[20]; + unsigned char buf[32]; const unsigned char *p = ecc_key_der_384; (void)data; @@ -2237,7 +2254,7 @@ int test_ecdsa_p521_pkey(void *data) EVP_PKEY *pkey = NULL; unsigned char ecdsaSig[163]; size_t ecdsaSigLen; - unsigned char buf[20]; + unsigned char buf[32]; const unsigned char *p = ecc_key_der_521; (void)data; @@ -2317,6 +2334,10 @@ int test_ecdsa_p192(void *data) PRINT_MSG("Verify with wolfprovider"); err = test_digest_verify(pkey, wpLibCtx, buf, sizeof(buf), md, NULL, ecdsaSig, ecdsaSigLen, 0, 0); +#ifdef WP_TEST_FIPS_V7 + PRINT_MSG("FIPS v7 refuses P-192 verify."); + err = err == 0; +#endif } if (err == 0) { PRINT_MSG("Verify bad signature with wolfprovider"); diff --git a/test/test_rsa.c b/test/test_rsa.c index 8886afe6..925bd50f 100644 --- a/test/test_rsa.c +++ b/test/test_rsa.c @@ -905,7 +905,9 @@ int test_rsa_sign_verify_pss(void *data) EVP_sha512(), 0) == 1; } #endif -#if defined(WP_HAVE_SHA384) && defined(WP_HAVE_SHA512) +/* FIPS v7 refuses a PSS MGF1 hash that differs from the message hash. */ +#if defined(WP_HAVE_SHA384) && defined(WP_HAVE_SHA512) && \ + !defined(WP_TEST_FIPS_V7) if (err == 0) { /* Use SHA-384 for MD and SHA-512 for MGF1 MD. */ err = test_rsa_sign_verify_pad(RSA_PKCS1_PSS_PADDING, EVP_sha384(), @@ -917,8 +919,7 @@ int test_rsa_sign_verify_pss(void *data) } /* wolfSSL caps the PSS salt at the hash length only for FIPS v7+ modules. */ -#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION_MAJOR) && \ - (HAVE_FIPS_VERSION_MAJOR >= 7) +#ifdef WP_TEST_FIPS_V7 #define WP_TEST_PSS_SALT_MAY_CAP #endif @@ -1696,7 +1697,7 @@ int test_rsa_enc_dec_pkcs1(void *data) } if (err == 0) { PRINT_MSG("Check that private decrypt works with valid key size."); - err = test_rsa_enc_dec(rsa_key_der_1024, sizeof(rsa_key_der_1024), + err = test_rsa_enc_dec(rsa_key_der_2048, sizeof(rsa_key_der_2048), RSA_PKCS1_PADDING, NULL, NULL); } @@ -1730,12 +1731,12 @@ int test_rsa_enc_dec_oaep(void *data) (void)data; /* Use SHA-1 for MD and MGF1 MD. */ - err = test_rsa_enc_dec(rsa_key_der_1024, sizeof(rsa_key_der_1024), + err = test_rsa_enc_dec(rsa_key_der_2048, sizeof(rsa_key_der_2048), RSA_PKCS1_OAEP_PADDING, EVP_sha1(), EVP_sha1()) == 1; #ifdef WP_HAVE_SHA256 if (err == 0) { /* Use SHA-256 for MD and MGF1 MD. */ - err = test_rsa_enc_dec(rsa_key_der_1024, sizeof(rsa_key_der_1024), + err = test_rsa_enc_dec(rsa_key_der_2048, sizeof(rsa_key_der_2048), RSA_PKCS1_OAEP_PADDING, EVP_sha256(), EVP_sha256()) == 1; } @@ -1743,7 +1744,7 @@ int test_rsa_enc_dec_oaep(void *data) #ifdef WP_HAVE_SHA384 if (err == 0) { /* Use SHA-384 for MD and SHA-512 for MGF1 MD. */ - err = test_rsa_enc_dec(rsa_key_der_1024, sizeof(rsa_key_der_1024), + err = test_rsa_enc_dec(rsa_key_der_2048, sizeof(rsa_key_der_2048), RSA_PKCS1_OAEP_PADDING, EVP_sha384(), EVP_sha512()) == 1; } @@ -2030,6 +2031,10 @@ int test_rsa_pkey_invalid_key_size(void *data) { if (err == 0) { err = test_pkey_verify(pkey, wpLibCtx, buf, sizeof(buf), rsaSig, rsaSigLen, 0, NULL, NULL); +#ifdef WP_TEST_FIPS_V7 + PRINT_MSG("FIPS v7 refuses to verify with a 1024-bit key."); + err = err == 0; +#endif } #endif /* HAVE_FIPS || HAVE_FIPS_VERSION */ diff --git a/test/unit.h b/test/unit.h index fc19b73e..0748d879 100644 --- a/test/unit.h +++ b/test/unit.h @@ -59,6 +59,12 @@ #define WP_HAVE_EPKI_TEST #endif +/* FIPS v7 modules: tests expect the v7 refusals. */ +#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION_MAJOR) && \ + (HAVE_FIPS_VERSION_MAJOR >= 7) + #define WP_TEST_FIPS_V7 +#endif + /* test_slhdsa.c selects a representative set and falls back to any enabled * set when a custom wolfSSL build enables only one parameter set. */ #if defined(WP_HAVE_SLH_DSA_SHA2_128F) || \ From cf16309385939c83fb76349c2e3a6f9eda281ab9 Mon Sep 17 00:00:00 2001 From: Colton Willey Date: Tue, 1 Dec 2026 14:00:10 -0800 Subject: [PATCH 08/10] Revert "Expect FIPS v7 refusals in the RSA, ECDSA and X25519 tests" This reverts commit 2ec40fd71d75a76e8f49e2788b97a634ba307a32. --- test/test_ecc.c | 47 +++++++++++++---------------------------------- test/test_rsa.c | 19 +++++++------------ test/unit.h | 6 ------ 3 files changed, 20 insertions(+), 52 deletions(-) diff --git a/test/test_ecc.c b/test/test_ecc.c index c40fab1e..b1cfeaac 100644 --- a/test/test_ecc.c +++ b/test/test_ecc.c @@ -33,9 +33,6 @@ #ifndef WC_MIN_DIGEST_SIZE #define WC_MIN_DIGEST_SIZE 20 #endif -#ifndef WC_MIN_DIGEST_SIZE_FOR_SIGN -#define WC_MIN_DIGEST_SIZE_FOR_SIGN WC_MIN_DIGEST_SIZE -#endif #ifdef WP_HAVE_ECC @@ -882,13 +879,7 @@ int test_ecdh_x25519_keygen(void *data) { (void)data; -#ifdef WP_TEST_FIPS_V7 - /* FIPS v7 allows X25519 only inside X25519MLKEM768. */ - PRINT_MSG("FIPS v7 refuses a standalone X25519 derive."); - return test_ecdh_keygen(NID_undef, "X25519", 32) == 0; -#else return test_ecdh_keygen(NID_undef, "X25519", 32); -#endif } #endif /* WP_HAVE_X25519 */ @@ -1892,10 +1883,6 @@ int test_ecdsa_p192_pkey(void *data) PRINT_MSG("Verify with wolfprovider"); err = test_pkey_verify_ecc(pkey, wpLibCtx, buf, sizeof(buf), ecdsaSig, ecdsaSigLen); -#ifdef WP_TEST_FIPS_V7 - PRINT_MSG("FIPS v7 refuses P-192 verify."); - err = err == 0; -#endif } if (err == 0) { PRINT_MSG("Verify bad signature with wolfprovider"); @@ -1943,7 +1930,7 @@ int test_ecdsa_p224_pkey(void *data) EVP_PKEY *pkey = NULL; unsigned char ecdsaSig[64]; size_t ecdsaSigLen; - unsigned char buf[32]; + unsigned char buf[20]; const unsigned char *p = ecc_key_der_224; (void)data; @@ -1998,7 +1985,7 @@ int test_ecdsa_p256_pkey(void *data) EVP_PKEY *pkey = NULL; unsigned char ecdsaSig[80]; size_t ecdsaSigLen; - unsigned char buf[32]; + unsigned char buf[20]; const unsigned char *p = ecc_key_der_256; (void)data; @@ -2047,16 +2034,13 @@ int test_ecdsa_p256_pkey(void *data) /* Raw EVP_PKEY_verify must reject inputs below WC_MIN_DIGEST_SIZE. */ int test_ecdsa_verify_undersized_hash(void *data) { - /* Verify tracks WC_MIN_DIGEST_SIZE (20, or 16 with MD5). Sign tracks - * WC_MIN_DIGEST_SIZE_FOR_SIGN, which FIPS 186-5 builds raise to 28. */ + /* Boundaries track WC_MIN_DIGEST_SIZE so the assertions stay valid for + * whatever hash set the wolfSSL build enabled (typically 20 for SHA-1, + * but 16 when MD5 is on or 28+ in FIPS 186-5 builds). */ static const size_t sizes[] = { 0, WC_MIN_DIGEST_SIZE - 1, WC_MIN_DIGEST_SIZE, WC_MAX_DIGEST_SIZE }; - static const size_t signSizes[] = { 0, - WC_MIN_DIGEST_SIZE_FOR_SIGN - 1, - WC_MIN_DIGEST_SIZE_FOR_SIGN, - WC_MAX_DIGEST_SIZE }; static const int expectFail[] = { 1, 1, 0, 0 }; int err; size_t i; @@ -2086,15 +2070,14 @@ int test_ecdsa_verify_undersized_hash(void *data) } } - /* Sign side: wolfProvider must refuse a raw input below the sign - * minimum. */ - for (i = 0; (err == 0) && (i < sizeof(signSizes)/sizeof(signSizes[0])); - i++) { + /* Same minimum applies on the sign side: wolfProvider must refuse to + * produce a raw signature it would later refuse to verify. */ + for (i = 0; (err == 0) && (i < sizeof(sizes)/sizeof(sizes[0])); i++) { int signErr; - PRINT_MSG("sign tbsLen=%zu", signSizes[i]); + PRINT_MSG("sign tbsLen=%zu", sizes[i]); ecdsaSigLen = sizeof(ecdsaSig); - signErr = test_pkey_sign_ecc(pkey, wpLibCtx, buf, signSizes[i], - ecdsaSig, &ecdsaSigLen); + signErr = test_pkey_sign_ecc(pkey, wpLibCtx, buf, sizes[i], ecdsaSig, + &ecdsaSigLen); err = (signErr != 0) != expectFail[i]; } @@ -2199,7 +2182,7 @@ int test_ecdsa_p384_pkey(void *data) EVP_PKEY *pkey = NULL; unsigned char ecdsaSig[120]; size_t ecdsaSigLen; - unsigned char buf[32]; + unsigned char buf[20]; const unsigned char *p = ecc_key_der_384; (void)data; @@ -2254,7 +2237,7 @@ int test_ecdsa_p521_pkey(void *data) EVP_PKEY *pkey = NULL; unsigned char ecdsaSig[163]; size_t ecdsaSigLen; - unsigned char buf[32]; + unsigned char buf[20]; const unsigned char *p = ecc_key_der_521; (void)data; @@ -2334,10 +2317,6 @@ int test_ecdsa_p192(void *data) PRINT_MSG("Verify with wolfprovider"); err = test_digest_verify(pkey, wpLibCtx, buf, sizeof(buf), md, NULL, ecdsaSig, ecdsaSigLen, 0, 0); -#ifdef WP_TEST_FIPS_V7 - PRINT_MSG("FIPS v7 refuses P-192 verify."); - err = err == 0; -#endif } if (err == 0) { PRINT_MSG("Verify bad signature with wolfprovider"); diff --git a/test/test_rsa.c b/test/test_rsa.c index 925bd50f..8886afe6 100644 --- a/test/test_rsa.c +++ b/test/test_rsa.c @@ -905,9 +905,7 @@ int test_rsa_sign_verify_pss(void *data) EVP_sha512(), 0) == 1; } #endif -/* FIPS v7 refuses a PSS MGF1 hash that differs from the message hash. */ -#if defined(WP_HAVE_SHA384) && defined(WP_HAVE_SHA512) && \ - !defined(WP_TEST_FIPS_V7) +#if defined(WP_HAVE_SHA384) && defined(WP_HAVE_SHA512) if (err == 0) { /* Use SHA-384 for MD and SHA-512 for MGF1 MD. */ err = test_rsa_sign_verify_pad(RSA_PKCS1_PSS_PADDING, EVP_sha384(), @@ -919,7 +917,8 @@ int test_rsa_sign_verify_pss(void *data) } /* wolfSSL caps the PSS salt at the hash length only for FIPS v7+ modules. */ -#ifdef WP_TEST_FIPS_V7 +#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION_MAJOR) && \ + (HAVE_FIPS_VERSION_MAJOR >= 7) #define WP_TEST_PSS_SALT_MAY_CAP #endif @@ -1697,7 +1696,7 @@ int test_rsa_enc_dec_pkcs1(void *data) } if (err == 0) { PRINT_MSG("Check that private decrypt works with valid key size."); - err = test_rsa_enc_dec(rsa_key_der_2048, sizeof(rsa_key_der_2048), + err = test_rsa_enc_dec(rsa_key_der_1024, sizeof(rsa_key_der_1024), RSA_PKCS1_PADDING, NULL, NULL); } @@ -1731,12 +1730,12 @@ int test_rsa_enc_dec_oaep(void *data) (void)data; /* Use SHA-1 for MD and MGF1 MD. */ - err = test_rsa_enc_dec(rsa_key_der_2048, sizeof(rsa_key_der_2048), + err = test_rsa_enc_dec(rsa_key_der_1024, sizeof(rsa_key_der_1024), RSA_PKCS1_OAEP_PADDING, EVP_sha1(), EVP_sha1()) == 1; #ifdef WP_HAVE_SHA256 if (err == 0) { /* Use SHA-256 for MD and MGF1 MD. */ - err = test_rsa_enc_dec(rsa_key_der_2048, sizeof(rsa_key_der_2048), + err = test_rsa_enc_dec(rsa_key_der_1024, sizeof(rsa_key_der_1024), RSA_PKCS1_OAEP_PADDING, EVP_sha256(), EVP_sha256()) == 1; } @@ -1744,7 +1743,7 @@ int test_rsa_enc_dec_oaep(void *data) #ifdef WP_HAVE_SHA384 if (err == 0) { /* Use SHA-384 for MD and SHA-512 for MGF1 MD. */ - err = test_rsa_enc_dec(rsa_key_der_2048, sizeof(rsa_key_der_2048), + err = test_rsa_enc_dec(rsa_key_der_1024, sizeof(rsa_key_der_1024), RSA_PKCS1_OAEP_PADDING, EVP_sha384(), EVP_sha512()) == 1; } @@ -2031,10 +2030,6 @@ int test_rsa_pkey_invalid_key_size(void *data) { if (err == 0) { err = test_pkey_verify(pkey, wpLibCtx, buf, sizeof(buf), rsaSig, rsaSigLen, 0, NULL, NULL); -#ifdef WP_TEST_FIPS_V7 - PRINT_MSG("FIPS v7 refuses to verify with a 1024-bit key."); - err = err == 0; -#endif } #endif /* HAVE_FIPS || HAVE_FIPS_VERSION */ diff --git a/test/unit.h b/test/unit.h index 0748d879..fc19b73e 100644 --- a/test/unit.h +++ b/test/unit.h @@ -59,12 +59,6 @@ #define WP_HAVE_EPKI_TEST #endif -/* FIPS v7 modules: tests expect the v7 refusals. */ -#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION_MAJOR) && \ - (HAVE_FIPS_VERSION_MAJOR >= 7) - #define WP_TEST_FIPS_V7 -#endif - /* test_slhdsa.c selects a representative set and falls back to any enabled * set when a custom wolfSSL build enables only one parameter set. */ #if defined(WP_HAVE_SLH_DSA_SHA2_128F) || \ From cb74c3cbd472b33e417b53af29b4bb5c07db2f80 Mon Sep 17 00:00:00 2001 From: Colton Willey Date: Thu, 3 Dec 2026 10:34:50 -0800 Subject: [PATCH 09/10] Remove unreleased ChangeLog entry --- ChangeLog.md | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index ac4fd6e7..562ee9e2 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,15 +1,3 @@ -# wolfProvider Unreleased - -## Enhancements and Optimizations -* Take AES-GCM IVs for TLS 1.2 records and EVP IV generation - (EVP_CTRL_GCM_SET_IV_FIXED with EVP_CTRL_GCM_IV_GEN) from the wolfCrypt - module's own counter instead of advancing them in the provider (FIPS 140-3 - IG C.H). The TLS explicit nonce on the wire is unchanged. Two IV_GEN calls in - a row now return distinct IVs, an unfinished encrypt stream is closed before - the next IV is handed out, and a decrypt init on a context with a generated - IV requires a new SET_IV_FIXED (and on FIPS v7 a new key) before encrypting - again (PR 531). - # wolfProvider version 1.2.2 (August 27, 2026) Release 1.2.2 has been developed according to wolfSSL's development and QA From b3fb903501cae0cda66ab8032358e5247e202b14 Mon Sep 17 00:00:00 2001 From: Colton Willey Date: Thu, 3 Dec 2026 11:29:41 -0800 Subject: [PATCH 10/10] Keep non-streaming GCM IV issuance inside wolfCrypt --- src/wp_aes_aead.c | 126 +++++++++++++++++++++++++++++++-------------- test/test_aestag.c | 87 ++++++++++++++++++++++++++++++- test/unit.c | 1 + test/unit.h | 1 + 4 files changed, 174 insertions(+), 41 deletions(-) diff --git a/src/wp_aes_aead.c b/src/wp_aes_aead.c index caeb0da2..0ac69088 100644 --- a/src/wp_aes_aead.c +++ b/src/wp_aes_aead.c @@ -90,7 +90,7 @@ typedef struct wp_AeadCtx { unsigned int keySet:1; /** GCM IV was generated by wolfCrypt, which counts each use. */ unsigned int ivMod:1; - /** GCM stream holds wolfCrypt's IV and has not been finalized. */ + /** A wolfCrypt IV is reserved or held by an unfinished GCM stream. */ unsigned int ivModOpen:1; /** Cache of authentication status. */ unsigned int authErr:1; @@ -615,6 +615,13 @@ static int wp_aead_get_ctx_params(wp_AeadCtx* ctx, OSSL_PARAM params[]) (!wp_aead_tag_len_valid(ctx->mode, sz))) { ok = 0; } +#if defined(WP_HAVE_AESGCM) && !defined(WOLFSSL_AESGCM_STREAM) + if ((ctx->mode == EVP_CIPH_GCM_MODE) && ctx->ivMod && + (!ctx->singleDone || ctx->authErr || + (ctx->ivState != IV_STATE_FINISHED))) { + ok = 0; + } +#endif if (ok && (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz))) { ok = 0; } @@ -1174,29 +1181,38 @@ static int wp_aesgcm_iv_inc(wp_AeadCtx* ctx) return ok; } -/* Finalize a stream left open on wolfCrypt's IV so wolfCrypt moves to its - * next IV; older wolfCrypt advances only in final. Returns 1 on success. */ +/* Let wolfCrypt finish an abandoned operation before issuing its next IV. + * For one-shot GCM, an empty encryption consumes the reserved IV. */ static int wp_aesgcm_close_iv(wp_AeadCtx* ctx) { int ok = 1; -#ifdef WOLFSSL_AESGCM_STREAM - int rc; - unsigned char tag[AES_BLOCK_SIZE]; if (ctx->ivModOpen) { + int rc; + unsigned char tag[AES_BLOCK_SIZE]; + +#ifdef WOLFSSL_AESGCM_STREAM rc = wc_AesGcmEncryptFinal(&ctx->aes, tag, (word32)sizeof(tag)); +#else + unsigned char iv[AES_BLOCK_SIZE]; + + rc = wc_AesGcmEncrypt_ex(&ctx->aes, NULL, NULL, 0, iv, + (word32)ctx->ivLen, tag, (word32)sizeof(tag), NULL, 0); + if (rc != 0) { + ctx->ivGen = 0; + ctx->ivModOpen = 0; + ctx->authErr = 1; + } +#endif OPENSSL_cleanse(tag, sizeof(tag)); if (rc != 0) { WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, - "wc_AesGcmEncryptFinal", rc); + "Finish abandoned GCM IV", rc); ok = 0; } else { ctx->ivModOpen = 0; } } -#else - (void)ctx; -#endif return ok; } @@ -1226,7 +1242,11 @@ static int wp_aesgcm_init_iv(wp_AeadCtx* ctx) #else /* wc_AesGcmEncrypt_ex() encrypts with the IV held in aes.reg. */ if (ctx->ivMod) { - XMEMCPY(ctx->iv, ctx->aes.reg, ctx->ivLen); + ok = wp_aesgcm_close_iv(ctx); + if (ok) { + XMEMCPY(ctx->iv, ctx->aes.reg, ctx->ivLen); + ctx->ivModOpen = 1; + } } #endif return ok; @@ -1269,6 +1289,11 @@ static int wp_aesgcm_get_rand_iv(wp_AeadCtx* ctx, unsigned char* out, XMEMCPY(out, ctx->iv + ctx->ivLen - olen, olen); #ifndef WOLFSSL_AESGCM_STREAM XMEMCPY(ctx->origIv, ctx->iv, ctx->ivLen); + if (ctx->ivMod && (ctx->ivState != IV_STATE_BUFFERED)) { + wp_aead_reset_op_state(ctx); + ctx->authErr = 0; + ctx->tagAvail = 0; + } #endif /* wolfCrypt advances its own IV. */ if (inc && (!ctx->ivMod) && (!wp_aesgcm_iv_inc(ctx))) { @@ -1384,13 +1409,20 @@ static int wp_aesgcm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv, ctx->ivSet = 1; ctx->ivMod = 1; ctx->ivModOpen = 0; +#ifndef WOLFSSL_AESGCM_STREAM + wp_aead_reset_op_state(ctx); + ctx->authErr = 0; + ctx->tagAvail = 0; +#endif } } else { XMEMCPY(ctx->iv, iv, len); } - ctx->ivGen = 1; - ctx->ivState = IV_STATE_BUFFERED; + if (ok) { + ctx->ivGen = 1; + ctx->ivState = IV_STATE_BUFFERED; + } } WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok); @@ -1669,8 +1701,8 @@ static int wp_aesgcm_tls_cipher(wp_AeadCtx* ctx, unsigned char* out, ok = 0; } - if (ok && ctx->ivMod && (!wp_aesgcm_close_iv(ctx))) { - ok = 0; + if (ok && ctx->ivMod) { + ok = ctx->ivGen && wp_aesgcm_close_iv(ctx); } if (ok && (!ctx->ivMod)) { if (ctx->enc) { @@ -1737,6 +1769,9 @@ static int wp_aesgcm_tls_cipher(wp_AeadCtx* ctx, unsigned char* out, } } +#ifndef WOLFSSL_AESGCM_STREAM + ctx->singleDone = 0; +#endif ctx->ivState = IV_STATE_FINISHED; ctx->tlsAadLen = UNINITIALISED_SIZET; *outLen = oLen; @@ -1938,7 +1973,9 @@ static int wp_aesgcm_single_update(wp_AeadCtx *ctx, unsigned char *out, *outLen = 0; if (ctx->singleDone || ctx->authErr || (!ctx->keySet) || - (ctx->ivState != IV_STATE_BUFFERED) || (!WP_FITS_WORD32(inLen)) || + ((ctx->ivState != IV_STATE_BUFFERED) && + !(ctx->ivMod && (ctx->ivState == IV_STATE_COPIED))) || + (ctx->ivMod && !ctx->ivGen) || (!WP_FITS_WORD32(inLen)) || (!WP_FITS_WORD32(ctx->aadLen)) || (outSize < inLen) || ((inLen != 0) && ((in == NULL) || (out == NULL))) || ((!ctx->enc) && (!ctx->singleTagSet))) { @@ -1949,12 +1986,15 @@ static int wp_aesgcm_single_update(wp_AeadCtx *ctx, unsigned char *out, ctx->tagLen = EVP_GCM_TLS_TAG_LEN; } ctx->singleDone = 1; - XMEMCPY(ctx->origIv, ctx->iv, ctx->ivLen); ctx->ivState = IV_STATE_COPIED; - rc = wc_AesGcmSetExtIV(&ctx->aes, ctx->origIv, (word32)ctx->ivLen); - if (rc != 0) { - WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmSetExtIV", rc); - ok = 0; + if (!ctx->ivMod) { + XMEMCPY(ctx->origIv, ctx->iv, ctx->ivLen); + rc = wc_AesGcmSetExtIV(&ctx->aes, ctx->origIv, (word32)ctx->ivLen); + if (rc != 0) { + WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, + "wc_AesGcmSetExtIV", rc); + ok = 0; + } } } if (ok && ctx->enc) { @@ -1963,6 +2003,15 @@ static int wp_aesgcm_single_update(wp_AeadCtx *ctx, unsigned char *out, (in != NULL) ? in : &empty, (word32)inLen, ctx->origIv, (word32)ctx->ivLen, ctx->buf, (word32)ctx->tagLen, ctx->aad, (word32)ctx->aadLen); + if (ctx->ivMod) { + ctx->ivModOpen = 0; + if (rc == 0) { + XMEMCPY(ctx->iv, ctx->origIv, ctx->ivLen); + } + else { + ctx->ivGen = 0; + } + } if (rc != 0) { WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmEncrypt_ex", rc); ok = 0; @@ -2014,14 +2063,11 @@ static int wp_aesgcm_encdec(wp_AeadCtx *ctx, unsigned char *out, size_t* outLen, int rc; unsigned char *tmp = NULL; byte *iv = NULL; - /* The first encrypt takes wolfCrypt's IV, written to origIv. */ - int modIv = ctx->ivMod && (offset == 0); WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_encdec"); - /* A completed operation must not be reused without a fresh IV; the nonce - * would otherwise repeat. An init that supplies an IV clears this state. */ - if (ctx->ivState == IV_STATE_FINISHED) { + /* Buffered replay is only for caller IVs on an active operation. */ + if (ctx->ivMod || (ctx->ivState == IV_STATE_FINISHED)) { return 0; } @@ -2050,16 +2096,12 @@ static int wp_aesgcm_encdec(wp_AeadCtx *ctx, unsigned char *out, size_t* outLen, if (ctx->ivState == IV_STATE_COPIED) { iv = ctx->origIv; } - if (modIv) { - iv = ctx->origIv; - } if (ok) { - if (!modIv) { - rc = wc_AesGcmSetExtIV(&ctx->aes, iv, (word32)ctx->ivLen); - if (rc != 0) { - WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmSetExtIV", rc); - ok = 0; - } + rc = wc_AesGcmSetExtIV(&ctx->aes, iv, (word32)ctx->ivLen); + if (rc != 0) { + WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, + "wc_AesGcmSetExtIV", rc); + ok = 0; } if (ok && ctx->ivState == IV_STATE_BUFFERED) { @@ -2161,6 +2203,8 @@ static int wp_aesgcm_stream_update(wp_AeadCtx *ctx, unsigned char *out, int ok = 1; int process = 0; int single = 0; + /* Replaying a module IV through the caller-IV API loses its counter. */ + int onePayload = ctx->singleUpdate || ctx->ivMod; size_t curLen = 0; WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_stream_update"); @@ -2176,7 +2220,7 @@ static int wp_aesgcm_stream_update(wp_AeadCtx *ctx, unsigned char *out, if (ctx->ivState == IV_STATE_FINISHED) { ok = 0; } - else if (ctx->singleUpdate && ctx->singleDone && inLen != 0) { + else if (onePayload && ctx->singleDone && inLen != 0) { ctx->authErr = 1; ok = 0; } @@ -2195,7 +2239,7 @@ static int wp_aesgcm_stream_update(wp_AeadCtx *ctx, unsigned char *out, ok = 0; } else if (inLen > 0) { - if (ctx->singleUpdate) { + if (onePayload) { single = 1; } else { @@ -2220,9 +2264,10 @@ static int wp_aesgcm_stream_update(wp_AeadCtx *ctx, unsigned char *out, else { *outLen = oLen; } - if (!ok && ctx->singleUpdate) { + if (!ok && onePayload) { *outLen = 0; ctx->authErr = 1; + ctx->tagAvail = 0; ctx->ivState = IV_STATE_FINISHED; } } @@ -2245,12 +2290,13 @@ static int wp_aesgcm_stream_final(wp_AeadCtx *ctx, unsigned char *out, size_t *outLen, size_t outSize) { int ok = 1; + int onePayload = ctx->singleUpdate || ctx->ivMod; (void)outSize; WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_stream_final"); /* authErr can bypass the single-update Final branch. */ - if (ctx->singleUpdate) { + if (onePayload) { *outLen = 0; } @@ -2260,7 +2306,7 @@ static int wp_aesgcm_stream_final(wp_AeadCtx *ctx, unsigned char *out, else if (ctx->authErr) { ok = 0; } - else if (ctx->singleUpdate) { + else if (onePayload) { if (ctx->ivState == IV_STATE_FINISHED) { ok = 0; } diff --git a/test/test_aestag.c b/test/test_aestag.c index 29fa31f9..62d10d24 100644 --- a/test/test_aestag.c +++ b/test/test_aestag.c @@ -735,6 +735,8 @@ static int test_aes_tag_fixed(void *data, const char *cipher, err = test_aes_tag_dec_multi(wcipher, key, iv, ivLen, aad, msg, sizeof(msg), enc, tag, dec); } +#ifdef WOLFSSL_AESGCM_STREAM + /* Module-generated IV encryption needs streaming for multipart input. */ if (err == 0) { PRINT_MSG("Encrypt with wolfprovider (multiple updates)"); err = test_aes_tag_fixed_enc_multi(wcipher, key, iv, ivFixedLen, ivLen, aad, @@ -745,6 +747,7 @@ static int test_aes_tag_fixed(void *data, const char *cipher, err = test_aes_tag_dec_multi(ocipher, key, iv, ivLen, aad, msg, sizeof(msg), enc, tag, dec); } +#endif if (err == 0) { PRINT_MSG("Encrypt with wolfprovider"); err = test_aes_tag_enc_ossh(wcipher, key, iv, @@ -1355,6 +1358,7 @@ int test_aes_gcm_iv_gen_multi(void *data) unsigned char key[16]; unsigned char fixed[EVP_GCM_TLS_FIXED_IV_LEN]; unsigned char iv[8][12]; + unsigned char abandonedIv[sizeof(iv[0])]; unsigned char enc[sizeof(msg)]; unsigned char tag[AES_BLOCK_SIZE]; unsigned char dec[sizeof(msg)]; @@ -1382,13 +1386,20 @@ int test_aes_gcm_iv_gen_multi(void *data) err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IV_FIXED, (int)sizeof(fixed), fixed) != 1; } + if (err == 0) { + PRINT_MSG("Request two IVs before encrypting"); + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_IV_GEN, + sizeof(abandonedIv), abandonedIv) != 1 || + memcmp(abandonedIv, fixed, sizeof(fixed)) != 0; + } for (i = 0; (err == 0) && (i < numIv); i++) { err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_IV_GEN, (int)sizeof(iv[i]), iv[i]) != 1; if (err == 0) { PRINT_BUFFER("IV", iv[i], sizeof(iv[i])); - err = memcmp(iv[i], fixed, sizeof(fixed)) != 0; + err = memcmp(iv[i], fixed, sizeof(fixed)) != 0 || + memcmp(iv[i], abandonedIv, sizeof(abandonedIv)) == 0; } for (j = 0; (err == 0) && (j < i); j++) { err = memcmp(iv[i], iv[j], sizeof(iv[i])) == 0; @@ -1423,6 +1434,80 @@ int test_aes_gcm_iv_gen_multi(void *data) return err; } +#ifndef WOLFSSL_AESGCM_STREAM +int test_aes_gcm_iv_gen_single_update(void *data) +{ + int err; + int outLen; + unsigned char key[16] = { 0 }; + unsigned char fixed[EVP_GCM_TLS_FIXED_IV_LEN] = { 1, 2, 3, 4 }; + unsigned char aad[] = "AAD"; + unsigned char freshAad[] = "Fresh AAD"; + unsigned char msg[] = "Test pattern"; + unsigned char iv[2][12]; + unsigned char enc[sizeof(msg)]; + unsigned char rejected[sizeof(msg)]; + unsigned char sentinel[sizeof(msg)]; + unsigned char tag[AES_BLOCK_SIZE]; + unsigned char dec[sizeof(msg)]; + EVP_CIPHER *wp = EVP_CIPHER_fetch(wpLibCtx, "AES-128-GCM", NULL); + EVP_CIPHER *ossl = EVP_CIPHER_fetch(osslLibCtx, "AES-128-GCM", NULL); + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + + (void)data; + + err = (wp == NULL) || (ossl == NULL) || (ctx == NULL) || + (EVP_EncryptInit(ctx, wp, key, NULL) != 1) || + (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IV_FIXED, sizeof(fixed), + fixed) != 1) || + (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_IV_GEN, sizeof(iv[0]), + iv[0]) != 1) || + (EVP_EncryptUpdate(ctx, NULL, &outLen, aad, sizeof(aad) - 1) != 1) || + (EVP_EncryptUpdate(ctx, enc, &outLen, msg, sizeof(msg)) != 1) || + (outLen != (int)sizeof(msg)); + if (err == 0) { + PRINT_MSG("Module-generated IV tag is unavailable before final"); + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, sizeof(tag), + tag) > 0; + ERR_clear_error(); + } + if (err == 0) { + PRINT_MSG("A second module-generated IV payload update fails"); + memset(sentinel, 0xa5, sizeof(sentinel)); + memcpy(rejected, sentinel, sizeof(rejected)); + outLen = -1; + err = (EVP_EncryptUpdate(ctx, rejected, &outLen, msg, sizeof(msg)) + != 0) || (outLen != 0) || + (memcmp(rejected, sentinel, sizeof(rejected)) != 0); + ERR_clear_error(); + } + if (err == 0) { + PRINT_MSG("Final and tag retrieval fail after a rejected update"); + err = (EVP_EncryptFinal_ex(ctx, enc, &outLen) > 0) || + (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, sizeof(tag), + tag) > 0); + ERR_clear_error(); + } + if (err == 0) { + PRINT_MSG("A fresh module IV starts a new operation after rejection"); + err = (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_IV_GEN, sizeof(iv[1]), + iv[1]) != 1) || (memcmp(iv[0], iv[1], sizeof(iv[0])) == 0) || + (EVP_EncryptUpdate(ctx, NULL, &outLen, freshAad, + sizeof(freshAad) - 1) != 1) || + (EVP_EncryptFinal_ex(ctx, enc, &outLen) != 1) || (outLen != 0) || + (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, sizeof(tag), + tag) != 1) || + test_aes_tag_dec(ossl, key, iv[1], sizeof(iv[1]), freshAad, msg, + 0, enc, tag, dec, 0, 0); + } + + EVP_CIPHER_CTX_free(ctx); + EVP_CIPHER_free(wp); + EVP_CIPHER_free(ossl); + return err; +} +#endif /* !WOLFSSL_AESGCM_STREAM */ + /******************************************************************************/ /* An IV from IV-gen is not handed out again after an abandoned message, the diff --git a/test/unit.c b/test/unit.c index 2a0bbc2f..ea02ab11 100644 --- a/test/unit.c +++ b/test/unit.c @@ -355,6 +355,7 @@ TEST_CASE test_case[] = { TEST_DECL(test_aes_gcm_stream_key_no_iv, NULL), TEST_DECL(test_aes_gcm_final_no_update_no_iv, NULL), #ifndef WOLFSSL_AESGCM_STREAM + TEST_DECL(test_aes_gcm_iv_gen_single_update, NULL), TEST_DECL(test_aes_gcm_single_update, NULL), #endif #endif diff --git a/test/unit.h b/test/unit.h index b94651fc..23735ec3 100644 --- a/test/unit.h +++ b/test/unit.h @@ -308,6 +308,7 @@ int test_aes_gcm_key_no_iv(void *data); int test_aes_gcm_stream_key_no_iv(void *data); int test_aes_gcm_final_no_update_no_iv(void *data); #ifndef WOLFSSL_AESGCM_STREAM +int test_aes_gcm_iv_gen_single_update(void *data); int test_aes_gcm_single_update(void *data); #endif #endif /* WP_HAVE_AESGCM */