Botan 3.14.1
Crypto and TLS for C&
aes.cpp
Go to the documentation of this file.
1/*
2* (C) 1999-2010,2015,2017,2018,2020,2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/aes.h>
8
9#include <botan/internal/bit_ops.h>
10#include <botan/internal/bswap.h>
11#include <botan/internal/ct_utils.h>
12#include <botan/internal/loadstor.h>
13#include <botan/internal/rotate.h>
14#include <concepts>
15
16#if defined(BOTAN_HAS_CPUID)
17 #include <botan/internal/cpuid.h>
18#endif
19
20namespace Botan {
21
22/*
23* One of three AES implementation strategies are used to get a constant time
24* implementation which is immune to common cache/timing based side channels:
25*
26* - If AES hardware support is available (AES-NI, POWER8, Aarch64) use that
27*
28* - If 128-bit SIMD with byte shuffles are available (SSSE3, NEON, or Altivec),
29* use the vperm technique published by Mike Hamburg at CHES 2009.
30*
31* - If no hardware or SIMD support, fall back to a constant time bitsliced
32* implementation. This uses the native word size: 32-bit words process 2
33* blocks in parallel, 64-bit words process 4 blocks. Each 64-bit word is
34* treated as two independent 32-bit halves (one pair of blocks each), so
35* the bitsliced layout within each half is identical to the 32-bit case.
36*
37* Useful references
38*
39* - "Accelerating AES with Vector Permute Instructions" Mike Hamburg
40* https://www.shiftleft.org/papers/vector_aes/vector_aes.pdf
41*
42* - "Faster and Timing-Attack Resistant AES-GCM" Käsper and Schwabe
43* https://eprint.iacr.org/2009/129.pdf
44*
45* - "A new combinational logic minimization technique with applications to cryptology."
46* Boyar and Peralta https://eprint.iacr.org/2009/191.pdf
47*
48* - "A depth-16 circuit for the AES S-box" Boyar and Peralta
49* https://eprint.iacr.org/2011/332.pdf
50*
51* - "A Very Compact S-box for AES" Canright
52* https://www.iacr.org/archive/ches2005/032.pdf
53* https://core.ac.uk/download/pdf/36694529.pdf (extended)
54*/
55
56namespace {
57
58/*
59* The word type used for bitsliced AES operations.
60*
61* On 64-bit platforms each word holds two independent 32-bit halves,
62* each encoding one pair of AES blocks, for 4 blocks total.
63* On 32-bit platforms each word encodes one pair of blocks (2 total).
64*/
65using aes_bs_word = word;
66static_assert(std::same_as<aes_bs_word, uint32_t> || std::same_as<aes_bs_word, uint64_t>);
67
68constexpr size_t AES_BITSLICED_BLOCKS = 8 * sizeof(aes_bs_word) / 16;
69
70/**
71* Replicate a 32-bit pattern into each 32-bit half of a wider word
72*/
73template <std::unsigned_integral W>
74constexpr inline W rep32(uint32_t x) {
75 if constexpr(std::same_as<W, uint32_t>) {
76 return x;
77 } else {
78 return (static_cast<W>(x) << 32) | x;
79 }
80}
81
82/**
83* Rotate right within each 32-bit half of a word.
84*
85* For 32-bit words this is a plain rotation. For 64-bit words each
86* 32-bit half is rotated independently, which preserves the invariant
87* that the two halves represent separate block-pairs.
88*/
89template <size_t N, std::unsigned_integral W>
90BOTAN_FORCE_INLINE constexpr W column_rotr(W x) {
91 if constexpr(std::same_as<W, uint32_t>) {
92 return rotr<N>(x);
93 } else {
94 static_assert(N > 0 && N < 32);
95 constexpr W mask = rep32<W>(0xFFFFFFFFU >> N);
96 return ((x >> N) & mask) | ((x << (32 - N)) & ~mask);
97 }
98}
99
100/*
101This is an AES sbox circuit which can execute in bitsliced mode up to 32x in
102parallel.
103
104The circuit is from the "Circuit Minimization Team" group
105http://www.cs.yale.edu/homes/peralta/CircuitStuff/CMT.html
106http://www.cs.yale.edu/homes/peralta/CircuitStuff/SLP_AES_113.txt
107
108This circuit has size 113 and depth 27. In software it is much faster than
109circuits which are considered faster for hardware purposes (where circuit depth
110is the critical constraint), because unlike in hardware, on common CPUs we can
111only execute - at best - 3 or 4 logic operations per cycle. So a smaller circuit
112is superior. On an x86-64 machine this circuit is about 15% faster than the
113circuit of size 128 and depth 16 given in "A depth-16 circuit for the AES S-box".
114
115Another circuit for AES Sbox of size 102 and depth 24 is describted in "New
116Circuit Minimization Techniques for Smaller and Faster AES SBoxes"
117[https://eprint.iacr.org/2019/802] however it relies on "non-standard" gates
118like MUX, NOR, NAND, etc and so in practice in bitsliced software, its size is
119actually a bit larger than this circuit, as few CPUs have such instructions and
120otherwise they must be emulated using a sequence of available bit operations.
121*/
122template <std::unsigned_integral W>
123void AES_SBOX(W V[8]) {
124 const W U0 = V[0];
125 const W U1 = V[1];
126 const W U2 = V[2];
127 const W U3 = V[3];
128 const W U4 = V[4];
129 const W U5 = V[5];
130 const W U6 = V[6];
131 const W U7 = V[7];
132
133 const W y14 = U3 ^ U5;
134 const W y13 = U0 ^ U6;
135 const W y9 = U0 ^ U3;
136 const W y8 = U0 ^ U5;
137 const W t0 = U1 ^ U2;
138 const W y1 = t0 ^ U7;
139 const W y4 = y1 ^ U3;
140 const W y12 = y13 ^ y14;
141 const W y2 = y1 ^ U0;
142 const W y5 = y1 ^ U6;
143 const W y3 = y5 ^ y8;
144 const W t1 = U4 ^ y12;
145 const W y15 = t1 ^ U5;
146 const W y20 = t1 ^ U1;
147 const W y6 = y15 ^ U7;
148 const W y10 = y15 ^ t0;
149 const W y11 = y20 ^ y9;
150 const W y7 = U7 ^ y11;
151 const W y17 = y10 ^ y11;
152 const W y19 = y10 ^ y8;
153 const W y16 = t0 ^ y11;
154 const W y21 = y13 ^ y16;
155 const W y18 = U0 ^ y16;
156 const W t2 = y12 & y15;
157 const W t3 = y3 & y6;
158 const W t4 = t3 ^ t2;
159 const W t5 = y4 & U7;
160 const W t6 = t5 ^ t2;
161 const W t7 = y13 & y16;
162 const W t8 = y5 & y1;
163 const W t9 = t8 ^ t7;
164 const W t10 = y2 & y7;
165 const W t11 = t10 ^ t7;
166 const W t12 = y9 & y11;
167 const W t13 = y14 & y17;
168 const W t14 = t13 ^ t12;
169 const W t15 = y8 & y10;
170 const W t16 = t15 ^ t12;
171 const W t17 = t4 ^ y20;
172 const W t18 = t6 ^ t16;
173 const W t19 = t9 ^ t14;
174 const W t20 = t11 ^ t16;
175 const W t21 = t17 ^ t14;
176 const W t22 = t18 ^ y19;
177 const W t23 = t19 ^ y21;
178 const W t24 = t20 ^ y18;
179 const W t25 = t21 ^ t22;
180 const W t26 = t21 & t23;
181 const W t27 = t24 ^ t26;
182 const W t28 = t25 & t27;
183 const W t29 = t28 ^ t22;
184 const W t30 = t23 ^ t24;
185 const W t31 = t22 ^ t26;
186 const W t32 = t31 & t30;
187 const W t33 = t32 ^ t24;
188 const W t34 = t23 ^ t33;
189 const W t35 = t27 ^ t33;
190 const W t36 = t24 & t35;
191 const W t37 = t36 ^ t34;
192 const W t38 = t27 ^ t36;
193 const W t39 = t29 & t38;
194 const W t40 = t25 ^ t39;
195 const W t41 = t40 ^ t37;
196 const W t42 = t29 ^ t33;
197 const W t43 = t29 ^ t40;
198 const W t44 = t33 ^ t37;
199 const W t45 = t42 ^ t41;
200 const W z0 = t44 & y15;
201 const W z1 = t37 & y6;
202 const W z2 = t33 & U7;
203 const W z3 = t43 & y16;
204 const W z4 = t40 & y1;
205 const W z5 = t29 & y7;
206 const W z6 = t42 & y11;
207 const W z7 = t45 & y17;
208 const W z8 = t41 & y10;
209 const W z9 = t44 & y12;
210 const W z10 = t37 & y3;
211 const W z11 = t33 & y4;
212 const W z12 = t43 & y13;
213 const W z13 = t40 & y5;
214 const W z14 = t29 & y2;
215 const W z15 = t42 & y9;
216 const W z16 = t45 & y14;
217 const W z17 = t41 & y8;
218 const W tc1 = z15 ^ z16;
219 const W tc2 = z10 ^ tc1;
220 const W tc3 = z9 ^ tc2;
221 const W tc4 = z0 ^ z2;
222 const W tc5 = z1 ^ z0;
223 const W tc6 = z3 ^ z4;
224 const W tc7 = z12 ^ tc4;
225 const W tc8 = z7 ^ tc6;
226 const W tc9 = z8 ^ tc7;
227 const W tc10 = tc8 ^ tc9;
228 const W tc11 = tc6 ^ tc5;
229 const W tc12 = z3 ^ z5;
230 const W tc13 = z13 ^ tc1;
231 const W tc14 = tc4 ^ tc12;
232 const W S3 = tc3 ^ tc11;
233 const W tc16 = z6 ^ tc8;
234 const W tc17 = z14 ^ tc10;
235 const W tc18 = ~tc13 ^ tc14;
236 const W S7 = z12 ^ tc18;
237 const W tc20 = z15 ^ tc16;
238 const W tc21 = tc2 ^ z11;
239 const W S0 = tc3 ^ tc16;
240 const W S6 = tc10 ^ tc18;
241 const W S4 = tc14 ^ S3;
242 const W S1 = ~(S3 ^ tc16);
243 const W tc26 = tc17 ^ tc20;
244 const W S2 = ~(tc26 ^ z17);
245 const W S5 = tc21 ^ tc17;
246
247 V[0] = S0;
248 V[1] = S1;
249 V[2] = S2;
250 V[3] = S3;
251 V[4] = S4;
252 V[5] = S5;
253 V[6] = S6;
254 V[7] = S7;
255}
256
257/*
258A circuit for inverse AES Sbox of size 121 and depth 21 from
259http://www.cs.yale.edu/homes/peralta/CircuitStuff/CMT.html
260http://www.cs.yale.edu/homes/peralta/CircuitStuff/Sinv.txt
261*/
262template <std::unsigned_integral W>
263void AES_INV_SBOX(W V[8]) {
264 const W U0 = V[0];
265 const W U1 = V[1];
266 const W U2 = V[2];
267 const W U3 = V[3];
268 const W U4 = V[4];
269 const W U5 = V[5];
270 const W U6 = V[6];
271 const W U7 = V[7];
272
273 const W Y0 = U0 ^ U3;
274 const W Y2 = ~(U1 ^ U3);
275 const W Y4 = U0 ^ Y2;
276 const W RTL0 = U6 ^ U7;
277 const W Y1 = Y2 ^ RTL0;
278 const W Y7 = ~(U2 ^ Y1);
279 const W RTL1 = U3 ^ U4;
280 const W Y6 = ~(U7 ^ RTL1);
281 const W Y3 = Y1 ^ RTL1;
282 const W RTL2 = ~(U0 ^ U2);
283 const W Y5 = U5 ^ RTL2;
284 const W sa1 = Y0 ^ Y2;
285 const W sa0 = Y1 ^ Y3;
286 const W sb1 = Y4 ^ Y6;
287 const W sb0 = Y5 ^ Y7;
288 const W ah = Y0 ^ Y1;
289 const W al = Y2 ^ Y3;
290 const W aa = sa0 ^ sa1;
291 const W bh = Y4 ^ Y5;
292 const W bl = Y6 ^ Y7;
293 const W bb = sb0 ^ sb1;
294 const W ab20 = sa0 ^ sb0;
295 const W ab22 = al ^ bl;
296 const W ab23 = Y3 ^ Y7;
297 const W ab21 = sa1 ^ sb1;
298 const W abcd1 = ah & bh;
299 const W rr1 = Y0 & Y4;
300 const W ph11 = ab20 ^ abcd1;
301 const W t01 = Y1 & Y5;
302 const W ph01 = t01 ^ abcd1;
303 const W abcd2 = al & bl;
304 const W r1 = Y2 & Y6;
305 const W pl11 = ab22 ^ abcd2;
306 const W r2 = Y3 & Y7;
307 const W pl01 = r2 ^ abcd2;
308 const W r3 = sa0 & sb0;
309 const W vr1 = aa & bb;
310 const W pr1 = vr1 ^ r3;
311 const W wr1 = sa1 & sb1;
312 const W qr1 = wr1 ^ r3;
313 const W ab0 = ph11 ^ rr1;
314 const W ab1 = ph01 ^ ab21;
315 const W ab2 = pl11 ^ r1;
316 const W ab3 = pl01 ^ qr1;
317 const W cp1 = ab0 ^ pr1;
318 const W cp2 = ab1 ^ qr1;
319 const W cp3 = ab2 ^ pr1;
320 const W cp4 = ab3 ^ ab23;
321 const W tinv1 = cp3 ^ cp4;
322 const W tinv2 = cp3 & cp1;
323 const W tinv3 = cp2 ^ tinv2;
324 const W tinv4 = cp1 ^ cp2;
325 const W tinv5 = cp4 ^ tinv2;
326 const W tinv6 = tinv5 & tinv4;
327 const W tinv7 = tinv3 & tinv1;
328 const W d2 = cp4 ^ tinv7;
329 const W d0 = cp2 ^ tinv6;
330 const W tinv8 = cp1 & cp4;
331 const W tinv9 = tinv4 & tinv8;
332 const W tinv10 = tinv4 ^ tinv2;
333 const W d1 = tinv9 ^ tinv10;
334 const W tinv11 = cp2 & cp3;
335 const W tinv12 = tinv1 & tinv11;
336 const W tinv13 = tinv1 ^ tinv2;
337 const W d3 = tinv12 ^ tinv13;
338 const W sd1 = d1 ^ d3;
339 const W sd0 = d0 ^ d2;
340 const W dl = d0 ^ d1; // NOLINT(misc-confusable-identifiers)
341 const W dh = d2 ^ d3;
342 const W dd = sd0 ^ sd1;
343 const W abcd3 = dh & bh;
344 const W rr2 = d3 & Y4;
345 const W t02 = d2 & Y5;
346 const W abcd4 = dl & bl;
347 const W r4 = d1 & Y6;
348 const W r5 = d0 & Y7;
349 const W r6 = sd0 & sb0;
350 const W vr2 = dd & bb;
351 const W wr2 = sd1 & sb1;
352 const W abcd5 = dh & ah;
353 const W r7 = d3 & Y0;
354 const W r8 = d2 & Y1;
355 const W abcd6 = dl & al;
356 const W r9 = d1 & Y2;
357 const W r10 = d0 & Y3;
358 const W r11 = sd0 & sa0;
359 const W vr3 = dd & aa;
360 const W wr3 = sd1 & sa1;
361 const W ph12 = rr2 ^ abcd3;
362 const W ph02 = t02 ^ abcd3;
363 const W pl12 = r4 ^ abcd4;
364 const W pl02 = r5 ^ abcd4;
365 const W pr2 = vr2 ^ r6;
366 const W qr2 = wr2 ^ r6;
367 const W p0 = ph12 ^ pr2;
368 const W p1 = ph02 ^ qr2;
369 const W p2 = pl12 ^ pr2;
370 const W p3 = pl02 ^ qr2;
371 const W ph13 = r7 ^ abcd5;
372 const W ph03 = r8 ^ abcd5;
373 const W pl13 = r9 ^ abcd6;
374 const W pl03 = r10 ^ abcd6;
375 const W pr3 = vr3 ^ r11;
376 const W qr3 = wr3 ^ r11;
377 const W p4 = ph13 ^ pr3;
378 const W S7 = ph03 ^ qr3;
379 const W p6 = pl13 ^ pr3;
380 const W p7 = pl03 ^ qr3;
381 const W S3 = p1 ^ p6;
382 const W S6 = p2 ^ p6;
383 const W S0 = p3 ^ p6;
384 const W X11 = p0 ^ p2;
385 const W S5 = S0 ^ X11;
386 const W X13 = p4 ^ p7;
387 const W X14 = X11 ^ X13;
388 const W S1 = S3 ^ X14;
389 const W X16 = p1 ^ S7;
390 const W S2 = X14 ^ X16;
391 const W X18 = p0 ^ p4;
392 const W X19 = S5 ^ X16;
393 const W S4 = X18 ^ X19;
394
395 V[0] = S0;
396 V[1] = S1;
397 V[2] = S2;
398 V[3] = S3;
399 V[4] = S4;
400 V[5] = S5;
401 V[6] = S6;
402 V[7] = S7;
403}
404
405template <std::unsigned_integral W>
406inline void bit_transpose(W B[8]) {
407 swap_bits<W>(B[1], B[0], rep32<W>(0x55555555), 1);
408 swap_bits<W>(B[3], B[2], rep32<W>(0x55555555), 1);
409 swap_bits<W>(B[5], B[4], rep32<W>(0x55555555), 1);
410 swap_bits<W>(B[7], B[6], rep32<W>(0x55555555), 1);
411
412 swap_bits<W>(B[2], B[0], rep32<W>(0x33333333), 2);
413 swap_bits<W>(B[3], B[1], rep32<W>(0x33333333), 2);
414 swap_bits<W>(B[6], B[4], rep32<W>(0x33333333), 2);
415 swap_bits<W>(B[7], B[5], rep32<W>(0x33333333), 2);
416
417 swap_bits<W>(B[4], B[0], rep32<W>(0x0F0F0F0F), 4);
418 swap_bits<W>(B[5], B[1], rep32<W>(0x0F0F0F0F), 4);
419 swap_bits<W>(B[6], B[2], rep32<W>(0x0F0F0F0F), 4);
420 swap_bits<W>(B[7], B[3], rep32<W>(0x0F0F0F0F), 4);
421}
422
423template <std::unsigned_integral W>
424inline void ks_expand(W B[8], const uint32_t K[], size_t r) {
425 /*
426 This is bit_transpose of K[r..r+4] || K[r..r+4], we can save some computation
427 due to knowing the first and second halves are the same data.
428
429 For 64-bit words each key word is replicated into both 32-bit halves
430 so that the same round key applies to all block-pairs.
431 */
432 for(size_t i = 0; i != 4; ++i) {
433 B[i] = rep32<W>(K[r + i]);
434 }
435
436 swap_bits<W>(B[1], B[0], rep32<W>(0x55555555), 1);
437 swap_bits<W>(B[3], B[2], rep32<W>(0x55555555), 1);
438
439 swap_bits<W>(B[2], B[0], rep32<W>(0x33333333), 2);
440 swap_bits<W>(B[3], B[1], rep32<W>(0x33333333), 2);
441
442 B[4] = B[0];
443 B[5] = B[1];
444 B[6] = B[2];
445 B[7] = B[3];
446
447 swap_bits<W>(B[4], B[0], rep32<W>(0x0F0F0F0F), 4);
448 swap_bits<W>(B[5], B[1], rep32<W>(0x0F0F0F0F), 4);
449 swap_bits<W>(B[6], B[2], rep32<W>(0x0F0F0F0F), 4);
450 swap_bits<W>(B[7], B[3], rep32<W>(0x0F0F0F0F), 4);
451}
452
453template <std::unsigned_integral W>
454inline void shift_rows(W B[8]) {
455 // 3 0 1 2 7 4 5 6 10 11 8 9 14 15 12 13 17 18 19 16 21 22 23 20 24 25 26 27 28 29 30 31
456 //
457 // For 64-bit words the replicated masks operate on each 32-bit half independently
458 for(size_t i = 0; i != 8; ++i) {
459 B[i] = bit_permute_step<W>(B[i], rep32<W>(0x00223311), 2);
460 B[i] = bit_permute_step<W>(B[i], rep32<W>(0x00550055), 1);
461 }
462}
463
464template <std::unsigned_integral W>
465inline void inv_shift_rows(W B[8]) {
466 for(size_t i = 0; i != 8; ++i) {
467 B[i] = bit_permute_step<W>(B[i], rep32<W>(0x00550055), 1);
468 B[i] = bit_permute_step<W>(B[i], rep32<W>(0x00223311), 2);
469 }
470}
471
472template <std::unsigned_integral W>
473inline void mix_columns(W B[8]) {
474 // carry high bits in B[0] to positions in 0x1b == 0b11011
475 const W X2[8] = {
476 B[1],
477 B[2],
478 B[3],
479 B[4] ^ B[0],
480 B[5] ^ B[0],
481 B[6],
482 B[7] ^ B[0],
483 B[0],
484 };
485
486 for(size_t i = 0; i != 8; i++) {
487 const W X3 = B[i] ^ X2[i];
488 B[i] = X2[i] ^ column_rotr<8>(B[i]) ^ column_rotr<16>(B[i]) ^ column_rotr<24>(X3);
489 }
490}
491
492template <std::unsigned_integral W>
493void inv_mix_columns(W B[8]) {
494 /*
495 OpenSSL's bsaes implementation credits Jussi Kivilinna with the lovely
496 matrix decomposition
497
498 | 0e 0b 0d 09 | | 02 03 01 01 | | 05 00 04 00 |
499 | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
500 | 0d 09 0e 0b | | 01 01 02 03 | | 04 00 05 00 |
501 | 0b 0d 09 0e | | 03 01 01 02 | | 00 04 00 05 |
502
503 Notice the first component is simply the MixColumns matrix. So we can
504 multiply first by (05,00,04,00) then perform MixColumns to get the equivalent
505 of InvMixColumn.
506 */
507 const W X4[8] = {
508 B[2],
509 B[3],
510 B[4] ^ B[0],
511 B[5] ^ B[0] ^ B[1],
512 B[6] ^ B[1],
513 B[7] ^ B[0],
514 B[0] ^ B[1],
515 B[1],
516 };
517
518 for(size_t i = 0; i != 8; i++) {
519 const W X5 = X4[i] ^ B[i];
520 B[i] = X5 ^ column_rotr<16>(X4[i]);
521 }
522
523 mix_columns(B);
524}
525
526/*
527* Load up to AES_BITSLICED_BLOCKS blocks from in[] into the bitsliced state B[8].
528*
529* For 32-bit W this is a direct load. For 64-bit W the data is loaded
530* as uint32_t words and then packed so that each 64-bit word holds two
531* independent 32-bit halves (one from each block-pair).
532*/
533template <std::unsigned_integral W>
534inline void bs_load(W B[8], const uint8_t in[], size_t n_blocks) {
535 if constexpr(std::same_as<W, uint32_t>) {
536 load_be(B, in, n_blocks * 4);
537 } else {
538 uint32_t T[16] = {0};
539 load_be(T, in, n_blocks * 4);
540 for(size_t i = 0; i != 8; ++i) {
541 B[i] = (static_cast<W>(T[i]) << 32) | T[i + 8];
542 }
543 }
544}
545
546/*
547* Store the bitsliced state B[8] to out[], reversing the packing done by bs_load.
548*/
549template <std::unsigned_integral W>
550inline void bs_store(uint8_t out[], const W B[8], size_t n_blocks) {
551 if constexpr(std::same_as<W, uint32_t>) {
552 copy_out_be(std::span(out, n_blocks * 16), std::span(B, 8));
553 } else {
554 uint32_t T[16];
555 for(size_t i = 0; i != 8; ++i) {
556 T[i] = static_cast<uint32_t>(B[i] >> 32);
557 T[i + 8] = static_cast<uint32_t>(B[i]);
558 }
559 copy_out_be(std::span(out, n_blocks * 16), T);
560 }
561}
562
563/*
564* AES Encryption
565*/
566void aes_encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks, const secure_vector<uint32_t>& EK) {
567 BOTAN_ASSERT(EK.size() == 44 || EK.size() == 52 || EK.size() == 60, "Key was set");
568
569 const size_t rounds = (EK.size() - 4) / 4;
570
571 using W = aes_bs_word;
572
573 W KS[13 * 8] = {0}; // actual maximum is (rounds - 1) * 8
574 for(size_t i = 0; i < rounds - 1; i += 1) {
575 ks_expand(&KS[8 * i], EK.data(), 4 * i + 4);
576 }
577
578 while(blocks > 0) {
579 const size_t this_loop = std::min(blocks, AES_BITSLICED_BLOCKS);
580
581 W B[8] = {0};
582
583 bs_load(B, in, this_loop);
584
585 CT::poison(B, 8);
586
587 for(size_t i = 0; i != 8; ++i) {
588 B[i] ^= rep32<W>(EK[i % 4]);
589 }
590
591 bit_transpose(B);
592
593 for(size_t r = 0; r != rounds - 1; ++r) {
594 AES_SBOX(B);
595 shift_rows(B);
596 mix_columns(B);
597
598 for(size_t i = 0; i != 8; ++i) {
599 B[i] ^= KS[8 * r + i];
600 }
601 }
602
603 // Final round:
604 AES_SBOX(B);
605 shift_rows(B);
606 bit_transpose(B);
607
608 for(size_t i = 0; i != 8; ++i) {
609 B[i] ^= rep32<W>(EK[4 * rounds + i % 4]);
610 }
611
612 CT::unpoison(B, 8);
613
614 bs_store(out, B, this_loop);
615
616 in += this_loop * 16;
617 out += this_loop * 16;
618 blocks -= this_loop;
619 }
620}
621
622/*
623* AES Decryption
624*/
625void aes_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks, const secure_vector<uint32_t>& DK) {
626 BOTAN_ASSERT(DK.size() == 44 || DK.size() == 52 || DK.size() == 60, "Key was set");
627
628 const size_t rounds = (DK.size() - 4) / 4;
629
630 using W = aes_bs_word;
631
632 W KS[13 * 8] = {0}; // actual maximum is (rounds - 1) * 8
633 for(size_t i = 0; i < rounds - 1; i += 1) {
634 ks_expand(&KS[8 * i], DK.data(), 4 * i + 4);
635 }
636
637 while(blocks > 0) {
638 const size_t this_loop = std::min(blocks, AES_BITSLICED_BLOCKS);
639
640 W B[8] = {0};
641
642 bs_load(B, in, this_loop);
643
644 CT::poison(B, 8);
645
646 for(size_t i = 0; i != 8; ++i) {
647 B[i] ^= rep32<W>(DK[i % 4]);
648 }
649
650 bit_transpose(B);
651
652 for(size_t r = 0; r != rounds - 1; ++r) {
653 AES_INV_SBOX(B);
654 inv_shift_rows(B);
655 inv_mix_columns(B);
656
657 for(size_t i = 0; i != 8; ++i) {
658 B[i] ^= KS[8 * r + i];
659 }
660 }
661
662 // Final round:
663 AES_INV_SBOX(B);
664 inv_shift_rows(B);
665 bit_transpose(B);
666
667 for(size_t i = 0; i != 8; ++i) {
668 B[i] ^= rep32<W>(DK[4 * rounds + i % 4]);
669 }
670
671 CT::unpoison(B, 8);
672
673 bs_store(out, B, this_loop);
674
675 in += this_loop * 16;
676 out += this_loop * 16;
677 blocks -= this_loop;
678 }
679}
680
681inline uint32_t xtime32(uint32_t s) {
682 const uint32_t lo_bit = 0x01010101;
683 const uint32_t mask = 0x7F7F7F7F;
684 const uint32_t poly = 0x1B;
685
686 return ((s & mask) << 1) ^ (((s >> 7) & lo_bit) * poly);
687}
688
689inline uint32_t InvMixColumn(uint32_t s1) {
690 const uint32_t s2 = xtime32(s1);
691 const uint32_t s4 = xtime32(s2);
692 const uint32_t s8 = xtime32(s4);
693 const uint32_t s9 = s8 ^ s1;
694 const uint32_t s11 = s9 ^ s2;
695 const uint32_t s13 = s9 ^ s4;
696 const uint32_t s14 = s8 ^ s4 ^ s2;
697
698 return s14 ^ rotr<8>(s9) ^ rotr<16>(s13) ^ rotr<24>(s11);
699}
700
701void InvMixColumn_x4(uint32_t x[4]) {
702 x[0] = InvMixColumn(x[0]);
703 x[1] = InvMixColumn(x[1]);
704 x[2] = InvMixColumn(x[2]);
705 x[3] = InvMixColumn(x[3]);
706}
707
708uint32_t SE_word(uint32_t x) {
709 aes_bs_word I[8] = {0};
710
711 for(size_t i = 0; i != 8; ++i) {
712 I[i] = (x >> (7 - i)) & 0x01010101;
713 }
714
715 AES_SBOX(I);
716
717 x = 0;
718
719 for(size_t i = 0; i != 8; ++i) {
720 x |= static_cast<uint32_t>((I[i] & 0x01010101) << (7 - i));
721 }
722
723 return x;
724}
725
726void aes_key_schedule(const uint8_t key[], size_t length, secure_vector<uint32_t>& EK, secure_vector<uint32_t>& DK) {
727 static const uint32_t RC[10] = {0x01000000,
728 0x02000000,
729 0x04000000,
730 0x08000000,
731 0x10000000,
732 0x20000000,
733 0x40000000,
734 0x80000000,
735 0x1B000000,
736 0x36000000};
737
738 const size_t X = length / 4;
739
740 // Can't happen, but make static analyzers happy
741 BOTAN_ASSERT_NOMSG(X == 4 || X == 6 || X == 8);
742
743 const size_t rounds = (length / 4) + 6;
744
745 // Help the optimizer
746 BOTAN_ASSERT_NOMSG(rounds == 10 || rounds == 12 || rounds == 14);
747
748 CT::poison(key, length);
749
750 const size_t KS_len = length + 28;
751 EK.resize(KS_len);
752 DK.resize(KS_len);
753
754 for(size_t i = 0; i != X; ++i) {
755 EK[i] = load_be<uint32_t>(key, i);
756 }
757
758 for(size_t i = X; i < 4 * (rounds + 1); i += X) {
759 EK[i] = EK[i - X] ^ RC[(i - X) / X] ^ rotl<8>(SE_word(EK[i - 1]));
760
761 for(size_t j = 1; j != X && (i + j) < EK.size(); ++j) {
762 EK[i + j] = EK[i + j - X];
763
764 if(X == 8 && j == 4) {
765 EK[i + j] ^= SE_word(EK[i + j - 1]);
766 } else {
767 EK[i + j] ^= EK[i + j - 1];
768 }
769 }
770 }
771
772 for(size_t i = 0; i != 4 * (rounds + 1); i += 4) {
773 DK[i] = EK[4 * rounds - i];
774 DK[i + 1] = EK[4 * rounds - i + 1];
775 DK[i + 2] = EK[4 * rounds - i + 2];
776 DK[i + 3] = EK[4 * rounds - i + 3];
777 }
778
779 for(size_t i = 4; i != 4 * rounds; i += 4) {
780 InvMixColumn_x4(&DK[i]);
781 }
782
783 CT::unpoison(EK.data(), EK.size());
784 CT::unpoison(DK.data(), DK.size());
785 CT::unpoison(key, length);
786}
787
788size_t aes_parallelism() {
789#if defined(BOTAN_HAS_AES_VAES)
791 return 8; // pipelined
792 }
793#endif
794
795#if defined(BOTAN_HAS_AES_HWAES)
797 return 4; // pipelined
798 }
799#endif
800
801#if defined(BOTAN_HAS_AES_VPERM)
803 return 2; // pipelined
804 }
805#endif
806
807 // bitsliced:
808 return AES_BITSLICED_BLOCKS;
809}
810
811std::string aes_provider() {
812#if defined(BOTAN_HAS_AES_VAES)
813 if(auto feat = CPUID::check(CPUID::Feature::AVX2_AES)) {
814 return *feat;
815 }
816#endif
817
818#if defined(BOTAN_HAS_AES_HWAES)
819 if(auto feat = CPUID::check(CPUID::Feature::HW_AES)) {
820 return *feat;
821 }
822#endif
823
824#if defined(BOTAN_HAS_AES_VPERM)
825 if(auto feat = CPUID::check(CPUID::Feature::SIMD_4X32)) {
826 return *feat;
827 }
828#endif
829
830 return "base";
831}
832
833} // namespace
834
835std::string AES_128::provider() const {
836 return aes_provider();
837}
838
839std::string AES_192::provider() const {
840 return aes_provider();
841}
842
843std::string AES_256::provider() const {
844 return aes_provider();
845}
846
847size_t AES_128::parallelism() const {
848 return aes_parallelism();
849}
850
851size_t AES_192::parallelism() const {
852 return aes_parallelism();
853}
854
855size_t AES_256::parallelism() const {
856 return aes_parallelism();
857}
858
860 return !m_EK.empty();
861}
862
864 return !m_EK.empty();
865}
866
868 return !m_EK.empty();
869}
870
871void AES_128::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
873
874#if defined(BOTAN_HAS_AES_VAES)
876 return x86_vaes_encrypt_n(in, out, blocks);
877 }
878#endif
879
880#if defined(BOTAN_HAS_AES_HWAES)
882 return hw_aes_encrypt_n(in, out, blocks);
883 }
884#endif
885
886#if defined(BOTAN_HAS_AES_VPERM)
888 return vperm_encrypt_n(in, out, blocks);
889 }
890#endif
891
892 aes_encrypt_n(in, out, blocks, m_EK);
893}
894
895void AES_128::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
897
898#if defined(BOTAN_HAS_AES_VAES)
900 return x86_vaes_decrypt_n(in, out, blocks);
901 }
902#endif
903
904#if defined(BOTAN_HAS_AES_HWAES)
906 return hw_aes_decrypt_n(in, out, blocks);
907 }
908#endif
909
910#if defined(BOTAN_HAS_AES_VPERM)
912 return vperm_decrypt_n(in, out, blocks);
913 }
914#endif
915
916 aes_decrypt_n(in, out, blocks, m_DK);
917}
918
919void AES_128::key_schedule(std::span<const uint8_t> key) {
920#if defined(BOTAN_HAS_AES_HWAES)
922 return hw_aes_key_schedule(key);
923 }
924#endif
925
926#if defined(BOTAN_HAS_AES_VPERM)
928 return vperm_key_schedule(key.data(), key.size());
929 }
930#endif
931
932 aes_key_schedule(key.data(), key.size(), m_EK, m_DK);
933}
934
936 zap(m_EK);
937 zap(m_DK);
938}
939
940void AES_192::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
942
943#if defined(BOTAN_HAS_AES_VAES)
945 return x86_vaes_encrypt_n(in, out, blocks);
946 }
947#endif
948
949#if defined(BOTAN_HAS_AES_HWAES)
951 return hw_aes_encrypt_n(in, out, blocks);
952 }
953#endif
954
955#if defined(BOTAN_HAS_AES_VPERM)
957 return vperm_encrypt_n(in, out, blocks);
958 }
959#endif
960
961 aes_encrypt_n(in, out, blocks, m_EK);
962}
963
964void AES_192::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
966
967#if defined(BOTAN_HAS_AES_VAES)
969 return x86_vaes_decrypt_n(in, out, blocks);
970 }
971#endif
972
973#if defined(BOTAN_HAS_AES_HWAES)
975 return hw_aes_decrypt_n(in, out, blocks);
976 }
977#endif
978
979#if defined(BOTAN_HAS_AES_VPERM)
981 return vperm_decrypt_n(in, out, blocks);
982 }
983#endif
984
985 aes_decrypt_n(in, out, blocks, m_DK);
986}
987
988void AES_192::key_schedule(std::span<const uint8_t> key) {
989#if defined(BOTAN_HAS_AES_HWAES)
991 return hw_aes_key_schedule(key);
992 }
993#endif
994
995#if defined(BOTAN_HAS_AES_VPERM)
997 return vperm_key_schedule(key.data(), key.size());
998 }
999#endif
1000
1001 aes_key_schedule(key.data(), key.size(), m_EK, m_DK);
1002}
1003
1005 zap(m_EK);
1006 zap(m_DK);
1007}
1008
1009void AES_256::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
1011
1012#if defined(BOTAN_HAS_AES_VAES)
1014 return x86_vaes_encrypt_n(in, out, blocks);
1015 }
1016#endif
1017
1018#if defined(BOTAN_HAS_AES_HWAES)
1020 return hw_aes_encrypt_n(in, out, blocks);
1021 }
1022#endif
1023
1024#if defined(BOTAN_HAS_AES_VPERM)
1026 return vperm_encrypt_n(in, out, blocks);
1027 }
1028#endif
1029
1030 aes_encrypt_n(in, out, blocks, m_EK);
1031}
1032
1033void AES_256::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
1035
1036#if defined(BOTAN_HAS_AES_VAES)
1038 return x86_vaes_decrypt_n(in, out, blocks);
1039 }
1040#endif
1041
1042#if defined(BOTAN_HAS_AES_HWAES)
1044 return hw_aes_decrypt_n(in, out, blocks);
1045 }
1046#endif
1047
1048#if defined(BOTAN_HAS_AES_VPERM)
1050 return vperm_decrypt_n(in, out, blocks);
1051 }
1052#endif
1053
1054 aes_decrypt_n(in, out, blocks, m_DK);
1055}
1056
1057void AES_256::key_schedule(std::span<const uint8_t> key) {
1058#if defined(BOTAN_HAS_AES_HWAES)
1060 return hw_aes_key_schedule(key);
1061 }
1062#endif
1063
1064#if defined(BOTAN_HAS_AES_VPERM)
1066 return vperm_key_schedule(key.data(), key.size());
1067 }
1068#endif
1069
1070 aes_key_schedule(key.data(), key.size(), m_EK, m_DK);
1071}
1072
1074 zap(m_EK);
1075 zap(m_DK);
1076}
1077
1078} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:69
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition aes.cpp:895
std::string provider() const override
Definition aes.cpp:835
size_t parallelism() const override
Definition aes.cpp:847
bool has_keying_material() const override
Definition aes.cpp:859
void clear() override
Definition aes.cpp:935
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition aes.cpp:871
bool has_keying_material() const override
Definition aes.cpp:863
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition aes.cpp:964
std::string provider() const override
Definition aes.cpp:839
size_t parallelism() const override
Definition aes.cpp:851
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition aes.cpp:940
void clear() override
Definition aes.cpp:1004
bool has_keying_material() const override
Definition aes.cpp:867
void clear() override
Definition aes.cpp:1073
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition aes.cpp:1009
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition aes.cpp:1033
std::string provider() const override
Definition aes.cpp:843
size_t parallelism() const override
Definition aes.cpp:855
static std::optional< std::string > check(CPUID::Feature feat)
Definition cpuid.h:66
static bool has(CPUID::Feature feat)
Definition cpuid.h:93
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:56
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:261
BOTAN_FORCE_INLINE constexpr void swap_bits(T &x, T &y, T mask, size_t shift)
Definition bit_ops.h:203
BOTAN_FORCE_INLINE constexpr T bit_permute_step(T x, T mask, size_t shift)
Definition bit_ops.h:193
void copy_out_be(std::span< uint8_t > out, const InR &in)
Definition loadstor.h:773
BOTAN_FORCE_INLINE constexpr T rotr(T input)
Definition rotate.h:35
BOTAN_FORCE_INLINE constexpr T rotl(T input)
Definition rotate.h:23
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
Definition types.h:131
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504