1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty *
4*0957b409SSimon J. Gerraty * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty * the following conditions:
11*0957b409SSimon J. Gerraty *
12*0957b409SSimon J. Gerraty * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty *
15*0957b409SSimon J. Gerraty * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty * SOFTWARE.
23*0957b409SSimon J. Gerraty */
24*0957b409SSimon J. Gerraty
25*0957b409SSimon J. Gerraty #define BR_ENABLE_INTRINSICS 1
26*0957b409SSimon J. Gerraty #include "inner.h"
27*0957b409SSimon J. Gerraty
28*0957b409SSimon J. Gerraty #if BR_AES_X86NI
29*0957b409SSimon J. Gerraty
30*0957b409SSimon J. Gerraty /* see bearssl_block.h */
31*0957b409SSimon J. Gerraty const br_block_ctr_class *
br_aes_x86ni_ctr_get_vtable(void)32*0957b409SSimon J. Gerraty br_aes_x86ni_ctr_get_vtable(void)
33*0957b409SSimon J. Gerraty {
34*0957b409SSimon J. Gerraty return br_aes_x86ni_supported() ? &br_aes_x86ni_ctr_vtable : NULL;
35*0957b409SSimon J. Gerraty }
36*0957b409SSimon J. Gerraty
37*0957b409SSimon J. Gerraty /* see bearssl_block.h */
38*0957b409SSimon J. Gerraty void
br_aes_x86ni_ctr_init(br_aes_x86ni_ctr_keys * ctx,const void * key,size_t len)39*0957b409SSimon J. Gerraty br_aes_x86ni_ctr_init(br_aes_x86ni_ctr_keys *ctx,
40*0957b409SSimon J. Gerraty const void *key, size_t len)
41*0957b409SSimon J. Gerraty {
42*0957b409SSimon J. Gerraty ctx->vtable = &br_aes_x86ni_ctr_vtable;
43*0957b409SSimon J. Gerraty ctx->num_rounds = br_aes_x86ni_keysched_enc(ctx->skey.skni, key, len);
44*0957b409SSimon J. Gerraty }
45*0957b409SSimon J. Gerraty
46*0957b409SSimon J. Gerraty BR_TARGETS_X86_UP
47*0957b409SSimon J. Gerraty
48*0957b409SSimon J. Gerraty /* see bearssl_block.h */
49*0957b409SSimon J. Gerraty BR_TARGET("sse2,sse4.1,aes")
50*0957b409SSimon J. Gerraty uint32_t
br_aes_x86ni_ctr_run(const br_aes_x86ni_ctr_keys * ctx,const void * iv,uint32_t cc,void * data,size_t len)51*0957b409SSimon J. Gerraty br_aes_x86ni_ctr_run(const br_aes_x86ni_ctr_keys *ctx,
52*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len)
53*0957b409SSimon J. Gerraty {
54*0957b409SSimon J. Gerraty unsigned char *buf;
55*0957b409SSimon J. Gerraty unsigned char ivbuf[16];
56*0957b409SSimon J. Gerraty unsigned num_rounds;
57*0957b409SSimon J. Gerraty __m128i sk[15];
58*0957b409SSimon J. Gerraty __m128i ivx;
59*0957b409SSimon J. Gerraty unsigned u;
60*0957b409SSimon J. Gerraty
61*0957b409SSimon J. Gerraty buf = data;
62*0957b409SSimon J. Gerraty memcpy(ivbuf, iv, 12);
63*0957b409SSimon J. Gerraty num_rounds = ctx->num_rounds;
64*0957b409SSimon J. Gerraty for (u = 0; u <= num_rounds; u ++) {
65*0957b409SSimon J. Gerraty sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
66*0957b409SSimon J. Gerraty }
67*0957b409SSimon J. Gerraty ivx = _mm_loadu_si128((void *)ivbuf);
68*0957b409SSimon J. Gerraty while (len > 0) {
69*0957b409SSimon J. Gerraty __m128i x0, x1, x2, x3;
70*0957b409SSimon J. Gerraty
71*0957b409SSimon J. Gerraty x0 = _mm_insert_epi32(ivx, br_bswap32(cc + 0), 3);
72*0957b409SSimon J. Gerraty x1 = _mm_insert_epi32(ivx, br_bswap32(cc + 1), 3);
73*0957b409SSimon J. Gerraty x2 = _mm_insert_epi32(ivx, br_bswap32(cc + 2), 3);
74*0957b409SSimon J. Gerraty x3 = _mm_insert_epi32(ivx, br_bswap32(cc + 3), 3);
75*0957b409SSimon J. Gerraty x0 = _mm_xor_si128(x0, sk[0]);
76*0957b409SSimon J. Gerraty x1 = _mm_xor_si128(x1, sk[0]);
77*0957b409SSimon J. Gerraty x2 = _mm_xor_si128(x2, sk[0]);
78*0957b409SSimon J. Gerraty x3 = _mm_xor_si128(x3, sk[0]);
79*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[1]);
80*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[1]);
81*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[1]);
82*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[1]);
83*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[2]);
84*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[2]);
85*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[2]);
86*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[2]);
87*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[3]);
88*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[3]);
89*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[3]);
90*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[3]);
91*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[4]);
92*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[4]);
93*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[4]);
94*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[4]);
95*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[5]);
96*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[5]);
97*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[5]);
98*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[5]);
99*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[6]);
100*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[6]);
101*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[6]);
102*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[6]);
103*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[7]);
104*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[7]);
105*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[7]);
106*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[7]);
107*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[8]);
108*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[8]);
109*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[8]);
110*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[8]);
111*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[9]);
112*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[9]);
113*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[9]);
114*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[9]);
115*0957b409SSimon J. Gerraty if (num_rounds == 10) {
116*0957b409SSimon J. Gerraty x0 = _mm_aesenclast_si128(x0, sk[10]);
117*0957b409SSimon J. Gerraty x1 = _mm_aesenclast_si128(x1, sk[10]);
118*0957b409SSimon J. Gerraty x2 = _mm_aesenclast_si128(x2, sk[10]);
119*0957b409SSimon J. Gerraty x3 = _mm_aesenclast_si128(x3, sk[10]);
120*0957b409SSimon J. Gerraty } else if (num_rounds == 12) {
121*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[10]);
122*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[10]);
123*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[10]);
124*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[10]);
125*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[11]);
126*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[11]);
127*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[11]);
128*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[11]);
129*0957b409SSimon J. Gerraty x0 = _mm_aesenclast_si128(x0, sk[12]);
130*0957b409SSimon J. Gerraty x1 = _mm_aesenclast_si128(x1, sk[12]);
131*0957b409SSimon J. Gerraty x2 = _mm_aesenclast_si128(x2, sk[12]);
132*0957b409SSimon J. Gerraty x3 = _mm_aesenclast_si128(x3, sk[12]);
133*0957b409SSimon J. Gerraty } else {
134*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[10]);
135*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[10]);
136*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[10]);
137*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[10]);
138*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[11]);
139*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[11]);
140*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[11]);
141*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[11]);
142*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[12]);
143*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[12]);
144*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[12]);
145*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[12]);
146*0957b409SSimon J. Gerraty x0 = _mm_aesenc_si128(x0, sk[13]);
147*0957b409SSimon J. Gerraty x1 = _mm_aesenc_si128(x1, sk[13]);
148*0957b409SSimon J. Gerraty x2 = _mm_aesenc_si128(x2, sk[13]);
149*0957b409SSimon J. Gerraty x3 = _mm_aesenc_si128(x3, sk[13]);
150*0957b409SSimon J. Gerraty x0 = _mm_aesenclast_si128(x0, sk[14]);
151*0957b409SSimon J. Gerraty x1 = _mm_aesenclast_si128(x1, sk[14]);
152*0957b409SSimon J. Gerraty x2 = _mm_aesenclast_si128(x2, sk[14]);
153*0957b409SSimon J. Gerraty x3 = _mm_aesenclast_si128(x3, sk[14]);
154*0957b409SSimon J. Gerraty }
155*0957b409SSimon J. Gerraty if (len >= 64) {
156*0957b409SSimon J. Gerraty x0 = _mm_xor_si128(x0,
157*0957b409SSimon J. Gerraty _mm_loadu_si128((void *)(buf + 0)));
158*0957b409SSimon J. Gerraty x1 = _mm_xor_si128(x1,
159*0957b409SSimon J. Gerraty _mm_loadu_si128((void *)(buf + 16)));
160*0957b409SSimon J. Gerraty x2 = _mm_xor_si128(x2,
161*0957b409SSimon J. Gerraty _mm_loadu_si128((void *)(buf + 32)));
162*0957b409SSimon J. Gerraty x3 = _mm_xor_si128(x3,
163*0957b409SSimon J. Gerraty _mm_loadu_si128((void *)(buf + 48)));
164*0957b409SSimon J. Gerraty _mm_storeu_si128((void *)(buf + 0), x0);
165*0957b409SSimon J. Gerraty _mm_storeu_si128((void *)(buf + 16), x1);
166*0957b409SSimon J. Gerraty _mm_storeu_si128((void *)(buf + 32), x2);
167*0957b409SSimon J. Gerraty _mm_storeu_si128((void *)(buf + 48), x3);
168*0957b409SSimon J. Gerraty buf += 64;
169*0957b409SSimon J. Gerraty len -= 64;
170*0957b409SSimon J. Gerraty cc += 4;
171*0957b409SSimon J. Gerraty } else {
172*0957b409SSimon J. Gerraty unsigned char tmp[64];
173*0957b409SSimon J. Gerraty
174*0957b409SSimon J. Gerraty _mm_storeu_si128((void *)(tmp + 0), x0);
175*0957b409SSimon J. Gerraty _mm_storeu_si128((void *)(tmp + 16), x1);
176*0957b409SSimon J. Gerraty _mm_storeu_si128((void *)(tmp + 32), x2);
177*0957b409SSimon J. Gerraty _mm_storeu_si128((void *)(tmp + 48), x3);
178*0957b409SSimon J. Gerraty for (u = 0; u < len; u ++) {
179*0957b409SSimon J. Gerraty buf[u] ^= tmp[u];
180*0957b409SSimon J. Gerraty }
181*0957b409SSimon J. Gerraty cc += (uint32_t)len >> 4;
182*0957b409SSimon J. Gerraty break;
183*0957b409SSimon J. Gerraty }
184*0957b409SSimon J. Gerraty }
185*0957b409SSimon J. Gerraty return cc;
186*0957b409SSimon J. Gerraty }
187*0957b409SSimon J. Gerraty
188*0957b409SSimon J. Gerraty BR_TARGETS_X86_DOWN
189*0957b409SSimon J. Gerraty
190*0957b409SSimon J. Gerraty /* see bearssl_block.h */
191*0957b409SSimon J. Gerraty const br_block_ctr_class br_aes_x86ni_ctr_vtable = {
192*0957b409SSimon J. Gerraty sizeof(br_aes_x86ni_ctr_keys),
193*0957b409SSimon J. Gerraty 16,
194*0957b409SSimon J. Gerraty 4,
195*0957b409SSimon J. Gerraty (void (*)(const br_block_ctr_class **, const void *, size_t))
196*0957b409SSimon J. Gerraty &br_aes_x86ni_ctr_init,
197*0957b409SSimon J. Gerraty (uint32_t (*)(const br_block_ctr_class *const *,
198*0957b409SSimon J. Gerraty const void *, uint32_t, void *, size_t))
199*0957b409SSimon J. Gerraty &br_aes_x86ni_ctr_run
200*0957b409SSimon J. Gerraty };
201*0957b409SSimon J. Gerraty
202*0957b409SSimon J. Gerraty #else
203*0957b409SSimon J. Gerraty
204*0957b409SSimon J. Gerraty /* see bearssl_block.h */
205*0957b409SSimon J. Gerraty const br_block_ctr_class *
br_aes_x86ni_ctr_get_vtable(void)206*0957b409SSimon J. Gerraty br_aes_x86ni_ctr_get_vtable(void)
207*0957b409SSimon J. Gerraty {
208*0957b409SSimon J. Gerraty return NULL;
209*0957b409SSimon J. Gerraty }
210*0957b409SSimon J. Gerraty
211*0957b409SSimon J. Gerraty #endif
212