Repository navigation
feat: add AES-CMAC and HMAC-SHA-384/SHA3 variants - #190
Merged
Merged
Conversation
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>
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>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #159
Closes #154
Summary
Adds the missing standard MACs to the
messagemodule: AES-CMAC (NIST SP 800-38B), HMAC-SHA-384 and HMAC over SHA-3.CMAC (
src/message/cmac.rs,src/message/cas_cmac.rs)CASCMACtrait (sign/verify), implemented byCMACAES128(16-byte key) andCMACAES256(32-byte key), built oncmac0.8 withaes0.9.CasError::InvalidKey(no new error variant, no panic).verifyusesMac::verify_slice(constant time) and returnsOk(false)for a wrong, tampered or truncated tag.cmacandaesare built with theirzeroizefeature, so the CMAC state and AES key schedules are wiped on drop. This also applies to the existing AES-GCM and AES-KW code.cmac0.8.0 anddbl0.5.0 (aes0.9.4 was already in the tree).cmac.rsand the impl incas_cmac.rs, the reverse ofcas_<module>.rsfor the trait and<algorithm>.rsfor the impl.HMAC (
src/message/hmac.rs)HMACSha384,HMACSha3_256,HMACSha3_384,HMACSha3_512. Existing:HMACSha512, andHMACrenamed toHMACSha256.HMACis kept as a#[deprecated]alias forHMACSha256, so downstream code (including the FFI binding crates) still compiles but gets a deprecation warning.Sha3_*can't be used withHmac. The SHA-3 variants useSimpleHmac, which computes the same RFC 2104 HMAC over anyDigest, 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 useHmac.#154: digest 0.11 migration
The migration itself landed in #173. Re-checked on this branch:
sha2-hkdfalias is gone.digest0.11.3. None of them pulldigest0.10.digest0.10 isslh-dsa0.0.3. It accounts for every remaining old-generation duplicate (digest0.10,hmac0.12,sha20.10,sha30.10,pkcs80.10,signature2,rand_core0.6 and the crates under them). Stableslh-dsa0.1.0 is still ondigest0.10, and 0.2.0 is only at rc.5 (it needssha30.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
tests/data/cmac/and used bytests/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.aes_cmac_test.jsonruns 209 cases. The 102 AES-192 cases are skipped (no AES-192 CMAC), and the 6 invalid key sizes must returnInvalidKey.hmac_sha384_test.jsonandhmac_sha3_{256,384,512}_test.jsonare added next to the existing SHA-256/512 files.tests/message.rs(sources and checksums intests/data/hmac/README.md):[L=32],[L=48]and[L=64]blocks of NIST CAVPHMAC.rspintests/data/hmac/HMAC.txt, unmodified (225, 300 and 375 records).signmust match every record. For the 75 full-length tags in each block,verifymust accept the tag and reject it with one byte flipped.[L=48]and[L=64]are new;[L=32]is unchanged.internalProjection.jsonfiles (usnistgov/ACVP-Server@975de31), committed unmodified intests/data/hmac/acvp/, 150 cases each. CAVP has no HMAC-SHA3 files. Every ACVP tag is truncated (10 to 20 bytes), so these checksign, and Wycheproof coversverifywith full-length tags.HMACSha3_256to SHA3-512 makes its ACVP test fail.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 deprecatedHMACalias still equalsHMACSha256.cargo build --release --lockedandcargo test --release --lockedpass 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.mdmodule table and the Wycheproof README list updated.🤖 Generated with Claude Code