AES.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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() {}
  34. ZT_ALWAYS_INLINE AES(const uint8_t key[32]) { 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])
  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
  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. /**
  66. * Compute GMAC-AES256 (GCM without ciphertext)
  67. *
  68. * @param iv 96-bit IV
  69. * @param in Input data
  70. * @param len Length of input
  71. * @param out 128-bit authorization tag from GMAC
  72. */
  73. ZT_ALWAYS_INLINE void gmac(const uint8_t iv[12],const void *in,const unsigned int len,uint8_t out[16]) const
  74. {
  75. #ifdef ZT_AES_AESNI
  76. if (likely(Utils::CPUID.aes)) {
  77. _gmac_aesni(iv,(const uint8_t *)in,len,out);
  78. return;
  79. }
  80. #endif
  81. _gmacSW(iv,(const uint8_t *)in,len,out);
  82. }
  83. /**
  84. * Encrypt or decrypt (they're the same) using AES256-CTR
  85. *
  86. * The counter here is a 128-bit big-endian that starts at the IV. The code only
  87. * increments the least significant 64 bits, making it only safe to use for a
  88. * maximum of 2^64-1 bytes (much larger than we ever do).
  89. *
  90. * @param iv 128-bit CTR IV
  91. * @param in Input plaintext or ciphertext
  92. * @param len Length of input
  93. * @param out Output plaintext or ciphertext
  94. */
  95. ZT_ALWAYS_INLINE void ctr(const uint8_t iv[16],const void *in,unsigned int len,void *out) const
  96. {
  97. #ifdef ZT_AES_AESNI
  98. if (likely(Utils::CPUID.aes)) {
  99. _ctr_aesni(_k.ni.k,iv,(const uint8_t *)in,len,(uint8_t *)out);
  100. return;
  101. }
  102. #endif
  103. _ctrSW(iv,in,len,out);
  104. }
  105. /**
  106. * Perform AES-GMAC-SIV encryption
  107. *
  108. * This is basically AES-CMAC-SIV but with GMAC in place of CMAC after
  109. * GMAC is run through AES as a keyed hash to make it behave like a
  110. * proper PRF.
  111. *
  112. * See: https://github.com/miscreant/meta/wiki/AES-SIV
  113. *
  114. * The advantage is that this can be described in terms of FIPS and NSA
  115. * ceritifable primitives that are present in FIPS-compliant crypto
  116. * modules.
  117. *
  118. * The extra AES-ECB (keyed hash) encryption of the AES-CTR IV prior
  119. * to use makes the IV itself a secret. This is not strictly necessary
  120. * but comes at little cost.
  121. *
  122. * This code is ZeroTier-specific in a few ways, like the way the IV
  123. * is specified, but would not be hard to generalize.
  124. *
  125. * @param k1 GMAC key
  126. * @param k2 GMAC auth tag keyed hash key
  127. * @param k3 CTR IV keyed hash key
  128. * @param k4 AES-CTR key
  129. * @param iv 64-bit packet IV
  130. * @param pc Packet characteristics byte
  131. * @param in Message plaintext
  132. * @param len Length of plaintext
  133. * @param out Output buffer to receive ciphertext
  134. * @param tag Output buffer to receive 64-bit authentication tag
  135. */
  136. static 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])
  137. {
  138. #ifdef __GNUC__
  139. uint8_t __attribute__ ((aligned (16))) miv[12];
  140. uint8_t __attribute__ ((aligned (16))) ctrIv[16];
  141. #else
  142. uint8_t miv[12];
  143. uint8_t ctrIv[16];
  144. #endif
  145. // GMAC IV is 64-bit packet IV followed by other packet attributes to extend to 96 bits
  146. #ifndef __GNUC__
  147. for(unsigned int i=0;i<8;++i) miv[i] = iv[i];
  148. #else
  149. *((uint64_t *)miv) = *((const uint64_t *)iv);
  150. #endif
  151. miv[8] = pc;
  152. miv[9] = (uint8_t)(len >> 16);
  153. miv[10] = (uint8_t)(len >> 8);
  154. miv[11] = (uint8_t)len;
  155. // Compute auth tag: AES-ECB[k2](GMAC[k1](miv,plaintext))[0:8]
  156. k1.gmac(miv,in,len,ctrIv);
  157. k2.encrypt(ctrIv,ctrIv); // ECB mode encrypt step is because GMAC is not a PRF
  158. #ifdef ZT_NO_TYPE_PUNNING
  159. for(unsigned int i=0;i<8;++i) tag[i] = ctrIv[i];
  160. #else
  161. *((uint64_t *)tag) = *((uint64_t *)ctrIv);
  162. #endif
  163. // Create synthetic CTR IV: AES-ECB[k3](TAG | MIV[0:4] | (MIV[4:8] XOR MIV[8:12]))
  164. #ifndef __GNUC__
  165. for(unsigned int i=0;i<4;++i) ctrIv[i+8] = miv[i];
  166. for(unsigned int i=4;i<8;++i) ctrIv[i+8] = miv[i] ^ miv[i+4];
  167. #else
  168. ((uint32_t *)ctrIv)[2] = ((const uint32_t *)miv)[0];
  169. ((uint32_t *)ctrIv)[3] = ((const uint32_t *)miv)[1] ^ ((const uint32_t *)miv)[2];
  170. #endif
  171. k3.encrypt(ctrIv,ctrIv);
  172. // Encrypt with AES[k4]-CTR
  173. k4.ctr(ctrIv,in,len,out);
  174. }
  175. /**
  176. * Decrypt a message encrypted with AES-GMAC-SIV and check its authenticity
  177. *
  178. * @param k1 GMAC key
  179. * @param k2 GMAC auth tag keyed hash key
  180. * @param k3 CTR IV keyed hash key
  181. * @param k4 AES-CTR key
  182. * @param iv 64-bit message IV
  183. * @param pc Packet characteristics byte
  184. * @param in Message ciphertext
  185. * @param len Length of ciphertext
  186. * @param out Output buffer to receive plaintext
  187. * @param tag Authentication tag supplied with message
  188. * @return True if authentication tags match and message appears authentic
  189. */
  190. static 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])
  191. {
  192. #ifdef __GNUC__
  193. uint8_t __attribute__ ((aligned (16))) miv[12];
  194. uint8_t __attribute__ ((aligned (16))) ctrIv[16];
  195. uint8_t __attribute__ ((aligned (16))) gmacOut[16];
  196. #else
  197. uint8_t miv[12];
  198. uint8_t ctrIv[16];
  199. uint8_t gmacOut[16];
  200. #endif
  201. // Extend packet IV to 96-bit message IV using direction byte and message length
  202. #ifdef ZT_NO_TYPE_PUNNING
  203. for(unsigned int i=0;i<8;++i) miv[i] = iv[i];
  204. #else
  205. *((uint64_t *)miv) = *((const uint64_t *)iv);
  206. #endif
  207. miv[8] = pc;
  208. miv[9] = (uint8_t)(len >> 16);
  209. miv[10] = (uint8_t)(len >> 8);
  210. miv[11] = (uint8_t)len;
  211. // Recover synthetic and secret CTR IV from auth tag and packet IV
  212. #ifndef __GNUC__
  213. for(unsigned int i=0;i<8;++i) ctrIv[i] = tag[i];
  214. for(unsigned int i=0;i<4;++i) ctrIv[i+8] = miv[i];
  215. for(unsigned int i=4;i<8;++i) ctrIv[i+8] = miv[i] ^ miv[i+4];
  216. #else
  217. *((uint64_t *)ctrIv) = *((const uint64_t *)tag);
  218. ((uint32_t *)ctrIv)[2] = ((const uint32_t *)miv)[0];
  219. ((uint32_t *)ctrIv)[3] = ((const uint32_t *)miv)[1] ^ ((const uint32_t *)miv)[2];
  220. #endif
  221. k3.encrypt(ctrIv,ctrIv);
  222. // Decrypt with AES[k4]-CTR
  223. k4.ctr(ctrIv,in,len,out);
  224. // Compute AES[k2](GMAC[k1](iv,plaintext))
  225. k1.gmac(miv,out,len,gmacOut);
  226. k2.encrypt(gmacOut,gmacOut);
  227. // Check that packet's auth tag matches first 64 bits of AES(GMAC)
  228. #ifdef ZT_NO_TYPE_PUNNING
  229. return Utils::secureEq(gmacOut,tag,8);
  230. #else
  231. return (*((const uint64_t *)gmacOut) == *((const uint64_t *)tag));
  232. #endif
  233. }
  234. /**
  235. * Use KBKDF with HMAC-SHA-384 to derive four sub-keys for AES-GMAC-SIV from a single master key
  236. *
  237. * See section 5.1 at https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-108.pdf
  238. *
  239. * @param masterKey Master 256-bit key
  240. * @param k1 GMAC key
  241. * @param k2 GMAC auth tag keyed hash key
  242. * @param k3 CTR IV keyed hash key
  243. * @param k4 AES-CTR key
  244. */
  245. static inline void initGmacCtrKeys(const uint8_t masterKey[32],AES &k1,AES &k2,AES &k3,AES &k4)
  246. {
  247. uint8_t k[32];
  248. KBKDFHMACSHA384(masterKey,ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K1,0,0,k);
  249. k1.init(k);
  250. KBKDFHMACSHA384(masterKey,ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K2,0,0,k);
  251. k2.init(k);
  252. KBKDFHMACSHA384(masterKey,ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K3,0,0,k);
  253. k3.init(k);
  254. KBKDFHMACSHA384(masterKey,ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K4,0,0,k);
  255. k4.init(k);
  256. }
  257. private:
  258. static const uint32_t Te0[256];
  259. static const uint32_t Te1[256];
  260. static const uint32_t Te2[256];
  261. static const uint32_t Te3[256];
  262. static const uint32_t rcon[10];
  263. void _initSW(const uint8_t key[32]);
  264. void _encryptSW(const uint8_t in[16],uint8_t out[16]) const;
  265. void _ctrSW(const uint8_t iv[16],const void *in,unsigned int len,void *out) const;
  266. void _gmacSW(const uint8_t iv[12],const uint8_t *in,unsigned int len,uint8_t out[16]) const;
  267. /**************************************************************************/
  268. union {
  269. #ifdef ZT_AES_ARMNEON
  270. // ARM NEON key and GMAC parameters
  271. struct {
  272. uint32x4_t k[15];
  273. } neon;
  274. #endif
  275. #ifdef ZT_AES_AESNI
  276. // AES-NI key and GMAC parameters
  277. struct {
  278. __m128i k[15];
  279. __m128i h,hh,hhh,hhhh;
  280. } ni;
  281. #endif
  282. // Software mode key and GMAC parameters
  283. struct {
  284. uint64_t h[2];
  285. uint32_t ek[60];
  286. } sw;
  287. } _k;
  288. /**************************************************************************/
  289. #ifdef ZT_AES_ARMNEON /******************************************************/
  290. static inline void _aes_256_expAssist_armneon(uint32x4_t prev1,uint32x4_t prev2,uint32_t rcon,uint32x4_t *e1,uint32x4_t *e2)
  291. {
  292. uint32_t round1[4], round2[4], prv1[4], prv2[4];
  293. vst1q_u32(prv1, prev1);
  294. vst1q_u32(prv2, prev2);
  295. round1[0] = sub_word(rot_word(prv2[3])) ^ rcon ^ prv1[0];
  296. round1[1] = sub_word(rot_word(round1[0])) ^ rcon ^ prv1[1];
  297. round1[2] = sub_word(rot_word(round1[1])) ^ rcon ^ prv1[2];
  298. round1[3] = sub_word(rot_word(round1[2])) ^ rcon ^ prv1[3];
  299. round2[0] = sub_word(rot_word(round1[3])) ^ rcon ^ prv2[0];
  300. round2[1] = sub_word(rot_word(round2[0])) ^ rcon ^ prv2[1];
  301. round2[2] = sub_word(rot_word(round2[1])) ^ rcon ^ prv2[2];
  302. round2[3] = sub_word(rot_word(round2[2])) ^ rcon ^ prv2[3];
  303. *e1 = vld1q_u3(round1);
  304. *e2 = vld1q_u3(round2);
  305. //uint32x4_t expansion[2] = {vld1q_u3(round1), vld1q_u3(round2)};
  306. //return expansion;
  307. }
  308. inline void _init_armneon(uint8x16_t encKey)
  309. {
  310. uint32x4_t *schedule = _k.neon.k;
  311. uint32x4_t e1,e2;
  312. (*schedule)[0] = vld1q_u32(encKey);
  313. (*schedule)[1] = vld1q_u32(encKey + 16);
  314. _aes_256_expAssist_armneon((*schedule)[0],(*schedule)[1],0x01,&e1,&e2);
  315. (*schedule)[2] = e1; (*schedule)[3] = e2;
  316. _aes_256_expAssist_armneon((*schedule)[2],(*schedule)[3],0x01,&e1,&e2);
  317. (*schedule)[4] = e1; (*schedule)[5] = e2;
  318. _aes_256_expAssist_armneon((*schedule)[4],(*schedule)[5],0x01,&e1,&e2);
  319. (*schedule)[6] = e1; (*schedule)[7] = e2;
  320. _aes_256_expAssist_armneon((*schedule)[6],(*schedule)[7],0x01,&e1,&e2);
  321. (*schedule)[8] = e1; (*schedule)[9] = e2;
  322. _aes_256_expAssist_armneon((*schedule)[8],(*schedule)[9],0x01,&e1,&e2);
  323. (*schedule)[10] = e1; (*schedule)[11] = e2;
  324. _aes_256_expAssist_armneon((*schedule)[10],(*schedule)[11],0x01,&e1,&e2);
  325. (*schedule)[12] = e1; (*schedule)[13] = e2;
  326. _aes_256_expAssist_armneon((*schedule)[12],(*schedule)[13],0x01,&e1,&e2);
  327. (*schedule)[14] = e1;
  328. /*
  329. doubleRound = _aes_256_expAssist_armneon((*schedule)[0], (*schedule)[1], 0x01);
  330. (*schedule)[2] = doubleRound[0];
  331. (*schedule)[3] = doubleRound[1];
  332. doubleRound = _aes_256_expAssist_armneon((*schedule)[2], (*schedule)[3], 0x02);
  333. (*schedule)[4] = doubleRound[0];
  334. (*schedule)[5] = doubleRound[1];
  335. doubleRound = _aes_256_expAssist_armneon((*schedule)[4], (*schedule)[5], 0x04);
  336. (*schedule)[6] = doubleRound[0];
  337. (*schedule)[7] = doubleRound[1];
  338. doubleRound = _aes_256_expAssist_armneon((*schedule)[6], (*schedule)[7], 0x08);
  339. (*schedule)[8] = doubleRound[0];
  340. (*schedule)[9] = doubleRound[1];
  341. doubleRound = _aes_256_expAssist_armneon((*schedule)[8], (*schedule)[9], 0x10);
  342. (*schedule)[10] = doubleRound[0];
  343. (*schedule)[11] = doubleRound[1];
  344. doubleRound = _aes_256_expAssist_armneon((*schedule)[10], (*schedule)[11], 0x20);
  345. (*schedule)[12] = doubleRound[0];
  346. (*schedule)[13] = doubleRound[1];
  347. doubleRound = _aes_256_expAssist_armneon((*schedule)[12], (*schedule)[13], 0x40);
  348. (*schedule)[14] = doubleRound[0];
  349. */
  350. }
  351. inline void _encrypt_armneon(uint8x16_t *data) const
  352. {
  353. *data = veorq_u8(*data, _k.neon.k[0]);
  354. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[1]));
  355. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[2]));
  356. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[3]));
  357. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[4]));
  358. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[5]));
  359. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[6]));
  360. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[7]));
  361. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[8]));
  362. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[9]));
  363. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[10]));
  364. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[11]));
  365. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[12]));
  366. *data = vaesmcq_u8(vaeseq_u8(*data, (uint8x16_t)_k.neon.k[13]));
  367. *data = vaeseq_u8(*data, _k.neon.k[14]);
  368. }
  369. #endif /*********************************************************************/
  370. #ifdef ZT_AES_AESNI /********************************************************/
  371. void _init_aesni(const uint8_t key[32]);
  372. ZT_ALWAYS_INLINE void _encrypt_aesni(const void *in,void *out) const
  373. {
  374. __m128i tmp;
  375. tmp = _mm_loadu_si128((const __m128i *)in);
  376. tmp = _mm_xor_si128(tmp,_k.ni.k[0]);
  377. tmp = _mm_aesenc_si128(tmp,_k.ni.k[1]);
  378. tmp = _mm_aesenc_si128(tmp,_k.ni.k[2]);
  379. tmp = _mm_aesenc_si128(tmp,_k.ni.k[3]);
  380. tmp = _mm_aesenc_si128(tmp,_k.ni.k[4]);
  381. tmp = _mm_aesenc_si128(tmp,_k.ni.k[5]);
  382. tmp = _mm_aesenc_si128(tmp,_k.ni.k[6]);
  383. tmp = _mm_aesenc_si128(tmp,_k.ni.k[7]);
  384. tmp = _mm_aesenc_si128(tmp,_k.ni.k[8]);
  385. tmp = _mm_aesenc_si128(tmp,_k.ni.k[9]);
  386. tmp = _mm_aesenc_si128(tmp,_k.ni.k[10]);
  387. tmp = _mm_aesenc_si128(tmp,_k.ni.k[11]);
  388. tmp = _mm_aesenc_si128(tmp,_k.ni.k[12]);
  389. tmp = _mm_aesenc_si128(tmp,_k.ni.k[13]);
  390. _mm_storeu_si128((__m128i *)out,_mm_aesenclast_si128(tmp,_k.ni.k[14]));
  391. }
  392. void _gmac_aesni(const uint8_t iv[12],const uint8_t *in,const unsigned int len,uint8_t out[16]) const;
  393. static void _ctr_aesni(const __m128i key[14],const uint8_t iv[16],const uint8_t *in,unsigned int len,uint8_t *out);
  394. #endif /* ZT_AES_AESNI ******************************************************/
  395. };
  396. } // namespace ZeroTier
  397. #endif