wolfSSL — Wrong Union Member Read in the EVP GCM IV Generation Control for SM4-GCM
The EVP_CTRL_GCM_IV_GEN control tests whether a key is set by reading the AES member of the cipher union, also when the context holds SM4-GCM state. For SM4-GCM the test is decided by unrelated bytes, so the control can fail on a keyed context or succeed on an unkeyed one.
- Advisory
- BYTERAY-2026-0427
- CWE
- CWE-323
- CVSS
- 3.7 (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:N)
- Vendor
- wolfSSL
- Product
- wolfSSL
- Reported
- 2026-09-28
- Disclosure
- 2026-12-03
- Affected
- wolfSSL 5.9.2 through 5.9.4, builds with OPENSSL_EXTRA and WOLFSSL_SM4_GCM (--enable-sm4-gcm with the wolfsm sources)
- Fixed in
- Master commit 31fd98b76d6c383d3c8968bc5fbd02acb3f66c9b (PR 11649, merged 2026-12-02). Not in a wolfSSL stable release as of 2026-12-03.
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
EVP_CTRL_GCM_IV_GEN is the control an application uses to send several GCM messages on one EVP context without re-initialising it. It hands the current IV to the caller and sets a flag that makes the next cipher operation advance the IV. Before it does that, it checks that a key and an IV are present.
The control is compiled for AES-GCM and for SM4-GCM, but the key check reads ctx->cipher.aes.keylen in both cases. ctx->cipher is a union. In an SM4-GCM context the active member is ctx->cipher.sm4, so the check reads bytes that are not an AES key length. If those bytes are zero, the control returns failure on a context that has a key and does not arm the IV advance. If they are non-zero, the control passes whether or not a key was ever set.
The read stays inside the union, so there is no memory-safety effect. The defect makes the control's precondition meaningless for SM4-GCM. Default builds are not affected: SM4 needs the separate wolfsm sources and --enable-sm4-gcm, and the control is part of the OpenSSL compatibility layer.
Affected Products and Versions
- Component: OpenSSL compatibility layer,
wolfSSL_EVP_CIPHER_CTX_ctrl()inwolfcrypt/src/evp.c, caseWOLFSSL_EVP_CTRL_GCM_IV_GEN. - Required build:
OPENSSL_EXTRA,WOLFSSL_SM4_GCM(configure.ac:4249-4263, with the SM4 implementation from the wolfsm repository), AES enabled, not Windows, not a FIPS v1 or selftest build (wolfcrypt/src/evp.c:6790-6791). - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- The
v5.9.2-stableandv5.9.4-stabletags contain the same check. Fixed on master by commit 31fd98b76d6c383d3c8968bc5fbd02acb3f66c9b (PR 11649); no release contains the fix. - A default
./configurebuild does not include this code. Earlier releases were not checked.
Technical Details
The cipher state of an EVP context is a union with one member per algorithm (wolfssl/openssl/evp.h:254-281). Aes aes and wc_Sm4 sm4 are two of the members. SM4-GCM initialisation writes only the SM4 member: wolfSSL_EVP_CipherInit() calls wc_Sm4GcmSetKey(&ctx->cipher.sm4, ...) at wolfcrypt/src/evp.c:8582-8587.
The control, at wolfcrypt/src/evp.c:6798-6809:
case WOLFSSL_EVP_CTRL_GCM_IV_GEN:
if ((ctx->flags & WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER) == 0)
break;
if (!ctx->authIvGenEnable) {
WOLFSSL_MSG("Must use EVP_CTRL_AEAD_SET_IV_FIXED before "
"EVP_CTRL_GCM_IV_GEN");
break;
}
if (ctx->cipher.aes.keylen == 0 || ctx->ivSz == 0) {
WOLFSSL_MSG("Key or IV not set");
break;
}
Nothing in the case looks at ctx->cipherType. The first test accepts any AEAD context. The second is satisfied once the application has called EVP_CTRL_AEAD_SET_IV_FIXED, which sets authIvGenEnable for SM4-GCM as well (wolfcrypt/src/evp.c:6777-6786). The third reads keylen, which in struct Aes follows the 60-word key schedule and the round count (wolfssl/wolfcrypt/aes.h:346-356).
For an SM4-GCM context the bytes at that offset belong to whichever wc_Sm4 field lies there, or to no SM4 field if the structure is shorter than that offset. wc_Sm4 is not defined in this repository; wolfssl/wolfcrypt/sm4.h:22-26 is a stub that points to wolfsm. The value the check sees therefore depends on the wolfsm structure layout and the build options, and cannot be derived from the wolfSSL tree alone.
- Value zero: the case breaks with the function's initial return value,
WOLFSSL_FAILURE(wolfcrypt/src/evp.c:6677).ctx->authIncIvis not set. - Value non-zero: the case continues to
wolfcrypt/src/evp.c:6829-6830, setsctx->authIncIv = 1and returns success. This happens even if no SM4 key was installed, which is the state the check exists to reject.
authIncIv is what the SM4-GCM paths test before they advance the IV (wolfcrypt/src/evp.c:1587-1590 in wolfSSL_EVP_CipherFinal(), wolfcrypt/src/evp.c:9255-9259 in wolfSSL_EVP_Cipher()). The advance itself has a separate defect at the tested commit, covered in BYTERAY-2026-0323: it increments ctx->cipher.sm4.iv, while the SM4-GCM encrypt and decrypt calls take the nonce from ctx->iv.
Reachability and Preconditions
- Build as listed above. The control is compiled out on Windows.
- The application creates an SM4-GCM EVP context, calls
EVP_CTRL_AEAD_SET_IV_FIXED, thenEVP_CTRL_GCM_IV_GEN. - The trigger is the local application's own API call. No peer input reaches the check, and a remote party cannot influence which way it resolves.
- No authentication state or user interaction is involved.
Impact
Confirmed from source: on SM4-GCM contexts the key-present check of EVP_CTRL_GCM_IV_GEN does not test SM4 state. The control either reports failure on a correctly keyed context, leaving the IV advance unarmed, or reports success on a context without a key. In the failure case the application gets WOLFSSL_FAILURE from the control and can see the problem. An application that does not check the return value continues with a context whose IV handling is not what it asked for.
Not established: which of the two outcomes a given wolfsm build produces. ByteRay did not examine the wolfsm structure layout. Nonce reuse on SM4-GCM contexts at the tested commit is a consequence of the Final path described in BYTERAY-2026-0323 and is not attributed to this check. There is no out-of-bounds access.
ByteRay did not build a proof of concept for this issue. The analysis is from source review.
Remediation and Mitigation
Fixed on master by commit 31fd98b76d6c383d3c8968bc5fbd02acb3f66c9b (PR 11649, merged 2026-12-02). The control now branches on the cipher type. SM4-GCM and SM4-CCM contexts are tested with ctx->cipher.sm4.keySet, ARIA-GCM contexts with their own key handle, ChaCha20-Poly1305 contexts are refused, and the AES test is used only for the remaining AES cases and only when AES is compiled in. The same commit changes the SM4 paths to advance ctx->iv. The fix is on master only. No release contains it as of 2026-12-03.
Until a release does, applications on affected builds should not use EVP_CTRL_GCM_IV_GEN with SM4-GCM. Call EVP_CipherInit with a fresh, unique IV for every message instead, and check the return value of every EVP control call.
Timeline
- 2026-09-28 — Reported to wolfSSL.
- 2026-10-05 — Pull request 11649 opened upstream.
- 2026-12-02 — Pull request 11649 merged to master.
- 2026-12-03 — Public disclosure.
Credits
Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.