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