Skip to main content

aes/
macros.rs

1// This macro is not used by the soft backend, to simplify the crate code we allow this macro
2// to be unused to prevent warnings e.g. when `force-soft` is enabled/
3#[allow(unused_macros)]
4macro_rules! impl_backends {
5    (
6        enc_name = $enc_name:ident,
7        dec_name = $dec_name:ident,
8        key_size = $key_size:ty,
9        keys_ty = $keys_ty:ty,
10        par_size = $par_size:ty,
11        expand_keys = $expand_keys:expr,
12        inv_keys = $inv_keys:expr,
13        encrypt = $encrypt:expr,
14        encrypt_par = $encrypt_par:expr,
15        decrypt = $decrypt:expr,
16        decrypt_par = $decrypt_par:expr,
17) => {
18        #[derive(Clone)]
19        pub(crate) struct $enc_name {
20            keys: $keys_ty,
21        }
22
23        impl cipher::BlockSizeUser for $enc_name {
24            type BlockSize = cipher::consts::U16;
25        }
26
27        impl cipher::ParBlocksSizeUser for $enc_name {
28            type ParBlocksSize = $par_size;
29        }
30
31        impl cipher::KeySizeUser for $enc_name {
32            type KeySize = $key_size;
33        }
34
35        impl cipher::KeyInit for $enc_name {
36            #[inline]
37            fn new(key: &cipher::Key<Self>) -> Self {
38                let keys = unsafe { $expand_keys(key.as_ref()) };
39                Self { keys }
40            }
41        }
42
43        impl cipher::BlockCipherEncBackend for $enc_name {
44            #[inline(always)]
45            fn encrypt_block(&self, block: cipher::inout::InOut<'_, '_, cipher::Block<Self>>) {
46                unsafe { $encrypt(&self.keys, block) }
47            }
48
49            #[inline(always)]
50            fn encrypt_par_blocks(
51                &self,
52                blocks: cipher::inout::InOut<'_, '_, cipher::ParBlocks<Self>>,
53            ) {
54                unsafe { $encrypt_par(&self.keys, blocks) }
55            }
56        }
57
58        #[derive(Clone)]
59        pub(crate) struct $dec_name {
60            keys: $keys_ty,
61        }
62
63        impl cipher::BlockSizeUser for $dec_name {
64            type BlockSize = cipher::consts::U16;
65        }
66
67        impl cipher::ParBlocksSizeUser for $dec_name {
68            type ParBlocksSize = $par_size;
69        }
70
71        impl cipher::KeySizeUser for $dec_name {
72            type KeySize = $key_size;
73        }
74
75        impl cipher::KeyInit for $dec_name {
76            #[inline]
77            fn new(key: &cipher::Key<Self>) -> Self {
78                From::from($enc_name::new(key))
79            }
80        }
81
82        impl From<$enc_name> for $dec_name {
83            #[inline]
84            fn from(enc: $enc_name) -> $dec_name {
85                let keys = unsafe { $inv_keys(&enc.keys) };
86                Self { keys }
87            }
88        }
89
90        impl cipher::BlockCipherDecBackend for $dec_name {
91            #[inline(always)]
92            fn decrypt_block(&self, block: cipher::inout::InOut<'_, '_, cipher::Block<Self>>) {
93                unsafe { $decrypt(&self.keys, block) }
94            }
95
96            #[inline(always)]
97            fn decrypt_par_blocks(
98                &self,
99                blocks: cipher::inout::InOut<'_, '_, cipher::ParBlocks<Self>>,
100            ) {
101                unsafe { $decrypt_par(&self.keys, blocks) }
102            }
103        }
104    };
105}