/rust/registry/src/index.crates.io-1949cf8c6b5b557f/aes-0.8.4/src/ni/aes256.rs
Line | Count | Source |
1 | | use super::{arch::*, utils::*}; |
2 | | use crate::{Block, Block8}; |
3 | | use cipher::inout::InOut; |
4 | | use core::mem; |
5 | | |
6 | | /// AES-192 round keys |
7 | | pub(super) type RoundKeys = [__m128i; 15]; |
8 | | |
9 | | #[inline] |
10 | | #[target_feature(enable = "aes")] |
11 | 0 | pub(super) unsafe fn encrypt1(keys: &RoundKeys, block: InOut<'_, '_, Block>) { |
12 | 0 | let (in_ptr, out_ptr) = block.into_raw(); |
13 | 0 | let mut b = _mm_loadu_si128(in_ptr as *const __m128i); |
14 | 0 | b = _mm_xor_si128(b, keys[0]); |
15 | 0 | b = _mm_aesenc_si128(b, keys[1]); |
16 | 0 | b = _mm_aesenc_si128(b, keys[2]); |
17 | 0 | b = _mm_aesenc_si128(b, keys[3]); |
18 | 0 | b = _mm_aesenc_si128(b, keys[4]); |
19 | 0 | b = _mm_aesenc_si128(b, keys[5]); |
20 | 0 | b = _mm_aesenc_si128(b, keys[6]); |
21 | 0 | b = _mm_aesenc_si128(b, keys[7]); |
22 | 0 | b = _mm_aesenc_si128(b, keys[8]); |
23 | 0 | b = _mm_aesenc_si128(b, keys[9]); |
24 | 0 | b = _mm_aesenc_si128(b, keys[10]); |
25 | 0 | b = _mm_aesenc_si128(b, keys[11]); |
26 | 0 | b = _mm_aesenc_si128(b, keys[12]); |
27 | 0 | b = _mm_aesenc_si128(b, keys[13]); |
28 | 0 | b = _mm_aesenclast_si128(b, keys[14]); |
29 | 0 | _mm_storeu_si128(out_ptr as *mut __m128i, b); |
30 | 0 | } |
31 | | |
32 | | #[inline] |
33 | | #[target_feature(enable = "aes")] |
34 | 0 | pub(super) unsafe fn encrypt8(keys: &RoundKeys, blocks: InOut<'_, '_, Block8>) { |
35 | 0 | let (in_ptr, out_ptr) = blocks.into_raw(); |
36 | 0 | let mut b = load8(in_ptr); |
37 | 0 | xor8(&mut b, keys[0]); |
38 | 0 | aesenc8(&mut b, keys[1]); |
39 | 0 | aesenc8(&mut b, keys[2]); |
40 | 0 | aesenc8(&mut b, keys[3]); |
41 | 0 | aesenc8(&mut b, keys[4]); |
42 | 0 | aesenc8(&mut b, keys[5]); |
43 | 0 | aesenc8(&mut b, keys[6]); |
44 | 0 | aesenc8(&mut b, keys[7]); |
45 | 0 | aesenc8(&mut b, keys[8]); |
46 | 0 | aesenc8(&mut b, keys[9]); |
47 | 0 | aesenc8(&mut b, keys[10]); |
48 | 0 | aesenc8(&mut b, keys[11]); |
49 | 0 | aesenc8(&mut b, keys[12]); |
50 | 0 | aesenc8(&mut b, keys[13]); |
51 | 0 | aesenclast8(&mut b, keys[14]); |
52 | 0 | store8(out_ptr, b); |
53 | 0 | } |
54 | | |
55 | | #[inline] |
56 | | #[target_feature(enable = "aes")] |
57 | 0 | pub(super) unsafe fn decrypt1(keys: &RoundKeys, block: InOut<'_, '_, Block>) { |
58 | 0 | let (in_ptr, out_ptr) = block.into_raw(); |
59 | 0 | let mut b = _mm_loadu_si128(in_ptr as *const __m128i); |
60 | 0 | b = _mm_xor_si128(b, keys[14]); |
61 | 0 | b = _mm_aesdec_si128(b, keys[13]); |
62 | 0 | b = _mm_aesdec_si128(b, keys[12]); |
63 | 0 | b = _mm_aesdec_si128(b, keys[11]); |
64 | 0 | b = _mm_aesdec_si128(b, keys[10]); |
65 | 0 | b = _mm_aesdec_si128(b, keys[9]); |
66 | 0 | b = _mm_aesdec_si128(b, keys[8]); |
67 | 0 | b = _mm_aesdec_si128(b, keys[7]); |
68 | 0 | b = _mm_aesdec_si128(b, keys[6]); |
69 | 0 | b = _mm_aesdec_si128(b, keys[5]); |
70 | 0 | b = _mm_aesdec_si128(b, keys[4]); |
71 | 0 | b = _mm_aesdec_si128(b, keys[3]); |
72 | 0 | b = _mm_aesdec_si128(b, keys[2]); |
73 | 0 | b = _mm_aesdec_si128(b, keys[1]); |
74 | 0 | b = _mm_aesdeclast_si128(b, keys[0]); |
75 | 0 | _mm_storeu_si128(out_ptr as *mut __m128i, b); |
76 | 0 | } |
77 | | |
78 | | #[inline] |
79 | | #[target_feature(enable = "aes")] |
80 | 0 | pub(super) unsafe fn decrypt8(keys: &RoundKeys, blocks: InOut<'_, '_, Block8>) { |
81 | 0 | let (in_ptr, out_ptr) = blocks.into_raw(); |
82 | 0 | let mut b = load8(in_ptr); |
83 | 0 | xor8(&mut b, keys[14]); |
84 | 0 | aesdec8(&mut b, keys[13]); |
85 | 0 | aesdec8(&mut b, keys[12]); |
86 | 0 | aesdec8(&mut b, keys[11]); |
87 | 0 | aesdec8(&mut b, keys[10]); |
88 | 0 | aesdec8(&mut b, keys[9]); |
89 | 0 | aesdec8(&mut b, keys[8]); |
90 | 0 | aesdec8(&mut b, keys[7]); |
91 | 0 | aesdec8(&mut b, keys[6]); |
92 | 0 | aesdec8(&mut b, keys[5]); |
93 | 0 | aesdec8(&mut b, keys[4]); |
94 | 0 | aesdec8(&mut b, keys[3]); |
95 | 0 | aesdec8(&mut b, keys[2]); |
96 | 0 | aesdec8(&mut b, keys[1]); |
97 | 0 | aesdeclast8(&mut b, keys[0]); |
98 | 0 | store8(out_ptr, b); |
99 | 0 | } |
100 | | |
101 | | macro_rules! expand_round { |
102 | | ($keys:expr, $pos:expr, $round:expr) => { |
103 | | let mut t1 = $keys[$pos - 2]; |
104 | | let mut t2; |
105 | | let mut t3 = $keys[$pos - 1]; |
106 | | let mut t4; |
107 | | |
108 | | t2 = _mm_aeskeygenassist_si128(t3, $round); |
109 | | t2 = _mm_shuffle_epi32(t2, 0xff); |
110 | | t4 = _mm_slli_si128(t1, 0x4); |
111 | | t1 = _mm_xor_si128(t1, t4); |
112 | | t4 = _mm_slli_si128(t4, 0x4); |
113 | | t1 = _mm_xor_si128(t1, t4); |
114 | | t4 = _mm_slli_si128(t4, 0x4); |
115 | | t1 = _mm_xor_si128(t1, t4); |
116 | | t1 = _mm_xor_si128(t1, t2); |
117 | | |
118 | | $keys[$pos] = t1; |
119 | | |
120 | | t4 = _mm_aeskeygenassist_si128(t1, 0x00); |
121 | | t2 = _mm_shuffle_epi32(t4, 0xaa); |
122 | | t4 = _mm_slli_si128(t3, 0x4); |
123 | | t3 = _mm_xor_si128(t3, t4); |
124 | | t4 = _mm_slli_si128(t4, 0x4); |
125 | | t3 = _mm_xor_si128(t3, t4); |
126 | | t4 = _mm_slli_si128(t4, 0x4); |
127 | | t3 = _mm_xor_si128(t3, t4); |
128 | | t3 = _mm_xor_si128(t3, t2); |
129 | | |
130 | | $keys[$pos + 1] = t3; |
131 | | }; |
132 | | } |
133 | | |
134 | | macro_rules! expand_round_last { |
135 | | ($keys:expr, $pos:expr, $round:expr) => { |
136 | | let mut t1 = $keys[$pos - 2]; |
137 | | let mut t2; |
138 | | let t3 = $keys[$pos - 1]; |
139 | | let mut t4; |
140 | | |
141 | | t2 = _mm_aeskeygenassist_si128(t3, $round); |
142 | | t2 = _mm_shuffle_epi32(t2, 0xff); |
143 | | t4 = _mm_slli_si128(t1, 0x4); |
144 | | t1 = _mm_xor_si128(t1, t4); |
145 | | t4 = _mm_slli_si128(t4, 0x4); |
146 | | t1 = _mm_xor_si128(t1, t4); |
147 | | t4 = _mm_slli_si128(t4, 0x4); |
148 | | t1 = _mm_xor_si128(t1, t4); |
149 | | t1 = _mm_xor_si128(t1, t2); |
150 | | |
151 | | $keys[$pos] = t1; |
152 | | }; |
153 | | } |
154 | | |
155 | | #[inline(always)] |
156 | 0 | pub(super) unsafe fn expand_key(key: &[u8; 32]) -> RoundKeys { |
157 | | // SAFETY: `RoundKeys` is a `[__m128i; 15]` which can be initialized |
158 | | // with all zeroes. |
159 | 0 | let mut keys: RoundKeys = mem::zeroed(); |
160 | | |
161 | 0 | let kp = key.as_ptr() as *const __m128i; |
162 | 0 | keys[0] = _mm_loadu_si128(kp); |
163 | 0 | keys[1] = _mm_loadu_si128(kp.add(1)); |
164 | | |
165 | 0 | expand_round!(keys, 2, 0x01); |
166 | 0 | expand_round!(keys, 4, 0x02); |
167 | 0 | expand_round!(keys, 6, 0x04); |
168 | 0 | expand_round!(keys, 8, 0x08); |
169 | 0 | expand_round!(keys, 10, 0x10); |
170 | 0 | expand_round!(keys, 12, 0x20); |
171 | 0 | expand_round_last!(keys, 14, 0x40); |
172 | | |
173 | 0 | keys |
174 | 0 | } |
175 | | |
176 | | #[inline] |
177 | | #[target_feature(enable = "aes")] |
178 | 0 | pub(super) unsafe fn inv_expanded_keys(keys: &RoundKeys) -> RoundKeys { |
179 | 0 | [ |
180 | 0 | keys[0], |
181 | 0 | _mm_aesimc_si128(keys[1]), |
182 | 0 | _mm_aesimc_si128(keys[2]), |
183 | 0 | _mm_aesimc_si128(keys[3]), |
184 | 0 | _mm_aesimc_si128(keys[4]), |
185 | 0 | _mm_aesimc_si128(keys[5]), |
186 | 0 | _mm_aesimc_si128(keys[6]), |
187 | 0 | _mm_aesimc_si128(keys[7]), |
188 | 0 | _mm_aesimc_si128(keys[8]), |
189 | 0 | _mm_aesimc_si128(keys[9]), |
190 | 0 | _mm_aesimc_si128(keys[10]), |
191 | 0 | _mm_aesimc_si128(keys[11]), |
192 | 0 | _mm_aesimc_si128(keys[12]), |
193 | 0 | _mm_aesimc_si128(keys[13]), |
194 | 0 | keys[14], |
195 | 0 | ] |
196 | 0 | } |