aesni.c 31 KB

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