Crate cmac

source ·
Expand description

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

Examples

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

To get the authentication code:

use aes::Aes128;
use cmac::{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:

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

mac.update(b"input message");

// `verify` will return `Ok(())` if tag is correct, `Err(MacError)` otherwise
mac.verify(&tag_bytes).unwrap();

Re-exports

Structs

  • Generic core CMAC instance, which operates over blocks.

Traits

  • Convenience wrapper trait covering functionality of Message Authentication algorithms.

Type Definitions

  • Generic CMAC instance.