wolfSSL — All-Zero Nonce After First Message in EVP SM4-GCM Finalization
After each SM4-GCM message, the EVP layer zeroes the context IV, and the IV store helper then copies that zeroed buffer into the SM4 state where other ciphers restore a nonce. Later messages on the same context are sealed under the key with an all-zero nonce, and every call returns success.
- Advisory
- BYTERAY-2026-0323
- CWE
- CWE-323
- CVSS
- 7.4 (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:N)
- Vendor
- wolfSSL
- Product
- wolfSSL
- Reported
- 2026-09-27
- Disclosure
- 2026-12-03
- Affected
- wolfSSL 5.9.2 (tested at master commit d95483f), builds with OPENSSL_EXTRA and WOLFSSL_SM4_GCM (--enable-opensslextra --enable-sm4-gcm, with the wolfSM add-on) whose applications process more than one SM4-GCM message on one EVP context
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
In wolfSSL's OpenSSL compatibility layer, an SM4-GCM message is buffered by the Update calls and processed in one step by wolfSSL_EVP_CipherFinal(), with the context's IV buffer ctx->iv as the nonce.
After that step, Final clears ctx->iv, with a comment that IV reuse is not recommended for SM4-GCM, and calls wolfSSL_StoreExternalIV(). For AES-GCM and ARIA-GCM that helper copies the cipher's internal nonce into ctx->iv. The SM4-GCM case copies in the other direction: it writes ctx->iv, which has just been zeroed, into the SM4 state. Nothing puts a nonce back into ctx->iv, and the context keeps no record that it has none.
The next message on the same context is processed with twelve zero bytes as the nonce, and so is every message after it. No call fails. Messages sealed this way share a keystream, and an observer can go on to forge tags. SM4 is not part of a default build and needs the separate wolfSM add-on; wolfSSL's own TLS record layer does not use the EVP layer.
Affected Products and Versions
- Component:
wolfcrypt/src/evp.c, theWC_SM4_GCM_TYPEcase ofwolfSSL_EVP_CipherFinal(); andsrc/ssl.c, the SM4-GCM case ofwolfSSL_StoreExternalIV(). - Required build:
OPENSSL_EXTRA(--enable-opensslextra) andWOLFSSL_SM4_GCM(--enable-sm4-gcm, disabled by default,configure.ac:4250). The SM4 implementation comes from the wolfSM add-on. In a stock treewolfssl/wolfcrypt/sm4.hstops the build with an#errorwhenWOLFSSL_SM4is defined (wolfssl/wolfcrypt/sm4.h:24). - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- A default
./configurebuild does not include this code. - Other releases were not checked.
Technical Details
The end of the SM4-GCM case in wolfSSL_EVP_CipherFinal() (wolfcrypt/src/evp.c:1595-1607):
if (ret == WOLFSSL_SUCCESS) {
if (ctx->authIncIv) {
ctx->authIncIv = 0;
}
else {
/* Clear IV, since IV reuse is not recommended for SM4 GCM.
*/
XMEMSET(ctx->iv, 0, SM4_BLOCK_SIZE);
}
if (wolfSSL_StoreExternalIV(ctx) != WOLFSSL_SUCCESS) {
ret = WOLFSSL_FAILURE;
}
}
The SM4-GCM case of wolfSSL_StoreExternalIV() (src/ssl.c:10965-10968):
case WC_SM4_GCM_TYPE:
WOLFSSL_MSG("SM4 GCM");
XMEMCPY(&ctx->cipher.sm4.iv, ctx->iv, SM4_BLOCK_SIZE);
break;
The AES-GCM case of the same function is XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, ctx->ivSz) (src/ssl.c:10846). In the SM4-GCM case the destination and source are swapped relative to the AES and ARIA cases. The only code that advances the SM4 nonce is an increment of ctx->cipher.sm4.iv that runs when ctx->authIncIv is set (wolfcrypt/src/evp.c:1587-1590), which is the opt-in EVP_CTRL_GCM_IV_GEN flow; BYTERAY-2026-0427 covers a separate defect in that control for SM4-GCM.
The sequence for two messages:
- The application initialises SM4-GCM through
wolfSSL_EVP_CipherInit()(wolfcrypt/src/evp.c:7593). The IV is copied intoctx->ivonly when it is not NULL, and a NULL IV is not an error (wolfcrypt/src/evp.c:8588-8590). - Message one:
wolfSSL_EVP_CipherFinal()(wolfcrypt/src/evp.c:1313) callswc_Sm4GcmEncrypt()orwc_Sm4GcmDecrypt()withctx->ivas the nonce (wolfcrypt/src/evp.c:1563-1572). - The nonce is not advanced, because
authIncIvis not set. - Final zeroes
ctx->iv(wolfcrypt/src/evp.c:1599-1602) and callswolfSSL_StoreExternalIV()(wolfcrypt/src/evp.c:1604-1606). wolfSSL_StoreExternalIV()copies the zeroedctx->ivintoctx->cipher.sm4.iv(src/ssl.c:10967).ctx->ivstays zero.- Message two and all later messages repeat step 2 with
ctx->ivequal to twelve zero bytes.
Reachability and Preconditions
- Build with
OPENSSL_EXTRA,WOLFSSL_SM4_GCMand the wolfSM add-on. - An application that processes two or more messages through one SM4-GCM EVP context and does not set a fresh IV after the first. A context initialised with a NULL IV uses the zero nonce from the first message onward.
- The attacker is a passive observer of the ciphertexts and tags. They need two or more affected messages under the same key and supply no input to the context.
- No access to the process is needed.
Impact
All messages after the first on such a context are sealed under the same key and the all-zero nonce. The XOR of any two of their ciphertexts is the XOR of the plaintexts, and one known plaintext gives the keystream for the rest up to its length. Two or more tagged messages under a repeated GCM nonce let an observer recover the authentication subkey and produce valid tags for other data under that key. Every API call involved returns success.
ByteRay tested SM4-GCM and SM4-CCM in one run through the public EVP API. The SM4 primitives, which are not in the wolfSSL tree, were supplied by a standalone implementation that records the nonce of each call. In that run the context IV was all zero after the first Final, the messages that followed were sealed under the all-zero nonce, and a repeated plaintext produced a byte-identical ciphertext and tag.
Remediation and Mitigation
ByteRay has not confirmed a fixed wolfSSL release for this issue as of 2026-12-03, and has not checked it against wolfSSL changes made after the tested commit.
The fix direction is to correct the copy in the SM4-GCM case of wolfSSL_StoreExternalIV() so that it stores the cipher's internal IV into ctx->iv, as the AES-GCM and ARIA-GCM cases do, and to enforce nonce freshness: track whether an IV is set, clear that state at each Final, and fail Update and Final until a new IV arrives. The stored nonce could also be advanced on every message, not only in the EVP_CTRL_GCM_IV_GEN flow. Init should reject a missing IV for this cipher.
Applications can avoid the issue by passing an explicit, unique IV to the init call for every message on an SM4-GCM context.
Timeline
- 2026-09-27 — Reported to wolfSSL.
- 2026-12-03 — Public disclosure.
Credits
Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.