Debian Codegraph @

debian/missing-sources/aes/src/armv8.rs

raw
1 //! AES block cipher implementation using the ARMv8 Cryptography Extensions.
2 //!
3 //! Based on this C intrinsics implementation:
4 //! <https://github.com/noloader/AES-Intrinsics/blob/master/aes-arm.c>
5 //!
6 //! Original C written and placed in public domain by Jeffrey Walton.
7 //! Based on code from ARM, and by Johannes Schneiders, Skip Hovsmith and
8 //! Barry O'Rourke for the mbedTLS project.
9
10 #![allow(clippy::needless_range_loop)]
11
12 #[cfg(feature = "hazmat")]
13 pub(crate) mod hazmat;
14
15 mod encdec;
16 mod expand;
17 mod intrinsics;
18 #[cfg(test)]
19 mod test_expand;
20
21 use self::{
22 encdec::{decrypt1, decrypt8, encrypt1, encrypt8},
23 expand::{expand_key, inv_expanded_keys},
24 };
25 use crate::{Block, Block8};
26 use cipher::{
27 consts::{U16, U24, U32, U8},
28 inout::InOut,
29 AlgorithmName, BlockBackend, BlockCipher, BlockClosure, BlockDecrypt, BlockEncrypt,
30 BlockSizeUser, Key, KeyInit, KeySizeUser, ParBlocksSizeUser,
31 };
32 use core::arch::aarch64::*;
33 use core::fmt;
34
35 macro_rules! define_aes_impl {
36 (
37 $name:ident,
38 $name_enc:ident,
39 $name_dec:ident,
40 $name_back_enc:ident,
41 $name_back_dec:ident,
42 $key_size:ty,
43 $rounds:tt,
44 $doc:expr $(,)?
45 ) => {
46 #[doc=$doc]
47 #[doc = "block cipher"]
48 #[derive(Clone)]
49 pub struct $name {
50 encrypt: $name_enc,
51 decrypt: $name_dec,
52 }
53
54 impl $name {
55 #[inline(always)]
56 pub(crate) fn get_enc_backend(&self) -> $name_back_enc<'_> {
57 self.encrypt.get_enc_backend()
58 }
59
60 #[inline(always)]
61 pub(crate) fn get_dec_backend(&self) -> $name_back_dec<'_> {
62 self.decrypt.get_dec_backend()
63 }
64 }
65
66 impl BlockCipher for $name {}
67
68 impl KeySizeUser for $name {
69 type KeySize = $key_size;
70 }
71
72 impl KeyInit for $name {
73 #[inline]
74 fn new(key: &Key<Self>) -> Self {
75 let encrypt = $name_enc::new(key);
76 let decrypt = $name_dec::from(&encrypt);
77 Self { encrypt, decrypt }
78 }
79 }
80
81 impl From<$name_enc> for $name {
82 #[inline]
83 fn from(encrypt: $name_enc) -> $name {
84 let decrypt = (&encrypt).into();
85 Self { encrypt, decrypt }
86 }
87 }
88
89 impl From<&$name_enc> for $name {
90 #[inline]
91 fn from(encrypt: &$name_enc) -> $name {
92 let decrypt = encrypt.into();
93 let encrypt = encrypt.clone();
94 Self { encrypt, decrypt }
95 }
96 }
97
98 impl BlockSizeUser for $name {
99 type BlockSize = U16;
100 }
101
102 impl BlockEncrypt for $name {
103 fn encrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
104 self.encrypt.encrypt_with_backend(f)
105 }
106 }
107
108 impl BlockDecrypt for $name {
109 fn decrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
110 self.decrypt.decrypt_with_backend(f)
111 }
112 }
113
114 impl fmt::Debug for $name {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
116 f.write_str(concat!(stringify!($name), " { .. }"))
117 }
118 }
119
120 impl AlgorithmName for $name {
121 fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 f.write_str(stringify!($name))
123 }
124 }
125
126 #[cfg(feature = "zeroize")]
127 impl zeroize::ZeroizeOnDrop for $name {}
128
129 #[doc=$doc]
130 #[doc = "block cipher (encrypt-only)"]
131 #[derive(Clone)]
132 pub struct $name_enc {
133 round_keys: [uint8x16_t; $rounds],
134 }
135
136 impl $name_enc {
137 #[inline(always)]
138 pub(crate) fn get_enc_backend(&self) -> $name_back_enc<'_> {
139 $name_back_enc(self)
140 }
141 }
142
143 impl BlockCipher for $name_enc {}
144
145 impl KeySizeUser for $name_enc {
146 type KeySize = $key_size;
147 }
148
149 impl KeyInit for $name_enc {
150 fn new(key: &Key<Self>) -> Self {
151 Self {
152 round_keys: unsafe { expand_key(key.as_ref()) },
153 }
154 }
155 }
156
157 impl BlockSizeUser for $name_enc {
158 type BlockSize = U16;
159 }
160
161 impl BlockEncrypt for $name_enc {
162 fn encrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
163 f.call(&mut self.get_enc_backend())
164 }
165 }
166
167 impl fmt::Debug for $name_enc {
168 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
169 f.write_str(concat!(stringify!($name_enc), " { .. }"))
170 }
171 }
172
173 impl AlgorithmName for $name_enc {
174 fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
175 f.write_str(stringify!($name_enc))
176 }
177 }
178
179 impl Drop for $name_enc {
180 #[inline]
181 fn drop(&mut self) {
182 #[cfg(feature = "zeroize")]
183 zeroize::Zeroize::zeroize(&mut self.round_keys);
184 }
185 }
186
187 #[cfg(feature = "zeroize")]
188 impl zeroize::ZeroizeOnDrop for $name_enc {}
189
190 #[doc=$doc]
191 #[doc = "block cipher (decrypt-only)"]
192 #[derive(Clone)]
193 pub struct $name_dec {
194 round_keys: [uint8x16_t; $rounds],
195 }
196
197 impl $name_dec {
198 #[inline(always)]
199 pub(crate) fn get_dec_backend(&self) -> $name_back_dec<'_> {
200 $name_back_dec(self)
201 }
202 }
203
204 impl BlockCipher for $name_dec {}
205
206 impl KeySizeUser for $name_dec {
207 type KeySize = $key_size;
208 }
209
210 impl KeyInit for $name_dec {
211 fn new(key: &Key<Self>) -> Self {
212 $name_enc::new(key).into()
213 }
214 }
215
216 impl From<$name_enc> for $name_dec {
217 #[inline]
218 fn from(enc: $name_enc) -> $name_dec {
219 Self::from(&enc)
220 }
221 }
222
223 impl From<&$name_enc> for $name_dec {
224 fn from(enc: &$name_enc) -> $name_dec {
225 let mut round_keys = enc.round_keys;
226 unsafe { inv_expanded_keys(&mut round_keys) };
227 Self { round_keys }
228 }
229 }
230
231 impl BlockSizeUser for $name_dec {
232 type BlockSize = U16;
233 }
234
235 impl BlockDecrypt for $name_dec {
236 fn decrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
237 f.call(&mut self.get_dec_backend());
238 }
239 }
240
241 impl fmt::Debug for $name_dec {
242 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
243 f.write_str(concat!(stringify!($name_dec), " { .. }"))
244 }
245 }
246
247 impl AlgorithmName for $name_dec {
248 fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
249 f.write_str(stringify!($name_dec))
250 }
251 }
252
253 impl Drop for $name_dec {
254 #[inline]
255 fn drop(&mut self) {
256 #[cfg(feature = "zeroize")]
257 zeroize::Zeroize::zeroize(&mut self.round_keys);
258 }
259 }
260
261 #[cfg(feature = "zeroize")]
262 impl zeroize::ZeroizeOnDrop for $name_dec {}
263
264 pub(crate) struct $name_back_enc<'a>(&'a $name_enc);
265
266 impl<'a> BlockSizeUser for $name_back_enc<'a> {
267 type BlockSize = U16;
268 }
269
270 impl<'a> ParBlocksSizeUser for $name_back_enc<'a> {
271 type ParBlocksSize = U8;
272 }
273
274 impl<'a> BlockBackend for $name_back_enc<'a> {
275 #[inline(always)]
276 fn proc_block(&mut self, block: InOut<'_, '_, Block>) {
277 unsafe {
278 encrypt1(&self.0.round_keys, block);
279 }
280 }
281
282 #[inline(always)]
283 fn proc_par_blocks(&mut self, blocks: InOut<'_, '_, Block8>) {
284 unsafe { encrypt8(&self.0.round_keys, blocks) }
285 }
286 }
287
288 pub(crate) struct $name_back_dec<'a>(&'a $name_dec);
289
290 impl<'a> BlockSizeUser for $name_back_dec<'a> {
291 type BlockSize = U16;
292 }
293
294 impl<'a> ParBlocksSizeUser for $name_back_dec<'a> {
295 type ParBlocksSize = U8;
296 }
297
298 impl<'a> BlockBackend for $name_back_dec<'a> {
299 #[inline(always)]
300 fn proc_block(&mut self, block: InOut<'_, '_, Block>) {
301 unsafe {
302 decrypt1(&self.0.round_keys, block);
303 }
304 }
305
306 #[inline(always)]
307 fn proc_par_blocks(&mut self, blocks: InOut<'_, '_, Block8>) {
308 unsafe { decrypt8(&self.0.round_keys, blocks) }
309 }
310 }
311 };
312 }
313
314 define_aes_impl!(
315 Aes128,
316 Aes128Enc,
317 Aes128Dec,
318 Aes128BackEnc,
319 Aes128BackDec,
320 U16,
321 11,
322 "AES-128",
323 );
324 define_aes_impl!(
325 Aes192,
326 Aes192Enc,
327 Aes192Dec,
328 Aes192BackEnc,
329 Aes192BackDec,
330 U24,
331 13,
332 "AES-192",
333 );
334 define_aes_impl!(
335 Aes256,
336 Aes256Enc,
337 Aes256Dec,
338 Aes256BackEnc,
339 Aes256BackDec,
340 U32,
341 15,
342 "AES-256",
343 );