Skip to main content

Module hmac

Module hmac 

Source
Expand description

§HMAC authentication

HmacSha256, HmacSha512, and HmacSha512256 provide Rustaceous wrappers for libsodium’s direct HMAC authentication variants.

HMAC computes a fixed-size authentication tag for a message using a shared secret key. Anyone with the same key can recompute the tag and verify that the message was produced by someone who knew the key and that the message was not changed. HMAC does not encrypt the message.

Use these types when:

  • you need one of libsodium’s direct crypto_auth_hmacsha* variants
  • two parties already share the same secret key
  • the message can be public, but tampering must be detected

HmacSha512256 matches libsodium’s default crypto_auth construction. HmacSha256 and HmacSha512 are available for protocol compatibility when those exact algorithms are required.

§Rustaceous API example

use dryoc::hmac::{HmacSha256, HmacSha256Key};
use dryoc::types::*;

let key = HmacSha256Key::generate();
let message = b"Uneasy lies the head that wears a crown.";

let mac = HmacSha256::compute_to_vec(key.clone(), message);
HmacSha256::compute_and_verify(&mac, key, message).expect("verify failed");

The concrete authenticators are type aliases over Hmac and can also be used through HmacVariant in generic code.

§Incremental interface

use dryoc::hmac::{HmacSha512256, HmacSha512256Key};
use dryoc::types::*;

let key = HmacSha512256Key::generate();
let mut auth = HmacSha512256::new(key.clone());
auth.update(b"Though she be but little, ");
auth.update(b"she is fierce.");
let mac = auth.finalize_to_vec();

let mut verifier = HmacSha512256::new(key);
verifier.update(b"Though she be but little, ");
verifier.update(b"she is fierce.");
verifier.verify(&mac).expect("verify failed");

§Generic HMAC variants

use dryoc::constants::{CRYPTO_AUTH_HMACSHA256_BYTES, CRYPTO_AUTH_HMACSHA256_KEYBYTES};
use dryoc::hmac::{Hmac, HmacSha256, HmacSha256Key, HmacSha256Variant, HmacVariant};
use dryoc::types::*;

fn authenticate<Variant, const KEY_LENGTH: usize, const MAC_LENGTH: usize>(
    key: StackByteArray<KEY_LENGTH>,
    input: &[u8],
) -> Vec<u8>
where
    Variant: HmacVariant<KEY_LENGTH, MAC_LENGTH>,
{
    Hmac::<Variant, KEY_LENGTH, MAC_LENGTH>::compute_to_vec(key, input)
}

let key = HmacSha256Key::generate();
let message = b"The quality of mercy is not strained.";
let generic_mac = authenticate::<
    HmacSha256Variant,
    CRYPTO_AUTH_HMACSHA256_KEYBYTES,
    CRYPTO_AUTH_HMACSHA256_BYTES,
>(key.clone(), message);
let concrete_mac = HmacSha256::compute_to_vec(key, message);
assert_eq!(generic_mac, concrete_mac);

Modules§

protectedprotected
Protected memory type aliases for HMAC

Structs§

Hmac
Rustaceous HMAC authenticator for a specific HmacVariant.
HmacSha256Variant
HMAC-SHA-256 algorithm marker.
HmacSha512Variant
HMAC-SHA-512 algorithm marker.
HmacSha512256Variant
HMAC-SHA-512-256 algorithm marker.

Traits§

HmacVariant
HMAC algorithm variant used by Hmac.

Type Aliases§

HmacSha256
Rustaceous HMAC-SHA-256 authenticator.
HmacSha512
Rustaceous HMAC-SHA-512 authenticator.
HmacSha256Key
Stack-allocated key for HMAC-SHA-256.
HmacSha256Mac
Stack-allocated message authentication code for HMAC-SHA-256.
HmacSha512Key
Stack-allocated key for HMAC-SHA-512.
HmacSha512Mac
Stack-allocated message authentication code for HMAC-SHA-512.
HmacSha512256
Rustaceous HMAC-SHA-512-256 authenticator.
HmacSha512256Key
Stack-allocated key for HMAC-SHA-512-256.
HmacSha512256Mac
Stack-allocated message authentication code for HMAC-SHA-512-256.