aesni.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * AES-NI support functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * [AES-WP] https://www.intel.com/content/www/us/en/developer/articles/tool/intel-advanced-encryption-standard-aes-instructions-set.html
  9. * [CLMUL-WP] https://www.intel.com/content/www/us/en/develop/download/intel-carry-less-multiplication-instruction-and-its-usage-for-computing-the-gcm-mode.html
  10. */
  11. #include "common.h"
  12. #if defined(MBEDTLS_AESNI_C)
  13. #include "mbedtls/aesni.h"
  14. #include <string.h>
  15. /* *INDENT-OFF* */
  16. #ifndef asm
  17. #define asm __asm
  18. #endif
  19. /* *INDENT-ON* */
  20. #if defined(MBEDTLS_AESNI_HAVE_CODE)
  21. #if MBEDTLS_AESNI_HAVE_CODE == 2
  22. #if defined(__GNUC__)
  23. #include <cpuid.h>
  24. #elif defined(_MSC_VER)
  25. #include <intrin.h>
  26. #else
  27. #error "`__cpuid` required by MBEDTLS_AESNI_C is not supported by the compiler"
  28. #endif
  29. #include <immintrin.h>
  30. #endif
  31. /*
  32. * AES-NI support detection routine
  33. */
  34. int mbedtls_aesni_has_support(unsigned int what)
  35. {
  36. static int done = 0;
  37. static unsigned int c = 0;
  38. if (!done) {
  39. #if MBEDTLS_AESNI_HAVE_CODE == 2
  40. static int info[4] = { 0, 0, 0, 0 };
  41. #if defined(_MSC_VER)
  42. __cpuid(info, 1);
  43. #else
  44. __cpuid(1, info[0], info[1], info[2], info[3]);
  45. #endif
  46. c = info[2];
  47. #else /* AESNI using asm */
  48. asm ("movl $1, %%eax \n\t"
  49. "cpuid \n\t"
  50. : "=c" (c)
  51. :
  52. : "eax", "ebx", "edx");
  53. #endif /* MBEDTLS_AESNI_HAVE_CODE */
  54. done = 1;
  55. }
  56. return (c & what) != 0;
  57. }
  58. #if MBEDTLS_AESNI_HAVE_CODE == 2
  59. /*
  60. * AES-NI AES-ECB block en(de)cryption
  61. */
  62. int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
  63. int mode,
  64. const unsigned char input[16],
  65. unsigned char output[16])
  66. {
  67. const __m128i *rk = (const __m128i *) (ctx->rk);
  68. unsigned nr = ctx->nr; // Number of remaining rounds
  69. // Load round key 0
  70. __m128i state;
  71. memcpy(&state, input, 16);
  72. state = _mm_xor_si128(state, rk[0]); // state ^= *rk;
  73. ++rk;
  74. --nr;
  75. if (mode == 0) {
  76. while (nr != 0) {
  77. state = _mm_aesdec_si128(state, *rk);
  78. ++rk;
  79. --nr;
  80. }
  81. state = _mm_aesdeclast_si128(state, *rk);
  82. } else {
  83. while (nr != 0) {
  84. state = _mm_aesenc_si128(state, *rk);
  85. ++rk;
  86. --nr;
  87. }
  88. state = _mm_aesenclast_si128(state, *rk);
  89. }
  90. memcpy(output, &state, 16);
  91. return 0;
  92. }
  93. /*
  94. * GCM multiplication: c = a times b in GF(2^128)
  95. * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
  96. */
  97. static void gcm_clmul(const __m128i aa, const __m128i bb,
  98. __m128i *cc, __m128i *dd)
  99. {
  100. /*
  101. * Caryless multiplication dd:cc = aa * bb
  102. * using [CLMUL-WP] algorithm 1 (p. 12).
  103. */
  104. *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
  105. *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
  106. __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
  107. __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
  108. ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0
  109. ee = ff; // e1+f1:e0+f0
  110. ff = _mm_srli_si128(ff, 8); // 0:e1+f1
  111. ee = _mm_slli_si128(ee, 8); // e0+f0:0
  112. *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1
  113. *cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0
  114. }
  115. static void gcm_shift(__m128i *cc, __m128i *dd)
  116. {
  117. /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
  118. * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
  119. // // *cc = r1:r0
  120. // // *dd = r3:r2
  121. __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
  122. __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
  123. __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
  124. __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
  125. __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63
  126. cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0
  127. dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63
  128. *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1
  129. *dd = _mm_or_si128(_mm_or_si128(dd_lo, dd_hi), xmm5); // r3<<1|r2>>62:r2<<1|r1>>63
  130. }
  131. static __m128i gcm_reduce(__m128i xx)
  132. {
  133. // // xx = x1:x0
  134. /* [CLMUL-WP] Algorithm 5 Step 2 */
  135. __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
  136. __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
  137. __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
  138. __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
  139. return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0
  140. }
  141. static __m128i gcm_mix(__m128i dx)
  142. {
  143. /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
  144. __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
  145. __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
  146. __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
  147. // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
  148. // bits carried from d. Now get those bits back in.
  149. __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
  150. __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
  151. __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
  152. __m128i hh = _mm_srli_si128(_mm_xor_si128(_mm_xor_si128(eh, fh), gh), 8); // 0:missing bits of d
  153. return _mm_xor_si128(_mm_xor_si128(_mm_xor_si128(_mm_xor_si128(ee, ff), gg), hh), dx);
  154. }
  155. void mbedtls_aesni_gcm_mult(unsigned char c[16],
  156. const unsigned char a[16],
  157. const unsigned char b[16])
  158. {
  159. __m128i aa = { 0 }, bb = { 0 }, cc, dd;
  160. /* The inputs are in big-endian order, so byte-reverse them */
  161. for (size_t i = 0; i < 16; i++) {
  162. ((uint8_t *) &aa)[i] = a[15 - i];
  163. ((uint8_t *) &bb)[i] = b[15 - i];
  164. }
  165. gcm_clmul(aa, bb, &cc, &dd);
  166. gcm_shift(&cc, &dd);
  167. /*
  168. * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
  169. * using [CLMUL-WP] algorithm 5 (p. 18).
  170. * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
  171. */
  172. __m128i dx = gcm_reduce(cc);
  173. __m128i xh = gcm_mix(dx);
  174. cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
  175. /* Now byte-reverse the outputs */
  176. for (size_t i = 0; i < 16; i++) {
  177. c[i] = ((uint8_t *) &cc)[15 - i];
  178. }
  179. return;
  180. }
  181. /*
  182. * Compute decryption round keys from encryption round keys
  183. */
  184. void mbedtls_aesni_inverse_key(unsigned char *invkey,
  185. const unsigned char *fwdkey, int nr)
  186. {
  187. __m128i *ik = (__m128i *) invkey;
  188. const __m128i *fk = (const __m128i *) fwdkey + nr;
  189. *ik = *fk;
  190. for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
  191. *ik = _mm_aesimc_si128(*fk);
  192. }
  193. *ik = *fk;
  194. }
  195. /*
  196. * Key expansion, 128-bit case
  197. */
  198. static __m128i aesni_set_rk_128(__m128i state, __m128i xword)
  199. {
  200. /*
  201. * Finish generating the next round key.
  202. *
  203. * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff
  204. * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).
  205. *
  206. * On exit, xword is r7:r6:r5:r4
  207. * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
  208. * and this is returned, to be written to the round key buffer.
  209. */
  210. xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X
  211. xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4
  212. state = _mm_slli_si128(state, 4); // r2:r1:r0:0
  213. xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4
  214. state = _mm_slli_si128(state, 4); // r1:r0:0:0
  215. xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4
  216. state = _mm_slli_si128(state, 4); // r0:0:0:0
  217. state = _mm_xor_si128(xword, state); // r7:r6:r5:r4
  218. return state;
  219. }
  220. static void aesni_setkey_enc_128(unsigned char *rk_bytes,
  221. const unsigned char *key)
  222. {
  223. __m128i *rk = (__m128i *) rk_bytes;
  224. memcpy(&rk[0], key, 16);
  225. rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
  226. rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
  227. rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
  228. rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
  229. rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
  230. rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
  231. rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
  232. rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
  233. rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
  234. rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
  235. }
  236. /*
  237. * Key expansion, 192-bit case
  238. */
  239. static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,
  240. unsigned char *rk)
  241. {
  242. /*
  243. * Finish generating the next 6 quarter-keys.
  244. *
  245. * On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4
  246. * and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON
  247. * (obtained with AESKEYGENASSIST).
  248. *
  249. * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10
  250. * and those are written to the round key buffer.
  251. */
  252. xword = _mm_shuffle_epi32(xword, 0x55); // X:X:X:X
  253. xword = _mm_xor_si128(xword, *state0); // X+r3:X+r2:X+r1:X+r0
  254. *state0 = _mm_slli_si128(*state0, 4); // r2:r1:r0:0
  255. xword = _mm_xor_si128(xword, *state0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
  256. *state0 = _mm_slli_si128(*state0, 4); // r1:r0:0:0
  257. xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
  258. *state0 = _mm_slli_si128(*state0, 4); // r0:0:0:0
  259. xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
  260. *state0 = xword; // = r9:r8:r7:r6
  261. xword = _mm_shuffle_epi32(xword, 0xff); // r9:r9:r9:r9
  262. xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5:r9+r4
  263. *state1 = _mm_slli_si128(*state1, 4); // stuff:stuff:r4:0
  264. xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5+r4:r9+r4
  265. *state1 = xword; // = stuff:stuff:r11:r10
  266. /* Store state0 and the low half of state1 into rk, which is conceptually
  267. * an array of 24-byte elements. Since 24 is not a multiple of 16,
  268. * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */
  269. memcpy(rk, state0, 16);
  270. memcpy(rk + 16, state1, 8);
  271. }
  272. static void aesni_setkey_enc_192(unsigned char *rk,
  273. const unsigned char *key)
  274. {
  275. /* First round: use original key */
  276. memcpy(rk, key, 24);
  277. /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
  278. __m128i state0 = ((__m128i *) rk)[0];
  279. __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
  280. aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1);
  281. aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2);
  282. aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3);
  283. aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4);
  284. aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5);
  285. aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6);
  286. aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7);
  287. aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8);
  288. }
  289. /*
  290. * Key expansion, 256-bit case
  291. */
  292. static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,
  293. __m128i *rk0, __m128i *rk1)
  294. {
  295. /*
  296. * Finish generating the next two round keys.
  297. *
  298. * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and
  299. * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
  300. * (obtained with AESKEYGENASSIST).
  301. *
  302. * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
  303. */
  304. xword = _mm_shuffle_epi32(xword, 0xff);
  305. xword = _mm_xor_si128(xword, state0);
  306. state0 = _mm_slli_si128(state0, 4);
  307. xword = _mm_xor_si128(xword, state0);
  308. state0 = _mm_slli_si128(state0, 4);
  309. xword = _mm_xor_si128(xword, state0);
  310. state0 = _mm_slli_si128(state0, 4);
  311. state0 = _mm_xor_si128(state0, xword);
  312. *rk0 = state0;
  313. /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )
  314. * and proceed to generate next round key from there */
  315. xword = _mm_aeskeygenassist_si128(state0, 0x00);
  316. xword = _mm_shuffle_epi32(xword, 0xaa);
  317. xword = _mm_xor_si128(xword, state1);
  318. state1 = _mm_slli_si128(state1, 4);
  319. xword = _mm_xor_si128(xword, state1);
  320. state1 = _mm_slli_si128(state1, 4);
  321. xword = _mm_xor_si128(xword, state1);
  322. state1 = _mm_slli_si128(state1, 4);
  323. state1 = _mm_xor_si128(state1, xword);
  324. *rk1 = state1;
  325. }
  326. static void aesni_setkey_enc_256(unsigned char *rk_bytes,
  327. const unsigned char *key)
  328. {
  329. __m128i *rk = (__m128i *) rk_bytes;
  330. memcpy(&rk[0], key, 16);
  331. memcpy(&rk[1], key + 16, 16);
  332. /*
  333. * Main "loop" - Generating one more key than necessary,
  334. * see definition of mbedtls_aes_context.buf
  335. */
  336. aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
  337. aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
  338. aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
  339. aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
  340. aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
  341. aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
  342. aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
  343. }
  344. #else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
  345. #if defined(__has_feature)
  346. #if __has_feature(memory_sanitizer)
  347. #warning \
  348. "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
  349. #endif
  350. #endif
  351. /*
  352. * Binutils needs to be at least 2.19 to support AES-NI instructions.
  353. * Unfortunately, a lot of users have a lower version now (2014-04).
  354. * Emit bytecode directly in order to support "old" version of gas.
  355. *
  356. * Opcodes from the Intel architecture reference manual, vol. 3.
  357. * We always use registers, so we don't need prefixes for memory operands.
  358. * Operand macros are in gas order (src, dst) as opposed to Intel order
  359. * (dst, src) in order to blend better into the surrounding assembly code.
  360. */
  361. #define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
  362. #define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
  363. #define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
  364. #define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
  365. #define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
  366. #define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
  367. #define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
  368. #define xmm0_xmm0 "0xC0"
  369. #define xmm0_xmm1 "0xC8"
  370. #define xmm0_xmm2 "0xD0"
  371. #define xmm0_xmm3 "0xD8"
  372. #define xmm0_xmm4 "0xE0"
  373. #define xmm1_xmm0 "0xC1"
  374. #define xmm1_xmm2 "0xD1"
  375. /*
  376. * AES-NI AES-ECB block en(de)cryption
  377. */
  378. int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
  379. int mode,
  380. const unsigned char input[16],
  381. unsigned char output[16])
  382. {
  383. asm ("movdqu (%3), %%xmm0 \n\t" // load input
  384. "movdqu (%1), %%xmm1 \n\t" // load round key 0
  385. "pxor %%xmm1, %%xmm0 \n\t" // round 0
  386. "add $16, %1 \n\t" // point to next round key
  387. "subl $1, %0 \n\t" // normal rounds = nr - 1
  388. "test %2, %2 \n\t" // mode?
  389. "jz 2f \n\t" // 0 = decrypt
  390. "1: \n\t" // encryption loop
  391. "movdqu (%1), %%xmm1 \n\t" // load round key
  392. AESENC(xmm1_xmm0) // do round
  393. "add $16, %1 \n\t" // point to next round key
  394. "subl $1, %0 \n\t" // loop
  395. "jnz 1b \n\t"
  396. "movdqu (%1), %%xmm1 \n\t" // load round key
  397. AESENCLAST(xmm1_xmm0) // last round
  398. "jmp 3f \n\t"
  399. "2: \n\t" // decryption loop
  400. "movdqu (%1), %%xmm1 \n\t"
  401. AESDEC(xmm1_xmm0) // do round
  402. "add $16, %1 \n\t"
  403. "subl $1, %0 \n\t"
  404. "jnz 2b \n\t"
  405. "movdqu (%1), %%xmm1 \n\t" // load round key
  406. AESDECLAST(xmm1_xmm0) // last round
  407. "3: \n\t"
  408. "movdqu %%xmm0, (%4) \n\t" // export output
  409. :
  410. : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
  411. : "memory", "cc", "xmm0", "xmm1");
  412. return 0;
  413. }
  414. /*
  415. * GCM multiplication: c = a times b in GF(2^128)
  416. * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
  417. */
  418. void mbedtls_aesni_gcm_mult(unsigned char c[16],
  419. const unsigned char a[16],
  420. const unsigned char b[16])
  421. {
  422. unsigned char aa[16], bb[16], cc[16];
  423. size_t i;
  424. /* The inputs are in big-endian order, so byte-reverse them */
  425. for (i = 0; i < 16; i++) {
  426. aa[i] = a[15 - i];
  427. bb[i] = b[15 - i];
  428. }
  429. asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
  430. "movdqu (%1), %%xmm1 \n\t" // b1:b0
  431. /*
  432. * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
  433. * using [CLMUL-WP] algorithm 1 (p. 12).
  434. */
  435. "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
  436. "movdqa %%xmm1, %%xmm3 \n\t" // same
  437. "movdqa %%xmm1, %%xmm4 \n\t" // same
  438. PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
  439. PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
  440. PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
  441. PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
  442. "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
  443. "movdqa %%xmm4, %%xmm3 \n\t" // same
  444. "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
  445. "pslldq $8, %%xmm3 \n\t" // e0+f0:0
  446. "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
  447. "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
  448. /*
  449. * Now shift the result one bit to the left,
  450. * taking advantage of [CLMUL-WP] eq 27 (p. 18)
  451. */
  452. "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
  453. "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
  454. "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
  455. "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
  456. "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
  457. "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
  458. "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
  459. "pslldq $8, %%xmm3 \n\t" // r0>>63:0
  460. "pslldq $8, %%xmm4 \n\t" // r2>>63:0
  461. "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
  462. "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
  463. "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
  464. "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
  465. /*
  466. * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
  467. * using [CLMUL-WP] algorithm 5 (p. 18).
  468. * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
  469. */
  470. /* Step 2 (1) */
  471. "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
  472. "movdqa %%xmm1, %%xmm4 \n\t" // same
  473. "movdqa %%xmm1, %%xmm5 \n\t" // same
  474. "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
  475. "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
  476. "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
  477. /* Step 2 (2) */
  478. "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
  479. "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
  480. "pslldq $8, %%xmm3 \n\t" // a+b+c:0
  481. "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
  482. /* Steps 3 and 4 */
  483. "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
  484. "movdqa %%xmm1,%%xmm4 \n\t" // same
  485. "movdqa %%xmm1,%%xmm5 \n\t" // same
  486. "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
  487. "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
  488. "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
  489. "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
  490. "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
  491. // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
  492. // bits carried from d. Now get those\t bits back in.
  493. "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
  494. "movdqa %%xmm1,%%xmm4 \n\t" // same
  495. "movdqa %%xmm1,%%xmm5 \n\t" // same
  496. "psllq $63, %%xmm3 \n\t" // d<<63:stuff
  497. "psllq $62, %%xmm4 \n\t" // d<<62:stuff
  498. "psllq $57, %%xmm5 \n\t" // d<<57:stuff
  499. "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
  500. "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
  501. "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
  502. "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
  503. "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
  504. "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
  505. "movdqu %%xmm0, (%2) \n\t" // done
  506. :
  507. : "r" (aa), "r" (bb), "r" (cc)
  508. : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
  509. /* Now byte-reverse the outputs */
  510. for (i = 0; i < 16; i++) {
  511. c[i] = cc[15 - i];
  512. }
  513. return;
  514. }
  515. /*
  516. * Compute decryption round keys from encryption round keys
  517. */
  518. void mbedtls_aesni_inverse_key(unsigned char *invkey,
  519. const unsigned char *fwdkey, int nr)
  520. {
  521. unsigned char *ik = invkey;
  522. const unsigned char *fk = fwdkey + 16 * nr;
  523. memcpy(ik, fk, 16);
  524. for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
  525. asm ("movdqu (%0), %%xmm0 \n\t"
  526. AESIMC(xmm0_xmm0)
  527. "movdqu %%xmm0, (%1) \n\t"
  528. :
  529. : "r" (fk), "r" (ik)
  530. : "memory", "xmm0");
  531. }
  532. memcpy(ik, fk, 16);
  533. }
  534. /*
  535. * Key expansion, 128-bit case
  536. */
  537. static void aesni_setkey_enc_128(unsigned char *rk,
  538. const unsigned char *key)
  539. {
  540. asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
  541. "movdqu %%xmm0, (%0) \n\t" // as round key 0
  542. "jmp 2f \n\t" // skip auxiliary routine
  543. /*
  544. * Finish generating the next round key.
  545. *
  546. * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
  547. * with X = rot( sub( r3 ) ) ^ RCON.
  548. *
  549. * On exit, xmm0 is r7:r6:r5:r4
  550. * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
  551. * and those are written to the round key buffer.
  552. */
  553. "1: \n\t"
  554. "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
  555. "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
  556. "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
  557. "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
  558. "pslldq $4, %%xmm0 \n\t" // etc
  559. "pxor %%xmm0, %%xmm1 \n\t"
  560. "pslldq $4, %%xmm0 \n\t"
  561. "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
  562. "add $16, %0 \n\t" // point to next round key
  563. "movdqu %%xmm0, (%0) \n\t" // write it
  564. "ret \n\t"
  565. /* Main "loop" */
  566. "2: \n\t"
  567. AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
  568. AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
  569. AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
  570. AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
  571. AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
  572. AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
  573. AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
  574. AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
  575. AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
  576. AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
  577. :
  578. : "r" (rk), "r" (key)
  579. : "memory", "cc", "0");
  580. }
  581. /*
  582. * Key expansion, 192-bit case
  583. */
  584. static void aesni_setkey_enc_192(unsigned char *rk,
  585. const unsigned char *key)
  586. {
  587. asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
  588. "movdqu %%xmm0, (%0) \n\t"
  589. "add $16, %0 \n\t"
  590. "movq 16(%1), %%xmm1 \n\t"
  591. "movq %%xmm1, (%0) \n\t"
  592. "add $8, %0 \n\t"
  593. "jmp 2f \n\t" // skip auxiliary routine
  594. /*
  595. * Finish generating the next 6 quarter-keys.
  596. *
  597. * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
  598. * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
  599. *
  600. * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
  601. * and those are written to the round key buffer.
  602. */
  603. "1: \n\t"
  604. "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
  605. "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
  606. "pslldq $4, %%xmm0 \n\t" // etc
  607. "pxor %%xmm0, %%xmm2 \n\t"
  608. "pslldq $4, %%xmm0 \n\t"
  609. "pxor %%xmm0, %%xmm2 \n\t"
  610. "pslldq $4, %%xmm0 \n\t"
  611. "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
  612. "movdqu %%xmm0, (%0) \n\t"
  613. "add $16, %0 \n\t"
  614. "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
  615. "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
  616. "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
  617. "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
  618. "movq %%xmm1, (%0) \n\t"
  619. "add $8, %0 \n\t"
  620. "ret \n\t"
  621. "2: \n\t"
  622. AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
  623. AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
  624. AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
  625. AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
  626. AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
  627. AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
  628. AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
  629. AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
  630. :
  631. : "r" (rk), "r" (key)
  632. : "memory", "cc", "0");
  633. }
  634. /*
  635. * Key expansion, 256-bit case
  636. */
  637. static void aesni_setkey_enc_256(unsigned char *rk,
  638. const unsigned char *key)
  639. {
  640. asm ("movdqu (%1), %%xmm0 \n\t"
  641. "movdqu %%xmm0, (%0) \n\t"
  642. "add $16, %0 \n\t"
  643. "movdqu 16(%1), %%xmm1 \n\t"
  644. "movdqu %%xmm1, (%0) \n\t"
  645. "jmp 2f \n\t" // skip auxiliary routine
  646. /*
  647. * Finish generating the next two round keys.
  648. *
  649. * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
  650. * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
  651. *
  652. * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
  653. * and those have been written to the output buffer.
  654. */
  655. "1: \n\t"
  656. "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
  657. "pxor %%xmm0, %%xmm2 \n\t"
  658. "pslldq $4, %%xmm0 \n\t"
  659. "pxor %%xmm0, %%xmm2 \n\t"
  660. "pslldq $4, %%xmm0 \n\t"
  661. "pxor %%xmm0, %%xmm2 \n\t"
  662. "pslldq $4, %%xmm0 \n\t"
  663. "pxor %%xmm2, %%xmm0 \n\t"
  664. "add $16, %0 \n\t"
  665. "movdqu %%xmm0, (%0) \n\t"
  666. /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
  667. * and proceed to generate next round key from there */
  668. AESKEYGENA(xmm0_xmm2, "0x00")
  669. "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
  670. "pxor %%xmm1, %%xmm2 \n\t"
  671. "pslldq $4, %%xmm1 \n\t"
  672. "pxor %%xmm1, %%xmm2 \n\t"
  673. "pslldq $4, %%xmm1 \n\t"
  674. "pxor %%xmm1, %%xmm2 \n\t"
  675. "pslldq $4, %%xmm1 \n\t"
  676. "pxor %%xmm2, %%xmm1 \n\t"
  677. "add $16, %0 \n\t"
  678. "movdqu %%xmm1, (%0) \n\t"
  679. "ret \n\t"
  680. /*
  681. * Main "loop" - Generating one more key than necessary,
  682. * see definition of mbedtls_aes_context.buf
  683. */
  684. "2: \n\t"
  685. AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
  686. AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
  687. AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
  688. AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
  689. AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
  690. AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
  691. AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
  692. :
  693. : "r" (rk), "r" (key)
  694. : "memory", "cc", "0");
  695. }
  696. #endif /* MBEDTLS_AESNI_HAVE_CODE */
  697. /*
  698. * Key expansion, wrapper
  699. */
  700. int mbedtls_aesni_setkey_enc(unsigned char *rk,
  701. const unsigned char *key,
  702. size_t bits)
  703. {
  704. switch (bits) {
  705. case 128: aesni_setkey_enc_128(rk, key); break;
  706. case 192: aesni_setkey_enc_192(rk, key); break;
  707. case 256: aesni_setkey_enc_256(rk, key); break;
  708. default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
  709. }
  710. return 0;
  711. }
  712. #endif /* MBEDTLS_AESNI_HAVE_CODE */
  713. #endif /* MBEDTLS_AESNI_C */