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