aesni.c 31 KB

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