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