Skip to main content

Module auth

Module auth 

Source
Expand description

§Secret-key message authentication

Auth implements libsodium’s secret-key authentication, based on HMAC-SHA512-256.

Use Auth to authenticate messages when:

  • you want to authenticate arbitrary messages
  • you have a pre-shared key between both parties
  • (optionally) you want to share the authentication tag publicly

The same HMAC key can authenticate multiple messages. Keep the key secret, and use separate keys when protocols require domain separation.

§Rustaceous API example, single-part interface

use dryoc::auth::*;
use dryoc::types::*;

// Generate a random key
let key = Key::generate();

// Compute the MAC in one shot. This API takes ownership of the key, so clone
// it when the same key is also needed for verification.
let mac = Auth::compute_to_vec(key.clone(), b"Data to authenticate");

// Verify the MAC
Auth::compute_and_verify(&mac, key, b"Data to authenticate").expect("verify failed");

§Rustaceous API example, incremental interface

use dryoc::auth::*;
use dryoc::types::*;

// Generate a random key
let key = Key::generate();

// Initialize the MAC
let mut mac = Auth::new(key.clone());
mac.update(b"Multi-part");
mac.update(b"data");
let mac = mac.finalize_to_vec();

// Verify the MAC
let mut verify_mac = Auth::new(key.clone());
verify_mac.update(b"Multi-part");
verify_mac.update(b"data");
verify_mac.verify(&mac).expect("verify failed");

// Check that invalid data fails
let mut verify_mac = Auth::new(key);
verify_mac.update(b"Multi-part");
verify_mac.update(b"bad data");
verify_mac
    .verify(&mac)
    .expect_err("verify should have failed");

Modules§

protectedprotected
Protected memory type aliases for Auth

Structs§

Auth
Secret-key authentication implementation based on libsodium’s HMAC-SHA512-256 crypto_auth_* functions.

Type Aliases§

Key
Stack-allocated key for secret-key authentication.
Mac
Stack-allocated message authentication code for secret-key authentication.