Skip to main content

Module onetimeauth

Module onetimeauth 

Source
Expand description

§One-time authentication

OnetimeAuth implements libsodium’s one-time authentication, based on the Poly1305 message authentication code.

Use OnetimeAuth to authenticate messages when:

  • you need to authenticate a message with a one-time Poly1305 key
  • your protocol derives a unique key for every distinct message

Never use the same key to authenticate two different messages. Poly1305 key reuse can let an attacker forge authentication codes. Reusing the key to verify the authentication code for the same message is safe.

§Rustaceous API example, single-part interface

use dryoc::onetimeauth::*;
use dryoc::types::*;

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

// Compute the MAC. Keep a copy only to verify this same message.
let mac = OnetimeAuth::compute_to_vec(key.clone(), b"Data to authenticate");

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

§Rustaceous API example, incremental interface

use dryoc::onetimeauth::*;
use dryoc::types::*;

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

// Initialize the MAC. Keep a copy only to verify this same message.
let mut mac = OnetimeAuth::new(key.clone());
mac.update(b"Multi-part");
mac.update(b"data");
let mac = mac.finalize_to_vec();

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

// Check that a modified MAC fails for the same message
let mut modified_mac = mac.clone();
modified_mac[0] ^= 1;
let mut verify_mac = OnetimeAuth::new(key);
verify_mac.update(b"Multi-part");
verify_mac.update(b"data");
verify_mac
    .verify(&modified_mac)
    .expect_err("verify should have failed");

Modules§

protectedprotected
Protected memory type aliases for OnetimeAuth

Structs§

OnetimeAuth
One-time authentication implementation based on Poly1305, compatible with libsodium’s crypto_onetimeauth_* functions.

Type Aliases§

Key
Stack-allocated key for one-time authentication.
Mac
Stack-allocated message authentication code for one-time authentication.