diff --git a/src/wp_aes_aead.c b/src/wp_aes_aead.c index 264091f7..0ac69088 100644 --- a/src/wp_aes_aead.c +++ b/src/wp_aes_aead.c @@ -88,6 +88,10 @@ typedef struct wp_AeadCtx { unsigned int ivSet:1; /** Key has been set. */ unsigned int keySet:1; + /** GCM IV was generated by wolfCrypt, which counts each use. */ + unsigned int ivMod:1; + /** A wolfCrypt IV is reserved or held by an unfinished GCM stream. */ + unsigned int ivModOpen:1; /** Cache of authentication status. */ unsigned int authErr:1; /** AAD set with call to update. */ @@ -170,6 +174,12 @@ static void *wp_aead_dupctx(wp_AeadCtx* src) if (!wolfssl_prov_is_running()) { ok = 0; } +#ifdef WP_HAVE_AESGCM + /* The generated IV sequence is held inside the wolfCrypt AES state. */ + if (ok && (src->mode == EVP_CIPH_GCM_MODE) && src->ivMod) { + ok = 0; + } +#endif #if defined(WP_HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM) /* wolfCrypt's GCM stream state can't be copied. */ if (ok && (src->mode == EVP_CIPH_GCM_MODE) && @@ -605,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; } @@ -1164,6 +1181,77 @@ static int wp_aesgcm_iv_inc(wp_AeadCtx* ctx) return ok; } +/* 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; + + 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, + "Finish abandoned GCM IV", rc); + ok = 0; + } else { + ctx->ivModOpen = 0; + } + } + 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 = 0; + + if (ctx->ivMod) { + 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); + } + 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) { + ok = wp_aesgcm_close_iv(ctx); + if (ok) { + XMEMCPY(ctx->iv, ctx->aes.reg, ctx->ivLen); + ctx->ivModOpen = 1; + } + } +#endif + return ok; +} + /** * Get the random part of the IV/nonce. * @@ -1191,15 +1279,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. */ @@ -1209,8 +1289,14 @@ 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 - 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) { @@ -1279,6 +1365,8 @@ 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->ivModOpen = 0; ctx->ivState = IV_STATE_BUFFERED; } else { @@ -1319,13 +1407,22 @@ 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; + 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); @@ -1352,6 +1449,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"); @@ -1364,6 +1463,13 @@ static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key, if (ok && (key != NULL) && (keyLen != ctx->keyLen)) { ok = 0; } + /* 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; @@ -1422,10 +1528,18 @@ 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 caller IV replaces a generated IV. */ + if (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 * so its IV/nonce cannot be reused. */ @@ -1520,6 +1634,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. @@ -1581,7 +1701,10 @@ static int wp_aesgcm_tls_cipher(wp_AeadCtx* ctx, unsigned char* out, ok = 0; } - if (ok) { + if (ok && ctx->ivMod) { + ok = ctx->ivGen && wp_aesgcm_close_iv(ctx); + } + if (ok && (!ctx->ivMod)) { if (ctx->enc) { if (!wp_aesgcm_get_rand_iv(ctx, out, EVP_GCM_TLS_EXPLICIT_IV_LEN, 0)) { @@ -1602,7 +1725,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); @@ -1632,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; @@ -1686,12 +1826,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; } } @@ -1789,6 +1924,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); @@ -1835,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))) { @@ -1846,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) { @@ -1860,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; @@ -1914,9 +2066,8 @@ static int wp_aesgcm_encdec(wp_AeadCtx *ctx, unsigned char *out, size_t* outLen, 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; } @@ -1948,7 +2099,8 @@ static int wp_aesgcm_encdec(wp_AeadCtx *ctx, unsigned char *out, size_t* outLen, 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); + WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, + "wc_AesGcmSetExtIV", rc); ok = 0; } @@ -2051,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"); @@ -2066,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; } @@ -2085,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 { @@ -2110,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; } } @@ -2135,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; } @@ -2150,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/src/wp_ecc_kmgmt.c b/src/wp_ecc_kmgmt.c index 3219f300..f7f764da 100644 --- a/src/wp_ecc_kmgmt.c +++ b/src/wp_ecc_kmgmt.c @@ -2913,7 +2913,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 771e42ff..afdef804 100644 --- a/src/wp_ecx_kmgmt.c +++ b/src/wp_ecx_kmgmt.c @@ -2454,7 +2454,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; } diff --git a/test/test_aestag.c b/test/test_aestag.c index 58c9cc71..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, @@ -1342,6 +1345,328 @@ 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 abandonedIv[sizeof(iv[0])]; + 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; + } + 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 || + 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; + } + 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; +} + +#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 + * 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; + 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[4][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); + } + + /* 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"); + 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[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) { + 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 < 3); 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 @@ -2746,6 +3071,32 @@ static int aead_copy_after_update(EVP_CIPHER *cipher, EVP_CIPHER *refCipher, return err; } +#ifdef WP_HAVE_AESGCM +static int aead_copy_module_iv(EVP_CIPHER *cipher, const unsigned char *key) +{ + unsigned char fixedIv[4] = { 1, 2, 3, 4 }; + unsigned char generatedIv[12]; + EVP_CIPHER_CTX *base = aead_base(cipher, 1, 0, key); + EVP_CIPHER_CTX *copy = EVP_CIPHER_CTX_new(); + int err = (base == NULL) || (copy == NULL) || + (EVP_CIPHER_CTX_ctrl(base, EVP_CTRL_GCM_SET_IV_FIXED, sizeof(fixedIv), + fixedIv) != 1); + + if (!err) { + err = EVP_CIPHER_CTX_copy(copy, base) == 1; + ERR_clear_error(); + } + if (!err) { + err = (EVP_CIPHER_CTX_ctrl(base, EVP_CTRL_GCM_IV_GEN, + sizeof(generatedIv), generatedIv) != 1) || + (memcmp(generatedIv, fixedIv, sizeof(fixedIv)) != 0); + } + EVP_CIPHER_CTX_free(copy); + EVP_CIPHER_CTX_free(base); + return err; +} +#endif /* WP_HAVE_AESGCM */ + int test_aes_aead_ctx_copy(void *data) { static const char *names[] = { @@ -2799,6 +3150,10 @@ int test_aes_aead_ctx_copy(void *data) PRINT_MSG("%s: reject copy after streaming starts", names[i]); err = aead_copy_after_update(cipher[1], cipher[0], 0, 16, 0); #endif + if (!err) { + PRINT_MSG("%s: reject copy with a module-managed IV", names[i]); + err = aead_copy_module_iv(cipher[1], key); + } } #endif EVP_CIPHER_CTX_free(base[0]); diff --git a/test/test_ecc.c b/test/test_ecc.c index c49bc14d..3c0fd0f3 100644 --- a/test/test_ecc.c +++ b/test/test_ecc.c @@ -1671,6 +1671,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, @@ -1690,6 +1691,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. diff --git a/test/unit.c b/test/unit.c index 80e13c8a..ea02ab11 100644 --- a/test/unit.c +++ b/test/unit.c @@ -338,6 +338,8 @@ 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_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), @@ -353,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 66c5820a..23735ec3 100644 --- a/test/unit.h +++ b/test/unit.h @@ -291,6 +291,8 @@ 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_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); @@ -306,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 */