Skip to main content

retail_mac/
lib.rs

1#![no_std]
2#![doc = include_str!("../README.md")]
3#![doc(
4    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg",
5    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg"
6)]
7#![cfg_attr(docsrs, feature(doc_cfg))]
8
9pub use digest::{self, Key, KeyInit, Mac};
10
11/// Block-level implementation.
12pub mod block_api;
13
14use block_api::RetailMacCore;
15use cipher::{AlgorithmName, BlockCipherDecrypt, BlockCipherEncrypt, KeySizeUser};
16use core::{fmt, ops::Mul};
17use digest::{
18    InvalidLength,
19    array::ArraySize,
20    block_api::CoreProxy,
21    block_api::SmallBlockSizeUser,
22    typenum::{Prod, U2},
23};
24
25digest::buffer_fixed!(
26    /// Generic Retail MAC instance.
27    #[derive(Clone)]
28    pub struct RetailMac<C: BlockCipherEncrypt + BlockCipherDecrypt + SmallBlockSizeUser>(RetailMacCore<C>);
29    impl: ResetMacTraits;
30);
31
32impl<C> KeySizeUser for RetailMac<C>
33where
34    C: BlockCipherEncrypt + BlockCipherDecrypt + SmallBlockSizeUser,
35    <C as SmallBlockSizeUser>::_BlockSize: Mul<U2>,
36    Prod<<C as SmallBlockSizeUser>::_BlockSize, U2>: ArraySize,
37{
38    type KeySize = Prod<<C as SmallBlockSizeUser>::_BlockSize, U2>;
39}
40
41impl<C> KeyInit for RetailMac<C>
42where
43    C: BlockCipherEncrypt + BlockCipherDecrypt + SmallBlockSizeUser + KeyInit,
44    <C as SmallBlockSizeUser>::_BlockSize: Mul<U2>,
45    Prod<<C as SmallBlockSizeUser>::_BlockSize, U2>: ArraySize,
46{
47    #[inline(always)]
48    fn new(key: &Key<Self>) -> Self {
49        Self {
50            core: KeyInit::new(key),
51            buffer: Default::default(),
52        }
53    }
54
55    #[inline(always)]
56    fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> {
57        KeyInit::new_from_slice(key).map(|core| Self {
58            core,
59            buffer: Default::default(),
60        })
61    }
62}
63
64impl<C> AlgorithmName for RetailMac<C>
65where
66    C: BlockCipherEncrypt + BlockCipherDecrypt + SmallBlockSizeUser + AlgorithmName,
67{
68    fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        <Self as CoreProxy>::Core::write_alg_name(f)
70    }
71}