/rust/registry/src/index.crates.io-1949cf8c6b5b557f/cbc-0.2.1/src/encrypt.rs
Line | Count | Source |
1 | | use crate::xor; |
2 | | use cipher::{ |
3 | | AlgorithmName, Array, Block, BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, |
4 | | BlockModeEncBackend, BlockModeEncClosure, BlockModeEncrypt, BlockSizeUser, InOut, InnerIvInit, |
5 | | Iv, IvSizeUser, IvState, ParBlocksSizeUser, SetIvState, array::ArraySize, common::InnerUser, |
6 | | consts::U1, |
7 | | }; |
8 | | use core::fmt; |
9 | | |
10 | | #[cfg(feature = "zeroize")] |
11 | | use cipher::zeroize::{Zeroize, ZeroizeOnDrop}; |
12 | | |
13 | | /// CBC mode encryptor. |
14 | | pub struct Encryptor<C> |
15 | | where |
16 | | C: BlockCipherEncrypt, |
17 | | { |
18 | | cipher: C, |
19 | | iv: Block<C>, |
20 | | } |
21 | | |
22 | | impl<C> BlockSizeUser for Encryptor<C> |
23 | | where |
24 | | C: BlockCipherEncrypt, |
25 | | { |
26 | | type BlockSize = C::BlockSize; |
27 | | } |
28 | | |
29 | | impl<C> InnerUser for Encryptor<C> |
30 | | where |
31 | | C: BlockCipherEncrypt, |
32 | | { |
33 | | type Inner = C; |
34 | | } |
35 | | |
36 | | impl<C> IvSizeUser for Encryptor<C> |
37 | | where |
38 | | C: BlockCipherEncrypt, |
39 | | { |
40 | | type IvSize = C::BlockSize; |
41 | | } |
42 | | |
43 | | impl<C> InnerIvInit for Encryptor<C> |
44 | | where |
45 | | C: BlockCipherEncrypt, |
46 | | { |
47 | | #[inline] |
48 | 1.53k | fn inner_iv_init(cipher: C, iv: &Iv<Self>) -> Self { |
49 | 1.53k | Self { |
50 | 1.53k | cipher, |
51 | 1.53k | iv: iv.clone(), |
52 | 1.53k | } |
53 | 1.53k | } |
54 | | } |
55 | | |
56 | | impl<C> IvState for Encryptor<C> |
57 | | where |
58 | | C: BlockCipherEncrypt, |
59 | | { |
60 | | #[inline] |
61 | | fn iv_state(&self) -> Iv<Self> { |
62 | | self.iv.clone() |
63 | | } |
64 | | } |
65 | | |
66 | | impl<C> SetIvState for Encryptor<C> |
67 | | where |
68 | | C: BlockCipherEncrypt, |
69 | | { |
70 | | #[inline] |
71 | | fn set_iv(&mut self, iv: &Iv<Self>) { |
72 | | self.iv = iv.clone(); |
73 | | } |
74 | | } |
75 | | |
76 | | impl<C> BlockModeEncrypt for Encryptor<C> |
77 | | where |
78 | | C: BlockCipherEncrypt, |
79 | | { |
80 | 18.3k | fn encrypt_with_backend(&mut self, f: impl BlockModeEncClosure<BlockSize = Self::BlockSize>) { |
81 | | struct Closure<'a, BS, BC> |
82 | | where |
83 | | BS: ArraySize, |
84 | | BC: BlockModeEncClosure<BlockSize = BS>, |
85 | | { |
86 | | iv: &'a mut Array<u8, BS>, |
87 | | f: BC, |
88 | | } |
89 | | |
90 | | impl<BS, BC> BlockSizeUser for Closure<'_, BS, BC> |
91 | | where |
92 | | BS: ArraySize, |
93 | | BC: BlockModeEncClosure<BlockSize = BS>, |
94 | | { |
95 | | type BlockSize = BS; |
96 | | } |
97 | | |
98 | | impl<BS, BC> BlockCipherEncClosure for Closure<'_, BS, BC> |
99 | | where |
100 | | BS: ArraySize, |
101 | | BC: BlockModeEncClosure<BlockSize = BS>, |
102 | | { |
103 | | #[inline(always)] |
104 | 18.3k | fn call<B: BlockCipherEncBackend<BlockSize = Self::BlockSize>>( |
105 | 18.3k | self, |
106 | 18.3k | cipher_backend: &B, |
107 | 18.3k | ) { |
108 | 18.3k | let Self { iv, f } = self; |
109 | 18.3k | f.call(&mut Backend { iv, cipher_backend }); |
110 | 18.3k | } Unexecuted instantiation: <<cbc::encrypt::Encryptor<_> as cipher::block::BlockModeEncrypt>::encrypt_with_backend::Closure<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, cipher::block::ctx::BlockCtx<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>>> as cipher::block::backends::BlockCipherEncClosure>::call::<aes::backends::soft::Aes256> <<cbc::encrypt::Encryptor<_> as cipher::block::BlockModeEncrypt>::encrypt_with_backend::Closure<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, cipher::block::ctx::BlockCtx<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>>> as cipher::block::backends::BlockCipherEncClosure>::call::<aes::backends::x86_aes::Aes<15>> Line | Count | Source | 104 | 18.3k | fn call<B: BlockCipherEncBackend<BlockSize = Self::BlockSize>>( | 105 | 18.3k | self, | 106 | 18.3k | cipher_backend: &B, | 107 | 18.3k | ) { | 108 | 18.3k | let Self { iv, f } = self; | 109 | 18.3k | f.call(&mut Backend { iv, cipher_backend }); | 110 | 18.3k | } |
Unexecuted instantiation: <<cbc::encrypt::Encryptor<_> as cipher::block::BlockModeEncrypt>::encrypt_with_backend::Closure<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, cipher::block::ctx::BlockCtx<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>>> as cipher::block::backends::BlockCipherEncClosure>::call::<aes::backends::x86_vaes256::Aes<15>> Unexecuted instantiation: <<cbc::encrypt::Encryptor<_> as cipher::block::BlockModeEncrypt>::encrypt_with_backend::Closure<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, cipher::block::ctx::BlockCtx<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>>> as cipher::block::backends::BlockCipherEncClosure>::call::<aes::backends::x86_vaes512::Aes<15>> |
111 | | } |
112 | | |
113 | 18.3k | let Self { cipher, iv } = self; |
114 | 18.3k | cipher.encrypt_with_backend(Closure { iv, f }); |
115 | 18.3k | } |
116 | | } |
117 | | |
118 | | impl<C> AlgorithmName for Encryptor<C> |
119 | | where |
120 | | C: BlockCipherEncrypt + AlgorithmName, |
121 | | { |
122 | | fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { |
123 | | f.write_str("cbc::Encryptor<")?; |
124 | | <C as AlgorithmName>::write_alg_name(f)?; |
125 | | f.write_str(">") |
126 | | } |
127 | | } |
128 | | |
129 | | impl<C> fmt::Debug for Encryptor<C> |
130 | | where |
131 | | C: BlockCipherEncrypt + AlgorithmName, |
132 | | { |
133 | | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
134 | | f.write_str("cbc::Encryptor<")?; |
135 | | <C as AlgorithmName>::write_alg_name(f)?; |
136 | | f.write_str("> { ... }") |
137 | | } |
138 | | } |
139 | | |
140 | | impl<C: BlockCipherEncrypt> Drop for Encryptor<C> { |
141 | 1.53k | fn drop(&mut self) { |
142 | | #[cfg(feature = "zeroize")] |
143 | | self.iv.zeroize(); |
144 | 1.53k | } |
145 | | } |
146 | | |
147 | | #[cfg(feature = "zeroize")] |
148 | | impl<C: BlockCipherEncrypt + ZeroizeOnDrop> ZeroizeOnDrop for Encryptor<C> {} |
149 | | |
150 | | struct Backend<'a, BS, BK> |
151 | | where |
152 | | BS: ArraySize, |
153 | | BK: BlockCipherEncBackend<BlockSize = BS>, |
154 | | { |
155 | | iv: &'a mut Array<u8, BS>, |
156 | | cipher_backend: &'a BK, |
157 | | } |
158 | | |
159 | | impl<BS, BK> BlockSizeUser for Backend<'_, BS, BK> |
160 | | where |
161 | | BS: ArraySize, |
162 | | BK: BlockCipherEncBackend<BlockSize = BS>, |
163 | | { |
164 | | type BlockSize = BS; |
165 | | } |
166 | | |
167 | | impl<BS, BK> ParBlocksSizeUser for Backend<'_, BS, BK> |
168 | | where |
169 | | BS: ArraySize, |
170 | | BK: BlockCipherEncBackend<BlockSize = BS>, |
171 | | { |
172 | | type ParBlocksSize = U1; |
173 | | } |
174 | | |
175 | | impl<BS, BK> BlockModeEncBackend for Backend<'_, BS, BK> |
176 | | where |
177 | | BS: ArraySize, |
178 | | BK: BlockCipherEncBackend<BlockSize = BS>, |
179 | | { |
180 | | #[inline(always)] |
181 | 18.3k | fn encrypt_block(&mut self, mut block: InOut<'_, '_, Block<Self>>) { |
182 | 18.3k | let mut t = block.clone_in(); |
183 | 18.3k | xor(&mut t, self.iv); |
184 | 18.3k | self.cipher_backend.encrypt_block((&mut t).into()); |
185 | 18.3k | *self.iv = t.clone(); |
186 | 18.3k | *block.get_out() = t; |
187 | 18.3k | } Unexecuted instantiation: <cbc::encrypt::Backend<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, aes::backends::soft::Aes256> as cipher::block::backends::BlockModeEncBackend>::encrypt_block <cbc::encrypt::Backend<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, aes::backends::x86_aes::Aes<15>> as cipher::block::backends::BlockModeEncBackend>::encrypt_block Line | Count | Source | 181 | 18.3k | fn encrypt_block(&mut self, mut block: InOut<'_, '_, Block<Self>>) { | 182 | 18.3k | let mut t = block.clone_in(); | 183 | 18.3k | xor(&mut t, self.iv); | 184 | 18.3k | self.cipher_backend.encrypt_block((&mut t).into()); | 185 | 18.3k | *self.iv = t.clone(); | 186 | 18.3k | *block.get_out() = t; | 187 | 18.3k | } |
Unexecuted instantiation: <cbc::encrypt::Backend<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, aes::backends::x86_vaes256::Aes<15>> as cipher::block::backends::BlockModeEncBackend>::encrypt_block Unexecuted instantiation: <cbc::encrypt::Backend<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, aes::backends::x86_vaes512::Aes<15>> as cipher::block::backends::BlockModeEncBackend>::encrypt_block |
188 | | } |