AES.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Copyright (c)2019 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: 2023-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. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  19. #include <wmmintrin.h>
  20. #include <emmintrin.h>
  21. #include <smmintrin.h>
  22. #define ZT_AES_AESNI 1
  23. // AES-aesni.c
  24. extern "C" void zt_crypt_ctr_aesni(const __m128i key[14],const uint8_t iv[16],const uint8_t *in,unsigned int len,uint8_t *out);
  25. #endif // x64
  26. #define ZT_AES_KEY_SIZE 32
  27. #define ZT_AES_BLOCK_SIZE 16
  28. namespace ZeroTier {
  29. /**
  30. * AES-256 and pals
  31. */
  32. class AES
  33. {
  34. public:
  35. /**
  36. * This will be true if your platform's type of AES acceleration is supported on this machine
  37. */
  38. static const bool HW_ACCEL;
  39. ZT_ALWAYS_INLINE AES() {}
  40. ZT_ALWAYS_INLINE AES(const uint8_t key[32]) { this->init(key); }
  41. ZT_ALWAYS_INLINE ~AES() { Utils::burn(&_k,sizeof(_k)); }
  42. /**
  43. * Set (or re-set) this AES256 cipher's key
  44. */
  45. ZT_ALWAYS_INLINE void init(const uint8_t key[32])
  46. {
  47. #ifdef ZT_AES_AESNI
  48. if (likely(HW_ACCEL)) {
  49. _init_aesni(key);
  50. return;
  51. }
  52. #endif
  53. _initSW(key);
  54. }
  55. /**
  56. * Encrypt a single AES block (ECB mode)
  57. *
  58. * @param in Input block
  59. * @param out Output block (can be same as input)
  60. */
  61. ZT_ALWAYS_INLINE void encrypt(const uint8_t in[16],uint8_t out[16]) const
  62. {
  63. #ifdef ZT_AES_AESNI
  64. if (likely(HW_ACCEL)) {
  65. _encrypt_aesni(in,out);
  66. return;
  67. }
  68. #endif
  69. _encryptSW(in,out);
  70. }
  71. /**
  72. * Compute GMAC-AES256 (GCM without ciphertext)
  73. *
  74. * @param iv 96-bit IV
  75. * @param in Input data
  76. * @param len Length of input
  77. * @param out 128-bit authorization tag from GMAC
  78. */
  79. ZT_ALWAYS_INLINE void gmac(const uint8_t iv[12],const void *in,const unsigned int len,uint8_t out[16]) const
  80. {
  81. #ifdef ZT_AES_AESNI
  82. if (likely(HW_ACCEL)) {
  83. _gmac_aesni(iv,(const uint8_t *)in,len,out);
  84. return;
  85. }
  86. #endif
  87. _gmacSW(iv,(const uint8_t *)in,len,out);
  88. }
  89. /**
  90. * Encrypt or decrypt (they're the same) using AES256-CTR
  91. *
  92. * The counter here is a 128-bit big-endian that starts at the IV. The code only
  93. * increments the least significant 64 bits, making it only safe to use for a
  94. * maximum of 2^64-1 bytes (much larger than we ever do).
  95. *
  96. * @param iv 128-bit CTR IV
  97. * @param in Input plaintext or ciphertext
  98. * @param len Length of input
  99. * @param out Output plaintext or ciphertext
  100. */
  101. ZT_ALWAYS_INLINE void ctr(const uint8_t iv[16],const void *in,unsigned int len,void *out) const
  102. {
  103. #ifdef ZT_AES_AESNI
  104. if (likely(HW_ACCEL)) {
  105. zt_crypt_ctr_aesni(_k.ni.k,iv,(const uint8_t *)in,len,(uint8_t *)out);
  106. return;
  107. }
  108. #endif
  109. uint64_t ctr[2],cenc[2];
  110. memcpy(ctr,iv,16);
  111. uint64_t bctr = Utils::ntoh(ctr[1]);
  112. const uint8_t *i = (const uint8_t *)in;
  113. uint8_t *o = (uint8_t *)out;
  114. while (len >= 16) {
  115. _encryptSW((const uint8_t *)ctr,(uint8_t *)cenc);
  116. ctr[1] = Utils::hton(++bctr);
  117. for(unsigned int k=0;k<16;++k)
  118. *(o++) = *(i++) ^ ((uint8_t *)cenc)[k];
  119. len -= 16;
  120. }
  121. if (len) {
  122. _encryptSW((const uint8_t *)ctr,(uint8_t *)cenc);
  123. for(unsigned int k=0;k<len;++k)
  124. *(o++) = *(i++) ^ ((uint8_t *)cenc)[k];
  125. }
  126. }
  127. /**
  128. * Perform AES-GMAC-SIV encryption
  129. *
  130. * This is basically AES-CMAC-SIV but with GMAC in place of CMAC after
  131. * GMAC is run through AES as a keyed hash to make it behave like a
  132. * proper PRF.
  133. *
  134. * See: https://github.com/miscreant/meta/wiki/AES-SIV
  135. *
  136. * The advantage is that this can be described in terms of FIPS and NSA
  137. * ceritifable primitives that are present in FIPS-compliant crypto
  138. * modules.
  139. *
  140. * The extra AES-ECB (keyed hash) encryption of the AES-CTR IV prior
  141. * to use makes the IV itself a secret. This is not strictly necessary
  142. * but comes at little cost.
  143. *
  144. * This code is ZeroTier-specific in a few ways, like the way the IV
  145. * is specified, but would not be hard to generalize.
  146. *
  147. * @param k1 GMAC key
  148. * @param k2 GMAC auth tag keyed hash key
  149. * @param k3 CTR IV keyed hash key
  150. * @param k4 AES-CTR key
  151. * @param iv 64-bit packet IV
  152. * @param pc Packet characteristics byte
  153. * @param in Message plaintext
  154. * @param len Length of plaintext
  155. * @param out Output buffer to receive ciphertext
  156. * @param tag Output buffer to receive 64-bit authentication tag
  157. */
  158. static ZT_ALWAYS_INLINE void gmacSivEncrypt(const AES &k1,const AES &k2,const AES &k3,const AES &k4,const uint8_t iv[8],const uint8_t pc,const void *in,const unsigned int len,void *out,uint8_t tag[8])
  159. {
  160. #ifdef __GNUC__
  161. uint8_t __attribute__ ((aligned (16))) miv[12];
  162. uint8_t __attribute__ ((aligned (16))) ctrIv[16];
  163. #else
  164. uint8_t miv[12];
  165. uint8_t ctrIv[16];
  166. #endif
  167. // GMAC IV is 64-bit packet IV followed by other packet attributes to extend to 96 bits
  168. #ifndef __GNUC__
  169. for(unsigned int i=0;i<8;++i) miv[i] = iv[i];
  170. #else
  171. *((uint64_t *)miv) = *((const uint64_t *)iv);
  172. #endif
  173. miv[8] = pc;
  174. miv[9] = (uint8_t)(len >> 16);
  175. miv[10] = (uint8_t)(len >> 8);
  176. miv[11] = (uint8_t)len;
  177. // Compute auth tag: AES-ECB[k2](GMAC[k1](miv,plaintext))[0:8]
  178. k1.gmac(miv,in,len,ctrIv);
  179. k2.encrypt(ctrIv,ctrIv); // ECB mode encrypt step is because GMAC is not a PRF
  180. #ifdef ZT_NO_TYPE_PUNNING
  181. for(unsigned int i=0;i<8;++i) tag[i] = ctrIv[i];
  182. #else
  183. *((uint64_t *)tag) = *((uint64_t *)ctrIv);
  184. #endif
  185. // Create synthetic CTR IV: AES-ECB[k3](TAG | MIV[0:4] | (MIV[4:8] XOR MIV[8:12]))
  186. #ifndef __GNUC__
  187. for(unsigned int i=0;i<4;++i) ctrIv[i+8] = miv[i];
  188. for(unsigned int i=4;i<8;++i) ctrIv[i+8] = miv[i] ^ miv[i+4];
  189. #else
  190. ((uint32_t *)ctrIv)[2] = ((const uint32_t *)miv)[0];
  191. ((uint32_t *)ctrIv)[3] = ((const uint32_t *)miv)[1] ^ ((const uint32_t *)miv)[2];
  192. #endif
  193. k3.encrypt(ctrIv,ctrIv);
  194. // Encrypt with AES[k4]-CTR
  195. k4.ctr(ctrIv,in,len,out);
  196. }
  197. /**
  198. * Decrypt a message encrypted with AES-GMAC-SIV and check its authenticity
  199. *
  200. * @param k1 GMAC key
  201. * @param k2 GMAC auth tag keyed hash key
  202. * @param k3 CTR IV keyed hash key
  203. * @param k4 AES-CTR key
  204. * @param iv 64-bit message IV
  205. * @param pc Packet characteristics byte
  206. * @param in Message ciphertext
  207. * @param len Length of ciphertext
  208. * @param out Output buffer to receive plaintext
  209. * @param tag Authentication tag supplied with message
  210. * @return True if authentication tags match and message appears authentic
  211. */
  212. static ZT_ALWAYS_INLINE bool gmacSivDecrypt(const AES &k1,const AES &k2,const AES &k3,const AES &k4,const uint8_t iv[8],const uint8_t pc,const void *in,const unsigned int len,void *out,const uint8_t tag[8])
  213. {
  214. #ifdef __GNUC__
  215. uint8_t __attribute__ ((aligned (16))) miv[12];
  216. uint8_t __attribute__ ((aligned (16))) ctrIv[16];
  217. uint8_t __attribute__ ((aligned (16))) gmacOut[16];
  218. #else
  219. uint8_t miv[12];
  220. uint8_t ctrIv[16];
  221. uint8_t gmacOut[16];
  222. #endif
  223. // Extend packet IV to 96-bit message IV using direction byte and message length
  224. #ifdef ZT_NO_TYPE_PUNNING
  225. for(unsigned int i=0;i<8;++i) miv[i] = iv[i];
  226. #else
  227. *((uint64_t *)miv) = *((const uint64_t *)iv);
  228. #endif
  229. miv[8] = pc;
  230. miv[9] = (uint8_t)(len >> 16);
  231. miv[10] = (uint8_t)(len >> 8);
  232. miv[11] = (uint8_t)len;
  233. // Recover synthetic and secret CTR IV from auth tag and packet IV
  234. #ifndef __GNUC__
  235. for(unsigned int i=0;i<8;++i) ctrIv[i] = tag[i];
  236. for(unsigned int i=0;i<4;++i) ctrIv[i+8] = miv[i];
  237. for(unsigned int i=4;i<8;++i) ctrIv[i+8] = miv[i] ^ miv[i+4];
  238. #else
  239. *((uint64_t *)ctrIv) = *((const uint64_t *)tag);
  240. ((uint32_t *)ctrIv)[2] = ((const uint32_t *)miv)[0];
  241. ((uint32_t *)ctrIv)[3] = ((const uint32_t *)miv)[1] ^ ((const uint32_t *)miv)[2];
  242. #endif
  243. k3.encrypt(ctrIv,ctrIv);
  244. // Decrypt with AES[k4]-CTR
  245. k4.ctr(ctrIv,in,len,out);
  246. // Compute AES[k2](GMAC[k1](iv,plaintext))
  247. k1.gmac(miv,out,len,gmacOut);
  248. k2.encrypt(gmacOut,gmacOut);
  249. // Check that packet's auth tag matches first 64 bits of AES(GMAC)
  250. #ifdef ZT_NO_TYPE_PUNNING
  251. return Utils::secureEq(gmacOut,tag,8);
  252. #else
  253. return (*((const uint64_t *)gmacOut) == *((const uint64_t *)tag));
  254. #endif
  255. }
  256. /**
  257. * Use KBKDF with HMAC-SHA-384 to derive four sub-keys for AES-GMAC-SIV from a single master key
  258. *
  259. * See section 5.1 at https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-108.pdf
  260. *
  261. * @param masterKey Master 256-bit key
  262. * @param k1 GMAC key
  263. * @param k2 GMAC auth tag keyed hash key
  264. * @param k3 CTR IV keyed hash key
  265. * @param k4 AES-CTR key
  266. */
  267. static ZT_ALWAYS_INLINE void initGmacCtrKeys(const uint8_t masterKey[32],AES &k1,AES &k2,AES &k3,AES &k4)
  268. {
  269. uint8_t k[32];
  270. KBKDFHMACSHA384(masterKey,ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K1,0,0,k);
  271. k1.init(k);
  272. KBKDFHMACSHA384(masterKey,ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K2,0,0,k);
  273. k2.init(k);
  274. KBKDFHMACSHA384(masterKey,ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K3,0,0,k);
  275. k3.init(k);
  276. KBKDFHMACSHA384(masterKey,ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K4,0,0,k);
  277. k4.init(k);
  278. }
  279. private:
  280. static const uint32_t Te0[256];
  281. static const uint32_t Te1[256];
  282. static const uint32_t Te2[256];
  283. static const uint32_t Te3[256];
  284. static const uint32_t rcon[10];
  285. void _initSW(const uint8_t key[32]);
  286. void _encryptSW(const uint8_t in[16],uint8_t out[16]) const;
  287. void _gmacSW(const uint8_t iv[12],const uint8_t *in,unsigned int len,uint8_t out[16]) const;
  288. /**************************************************************************/
  289. union {
  290. #ifdef ZT_AES_ARMNEON
  291. struct {
  292. uint32x4_t k[15];
  293. } neon;
  294. #endif
  295. #ifdef ZT_AES_AESNI
  296. struct {
  297. __m128i k[15];
  298. __m128i h,hh,hhh,hhhh;
  299. } ni;
  300. #endif
  301. struct {
  302. uint64_t h[2];
  303. uint32_t ek[60];
  304. } sw;
  305. } _k;
  306. /**************************************************************************/
  307. #ifdef ZT_AES_ARMNEON /******************************************************/
  308. static inline void _aes_256_expAssist_armneon(uint32x4_t prev1,uint32x4_t prev2,uint32_t rcon,uint32x4_t *e1,uint32x4_t *e2)
  309. {
  310. uint32_t round1[4], round2[4], prv1[4], prv2[4];
  311. vst1q_u32(prv1, prev1);
  312. vst1q_u32(prv2, prev2);
  313. round1[0] = sub_word(rot_word(prv2[3])) ^ rcon ^ prv1[0];
  314. round1[1] = sub_word(rot_word(round1[0])) ^ rcon ^ prv1[1];
  315. round1[2] = sub_word(rot_word(round1[1])) ^ rcon ^ prv1[2];
  316. round1[3] = sub_word(rot_word(round1[2])) ^ rcon ^ prv1[3];
  317. round2[0] = sub_word(rot_word(round1[3])) ^ rcon ^ prv2[0];
  318. round2[1] = sub_word(rot_word(round2[0])) ^ rcon ^ prv2[1];
  319. round2[2] = sub_word(rot_word(round2[1])) ^ rcon ^ prv2[2];
  320. round2[3] = sub_word(rot_word(round2[2])) ^ rcon ^ prv2[3];
  321. *e1 = vld1q_u3(round1);
  322. *e2 = vld1q_u3(round2);
  323. //uint32x4_t expansion[2] = {vld1q_u3(round1), vld1q_u3(round2)};
  324. //return expansion;
  325. }
  326. inline void _init_armneon(uint8x16_t encKey)
  327. {
  328. uint32x4_t *schedule = _k.neon.k;
  329. uint32x4_t e1,e2;
  330. (*schedule)[0] = vld1q_u32(encKey);
  331. (*schedule)[1] = vld1q_u32(encKey + 16);
  332. _aes_256_expAssist_armneon((*schedule)[0],(*schedule)[1],0x01,&e1,&e2);
  333. (*schedule)[2] = e1; (*schedule)[3] = e2;
  334. _aes_256_expAssist_armneon((*schedule)[2],(*schedule)[3],0x01,&e1,&e2);
  335. (*schedule)[4] = e1; (*schedule)[5] = e2;
  336. _aes_256_expAssist_armneon((*schedule)[4],(*schedule)[5],0x01,&e1,&e2);
  337. (*schedule)[6] = e1; (*schedule)[7] = e2;
  338. _aes_256_expAssist_armneon((*schedule)[6],(*schedule)[7],0x01,&e1,&e2);
  339. (*schedule)[8] = e1; (*schedule)[9] = e2;
  340. _aes_256_expAssist_armneon((*schedule)[8],(*schedule)[9],0x01,&e1,&e2);
  341. (*schedule)[10] = e1; (*schedule)[11] = e2;
  342. _aes_256_expAssist_armneon((*schedule)[10],(*schedule)[11],0x01,&e1,&e2);
  343. (*schedule)[12] = e1; (*schedule)[13] = e2;
  344. _aes_256_expAssist_armneon((*schedule)[12],(*schedule)[13],0x01,&e1,&e2);
  345. (*schedule)[14] = e1;
  346. /*
  347. doubleRound = _aes_256_expAssist_armneon((*schedule)[0], (*schedule)[1], 0x01);
  348. (*schedule)[2] = doubleRound[0];
  349. (*schedule)[3] = doubleRound[1];
  350. doubleRound = _aes_256_expAssist_armneon((*schedule)[2], (*schedule)[3], 0x02);
  351. (*schedule)[4] = doubleRound[0];
  352. (*schedule)[5] = doubleRound[1];
  353. doubleRound = _aes_256_expAssist_armneon((*schedule)[4], (*schedule)[5], 0x04);
  354. (*schedule)[6] = doubleRound[0];
  355. (*schedule)[7] = doubleRound[1];
  356. doubleRound = _aes_256_expAssist_armneon((*schedule)[6], (*schedule)[7], 0x08);
  357. (*schedule)[8] = doubleRound[0];
  358. (*schedule)[9] = doubleRound[1];
  359. doubleRound = _aes_256_expAssist_armneon((*schedule)[8], (*schedule)[9], 0x10);
  360. (*schedule)[10] = doubleRound[0];
  361. (*schedule)[11] = doubleRound[1];
  362. doubleRound = _aes_256_expAssist_armneon((*schedule)[10], (*schedule)[11], 0x20);
  363. (*schedule)[12] = doubleRound[0];
  364. (*schedule)[13] = doubleRound[1];
  365. doubleRound = _aes_256_expAssist_armneon((*schedule)[12], (*schedule)[13], 0x40);
  366. (*schedule)[14] = doubleRound[0];
  367. */
  368. }
  369. inline void _encrypt_armneon(uint8x16_t *data) const
  370. {
  371. *data = veorq_u8(*data, _k.neon.k[0]);
  372. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[1]));
  373. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[2]));
  374. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[3]));
  375. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[4]));
  376. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[5]));
  377. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[6]));
  378. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[7]));
  379. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[8]));
  380. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[9]));
  381. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[10]));
  382. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[11]));
  383. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[12]));
  384. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[13]));
  385. *data = vaeseq_u8(*data, _k.neon.k[14]);
  386. }
  387. #endif /*********************************************************************/
  388. #ifdef ZT_AES_AESNI /********************************************************/
  389. static ZT_ALWAYS_INLINE __m128i _init256_1_aesni(__m128i a,__m128i b)
  390. {
  391. __m128i x,y;
  392. b = _mm_shuffle_epi32(b,0xff);
  393. y = _mm_slli_si128(a,0x04);
  394. x = _mm_xor_si128(a,y);
  395. y = _mm_slli_si128(y,0x04);
  396. x = _mm_xor_si128(x,y);
  397. y = _mm_slli_si128(y,0x04);
  398. x = _mm_xor_si128(x,y);
  399. x = _mm_xor_si128(x,b);
  400. return x;
  401. }
  402. static ZT_ALWAYS_INLINE __m128i _init256_2_aesni(__m128i a,__m128i b)
  403. {
  404. __m128i x,y,z;
  405. y = _mm_aeskeygenassist_si128(a,0x00);
  406. z = _mm_shuffle_epi32(y,0xaa);
  407. y = _mm_slli_si128(b,0x04);
  408. x = _mm_xor_si128(b,y);
  409. y = _mm_slli_si128(y,0x04);
  410. x = _mm_xor_si128(x,y);
  411. y = _mm_slli_si128(y,0x04);
  412. x = _mm_xor_si128(x,y);
  413. x = _mm_xor_si128(x,z);
  414. return x;
  415. }
  416. ZT_ALWAYS_INLINE void _init_aesni(const uint8_t key[32])
  417. {
  418. __m128i t1,t2;
  419. _k.ni.k[0] = t1 = _mm_loadu_si128((const __m128i *)key);
  420. _k.ni.k[1] = t2 = _mm_loadu_si128((const __m128i *)(key+16));
  421. _k.ni.k[2] = t1 = _init256_1_aesni(t1,_mm_aeskeygenassist_si128(t2,0x01));
  422. _k.ni.k[3] = t2 = _init256_2_aesni(t1,t2);
  423. _k.ni.k[4] = t1 = _init256_1_aesni(t1,_mm_aeskeygenassist_si128(t2,0x02));
  424. _k.ni.k[5] = t2 = _init256_2_aesni(t1,t2);
  425. _k.ni.k[6] = t1 = _init256_1_aesni(t1,_mm_aeskeygenassist_si128(t2,0x04));
  426. _k.ni.k[7] = t2 = _init256_2_aesni(t1,t2);
  427. _k.ni.k[8] = t1 = _init256_1_aesni(t1,_mm_aeskeygenassist_si128(t2,0x08));
  428. _k.ni.k[9] = t2 = _init256_2_aesni(t1,t2);
  429. _k.ni.k[10] = t1 = _init256_1_aesni(t1,_mm_aeskeygenassist_si128(t2,0x10));
  430. _k.ni.k[11] = t2 = _init256_2_aesni(t1,t2);
  431. _k.ni.k[12] = t1 = _init256_1_aesni(t1,_mm_aeskeygenassist_si128(t2,0x20));
  432. _k.ni.k[13] = t2 = _init256_2_aesni(t1,t2);
  433. _k.ni.k[14] = _init256_1_aesni(t1,_mm_aeskeygenassist_si128(t2,0x40));
  434. __m128i h = _mm_xor_si128(_mm_setzero_si128(),_k.ni.k[0]);
  435. h = _mm_aesenc_si128(h,_k.ni.k[1]);
  436. h = _mm_aesenc_si128(h,_k.ni.k[2]);
  437. h = _mm_aesenc_si128(h,_k.ni.k[3]);
  438. h = _mm_aesenc_si128(h,_k.ni.k[4]);
  439. h = _mm_aesenc_si128(h,_k.ni.k[5]);
  440. h = _mm_aesenc_si128(h,_k.ni.k[6]);
  441. h = _mm_aesenc_si128(h,_k.ni.k[7]);
  442. h = _mm_aesenc_si128(h,_k.ni.k[8]);
  443. h = _mm_aesenc_si128(h,_k.ni.k[9]);
  444. h = _mm_aesenc_si128(h,_k.ni.k[10]);
  445. h = _mm_aesenc_si128(h,_k.ni.k[11]);
  446. h = _mm_aesenc_si128(h,_k.ni.k[12]);
  447. h = _mm_aesenc_si128(h,_k.ni.k[13]);
  448. h = _mm_aesenclast_si128(h,_k.ni.k[14]);
  449. const __m128i shuf = _mm_set_epi8(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
  450. __m128i hswap = _mm_shuffle_epi8(h,shuf);
  451. __m128i hh = _mult_block_aesni(shuf,hswap,h);
  452. __m128i hhh = _mult_block_aesni(shuf,hswap,hh);
  453. __m128i hhhh = _mult_block_aesni(shuf,hswap,hhh);
  454. _k.ni.h = hswap;
  455. _k.ni.hh = _mm_shuffle_epi8(hh,shuf);
  456. _k.ni.hhh = _mm_shuffle_epi8(hhh,shuf);
  457. _k.ni.hhhh = _mm_shuffle_epi8(hhhh,shuf);
  458. }
  459. ZT_ALWAYS_INLINE void _encrypt_aesni(const void *in,void *out) const
  460. {
  461. __m128i tmp;
  462. tmp = _mm_loadu_si128((const __m128i *)in);
  463. tmp = _mm_xor_si128(tmp,_k.ni.k[0]);
  464. tmp = _mm_aesenc_si128(tmp,_k.ni.k[1]);
  465. tmp = _mm_aesenc_si128(tmp,_k.ni.k[2]);
  466. tmp = _mm_aesenc_si128(tmp,_k.ni.k[3]);
  467. tmp = _mm_aesenc_si128(tmp,_k.ni.k[4]);
  468. tmp = _mm_aesenc_si128(tmp,_k.ni.k[5]);
  469. tmp = _mm_aesenc_si128(tmp,_k.ni.k[6]);
  470. tmp = _mm_aesenc_si128(tmp,_k.ni.k[7]);
  471. tmp = _mm_aesenc_si128(tmp,_k.ni.k[8]);
  472. tmp = _mm_aesenc_si128(tmp,_k.ni.k[9]);
  473. tmp = _mm_aesenc_si128(tmp,_k.ni.k[10]);
  474. tmp = _mm_aesenc_si128(tmp,_k.ni.k[11]);
  475. tmp = _mm_aesenc_si128(tmp,_k.ni.k[12]);
  476. tmp = _mm_aesenc_si128(tmp,_k.ni.k[13]);
  477. _mm_storeu_si128((__m128i *)out,_mm_aesenclast_si128(tmp,_k.ni.k[14]));
  478. }
  479. static ZT_ALWAYS_INLINE __m128i _mult_block_aesni(__m128i shuf,__m128i h,__m128i y)
  480. {
  481. y = _mm_shuffle_epi8(y,shuf);
  482. __m128i t1 = _mm_clmulepi64_si128(h,y,0x00);
  483. __m128i t2 = _mm_clmulepi64_si128(h,y,0x01);
  484. __m128i t3 = _mm_clmulepi64_si128(h,y,0x10);
  485. __m128i t4 = _mm_clmulepi64_si128(h,y,0x11);
  486. t2 = _mm_xor_si128(t2,t3);
  487. t3 = _mm_slli_si128(t2,8);
  488. t2 = _mm_srli_si128(t2,8);
  489. t1 = _mm_xor_si128(t1,t3);
  490. t4 = _mm_xor_si128(t4,t2);
  491. __m128i t5 = _mm_srli_epi32(t1,31);
  492. t1 = _mm_slli_epi32(t1,1);
  493. __m128i t6 = _mm_srli_epi32(t4,31);
  494. t4 = _mm_slli_epi32(t4,1);
  495. t3 = _mm_srli_si128(t5,12);
  496. t6 = _mm_slli_si128(t6,4);
  497. t5 = _mm_slli_si128(t5,4);
  498. t1 = _mm_or_si128(t1,t5);
  499. t4 = _mm_or_si128(t4,t6);
  500. t4 = _mm_or_si128(t4,t3);
  501. t5 = _mm_slli_epi32(t1,31);
  502. t6 = _mm_slli_epi32(t1,30);
  503. t3 = _mm_slli_epi32(t1,25);
  504. t5 = _mm_xor_si128(t5,t6);
  505. t5 = _mm_xor_si128(t5,t3);
  506. t6 = _mm_srli_si128(t5,4);
  507. t4 = _mm_xor_si128(t4,t6);
  508. t5 = _mm_slli_si128(t5,12);
  509. t1 = _mm_xor_si128(t1,t5);
  510. t4 = _mm_xor_si128(t4,t1);
  511. t5 = _mm_srli_epi32(t1,1);
  512. t2 = _mm_srli_epi32(t1,2);
  513. t3 = _mm_srli_epi32(t1,7);
  514. t4 = _mm_xor_si128(t4,t2);
  515. t4 = _mm_xor_si128(t4,t3);
  516. t4 = _mm_xor_si128(t4,t5);
  517. return _mm_shuffle_epi8(t4,shuf);
  518. }
  519. static ZT_ALWAYS_INLINE __m128i _ghash_aesni(__m128i shuf,__m128i h,__m128i y,__m128i x) { return _mult_block_aesni(shuf,h,_mm_xor_si128(y,x)); }
  520. ZT_ALWAYS_INLINE void _gmac_aesni(const uint8_t iv[12],const uint8_t *in,const unsigned int len,uint8_t out[16]) const
  521. {
  522. const __m128i *const ab = (const __m128i *)in;
  523. const unsigned int blocks = len / 16;
  524. const unsigned int pblocks = blocks - (blocks % 4);
  525. const unsigned int rem = len % 16;
  526. const __m128i shuf = _mm_set_epi8(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
  527. __m128i y = _mm_setzero_si128();
  528. unsigned int i = 0;
  529. for (;i<pblocks;i+=4) {
  530. __m128i d1 = _mm_shuffle_epi8(_mm_xor_si128(y,_mm_loadu_si128(ab + i + 0)),shuf);
  531. __m128i d2 = _mm_shuffle_epi8(_mm_loadu_si128(ab + i + 1),shuf);
  532. __m128i d3 = _mm_shuffle_epi8(_mm_loadu_si128(ab + i + 2),shuf);
  533. __m128i d4 = _mm_shuffle_epi8(_mm_loadu_si128(ab + i + 3),shuf);
  534. _mm_prefetch(ab + i + 4,_MM_HINT_T0);
  535. __m128i t0 = _mm_clmulepi64_si128(_k.ni.hhhh,d1,0x00);
  536. __m128i t1 = _mm_clmulepi64_si128(_k.ni.hhh,d2,0x00);
  537. __m128i t2 = _mm_clmulepi64_si128(_k.ni.hh,d3,0x00);
  538. __m128i t3 = _mm_clmulepi64_si128(_k.ni.h,d4,0x00);
  539. __m128i t8 = _mm_xor_si128(t0,t1);
  540. t8 = _mm_xor_si128(t8,t2);
  541. t8 = _mm_xor_si128(t8,t3);
  542. __m128i t4 = _mm_clmulepi64_si128(_k.ni.hhhh,d1,0x11);
  543. __m128i t5 = _mm_clmulepi64_si128(_k.ni.hhh,d2,0x11);
  544. __m128i t6 = _mm_clmulepi64_si128(_k.ni.hh,d3,0x11);
  545. __m128i t7 = _mm_clmulepi64_si128(_k.ni.h,d4,0x11);
  546. __m128i t9 = _mm_xor_si128(t4,t5);
  547. t9 = _mm_xor_si128(t9,t6);
  548. t9 = _mm_xor_si128(t9,t7);
  549. t0 = _mm_shuffle_epi32(_k.ni.hhhh,78);
  550. t4 = _mm_shuffle_epi32(d1,78);
  551. t0 = _mm_xor_si128(t0,_k.ni.hhhh);
  552. t4 = _mm_xor_si128(t4,d1);
  553. t1 = _mm_shuffle_epi32(_k.ni.hhh,78);
  554. t5 = _mm_shuffle_epi32(d2,78);
  555. t1 = _mm_xor_si128(t1,_k.ni.hhh);
  556. t5 = _mm_xor_si128(t5,d2);
  557. t2 = _mm_shuffle_epi32(_k.ni.hh,78);
  558. t6 = _mm_shuffle_epi32(d3,78);
  559. t2 = _mm_xor_si128(t2,_k.ni.hh);
  560. t6 = _mm_xor_si128(t6,d3);
  561. t3 = _mm_shuffle_epi32(_k.ni.h,78);
  562. t7 = _mm_shuffle_epi32(d4,78);
  563. t3 = _mm_xor_si128(t3,_k.ni.h);
  564. t7 = _mm_xor_si128(t7,d4);
  565. t0 = _mm_clmulepi64_si128(t0,t4,0x00);
  566. t1 = _mm_clmulepi64_si128(t1,t5,0x00);
  567. t2 = _mm_clmulepi64_si128(t2,t6,0x00);
  568. t3 = _mm_clmulepi64_si128(t3,t7,0x00);
  569. t0 = _mm_xor_si128(t0,t8);
  570. t0 = _mm_xor_si128(t0,t9);
  571. t0 = _mm_xor_si128(t1,t0);
  572. t0 = _mm_xor_si128(t2,t0);
  573. t0 = _mm_xor_si128(t3,t0);
  574. t4 = _mm_slli_si128(t0,8);
  575. t0 = _mm_srli_si128(t0,8);
  576. t3 = _mm_xor_si128(t4,t8);
  577. t6 = _mm_xor_si128(t0,t9);
  578. t7 = _mm_srli_epi32(t3,31);
  579. t8 = _mm_srli_epi32(t6,31);
  580. t3 = _mm_slli_epi32(t3,1);
  581. t6 = _mm_slli_epi32(t6,1);
  582. t9 = _mm_srli_si128(t7,12);
  583. t8 = _mm_slli_si128(t8,4);
  584. t7 = _mm_slli_si128(t7,4);
  585. t3 = _mm_or_si128(t3,t7);
  586. t6 = _mm_or_si128(t6,t8);
  587. t6 = _mm_or_si128(t6,t9);
  588. t7 = _mm_slli_epi32(t3,31);
  589. t8 = _mm_slli_epi32(t3,30);
  590. t9 = _mm_slli_epi32(t3,25);
  591. t7 = _mm_xor_si128(t7,t8);
  592. t7 = _mm_xor_si128(t7,t9);
  593. t8 = _mm_srli_si128(t7,4);
  594. t7 = _mm_slli_si128(t7,12);
  595. t3 = _mm_xor_si128(t3,t7);
  596. t2 = _mm_srli_epi32(t3,1);
  597. t4 = _mm_srli_epi32(t3,2);
  598. t5 = _mm_srli_epi32(t3,7);
  599. t2 = _mm_xor_si128(t2,t4);
  600. t2 = _mm_xor_si128(t2,t5);
  601. t2 = _mm_xor_si128(t2,t8);
  602. t3 = _mm_xor_si128(t3,t2);
  603. t6 = _mm_xor_si128(t6,t3);
  604. y = _mm_shuffle_epi8(t6,shuf);
  605. }
  606. for (;i<blocks;++i)
  607. y = _ghash_aesni(shuf,_k.ni.h,y,_mm_loadu_si128(ab + i));
  608. if (rem) {
  609. __m128i last = _mm_setzero_si128();
  610. memcpy(&last,ab + blocks,rem);
  611. y = _ghash_aesni(shuf,_k.ni.h,y,last);
  612. }
  613. y = _ghash_aesni(shuf,_k.ni.h,y,_mm_set_epi64((__m64)0LL,(__m64)Utils::hton((uint64_t)len * (uint64_t)8)));
  614. __m128i t = _mm_xor_si128(_mm_set_epi32(0x01000000,(int)*((const uint32_t *)(iv+8)),(int)*((const uint32_t *)(iv+4)),(int)*((const uint32_t *)(iv))),_k.ni.k[0]);
  615. t = _mm_aesenc_si128(t,_k.ni.k[1]);
  616. t = _mm_aesenc_si128(t,_k.ni.k[2]);
  617. t = _mm_aesenc_si128(t,_k.ni.k[3]);
  618. t = _mm_aesenc_si128(t,_k.ni.k[4]);
  619. t = _mm_aesenc_si128(t,_k.ni.k[5]);
  620. t = _mm_aesenc_si128(t,_k.ni.k[6]);
  621. t = _mm_aesenc_si128(t,_k.ni.k[7]);
  622. t = _mm_aesenc_si128(t,_k.ni.k[8]);
  623. t = _mm_aesenc_si128(t,_k.ni.k[9]);
  624. t = _mm_aesenc_si128(t,_k.ni.k[10]);
  625. t = _mm_aesenc_si128(t,_k.ni.k[11]);
  626. t = _mm_aesenc_si128(t,_k.ni.k[12]);
  627. t = _mm_aesenc_si128(t,_k.ni.k[13]);
  628. t = _mm_aesenclast_si128(t,_k.ni.k[14]);
  629. _mm_storeu_si128((__m128i *)out,_mm_xor_si128(y,t));
  630. }
  631. #endif /* ZT_AES_AESNI ******************************************************/
  632. };
  633. } // namespace ZeroTier
  634. #endif