use crate::utils::{IPAD, OPAD, get_der_key}; use core::{fmt, slice}; use digest::{ InvalidLength, KeyInit, MacMarker, Output, Reset, block_api::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, EagerHash, FixedOutputCore, OutputSizeUser, UpdateCore, }, block_buffer::Eager, common::{Key, KeySizeUser}, }; /// Generic core HMAC instance, which operates over blocks. pub struct HmacCore { digest: D::Core, opad_digest: D::Core, } impl Clone for HmacCore { fn clone(&self) -> Self { Self { digest: self.digest.clone(), opad_digest: self.opad_digest.clone(), } } } impl MacMarker for HmacCore {} impl BufferKindUser for HmacCore { type BufferKind = Eager; } impl KeySizeUser for HmacCore { type KeySize = <::Core as BlockSizeUser>::BlockSize; } impl BlockSizeUser for HmacCore { type BlockSize = <::Core as BlockSizeUser>::BlockSize; } impl OutputSizeUser for HmacCore { type OutputSize = <::Core as OutputSizeUser>::OutputSize; } impl KeyInit for HmacCore { #[inline(always)] fn new(key: &Key) -> Self { Self::new_from_slice(key.as_slice()).expect("HMAC accepts keys of any length") } #[inline(always)] fn new_from_slice(key: &[u8]) -> Result { let mut buf = get_der_key::(key); buf.iter_mut().for_each(|b: &mut u8| *b ^= IPAD); let mut digest = D::Core::default(); digest.update_blocks(slice::from_ref(&buf)); buf.iter_mut().for_each(|b: &mut u8| *b ^= IPAD ^ OPAD); let mut opad_digest = D::Core::default(); opad_digest.update_blocks(slice::from_ref(&buf)); Ok(Self { opad_digest, digest, }) } } impl UpdateCore for HmacCore { #[inline(always)] fn update_blocks(&mut self, blocks: &[Block]) { self.digest.update_blocks(blocks); } } impl FixedOutputCore for HmacCore { #[inline(always)] fn finalize_fixed_core(&mut self, buffer: &mut Buffer, out: &mut Output) { let mut hash = Output::::default(); self.digest.finalize_fixed_core(buffer, &mut hash); // finalize_fixed_core should reset the buffer as well, but // to be extra safe we reset it explicitly again. buffer.reset(); let h = &mut self.opad_digest; buffer.digest_blocks(&hash, |b| h.update_blocks(b)); h.finalize_fixed_core(buffer, out); } } impl AlgorithmName for HmacCore { fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("Hmac<")?; ::write_alg_name(f)?; f.write_str(">") } } impl fmt::Debug for HmacCore where D::Core: AlgorithmName, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("HmacCore { ... }") } } /// Generic core HMAC instance, which operates over blocks. pub struct HmacResetCore { digest: D::Core, opad_digest: D::Core, ipad_digest: D::Core, } impl Clone for HmacResetCore { fn clone(&self) -> Self { Self { digest: self.digest.clone(), opad_digest: self.opad_digest.clone(), ipad_digest: self.ipad_digest.clone(), } } } impl MacMarker for HmacResetCore {} impl BufferKindUser for HmacResetCore { type BufferKind = Eager; } impl KeySizeUser for HmacResetCore { type KeySize = <::Core as BlockSizeUser>::BlockSize; } impl BlockSizeUser for HmacResetCore { type BlockSize = <::Core as BlockSizeUser>::BlockSize; } impl OutputSizeUser for HmacResetCore { type OutputSize = <::Core as OutputSizeUser>::OutputSize; } impl KeyInit for HmacResetCore { #[inline(always)] fn new(key: &Key) -> Self { Self::new_from_slice(key.as_slice()).expect("HMAC accepts keys of any length") } #[inline(always)] fn new_from_slice(key: &[u8]) -> Result { let mut buf = get_der_key::(key); buf.iter_mut().for_each(|b: &mut u8| *b ^= IPAD); let mut digest = D::Core::default(); digest.update_blocks(slice::from_ref(&buf)); buf.iter_mut().for_each(|b: &mut u8| *b ^= IPAD ^ OPAD); let mut opad_digest = D::Core::default(); opad_digest.update_blocks(slice::from_ref(&buf)); Ok(Self { ipad_digest: digest.clone(), opad_digest, digest, }) } } impl UpdateCore for HmacResetCore { #[inline(always)] fn update_blocks(&mut self, blocks: &[Block]) { self.digest.update_blocks(blocks); } } impl FixedOutputCore for HmacResetCore { #[inline(always)] fn finalize_fixed_core(&mut self, buffer: &mut Buffer, out: &mut Output) { let mut hash = Output::::default(); self.digest.finalize_fixed_core(buffer, &mut hash); // finalize_fixed_core should reset the buffer as well, but // to be extra safe we reset it explicitly again. buffer.reset(); let mut h = self.opad_digest.clone(); buffer.digest_blocks(&hash, |b| h.update_blocks(b)); h.finalize_fixed_core(buffer, out); } } impl Reset for HmacResetCore { #[inline(always)] fn reset(&mut self) { self.digest = self.ipad_digest.clone(); } } impl AlgorithmName for HmacResetCore { fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("Hmac<")?; ::write_alg_name(f)?; f.write_str(">") } } impl fmt::Debug for HmacResetCore where D::Core: AlgorithmName, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("HmacResetCore { ... }") } }