Skip to content

crypto/cryptosoft: do not skip an IV byte in the CMAC authenc path - #21930

Merged
GUIDINGLI merged 1 commit into
apache:masterfrom
ThePassionate:crypto-cmac-fix
Nov 17, 2026
Merged

GUIDINGLI merged 1 commit into
apache:masterfrom
ThePassionate:crypto-cmac-fix

Conversation

@ThePassionate

@ThePassionate ThePassionate commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The software AES-CMAC computation through a composite cryptodev session is wrong for every message that carries data: the 16-byte NIST SP 800-38B / RFC 4493 vector returns f212d4c2154c8766de60c18c98fa0c93 instead of 070a16b46b4d4144f79bdd9dd04a287c. The pre-existing aescmac test in nuttx-apps fails on its second case on unmodified master.

Root cause, in swcr_authenc():

  • A composite request without an explicit IV was assumed to carry the IV at the start of the source buffer, so crd_skip was advanced by exf->blocksize and crd_len was shortened by the same amount. The AES-CMAC cipher xform has no IV (ivsize 0) and a block size of 1, so a plain CMAC request got skip = 1, len = len - 1.
  • The data loop then copies from buf + i and ignores crd_skip entirely, so the MAC is computed over the first len - 1 bytes of the message instead of the full message.

The empty-message case passes only by luck: the length becomes negative and the loop does not run at all.

Fix: only apply the prepended-IV adjustment when the xform actually has an IV (ivsize > 0); leave skip and length untouched otherwise. One line, no behavior change for the xforms that do carry a prepended IV.

Impact

  • crypto/cryptosoft.c, one line. No interface change.
  • Fixes every CMAC-via-composite-session computation of non-empty messages.
  • The GCM/GMAC/ChaCha20-Poly1305 composite paths are unchanged: they either use an explicit IV or have ivsize > 0, and the existing chachapoly test (explicit IV) passes unchanged.

Testing

Board sim:crypto (nuttx master 4bf334e). Before the fix, aescmac (nuttx-apps) stops at case 2 (the 16-byte vector) with the wrong digest above; after the fix all four NIST cases pass:

nsh> aescmac
aescbc test 0 ok      (0-byte message,  bb1d6929...)
aescbc test 1 ok      (16-byte message, 070a16b4...  <- was f212d4c2...)
aescbc test 2 ok      (20-byte message, d1d67b76...)
aescbc test 3 ok      (64-byte message, 51f0bea7...)

(The success message of the upstream aescmac test prints aescbc - a copy-paste in the test itself; the four cases above are the AES-CMAC vectors of RFC 4493 / NIST SP 800-38B.)

The wrong value is reproducible analytically: AES-CMAC over the first 15 bytes of the 16-byte vector with the same key yields exactly f212d4c2..., confirming the one-byte truncation.

Regression: aescbc, hmac (20 vectors), chacha20 and chachapoly pass unchanged. tools/checkpatch.sh -c -u -m -g <base>..HEAD passes.

swcr_authenc() assumed that a composite request without an explicit
IV carries the IV at the start of the source buffer, so it advanced
crd_skip by the block size and shortened crd_len by the same amount.
The CMAC cipher xform has no IV (ivsize 0) and a block size of 1,
so for a plain AES-CMAC request the message was truncated by one
byte and the MAC was computed over len-1 bytes: the NIST 16-byte
vector returned f212d4c2154c8766de60c18c98fa0c93 instead of the
RFC 4493 value 070a16b46b4d4144f79bdd9dd04a287c.  The empty-message
case only passed by luck, because the negative length kept the loop
from running at all.

Only apply the prepended-IV adjustment when the xform actually has an
IV (ivsize > 0); leave the skip and length untouched otherwise.

Signed-off-by: makejian <makejian@xiaomi.com>
@github-actions github-actions Bot added Size: XS The size of the change in this PR is very small Area: Crypto labels Nov 17, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@GUIDINGLI
GUIDINGLI merged commit ed4cc5b into apache:master Nov 17, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Crypto Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants