ecb/
encrypt.rs

1use cipher::{
2    consts::U1,
3    crypto_common::{InnerInit, InnerUser},
4    generic_array::ArrayLength,
5    inout::InOut,
6    AlgorithmName, Block, BlockBackend, BlockCipher, BlockClosure, BlockEncryptMut, BlockSizeUser,
7    ParBlocksSizeUser,
8};
9use core::fmt;
10
11/// ECB mode encryptor.
12#[derive(Clone)]
13pub struct Encryptor<C>
14where
15    C: BlockEncryptMut + BlockCipher,
16{
17    cipher: C,
18}
19
20impl<C> BlockSizeUser for Encryptor<C>
21where
22    C: BlockEncryptMut + BlockCipher,
23{
24    type BlockSize = C::BlockSize;
25}
26
27impl<C> BlockEncryptMut for Encryptor<C>
28where
29    C: BlockEncryptMut + BlockCipher,
30{
31    fn encrypt_with_backend_mut(&mut self, f: impl BlockClosure<BlockSize = Self::BlockSize>) {
32        let Self { cipher, .. } = self;
33        cipher.encrypt_with_backend_mut(Closure { f })
34    }
35}
36
37impl<C> InnerUser for Encryptor<C>
38where
39    C: BlockEncryptMut + BlockCipher,
40{
41    type Inner = C;
42}
43
44impl<C> InnerInit for Encryptor<C>
45where
46    C: BlockEncryptMut + BlockCipher,
47{
48    #[inline]
49    fn inner_init(cipher: C) -> Self {
50        Self { cipher }
51    }
52}
53
54impl<C> AlgorithmName for Encryptor<C>
55where
56    C: BlockEncryptMut + BlockCipher + AlgorithmName,
57{
58    fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        f.write_str("ecb::Encryptor<")?;
60        <C as AlgorithmName>::write_alg_name(f)?;
61        f.write_str(">")
62    }
63}
64
65impl<C> fmt::Debug for Encryptor<C>
66where
67    C: BlockEncryptMut + BlockCipher + AlgorithmName,
68{
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.write_str("ecb::Encryptor<")?;
71        <C as AlgorithmName>::write_alg_name(f)?;
72        f.write_str("> { ... }")
73    }
74}
75
76struct Closure<BS, BC>
77where
78    BS: ArrayLength<u8>,
79    BC: BlockClosure<BlockSize = BS>,
80{
81    f: BC,
82}
83
84impl<BS, BC> BlockSizeUser for Closure<BS, BC>
85where
86    BS: ArrayLength<u8>,
87    BC: BlockClosure<BlockSize = BS>,
88{
89    type BlockSize = BS;
90}
91
92impl<BS, BC> BlockClosure for Closure<BS, BC>
93where
94    BS: ArrayLength<u8>,
95    BC: BlockClosure<BlockSize = BS>,
96{
97    #[inline(always)]
98    fn call<B: BlockBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B) {
99        let Self { f, .. } = self;
100        f.call(&mut Backend { backend });
101    }
102}
103
104struct Backend<'a, BS, BK>
105where
106    BS: ArrayLength<u8>,
107    BK: BlockBackend<BlockSize = BS>,
108{
109    backend: &'a mut BK,
110}
111
112impl<'a, BS, BK> BlockSizeUser for Backend<'a, BS, BK>
113where
114    BS: ArrayLength<u8>,
115    BK: BlockBackend<BlockSize = BS>,
116{
117    type BlockSize = BS;
118}
119
120impl<'a, BS, BK> ParBlocksSizeUser for Backend<'a, BS, BK>
121where
122    BS: ArrayLength<u8>,
123    BK: BlockBackend<BlockSize = BS>,
124{
125    type ParBlocksSize = U1;
126}
127
128impl<'a, BS, BK> BlockBackend for Backend<'a, BS, BK>
129where
130    BS: ArrayLength<u8>,
131    BK: BlockBackend<BlockSize = BS>,
132{
133    #[inline(always)]
134    fn proc_block(&mut self, block: InOut<'_, '_, Block<Self>>) {
135        self.backend.proc_block(block);
136    }
137}