dryoc/classic/
crypto_auth_hmacsha512.rs1use crate::classic::crypto_auth_hmac_impl::{
44 HmacState, hmac, hmac_final, hmac_init, hmac_keygen, hmac_update, hmac_verify,
45};
46use crate::constants::{CRYPTO_AUTH_HMACSHA512_BYTES, CRYPTO_AUTH_HMACSHA512_KEYBYTES};
47use crate::error::Error;
48use crate::sha512::Sha512;
49
50pub type Key = [u8; CRYPTO_AUTH_HMACSHA512_KEYBYTES];
52pub type Mac = [u8; CRYPTO_AUTH_HMACSHA512_BYTES];
54
55pub struct HmacSha512State(HmacState<Sha512, 128, CRYPTO_AUTH_HMACSHA512_BYTES>);
57
58pub fn crypto_auth_hmacsha512(mac: &mut Mac, message: &[u8], key: &Key) {
60 hmac::<Sha512, CRYPTO_AUTH_HMACSHA512_KEYBYTES, 128, CRYPTO_AUTH_HMACSHA512_BYTES>(
61 mac, message, key,
62 );
63}
64
65pub fn crypto_auth_hmacsha512_verify(mac: &Mac, input: &[u8], key: &Key) -> Result<(), Error> {
71 hmac_verify::<Sha512, CRYPTO_AUTH_HMACSHA512_KEYBYTES, 128, CRYPTO_AUTH_HMACSHA512_BYTES>(
72 mac, input, key,
73 )
74}
75
76#[must_use]
78pub fn crypto_auth_hmacsha512_keygen() -> Key {
79 hmac_keygen()
80}
81
82#[must_use]
84pub fn crypto_auth_hmacsha512_init(key: &[u8]) -> HmacSha512State {
85 HmacSha512State(hmac_init::<Sha512, 128, CRYPTO_AUTH_HMACSHA512_BYTES>(key))
86}
87
88pub fn crypto_auth_hmacsha512_update(state: &mut HmacSha512State, input: &[u8]) {
90 hmac_update(&mut state.0, input);
91}
92
93pub fn crypto_auth_hmacsha512_final(state: HmacSha512State, output: &mut Mac) {
95 hmac_final(state.0, output);
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101 use crate::classic::crypto_auth_hmac_impl::test_util::hmac_classic_tests;
102
103 hmac_classic_tests! {
104 hash: sha2::Sha512,
105 block: 128,
106 bytes: CRYPTO_AUTH_HMACSHA512_BYTES,
107 keybytes: CRYPTO_AUTH_HMACSHA512_KEYBYTES,
108 tag: sha512,
109 chunk: 31,
110 one_shot: crypto_auth_hmacsha512,
111 verify: crypto_auth_hmacsha512_verify,
112 keygen: crypto_auth_hmacsha512_keygen,
113 init: crypto_auth_hmacsha512_init,
114 update: crypto_auth_hmacsha512_update,
115 finalize: crypto_auth_hmacsha512_final,
116 sodium_one_shot: auth_hmacsha512,
117 sodium_state: AuthHmacSha512State,
118 keybytes_test: test_rfc4231_case_1_matches_one_shot_for_keybytes_key(b"Hi There"),
119 rfc4231: {
120 test_rfc4231_case_1 => RFC4231_CASE_1,
121 test_rfc4231_short_key_case_2 => RFC4231_CASE_2,
122 test_rfc4231_long_message_case_3 => RFC4231_CASE_3,
123 test_rfc4231_case_4 => RFC4231_CASE_4,
124 test_rfc4231_long_key_case_6 => RFC4231_CASE_6,
125 test_rfc4231_long_key_and_message_case_7 => RFC4231_CASE_7,
126 },
127 }
128}