Path: blob/main/contrib/bearssl/src/symcipher/aes_x86ni.c
39482 views
/*1* Copyright (c) 2017 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#define BR_ENABLE_INTRINSICS 125#include "inner.h"2627/*28* This code contains the AES key schedule implementation using the29* AES-NI opcodes.30*/3132#if BR_AES_X86NI3334/* see inner.h */35int36br_aes_x86ni_supported(void)37{38/*39* Bit mask for features in ECX:40* 19 SSE4.1 (used for _mm_insert_epi32(), for AES-CTR)41* 25 AES-NI42*/43return br_cpuid(0, 0, 0x02080000, 0);44}4546BR_TARGETS_X86_UP4748BR_TARGET("sse2,aes")49static inline __m128i50expand_step128(__m128i k, __m128i k2)51{52k = _mm_xor_si128(k, _mm_slli_si128(k, 4));53k = _mm_xor_si128(k, _mm_slli_si128(k, 4));54k = _mm_xor_si128(k, _mm_slli_si128(k, 4));55k2 = _mm_shuffle_epi32(k2, 0xFF);56return _mm_xor_si128(k, k2);57}5859BR_TARGET("sse2,aes")60static inline void61expand_step192(__m128i *t1, __m128i *t2, __m128i *t3)62{63__m128i t4;6465*t2 = _mm_shuffle_epi32(*t2, 0x55);66t4 = _mm_slli_si128(*t1, 0x4);67*t1 = _mm_xor_si128(*t1, t4);68t4 = _mm_slli_si128(t4, 0x4);69*t1 = _mm_xor_si128(*t1, t4);70t4 = _mm_slli_si128(t4, 0x4);71*t1 = _mm_xor_si128(*t1, t4);72*t1 = _mm_xor_si128(*t1, *t2);73*t2 = _mm_shuffle_epi32(*t1, 0xFF);74t4 = _mm_slli_si128(*t3, 0x4);75*t3 = _mm_xor_si128(*t3, t4);76*t3 = _mm_xor_si128(*t3, *t2);77}7879BR_TARGET("sse2,aes")80static inline void81expand_step256_1(__m128i *t1, __m128i *t2)82{83__m128i t4;8485*t2 = _mm_shuffle_epi32(*t2, 0xFF);86t4 = _mm_slli_si128(*t1, 0x4);87*t1 = _mm_xor_si128(*t1, t4);88t4 = _mm_slli_si128(t4, 0x4);89*t1 = _mm_xor_si128(*t1, t4);90t4 = _mm_slli_si128(t4, 0x4);91*t1 = _mm_xor_si128(*t1, t4);92*t1 = _mm_xor_si128(*t1, *t2);93}9495BR_TARGET("sse2,aes")96static inline void97expand_step256_2(__m128i *t1, __m128i *t3)98{99__m128i t2, t4;100101t4 = _mm_aeskeygenassist_si128(*t1, 0x0);102t2 = _mm_shuffle_epi32(t4, 0xAA);103t4 = _mm_slli_si128(*t3, 0x4);104*t3 = _mm_xor_si128(*t3, t4);105t4 = _mm_slli_si128(t4, 0x4);106*t3 = _mm_xor_si128(*t3, t4);107t4 = _mm_slli_si128(t4, 0x4);108*t3 = _mm_xor_si128(*t3, t4);109*t3 = _mm_xor_si128(*t3, t2);110}111112/*113* Perform key schedule for AES, encryption direction. Subkeys are written114* in sk[], and the number of rounds is returned. Key length MUST be 16,115* 24 or 32 bytes.116*/117BR_TARGET("sse2,aes")118static unsigned119x86ni_keysched(__m128i *sk, const void *key, size_t len)120{121const unsigned char *kb;122123#define KEXP128(k, i, rcon) do { \124k = expand_step128(k, _mm_aeskeygenassist_si128(k, rcon)); \125sk[i] = k; \126} while (0)127128#define KEXP192(i, rcon1, rcon2) do { \129sk[(i) + 0] = t1; \130sk[(i) + 1] = t3; \131t2 = _mm_aeskeygenassist_si128(t3, rcon1); \132expand_step192(&t1, &t2, &t3); \133sk[(i) + 1] = _mm_castpd_si128(_mm_shuffle_pd( \134_mm_castsi128_pd(sk[(i) + 1]), \135_mm_castsi128_pd(t1), 0)); \136sk[(i) + 2] = _mm_castpd_si128(_mm_shuffle_pd( \137_mm_castsi128_pd(t1), \138_mm_castsi128_pd(t3), 1)); \139t2 = _mm_aeskeygenassist_si128(t3, rcon2); \140expand_step192(&t1, &t2, &t3); \141} while (0)142143#define KEXP256(i, rcon) do { \144sk[(i) + 0] = t3; \145t2 = _mm_aeskeygenassist_si128(t3, rcon); \146expand_step256_1(&t1, &t2); \147sk[(i) + 1] = t1; \148expand_step256_2(&t1, &t3); \149} while (0)150151kb = key;152switch (len) {153__m128i t1, t2, t3;154155case 16:156t1 = _mm_loadu_si128((const void *)kb);157sk[0] = t1;158KEXP128(t1, 1, 0x01);159KEXP128(t1, 2, 0x02);160KEXP128(t1, 3, 0x04);161KEXP128(t1, 4, 0x08);162KEXP128(t1, 5, 0x10);163KEXP128(t1, 6, 0x20);164KEXP128(t1, 7, 0x40);165KEXP128(t1, 8, 0x80);166KEXP128(t1, 9, 0x1B);167KEXP128(t1, 10, 0x36);168return 10;169170case 24:171t1 = _mm_loadu_si128((const void *)kb);172t3 = _mm_loadu_si128((const void *)(kb + 8));173t3 = _mm_shuffle_epi32(t3, 0x4E);174KEXP192(0, 0x01, 0x02);175KEXP192(3, 0x04, 0x08);176KEXP192(6, 0x10, 0x20);177KEXP192(9, 0x40, 0x80);178sk[12] = t1;179return 12;180181case 32:182t1 = _mm_loadu_si128((const void *)kb);183t3 = _mm_loadu_si128((const void *)(kb + 16));184sk[0] = t1;185KEXP256( 1, 0x01);186KEXP256( 3, 0x02);187KEXP256( 5, 0x04);188KEXP256( 7, 0x08);189KEXP256( 9, 0x10);190KEXP256(11, 0x20);191sk[13] = t3;192t2 = _mm_aeskeygenassist_si128(t3, 0x40);193expand_step256_1(&t1, &t2);194sk[14] = t1;195return 14;196197default:198return 0;199}200201#undef KEXP128202#undef KEXP192203#undef KEXP256204}205206/* see inner.h */207BR_TARGET("sse2,aes")208unsigned209br_aes_x86ni_keysched_enc(unsigned char *skni, const void *key, size_t len)210{211__m128i sk[15];212unsigned num_rounds;213214num_rounds = x86ni_keysched(sk, key, len);215memcpy(skni, sk, (num_rounds + 1) << 4);216return num_rounds;217}218219/* see inner.h */220BR_TARGET("sse2,aes")221unsigned222br_aes_x86ni_keysched_dec(unsigned char *skni, const void *key, size_t len)223{224__m128i sk[15];225unsigned u, num_rounds;226227num_rounds = x86ni_keysched(sk, key, len);228_mm_storeu_si128((void *)skni, sk[num_rounds]);229for (u = 1; u < num_rounds; u ++) {230_mm_storeu_si128((void *)(skni + (u << 4)),231_mm_aesimc_si128(sk[num_rounds - u]));232}233_mm_storeu_si128((void *)(skni + (num_rounds << 4)), sk[0]);234return num_rounds;235}236237BR_TARGETS_X86_DOWN238239#endif240241242