Skip to content

Commit 10953f0

Browse files
committed
CMAC: fix wraparound in streaming update.
The guard `if (cmac->totalSz != 0)` was used to skip XOR-chaining on the first block (where digest is all-zeros and the XOR is a no-op). However, totalSz is word32 and wraps to zero after 2^28 block flushes (4 GiB), causing the guard to erroneously fire again and discard the live CBC-MAC chain state. Any two messages sharing a common suffix beyond the 4 GiB mark then produce identical CMAC tags, enabling a zero-work prefix-substitution forgery. The fix removes the guard, making the XOR unconditional; the no-op property on the first block is preserved because digest is zero-initialized by wc_InitCmac_ex. Identified by: Nicholas Carlini (Anthropic) & Thai Duong (Calif.io)
1 parent 24f9981 commit 10953f0

2 files changed

Lines changed: 2 additions & 6 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16377,9 +16377,7 @@ int wc_local_CmacUpdateAes(struct Cmac *cmac, const byte* in, word32 inSz) {
1637716377
in += add;
1637816378

1637916379
if (cmac->bufferSz == WC_AES_BLOCK_SIZE && inSz != 0) {
16380-
if (cmac->totalSz != 0) {
16381-
xorbuf(cmac->buffer, cmac->digest, WC_AES_BLOCK_SIZE);
16382-
}
16380+
xorbuf(cmac->buffer, cmac->digest, WC_AES_BLOCK_SIZE);
1638316381
ret = AesEncrypt_preFetchOpt(aes, cmac->buffer,
1638416382
cmac->digest, &did_prefetches);
1638516383
if (ret == 0) {

wolfcrypt/src/cmac.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ int wc_CmacUpdate(Cmac* cmac, const byte* in, word32 inSz)
238238
inSz -= add;
239239

240240
if (cmac->bufferSz == WC_AES_BLOCK_SIZE && inSz != 0) {
241-
if (cmac->totalSz != 0) {
242-
xorbuf(cmac->buffer, cmac->digest, WC_AES_BLOCK_SIZE);
243-
}
241+
xorbuf(cmac->buffer, cmac->digest, WC_AES_BLOCK_SIZE);
244242
wc_AesEncryptDirect(&cmac->aes, cmac->digest,
245243
cmac->buffer);
246244
cmac->totalSz += WC_AES_BLOCK_SIZE;

0 commit comments

Comments
 (0)