wolfSSL — All-Zero Nonce After First Message in EVP SM4-CCM Finalization
After each SM4-CCM 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 processed under the key with an all-zero nonce, and every call returns success.
- Advisory
- BYTERAY-2026-0324
- 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_CCM (--enable-opensslextra --enable-sm4-ccm, with the wolfSM add-on) whose applications process more than one SM4-CCM message on one EVP context
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
In wolfSSL's OpenSSL compatibility layer, an SM4-CCM 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-CCM, and calls wolfSSL_StoreExternalIV(). For the AES cases that helper copies the cipher's internal nonce into ctx->iv. The SM4-CCM case copies in the other direction and writes the zeroed ctx->iv into the SM4 state. Nothing puts a nonce back into ctx->iv.
Every later message on the same context is processed with twelve zero bytes as the nonce, and a context that was initialised with a NULL IV uses that nonce from its first message. No call fails. CCM encrypts in counter mode, so these messages share a keystream. 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_CCM_TYPEcase ofwolfSSL_EVP_CipherFinal(); andsrc/ssl.c, the SM4-CCM case ofwolfSSL_StoreExternalIV(). - Required build:
OPENSSL_EXTRA(--enable-opensslextra) andWOLFSSL_SM4_CCM(--enable-sm4-ccm, disabled by default,configure.ac:4266). 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-CCM case in wolfSSL_EVP_CipherFinal() (wolfcrypt/src/evp.c:1646-1658):
if (ret == WOLFSSL_SUCCESS) {
if (ctx->authIncIv) {
ctx->authIncIv = 0;
}
else {
/* Clear IV, since IV reuse is not recommended
* for SM4 CCM. */
XMEMSET(ctx->iv, 0, SM4_BLOCK_SIZE);
}
if (wolfSSL_StoreExternalIV(ctx) != WOLFSSL_SUCCESS) {
ret = WOLFSSL_FAILURE;
}
}
The SM4-CCM case of wolfSSL_StoreExternalIV() (src/ssl.c:10971-10974):
case WC_SM4_CCM_TYPE:
WOLFSSL_MSG("SM4 CCM");
XMEMCPY(&ctx->cipher.sm4.iv, ctx->iv, SM4_BLOCK_SIZE);
break;
The AES-CCM case of the same function is XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, ctx->ivSz) (src/ssl.c:10854). In the SM4-CCM case the destination and source are swapped relative to the AES case. 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:1638-1641). That increment never reaches ctx->iv, which is the buffer the encrypt and decrypt calls read.
The sequence for two messages:
- The application initialises SM4-CCM 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:8618-8620). - Message one:
wolfSSL_EVP_CipherFinal()(wolfcrypt/src/evp.c:1313) callswc_Sm4CcmEncrypt()orwc_Sm4CcmDecrypt()withctx->ivas the nonce (wolfcrypt/src/evp.c:1614-1623). - No nonce advance takes effect.
- Final zeroes
ctx->iv(wolfcrypt/src/evp.c:1650-1654) and callswolfSSL_StoreExternalIV()(wolfcrypt/src/evp.c:1655-1657). wolfSSL_StoreExternalIV()copies the zeroedctx->ivintoctx->cipher.sm4.iv(src/ssl.c:10973).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_CCMand the wolfSM add-on. - An application that either initialises an SM4-CCM EVP context with a NULL IV, or sends two or more messages on one context without a fresh IV for each.
- The attacker is a passive observer of the ciphertexts and tags, with two or more affected messages under the same key. Replaying or forging a message additionally requires the ability to send ciphertexts to the receiver.
- The attacker supplies no input to the sending context and needs no access to the process.
Impact
All messages after the first on such a context, and the first as well on a NULL-IV context, are processed under the same key and the all-zero nonce. The counter-mode keystream repeats, so the XOR of two ciphertexts is the XOR of their plaintexts and one known plaintext decrypts the others up to its length. An identical plaintext produces an identical ciphertext and tag. ByteRay's assessment is that the repeated key and nonce also undermine tag authenticity for that key; beyond replay, forgery was not demonstrated.
ByteRay tested SM4-CCM and SM4-GCM together 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. After the first Final the context IV was all zero, messages two to four were sealed under the all-zero nonce, a repeated plaintext produced a byte-identical ciphertext and tag, and two different messages showed the same keystream.
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 SM4-CCM case of wolfSSL_StoreExternalIV() so that it copies from ctx->cipher.sm4.iv into ctx->iv, as the AES cases do, and to advance the nonce after each successful Final so that the next message always gets a new value. Init should reject a missing IV for SM4-GCM and SM4-CCM and not leave the zero-filled buffer as the working nonce. A regression test that checks for distinct nonces across several messages on one SM4-CCM context would keep the behaviour from returning.
Applications can avoid the issue by passing an explicit, unique IV to the init call for every message on an SM4-CCM 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.