use super::crypto_auth_hmacsha512256::{
HmacSha512256State, crypto_auth_hmacsha512256, crypto_auth_hmacsha512256_final,
crypto_auth_hmacsha512256_init, crypto_auth_hmacsha512256_keygen,
crypto_auth_hmacsha512256_update, crypto_auth_hmacsha512256_verify,
};
use crate::constants::{CRYPTO_AUTH_BYTES, CRYPTO_AUTH_KEYBYTES};
use crate::error::Error;
pub type Key = [u8; CRYPTO_AUTH_KEYBYTES];
pub type Mac = [u8; CRYPTO_AUTH_BYTES];
pub fn crypto_auth(mac: &mut Mac, message: &[u8], key: &Key) {
crypto_auth_hmacsha512256(mac, message, key)
}
pub fn crypto_auth_verify(mac: &Mac, input: &[u8], key: &Key) -> Result<(), Error> {
crypto_auth_hmacsha512256_verify(mac, input, key)
}
pub struct AuthState {
state: HmacSha512256State,
}
pub fn crypto_auth_keygen() -> Key {
crypto_auth_hmacsha512256_keygen()
}
pub fn crypto_auth_init(key: &Key) -> AuthState {
AuthState {
state: crypto_auth_hmacsha512256_init(key),
}
}
pub fn crypto_auth_update(state: &mut AuthState, input: &[u8]) {
crypto_auth_hmacsha512256_update(&mut state.state, input)
}
pub fn crypto_auth_final(state: AuthState, output: &mut [u8; CRYPTO_AUTH_BYTES]) {
crypto_auth_hmacsha512256_final(state.state, output)
}
#[cfg(all(test, dryoc_native_tests))]
mod tests {
use rand::TryRng;
use super::*;
#[test]
fn test_crypto_auth() {
use rand::rngs::SysRng;
use sodiumoxide::crypto::auth;
use sodiumoxide::crypto::auth::Key as SOKey;
use crate::rng::copy_randombytes;
for _ in 0..20 {
let mlen = (SysRng.try_next_u32().unwrap() % 5000) as usize;
let mut message = vec![0u8; mlen];
copy_randombytes(&mut message);
let key = crypto_auth_keygen();
let so_tag =
auth::authenticate(&message, &SOKey::from_slice(&key).expect("key failed"));
let mut mac = Mac::default();
crypto_auth(&mut mac, &message, &key);
assert_eq!(mac, so_tag.0);
crypto_auth_verify(&mac, &message, &key).expect("verify failed");
crypto_auth_verify(&mac, b"invalid message", &key)
.expect_err("verify should have failed");
}
}
}