AES.hpp 23 KB

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