AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
aes.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-crypto-export.hpp"
23
24#include "crypto/cipher/blockCipher.hpp"
25
26#include <bitset>
27#include <cstring>
28
29#ifdef ANCH_CPU_DETECTION
30#include "device/cpu.hpp"
31#endif
32
33#ifdef ANCH_CPU_AES
34#include <wmmintrin.h>
35#include <smmintrin.h>
36#endif
37
38#if !defined (ALIGN16)
39# if defined (__GNUC__)
40# define ALIGN16 __attribute__ ( (aligned (16)))
41# else
42# define ALIGN16 __declspec (align (16))
43# endif
44#endif
45
46
47namespace anch::crypto {
48
49#ifdef ANCH_CPU_AES
50 template<std::size_t S>
51 struct AesniKey {
52 ALIGN16 __m128i cipherKey[S];
53 ALIGN16 __m128i decipherKey[S];
54 };
55#endif
56
67 template<std::size_t K, std::size_t R>
68 class ANCH_CRYPTO_EXPORT AES: public anch::crypto::BlockCipher<16> {
69 // Attributes +
70 private:
71#if defined ANCH_CPU_DETECTION || !defined(ANCH_CPU_AES)
73 uint8_t _state[4][4];
74#endif
75
77 union ExpKey {
78#ifdef ANCH_CPU_DETECTION
79 uint32_t swKey[4 * (R + 1)];
80 anch::crypto::AesniKey<R + 1> hwKey;
81#elif ANCH_CPU_AES
82 anch::crypto::AesniKey<R + 1> hwKey;
83#else
84 uint32_t swKey[4 * (R + 1)];
85#endif
86 };
87 ExpKey _expKey;
88 // Attributes -
89
90
91 // Constructors +
92 public:
98 AES(const uint8_t key[4*K]);
99
106 AES(const AES& other);
107 // Constructors -
108
109
110 // Destructors +
114 virtual ~AES();
115 // Destructors -
116
117
118 // Methods +
119 public:
126 void cipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output);
127
134 void decipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output);
135
136 private:
137#ifdef ANCH_CPU_AES
144 void aesniCipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output);
145
152 void aesniDecipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output);
153
162 __m128i aes128assist(__m128i& temp1, __m128i& temp2);
163
169 void aesni128ExpandKey(uint8_t key[4 * K]);
170
178 void aes192assist(__m128i& temp1, __m128i& temp2, __m128i& temp3);
179
185 void aesni192ExpandKey(uint8_t key[4 * K]);
186
193 void aes256assist1(__m128i& temp1, __m128i& temp2);
194
201 void aes256assist2(__m128i& temp1, __m128i& temp3);
202
208 void aesni256ExpandKey(uint8_t key[4 * K]);
209
215 void aesniExpandKey(uint8_t key[4 * K]);
216#endif // ANCH_CPU_AES
217
218#if defined ANCH_CPU_DETECTION || !defined(ANCH_CPU_AES)
225 void swCipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output);
226
233 void swDecipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output);
234
240 void expandKey(const uint8_t key[4 * K]);
241
250 uint32_t subWord(const uint32_t& word);
251
259 uint32_t rotateWord(const uint32_t& word);
260
265 void cipherSubBytes();
266
271 void decipherSubBytes();
272
276 void cipherShiftRows();
277
281 void decipherShiftRows();
282
286 void cipherMixColumns();
287
291 void decipherMixColumns();
292
298 void addRoundKey(const uint32_t& round);
299#endif // ANCH_CPU_DETECTION || !ANCH_CPU_AES
300 // Methods -
301
302 };
303
304 // Constructors +
305 template<std::size_t K, std::size_t R>
306 AES<K,R>::AES(const uint8_t key[4*K]): _state(), _expKey() {
307#ifdef ANCH_CPU_DETECTION
308 if(anch::device::CPU::getInstance().isAES()) {
309 // AES-NI key expansion loads 16 bytes blocks: round buffer size up to avoid out of bounds read for AES-192 (24 bytes key)
310 uint8_t aesKey[(4*K + 15) / 16 * 16] = {};
311 std::memcpy(aesKey, key, 4*K);
312 aesniExpandKey(aesKey);
313 } else {
314 expandKey(key);
315 }
316#elif ANCH_CPU_AES
317 uint8_t aesKey[(4*K + 15) / 16 * 16] = {};
318 std::memcpy(aesKey, key, 4*K);
319 aesniExpandKey(aesKey);
320#else
321 expandKey(key);
322#endif
323 }
324
325 template<std::size_t K, std::size_t R>
326 AES<K,R>::AES(const AES& other): _state(), _expKey() {
327#ifdef ANCH_CPU_DETECTION
328 if(anch::device::CPU::getInstance().isAES()) {
329 std::memcpy(_expKey.hwKey.cipherKey, other._expKey.hwKey.cipherKey, (R + 1) * sizeof(__m128i));
330 std::memcpy(_expKey.hwKey.decipherKey, other._expKey.hwKey.decipherKey, (R + 1) * sizeof(__m128i));
331 } else {
332 std::memcpy(_expKey.swKey, other._expKey.swKey, 4 * (R + 1) * sizeof(uint32_t));
333 }
334#elif ANCH_CPU_AES
335 std::memcpy(_expKey.hwKey.cipherKey, other._expKey.hwKey.cipherKey, (R + 1) * sizeof(__m128i));
336 std::memcpy(_expKey.hwKey.decipherKey, other._expKey.hwKey.decipherKey, (R + 1) * sizeof(__m128i));
337#else
338 std::memcpy(_expKey.swKey, other._expKey.swKey, 4 * (R + 1) * sizeof(uint32_t));
339#endif
340 }
341 // Constructors -
342
343
344 // Destructors +
345 template<std::size_t K, std::size_t R>
347 // Nothing to do
348 }
349 // Destructors -
350
351
352 // Methods +
353 template<std::size_t K, std::size_t R>
354 void
355 AES<K,R>::cipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output) {
356#ifdef ANCH_CPU_DETECTION
357 if(anch::device::CPU::getInstance().isAES()) {
358 aesniCipher(input, output);
359 } else {
360 swCipher(input, output);
361 }
362#elif ANCH_CPU_AES
363 aesniCipher(input, output);
364#else
365 swCipher(input, output);
366#endif
367 }
368
369 template<std::size_t K, std::size_t R>
370 void
371 AES<K,R>::decipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output) {
372#ifdef ANCH_CPU_DETECTION
373 if(anch::device::CPU::getInstance().isAES()) {
374 aesniDecipher(input, output);
375 } else {
376 swDecipher(input, output);
377 }
378#elif ANCH_CPU_AES
379 aesniDecipher(input, output);
380#else
381 swDecipher(input, output);
382#endif
383 }
384
385#ifdef ANCH_CPU_AES
386 template<std::size_t K, std::size_t R>
387 void
388 AES<K,R>::aesniCipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output) {
389 __m128i tmp;
390 std::size_t j;
391 ALIGN16 uint8_t in[16];
392 std::memcpy(in, input.data(), 16);
393 ALIGN16 __m128i out/*[2]*/;
394 /*for(std::size_t i = 0 ; i < 2 ; ++i) {
395 tmp = _mm_loadu_si128(&(reinterpret_cast<__m128i*>(in)[i]));
396 tmp = _mm_xor_si128(tmp, _expKey.hwKey.cipherKey[0]);
397 for(j = 1 ; j < R ; ++j) {
398 tmp = _mm_aesenc_si128(tmp, _expKey.hwKey.cipherKey[j]);
399 }
400 tmp = _mm_aesenclast_si128(tmp, _expKey.hwKey.cipherKey[j]);
401 _mm_storeu_si128(&(out[i]), tmp);
402 }*/
403 tmp = _mm_loadu_si128(reinterpret_cast<__m128i*>(in));
404 tmp = _mm_xor_si128(tmp, _expKey.hwKey.cipherKey[0]);
405 for(j = 1 ; j < R ; ++j) {
406 tmp = _mm_aesenc_si128(tmp, _expKey.hwKey.cipherKey[j]);
407 }
408 tmp = _mm_aesenclast_si128(tmp, _expKey.hwKey.cipherKey[j]);
409 _mm_storeu_si128(&out, tmp);
410 std::memcpy(output.data(), reinterpret_cast<uint8_t*>(&out), 16);
411 }
412
413 template<std::size_t K, std::size_t R>
414 void
415 AES<K,R>::aesniDecipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output) {
416 __m128i tmp;
417 std::size_t j;
418 ALIGN16 uint8_t in[16];
419 std::memcpy(in, input.data(), 16);
420 ALIGN16 __m128i out/*[2]*/;
421 /*for(std::size_t i = 0 ; i < 2 ; ++i) {
422 tmp = _mm_loadu_si128(&(reinterpret_cast<__m128i*>(in)[i]));
423 tmp = _mm_xor_si128(tmp, _expKey.hwKey.decipherKey[0]);
424 for(j = 1 ; j < R ; ++j) {
425 tmp = _mm_aesdec_si128(tmp, _expKey.hwKey.decipherKey[j]);
426 }
427 tmp = _mm_aesdeclast_si128(tmp, _expKey.hwKey.decipherKey[j]);
428 _mm_storeu_si128(&(out[i]), tmp);
429 }*/
430 tmp = _mm_loadu_si128(reinterpret_cast<__m128i*>(in));
431 tmp = _mm_xor_si128(tmp, _expKey.hwKey.decipherKey[0]);
432 for(j = 1 ; j < R ; ++j) {
433 tmp = _mm_aesdec_si128(tmp, _expKey.hwKey.decipherKey[j]);
434 }
435 tmp = _mm_aesdeclast_si128(tmp, _expKey.hwKey.decipherKey[j]);
436 _mm_storeu_si128(&out, tmp);
437 std::memcpy(output.data(), reinterpret_cast<uint8_t*>(&out), 16);
438 }
439
440 template<std::size_t K, std::size_t R>
441 inline __m128i
442 AES<K,R>::aes128assist(__m128i& temp1, __m128i& temp2) {
443 __m128i temp3;
444 temp2 = _mm_shuffle_epi32(temp2 ,0xff);
445 temp3 = _mm_slli_si128(temp1, 0x4);
446 temp1 = _mm_xor_si128(temp1, temp3);
447 temp3 = _mm_slli_si128(temp3, 0x4);
448 temp1 = _mm_xor_si128(temp1, temp3);
449 temp3 = _mm_slli_si128(temp3, 0x4);
450 temp1 = _mm_xor_si128(temp1, temp3);
451 temp1 = _mm_xor_si128(temp1, temp2);
452 return temp1;
453 }
454
455 template<std::size_t K, std::size_t R>
456 inline void
457 AES<K,R>::aesni128ExpandKey([[maybe_unused]] uint8_t key[4 * K]) {
458 if constexpr (K == 4) {
459 __m128i temp1, temp2;
460 temp1 = _mm_loadu_si128(reinterpret_cast<__m128i*>(key));
461 _expKey.hwKey.cipherKey[0] = temp1;
462 temp2 = _mm_aeskeygenassist_si128(temp1, 0x1);
463 temp1 = aes128assist(temp1, temp2);
464 _expKey.hwKey.cipherKey[1] = temp1;
465 temp2 = _mm_aeskeygenassist_si128(temp1, 0x2);
466 temp1 = aes128assist(temp1, temp2);
467 _expKey.hwKey.cipherKey[2] = temp1;
468 temp2 = _mm_aeskeygenassist_si128(temp1, 0x4);
469 temp1 = aes128assist(temp1, temp2);
470 _expKey.hwKey.cipherKey[3] = temp1;
471 temp2 = _mm_aeskeygenassist_si128(temp1, 0x8);
472 temp1 = aes128assist(temp1, temp2);
473 _expKey.hwKey.cipherKey[4] = temp1;
474 temp2 = _mm_aeskeygenassist_si128(temp1, 0x10);
475 temp1 = aes128assist(temp1, temp2);
476 _expKey.hwKey.cipherKey[5] = temp1;
477 temp2 = _mm_aeskeygenassist_si128(temp1, 0x20);
478 temp1 = aes128assist(temp1, temp2);
479 _expKey.hwKey.cipherKey[6] = temp1;
480 temp2 = _mm_aeskeygenassist_si128(temp1, 0x40);
481 temp1 = aes128assist(temp1, temp2);
482 _expKey.hwKey.cipherKey[7] = temp1;
483 temp2 = _mm_aeskeygenassist_si128(temp1, 0x80);
484 temp1 = aes128assist(temp1, temp2);
485 _expKey.hwKey.cipherKey[8] = temp1;
486 temp2 = _mm_aeskeygenassist_si128(temp1, 0x1b);
487 temp1 = aes128assist(temp1, temp2);
488 _expKey.hwKey.cipherKey[9] = temp1;
489 temp2 = _mm_aeskeygenassist_si128(temp1, 0x36);
490 temp1 = aes128assist(temp1, temp2);
491 _expKey.hwKey.cipherKey[10] = temp1;
492 }
493 }
494
495 template<std::size_t K, std::size_t R>
496 inline void
497 AES<K,R>::aes192assist(__m128i& temp1, __m128i& temp2, __m128i& temp3) {
498 __m128i temp4;
499 temp2 = _mm_shuffle_epi32(temp2, 0x55);
500 temp4 = _mm_slli_si128(temp1, 0x4);
501 temp1 = _mm_xor_si128(temp1, temp4);
502 temp4 = _mm_slli_si128(temp4, 0x4);
503 temp1 = _mm_xor_si128(temp1, temp4);
504 temp4 = _mm_slli_si128(temp4, 0x4);
505 temp1 = _mm_xor_si128(temp1, temp4);
506 temp1 = _mm_xor_si128(temp1, temp2);
507 temp2 = _mm_shuffle_epi32(temp1, 0xff);
508 temp4 = _mm_slli_si128(temp3, 0x4);
509 temp3 = _mm_xor_si128(temp3, temp4);
510 temp3 = _mm_xor_si128(temp3, temp2);
511 }
512
513 template<std::size_t K, std::size_t R>
514 inline void
515 AES<K,R>::aesni192ExpandKey([[maybe_unused]] uint8_t key[4 * K]) {
516 if constexpr (K == 6) {
517 __m128i temp1, temp2, temp3;
518 temp1 = _mm_loadu_si128(reinterpret_cast<__m128i*>(key));
519 temp3 = _mm_loadu_si128(reinterpret_cast<__m128i*>((key + 16)));
520 _expKey.hwKey.cipherKey[0] = temp1;
521 _expKey.hwKey.cipherKey[1] = temp3;
522 temp2 = _mm_aeskeygenassist_si128(temp3, 0x1);
523 aes192assist(temp1, temp2, temp3);
524 _expKey.hwKey.cipherKey[1] = reinterpret_cast<__m128i>(_mm_shuffle_pd(reinterpret_cast<__m128d>(_expKey.hwKey.cipherKey[1]),
525 reinterpret_cast<__m128d>(temp1), 0));
526 _expKey.hwKey.cipherKey[2] = reinterpret_cast<__m128i>(_mm_shuffle_pd(reinterpret_cast<__m128d>(temp1),
527 reinterpret_cast<__m128d>(temp3), 1));
528 temp2 = _mm_aeskeygenassist_si128(temp3, 0x2);
529 aes192assist(temp1, temp2, temp3);
530 _expKey.hwKey.cipherKey[3] = temp1;
531 _expKey.hwKey.cipherKey[4] = temp3;
532 temp2 = _mm_aeskeygenassist_si128(temp3, 0x4);
533 aes192assist(temp1, temp2, temp3);
534 _expKey.hwKey.cipherKey[4] = reinterpret_cast<__m128i>(_mm_shuffle_pd(reinterpret_cast<__m128d>(_expKey.hwKey.cipherKey[4]),
535 reinterpret_cast<__m128d>(temp1), 0));
536 _expKey.hwKey.cipherKey[5] = reinterpret_cast<__m128i>(_mm_shuffle_pd(reinterpret_cast<__m128d>(temp1),
537 reinterpret_cast<__m128d>(temp3), 1));
538 temp2 = _mm_aeskeygenassist_si128(temp3, 0x8);
539 aes192assist(temp1, temp2, temp3);
540 _expKey.hwKey.cipherKey[6] = temp1;
541 _expKey.hwKey.cipherKey[7] = temp3;
542 temp2 = _mm_aeskeygenassist_si128(temp3, 0x10);
543 aes192assist(temp1, temp2, temp3);
544 _expKey.hwKey.cipherKey[7] = reinterpret_cast<__m128i>(_mm_shuffle_pd(reinterpret_cast<__m128d>(_expKey.hwKey.cipherKey[7]),
545 reinterpret_cast<__m128d>(temp1), 0));
546 _expKey.hwKey.cipherKey[8] = reinterpret_cast<__m128i>(_mm_shuffle_pd(reinterpret_cast<__m128d>(temp1),
547 reinterpret_cast<__m128d>(temp3), 1));
548 temp2 = _mm_aeskeygenassist_si128 (temp3, 0x20);
549 aes192assist(temp1, temp2, temp3);
550 _expKey.hwKey.cipherKey[9] = temp1;
551 _expKey.hwKey.cipherKey[10] = temp3;
552 temp2 = _mm_aeskeygenassist_si128(temp3, 0x40);
553 aes192assist(temp1, temp2, temp3);
554 _expKey.hwKey.cipherKey[10] = reinterpret_cast<__m128i>(_mm_shuffle_pd(reinterpret_cast<__m128d>(_expKey.hwKey.cipherKey[10]),
555 reinterpret_cast<__m128d>(temp1), 0));
556 _expKey.hwKey.cipherKey[11] = reinterpret_cast<__m128i>(_mm_shuffle_pd(reinterpret_cast<__m128d>(temp1),
557 reinterpret_cast<__m128d>(temp3), 1));
558 temp2 = _mm_aeskeygenassist_si128(temp3, 0x80);
559 aes192assist(temp1, temp2, temp3);
560 _expKey.hwKey.cipherKey[12] = temp1;
561 }
562 }
563
564 template<std::size_t K, std::size_t R>
565 inline void
566 AES<K,R>::aes256assist1(__m128i& temp1, __m128i& temp2) {
567 __m128i temp4;
568 temp2 = _mm_shuffle_epi32(temp2, 0xff);
569 temp4 = _mm_slli_si128(temp1, 0x4);
570 temp1 = _mm_xor_si128(temp1, temp4);
571 temp4 = _mm_slli_si128(temp4, 0x4);
572 temp1 = _mm_xor_si128(temp1, temp4);
573 temp4 = _mm_slli_si128(temp4, 0x4);
574 temp1 = _mm_xor_si128(temp1, temp4);
575 temp1 = _mm_xor_si128(temp1, temp2);
576 }
577
578 template<std::size_t K, std::size_t R>
579 inline void
580 AES<K,R>::aes256assist2(__m128i& temp1, __m128i& temp3) {
581 __m128i temp2,temp4;
582 temp4 = _mm_aeskeygenassist_si128(temp1, 0x0);
583 temp2 = _mm_shuffle_epi32(temp4, 0xaa);
584 temp4 = _mm_slli_si128(temp3, 0x4);
585 temp3 = _mm_xor_si128(temp3, temp4);
586 temp4 = _mm_slli_si128(temp4, 0x4);
587 temp3 = _mm_xor_si128(temp3, temp4);
588 temp4 = _mm_slli_si128(temp4, 0x4);
589 temp3 = _mm_xor_si128(temp3, temp4);
590 temp3 = _mm_xor_si128(temp3, temp2);
591 }
592
593 template<std::size_t K, std::size_t R>
594 inline void
595 AES<K,R>::aesni256ExpandKey([[maybe_unused]] uint8_t key[4 * K]) {
596 if constexpr (K == 8) {
597 __m128i temp1, temp2, temp3;
598 temp1 = _mm_loadu_si128(reinterpret_cast<__m128i*>(key));
599 temp3 = _mm_loadu_si128(reinterpret_cast<__m128i*>(key + 16));
600 _expKey.hwKey.cipherKey[0] = temp1;
601 _expKey.hwKey.cipherKey[1] = temp3;
602 temp2 = _mm_aeskeygenassist_si128(temp3, 0x01);
603 aes256assist1(temp1, temp2);
604 _expKey.hwKey.cipherKey[2] = temp1;
605 aes256assist2(temp1, temp3);
606 _expKey.hwKey.cipherKey[3] = temp3;
607 temp2 = _mm_aeskeygenassist_si128(temp3, 0x02);
608 aes256assist1(temp1, temp2);
609 _expKey.hwKey.cipherKey[4] = temp1;
610 aes256assist2(temp1, temp3);
611 _expKey.hwKey.cipherKey[5] = temp3;
612 temp2 = _mm_aeskeygenassist_si128(temp3, 0x04);
613 aes256assist1(temp1, temp2);
614 _expKey.hwKey.cipherKey[6] = temp1;
615 aes256assist2(temp1, temp3);
616 _expKey.hwKey.cipherKey[7] = temp3;
617 temp2 = _mm_aeskeygenassist_si128(temp3, 0x08);
618 aes256assist1(temp1, temp2);
619 _expKey.hwKey.cipherKey[8] = temp1;
620 aes256assist2(temp1, temp3);
621 _expKey.hwKey.cipherKey[9] = temp3;
622 temp2 = _mm_aeskeygenassist_si128(temp3, 0x10);
623 aes256assist1(temp1, temp2);
624 _expKey.hwKey.cipherKey[10] = temp1;
625 aes256assist2(temp1, temp3);
626 _expKey.hwKey.cipherKey[11] = temp3;
627 temp2 = _mm_aeskeygenassist_si128(temp3, 0x20);
628 aes256assist1(temp1, temp2);
629 _expKey.hwKey.cipherKey[12] = temp1;
630 aes256assist2(temp1, temp3);
631 _expKey.hwKey.cipherKey[13] = temp3;
632 temp2 = _mm_aeskeygenassist_si128(temp3, 0x40);
633 aes256assist1(temp1, temp2);
634 _expKey.hwKey.cipherKey[14] = temp1;
635 }
636 }
637
638 template<std::size_t K, std::size_t R>
639 void
640 AES<K,R>::aesniExpandKey(uint8_t key[4 * K]) {
641 // Compute cipher key +
642 aesni128ExpandKey(key);
643 aesni192ExpandKey(key);
644 aesni256ExpandKey(key);
645 // Compute cipher key -
646
647 // Compute decipher key +
648 _expKey.hwKey.decipherKey[R] = _expKey.hwKey.cipherKey[0];
649 _expKey.hwKey.decipherKey[R - 1] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[1]);
650 _expKey.hwKey.decipherKey[R - 2] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[2]);
651 _expKey.hwKey.decipherKey[R - 3] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[3]);
652 _expKey.hwKey.decipherKey[R - 4] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[4]);
653 _expKey.hwKey.decipherKey[R - 5] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[5]);
654 _expKey.hwKey.decipherKey[R - 6] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[6]);
655 _expKey.hwKey.decipherKey[R - 7] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[7]);
656 _expKey.hwKey.decipherKey[R - 8] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[8]);
657 _expKey.hwKey.decipherKey[R - 9] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[9]);
658 if constexpr (R > 10) {
659 _expKey.hwKey.decipherKey[R - 10] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[10]);
660 _expKey.hwKey.decipherKey[R - 11] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[11]);
661 }
662 if constexpr (R > 12) {
663 _expKey.hwKey.decipherKey[R - 12] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[12]);
664 _expKey.hwKey.decipherKey[R - 13] = _mm_aesimc_si128(_expKey.hwKey.cipherKey[13]);
665 }
666 _expKey.hwKey.decipherKey[0] = _expKey.hwKey.cipherKey[R];
667 // Compute decipher key -
668 }
669#endif // ANCH_CPU_AES
670
671#if defined ANCH_CPU_DETECTION || !defined(ANCH_CPU_AES)
673 extern ANCH_CRYPTO_EXPORT const uint8_t ANCH_AES_CIPHER_SBOX[256];
674
676 extern ANCH_CRYPTO_EXPORT const uint8_t ANCH_AES_DECIPHER_SBOX[256];
677
679 extern ANCH_CRYPTO_EXPORT const uint32_t ANCH_AES_RCON[11];
680
681 template<std::size_t K, std::size_t R>
682 void
683 AES<K,R>::swCipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output) {
684 std::memcpy(&_state, input.data(), 16);
685 unsigned int round = 0;
686
687 // Initial round +
688 addRoundKey(round);
689 // Initial round -
690
691 // Rounds +
692 for(round = 1 ; round < R ; ++round) {
693 cipherSubBytes();
694 cipherShiftRows();
695 cipherMixColumns();
696 addRoundKey(round);
697 }
698 // Rounds -
699
700 // Final round +
701 cipherSubBytes();
702 cipherShiftRows();
703 addRoundKey(round);
704 // Final round -
705
706 std::memcpy(output.data(), &_state, 16);
707 }
708
709 template<std::size_t K, std::size_t R>
710 void
711 AES<K,R>::swDecipher(const std::array<uint8_t,16>& input, std::array<uint8_t,16>& output) {
712 std::memcpy(&_state, input.data(), 16);
713 unsigned int round = R;
714
715 // Initial round +
716 addRoundKey(round);
717 decipherShiftRows();
718 decipherSubBytes();
719 // Initial round -
720
721 // Rounds +
722 for(round = R - 1 ; round > 0 ; --round) {
723 addRoundKey(round);
724 decipherMixColumns();
725 decipherShiftRows();
726 decipherSubBytes();
727 }
728 // Rounds -
729
730 // Final round +
731 addRoundKey(round);
732 // Final round -
733
734 std::memcpy(output.data(), &_state, 16);
735 }
736
737 template<std::size_t K, std::size_t R>
738 void
739 AES<K,R>::expandKey(const uint8_t key[4 * K]) {
740 std::memcpy(_expKey.swKey, key, 4 * K);
741 for(std::size_t i = K ; i < 4 * (R + 1) ; ++i) {
742 uint32_t mod = static_cast<uint32_t>(i % K);
743 if(mod == 0) {
744 _expKey.swKey[i] = _expKey.swKey[i - K] ^ (subWord(rotateWord(_expKey.swKey[i - 1])) ^ ANCH_AES_RCON[i / K]);
745 } else if(K > 6 && mod == 4) {
746 _expKey.swKey[i] = _expKey.swKey[i - K] ^ subWord(_expKey.swKey[i - 1]);
747 } else {
748 _expKey.swKey[i] = _expKey.swKey[i - K] ^ _expKey.swKey[i - 1];
749 }
750 }
751 }
752
753 template<std::size_t K, std::size_t R>
754 inline uint32_t
755 AES<K,R>::subWord(const uint32_t& word) {
756 uint32_t res;
757 uint8_t* resBytes = reinterpret_cast<uint8_t*>(&res);
758 const uint8_t* const bytes = reinterpret_cast<const uint8_t* const>(&word);
759 resBytes[0] = ANCH_AES_CIPHER_SBOX[bytes[0]];
760 resBytes[1] = ANCH_AES_CIPHER_SBOX[bytes[1]];
761 resBytes[2] = ANCH_AES_CIPHER_SBOX[bytes[2]];
762 resBytes[3] = ANCH_AES_CIPHER_SBOX[bytes[3]];
763 return res;
764 }
765
766 template<std::size_t K, std::size_t R>
767 inline uint32_t
768 AES<K,R>::rotateWord(const uint32_t& word) {
769 return ((word << 24) | (word >> 8));
770 }
771
772 template<std::size_t K, std::size_t R>
773 inline void
774 AES<K,R>::cipherSubBytes() {
775 _state[0][0] = ANCH_AES_CIPHER_SBOX[_state[0][0]];
776 _state[0][1] = ANCH_AES_CIPHER_SBOX[_state[0][1]];
777 _state[0][2] = ANCH_AES_CIPHER_SBOX[_state[0][2]];
778 _state[0][3] = ANCH_AES_CIPHER_SBOX[_state[0][3]];
779 _state[1][0] = ANCH_AES_CIPHER_SBOX[_state[1][0]];
780 _state[1][1] = ANCH_AES_CIPHER_SBOX[_state[1][1]];
781 _state[1][2] = ANCH_AES_CIPHER_SBOX[_state[1][2]];
782 _state[1][3] = ANCH_AES_CIPHER_SBOX[_state[1][3]];
783 _state[2][0] = ANCH_AES_CIPHER_SBOX[_state[2][0]];
784 _state[2][1] = ANCH_AES_CIPHER_SBOX[_state[2][1]];
785 _state[2][2] = ANCH_AES_CIPHER_SBOX[_state[2][2]];
786 _state[2][3] = ANCH_AES_CIPHER_SBOX[_state[2][3]];
787 _state[3][0] = ANCH_AES_CIPHER_SBOX[_state[3][0]];
788 _state[3][1] = ANCH_AES_CIPHER_SBOX[_state[3][1]];
789 _state[3][2] = ANCH_AES_CIPHER_SBOX[_state[3][2]];
790 _state[3][3] = ANCH_AES_CIPHER_SBOX[_state[3][3]];
791 }
792
793 template<std::size_t K, std::size_t R>
794 inline void
795 AES<K,R>::decipherSubBytes() {
796 _state[0][0] = ANCH_AES_DECIPHER_SBOX[_state[0][0]];
797 _state[0][1] = ANCH_AES_DECIPHER_SBOX[_state[0][1]];
798 _state[0][2] = ANCH_AES_DECIPHER_SBOX[_state[0][2]];
799 _state[0][3] = ANCH_AES_DECIPHER_SBOX[_state[0][3]];
800 _state[1][0] = ANCH_AES_DECIPHER_SBOX[_state[1][0]];
801 _state[1][1] = ANCH_AES_DECIPHER_SBOX[_state[1][1]];
802 _state[1][2] = ANCH_AES_DECIPHER_SBOX[_state[1][2]];
803 _state[1][3] = ANCH_AES_DECIPHER_SBOX[_state[1][3]];
804 _state[2][0] = ANCH_AES_DECIPHER_SBOX[_state[2][0]];
805 _state[2][1] = ANCH_AES_DECIPHER_SBOX[_state[2][1]];
806 _state[2][2] = ANCH_AES_DECIPHER_SBOX[_state[2][2]];
807 _state[2][3] = ANCH_AES_DECIPHER_SBOX[_state[2][3]];
808 _state[3][0] = ANCH_AES_DECIPHER_SBOX[_state[3][0]];
809 _state[3][1] = ANCH_AES_DECIPHER_SBOX[_state[3][1]];
810 _state[3][2] = ANCH_AES_DECIPHER_SBOX[_state[3][2]];
811 _state[3][3] = ANCH_AES_DECIPHER_SBOX[_state[3][3]];
812 }
813
814 template<std::size_t K, std::size_t R>
815 inline void
816 AES<K,R>::cipherShiftRows() {
817 uint8_t tmp = _state[0][1];
818 _state[0][1] = _state[1][1];
819 _state[1][1] = _state[2][1];
820 _state[2][1] = _state[3][1];
821 _state[3][1] = tmp;
822 tmp = _state[1][2];
823 _state[1][2] = _state[3][2];
824 _state[3][2] = tmp;
825 tmp = _state[2][2];
826 _state[2][2] = _state[0][2];
827 _state[0][2] = tmp;
828 tmp = _state[3][3];
829 _state[3][3] = _state[2][3];
830 _state[2][3] = _state[1][3];
831 _state[1][3] = _state[0][3];
832 _state[0][3] = tmp;
833 }
834
835 template<std::size_t K, std::size_t R>
836 inline void
837 AES<K,R>::decipherShiftRows() {
838 uint8_t tmp = _state[3][1];
839 _state[3][1] = _state[2][1];
840 _state[2][1] = _state[1][1];
841 _state[1][1] = _state[0][1];
842 _state[0][1] = tmp;
843 tmp = _state[1][2];
844 _state[1][2] = _state[3][2];
845 _state[3][2] = tmp;
846 tmp = _state[2][2];
847 _state[2][2] = _state[0][2];
848 _state[0][2] = tmp;
849 tmp = _state[0][3];
850 _state[0][3] = _state[1][3];
851 _state[1][3] = _state[2][3];
852 _state[2][3] = _state[3][3];
853 _state[3][3] = tmp;
854 }
855
856 template<std::size_t K, std::size_t R>
857 inline void
858 AES<K,R>::cipherMixColumns() {
859 uint8_t state[4];
860
861 state[0] = _state[0][0];
862 state[1] = _state[0][1];
863 state[2] = _state[0][2];
864 state[3] = _state[0][3];
865 _state[0][0] = ANCH_GALOIS_MULT2[state[0]] ^ ANCH_GALOIS_MULT3[state[1]] ^ state[2] ^ state[3];
866 _state[0][1] = state[0] ^ ANCH_GALOIS_MULT2[state[1]] ^ ANCH_GALOIS_MULT3[state[2]] ^ state[3];
867 _state[0][2] = state[0] ^ state[1] ^ ANCH_GALOIS_MULT2[state[2]] ^ ANCH_GALOIS_MULT3[state[3]];
868 _state[0][3] = ANCH_GALOIS_MULT3[state[0]] ^ state[1] ^ state[2] ^ ANCH_GALOIS_MULT2[state[3]];
869
870 state[0] = _state[1][0];
871 state[1] = _state[1][1];
872 state[2] = _state[1][2];
873 state[3] = _state[1][3];
874 _state[1][0] = ANCH_GALOIS_MULT2[state[0]] ^ ANCH_GALOIS_MULT3[state[1]] ^ state[2] ^ state[3];
875 _state[1][1] = state[0] ^ ANCH_GALOIS_MULT2[state[1]] ^ ANCH_GALOIS_MULT3[state[2]] ^ state[3];
876 _state[1][2] = state[0] ^ state[1] ^ ANCH_GALOIS_MULT2[state[2]] ^ ANCH_GALOIS_MULT3[state[3]];
877 _state[1][3] = ANCH_GALOIS_MULT3[state[0]] ^ state[1] ^ state[2] ^ ANCH_GALOIS_MULT2[state[3]];
878
879 state[0] = _state[2][0];
880 state[1] = _state[2][1];
881 state[2] = _state[2][2];
882 state[3] = _state[2][3];
883 _state[2][0] = ANCH_GALOIS_MULT2[state[0]] ^ ANCH_GALOIS_MULT3[state[1]] ^ state[2] ^ state[3];
884 _state[2][1] = state[0] ^ ANCH_GALOIS_MULT2[state[1]] ^ ANCH_GALOIS_MULT3[state[2]] ^ state[3];
885 _state[2][2] = state[0] ^ state[1] ^ ANCH_GALOIS_MULT2[state[2]] ^ ANCH_GALOIS_MULT3[state[3]];
886 _state[2][3] = ANCH_GALOIS_MULT3[state[0]] ^ state[1] ^ state[2] ^ ANCH_GALOIS_MULT2[state[3]];
887
888 state[0] = _state[3][0];
889 state[1] = _state[3][1];
890 state[2] = _state[3][2];
891 state[3] = _state[3][3];
892 _state[3][0] = ANCH_GALOIS_MULT2[state[0]] ^ ANCH_GALOIS_MULT3[state[1]] ^ state[2] ^ state[3];
893 _state[3][1] = state[0] ^ ANCH_GALOIS_MULT2[state[1]] ^ ANCH_GALOIS_MULT3[state[2]] ^ state[3];
894 _state[3][2] = state[0] ^ state[1] ^ ANCH_GALOIS_MULT2[state[2]] ^ ANCH_GALOIS_MULT3[state[3]];
895 _state[3][3] = ANCH_GALOIS_MULT3[state[0]] ^ state[1] ^ state[2] ^ ANCH_GALOIS_MULT2[state[3]];
896 }
897
898 template<std::size_t K, std::size_t R>
899 inline void
900 AES<K,R>::decipherMixColumns() {
901 uint8_t state[4];
902
903 state[0] = _state[0][0];
904 state[1] = _state[0][1];
905 state[2] = _state[0][2];
906 state[3] = _state[0][3];
907 _state[0][0] = ANCH_GALOIS_MULT14[state[0]] ^ ANCH_GALOIS_MULT11[state[1]] ^ ANCH_GALOIS_MULT13[state[2]] ^ ANCH_GALOIS_MULT9[state[3]];
908 _state[0][1] = ANCH_GALOIS_MULT9[state[0]] ^ ANCH_GALOIS_MULT14[state[1]] ^ ANCH_GALOIS_MULT11[state[2]] ^ ANCH_GALOIS_MULT13[state[3]];
909 _state[0][2] = ANCH_GALOIS_MULT13[state[0]] ^ ANCH_GALOIS_MULT9[state[1]] ^ ANCH_GALOIS_MULT14[state[2]] ^ ANCH_GALOIS_MULT11[state[3]];
910 _state[0][3] = ANCH_GALOIS_MULT11[state[0]] ^ ANCH_GALOIS_MULT13[state[1]] ^ ANCH_GALOIS_MULT9[state[2]] ^ ANCH_GALOIS_MULT14[state[3]];
911
912 state[0] = _state[1][0];
913 state[1] = _state[1][1];
914 state[2] = _state[1][2];
915 state[3] = _state[1][3];
916 _state[1][0] = ANCH_GALOIS_MULT14[state[0]] ^ ANCH_GALOIS_MULT11[state[1]] ^ ANCH_GALOIS_MULT13[state[2]] ^ ANCH_GALOIS_MULT9[state[3]];
917 _state[1][1] = ANCH_GALOIS_MULT9[state[0]] ^ ANCH_GALOIS_MULT14[state[1]] ^ ANCH_GALOIS_MULT11[state[2]] ^ ANCH_GALOIS_MULT13[state[3]];
918 _state[1][2] = ANCH_GALOIS_MULT13[state[0]] ^ ANCH_GALOIS_MULT9[state[1]] ^ ANCH_GALOIS_MULT14[state[2]] ^ ANCH_GALOIS_MULT11[state[3]];
919 _state[1][3] = ANCH_GALOIS_MULT11[state[0]] ^ ANCH_GALOIS_MULT13[state[1]] ^ ANCH_GALOIS_MULT9[state[2]] ^ ANCH_GALOIS_MULT14[state[3]];
920
921 state[0] = _state[2][0];
922 state[1] = _state[2][1];
923 state[2] = _state[2][2];
924 state[3] = _state[2][3];
925 _state[2][0] = ANCH_GALOIS_MULT14[state[0]] ^ ANCH_GALOIS_MULT11[state[1]] ^ ANCH_GALOIS_MULT13[state[2]] ^ ANCH_GALOIS_MULT9[state[3]];
926 _state[2][1] = ANCH_GALOIS_MULT9[state[0]] ^ ANCH_GALOIS_MULT14[state[1]] ^ ANCH_GALOIS_MULT11[state[2]] ^ ANCH_GALOIS_MULT13[state[3]];
927 _state[2][2] = ANCH_GALOIS_MULT13[state[0]] ^ ANCH_GALOIS_MULT9[state[1]] ^ ANCH_GALOIS_MULT14[state[2]] ^ ANCH_GALOIS_MULT11[state[3]];
928 _state[2][3] = ANCH_GALOIS_MULT11[state[0]] ^ ANCH_GALOIS_MULT13[state[1]] ^ ANCH_GALOIS_MULT9[state[2]] ^ ANCH_GALOIS_MULT14[state[3]];
929
930 state[0] = _state[3][0];
931 state[1] = _state[3][1];
932 state[2] = _state[3][2];
933 state[3] = _state[3][3];
934 _state[3][0] = ANCH_GALOIS_MULT14[state[0]] ^ ANCH_GALOIS_MULT11[state[1]] ^ ANCH_GALOIS_MULT13[state[2]] ^ ANCH_GALOIS_MULT9[state[3]];
935 _state[3][1] = ANCH_GALOIS_MULT9[state[0]] ^ ANCH_GALOIS_MULT14[state[1]] ^ ANCH_GALOIS_MULT11[state[2]] ^ ANCH_GALOIS_MULT13[state[3]];
936 _state[3][2] = ANCH_GALOIS_MULT13[state[0]] ^ ANCH_GALOIS_MULT9[state[1]] ^ ANCH_GALOIS_MULT14[state[2]] ^ ANCH_GALOIS_MULT11[state[3]];
937 _state[3][3] = ANCH_GALOIS_MULT11[state[0]] ^ ANCH_GALOIS_MULT13[state[1]] ^ ANCH_GALOIS_MULT9[state[2]] ^ ANCH_GALOIS_MULT14[state[3]];
938 }
939
940 template<std::size_t K, std::size_t R>
941 inline void
942 AES<K,R>::addRoundKey(const uint32_t& round) {
943 const uint32_t* key = _expKey.swKey + 4 * round;
944 // _state is an array of bytes: it is not accessed through a uint32_t pointer (strict aliasing, alignment)
945 uint32_t state[4];
946 std::memcpy(state, _state, sizeof(state));
947 state[0] ^= key[0];
948 state[1] ^= key[1];
949 state[2] ^= key[2];
950 state[3] ^= key[3];
951 std::memcpy(_state, state, sizeof(state));
952 }
953#endif // ANCH_CPU_DETECTION || !ANCH_CPU_AES
954 // Methods -
955
956} // anch::crypto
static CPU & getInstance()
AES block cipher algorithm implementation.
Definition aes.hpp:68
AES(const AES &other)
Definition aes.hpp:326
void cipher(const std::array< uint8_t, 16 > &input, std::array< uint8_t, 16 > &output)
Definition aes.hpp:355
virtual ~AES()
Definition aes.hpp:346
void decipher(const std::array< uint8_t, 16 > &input, std::array< uint8_t, 16 > &output)
Definition aes.hpp:371
AES(const uint8_t key[4 *K])
Definition aes.hpp:306
Block cipher interface.
Definition blockCipher.hpp:59
Cryptography namespace.
Definition base64.hpp:30
const uint8_t ANCH_AES_CIPHER_SBOX[256]
const uint8_t ANCH_GALOIS_MULT11[256]
const uint8_t ANCH_GALOIS_MULT9[256]
const uint32_t ANCH_AES_RCON[11]
const uint8_t ANCH_AES_DECIPHER_SBOX[256]
const uint8_t ANCH_GALOIS_MULT3[256]
const uint8_t ANCH_GALOIS_MULT14[256]
const uint8_t ANCH_GALOIS_MULT13[256]
const uint8_t ANCH_GALOIS_MULT2[256]