AES.hpp 9.2 KB

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