dryoc/classic/
crypto_auth_hmacsha256.rs1use crate::classic::crypto_auth_hmac_impl::{
41 HmacState, hmac, hmac_final, hmac_init, hmac_keygen, hmac_update, hmac_verify,
42};
43use crate::constants::{CRYPTO_AUTH_HMACSHA256_BYTES, CRYPTO_AUTH_HMACSHA256_KEYBYTES};
44use crate::error::Error;
45use crate::sha256::Sha256;
46
47pub type Key = [u8; CRYPTO_AUTH_HMACSHA256_KEYBYTES];
49pub type Mac = [u8; CRYPTO_AUTH_HMACSHA256_BYTES];
51
52pub struct HmacSha256State(HmacState<Sha256, 64, CRYPTO_AUTH_HMACSHA256_BYTES>);
54
55pub fn crypto_auth_hmacsha256(mac: &mut Mac, message: &[u8], key: &Key) {
57 hmac::<Sha256, CRYPTO_AUTH_HMACSHA256_KEYBYTES, 64, CRYPTO_AUTH_HMACSHA256_BYTES>(
58 mac, message, key,
59 );
60}
61
62pub fn crypto_auth_hmacsha256_verify(mac: &Mac, input: &[u8], key: &Key) -> Result<(), Error> {
68 hmac_verify::<Sha256, CRYPTO_AUTH_HMACSHA256_KEYBYTES, 64, CRYPTO_AUTH_HMACSHA256_BYTES>(
69 mac, input, key,
70 )
71}
72
73#[must_use]
75pub fn crypto_auth_hmacsha256_keygen() -> Key {
76 hmac_keygen()
77}
78
79#[must_use]
81pub fn crypto_auth_hmacsha256_init(key: &[u8]) -> HmacSha256State {
82 HmacSha256State(hmac_init::<Sha256, 64, CRYPTO_AUTH_HMACSHA256_BYTES>(key))
83}
84
85pub fn crypto_auth_hmacsha256_update(state: &mut HmacSha256State, input: &[u8]) {
87 hmac_update(&mut state.0, input);
88}
89
90pub fn crypto_auth_hmacsha256_final(state: HmacSha256State, output: &mut Mac) {
92 hmac_final(state.0, output);
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98 use crate::classic::crypto_auth_hmac_impl::test_util::hmac_classic_tests;
99
100 hmac_classic_tests! {
101 hash: sha2::Sha256,
102 block: 64,
103 bytes: CRYPTO_AUTH_HMACSHA256_BYTES,
104 keybytes: CRYPTO_AUTH_HMACSHA256_KEYBYTES,
105 tag: sha256,
106 chunk: 17,
107 one_shot: crypto_auth_hmacsha256,
108 verify: crypto_auth_hmacsha256_verify,
109 keygen: crypto_auth_hmacsha256_keygen,
110 init: crypto_auth_hmacsha256_init,
111 update: crypto_auth_hmacsha256_update,
112 finalize: crypto_auth_hmacsha256_final,
113 sodium_one_shot: auth_hmacsha256,
114 sodium_state: AuthHmacSha256State,
115 keybytes_test: test_one_shot_matches_incremental_for_keybytes_key(b"message"),
116 rfc4231: {
117 test_rfc4231_case_1 => RFC4231_CASE_1,
118 test_rfc4231_short_key_case_2 => RFC4231_CASE_2,
119 test_rfc4231_long_message_case_3 => RFC4231_CASE_3,
120 test_rfc4231_case_4 => RFC4231_CASE_4,
121 test_rfc4231_long_key_case_6 => RFC4231_CASE_6,
122 test_rfc4231_long_key_and_message_case_7 => RFC4231_CASE_7,
123 },
124 }
125}