hs-test: cache docker build in local filesystem
[vpp.git] / src / vppinfra / crypto / aes.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2020 Cisco and/or its affiliates.
4  * 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 __aes_h__
19 #define __aes_h__
20
21 typedef enum
22 {
23   AES_KEY_128 = 0,
24   AES_KEY_192 = 1,
25   AES_KEY_256 = 2,
26 } aes_key_size_t;
27
28 #define AES_KEY_ROUNDS(x)               (10 + x * 2)
29 #define AES_KEY_BYTES(x)                (16 + x * 8)
30
31 static_always_inline u8x16
32 aes_block_load (u8 * p)
33 {
34   return *(u8x16u *) p;
35 }
36
37 static_always_inline u8x16
38 aes_enc_round_x1 (u8x16 a, u8x16 k)
39 {
40 #if defined (__AES__)
41   return (u8x16) _mm_aesenc_si128 ((__m128i) a, (__m128i) k);
42 #elif defined (__ARM_FEATURE_CRYPTO)
43   return vaesmcq_u8 (vaeseq_u8 (a, u8x16_splat (0))) ^ k;
44 #endif
45 }
46
47 #if defined(__VAES__) && defined(__AVX512F__)
48 static_always_inline u8x64
49 aes_enc_round_x4 (u8x64 a, u8x64 k)
50 {
51   return (u8x64) _mm512_aesenc_epi128 ((__m512i) a, (__m512i) k);
52 }
53
54 static_always_inline u8x64
55 aes_enc_last_round_x4 (u8x64 a, u8x64 k)
56 {
57   return (u8x64) _mm512_aesenclast_epi128 ((__m512i) a, (__m512i) k);
58 }
59
60 static_always_inline u8x64
61 aes_dec_round_x4 (u8x64 a, u8x64 k)
62 {
63   return (u8x64) _mm512_aesdec_epi128 ((__m512i) a, (__m512i) k);
64 }
65
66 static_always_inline u8x64
67 aes_dec_last_round_x4 (u8x64 a, u8x64 k)
68 {
69   return (u8x64) _mm512_aesdeclast_epi128 ((__m512i) a, (__m512i) k);
70 }
71 #endif
72
73 #ifdef __VAES__
74 static_always_inline u8x32
75 aes_enc_round_x2 (u8x32 a, u8x32 k)
76 {
77   return (u8x32) _mm256_aesenc_epi128 ((__m256i) a, (__m256i) k);
78 }
79
80 static_always_inline u8x32
81 aes_enc_last_round_x2 (u8x32 a, u8x32 k)
82 {
83   return (u8x32) _mm256_aesenclast_epi128 ((__m256i) a, (__m256i) k);
84 }
85
86 static_always_inline u8x32
87 aes_dec_round_x2 (u8x32 a, u8x32 k)
88 {
89   return (u8x32) _mm256_aesdec_epi128 ((__m256i) a, (__m256i) k);
90 }
91
92 static_always_inline u8x32
93 aes_dec_last_round_x2 (u8x32 a, u8x32 k)
94 {
95   return (u8x32) _mm256_aesdeclast_epi128 ((__m256i) a, (__m256i) k);
96 }
97 #endif
98
99 static_always_inline u8x16
100 aes_enc_last_round_x1 (u8x16 a, u8x16 k)
101 {
102 #if defined (__AES__)
103   return (u8x16) _mm_aesenclast_si128 ((__m128i) a, (__m128i) k);
104 #elif defined (__ARM_FEATURE_CRYPTO)
105   return vaeseq_u8 (a, u8x16_splat (0)) ^ k;
106 #endif
107 }
108
109 #ifdef __x86_64__
110
111 static_always_inline u8x16
112 aes_dec_round_x1 (u8x16 a, u8x16 k)
113 {
114   return (u8x16) _mm_aesdec_si128 ((__m128i) a, (__m128i) k);
115 }
116
117 static_always_inline u8x16
118 aes_dec_last_round_x1 (u8x16 a, u8x16 k)
119 {
120   return (u8x16) _mm_aesdeclast_si128 ((__m128i) a, (__m128i) k);
121 }
122 #endif
123
124 static_always_inline void
125 aes_block_store (u8 * p, u8x16 r)
126 {
127   *(u8x16u *) p = r;
128 }
129
130 static_always_inline u8x16
131 aes_encrypt_block (u8x16 block, const u8x16 * round_keys, aes_key_size_t ks)
132 {
133   int rounds = AES_KEY_ROUNDS (ks);
134   block ^= round_keys[0];
135   for (int i = 1; i < rounds; i += 1)
136     block = aes_enc_round_x1 (block, round_keys[i]);
137   return aes_enc_last_round_x1 (block, round_keys[rounds]);
138 }
139
140 static_always_inline u8x16
141 aes_inv_mix_column (u8x16 a)
142 {
143 #if defined (__AES__)
144   return (u8x16) _mm_aesimc_si128 ((__m128i) a);
145 #elif defined (__ARM_FEATURE_CRYPTO)
146   return vaesimcq_u8 (a);
147 #endif
148 }
149
150 #ifdef __x86_64__
151 #define aes_keygen_assist(a, b) \
152   (u8x16) _mm_aeskeygenassist_si128((__m128i) a, b)
153
154 /* AES-NI based AES key expansion based on code samples from
155    Intel(r) Advanced Encryption Standard (AES) New Instructions White Paper
156    (323641-001) */
157
158 static_always_inline void
159 aes128_key_assist (u8x16 * rk, u8x16 r)
160 {
161   u8x16 t = rk[-1];
162   t ^= u8x16_word_shift_left (t, 4);
163   t ^= u8x16_word_shift_left (t, 4);
164   t ^= u8x16_word_shift_left (t, 4);
165   rk[0] = t ^ (u8x16) u32x4_shuffle ((u32x4) r, 3, 3, 3, 3);
166 }
167
168 static_always_inline void
169 aes128_key_expand (u8x16 *rk, u8x16u const *k)
170 {
171   rk[0] = k[0];
172   aes128_key_assist (rk + 1, aes_keygen_assist (rk[0], 0x01));
173   aes128_key_assist (rk + 2, aes_keygen_assist (rk[1], 0x02));
174   aes128_key_assist (rk + 3, aes_keygen_assist (rk[2], 0x04));
175   aes128_key_assist (rk + 4, aes_keygen_assist (rk[3], 0x08));
176   aes128_key_assist (rk + 5, aes_keygen_assist (rk[4], 0x10));
177   aes128_key_assist (rk + 6, aes_keygen_assist (rk[5], 0x20));
178   aes128_key_assist (rk + 7, aes_keygen_assist (rk[6], 0x40));
179   aes128_key_assist (rk + 8, aes_keygen_assist (rk[7], 0x80));
180   aes128_key_assist (rk + 9, aes_keygen_assist (rk[8], 0x1b));
181   aes128_key_assist (rk + 10, aes_keygen_assist (rk[9], 0x36));
182 }
183
184 static_always_inline void
185 aes192_key_assist (u8x16 * r1, u8x16 * r2, u8x16 key_assist)
186 {
187   u8x16 t;
188   r1[0] ^= t = u8x16_word_shift_left (r1[0], 4);
189   r1[0] ^= t = u8x16_word_shift_left (t, 4);
190   r1[0] ^= u8x16_word_shift_left (t, 4);
191   r1[0] ^= (u8x16) _mm_shuffle_epi32 ((__m128i) key_assist, 0x55);
192   r2[0] ^= u8x16_word_shift_left (r2[0], 4);
193   r2[0] ^= (u8x16) _mm_shuffle_epi32 ((__m128i) r1[0], 0xff);
194 }
195
196 static_always_inline void
197 aes192_key_expand (u8x16 * rk, u8x16u const *k)
198 {
199   u8x16 r1, r2;
200
201   rk[0] = r1 = k[0];
202   rk[1] = r2 = (u8x16) (u64x2) { *(u64 *) (k + 1), 0 };
203
204   aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x1));
205   rk[1] = (u8x16) _mm_shuffle_pd ((__m128d) rk[1], (__m128d) r1, 0);
206   rk[2] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
207
208   aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x2));
209   rk[3] = r1;
210   rk[4] = r2;
211
212   aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x4));
213   rk[4] = (u8x16) _mm_shuffle_pd ((__m128d) rk[4], (__m128d) r1, 0);
214   rk[5] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
215
216   aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x8));
217   rk[6] = r1;
218   rk[7] = r2;
219
220   aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x10));
221   rk[7] = (u8x16) _mm_shuffle_pd ((__m128d) rk[7], (__m128d) r1, 0);
222   rk[8] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
223
224   aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x20));
225   rk[9] = r1;
226   rk[10] = r2;
227
228   aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x40));
229   rk[10] = (u8x16) _mm_shuffle_pd ((__m128d) rk[10], (__m128d) r1, 0);
230   rk[11] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
231
232   aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x80));
233   rk[12] = r1;
234 }
235
236 static_always_inline void
237 aes256_key_assist (u8x16 * rk, int i, u8x16 key_assist)
238 {
239   u8x16 r, t;
240   rk += i;
241   r = rk[-2];
242   r ^= t = u8x16_word_shift_left (r, 4);
243   r ^= t = u8x16_word_shift_left (t, 4);
244   r ^= u8x16_word_shift_left (t, 4);
245   r ^= (u8x16) u32x4_shuffle ((u32x4) key_assist, 3, 3, 3, 3);
246   rk[0] = r;
247
248   if (i >= 14)
249     return;
250
251   key_assist = aes_keygen_assist (rk[0], 0x0);
252   r = rk[-1];
253   r ^= t = u8x16_word_shift_left (r, 4);
254   r ^= t = u8x16_word_shift_left (t, 4);
255   r ^= u8x16_word_shift_left (t, 4);
256   r ^= (u8x16) u32x4_shuffle ((u32x4) key_assist, 2, 2, 2, 2);
257   rk[1] = r;
258 }
259
260 static_always_inline void
261 aes256_key_expand (u8x16 * rk, u8x16u const *k)
262 {
263   rk[0] = k[0];
264   rk[1] = k[1];
265   aes256_key_assist (rk, 2, aes_keygen_assist (rk[1], 0x01));
266   aes256_key_assist (rk, 4, aes_keygen_assist (rk[3], 0x02));
267   aes256_key_assist (rk, 6, aes_keygen_assist (rk[5], 0x04));
268   aes256_key_assist (rk, 8, aes_keygen_assist (rk[7], 0x08));
269   aes256_key_assist (rk, 10, aes_keygen_assist (rk[9], 0x10));
270   aes256_key_assist (rk, 12, aes_keygen_assist (rk[11], 0x20));
271   aes256_key_assist (rk, 14, aes_keygen_assist (rk[13], 0x40));
272 }
273 #endif
274
275 #ifdef __aarch64__
276
277 static const u8x16 aese_prep_mask1 =
278   { 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12 };
279 static const u8x16 aese_prep_mask2 =
280   { 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15 };
281
282 static_always_inline void
283 aes128_key_expand_round_neon (u8x16 * rk, u32 rcon)
284 {
285   u8x16 r, t, last_round = rk[-1], z = { };
286   r = vqtbl1q_u8 (last_round, aese_prep_mask1);
287   r = vaeseq_u8 (r, z);
288   r ^= (u8x16) vdupq_n_u32 (rcon);
289   r ^= last_round;
290   r ^= t = vextq_u8 (z, last_round, 12);
291   r ^= t = vextq_u8 (z, t, 12);
292   r ^= vextq_u8 (z, t, 12);
293   rk[0] = r;
294 }
295
296 static_always_inline void
297 aes128_key_expand (u8x16 *rk, u8x16u const *k)
298 {
299   rk[0] = k[0];
300   aes128_key_expand_round_neon (rk + 1, 0x01);
301   aes128_key_expand_round_neon (rk + 2, 0x02);
302   aes128_key_expand_round_neon (rk + 3, 0x04);
303   aes128_key_expand_round_neon (rk + 4, 0x08);
304   aes128_key_expand_round_neon (rk + 5, 0x10);
305   aes128_key_expand_round_neon (rk + 6, 0x20);
306   aes128_key_expand_round_neon (rk + 7, 0x40);
307   aes128_key_expand_round_neon (rk + 8, 0x80);
308   aes128_key_expand_round_neon (rk + 9, 0x1b);
309   aes128_key_expand_round_neon (rk + 10, 0x36);
310 }
311
312 static_always_inline void
313 aes192_key_expand_round_neon (u8x8 * rk, u32 rcon)
314 {
315   u8x8 r, last_round = rk[-1], z = { };
316   u8x16 r2, z2 = { };
317
318   r2 = (u8x16) vdupq_lane_u64 ((uint64x1_t) last_round, 0);
319   r2 = vqtbl1q_u8 (r2, aese_prep_mask1);
320   r2 = vaeseq_u8 (r2, z2);
321   r2 ^= (u8x16) vdupq_n_u32 (rcon);
322
323   r = (u8x8) vdup_laneq_u64 ((u64x2) r2, 0);
324   r ^= rk[-3];
325   r ^= vext_u8 (z, rk[-3], 4);
326   rk[0] = r;
327
328   r = rk[-2] ^ vext_u8 (r, z, 4);
329   r ^= vext_u8 (z, r, 4);
330   rk[1] = r;
331
332   if (rcon == 0x80)
333     return;
334
335   r = rk[-1] ^ vext_u8 (r, z, 4);
336   r ^= vext_u8 (z, r, 4);
337   rk[2] = r;
338 }
339
340 static_always_inline void
341 aes192_key_expand (u8x16 * ek, const u8x16u * k)
342 {
343   u8x8 *rk = (u8x8 *) ek;
344   ek[0] = k[0];
345   rk[2] = *(u8x8u *) (k + 1);
346   aes192_key_expand_round_neon (rk + 3, 0x01);
347   aes192_key_expand_round_neon (rk + 6, 0x02);
348   aes192_key_expand_round_neon (rk + 9, 0x04);
349   aes192_key_expand_round_neon (rk + 12, 0x08);
350   aes192_key_expand_round_neon (rk + 15, 0x10);
351   aes192_key_expand_round_neon (rk + 18, 0x20);
352   aes192_key_expand_round_neon (rk + 21, 0x40);
353   aes192_key_expand_round_neon (rk + 24, 0x80);
354 }
355
356
357 static_always_inline void
358 aes256_key_expand_round_neon (u8x16 * rk, u32 rcon)
359 {
360   u8x16 r, t, z = { };
361
362   r = vqtbl1q_u8 (rk[-1], rcon ? aese_prep_mask1 : aese_prep_mask2);
363   r = vaeseq_u8 (r, z);
364   if (rcon)
365     r ^= (u8x16) vdupq_n_u32 (rcon);
366   r ^= rk[-2];
367   r ^= t = vextq_u8 (z, rk[-2], 12);
368   r ^= t = vextq_u8 (z, t, 12);
369   r ^= vextq_u8 (z, t, 12);
370   rk[0] = r;
371 }
372
373 static_always_inline void
374 aes256_key_expand (u8x16 *rk, u8x16u const *k)
375 {
376   rk[0] = k[0];
377   rk[1] = k[1];
378   aes256_key_expand_round_neon (rk + 2, 0x01);
379   aes256_key_expand_round_neon (rk + 3, 0);
380   aes256_key_expand_round_neon (rk + 4, 0x02);
381   aes256_key_expand_round_neon (rk + 5, 0);
382   aes256_key_expand_round_neon (rk + 6, 0x04);
383   aes256_key_expand_round_neon (rk + 7, 0);
384   aes256_key_expand_round_neon (rk + 8, 0x08);
385   aes256_key_expand_round_neon (rk + 9, 0);
386   aes256_key_expand_round_neon (rk + 10, 0x10);
387   aes256_key_expand_round_neon (rk + 11, 0);
388   aes256_key_expand_round_neon (rk + 12, 0x20);
389   aes256_key_expand_round_neon (rk + 13, 0);
390   aes256_key_expand_round_neon (rk + 14, 0x40);
391 }
392
393 #endif
394
395 static_always_inline void
396 aes_key_expand (u8x16 * key_schedule, u8 const *key, aes_key_size_t ks)
397 {
398   switch (ks)
399     {
400     case AES_KEY_128:
401       aes128_key_expand (key_schedule, (u8x16u const *) key);
402       break;
403     case AES_KEY_192:
404       aes192_key_expand (key_schedule, (u8x16u const *) key);
405       break;
406     case AES_KEY_256:
407       aes256_key_expand (key_schedule, (u8x16u const *) key);
408       break;
409     }
410 }
411
412 static_always_inline void
413 aes_key_enc_to_dec (u8x16 * ke, u8x16 * kd, aes_key_size_t ks)
414 {
415   int rounds = AES_KEY_ROUNDS (ks);
416
417   kd[rounds] = ke[0];
418   kd[0] = ke[rounds];
419
420   for (int i = 1; i < (rounds / 2); i++)
421     {
422       kd[rounds - i] = aes_inv_mix_column (ke[i]);
423       kd[i] = aes_inv_mix_column (ke[rounds - i]);
424     }
425
426   kd[rounds / 2] = aes_inv_mix_column (ke[rounds / 2]);
427 }
428 #if defined(__VAES__) && defined(__AVX512F__)
429 #define N_AES_LANES                4
430 #define aes_load_partial(p, n)     u8x64_load_partial ((u8 *) (p), n)
431 #define aes_store_partial(v, p, n) u8x64_store_partial (v, (u8 *) (p), n)
432 #define aes_reflect(r)             u8x64_reflect_u8x16 (r)
433 typedef u8x64 aes_data_t;
434 typedef u8x64u aes_mem_t;
435 typedef u32x16 aes_counter_t;
436 #elif defined(__VAES__)
437 #define N_AES_LANES                2
438 #define aes_load_partial(p, n)     u8x32_load_partial ((u8 *) (p), n)
439 #define aes_store_partial(v, p, n) u8x32_store_partial (v, (u8 *) (p), n)
440 #define aes_reflect(r)             u8x32_reflect_u8x16 (r)
441 typedef u8x32 aes_data_t;
442 typedef u8x32u aes_mem_t;
443 typedef u32x8 aes_counter_t;
444 #else
445 #define N_AES_LANES                1
446 #define aes_load_partial(p, n)     u8x16_load_partial ((u8 *) (p), n)
447 #define aes_store_partial(v, p, n) u8x16_store_partial (v, (u8 *) (p), n)
448 #define aes_reflect(r)             u8x16_reflect (r)
449 typedef u8x16 aes_data_t;
450 typedef u8x16u aes_mem_t;
451 typedef u32x4 aes_counter_t;
452 #endif
453
454 #define N_AES_BYTES (N_AES_LANES * 16)
455
456 typedef union
457 {
458   u8x16 x1;
459   u8x32 x2;
460   u8x64 x4;
461   u8x16 lanes[4];
462 } aes_expaned_key_t;
463
464 static_always_inline void
465 aes_enc_round (aes_data_t *r, const aes_expaned_key_t *ek, uword n_blocks)
466 {
467   for (int i = 0; i < n_blocks; i++)
468 #if N_AES_LANES == 4
469     r[i] = aes_enc_round_x4 (r[i], ek->x4);
470 #elif N_AES_LANES == 2
471     r[i] = aes_enc_round_x2 (r[i], ek->x2);
472 #else
473     r[i] = aes_enc_round_x1 (r[i], ek->x1);
474 #endif
475 }
476
477 static_always_inline void
478 aes_enc_last_round (aes_data_t *r, aes_data_t *d, const aes_expaned_key_t *ek,
479                     uword n_blocks)
480 {
481   for (int i = 0; i < n_blocks; i++)
482 #if N_AES_LANES == 4
483     d[i] ^= r[i] = aes_enc_last_round_x4 (r[i], ek->x4);
484 #elif N_AES_LANES == 2
485     d[i] ^= r[i] = aes_enc_last_round_x2 (r[i], ek->x2);
486 #else
487     d[i] ^= r[i] = aes_enc_last_round_x1 (r[i], ek->x1);
488 #endif
489 }
490
491 #endif /* __aes_h__ */