Path: blob/master/thirdparty/mbedtls/library/aesni.c
10278 views
/*1* AES-NI support functions2*3* Copyright The Mbed TLS Contributors4* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later5*/67/*8* [AES-WP] https://www.intel.com/content/www/us/en/developer/articles/tool/intel-advanced-encryption-standard-aes-instructions-set.html9* [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.html10*/1112#include "common.h"1314#if defined(MBEDTLS_AESNI_C)1516#include "aesni.h"1718#include <string.h>1920#if defined(MBEDTLS_AESNI_HAVE_CODE)2122#if MBEDTLS_AESNI_HAVE_CODE == 223#if defined(__GNUC__)24#include <cpuid.h>25#elif defined(_MSC_VER)26#include <intrin.h>27#else28#error "`__cpuid` required by MBEDTLS_AESNI_C is not supported by the compiler"29#endif30#include <immintrin.h>31#endif3233#if defined(MBEDTLS_ARCH_IS_X86)34#if defined(MBEDTLS_COMPILER_IS_GCC)35#pragma GCC push_options36#pragma GCC target ("pclmul,sse2,aes")37#define MBEDTLS_POP_TARGET_PRAGMA38#elif defined(__clang__) && (__clang_major__ >= 5)39#pragma clang attribute push (__attribute__((target("pclmul,sse2,aes"))), apply_to=function)40#define MBEDTLS_POP_TARGET_PRAGMA41#endif42#endif4344#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)45/*46* AES-NI support detection routine47*/48int mbedtls_aesni_has_support(unsigned int what)49{50/* To avoid a race condition, tell the compiler that the assignment51* `done = 1` and the assignment to `c` may not be reordered.52* https://github.com/Mbed-TLS/mbedtls/issues/984053*54* Note that we may also be worried about memory access reordering,55* but fortunately the x86 memory model is not too wild: stores56* from the same thread are observed consistently by other threads.57* (See example 8-1 in Sewell et al., "x86-TSO: A Rigorous and Usable58* Programmer’s Model for x86 Multiprocessors", CACM, 2010,59* https://www.cl.cam.ac.uk/~pes20/weakmemory/cacm.pdf)60*/61static volatile int done = 0;62static volatile unsigned int c = 0;6364if (!done) {65#if MBEDTLS_AESNI_HAVE_CODE == 266static int info[4] = { 0, 0, 0, 0 };67#if defined(_MSC_VER)68__cpuid(info, 1);69#else70__cpuid(1, info[0], info[1], info[2], info[3]);71#endif72c = info[2];73#else /* AESNI using asm */74asm ("movl $1, %%eax \n\t"75"cpuid \n\t"76: "=c" (c)77:78: "eax", "ebx", "edx");79#endif /* MBEDTLS_AESNI_HAVE_CODE */80done = 1;81}8283return (c & what) != 0;84}85#endif /* !MBEDTLS_AES_USE_HARDWARE_ONLY */8687#if MBEDTLS_AESNI_HAVE_CODE == 28889/*90* AES-NI AES-ECB block en(de)cryption91*/92int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,93int mode,94const unsigned char input[16],95unsigned char output[16])96{97const __m128i *rk = (const __m128i *) (ctx->buf + ctx->rk_offset);98unsigned nr = ctx->nr; // Number of remaining rounds99100// Load round key 0101__m128i state;102memcpy(&state, input, 16);103state = _mm_xor_si128(state, rk[0]); // state ^= *rk;104++rk;105--nr;106107#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)108if (mode == MBEDTLS_AES_DECRYPT) {109while (nr != 0) {110state = _mm_aesdec_si128(state, *rk);111++rk;112--nr;113}114state = _mm_aesdeclast_si128(state, *rk);115} else116#else117(void) mode;118#endif119{120while (nr != 0) {121state = _mm_aesenc_si128(state, *rk);122++rk;123--nr;124}125state = _mm_aesenclast_si128(state, *rk);126}127128memcpy(output, &state, 16);129return 0;130}131132/*133* GCM multiplication: c = a times b in GF(2^128)134* Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.135*/136137static void gcm_clmul(const __m128i aa, const __m128i bb,138__m128i *cc, __m128i *dd)139{140/*141* Caryless multiplication dd:cc = aa * bb142* using [CLMUL-WP] algorithm 1 (p. 12).143*/144*cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0145*dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0146__m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0147__m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0148ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0149ee = ff; // e1+f1:e0+f0150ff = _mm_srli_si128(ff, 8); // 0:e1+f1151ee = _mm_slli_si128(ee, 8); // e0+f0:0152*dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1153*cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0154}155156static void gcm_shift(__m128i *cc, __m128i *dd)157{158/* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,159* taking advantage of [CLMUL-WP] eq 27 (p. 18). */160// // *cc = r1:r0161// // *dd = r3:r2162__m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1163__m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1164__m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63165__m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63166__m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63167cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0168dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63169170*cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1171*dd = _mm_or_si128(_mm_or_si128(dd_lo, dd_hi), xmm5); // r3<<1|r2>>62:r2<<1|r1>>63172}173174static __m128i gcm_reduce(__m128i xx)175{176// // xx = x1:x0177/* [CLMUL-WP] Algorithm 5 Step 2 */178__m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a179__m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b180__m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c181__m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0182return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0183}184185static __m128i gcm_mix(__m128i dx)186{187/* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */188__m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'189__m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'190__m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'191192// e0'+f0'+g0' is almost e0+f0+g0, except for some missing193// bits carried from d. Now get those bits back in.194__m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff195__m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff196__m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff197__m128i hh = _mm_srli_si128(_mm_xor_si128(_mm_xor_si128(eh, fh), gh), 8); // 0:missing bits of d198199return _mm_xor_si128(_mm_xor_si128(_mm_xor_si128(_mm_xor_si128(ee, ff), gg), hh), dx);200}201202void mbedtls_aesni_gcm_mult(unsigned char c[16],203const unsigned char a[16],204const unsigned char b[16])205{206__m128i aa = { 0 }, bb = { 0 }, cc, dd;207208/* The inputs are in big-endian order, so byte-reverse them */209for (size_t i = 0; i < 16; i++) {210((uint8_t *) &aa)[i] = a[15 - i];211((uint8_t *) &bb)[i] = b[15 - i];212}213214gcm_clmul(aa, bb, &cc, &dd);215gcm_shift(&cc, &dd);216/*217* Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1218* using [CLMUL-WP] algorithm 5 (p. 18).219* Currently dd:cc holds x3:x2:x1:x0 (already shifted).220*/221__m128i dx = gcm_reduce(cc);222__m128i xh = gcm_mix(dx);223cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0224225/* Now byte-reverse the outputs */226for (size_t i = 0; i < 16; i++) {227c[i] = ((uint8_t *) &cc)[15 - i];228}229230return;231}232233/*234* Compute decryption round keys from encryption round keys235*/236#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)237void mbedtls_aesni_inverse_key(unsigned char *invkey,238const unsigned char *fwdkey, int nr)239{240__m128i *ik = (__m128i *) invkey;241const __m128i *fk = (const __m128i *) fwdkey + nr;242243*ik = *fk;244for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {245*ik = _mm_aesimc_si128(*fk);246}247*ik = *fk;248}249#endif250251/*252* Key expansion, 128-bit case253*/254static __m128i aesni_set_rk_128(__m128i state, __m128i xword)255{256/*257* Finish generating the next round key.258*259* On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff260* with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).261*262* On exit, xword is r7:r6:r5:r4263* with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3264* and this is returned, to be written to the round key buffer.265*/266xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X267xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4268state = _mm_slli_si128(state, 4); // r2:r1:r0:0269xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4270state = _mm_slli_si128(state, 4); // r1:r0:0:0271xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4272state = _mm_slli_si128(state, 4); // r0:0:0:0273state = _mm_xor_si128(xword, state); // r7:r6:r5:r4274return state;275}276277static void aesni_setkey_enc_128(unsigned char *rk_bytes,278const unsigned char *key)279{280__m128i *rk = (__m128i *) rk_bytes;281282memcpy(&rk[0], key, 16);283rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));284rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));285rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));286rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));287rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));288rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));289rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));290rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));291rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));292rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));293}294295/*296* Key expansion, 192-bit case297*/298#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)299static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,300unsigned char *rk)301{302/*303* Finish generating the next 6 quarter-keys.304*305* On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4306* and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON307* (obtained with AESKEYGENASSIST).308*309* On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10310* and those are written to the round key buffer.311*/312xword = _mm_shuffle_epi32(xword, 0x55); // X:X:X:X313xword = _mm_xor_si128(xword, *state0); // X+r3:X+r2:X+r1:X+r0314*state0 = _mm_slli_si128(*state0, 4); // r2:r1:r0:0315xword = _mm_xor_si128(xword, *state0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0316*state0 = _mm_slli_si128(*state0, 4); // r1:r0:0:0317xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0318*state0 = _mm_slli_si128(*state0, 4); // r0:0:0:0319xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0320*state0 = xword; // = r9:r8:r7:r6321322xword = _mm_shuffle_epi32(xword, 0xff); // r9:r9:r9:r9323xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5:r9+r4324*state1 = _mm_slli_si128(*state1, 4); // stuff:stuff:r4:0325xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5+r4:r9+r4326*state1 = xword; // = stuff:stuff:r11:r10327328/* Store state0 and the low half of state1 into rk, which is conceptually329* an array of 24-byte elements. Since 24 is not a multiple of 16,330* rk is not necessarily aligned so just `*rk = *state0` doesn't work. */331memcpy(rk, state0, 16);332memcpy(rk + 16, state1, 8);333}334335static void aesni_setkey_enc_192(unsigned char *rk,336const unsigned char *key)337{338/* First round: use original key */339memcpy(rk, key, 24);340/* aes.c guarantees that rk is aligned on a 16-byte boundary. */341__m128i state0 = ((__m128i *) rk)[0];342__m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);343344aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1);345aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2);346aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3);347aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4);348aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5);349aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6);350aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7);351aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8);352}353#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */354355/*356* Key expansion, 256-bit case357*/358#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)359static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,360__m128i *rk0, __m128i *rk1)361{362/*363* Finish generating the next two round keys.364*365* On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and366* xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON367* (obtained with AESKEYGENASSIST).368*369* On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12370*/371xword = _mm_shuffle_epi32(xword, 0xff);372xword = _mm_xor_si128(xword, state0);373state0 = _mm_slli_si128(state0, 4);374xword = _mm_xor_si128(xword, state0);375state0 = _mm_slli_si128(state0, 4);376xword = _mm_xor_si128(xword, state0);377state0 = _mm_slli_si128(state0, 4);378state0 = _mm_xor_si128(state0, xword);379*rk0 = state0;380381/* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )382* and proceed to generate next round key from there */383xword = _mm_aeskeygenassist_si128(state0, 0x00);384xword = _mm_shuffle_epi32(xword, 0xaa);385xword = _mm_xor_si128(xword, state1);386state1 = _mm_slli_si128(state1, 4);387xword = _mm_xor_si128(xword, state1);388state1 = _mm_slli_si128(state1, 4);389xword = _mm_xor_si128(xword, state1);390state1 = _mm_slli_si128(state1, 4);391state1 = _mm_xor_si128(state1, xword);392*rk1 = state1;393}394395static void aesni_setkey_enc_256(unsigned char *rk_bytes,396const unsigned char *key)397{398__m128i *rk = (__m128i *) rk_bytes;399400memcpy(&rk[0], key, 16);401memcpy(&rk[1], key + 16, 16);402403/*404* Main "loop" - Generating one more key than necessary,405* see definition of mbedtls_aes_context.buf406*/407aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);408aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);409aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);410aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);411aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);412aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);413aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);414}415#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */416417#if defined(MBEDTLS_POP_TARGET_PRAGMA)418#if defined(__clang__)419#pragma clang attribute pop420#elif defined(__GNUC__)421#pragma GCC pop_options422#endif423#undef MBEDTLS_POP_TARGET_PRAGMA424#endif425426#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */427428#if defined(__has_feature)429#if __has_feature(memory_sanitizer)430#warning \431"MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."432#endif433#endif434435/*436* Binutils needs to be at least 2.19 to support AES-NI instructions.437* Unfortunately, a lot of users have a lower version now (2014-04).438* Emit bytecode directly in order to support "old" version of gas.439*440* Opcodes from the Intel architecture reference manual, vol. 3.441* We always use registers, so we don't need prefixes for memory operands.442* Operand macros are in gas order (src, dst) as opposed to Intel order443* (dst, src) in order to blend better into the surrounding assembly code.444*/445#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"446#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"447#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"448#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"449#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"450#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"451#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"452453#define xmm0_xmm0 "0xC0"454#define xmm0_xmm1 "0xC8"455#define xmm0_xmm2 "0xD0"456#define xmm0_xmm3 "0xD8"457#define xmm0_xmm4 "0xE0"458#define xmm1_xmm0 "0xC1"459#define xmm1_xmm2 "0xD1"460461/*462* AES-NI AES-ECB block en(de)cryption463*/464int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,465int mode,466const unsigned char input[16],467unsigned char output[16])468{469asm ("movdqu (%3), %%xmm0 \n\t" // load input470"movdqu (%1), %%xmm1 \n\t" // load round key 0471"pxor %%xmm1, %%xmm0 \n\t" // round 0472"add $16, %1 \n\t" // point to next round key473"subl $1, %0 \n\t" // normal rounds = nr - 1474"test %2, %2 \n\t" // mode?475"jz 2f \n\t" // 0 = decrypt476477"1: \n\t" // encryption loop478"movdqu (%1), %%xmm1 \n\t" // load round key479AESENC(xmm1_xmm0) // do round480"add $16, %1 \n\t" // point to next round key481"subl $1, %0 \n\t" // loop482"jnz 1b \n\t"483"movdqu (%1), %%xmm1 \n\t" // load round key484AESENCLAST(xmm1_xmm0) // last round485#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)486"jmp 3f \n\t"487488"2: \n\t" // decryption loop489"movdqu (%1), %%xmm1 \n\t"490AESDEC(xmm1_xmm0) // do round491"add $16, %1 \n\t"492"subl $1, %0 \n\t"493"jnz 2b \n\t"494"movdqu (%1), %%xmm1 \n\t" // load round key495AESDECLAST(xmm1_xmm0) // last round496#endif497498"3: \n\t"499"movdqu %%xmm0, (%4) \n\t" // export output500:501: "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output)502: "memory", "cc", "xmm0", "xmm1", "0", "1");503504505return 0;506}507508/*509* GCM multiplication: c = a times b in GF(2^128)510* Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.511*/512void mbedtls_aesni_gcm_mult(unsigned char c[16],513const unsigned char a[16],514const unsigned char b[16])515{516unsigned char aa[16], bb[16], cc[16];517size_t i;518519/* The inputs are in big-endian order, so byte-reverse them */520for (i = 0; i < 16; i++) {521aa[i] = a[15 - i];522bb[i] = b[15 - i];523}524525asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0526"movdqu (%1), %%xmm1 \n\t" // b1:b0527528/*529* Caryless multiplication xmm2:xmm1 = xmm0 * xmm1530* using [CLMUL-WP] algorithm 1 (p. 12).531*/532"movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0533"movdqa %%xmm1, %%xmm3 \n\t" // same534"movdqa %%xmm1, %%xmm4 \n\t" // same535PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0536PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0537PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0538PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0539"pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0540"movdqa %%xmm4, %%xmm3 \n\t" // same541"psrldq $8, %%xmm4 \n\t" // 0:e1+f1542"pslldq $8, %%xmm3 \n\t" // e0+f0:0543"pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1544"pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0545546/*547* Now shift the result one bit to the left,548* taking advantage of [CLMUL-WP] eq 27 (p. 18)549*/550"movdqa %%xmm1, %%xmm3 \n\t" // r1:r0551"movdqa %%xmm2, %%xmm4 \n\t" // r3:r2552"psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1553"psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1554"psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63555"psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63556"movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63557"pslldq $8, %%xmm3 \n\t" // r0>>63:0558"pslldq $8, %%xmm4 \n\t" // r2>>63:0559"psrldq $8, %%xmm5 \n\t" // 0:r1>>63560"por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1561"por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1562"por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63563564/*565* Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1566* using [CLMUL-WP] algorithm 5 (p. 18).567* Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).568*/569/* Step 2 (1) */570"movdqa %%xmm1, %%xmm3 \n\t" // x1:x0571"movdqa %%xmm1, %%xmm4 \n\t" // same572"movdqa %%xmm1, %%xmm5 \n\t" // same573"psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a574"psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b575"psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c576577/* Step 2 (2) */578"pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b579"pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c580"pslldq $8, %%xmm3 \n\t" // a+b+c:0581"pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0582583/* Steps 3 and 4 */584"movdqa %%xmm1,%%xmm0 \n\t" // d:x0585"movdqa %%xmm1,%%xmm4 \n\t" // same586"movdqa %%xmm1,%%xmm5 \n\t" // same587"psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'588"psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'589"psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'590"pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'591"pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'592// e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing593// bits carried from d. Now get those\t bits back in.594"movdqa %%xmm1,%%xmm3 \n\t" // d:x0595"movdqa %%xmm1,%%xmm4 \n\t" // same596"movdqa %%xmm1,%%xmm5 \n\t" // same597"psllq $63, %%xmm3 \n\t" // d<<63:stuff598"psllq $62, %%xmm4 \n\t" // d<<62:stuff599"psllq $57, %%xmm5 \n\t" // d<<57:stuff600"pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff601"pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff602"psrldq $8, %%xmm3 \n\t" // 0:missing bits of d603"pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0604"pxor %%xmm1, %%xmm0 \n\t" // h1:h0605"pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0606607"movdqu %%xmm0, (%2) \n\t" // done608:609: "r" (aa), "r" (bb), "r" (cc)610: "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");611612/* Now byte-reverse the outputs */613for (i = 0; i < 16; i++) {614c[i] = cc[15 - i];615}616617return;618}619620/*621* Compute decryption round keys from encryption round keys622*/623#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)624void mbedtls_aesni_inverse_key(unsigned char *invkey,625const unsigned char *fwdkey, int nr)626{627unsigned char *ik = invkey;628const unsigned char *fk = fwdkey + 16 * nr;629630memcpy(ik, fk, 16);631632for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {633asm ("movdqu (%0), %%xmm0 \n\t"634AESIMC(xmm0_xmm0)635"movdqu %%xmm0, (%1) \n\t"636:637: "r" (fk), "r" (ik)638: "memory", "xmm0");639}640641memcpy(ik, fk, 16);642}643#endif644645/*646* Key expansion, 128-bit case647*/648static void aesni_setkey_enc_128(unsigned char *rk,649const unsigned char *key)650{651asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key652"movdqu %%xmm0, (%0) \n\t" // as round key 0653"jmp 2f \n\t" // skip auxiliary routine654655/*656* Finish generating the next round key.657*658* On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff659* with X = rot( sub( r3 ) ) ^ RCON.660*661* On exit, xmm0 is r7:r6:r5:r4662* with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3663* and those are written to the round key buffer.664*/665"1: \n\t"666"pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X667"pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4668"pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0669"pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4670"pslldq $4, %%xmm0 \n\t" // etc671"pxor %%xmm0, %%xmm1 \n\t"672"pslldq $4, %%xmm0 \n\t"673"pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!674"add $16, %0 \n\t" // point to next round key675"movdqu %%xmm0, (%0) \n\t" // write it676"ret \n\t"677678/* Main "loop" */679"2: \n\t"680AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"681AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"682AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"683AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"684AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"685AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"686AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"687AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"688AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"689AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"690:691: "r" (rk), "r" (key)692: "memory", "cc", "xmm0", "xmm1", "0");693}694695/*696* Key expansion, 192-bit case697*/698#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)699static void aesni_setkey_enc_192(unsigned char *rk,700const unsigned char *key)701{702asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key703"movdqu %%xmm0, (%0) \n\t"704"add $16, %0 \n\t"705"movq 16(%1), %%xmm1 \n\t"706"movq %%xmm1, (%0) \n\t"707"add $8, %0 \n\t"708"jmp 2f \n\t" // skip auxiliary routine709710/*711* Finish generating the next 6 quarter-keys.712*713* On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4714* and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.715*716* On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10717* and those are written to the round key buffer.718*/719"1: \n\t"720"pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X721"pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4722"pslldq $4, %%xmm0 \n\t" // etc723"pxor %%xmm0, %%xmm2 \n\t"724"pslldq $4, %%xmm0 \n\t"725"pxor %%xmm0, %%xmm2 \n\t"726"pslldq $4, %%xmm0 \n\t"727"pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6728"movdqu %%xmm0, (%0) \n\t"729"add $16, %0 \n\t"730"pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9731"pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10732"pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0733"pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10734"movq %%xmm1, (%0) \n\t"735"add $8, %0 \n\t"736"ret \n\t"737738"2: \n\t"739AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"740AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"741AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"742AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"743AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"744AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"745AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"746AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"747748:749: "r" (rk), "r" (key)750: "memory", "cc", "xmm0", "xmm1", "xmm2", "0");751}752#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */753754/*755* Key expansion, 256-bit case756*/757#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)758static void aesni_setkey_enc_256(unsigned char *rk,759const unsigned char *key)760{761asm ("movdqu (%1), %%xmm0 \n\t"762"movdqu %%xmm0, (%0) \n\t"763"add $16, %0 \n\t"764"movdqu 16(%1), %%xmm1 \n\t"765"movdqu %%xmm1, (%0) \n\t"766"jmp 2f \n\t" // skip auxiliary routine767768/*769* Finish generating the next two round keys.770*771* On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and772* xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON773*774* On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12775* and those have been written to the output buffer.776*/777"1: \n\t"778"pshufd $0xff, %%xmm2, %%xmm2 \n\t"779"pxor %%xmm0, %%xmm2 \n\t"780"pslldq $4, %%xmm0 \n\t"781"pxor %%xmm0, %%xmm2 \n\t"782"pslldq $4, %%xmm0 \n\t"783"pxor %%xmm0, %%xmm2 \n\t"784"pslldq $4, %%xmm0 \n\t"785"pxor %%xmm2, %%xmm0 \n\t"786"add $16, %0 \n\t"787"movdqu %%xmm0, (%0) \n\t"788789/* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )790* and proceed to generate next round key from there */791AESKEYGENA(xmm0_xmm2, "0x00")792"pshufd $0xaa, %%xmm2, %%xmm2 \n\t"793"pxor %%xmm1, %%xmm2 \n\t"794"pslldq $4, %%xmm1 \n\t"795"pxor %%xmm1, %%xmm2 \n\t"796"pslldq $4, %%xmm1 \n\t"797"pxor %%xmm1, %%xmm2 \n\t"798"pslldq $4, %%xmm1 \n\t"799"pxor %%xmm2, %%xmm1 \n\t"800"add $16, %0 \n\t"801"movdqu %%xmm1, (%0) \n\t"802"ret \n\t"803804/*805* Main "loop" - Generating one more key than necessary,806* see definition of mbedtls_aes_context.buf807*/808"2: \n\t"809AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"810AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"811AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"812AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"813AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"814AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"815AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"816:817: "r" (rk), "r" (key)818: "memory", "cc", "xmm0", "xmm1", "xmm2", "0");819}820#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */821822#endif /* MBEDTLS_AESNI_HAVE_CODE */823824/*825* Key expansion, wrapper826*/827int mbedtls_aesni_setkey_enc(unsigned char *rk,828const unsigned char *key,829size_t bits)830{831switch (bits) {832case 128: aesni_setkey_enc_128(rk, key); break;833#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)834case 192: aesni_setkey_enc_192(rk, key); break;835case 256: aesni_setkey_enc_256(rk, key); break;836#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */837default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;838}839840return 0;841}842843#endif /* MBEDTLS_AESNI_HAVE_CODE */844845#endif /* MBEDTLS_AESNI_C */846847848