Added "ctmulq" implementation of Poly1305 (using 64->128 multiplications when available).
[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 } else {
82 x2 = x0;
83 }
84 } else {
85 x1 = x0;
86 x2 = x0;
87 }
88 x3 = x0;
89 }
90 e0 = x0;
91 e1 = x1;
92 e2 = x2;
93 e3 = x3;
94 x0 = _mm_xor_si128(x0, sk[0]);
95 x1 = _mm_xor_si128(x1, sk[0]);
96 x2 = _mm_xor_si128(x2, sk[0]);
97 x3 = _mm_xor_si128(x3, sk[0]);
98 x0 = _mm_aesdec_si128(x0, sk[1]);
99 x1 = _mm_aesdec_si128(x1, sk[1]);
100 x2 = _mm_aesdec_si128(x2, sk[1]);
101 x3 = _mm_aesdec_si128(x3, sk[1]);
102 x0 = _mm_aesdec_si128(x0, sk[2]);
103 x1 = _mm_aesdec_si128(x1, sk[2]);
104 x2 = _mm_aesdec_si128(x2, sk[2]);
105 x3 = _mm_aesdec_si128(x3, sk[2]);
106 x0 = _mm_aesdec_si128(x0, sk[3]);
107 x1 = _mm_aesdec_si128(x1, sk[3]);
108 x2 = _mm_aesdec_si128(x2, sk[3]);
109 x3 = _mm_aesdec_si128(x3, sk[3]);
110 x0 = _mm_aesdec_si128(x0, sk[4]);
111 x1 = _mm_aesdec_si128(x1, sk[4]);
112 x2 = _mm_aesdec_si128(x2, sk[4]);
113 x3 = _mm_aesdec_si128(x3, sk[4]);
114 x0 = _mm_aesdec_si128(x0, sk[5]);
115 x1 = _mm_aesdec_si128(x1, sk[5]);
116 x2 = _mm_aesdec_si128(x2, sk[5]);
117 x3 = _mm_aesdec_si128(x3, sk[5]);
118 x0 = _mm_aesdec_si128(x0, sk[6]);
119 x1 = _mm_aesdec_si128(x1, sk[6]);
120 x2 = _mm_aesdec_si128(x2, sk[6]);
121 x3 = _mm_aesdec_si128(x3, sk[6]);
122 x0 = _mm_aesdec_si128(x0, sk[7]);
123 x1 = _mm_aesdec_si128(x1, sk[7]);
124 x2 = _mm_aesdec_si128(x2, sk[7]);
125 x3 = _mm_aesdec_si128(x3, sk[7]);
126 x0 = _mm_aesdec_si128(x0, sk[8]);
127 x1 = _mm_aesdec_si128(x1, sk[8]);
128 x2 = _mm_aesdec_si128(x2, sk[8]);
129 x3 = _mm_aesdec_si128(x3, sk[8]);
130 x0 = _mm_aesdec_si128(x0, sk[9]);
131 x1 = _mm_aesdec_si128(x1, sk[9]);
132 x2 = _mm_aesdec_si128(x2, sk[9]);
133 x3 = _mm_aesdec_si128(x3, sk[9]);
134 if (num_rounds == 10) {
135 x0 = _mm_aesdeclast_si128(x0, sk[10]);
136 x1 = _mm_aesdeclast_si128(x1, sk[10]);
137 x2 = _mm_aesdeclast_si128(x2, sk[10]);
138 x3 = _mm_aesdeclast_si128(x3, sk[10]);
139 } else if (num_rounds == 12) {
140 x0 = _mm_aesdec_si128(x0, sk[10]);
141 x1 = _mm_aesdec_si128(x1, sk[10]);
142 x2 = _mm_aesdec_si128(x2, sk[10]);
143 x3 = _mm_aesdec_si128(x3, sk[10]);
144 x0 = _mm_aesdec_si128(x0, sk[11]);
145 x1 = _mm_aesdec_si128(x1, sk[11]);
146 x2 = _mm_aesdec_si128(x2, sk[11]);
147 x3 = _mm_aesdec_si128(x3, sk[11]);
148 x0 = _mm_aesdeclast_si128(x0, sk[12]);
149 x1 = _mm_aesdeclast_si128(x1, sk[12]);
150 x2 = _mm_aesdeclast_si128(x2, sk[12]);
151 x3 = _mm_aesdeclast_si128(x3, sk[12]);
152 } else {
153 x0 = _mm_aesdec_si128(x0, sk[10]);
154 x1 = _mm_aesdec_si128(x1, sk[10]);
155 x2 = _mm_aesdec_si128(x2, sk[10]);
156 x3 = _mm_aesdec_si128(x3, sk[10]);
157 x0 = _mm_aesdec_si128(x0, sk[11]);
158 x1 = _mm_aesdec_si128(x1, sk[11]);
159 x2 = _mm_aesdec_si128(x2, sk[11]);
160 x3 = _mm_aesdec_si128(x3, sk[11]);
161 x0 = _mm_aesdec_si128(x0, sk[12]);
162 x1 = _mm_aesdec_si128(x1, sk[12]);
163 x2 = _mm_aesdec_si128(x2, sk[12]);
164 x3 = _mm_aesdec_si128(x3, sk[12]);
165 x0 = _mm_aesdec_si128(x0, sk[13]);
166 x1 = _mm_aesdec_si128(x1, sk[13]);
167 x2 = _mm_aesdec_si128(x2, sk[13]);
168 x3 = _mm_aesdec_si128(x3, sk[13]);
169 x0 = _mm_aesdeclast_si128(x0, sk[14]);
170 x1 = _mm_aesdeclast_si128(x1, sk[14]);
171 x2 = _mm_aesdeclast_si128(x2, sk[14]);
172 x3 = _mm_aesdeclast_si128(x3, sk[14]);
173 }
174 x0 = _mm_xor_si128(x0, ivx);
175 x1 = _mm_xor_si128(x1, e0);
176 x2 = _mm_xor_si128(x2, e1);
177 x3 = _mm_xor_si128(x3, e2);
178 ivx = e3;
179 _mm_storeu_si128((void *)(buf + 0), x0);
180 if (len >= 64) {
181 _mm_storeu_si128((void *)(buf + 16), x1);
182 _mm_storeu_si128((void *)(buf + 32), x2);
183 _mm_storeu_si128((void *)(buf + 48), x3);
184 buf += 64;
185 len -= 64;
186 } else {
187 if (len >= 32) {
188 _mm_storeu_si128((void *)(buf + 16), x1);
189 if (len >= 48) {
190 _mm_storeu_si128(
191 (void *)(buf + 32), x2);
192 }
193 }
194 break;
195 }
196 }
197 _mm_storeu_si128(iv, ivx);
198 }
199
200 /* see bearssl_block.h */
201 const br_block_cbcdec_class br_aes_x86ni_cbcdec_vtable = {
202 sizeof(br_aes_x86ni_cbcdec_keys),
203 16,
204 4,
205 (void (*)(const br_block_cbcdec_class **, const void *, size_t))
206 &br_aes_x86ni_cbcdec_init,
207 (void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
208 &br_aes_x86ni_cbcdec_run
209 };
210
211 /* see bearssl_block.h */
212 const br_block_cbcdec_class *
213 br_aes_x86ni_cbcdec_get_vtable(void)
214 {
215 return br_aes_x86ni_supported() ? &br_aes_x86ni_cbcdec_vtable : NULL;
216 }
217
218 #else
219
220 /* see bearssl_block.h */
221 const br_block_cbcdec_class *
222 br_aes_x86ni_cbcdec_get_vtable(void)
223 {
224 return NULL;
225 }
226
227 #endif