cmac: take a NULL key in init as a restart with the cached key - #473
cmac: take a NULL key in init as a restart with the cached key#473yosuke-wolfssl wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes CMAC context reinitialization semantics so EVP_MAC_init(ctx, NULL, 0, NULL) correctly resets the CMAC state while retaining the cached key, aligning wolfProvider behavior with the documented EVP MAC contract and OpenSSL’s default provider behavior.
Changes:
- Update
wp_cmac_set_key()/wp_cmac_init()so a NULL key triggers a restart using the cached key (when present). - Add a unit test (
test_cmac_reinit) that exercises reinit-after-final and mid-stream reset behavior and compares outputs against OpenSSL. - Wire the new CMAC reinit test into the unit test harness.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/unit.h | Declares the new test_cmac_reinit unit test. |
| test/unit.c | Registers test_cmac_reinit in the unit test list. |
| test/test_cmac.c | Adds coverage for CMAC reset behavior (post-final and mid-stream) and OpenSSL equivalence checks. |
| src/wp_cmac.c | Implements CMAC restart-on-init with cached key when key == NULL. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #473
Scan targets checked: wolfprovider-bugs, wolfprovider-src
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
b4e345f to
e9d81e0
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #473
Scan targets checked: wolfprovider-bugs, wolfprovider-src
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
e9d81e0 to
24f9585
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #473
Scan targets checked: wolfprovider-bugs, wolfprovider-src
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
24f9585 to
e6adfa2
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #473
Scan targets checked: wolfprovider-bugs, wolfprovider-src
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Fenrir's latest completed scan found no issues; clearing the prior automated change request.
|
Jenkins retest please |
- wp_cmac_set_key() resolves a NULL key to the cached length in macCtx->keyLen and fails when that length is zero. The length, expKeySize and cleanse/copy steps run only for a non-NULL key. - The wc_InitCmac_ex()/wc_InitCmac() restart runs for any key, cached or supplied, and takes the resolved length; macCtx->keyLen is assigned after it succeeds. - wp_cmac_init() calls wp_cmac_set_key() for every init. - wp_cmac_update() and wp_cmac_final() fail when macCtx->keyLen is zero. - The key parameter notes on wp_cmac_set_key() and wp_cmac_init() record that NULL restarts with the cached key. - test_cmac_reinit drives one EVP_MAC_CTX through keyless init after final and mid-stream, comparing each MAC against OpenSSL, and checks that a keyless restart after a rejected key refuses both EVP_MAC_update() and EVP_MAC_final(). - test/unit.c and test/unit.h register test_cmac_reinit. Issue: F-11550
e6adfa2 to
b3a4bab
Compare
Problem
wp_cmac_init()only restarted the wolfSSL CMAC object when a key was supplied, soEVP_MAC_init(ctx, NULL, 0, NULL)— the documented way to reset a MAC context while keeping the installed key — did nothing:EVP_MAC_final()wc_CmacFinal()zeroes theCmac, so the nextEVP_MAC_update()returns 0finalmsgA‖msgBinstead ofmsgB, reporting success at every callSeparately,
wp_cmac_update()andwp_cmac_final()accepted a context whose key install had failed. The FIPS bundles'wc_CmacUpdate()/wc_CmacFinal()validate only pointers — nocmac->typecheck — so a context that never received a usable key returned a MAC computed with an all-zero AES key. Current non-FIPS wolfSSL rejects that viadefault: BAD_FUNC_ARG, which is why it reproduces only under FIPS.Fix (
src/wp_cmac.c)wp_cmac_set_key()takeskey == NULLas "keep the cached key, just restart":keyLenresolves frommacCtx->keyLen, and a zero resolved length fails. The length andexpKeySizechecks and the cleanse/copy ofmacCtx->keyare gated onkey != NULL, andmacCtx->keyLenis assigned only afterwc_InitCmac_ex()succeeds.wp_cmac_init()calls it on every init, so the AES key schedule and the k1/k2 subkeys are re-derived.wp_cmac_update()andwp_cmac_final()fail whenmacCtx->keyLenis 0, rather than relying on wolfSSL to reject an uninitializedCmac.Closes f_11550.
Tests
test_cmac_reinitdrives oneEVP_MAC_CTXthrough keyless init afterfinaland mid-stream, comparing every MAC against OpenSSL, and checks that a keyless restart after a rejected key refuses bothEVP_MAC_update()andEVP_MAC_final().Verification
v5.2.4FIPS bundle; clean under-Werror.wp_cmac.cfails the reinit legs; removing only thefinalguard fails the newfinalcase.PRB-fips-scripts-testfailure exactly — test 22,CMAC produced a MAC from a key that was never set; it passes after.