Skip to main content

Module hmac

Module hmac 

Source
Expand description

Hash-based message authentication from https://datatracker.ietf.org/doc/html/rfc2104.

HMAC-SHA256 and HMAC-SHA512 are supported.

MACs can be computed in a single shot:

use bssl_crypto::hmac::HmacSha256;

let mac: [u8; 32] = HmacSha256::mac(b"key", b"hello");

Or they can be computed incrementally:

use bssl_crypto::hmac::HmacSha256;

let key = bssl_crypto::rand_array();
let mut ctx = HmacSha256::new(&key);
ctx.update(b"hel");
ctx.update(b"lo");
let mac: [u8; 32] = ctx.digest();

WARNING comparing MACs using typical methods will often leak information about the size of the matching prefix. Use the verify method instead.

If you need to compute many MACs with the same key, contexts can be cloned:

use bssl_crypto::hmac::HmacSha256;

let key = bssl_crypto::rand_array();
let mut keyed_ctx = HmacSha256::new(&key);
let mut ctx1 = keyed_ctx.clone();
ctx1.update(b"foo");
let mut ctx2 = keyed_ctx.clone();
ctx2.update(b"foo");

assert_eq!(ctx1.digest(), ctx2.digest());

Structsยง

HmacSha256
HMAC-SHA256.
HmacSha512
HMAC-SHA512.