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