xref: /freebsd/contrib/bearssl/src/symcipher/aes_x86ni_cbcdec.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
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_cbcdec_class *
br_aes_x86ni_cbcdec_get_vtable(void)32*0957b409SSimon J. Gerraty br_aes_x86ni_cbcdec_get_vtable(void)
33*0957b409SSimon J. Gerraty {
34*0957b409SSimon J. Gerraty 	return br_aes_x86ni_supported() ? &br_aes_x86ni_cbcdec_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_cbcdec_init(br_aes_x86ni_cbcdec_keys * ctx,const void * key,size_t len)39*0957b409SSimon J. Gerraty br_aes_x86ni_cbcdec_init(br_aes_x86ni_cbcdec_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_cbcdec_vtable;
43*0957b409SSimon J. Gerraty 	ctx->num_rounds = br_aes_x86ni_keysched_dec(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,aes")
50*0957b409SSimon J. Gerraty void
br_aes_x86ni_cbcdec_run(const br_aes_x86ni_cbcdec_keys * ctx,void * iv,void * data,size_t len)51*0957b409SSimon J. Gerraty br_aes_x86ni_cbcdec_run(const br_aes_x86ni_cbcdec_keys *ctx,
52*0957b409SSimon J. Gerraty 	void *iv, void *data, size_t len)
53*0957b409SSimon J. Gerraty {
54*0957b409SSimon J. Gerraty 	unsigned char *buf;
55*0957b409SSimon J. Gerraty 	unsigned num_rounds;
56*0957b409SSimon J. Gerraty 	__m128i sk[15], ivx;
57*0957b409SSimon J. Gerraty 	unsigned u;
58*0957b409SSimon J. Gerraty 
59*0957b409SSimon J. Gerraty 	buf = data;
60*0957b409SSimon J. Gerraty 	ivx = _mm_loadu_si128(iv);
61*0957b409SSimon J. Gerraty 	num_rounds = ctx->num_rounds;
62*0957b409SSimon J. Gerraty 	for (u = 0; u <= num_rounds; u ++) {
63*0957b409SSimon J. Gerraty 		sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
64*0957b409SSimon J. Gerraty 	}
65*0957b409SSimon J. Gerraty 	while (len > 0) {
66*0957b409SSimon J. Gerraty 		__m128i x0, x1, x2, x3, e0, e1, e2, e3;
67*0957b409SSimon J. Gerraty 
68*0957b409SSimon J. Gerraty 		x0 = _mm_loadu_si128((void *)(buf +  0));
69*0957b409SSimon J. Gerraty 		if (len >= 64) {
70*0957b409SSimon J. Gerraty 			x1 = _mm_loadu_si128((void *)(buf + 16));
71*0957b409SSimon J. Gerraty 			x2 = _mm_loadu_si128((void *)(buf + 32));
72*0957b409SSimon J. Gerraty 			x3 = _mm_loadu_si128((void *)(buf + 48));
73*0957b409SSimon J. Gerraty 		} else {
74*0957b409SSimon J. Gerraty 			x0 = _mm_loadu_si128((void *)(buf +  0));
75*0957b409SSimon J. Gerraty 			if (len >= 32) {
76*0957b409SSimon J. Gerraty 				x1 = _mm_loadu_si128((void *)(buf + 16));
77*0957b409SSimon J. Gerraty 				if (len >= 48) {
78*0957b409SSimon J. Gerraty 					x2 = _mm_loadu_si128(
79*0957b409SSimon J. Gerraty 						(void *)(buf + 32));
80*0957b409SSimon J. Gerraty 					x3 = x2;
81*0957b409SSimon J. Gerraty 				} else {
82*0957b409SSimon J. Gerraty 					x2 = x0;
83*0957b409SSimon J. Gerraty 					x3 = x1;
84*0957b409SSimon J. Gerraty 				}
85*0957b409SSimon J. Gerraty 			} else {
86*0957b409SSimon J. Gerraty 				x1 = x0;
87*0957b409SSimon J. Gerraty 				x2 = x0;
88*0957b409SSimon J. Gerraty 				x3 = x0;
89*0957b409SSimon J. Gerraty 			}
90*0957b409SSimon J. Gerraty 		}
91*0957b409SSimon J. Gerraty 		e0 = x0;
92*0957b409SSimon J. Gerraty 		e1 = x1;
93*0957b409SSimon J. Gerraty 		e2 = x2;
94*0957b409SSimon J. Gerraty 		e3 = x3;
95*0957b409SSimon J. Gerraty 		x0 = _mm_xor_si128(x0, sk[0]);
96*0957b409SSimon J. Gerraty 		x1 = _mm_xor_si128(x1, sk[0]);
97*0957b409SSimon J. Gerraty 		x2 = _mm_xor_si128(x2, sk[0]);
98*0957b409SSimon J. Gerraty 		x3 = _mm_xor_si128(x3, sk[0]);
99*0957b409SSimon J. Gerraty 		x0 = _mm_aesdec_si128(x0, sk[1]);
100*0957b409SSimon J. Gerraty 		x1 = _mm_aesdec_si128(x1, sk[1]);
101*0957b409SSimon J. Gerraty 		x2 = _mm_aesdec_si128(x2, sk[1]);
102*0957b409SSimon J. Gerraty 		x3 = _mm_aesdec_si128(x3, sk[1]);
103*0957b409SSimon J. Gerraty 		x0 = _mm_aesdec_si128(x0, sk[2]);
104*0957b409SSimon J. Gerraty 		x1 = _mm_aesdec_si128(x1, sk[2]);
105*0957b409SSimon J. Gerraty 		x2 = _mm_aesdec_si128(x2, sk[2]);
106*0957b409SSimon J. Gerraty 		x3 = _mm_aesdec_si128(x3, sk[2]);
107*0957b409SSimon J. Gerraty 		x0 = _mm_aesdec_si128(x0, sk[3]);
108*0957b409SSimon J. Gerraty 		x1 = _mm_aesdec_si128(x1, sk[3]);
109*0957b409SSimon J. Gerraty 		x2 = _mm_aesdec_si128(x2, sk[3]);
110*0957b409SSimon J. Gerraty 		x3 = _mm_aesdec_si128(x3, sk[3]);
111*0957b409SSimon J. Gerraty 		x0 = _mm_aesdec_si128(x0, sk[4]);
112*0957b409SSimon J. Gerraty 		x1 = _mm_aesdec_si128(x1, sk[4]);
113*0957b409SSimon J. Gerraty 		x2 = _mm_aesdec_si128(x2, sk[4]);
114*0957b409SSimon J. Gerraty 		x3 = _mm_aesdec_si128(x3, sk[4]);
115*0957b409SSimon J. Gerraty 		x0 = _mm_aesdec_si128(x0, sk[5]);
116*0957b409SSimon J. Gerraty 		x1 = _mm_aesdec_si128(x1, sk[5]);
117*0957b409SSimon J. Gerraty 		x2 = _mm_aesdec_si128(x2, sk[5]);
118*0957b409SSimon J. Gerraty 		x3 = _mm_aesdec_si128(x3, sk[5]);
119*0957b409SSimon J. Gerraty 		x0 = _mm_aesdec_si128(x0, sk[6]);
120*0957b409SSimon J. Gerraty 		x1 = _mm_aesdec_si128(x1, sk[6]);
121*0957b409SSimon J. Gerraty 		x2 = _mm_aesdec_si128(x2, sk[6]);
122*0957b409SSimon J. Gerraty 		x3 = _mm_aesdec_si128(x3, sk[6]);
123*0957b409SSimon J. Gerraty 		x0 = _mm_aesdec_si128(x0, sk[7]);
124*0957b409SSimon J. Gerraty 		x1 = _mm_aesdec_si128(x1, sk[7]);
125*0957b409SSimon J. Gerraty 		x2 = _mm_aesdec_si128(x2, sk[7]);
126*0957b409SSimon J. Gerraty 		x3 = _mm_aesdec_si128(x3, sk[7]);
127*0957b409SSimon J. Gerraty 		x0 = _mm_aesdec_si128(x0, sk[8]);
128*0957b409SSimon J. Gerraty 		x1 = _mm_aesdec_si128(x1, sk[8]);
129*0957b409SSimon J. Gerraty 		x2 = _mm_aesdec_si128(x2, sk[8]);
130*0957b409SSimon J. Gerraty 		x3 = _mm_aesdec_si128(x3, sk[8]);
131*0957b409SSimon J. Gerraty 		x0 = _mm_aesdec_si128(x0, sk[9]);
132*0957b409SSimon J. Gerraty 		x1 = _mm_aesdec_si128(x1, sk[9]);
133*0957b409SSimon J. Gerraty 		x2 = _mm_aesdec_si128(x2, sk[9]);
134*0957b409SSimon J. Gerraty 		x3 = _mm_aesdec_si128(x3, sk[9]);
135*0957b409SSimon J. Gerraty 		if (num_rounds == 10) {
136*0957b409SSimon J. Gerraty 			x0 = _mm_aesdeclast_si128(x0, sk[10]);
137*0957b409SSimon J. Gerraty 			x1 = _mm_aesdeclast_si128(x1, sk[10]);
138*0957b409SSimon J. Gerraty 			x2 = _mm_aesdeclast_si128(x2, sk[10]);
139*0957b409SSimon J. Gerraty 			x3 = _mm_aesdeclast_si128(x3, sk[10]);
140*0957b409SSimon J. Gerraty 		} else if (num_rounds == 12) {
141*0957b409SSimon J. Gerraty 			x0 = _mm_aesdec_si128(x0, sk[10]);
142*0957b409SSimon J. Gerraty 			x1 = _mm_aesdec_si128(x1, sk[10]);
143*0957b409SSimon J. Gerraty 			x2 = _mm_aesdec_si128(x2, sk[10]);
144*0957b409SSimon J. Gerraty 			x3 = _mm_aesdec_si128(x3, sk[10]);
145*0957b409SSimon J. Gerraty 			x0 = _mm_aesdec_si128(x0, sk[11]);
146*0957b409SSimon J. Gerraty 			x1 = _mm_aesdec_si128(x1, sk[11]);
147*0957b409SSimon J. Gerraty 			x2 = _mm_aesdec_si128(x2, sk[11]);
148*0957b409SSimon J. Gerraty 			x3 = _mm_aesdec_si128(x3, sk[11]);
149*0957b409SSimon J. Gerraty 			x0 = _mm_aesdeclast_si128(x0, sk[12]);
150*0957b409SSimon J. Gerraty 			x1 = _mm_aesdeclast_si128(x1, sk[12]);
151*0957b409SSimon J. Gerraty 			x2 = _mm_aesdeclast_si128(x2, sk[12]);
152*0957b409SSimon J. Gerraty 			x3 = _mm_aesdeclast_si128(x3, sk[12]);
153*0957b409SSimon J. Gerraty 		} else {
154*0957b409SSimon J. Gerraty 			x0 = _mm_aesdec_si128(x0, sk[10]);
155*0957b409SSimon J. Gerraty 			x1 = _mm_aesdec_si128(x1, sk[10]);
156*0957b409SSimon J. Gerraty 			x2 = _mm_aesdec_si128(x2, sk[10]);
157*0957b409SSimon J. Gerraty 			x3 = _mm_aesdec_si128(x3, sk[10]);
158*0957b409SSimon J. Gerraty 			x0 = _mm_aesdec_si128(x0, sk[11]);
159*0957b409SSimon J. Gerraty 			x1 = _mm_aesdec_si128(x1, sk[11]);
160*0957b409SSimon J. Gerraty 			x2 = _mm_aesdec_si128(x2, sk[11]);
161*0957b409SSimon J. Gerraty 			x3 = _mm_aesdec_si128(x3, sk[11]);
162*0957b409SSimon J. Gerraty 			x0 = _mm_aesdec_si128(x0, sk[12]);
163*0957b409SSimon J. Gerraty 			x1 = _mm_aesdec_si128(x1, sk[12]);
164*0957b409SSimon J. Gerraty 			x2 = _mm_aesdec_si128(x2, sk[12]);
165*0957b409SSimon J. Gerraty 			x3 = _mm_aesdec_si128(x3, sk[12]);
166*0957b409SSimon J. Gerraty 			x0 = _mm_aesdec_si128(x0, sk[13]);
167*0957b409SSimon J. Gerraty 			x1 = _mm_aesdec_si128(x1, sk[13]);
168*0957b409SSimon J. Gerraty 			x2 = _mm_aesdec_si128(x2, sk[13]);
169*0957b409SSimon J. Gerraty 			x3 = _mm_aesdec_si128(x3, sk[13]);
170*0957b409SSimon J. Gerraty 			x0 = _mm_aesdeclast_si128(x0, sk[14]);
171*0957b409SSimon J. Gerraty 			x1 = _mm_aesdeclast_si128(x1, sk[14]);
172*0957b409SSimon J. Gerraty 			x2 = _mm_aesdeclast_si128(x2, sk[14]);
173*0957b409SSimon J. Gerraty 			x3 = _mm_aesdeclast_si128(x3, sk[14]);
174*0957b409SSimon J. Gerraty 		}
175*0957b409SSimon J. Gerraty 		x0 = _mm_xor_si128(x0, ivx);
176*0957b409SSimon J. Gerraty 		x1 = _mm_xor_si128(x1, e0);
177*0957b409SSimon J. Gerraty 		x2 = _mm_xor_si128(x2, e1);
178*0957b409SSimon J. Gerraty 		x3 = _mm_xor_si128(x3, e2);
179*0957b409SSimon J. Gerraty 		ivx = e3;
180*0957b409SSimon J. Gerraty 		_mm_storeu_si128((void *)(buf +  0), x0);
181*0957b409SSimon J. Gerraty 		if (len >= 64) {
182*0957b409SSimon J. Gerraty 			_mm_storeu_si128((void *)(buf + 16), x1);
183*0957b409SSimon J. Gerraty 			_mm_storeu_si128((void *)(buf + 32), x2);
184*0957b409SSimon J. Gerraty 			_mm_storeu_si128((void *)(buf + 48), x3);
185*0957b409SSimon J. Gerraty 			buf += 64;
186*0957b409SSimon J. Gerraty 			len -= 64;
187*0957b409SSimon J. Gerraty 		} else {
188*0957b409SSimon J. Gerraty 			if (len >= 32) {
189*0957b409SSimon J. Gerraty 				_mm_storeu_si128((void *)(buf + 16), x1);
190*0957b409SSimon J. Gerraty 				if (len >= 48) {
191*0957b409SSimon J. Gerraty 					_mm_storeu_si128(
192*0957b409SSimon J. Gerraty 						(void *)(buf + 32), x2);
193*0957b409SSimon J. Gerraty 				}
194*0957b409SSimon J. Gerraty 			}
195*0957b409SSimon J. Gerraty 			break;
196*0957b409SSimon J. Gerraty 		}
197*0957b409SSimon J. Gerraty 	}
198*0957b409SSimon J. Gerraty 	_mm_storeu_si128(iv, ivx);
199*0957b409SSimon J. Gerraty }
200*0957b409SSimon J. Gerraty 
201*0957b409SSimon J. Gerraty BR_TARGETS_X86_DOWN
202*0957b409SSimon J. Gerraty 
203*0957b409SSimon J. Gerraty /* see bearssl_block.h */
204*0957b409SSimon J. Gerraty const br_block_cbcdec_class br_aes_x86ni_cbcdec_vtable = {
205*0957b409SSimon J. Gerraty 	sizeof(br_aes_x86ni_cbcdec_keys),
206*0957b409SSimon J. Gerraty 	16,
207*0957b409SSimon J. Gerraty 	4,
208*0957b409SSimon J. Gerraty 	(void (*)(const br_block_cbcdec_class **, const void *, size_t))
209*0957b409SSimon J. Gerraty 		&br_aes_x86ni_cbcdec_init,
210*0957b409SSimon J. Gerraty 	(void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
211*0957b409SSimon J. Gerraty 		&br_aes_x86ni_cbcdec_run
212*0957b409SSimon J. Gerraty };
213*0957b409SSimon J. Gerraty 
214*0957b409SSimon J. Gerraty #else
215*0957b409SSimon J. Gerraty 
216*0957b409SSimon J. Gerraty /* see bearssl_block.h */
217*0957b409SSimon J. Gerraty const br_block_cbcdec_class *
br_aes_x86ni_cbcdec_get_vtable(void)218*0957b409SSimon J. Gerraty br_aes_x86ni_cbcdec_get_vtable(void)
219*0957b409SSimon J. Gerraty {
220*0957b409SSimon J. Gerraty 	return NULL;
221*0957b409SSimon J. Gerraty }
222*0957b409SSimon J. Gerraty 
223*0957b409SSimon J. Gerraty #endif
224