AES.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include <cstring>
  20. #ifndef ZT_AES_NO_ACCEL
  21. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  22. #include <wmmintrin.h>
  23. #include <emmintrin.h>
  24. #include <smmintrin.h>
  25. #include <immintrin.h>
  26. #define ZT_AES_AESNI 1
  27. #endif
  28. #endif
  29. namespace ZeroTier {
  30. /**
  31. * AES-256 and pals including GMAC, CTR, etc.
  32. *
  33. * This includes hardware acceleration for certain processors. The software
  34. * mode is fallback and is significantly slower.
  35. */
  36. class AES
  37. {
  38. public:
  39. /**
  40. * @return True if this system has hardware AES acceleration
  41. */
  42. static ZT_ALWAYS_INLINE bool accelerated()
  43. {
  44. #ifdef ZT_AES_AESNI
  45. return Utils::CPUID.aes;
  46. #else
  47. return false;
  48. #endif
  49. }
  50. /**
  51. * Create an un-initialized AES instance (must call init() before use)
  52. */
  53. ZT_ALWAYS_INLINE AES() noexcept {}
  54. /**
  55. * Create an AES instance with the given key
  56. *
  57. * @param key 256-bit key
  58. */
  59. explicit ZT_ALWAYS_INLINE AES(const uint8_t key[32]) noexcept { this->init(key); }
  60. ZT_ALWAYS_INLINE ~AES() { Utils::burn(&_k,sizeof(_k)); }
  61. /**
  62. * Set (or re-set) this AES256 cipher's key
  63. */
  64. ZT_ALWAYS_INLINE void init(const uint8_t key[32]) noexcept
  65. {
  66. #ifdef ZT_AES_AESNI
  67. if (likely(Utils::CPUID.aes)) {
  68. _init_aesni(key);
  69. return;
  70. }
  71. #endif
  72. _initSW(key);
  73. }
  74. /**
  75. * Encrypt a single AES block
  76. *
  77. * @param in Input block
  78. * @param out Output block (can be same as input)
  79. */
  80. ZT_ALWAYS_INLINE void encrypt(const uint8_t in[16],uint8_t out[16]) const noexcept
  81. {
  82. #ifdef ZT_AES_AESNI
  83. if (likely(Utils::CPUID.aes)) {
  84. _encrypt_aesni(in,out);
  85. return;
  86. }
  87. #endif
  88. _encryptSW(in,out);
  89. }
  90. /**
  91. * Decrypt a single AES block
  92. *
  93. * @param in Input block
  94. * @param out Output block (can be same as input)
  95. */
  96. ZT_ALWAYS_INLINE void decrypt(const uint8_t in[16],uint8_t out[16]) const noexcept
  97. {
  98. #ifdef ZT_AES_AESNI
  99. if (likely(Utils::CPUID.aes)) {
  100. _decrypt_aesni(in,out);
  101. return;
  102. }
  103. #endif
  104. _decryptSW(in,out);
  105. }
  106. /**
  107. * Streaming GMAC calculator
  108. */
  109. class GMAC
  110. {
  111. public:
  112. /**
  113. * Create a new instance of GMAC (must be initialized with init() before use)
  114. *
  115. * @param aes Keyed AES instance to use
  116. */
  117. ZT_ALWAYS_INLINE GMAC(const AES &aes) : _aes(aes) {}
  118. /**
  119. * Reset and initialize for a new GMAC calculation
  120. *
  121. * @param iv 96-bit initialization vector (pad with zeroes if actual IV is shorter)
  122. */
  123. ZT_ALWAYS_INLINE void init(const uint8_t iv[12]) noexcept
  124. {
  125. _rp = 0;
  126. _len = 0;
  127. // We fill the least significant 32 bits in the _iv field with 1 since in GCM mode
  128. // this would hold the counter, but we're not doing GCM. The counter is therefore
  129. // always 1.
  130. #ifdef ZT_AES_AESNI // also implies an x64 processor
  131. *reinterpret_cast<uint64_t *>(_iv) = *reinterpret_cast<const uint64_t *>(iv);
  132. *reinterpret_cast<uint32_t *>(_iv + 8) = *reinterpret_cast<const uint64_t *>(iv + 8);
  133. *reinterpret_cast<uint32_t *>(_iv + 12) = 0x01000000; // 0x00000001 in big-endian byte order
  134. #else
  135. for(int i=0;i<12;++i)
  136. _iv[i] = iv[i];
  137. _iv[12] = 0;
  138. _iv[13] = 0;
  139. _iv[14] = 0;
  140. _iv[15] = 1;
  141. #endif
  142. _y[0] = 0;
  143. _y[1] = 0;
  144. }
  145. /**
  146. * Process data through GMAC
  147. *
  148. * @param data Bytes to process
  149. * @param len Length of input
  150. */
  151. void update(const void *data,unsigned int len) noexcept;
  152. /**
  153. * Process any remaining cached bytes and generate tag
  154. *
  155. * Don't call finish() more than once or you'll get an invalid result.
  156. *
  157. * @param tag 128-bit GMAC tag (can be truncated)
  158. */
  159. void finish(uint8_t tag[16]) noexcept;
  160. private:
  161. const AES &_aes;
  162. unsigned int _rp;
  163. unsigned int _len;
  164. uint8_t _r[16]; // remainder
  165. uint8_t _iv[16];
  166. uint64_t _y[2];
  167. };
  168. /**
  169. * Streaming AES-CTR encrypt/decrypt
  170. */
  171. class CTR
  172. {
  173. public:
  174. ZT_ALWAYS_INLINE CTR(const AES &aes) noexcept : _aes(aes) {}
  175. /**
  176. * Initialize this CTR instance to encrypt a new stream
  177. *
  178. * @param iv Unique initialization vector
  179. * @param output Buffer to which to store output (MUST be large enough for total bytes processed!)
  180. */
  181. ZT_ALWAYS_INLINE void init(const uint8_t iv[16],void *const output) noexcept
  182. {
  183. _ctr[0] = Utils::loadAsIsEndian<uint64_t>(iv);
  184. _ctr[1] = Utils::loadAsIsEndian<uint64_t>(iv + 8);
  185. _out = reinterpret_cast<uint8_t *>(output);
  186. _len = 0;
  187. }
  188. /**
  189. * Encrypt or decrypt data, writing result to the output provided to init()
  190. *
  191. * @param input Input data
  192. * @param len Length of input
  193. */
  194. void crypt(const void *input,unsigned int len) noexcept;
  195. /**
  196. * Finish any remaining bytes if total bytes processed wasn't a multiple of 16
  197. *
  198. * Don't call more than once for a given stream or data may be corrupted.
  199. */
  200. void finish() noexcept;
  201. private:
  202. const AES &_aes;
  203. uint64_t _ctr[2];
  204. uint8_t *_out;
  205. unsigned int _len;
  206. };
  207. private:
  208. static const uint32_t Te0[256];
  209. static const uint32_t Te1[256];
  210. static const uint32_t Te2[256];
  211. static const uint32_t Te3[256];
  212. static const uint32_t Te4[256];
  213. static const uint32_t Td0[256];
  214. static const uint32_t Td1[256];
  215. static const uint32_t Td2[256];
  216. static const uint32_t Td3[256];
  217. static const uint8_t Td4[256];
  218. static const uint32_t rcon[10];
  219. void _initSW(const uint8_t key[32]) noexcept;
  220. void _encryptSW(const uint8_t in[16],uint8_t out[16]) const noexcept;
  221. void _decryptSW(const uint8_t in[16],uint8_t out[16]) const noexcept;
  222. union {
  223. #ifdef ZT_AES_AESNI
  224. struct {
  225. __m128i k[28];
  226. __m128i h,hh,hhh,hhhh;
  227. } ni;
  228. #endif
  229. struct {
  230. uint64_t h[2];
  231. uint32_t ek[60];
  232. uint32_t dk[60];
  233. } sw;
  234. } _k;
  235. #ifdef ZT_AES_AESNI
  236. static const __m128i s_shuf;
  237. void _init_aesni(const uint8_t key[32]) noexcept;
  238. ZT_ALWAYS_INLINE void _encrypt_aesni(const void *const in,void *const out) const noexcept
  239. {
  240. __m128i tmp = _mm_loadu_si128((const __m128i *)in);
  241. tmp = _mm_xor_si128(tmp,_k.ni.k[0]);
  242. tmp = _mm_aesenc_si128(tmp,_k.ni.k[1]);
  243. tmp = _mm_aesenc_si128(tmp,_k.ni.k[2]);
  244. tmp = _mm_aesenc_si128(tmp,_k.ni.k[3]);
  245. tmp = _mm_aesenc_si128(tmp,_k.ni.k[4]);
  246. tmp = _mm_aesenc_si128(tmp,_k.ni.k[5]);
  247. tmp = _mm_aesenc_si128(tmp,_k.ni.k[6]);
  248. tmp = _mm_aesenc_si128(tmp,_k.ni.k[7]);
  249. tmp = _mm_aesenc_si128(tmp,_k.ni.k[8]);
  250. tmp = _mm_aesenc_si128(tmp,_k.ni.k[9]);
  251. tmp = _mm_aesenc_si128(tmp,_k.ni.k[10]);
  252. tmp = _mm_aesenc_si128(tmp,_k.ni.k[11]);
  253. tmp = _mm_aesenc_si128(tmp,_k.ni.k[12]);
  254. tmp = _mm_aesenc_si128(tmp,_k.ni.k[13]);
  255. _mm_storeu_si128((__m128i *)out,_mm_aesenclast_si128(tmp,_k.ni.k[14]));
  256. }
  257. ZT_ALWAYS_INLINE void _decrypt_aesni(const void *in,void *out) const noexcept
  258. {
  259. __m128i tmp = _mm_loadu_si128((const __m128i *)in);
  260. tmp = _mm_xor_si128(tmp,_k.ni.k[14]);
  261. tmp = _mm_aesdec_si128(tmp,_k.ni.k[15]);
  262. tmp = _mm_aesdec_si128(tmp,_k.ni.k[16]);
  263. tmp = _mm_aesdec_si128(tmp,_k.ni.k[17]);
  264. tmp = _mm_aesdec_si128(tmp,_k.ni.k[18]);
  265. tmp = _mm_aesdec_si128(tmp,_k.ni.k[19]);
  266. tmp = _mm_aesdec_si128(tmp,_k.ni.k[20]);
  267. tmp = _mm_aesdec_si128(tmp,_k.ni.k[21]);
  268. tmp = _mm_aesdec_si128(tmp,_k.ni.k[22]);
  269. tmp = _mm_aesdec_si128(tmp,_k.ni.k[23]);
  270. tmp = _mm_aesdec_si128(tmp,_k.ni.k[24]);
  271. tmp = _mm_aesdec_si128(tmp,_k.ni.k[25]);
  272. tmp = _mm_aesdec_si128(tmp,_k.ni.k[26]);
  273. tmp = _mm_aesdec_si128(tmp,_k.ni.k[27]);
  274. _mm_storeu_si128((__m128i *)out,_mm_aesdeclast_si128(tmp,_k.ni.k[0]));
  275. }
  276. static ZT_ALWAYS_INLINE __m128i _mult_block_aesni(const __m128i shuf,const __m128i h,__m128i y) noexcept
  277. {
  278. y = _mm_shuffle_epi8(y,shuf);
  279. __m128i t1 = _mm_clmulepi64_si128(h,y,0x00);
  280. __m128i t2 = _mm_clmulepi64_si128(h,y,0x01);
  281. __m128i t3 = _mm_clmulepi64_si128(h,y,0x10);
  282. __m128i t4 = _mm_clmulepi64_si128(h,y,0x11);
  283. t2 = _mm_xor_si128(t2,t3);
  284. t3 = _mm_slli_si128(t2,8);
  285. t2 = _mm_srli_si128(t2,8);
  286. t1 = _mm_xor_si128(t1,t3);
  287. t4 = _mm_xor_si128(t4,t2);
  288. __m128i t5 = _mm_srli_epi32(t1,31);
  289. t1 = _mm_slli_epi32(t1,1);
  290. __m128i t6 = _mm_srli_epi32(t4,31);
  291. t4 = _mm_slli_epi32(t4,1);
  292. t3 = _mm_srli_si128(t5,12);
  293. t6 = _mm_slli_si128(t6,4);
  294. t5 = _mm_slli_si128(t5,4);
  295. t1 = _mm_or_si128(t1,t5);
  296. t4 = _mm_or_si128(t4,t6);
  297. t4 = _mm_or_si128(t4,t3);
  298. t5 = _mm_slli_epi32(t1,31);
  299. t6 = _mm_slli_epi32(t1,30);
  300. t3 = _mm_slli_epi32(t1,25);
  301. t5 = _mm_xor_si128(t5,t6);
  302. t5 = _mm_xor_si128(t5,t3);
  303. t6 = _mm_srli_si128(t5,4);
  304. t4 = _mm_xor_si128(t4,t6);
  305. t5 = _mm_slli_si128(t5,12);
  306. t1 = _mm_xor_si128(t1,t5);
  307. t4 = _mm_xor_si128(t4,t1);
  308. t5 = _mm_srli_epi32(t1,1);
  309. t2 = _mm_srli_epi32(t1,2);
  310. t3 = _mm_srli_epi32(t1,7);
  311. t4 = _mm_xor_si128(t4,t2);
  312. t4 = _mm_xor_si128(t4,t3);
  313. t4 = _mm_xor_si128(t4,t5);
  314. return _mm_shuffle_epi8(t4,shuf);
  315. }
  316. #endif
  317. };
  318. } // namespace ZeroTier
  319. #endif