Skip to content

feat: add AES-CMAC and HMAC-SHA-384/SHA3 variants - #190

Merged
WingZer0o merged 4 commits into
mainfrom
159-macs-aes-cmac-hmac-sha384-hmac-sha3
Nov 27, 2026
Merged

WingZer0o merged 4 commits into
mainfrom
159-macs-aes-cmac-hmac-sha384-hmac-sha3

Conversation

@WingZer0o

@WingZer0o WingZer0o commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Closes #159
Closes #154

Summary

Adds the missing standard MACs to the message module: AES-CMAC (NIST SP 800-38B), HMAC-SHA-384 and HMAC over SHA-3.

CMAC (src/message/cmac.rs, src/message/cas_cmac.rs)

  • New CASCMAC trait (sign / verify), implemented by CMACAES128 (16-byte key) and CMACAES256 (32-byte key), built on cmac 0.8 with aes 0.9.
  • Both always produce the full 16-byte tag. A key of the wrong length returns CasError::InvalidKey (no new error variant, no panic). verify uses Mac::verify_slice (constant time) and returns Ok(false) for a wrong, tampered or truncated tag.
  • The key argument is zeroized after use. cmac and aes are built with their zeroize feature, so the CMAC state and AES key schedules are wiped on drop. This also applies to the existing AES-GCM and AES-KW code.
  • New dependencies: cmac 0.8.0 and dbl 0.5.0 (aes 0.9.4 was already in the tree).
  • Layout differs from the module pattern: the trait lives in cmac.rs and the impl in cas_cmac.rs, the reverse of cas_<module>.rs for the trait and <algorithm>.rs for the impl.
  • No AES-192 CMAC, to match the AES-128/256 sizes the rest of the library offers.

HMAC (src/message/hmac.rs)

  • New: HMACSha384, HMACSha3_256, HMACSha3_384, HMACSha3_512. Existing: HMACSha512, and HMAC renamed to HMACSha256.
  • HMAC is kept as a #[deprecated] alias for HMACSha256, so downstream code (including the FFI binding crates) still compiles but gets a deprecation warning.
  • sha3 0.12 has no block-level core, so Sha3_* can't be used with Hmac. The SHA-3 variants use SimpleHmac, which computes the same RFC 2104 HMAC over any Digest, the same approach PBKDF2-HMAC-SHA3 took in chore(deps): upgrade sha2, sha3, blake2, hmac and pbkdf2 to digest 0.11 #173. The SHA-2 variants still use Hmac.

#154: digest 0.11 migration

The migration itself landed in #173. Re-checked on this branch:

  • sha2 0.11.2, sha3 0.12.0, blake2 0.11.0, hmac 0.13.0 and pbkdf2 0.13.0 are in place, and the sha2-hkdf alias is gone.
  • argon2, scrypt, hpke, rsa and ed25519-dalek all resolve digest 0.11.3. None of them pull digest 0.10.
  • The only crate still on digest 0.10 is slh-dsa 0.0.3. It accounts for every remaining old-generation duplicate (digest 0.10, hmac 0.12, sha2 0.10, sha3 0.10, pkcs8 0.10, signature 2, rand_core 0.6 and the crates under them). Stable slh-dsa 0.1.0 is still on digest 0.10, and 0.2.0 is only at rc.5 (it needs sha3 0.11). The upgrade is tracked in Track slh-dsa 0.2.0 (currently 0.2.0-rc.5) #171.
  • cargo audit --deny unsound --deny yanked: clean.

Tests

  • CMAC: NIST SP 800-38B Appendix D examples for AES-128 and AES-256 are committed in tests/data/cmac/ and used by tests/message.rs. The AES-128 examples are also the RFC 4493 vectors. The tests also check a tampered tag, a truncated tag, wrong key lengths and a round trip.
  • CMAC, Wycheproof: aes_cmac_test.json runs 209 cases. The 102 AES-192 cases are skipped (no AES-192 CMAC), and the 6 invalid key sizes must return InvalidKey.
  • HMAC, Wycheproof: hmac_sha384_test.json and hmac_sha3_{256,384,512}_test.json are added next to the existing SHA-256/512 files.
  • HMAC, NIST: every variant now has committed NIST vectors, all run by tests/message.rs (sources and checksums in tests/data/hmac/README.md):
    • SHA-256/384/512: the [L=32], [L=48] and [L=64] blocks of NIST CAVP HMAC.rsp in tests/data/hmac/HMAC.txt, unmodified (225, 300 and 375 records). sign must match every record. For the 75 full-length tags in each block, verify must accept the tag and reject it with one byte flipped. [L=48] and [L=64] are new; [L=32] is unchanged.
    • SHA3-256/384/512: NIST ACVP internalProjection.json files (usnistgov/ACVP-Server@975de31), committed unmodified in tests/data/hmac/acvp/, 150 cases each. CAVP has no HMAC-SHA3 files. Every ACVP tag is truncated (10 to 20 bytes), so these check sign, and Wycheproof covers verify with full-length tags.
    • Sanity check: wiring HMACSha3_256 to SHA3-512 makes its ACVP test fail.
  • HMAC, tests/message.rs: round-trip and tag-length checks for all six variants, a check that all six give different tags (to catch a variant wired to the wrong hash), and a check that the deprecated HMAC alias still equals HMACSha256.
  • Not done: CMAC uses the SP 800-38B examples rather than the full CAVP CMACGenAES file.
  • cargo build --release --locked and cargo test --release --locked pass on Linux (WSL). This PR's CI covers Windows and macOS.

Docs

  • docs/EXAMPLES.md: new "Message Authentication (CMAC)" section and Table of Contents entry. The HMAC section now has a table of all six variants and an updated example. Both examples were compiled and run in a fresh crate.
  • CLAUDE.md module table and the Wycheproof README list updated.

🤖 Generated with Claude Code

WingZer0o and others added 3 commits November 27, 2026 13:07
cmac uses a regular block cipher of aes instead of a cipher mode like GCM
Adds HMACSha384, HMACSha3_256, HMACSha3_384 and HMACSha3_512 alongside
HMACSha512, and renames HMAC to HMACSha256 (HMAC stays as a deprecated
alias). sha3 0.12 has no block-level core, so the SHA-3 variants use
SimpleHmac, which computes the same RFC 2104 HMAC over any Digest.

Tests: Wycheproof hmac_sha384 and hmac_sha3_{256,384,512} vectors, plus
round-trip, tag-length and distinct-variant checks for all six.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@WingZer0o WingZer0o linked an issue Nov 27, 2026 that may be closed by this pull request
3 tasks done
- tests/data/hmac/HMAC.txt now carries the [L=48] (SHA-384) and [L=64]
  (SHA-512) blocks of NIST CAVP HMAC.rsp next to [L=32], unmodified.
- tests/data/hmac/acvp/: NIST ACVP HMAC-SHA3-256/384/512
  internalProjection.json files (usnistgov/ACVP-Server@975de31), unmodified.
- tests/message.rs runs every record: 225/300/375 CAVP records for
  SHA-256/384/512 (sign, plus verify and a tampered tag for the 75
  full-length ones each) and 150 ACVP cases per SHA-3 variant.
- tests/data/hmac/README.md records the sources and checksums.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@WingZer0o
WingZer0o merged commit 894de46 into main Nov 27, 2026
9 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 27, 2026
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.

MACs: AES-CMAC, HMAC-SHA384, HMAC-SHA3 Move the hash/MAC/PBKDF2 dependencies to the digest 0.11 ecosystem

1 participant