Skip to content

cmac: take a NULL key in init as a restart with the cached key - #473

Open
yosuke-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f_11550
Open

cmac: take a NULL key in init as a restart with the cached key#473
yosuke-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f_11550

Conversation

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor

Problem

wp_cmac_init() only restarted the wolfSSL CMAC object when a key was supplied, so EVP_MAC_init(ctx, NULL, 0, NULL) — the documented way to reset a MAC context while keeping the installed key — did nothing. Two failure modes:

Reset point Result
After EVP_MAC_final() wc_CmacFinal() frees and zeroes the Cmac, so the next EVP_MAC_update() returns 0
Mid-stream, before final digest/buffer/totalSz survive, so the MAC silently covers msgA‖msgB instead of msgB

The second case reports success at every call — a wrong MAC with no error. OpenSSL's default provider implements the reset explicitly (cmac_prov.cCMAC_Init(ctx, NULL, 0, NULL, NULL)), and OpenSSL's evp_test exercises it for every MAC KAT: scripts/evp_test/evpmac_common.txt carries three CMAC entries with no NoReinit, so scripts/test-openssl.sh reproduces this today.

Fix (src/wp_cmac.c)

  • wp_cmac_set_key() takes key == NULL as "keep the cached key, just restart". The length and expKeySize checks and the cleanse/copy of macCtx->key are gated on key != NULL; a NULL key with nothing cached fails.
  • wp_cmac_init() calls it whenever a key is supplied or one is already cached, so wc_InitCmac_ex() runs on every init and re-derives the AES key schedule and the k1/k2 subkeys.

A context that never received a key still returns success from init, matching wp_hmac_init() and wp_gmac_init(). OpenSSL fails there, but that divergence is uniform across all three wolfProvider MACs and belongs in its own change.

Closes f_11550.

Tests

test_cmac_reinit drives one EVP_MAC_CTX through init/update/final rounds with no key argument, plus an init mid-stream, and compares every MAC against OpenSSL.

Verification

  • Build clean under -Werror.
  • Negative control fails on both facets before the fix: EVP_MAC_update returns 0 after final, and CMAC after mid-stream reset covers stale data on the mid-stream path.
  • 239/239 unit tests pass in both the default and --enable-debug configs; all five CMAC cases pass.

@yosuke-wolfssl yosuke-wolfssl self-assigned this Aug 26, 2026
Copilot AI lite review requested due to automatic review settings August 26, 2026 04:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/wp_cmac.c

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/wp_cmac.c
Comment thread src/wp_cmac.c

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/test_cmac.c
- wp_cmac_set_key() substitutes the cached length for a NULL key,
  rejects a zero length, and applies the maximum-length and
  expKeySize checks only to a supplied key.
- wp_cmac_set_key() clears macCtx->keyLen while copying a supplied
  key into macCtx->key, runs the wolfSSL CMAC init from that
  buffer, and assigns macCtx->keyLen once that call succeeds.
- wp_cmac_init() calls wp_cmac_set_key() with restart set whenever
  a key is passed in or one is already cached.
- The doxygen for the key parameter of both functions covers the
  NULL case.
- test_cmac_reinit drives one EVP_MAC_CTX through init, update and
  final rounds with no key argument and through an init
  mid-stream, comparing the MACs with OpenSSL.
- test_cmac_reinit_stale_key checks that a key rejected for want
  of a cipher is not picked up by a later keyless init.
- Both new tests run against OpenSSL and wolfProvider;
  test_cmac_reinit is declared in unit.h and registered in unit.c.

Issue: F-11550
Comment thread test/test_cmac.c

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/wp_cmac.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants