Skip to content

Fix CMac init: size zero IV to cipher block size, not key length#263

Open
FerroLx wants to merge 1 commit into
bcgit:masterfrom
munimen-security:fix/cmac-iv-block-size
Open

Fix CMac init: size zero IV to cipher block size, not key length#263
FerroLx wants to merge 1 commit into
bcgit:masterfrom
munimen-security:fix/cmac-iv-block-size

Conversation

@FerroLx

@FerroLx FerroLx commented Jul 24, 2026

Copy link
Copy Markdown

Problem

CMac.init sizes the initial zero IV to keyParams.key.length:

final zeroIV = Uint8List(keyParams.key.length);

For AES-128 the key length (16) equals the AES block size (16), so it works. For
AES-192/256 the IV becomes 24/32 bytes and CBCBlockCipher.init throws
Initialization vector must be the same length as block size — CMAC with those
key sizes is unusable.

Fix

Per NIST SP 800-38B, CMAC subkey derivation encrypts a zero block of the cipher
block size
(independent of key length):

final zeroIV = Uint8List(_cipher.blockSize);

For AES-128 this is identical (16 == 16); for AES-192/256 it is correct instead of throwing.

Impact / how it was found

Surfaced reading an ICAO 9303 eMRTD (Portuguese Cartão de Cidadão) whose PACE secure
messaging negotiates AES-256: the PACE mutual-auth token and SM MAC use CMAC-AES-256
and crashed here. The bug is fail-closed (it raised, never produced a weak MAC), but it
blocks CMAC-AES-192/256 entirely. Verified end-to-end against a real card after this fix.

CMac.init sized the initial zero IV to `keyParams.key.length`. For AES-128 the
key length (16) coincides with the AES block size (16), so it worked; but for
AES-192/256 the IV became 24/32 bytes and CBCBlockCipher.init throws
"Initialization vector must be the same length as block size", making CMAC with
those key sizes unusable.

Per NIST SP 800-38B the CMAC subkey derivation runs the block cipher over a zero
block of the CIPHER BLOCK SIZE, independent of key length. Use `_cipher.blockSize`.

This is fail-closed (it raised, never produced a weak MAC), but it blocks CMAC-AES-256
outright — e.g. reading ICAO 9303 eMRTDs that negotiate AES-256 PACE secure messaging.
@FerroLx
FerroLx force-pushed the fix/cmac-iv-block-size branch from 14cf7ae to 95059e4 Compare July 24, 2026 10:15
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.

1 participant