#message-authentication #mac #omac

no-std cmac

Generic implementation of Cipher-based Message Authentication Code

22 releases

Uses new Rust 2024

new 0.8.0 Apr 10, 2026
0.8.0-rc.5 Mar 25, 2026
0.8.0-rc.4 Feb 2, 2026
0.8.0-rc.3 Nov 5, 2025
0.0.0 Jul 22, 2017

#2700 in Cryptography

Download history 111243/week @ 2025-12-19 54768/week @ 2025-12-26 229102/week @ 2026-01-02 380623/week @ 2026-01-09 473937/week @ 2026-01-16 434458/week @ 2026-01-23 421429/week @ 2026-01-30 374045/week @ 2026-02-06 348569/week @ 2026-02-13 423979/week @ 2026-02-20 491512/week @ 2026-02-27 483998/week @ 2026-03-06 391863/week @ 2026-03-13 243728/week @ 2026-03-20 245778/week @ 2026-03-27 248637/week @ 2026-04-03

1,215,946 downloads per month
Used in 237 crates (39 directly)

MIT/Apache

1MB
152 lines

RustCrypto: CMAC

crate Docs Build Status Apache2/MIT licensed Rust Version Project Chat

Generic implementation of Cipher-based Message Authentication Code (CMAC), otherwise known as OMAC1.

Examples

We will use AES-128 block cipher from the aes crate.

To get the authentication code:

use aes::Aes128;
use cmac::{digest::KeyInit, Cmac, Mac};

// Create `Mac` trait implementation, namely CMAC-AES128
let mut mac = Cmac::<Aes128>::new_from_slice(b"very secret key.").unwrap();
mac.update(b"input message");

// `result` has type `Output` which is a thin wrapper around array of
// bytes for providing constant time equality check
let result = mac.finalize();
// To get underlying array use the `into_bytes` method, but be careful,
// since incorrect use of the tag value may permit timing attacks which
// defeat the security provided by the `Output` wrapper
let tag_bytes = result.into_bytes();

To verify the message:

use aes::Aes128;
use cmac::{digest::KeyInit, Cmac, Mac};

let mut mac = Cmac::<Aes128>::new_from_slice(b"very secret key.").unwrap();

mac.update(b"input message");

# let tag_bytes = mac.clone().finalize().into_bytes();
// `verify` will return `Ok(())` if tag is correct, `Err(MacError)` otherwise
mac.verify(&tag_bytes).unwrap();

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies