blob: 85d6f7916f8d2d4aede5f3f1a87f85ca7f5b239f [file] [log] [blame]
Damjan Mariondeb8af62019-04-02 19:06:50 +02001/*
2 *------------------------------------------------------------------
Damjan Marion776644e2020-01-31 10:24:07 +01003 * Copyright (c) 2020 Cisco and/or its affiliates.
Damjan Mariondeb8af62019-04-02 19:06:50 +02004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
18#ifndef __aesni_h__
19#define __aesni_h__
20
21typedef enum
22{
Damjan Marion7d08e392020-01-28 09:55:25 +010023 AES_KEY_128 = 0,
24 AES_KEY_192 = 1,
25 AES_KEY_256 = 2,
26} aes_key_size_t;
Damjan Mariondeb8af62019-04-02 19:06:50 +020027
Damjan Marion7d08e392020-01-28 09:55:25 +010028#define AES_KEY_ROUNDS(x) (10 + x * 2)
29#define AES_KEY_BYTES(x) (16 + x * 8)
Damjan Mariondeb8af62019-04-02 19:06:50 +020030
Damjan Marion776644e2020-01-31 10:24:07 +010031#ifdef __x86_64__
32
Damjan Marion93975e62020-01-30 15:46:23 +010033static_always_inline u8x16
34aes_block_load (u8 * p)
35{
36 return (u8x16) _mm_loadu_si128 ((__m128i *) p);
37}
38
39static_always_inline u8x16
40aes_enc_round (u8x16 a, u8x16 k)
41{
42 return (u8x16) _mm_aesenc_si128 ((__m128i) a, (__m128i) k);
43}
44
45static_always_inline u8x16
46aes_enc_last_round (u8x16 a, u8x16 k)
47{
48 return (u8x16) _mm_aesenclast_si128 ((__m128i) a, (__m128i) k);
49}
50
51static_always_inline u8x16
52aes_dec_round (u8x16 a, u8x16 k)
53{
54 return (u8x16) _mm_aesdec_si128 ((__m128i) a, (__m128i) k);
55}
56
57static_always_inline u8x16
58aes_dec_last_round (u8x16 a, u8x16 k)
59{
60 return (u8x16) _mm_aesdeclast_si128 ((__m128i) a, (__m128i) k);
61}
62
63static_always_inline void
64aes_block_store (u8 * p, u8x16 r)
65{
66 _mm_storeu_si128 ((__m128i *) p, (__m128i) r);
67}
68
69static_always_inline u8x16
70aes_inv_mix_column (u8x16 a)
71{
72 return (u8x16) _mm_aesimc_si128 ((__m128i) a);
73}
Damjan Mariondeb8af62019-04-02 19:06:50 +020074
75/* AES-NI based AES key expansion based on code samples from
76 Intel(r) Advanced Encryption Standard (AES) New Instructions White Paper
77 (323641-001) */
78
Damjan Marion93975e62020-01-30 15:46:23 +010079static_always_inline void
80aes128_key_assist (__m128i * k, __m128i r)
Damjan Mariondeb8af62019-04-02 19:06:50 +020081{
Damjan Marion93975e62020-01-30 15:46:23 +010082 __m128i t = k[-1];
83 t ^= _mm_slli_si128 (t, 4);
84 t ^= _mm_slli_si128 (t, 4);
85 t ^= _mm_slli_si128 (t, 4);
86 k[0] = t ^ _mm_shuffle_epi32 (r, 0xff);
Damjan Mariondeb8af62019-04-02 19:06:50 +020087}
88
89static_always_inline void
Damjan Marion93975e62020-01-30 15:46:23 +010090aes128_key_expand (u8x16 * key_schedule, u8 * key)
Damjan Mariondeb8af62019-04-02 19:06:50 +020091{
Damjan Marion93975e62020-01-30 15:46:23 +010092 __m128i *k = (__m128i *) key_schedule;
Damjan Mariondeb8af62019-04-02 19:06:50 +020093 k[0] = _mm_loadu_si128 ((const __m128i *) key);
Damjan Marion93975e62020-01-30 15:46:23 +010094 aes128_key_assist (k + 1, _mm_aeskeygenassist_si128 (k[0], 0x01));
95 aes128_key_assist (k + 2, _mm_aeskeygenassist_si128 (k[1], 0x02));
96 aes128_key_assist (k + 3, _mm_aeskeygenassist_si128 (k[2], 0x04));
97 aes128_key_assist (k + 4, _mm_aeskeygenassist_si128 (k[3], 0x08));
98 aes128_key_assist (k + 5, _mm_aeskeygenassist_si128 (k[4], 0x10));
99 aes128_key_assist (k + 6, _mm_aeskeygenassist_si128 (k[5], 0x20));
100 aes128_key_assist (k + 7, _mm_aeskeygenassist_si128 (k[6], 0x40));
101 aes128_key_assist (k + 8, _mm_aeskeygenassist_si128 (k[7], 0x80));
102 aes128_key_assist (k + 9, _mm_aeskeygenassist_si128 (k[8], 0x1b));
103 aes128_key_assist (k + 10, _mm_aeskeygenassist_si128 (k[9], 0x36));
Damjan Mariondeb8af62019-04-02 19:06:50 +0200104}
105
106static_always_inline void
Damjan Marion93975e62020-01-30 15:46:23 +0100107aes192_key_assist (__m128i * r1, __m128i * r2, __m128i key_assist)
Damjan Mariondeb8af62019-04-02 19:06:50 +0200108{
Damjan Marion93975e62020-01-30 15:46:23 +0100109 __m128i t;
110 *r1 ^= t = _mm_slli_si128 (*r1, 0x4);
111 *r1 ^= t = _mm_slli_si128 (t, 0x4);
112 *r1 ^= _mm_slli_si128 (t, 0x4);
113 *r1 ^= _mm_shuffle_epi32 (key_assist, 0x55);
114 *r2 ^= _mm_slli_si128 (*r2, 0x4);
115 *r2 ^= _mm_shuffle_epi32 (*r1, 0xff);
Damjan Mariondeb8af62019-04-02 19:06:50 +0200116}
117
118static_always_inline void
Damjan Marion93975e62020-01-30 15:46:23 +0100119aes192_key_expand (u8x16 * key_schedule, u8 * key)
Damjan Mariondeb8af62019-04-02 19:06:50 +0200120{
Damjan Marion93975e62020-01-30 15:46:23 +0100121 __m128i r1, r2, *k = (__m128i *) key_schedule;
Damjan Mariondeb8af62019-04-02 19:06:50 +0200122
123 k[0] = r1 = _mm_loadu_si128 ((__m128i *) key);
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200124 /* load the 24-bytes key as 2 * 16-bytes (and ignore last 8-bytes) */
Damjan Marion93975e62020-01-30 15:46:23 +0100125 k[1] = r2 = CLIB_MEM_OVERFLOW_LOAD (_mm_loadu_si128, (__m128i *) key + 1);
Damjan Mariondeb8af62019-04-02 19:06:50 +0200126
Damjan Marion93975e62020-01-30 15:46:23 +0100127 aes192_key_assist (&r1, &r2, _mm_aeskeygenassist_si128 (r2, 0x1));
Damjan Mariondeb8af62019-04-02 19:06:50 +0200128 k[1] = (__m128i) _mm_shuffle_pd ((__m128d) k[1], (__m128d) r1, 0);
Damjan Marion93975e62020-01-30 15:46:23 +0100129 k[2] = (__m128i) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
130
131 aes192_key_assist (&r1, &r2, _mm_aeskeygenassist_si128 (r2, 0x2));
Damjan Mariondeb8af62019-04-02 19:06:50 +0200132 k[3] = r1;
Damjan Marion93975e62020-01-30 15:46:23 +0100133 k[4] = r2;
Damjan Mariondeb8af62019-04-02 19:06:50 +0200134
Damjan Marion93975e62020-01-30 15:46:23 +0100135 aes192_key_assist (&r1, &r2, _mm_aeskeygenassist_si128 (r2, 0x4));
Damjan Mariondeb8af62019-04-02 19:06:50 +0200136 k[4] = (__m128i) _mm_shuffle_pd ((__m128d) k[4], (__m128d) r1, 0);
Damjan Marion93975e62020-01-30 15:46:23 +0100137 k[5] = (__m128i) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
Damjan Mariondeb8af62019-04-02 19:06:50 +0200138
Damjan Marion93975e62020-01-30 15:46:23 +0100139 aes192_key_assist (&r1, &r2, _mm_aeskeygenassist_si128 (r2, 0x8));
140 k[6] = r1;
141 k[7] = r2;
142
143 aes192_key_assist (&r1, &r2, _mm_aeskeygenassist_si128 (r2, 0x10));
Damjan Mariondeb8af62019-04-02 19:06:50 +0200144 k[7] = (__m128i) _mm_shuffle_pd ((__m128d) k[7], (__m128d) r1, 0);
Damjan Marion93975e62020-01-30 15:46:23 +0100145 k[8] = (__m128i) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
146
147 aes192_key_assist (&r1, &r2, _mm_aeskeygenassist_si128 (r2, 0x20));
Damjan Mariondeb8af62019-04-02 19:06:50 +0200148 k[9] = r1;
Damjan Marion93975e62020-01-30 15:46:23 +0100149 k[10] = r2;
Damjan Mariondeb8af62019-04-02 19:06:50 +0200150
Damjan Marion93975e62020-01-30 15:46:23 +0100151 aes192_key_assist (&r1, &r2, _mm_aeskeygenassist_si128 (r2, 0x40));
Damjan Mariondeb8af62019-04-02 19:06:50 +0200152 k[10] = (__m128i) _mm_shuffle_pd ((__m128d) k[10], (__m128d) r1, 0);
Damjan Marion93975e62020-01-30 15:46:23 +0100153 k[11] = (__m128i) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
154
155 aes192_key_assist (&r1, &r2, _mm_aeskeygenassist_si128 (r2, 0x80));
Damjan Mariondeb8af62019-04-02 19:06:50 +0200156 k[12] = r1;
157}
158
159static_always_inline void
Damjan Marion93975e62020-01-30 15:46:23 +0100160aes256_key_assist (__m128i * k, int i, __m128i key_assist)
Damjan Mariondeb8af62019-04-02 19:06:50 +0200161{
Damjan Marion93975e62020-01-30 15:46:23 +0100162 __m128i r, t;
163 k += i;
164 r = k[-2];
165 r ^= t = _mm_slli_si128 (r, 0x4);
166 r ^= t = _mm_slli_si128 (t, 0x4);
167 r ^= _mm_slli_si128 (t, 0x4);
168 r ^= _mm_shuffle_epi32 (key_assist, 0xff);
169 k[0] = r;
170
171 if (i >= 14)
172 return;
173
174 r = k[-1];
175 r ^= t = _mm_slli_si128 (r, 0x4);
176 r ^= t = _mm_slli_si128 (t, 0x4);
177 r ^= _mm_slli_si128 (t, 0x4);
178 r ^= _mm_shuffle_epi32 (_mm_aeskeygenassist_si128 (k[0], 0x0), 0xaa);
179 k[1] = r;
Damjan Mariondeb8af62019-04-02 19:06:50 +0200180}
181
182static_always_inline void
Damjan Marion93975e62020-01-30 15:46:23 +0100183aes256_key_expand (u8x16 * key_schedule, u8 * key)
Damjan Mariondeb8af62019-04-02 19:06:50 +0200184{
Damjan Marion93975e62020-01-30 15:46:23 +0100185 __m128i *k = (__m128i *) key_schedule;
186 k[0] = _mm_loadu_si128 ((__m128i *) key);
187 k[1] = _mm_loadu_si128 ((__m128i *) (key + 16));
188 aes256_key_assist (k, 2, _mm_aeskeygenassist_si128 (k[1], 0x01));
189 aes256_key_assist (k, 4, _mm_aeskeygenassist_si128 (k[3], 0x02));
190 aes256_key_assist (k, 6, _mm_aeskeygenassist_si128 (k[5], 0x04));
191 aes256_key_assist (k, 8, _mm_aeskeygenassist_si128 (k[7], 0x08));
192 aes256_key_assist (k, 10, _mm_aeskeygenassist_si128 (k[9], 0x10));
193 aes256_key_assist (k, 12, _mm_aeskeygenassist_si128 (k[11], 0x20));
194 aes256_key_assist (k, 14, _mm_aeskeygenassist_si128 (k[13], 0x40));
Damjan Mariondeb8af62019-04-02 19:06:50 +0200195}
Damjan Marion776644e2020-01-31 10:24:07 +0100196#endif
197
198#ifdef __aarch64__
199
200static_always_inline u8x16
201aes_inv_mix_column (u8x16 a)
202{
203 return vaesimcq_u8 (a);
204}
205
206static const u8x16 aese_prep_mask1 =
207 { 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12 };
208static const u8x16 aese_prep_mask2 =
209 { 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15 };
210
211static inline void
212aes128_key_expand_round_neon (u8x16 * rk, u32 rcon)
213{
214 u8x16 r, t, last_round = rk[-1], z = { };
215 r = vqtbl1q_u8 (last_round, aese_prep_mask1);
216 r = vaeseq_u8 (r, z);
217 r ^= (u8x16) vdupq_n_u32 (rcon);
218 r ^= last_round;
219 r ^= t = vextq_u8 (z, last_round, 12);
220 r ^= t = vextq_u8 (z, t, 12);
221 r ^= vextq_u8 (z, t, 12);
222 rk[0] = r;
223}
224
225void
226aes128_key_expand (u8x16 * rk, const u8 * k)
227{
228 rk[0] = vld1q_u8 (k);
229 aes128_key_expand_round_neon (rk + 1, 0x01);
230 aes128_key_expand_round_neon (rk + 2, 0x02);
231 aes128_key_expand_round_neon (rk + 3, 0x04);
232 aes128_key_expand_round_neon (rk + 4, 0x08);
233 aes128_key_expand_round_neon (rk + 5, 0x10);
234 aes128_key_expand_round_neon (rk + 6, 0x20);
235 aes128_key_expand_round_neon (rk + 7, 0x40);
236 aes128_key_expand_round_neon (rk + 8, 0x80);
237 aes128_key_expand_round_neon (rk + 9, 0x1b);
238 aes128_key_expand_round_neon (rk + 10, 0x36);
239}
240
241static inline void
242aes192_key_expand_round_neon (u8x8 * rk, u32 rcon)
243{
244 u8x8 r, last_round = rk[-1], z = { };
245 u8x16 r2, z2 = { };
246
247 r2 = (u8x16) vdupq_lane_u64 ((uint64x1_t) last_round, 0);
248 r2 = vqtbl1q_u8 (r2, aese_prep_mask1);
249 r2 = vaeseq_u8 (r2, z2);
250 r2 ^= (u8x16) vdupq_n_u32 (rcon);
251
252 r = (u8x8) vdup_laneq_u64 ((u64x2) r2, 0);
253 r ^= rk[-3];
254 r ^= vext_u8 (z, rk[-3], 4);
255 rk[0] = r;
256
257 r = rk[-2] ^ vext_u8 (r, z, 4);
258 r ^= vext_u8 (z, r, 4);
259 rk[1] = r;
260
261 if (rcon == 0x80)
262 return;
263
264 r = rk[-1] ^ vext_u8 (r, z, 4);
265 r ^= vext_u8 (z, r, 4);
266 rk[2] = r;
267}
268
269void
270aes192_key_expand (u8x16 * ek, const u8 * k)
271{
272 u8x8 *rk = (u8x8 *) ek;
273 ek[0] = vld1q_u8 (k);
274 rk[2] = vld1_u8 (k + 16);
275 aes192_key_expand_round_neon (rk + 3, 0x01);
276 aes192_key_expand_round_neon (rk + 6, 0x02);
277 aes192_key_expand_round_neon (rk + 9, 0x04);
278 aes192_key_expand_round_neon (rk + 12, 0x08);
279 aes192_key_expand_round_neon (rk + 15, 0x10);
280 aes192_key_expand_round_neon (rk + 18, 0x20);
281 aes192_key_expand_round_neon (rk + 21, 0x40);
282 aes192_key_expand_round_neon (rk + 24, 0x80);
283}
284
285
286static inline void
287aes256_key_expand_round_neon (u8x16 * rk, u32 rcon)
288{
289 u8x16 r, t, z = { };
290
291 r = vqtbl1q_u8 (rk[-1], rcon ? aese_prep_mask1 : aese_prep_mask2);
292 r = vaeseq_u8 (r, z);
293 if (rcon)
294 r ^= (u8x16) vdupq_n_u32 (rcon);
295 r ^= rk[-2];
296 r ^= t = vextq_u8 (z, rk[-2], 12);
297 r ^= t = vextq_u8 (z, t, 12);
298 r ^= vextq_u8 (z, t, 12);
299 rk[0] = r;
300}
301
302void
303aes256_key_expand (u8x16 * rk, const u8 * k)
304{
305 rk[0] = vld1q_u8 (k);
306 rk[1] = vld1q_u8 (k + 16);
307 aes256_key_expand_round_neon (rk + 2, 0x01);
308 aes256_key_expand_round_neon (rk + 3, 0);
309 aes256_key_expand_round_neon (rk + 4, 0x02);
310 aes256_key_expand_round_neon (rk + 5, 0);
311 aes256_key_expand_round_neon (rk + 6, 0x04);
312 aes256_key_expand_round_neon (rk + 7, 0);
313 aes256_key_expand_round_neon (rk + 8, 0x08);
314 aes256_key_expand_round_neon (rk + 9, 0);
315 aes256_key_expand_round_neon (rk + 10, 0x10);
316 aes256_key_expand_round_neon (rk + 11, 0);
317 aes256_key_expand_round_neon (rk + 12, 0x20);
318 aes256_key_expand_round_neon (rk + 13, 0);
319 aes256_key_expand_round_neon (rk + 14, 0x40);
320}
321
322#endif
Damjan Mariondeb8af62019-04-02 19:06:50 +0200323
324static_always_inline void
Damjan Marion93975e62020-01-30 15:46:23 +0100325aes_key_expand (u8x16 * key_schedule, u8 * key, aes_key_size_t ks)
Damjan Mariondeb8af62019-04-02 19:06:50 +0200326{
327 switch (ks)
328 {
Damjan Marion7d08e392020-01-28 09:55:25 +0100329 case AES_KEY_128:
Damjan Marion93975e62020-01-30 15:46:23 +0100330 aes128_key_expand (key_schedule, key);
Damjan Mariondeb8af62019-04-02 19:06:50 +0200331 break;
Damjan Marion7d08e392020-01-28 09:55:25 +0100332 case AES_KEY_192:
Damjan Marion93975e62020-01-30 15:46:23 +0100333 aes192_key_expand (key_schedule, key);
Damjan Mariondeb8af62019-04-02 19:06:50 +0200334 break;
Damjan Marion7d08e392020-01-28 09:55:25 +0100335 case AES_KEY_256:
Damjan Marion93975e62020-01-30 15:46:23 +0100336 aes256_key_expand (key_schedule, key);
Damjan Mariondeb8af62019-04-02 19:06:50 +0200337 break;
338 }
339}
340
Damjan Mariondeb8af62019-04-02 19:06:50 +0200341static_always_inline void
Damjan Marion93975e62020-01-30 15:46:23 +0100342aes_key_enc_to_dec (u8x16 * ke, u8x16 * kd, aes_key_size_t ks)
Damjan Mariondeb8af62019-04-02 19:06:50 +0200343{
Damjan Marion7d08e392020-01-28 09:55:25 +0100344 int rounds = AES_KEY_ROUNDS (ks);
Damjan Mariondeb8af62019-04-02 19:06:50 +0200345
Damjan Marion78b58f62020-01-29 10:31:26 +0100346 kd[rounds] = ke[0];
347 kd[0] = ke[rounds];
Damjan Mariondeb8af62019-04-02 19:06:50 +0200348
349 for (int i = 1; i < (rounds / 2); i++)
350 {
Damjan Marion93975e62020-01-30 15:46:23 +0100351 kd[rounds - i] = aes_inv_mix_column (ke[i]);
352 kd[i] = aes_inv_mix_column (ke[rounds - i]);
Damjan Mariondeb8af62019-04-02 19:06:50 +0200353 }
354
Damjan Marion93975e62020-01-30 15:46:23 +0100355 kd[rounds / 2] = aes_inv_mix_column (ke[rounds / 2]);
Damjan Mariondeb8af62019-04-02 19:06:50 +0200356}
357
358#endif /* __aesni_h__ */
359
360/*
361 * fd.io coding-style-patch-verification: ON
362 *
363 * Local Variables:
364 * eval: (c-set-style "gnu")
365 * End:
366 */