AES.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_AES_HPP
  14. #define ZT_AES_HPP
  15. #include "Constants.hpp"
  16. #include "Utils.hpp"
  17. #include "SHA512.hpp"
  18. #include <cstdint>
  19. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  20. #include <xmmintrin.h>
  21. #include <wmmintrin.h>
  22. #include <emmintrin.h>
  23. #include <smmintrin.h>
  24. #define ZT_AES_AESNI 1
  25. #endif
  26. namespace ZeroTier {
  27. /**
  28. * AES-256 and pals including GMAC, CTR, etc.
  29. */
  30. class AES
  31. {
  32. public:
  33. ZT_ALWAYS_INLINE AES() noexcept {}
  34. explicit ZT_ALWAYS_INLINE AES(const uint8_t key[32]) noexcept { this->init(key); }
  35. ZT_ALWAYS_INLINE ~AES() { Utils::burn(&_k,sizeof(_k)); }
  36. /**
  37. * Set (or re-set) this AES256 cipher's key
  38. */
  39. ZT_ALWAYS_INLINE void init(const uint8_t key[32]) noexcept
  40. {
  41. #ifdef ZT_AES_AESNI
  42. if (likely(Utils::CPUID.aes)) {
  43. _init_aesni(key);
  44. return;
  45. }
  46. #endif
  47. _initSW(key);
  48. }
  49. /**
  50. * Encrypt a single AES block (ECB mode)
  51. *
  52. * @param in Input block
  53. * @param out Output block (can be same as input)
  54. */
  55. ZT_ALWAYS_INLINE void encrypt(const uint8_t in[16],uint8_t out[16]) const noexcept
  56. {
  57. #ifdef ZT_AES_AESNI
  58. if (likely(Utils::CPUID.aes)) {
  59. _encrypt_aesni(in,out);
  60. return;
  61. }
  62. #endif
  63. _encryptSW(in,out);
  64. }
  65. private:
  66. static const uint32_t Te0[256];
  67. static const uint32_t Te1[256];
  68. static const uint32_t Te2[256];
  69. static const uint32_t Te3[256];
  70. static const uint32_t rcon[10];
  71. void _initSW(const uint8_t key[32]) noexcept;
  72. void _encryptSW(const uint8_t in[16],uint8_t out[16]) const noexcept;
  73. void _gmacSW(const uint8_t iv[12],const uint8_t *in,unsigned int len,uint8_t out[16]) const noexcept;
  74. union {
  75. #ifdef ZT_AES_ARMNEON
  76. // ARM NEON key and GMAC parameters
  77. struct {
  78. uint32x4_t k[15];
  79. } neon;
  80. #endif
  81. #ifdef ZT_AES_AESNI
  82. // AES-NI key and GMAC parameters
  83. struct {
  84. __m128i k[15];
  85. __m128i h,hh,hhh,hhhh;
  86. } ni;
  87. #endif
  88. // Software mode key and GMAC parameters
  89. struct {
  90. uint64_t h[2];
  91. uint32_t ek[60];
  92. } sw;
  93. } _k;
  94. #ifdef ZT_AES_ARMNEON
  95. static inline void _aes_256_expAssist_armneon(uint32x4_t prev1,uint32x4_t prev2,uint32_t rcon,uint32x4_t *e1,uint32x4_t *e2) noexcept
  96. {
  97. uint32_t round1[4], round2[4], prv1[4], prv2[4];
  98. vst1q_u32(prv1, prev1);
  99. vst1q_u32(prv2, prev2);
  100. round1[0] = sub_word(rot_word(prv2[3])) ^ rcon ^ prv1[0];
  101. round1[1] = sub_word(rot_word(round1[0])) ^ rcon ^ prv1[1];
  102. round1[2] = sub_word(rot_word(round1[1])) ^ rcon ^ prv1[2];
  103. round1[3] = sub_word(rot_word(round1[2])) ^ rcon ^ prv1[3];
  104. round2[0] = sub_word(rot_word(round1[3])) ^ rcon ^ prv2[0];
  105. round2[1] = sub_word(rot_word(round2[0])) ^ rcon ^ prv2[1];
  106. round2[2] = sub_word(rot_word(round2[1])) ^ rcon ^ prv2[2];
  107. round2[3] = sub_word(rot_word(round2[2])) ^ rcon ^ prv2[3];
  108. *e1 = vld1q_u3(round1);
  109. *e2 = vld1q_u3(round2);
  110. //uint32x4_t expansion[2] = {vld1q_u3(round1), vld1q_u3(round2)};
  111. //return expansion;
  112. }
  113. inline void _init_armneon(uint8x16_t encKey) noexcept
  114. {
  115. uint32x4_t *schedule = _k.neon.k;
  116. uint32x4_t e1,e2;
  117. (*schedule)[0] = vld1q_u32(encKey);
  118. (*schedule)[1] = vld1q_u32(encKey + 16);
  119. _aes_256_expAssist_armneon((*schedule)[0],(*schedule)[1],0x01,&e1,&e2);
  120. (*schedule)[2] = e1; (*schedule)[3] = e2;
  121. _aes_256_expAssist_armneon((*schedule)[2],(*schedule)[3],0x01,&e1,&e2);
  122. (*schedule)[4] = e1; (*schedule)[5] = e2;
  123. _aes_256_expAssist_armneon((*schedule)[4],(*schedule)[5],0x01,&e1,&e2);
  124. (*schedule)[6] = e1; (*schedule)[7] = e2;
  125. _aes_256_expAssist_armneon((*schedule)[6],(*schedule)[7],0x01,&e1,&e2);
  126. (*schedule)[8] = e1; (*schedule)[9] = e2;
  127. _aes_256_expAssist_armneon((*schedule)[8],(*schedule)[9],0x01,&e1,&e2);
  128. (*schedule)[10] = e1; (*schedule)[11] = e2;
  129. _aes_256_expAssist_armneon((*schedule)[10],(*schedule)[11],0x01,&e1,&e2);
  130. (*schedule)[12] = e1; (*schedule)[13] = e2;
  131. _aes_256_expAssist_armneon((*schedule)[12],(*schedule)[13],0x01,&e1,&e2);
  132. (*schedule)[14] = e1;
  133. /*
  134. doubleRound = _aes_256_expAssist_armneon((*schedule)[0], (*schedule)[1], 0x01);
  135. (*schedule)[2] = doubleRound[0];
  136. (*schedule)[3] = doubleRound[1];
  137. doubleRound = _aes_256_expAssist_armneon((*schedule)[2], (*schedule)[3], 0x02);
  138. (*schedule)[4] = doubleRound[0];
  139. (*schedule)[5] = doubleRound[1];
  140. doubleRound = _aes_256_expAssist_armneon((*schedule)[4], (*schedule)[5], 0x04);
  141. (*schedule)[6] = doubleRound[0];
  142. (*schedule)[7] = doubleRound[1];
  143. doubleRound = _aes_256_expAssist_armneon((*schedule)[6], (*schedule)[7], 0x08);
  144. (*schedule)[8] = doubleRound[0];
  145. (*schedule)[9] = doubleRound[1];
  146. doubleRound = _aes_256_expAssist_armneon((*schedule)[8], (*schedule)[9], 0x10);
  147. (*schedule)[10] = doubleRound[0];
  148. (*schedule)[11] = doubleRound[1];
  149. doubleRound = _aes_256_expAssist_armneon((*schedule)[10], (*schedule)[11], 0x20);
  150. (*schedule)[12] = doubleRound[0];
  151. (*schedule)[13] = doubleRound[1];
  152. doubleRound = _aes_256_expAssist_armneon((*schedule)[12], (*schedule)[13], 0x40);
  153. (*schedule)[14] = doubleRound[0];
  154. */
  155. }
  156. inline void _encrypt_armneon(uint8x16_t *data) const noexcept
  157. {
  158. *data = veorq_u8(*data, _k.neon.k[0]);
  159. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[1]));
  160. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[2]));
  161. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[3]));
  162. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[4]));
  163. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[5]));
  164. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[6]));
  165. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[7]));
  166. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[8]));
  167. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[9]));
  168. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[10]));
  169. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[11]));
  170. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[12]));
  171. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[13]));
  172. *data = vaeseq_u8(*data, _k.neon.k[14]);
  173. }
  174. #endif
  175. #ifdef ZT_AES_AESNI
  176. void _init_aesni(const uint8_t key[32]) noexcept;
  177. ZT_ALWAYS_INLINE void _encrypt_aesni(const void *const in,void *const out) const noexcept
  178. {
  179. __m128i tmp;
  180. tmp = _mm_loadu_si128((const __m128i *)in);
  181. tmp = _mm_xor_si128(tmp,_k.ni.k[0]);
  182. tmp = _mm_aesenc_si128(tmp,_k.ni.k[1]);
  183. tmp = _mm_aesenc_si128(tmp,_k.ni.k[2]);
  184. tmp = _mm_aesenc_si128(tmp,_k.ni.k[3]);
  185. tmp = _mm_aesenc_si128(tmp,_k.ni.k[4]);
  186. tmp = _mm_aesenc_si128(tmp,_k.ni.k[5]);
  187. tmp = _mm_aesenc_si128(tmp,_k.ni.k[6]);
  188. tmp = _mm_aesenc_si128(tmp,_k.ni.k[7]);
  189. tmp = _mm_aesenc_si128(tmp,_k.ni.k[8]);
  190. tmp = _mm_aesenc_si128(tmp,_k.ni.k[9]);
  191. tmp = _mm_aesenc_si128(tmp,_k.ni.k[10]);
  192. tmp = _mm_aesenc_si128(tmp,_k.ni.k[11]);
  193. tmp = _mm_aesenc_si128(tmp,_k.ni.k[12]);
  194. tmp = _mm_aesenc_si128(tmp,_k.ni.k[13]);
  195. _mm_storeu_si128((__m128i *)out,_mm_aesenclast_si128(tmp,_k.ni.k[14]));
  196. }
  197. void _gmac_aesni(const uint8_t iv[12],const uint8_t *in,unsigned int len,uint8_t out[16]) const noexcept;
  198. #endif
  199. };
  200. } // namespace ZeroTier
  201. #endif