aes_bear.c revision 1.4 1 1.4 riastrad /* $NetBSD: aes_bear.c,v 1.4 2020/07/25 22:28:27 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #include <sys/cdefs.h>
30 1.4 riastrad __KERNEL_RCSID(1, "$NetBSD: aes_bear.c,v 1.4 2020/07/25 22:28:27 riastradh Exp $");
31 1.1 riastrad
32 1.1 riastrad #include <sys/types.h>
33 1.1 riastrad #include <sys/endian.h>
34 1.2 riastrad
35 1.2 riastrad #ifdef _KERNEL
36 1.1 riastrad #include <sys/systm.h>
37 1.2 riastrad #else
38 1.2 riastrad #include <assert.h>
39 1.2 riastrad #include <err.h>
40 1.2 riastrad #include <string.h>
41 1.2 riastrad #define KASSERT assert
42 1.2 riastrad #define panic(fmt, args...) err(1, fmt, args)
43 1.2 riastrad #endif
44 1.1 riastrad
45 1.1 riastrad #include <crypto/aes/aes.h>
46 1.1 riastrad #include <crypto/aes/aes_bear.h>
47 1.3 riastrad #include <crypto/aes/aes_impl.h>
48 1.1 riastrad
49 1.1 riastrad static void
50 1.1 riastrad aesbear_setkey(uint32_t rk[static 60], const void *key, uint32_t nrounds)
51 1.1 riastrad {
52 1.1 riastrad size_t key_len;
53 1.1 riastrad
54 1.1 riastrad switch (nrounds) {
55 1.1 riastrad case 10:
56 1.1 riastrad key_len = 16;
57 1.1 riastrad break;
58 1.1 riastrad case 12:
59 1.1 riastrad key_len = 24;
60 1.1 riastrad break;
61 1.1 riastrad case 14:
62 1.1 riastrad key_len = 32;
63 1.1 riastrad break;
64 1.1 riastrad default:
65 1.1 riastrad panic("invalid AES nrounds: %u", nrounds);
66 1.1 riastrad }
67 1.1 riastrad
68 1.1 riastrad br_aes_ct_keysched(rk, key, key_len);
69 1.1 riastrad }
70 1.1 riastrad
71 1.1 riastrad static void
72 1.1 riastrad aesbear_setenckey(struct aesenc *enc, const uint8_t *key, uint32_t nrounds)
73 1.1 riastrad {
74 1.1 riastrad
75 1.1 riastrad aesbear_setkey(enc->aese_aes.aes_rk, key, nrounds);
76 1.1 riastrad }
77 1.1 riastrad
78 1.1 riastrad static void
79 1.1 riastrad aesbear_setdeckey(struct aesdec *dec, const uint8_t *key, uint32_t nrounds)
80 1.1 riastrad {
81 1.1 riastrad
82 1.1 riastrad /*
83 1.1 riastrad * BearSSL computes InvMixColumns on the fly -- no need for
84 1.1 riastrad * distinct decryption round keys.
85 1.1 riastrad */
86 1.1 riastrad aesbear_setkey(dec->aesd_aes.aes_rk, key, nrounds);
87 1.1 riastrad }
88 1.1 riastrad
89 1.1 riastrad static void
90 1.1 riastrad aesbear_enc(const struct aesenc *enc, const uint8_t in[static 16],
91 1.1 riastrad uint8_t out[static 16], uint32_t nrounds)
92 1.1 riastrad {
93 1.1 riastrad uint32_t sk_exp[120];
94 1.1 riastrad uint32_t q[8];
95 1.1 riastrad
96 1.1 riastrad /* Expand round keys for bitslicing. */
97 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk);
98 1.1 riastrad
99 1.1 riastrad /* Load input block interleaved with garbage block. */
100 1.1 riastrad q[2*0] = le32dec(in + 4*0);
101 1.1 riastrad q[2*1] = le32dec(in + 4*1);
102 1.1 riastrad q[2*2] = le32dec(in + 4*2);
103 1.1 riastrad q[2*3] = le32dec(in + 4*3);
104 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
105 1.1 riastrad
106 1.1 riastrad /* Transform to bitslice, decrypt, transform from bitslice. */
107 1.1 riastrad br_aes_ct_ortho(q);
108 1.1 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
109 1.1 riastrad br_aes_ct_ortho(q);
110 1.1 riastrad
111 1.1 riastrad /* Store output block. */
112 1.1 riastrad le32enc(out + 4*0, q[2*0]);
113 1.1 riastrad le32enc(out + 4*1, q[2*1]);
114 1.1 riastrad le32enc(out + 4*2, q[2*2]);
115 1.1 riastrad le32enc(out + 4*3, q[2*3]);
116 1.1 riastrad
117 1.1 riastrad /* Paranoia: Zero temporary buffers. */
118 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
119 1.1 riastrad explicit_memset(q, 0, sizeof q);
120 1.1 riastrad }
121 1.1 riastrad
122 1.1 riastrad static void
123 1.1 riastrad aesbear_dec(const struct aesdec *dec, const uint8_t in[static 16],
124 1.1 riastrad uint8_t out[static 16], uint32_t nrounds)
125 1.1 riastrad {
126 1.1 riastrad uint32_t sk_exp[120];
127 1.1 riastrad uint32_t q[8];
128 1.1 riastrad
129 1.1 riastrad /* Expand round keys for bitslicing. */
130 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, dec->aesd_aes.aes_rk);
131 1.1 riastrad
132 1.1 riastrad /* Load input block interleaved with garbage. */
133 1.1 riastrad q[2*0] = le32dec(in + 4*0);
134 1.1 riastrad q[2*1] = le32dec(in + 4*1);
135 1.1 riastrad q[2*2] = le32dec(in + 4*2);
136 1.1 riastrad q[2*3] = le32dec(in + 4*3);
137 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
138 1.1 riastrad
139 1.1 riastrad /* Transform to bitslice, decrypt, transform from bitslice. */
140 1.1 riastrad br_aes_ct_ortho(q);
141 1.1 riastrad br_aes_ct_bitslice_decrypt(nrounds, sk_exp, q);
142 1.1 riastrad br_aes_ct_ortho(q);
143 1.1 riastrad
144 1.1 riastrad /* Store output block. */
145 1.1 riastrad le32enc(out + 4*0, q[2*0]);
146 1.1 riastrad le32enc(out + 4*1, q[2*1]);
147 1.1 riastrad le32enc(out + 4*2, q[2*2]);
148 1.1 riastrad le32enc(out + 4*3, q[2*3]);
149 1.1 riastrad
150 1.1 riastrad /* Paranoia: Zero temporary buffers. */
151 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
152 1.1 riastrad explicit_memset(q, 0, sizeof q);
153 1.1 riastrad }
154 1.1 riastrad
155 1.1 riastrad static void
156 1.1 riastrad aesbear_cbc_enc(const struct aesenc *enc, const uint8_t in[static 16],
157 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
158 1.1 riastrad uint32_t nrounds)
159 1.1 riastrad {
160 1.1 riastrad uint32_t sk_exp[120];
161 1.1 riastrad uint32_t q[8];
162 1.1 riastrad uint32_t cv0, cv1, cv2, cv3;
163 1.1 riastrad
164 1.1 riastrad KASSERT(nbytes % 16 == 0);
165 1.1 riastrad
166 1.1 riastrad /* Skip if there's nothing to do. */
167 1.1 riastrad if (nbytes == 0)
168 1.1 riastrad return;
169 1.1 riastrad
170 1.1 riastrad /* Expand round keys for bitslicing. */
171 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk);
172 1.1 riastrad
173 1.1 riastrad /* Initialize garbage block. */
174 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
175 1.1 riastrad
176 1.1 riastrad /* Load IV. */
177 1.1 riastrad cv0 = le32dec(iv + 4*0);
178 1.1 riastrad cv1 = le32dec(iv + 4*1);
179 1.1 riastrad cv2 = le32dec(iv + 4*2);
180 1.1 riastrad cv3 = le32dec(iv + 4*3);
181 1.1 riastrad
182 1.1 riastrad for (; nbytes; nbytes -= 16, in += 16, out += 16) {
183 1.1 riastrad /* Load input block and apply CV. */
184 1.1 riastrad q[2*0] = cv0 ^ le32dec(in + 4*0);
185 1.1 riastrad q[2*1] = cv1 ^ le32dec(in + 4*1);
186 1.1 riastrad q[2*2] = cv2 ^ le32dec(in + 4*2);
187 1.1 riastrad q[2*3] = cv3 ^ le32dec(in + 4*3);
188 1.1 riastrad
189 1.1 riastrad /* Transform to bitslice, encrypt, transform from bitslice. */
190 1.1 riastrad br_aes_ct_ortho(q);
191 1.1 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
192 1.1 riastrad br_aes_ct_ortho(q);
193 1.1 riastrad
194 1.1 riastrad /* Remember ciphertext as CV and store output block. */
195 1.1 riastrad cv0 = q[2*0];
196 1.1 riastrad cv1 = q[2*1];
197 1.1 riastrad cv2 = q[2*2];
198 1.1 riastrad cv3 = q[2*3];
199 1.1 riastrad le32enc(out + 4*0, cv0);
200 1.1 riastrad le32enc(out + 4*1, cv1);
201 1.1 riastrad le32enc(out + 4*2, cv2);
202 1.1 riastrad le32enc(out + 4*3, cv3);
203 1.1 riastrad }
204 1.1 riastrad
205 1.1 riastrad /* Store updated IV. */
206 1.1 riastrad le32enc(iv + 4*0, cv0);
207 1.1 riastrad le32enc(iv + 4*1, cv1);
208 1.1 riastrad le32enc(iv + 4*2, cv2);
209 1.1 riastrad le32enc(iv + 4*3, cv3);
210 1.1 riastrad
211 1.1 riastrad /* Paranoia: Zero temporary buffers. */
212 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
213 1.1 riastrad explicit_memset(q, 0, sizeof q);
214 1.1 riastrad }
215 1.1 riastrad
216 1.1 riastrad static void
217 1.1 riastrad aesbear_cbc_dec(const struct aesdec *dec, const uint8_t in[static 16],
218 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
219 1.1 riastrad uint32_t nrounds)
220 1.1 riastrad {
221 1.1 riastrad uint32_t sk_exp[120];
222 1.1 riastrad uint32_t q[8];
223 1.1 riastrad uint32_t cv0, cv1, cv2, cv3, iv0, iv1, iv2, iv3;
224 1.1 riastrad
225 1.1 riastrad KASSERT(nbytes % 16 == 0);
226 1.1 riastrad
227 1.1 riastrad /* Skip if there's nothing to do. */
228 1.1 riastrad if (nbytes == 0)
229 1.1 riastrad return;
230 1.1 riastrad
231 1.1 riastrad /* Expand round keys for bitslicing. */
232 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, dec->aesd_aes.aes_rk);
233 1.1 riastrad
234 1.1 riastrad /* Load the IV. */
235 1.1 riastrad iv0 = le32dec(iv + 4*0);
236 1.1 riastrad iv1 = le32dec(iv + 4*1);
237 1.1 riastrad iv2 = le32dec(iv + 4*2);
238 1.1 riastrad iv3 = le32dec(iv + 4*3);
239 1.1 riastrad
240 1.1 riastrad /* Load the last cipher block. */
241 1.1 riastrad cv0 = le32dec(in + nbytes - 16 + 4*0);
242 1.1 riastrad cv1 = le32dec(in + nbytes - 16 + 4*1);
243 1.1 riastrad cv2 = le32dec(in + nbytes - 16 + 4*2);
244 1.1 riastrad cv3 = le32dec(in + nbytes - 16 + 4*3);
245 1.1 riastrad
246 1.1 riastrad /* Store the updated IV. */
247 1.1 riastrad le32enc(iv + 4*0, cv0);
248 1.1 riastrad le32enc(iv + 4*1, cv1);
249 1.1 riastrad le32enc(iv + 4*2, cv2);
250 1.1 riastrad le32enc(iv + 4*3, cv3);
251 1.1 riastrad
252 1.1 riastrad /* Handle the last cipher block separately if odd number. */
253 1.1 riastrad if (nbytes % 32) {
254 1.1 riastrad KASSERT(nbytes % 32 == 16);
255 1.1 riastrad
256 1.1 riastrad /* Set up the last cipher block and a garbage block. */
257 1.1 riastrad q[2*0] = cv0;
258 1.1 riastrad q[2*1] = cv1;
259 1.1 riastrad q[2*2] = cv2;
260 1.1 riastrad q[2*3] = cv3;
261 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
262 1.1 riastrad
263 1.1 riastrad /* Decrypt. */
264 1.1 riastrad br_aes_ct_ortho(q);
265 1.1 riastrad br_aes_ct_bitslice_decrypt(nrounds, sk_exp, q);
266 1.1 riastrad br_aes_ct_ortho(q);
267 1.1 riastrad
268 1.1 riastrad /* If this was the only cipher block, we're done. */
269 1.1 riastrad nbytes -= 16;
270 1.1 riastrad if (nbytes == 0)
271 1.1 riastrad goto out;
272 1.1 riastrad
273 1.1 riastrad /*
274 1.1 riastrad * Otherwise, load up the penultimate cipher block, and
275 1.1 riastrad * store the output block.
276 1.1 riastrad */
277 1.1 riastrad cv0 = le32dec(in + nbytes - 16 + 4*0);
278 1.1 riastrad cv1 = le32dec(in + nbytes - 16 + 4*1);
279 1.1 riastrad cv2 = le32dec(in + nbytes - 16 + 4*2);
280 1.1 riastrad cv3 = le32dec(in + nbytes - 16 + 4*3);
281 1.1 riastrad le32enc(out + nbytes + 4*0, cv0 ^ q[2*0]);
282 1.1 riastrad le32enc(out + nbytes + 4*1, cv1 ^ q[2*1]);
283 1.1 riastrad le32enc(out + nbytes + 4*2, cv2 ^ q[2*2]);
284 1.1 riastrad le32enc(out + nbytes + 4*3, cv3 ^ q[2*3]);
285 1.1 riastrad }
286 1.1 riastrad
287 1.1 riastrad for (;;) {
288 1.1 riastrad KASSERT(nbytes >= 32);
289 1.1 riastrad
290 1.1 riastrad /*
291 1.1 riastrad * 1. Set up upper cipher block from cvN.
292 1.1 riastrad * 2. Load lower cipher block into cvN and set it up.
293 1.1 riastrad * 3. Decrypt.
294 1.1 riastrad */
295 1.1 riastrad q[2*0 + 1] = cv0;
296 1.1 riastrad q[2*1 + 1] = cv1;
297 1.1 riastrad q[2*2 + 1] = cv2;
298 1.1 riastrad q[2*3 + 1] = cv3;
299 1.1 riastrad cv0 = q[2*0] = le32dec(in + nbytes - 32 + 4*0);
300 1.1 riastrad cv1 = q[2*1] = le32dec(in + nbytes - 32 + 4*1);
301 1.1 riastrad cv2 = q[2*2] = le32dec(in + nbytes - 32 + 4*2);
302 1.1 riastrad cv3 = q[2*3] = le32dec(in + nbytes - 32 + 4*3);
303 1.1 riastrad
304 1.1 riastrad br_aes_ct_ortho(q);
305 1.1 riastrad br_aes_ct_bitslice_decrypt(nrounds, sk_exp, q);
306 1.1 riastrad br_aes_ct_ortho(q);
307 1.1 riastrad
308 1.1 riastrad /* Store the upper output block. */
309 1.1 riastrad le32enc(out + nbytes - 16 + 4*0, q[2*0 + 1] ^ cv0);
310 1.1 riastrad le32enc(out + nbytes - 16 + 4*1, q[2*1 + 1] ^ cv1);
311 1.1 riastrad le32enc(out + nbytes - 16 + 4*2, q[2*2 + 1] ^ cv2);
312 1.1 riastrad le32enc(out + nbytes - 16 + 4*3, q[2*3 + 1] ^ cv3);
313 1.1 riastrad
314 1.1 riastrad /* Stop if we've reached the first output block. */
315 1.1 riastrad nbytes -= 32;
316 1.1 riastrad if (nbytes == 0)
317 1.1 riastrad goto out;
318 1.1 riastrad
319 1.1 riastrad /*
320 1.1 riastrad * Load the preceding cipher block, and apply it as the
321 1.1 riastrad * chaining value to this one.
322 1.1 riastrad */
323 1.1 riastrad cv0 = le32dec(in + nbytes - 16 + 4*0);
324 1.1 riastrad cv1 = le32dec(in + nbytes - 16 + 4*1);
325 1.1 riastrad cv2 = le32dec(in + nbytes - 16 + 4*2);
326 1.1 riastrad cv3 = le32dec(in + nbytes - 16 + 4*3);
327 1.1 riastrad le32enc(out + nbytes + 4*0, q[2*0] ^ cv0);
328 1.1 riastrad le32enc(out + nbytes + 4*1, q[2*1] ^ cv1);
329 1.1 riastrad le32enc(out + nbytes + 4*2, q[2*2] ^ cv2);
330 1.1 riastrad le32enc(out + nbytes + 4*3, q[2*3] ^ cv3);
331 1.1 riastrad }
332 1.1 riastrad
333 1.1 riastrad out: /* Store the first output block. */
334 1.1 riastrad le32enc(out + 4*0, q[2*0] ^ iv0);
335 1.1 riastrad le32enc(out + 4*1, q[2*1] ^ iv1);
336 1.1 riastrad le32enc(out + 4*2, q[2*2] ^ iv2);
337 1.1 riastrad le32enc(out + 4*3, q[2*3] ^ iv3);
338 1.1 riastrad
339 1.1 riastrad /* Paranoia: Zero temporary buffers. */
340 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
341 1.1 riastrad explicit_memset(q, 0, sizeof q);
342 1.1 riastrad }
343 1.1 riastrad
344 1.1 riastrad static inline void
345 1.1 riastrad aesbear_xts_update(uint32_t *t0, uint32_t *t1, uint32_t *t2, uint32_t *t3)
346 1.1 riastrad {
347 1.1 riastrad uint32_t s0, s1, s2, s3;
348 1.1 riastrad
349 1.1 riastrad s0 = *t0 >> 31;
350 1.1 riastrad s1 = *t1 >> 31;
351 1.1 riastrad s2 = *t2 >> 31;
352 1.1 riastrad s3 = *t3 >> 31;
353 1.1 riastrad *t0 = (*t0 << 1) ^ (-s3 & 0x87);
354 1.1 riastrad *t1 = (*t1 << 1) ^ s0;
355 1.1 riastrad *t2 = (*t2 << 1) ^ s1;
356 1.1 riastrad *t3 = (*t3 << 1) ^ s2;
357 1.1 riastrad }
358 1.1 riastrad
359 1.1 riastrad static int
360 1.1 riastrad aesbear_xts_update_selftest(void)
361 1.1 riastrad {
362 1.1 riastrad static const struct {
363 1.1 riastrad uint32_t in[4], out[4];
364 1.1 riastrad } cases[] = {
365 1.1 riastrad { {1}, {2} },
366 1.1 riastrad { {0x80000000U,0,0,0}, {0,1,0,0} },
367 1.1 riastrad { {0,0x80000000U,0,0}, {0,0,1,0} },
368 1.1 riastrad { {0,0,0x80000000U,0}, {0,0,0,1} },
369 1.1 riastrad { {0,0,0,0x80000000U}, {0x87,0,0,0} },
370 1.1 riastrad { {0,0x80000000U,0,0x80000000U}, {0x87,0,1,0} },
371 1.1 riastrad };
372 1.1 riastrad unsigned i;
373 1.1 riastrad uint32_t t0, t1, t2, t3;
374 1.1 riastrad
375 1.1 riastrad for (i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) {
376 1.1 riastrad t0 = cases[i].in[0];
377 1.1 riastrad t1 = cases[i].in[1];
378 1.1 riastrad t2 = cases[i].in[2];
379 1.1 riastrad t3 = cases[i].in[3];
380 1.1 riastrad aesbear_xts_update(&t0, &t1, &t2, &t3);
381 1.1 riastrad if (t0 != cases[i].out[0] ||
382 1.1 riastrad t1 != cases[i].out[1] ||
383 1.1 riastrad t2 != cases[i].out[2] ||
384 1.1 riastrad t3 != cases[i].out[3])
385 1.1 riastrad return -1;
386 1.1 riastrad }
387 1.1 riastrad
388 1.1 riastrad /* Success! */
389 1.1 riastrad return 0;
390 1.1 riastrad }
391 1.1 riastrad
392 1.1 riastrad static void
393 1.1 riastrad aesbear_xts_enc(const struct aesenc *enc, const uint8_t in[static 16],
394 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
395 1.1 riastrad uint32_t nrounds)
396 1.1 riastrad {
397 1.1 riastrad uint32_t sk_exp[120];
398 1.1 riastrad uint32_t q[8];
399 1.1 riastrad uint32_t t0, t1, t2, t3, u0, u1, u2, u3;
400 1.1 riastrad
401 1.1 riastrad KASSERT(nbytes % 16 == 0);
402 1.1 riastrad
403 1.1 riastrad /* Skip if there's nothing to do. */
404 1.1 riastrad if (nbytes == 0)
405 1.1 riastrad return;
406 1.1 riastrad
407 1.1 riastrad /* Expand round keys for bitslicing. */
408 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk);
409 1.1 riastrad
410 1.1 riastrad /* Load tweak. */
411 1.1 riastrad t0 = le32dec(tweak + 4*0);
412 1.1 riastrad t1 = le32dec(tweak + 4*1);
413 1.1 riastrad t2 = le32dec(tweak + 4*2);
414 1.1 riastrad t3 = le32dec(tweak + 4*3);
415 1.1 riastrad
416 1.1 riastrad /* Handle the first block separately if odd number. */
417 1.1 riastrad if (nbytes % 32) {
418 1.1 riastrad KASSERT(nbytes % 32 == 16);
419 1.1 riastrad
420 1.1 riastrad /* Load up the first block and a garbage block. */
421 1.1 riastrad q[2*0] = le32dec(in + 4*0) ^ t0;
422 1.1 riastrad q[2*1] = le32dec(in + 4*1) ^ t1;
423 1.1 riastrad q[2*2] = le32dec(in + 4*2) ^ t2;
424 1.1 riastrad q[2*3] = le32dec(in + 4*3) ^ t3;
425 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
426 1.1 riastrad
427 1.1 riastrad /* Encrypt two blocks. */
428 1.1 riastrad br_aes_ct_ortho(q);
429 1.1 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
430 1.1 riastrad br_aes_ct_ortho(q);
431 1.1 riastrad
432 1.1 riastrad /* Store the first cipher block. */
433 1.1 riastrad le32enc(out + 4*0, q[2*0] ^ t0);
434 1.1 riastrad le32enc(out + 4*1, q[2*1] ^ t1);
435 1.1 riastrad le32enc(out + 4*2, q[2*2] ^ t2);
436 1.1 riastrad le32enc(out + 4*3, q[2*3] ^ t3);
437 1.1 riastrad
438 1.1 riastrad /* Advance to the next block. */
439 1.1 riastrad aesbear_xts_update(&t0, &t1, &t2, &t3);
440 1.1 riastrad if ((nbytes -= 16) == 0)
441 1.1 riastrad goto out;
442 1.1 riastrad in += 16;
443 1.1 riastrad out += 16;
444 1.1 riastrad }
445 1.1 riastrad
446 1.1 riastrad do {
447 1.1 riastrad KASSERT(nbytes >= 32);
448 1.1 riastrad
449 1.1 riastrad /* Compute the upper tweak. */
450 1.1 riastrad u0 = t0; u1 = t1; u2 = t2; u3 = t3;
451 1.1 riastrad aesbear_xts_update(&u0, &u1, &u2, &u3);
452 1.1 riastrad
453 1.1 riastrad /* Load lower and upper blocks. */
454 1.1 riastrad q[2*0] = le32dec(in + 4*0) ^ t0;
455 1.1 riastrad q[2*1] = le32dec(in + 4*1) ^ t1;
456 1.1 riastrad q[2*2] = le32dec(in + 4*2) ^ t2;
457 1.1 riastrad q[2*3] = le32dec(in + 4*3) ^ t3;
458 1.1 riastrad q[2*0 + 1] = le32dec(in + 16 + 4*0) ^ u0;
459 1.1 riastrad q[2*1 + 1] = le32dec(in + 16 + 4*1) ^ u1;
460 1.1 riastrad q[2*2 + 1] = le32dec(in + 16 + 4*2) ^ u2;
461 1.1 riastrad q[2*3 + 1] = le32dec(in + 16 + 4*3) ^ u3;
462 1.1 riastrad
463 1.1 riastrad /* Encrypt two blocks. */
464 1.1 riastrad br_aes_ct_ortho(q);
465 1.1 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
466 1.1 riastrad br_aes_ct_ortho(q);
467 1.1 riastrad
468 1.1 riastrad /* Store lower and upper blocks. */
469 1.1 riastrad le32enc(out + 4*0, q[2*0] ^ t0);
470 1.1 riastrad le32enc(out + 4*1, q[2*1] ^ t1);
471 1.1 riastrad le32enc(out + 4*2, q[2*2] ^ t2);
472 1.1 riastrad le32enc(out + 4*3, q[2*3] ^ t3);
473 1.1 riastrad le32enc(out + 16 + 4*0, q[2*0 + 1] ^ u0);
474 1.1 riastrad le32enc(out + 16 + 4*1, q[2*1 + 1] ^ u1);
475 1.1 riastrad le32enc(out + 16 + 4*2, q[2*2 + 1] ^ u2);
476 1.1 riastrad le32enc(out + 16 + 4*3, q[2*3 + 1] ^ u3);
477 1.1 riastrad
478 1.1 riastrad /* Advance to the next pair of blocks. */
479 1.1 riastrad t0 = u0; t1 = u1; t2 = u2; t3 = u3;
480 1.1 riastrad aesbear_xts_update(&t0, &t1, &t2, &t3);
481 1.1 riastrad in += 32;
482 1.1 riastrad out += 32;
483 1.1 riastrad } while (nbytes -= 32, nbytes);
484 1.1 riastrad
485 1.1 riastrad out: /* Store the updated tweak. */
486 1.1 riastrad le32enc(tweak + 4*0, t0);
487 1.1 riastrad le32enc(tweak + 4*1, t1);
488 1.1 riastrad le32enc(tweak + 4*2, t2);
489 1.1 riastrad le32enc(tweak + 4*3, t3);
490 1.1 riastrad
491 1.1 riastrad /* Paranoia: Zero temporary buffers. */
492 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
493 1.1 riastrad explicit_memset(q, 0, sizeof q);
494 1.1 riastrad }
495 1.1 riastrad
496 1.1 riastrad static void
497 1.1 riastrad aesbear_xts_dec(const struct aesdec *dec, const uint8_t in[static 16],
498 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
499 1.1 riastrad uint32_t nrounds)
500 1.1 riastrad {
501 1.1 riastrad uint32_t sk_exp[120];
502 1.1 riastrad uint32_t q[8];
503 1.1 riastrad uint32_t t0, t1, t2, t3, u0, u1, u2, u3;
504 1.1 riastrad
505 1.1 riastrad KASSERT(nbytes % 16 == 0);
506 1.1 riastrad
507 1.1 riastrad /* Skip if there's nothing to do. */
508 1.1 riastrad if (nbytes == 0)
509 1.1 riastrad return;
510 1.1 riastrad
511 1.1 riastrad /* Expand round keys for bitslicing. */
512 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, dec->aesd_aes.aes_rk);
513 1.1 riastrad
514 1.1 riastrad /* Load tweak. */
515 1.1 riastrad t0 = le32dec(tweak + 4*0);
516 1.1 riastrad t1 = le32dec(tweak + 4*1);
517 1.1 riastrad t2 = le32dec(tweak + 4*2);
518 1.1 riastrad t3 = le32dec(tweak + 4*3);
519 1.1 riastrad
520 1.1 riastrad /* Handle the first block separately if odd number. */
521 1.1 riastrad if (nbytes % 32) {
522 1.1 riastrad KASSERT(nbytes % 32 == 16);
523 1.1 riastrad
524 1.1 riastrad /* Load up the first block and a garbage block. */
525 1.1 riastrad q[2*0] = le32dec(in + 4*0) ^ t0;
526 1.1 riastrad q[2*1] = le32dec(in + 4*1) ^ t1;
527 1.1 riastrad q[2*2] = le32dec(in + 4*2) ^ t2;
528 1.1 riastrad q[2*3] = le32dec(in + 4*3) ^ t3;
529 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
530 1.1 riastrad
531 1.1 riastrad /* Decrypt two blocks. */
532 1.1 riastrad br_aes_ct_ortho(q);
533 1.1 riastrad br_aes_ct_bitslice_decrypt(nrounds, sk_exp, q);
534 1.1 riastrad br_aes_ct_ortho(q);
535 1.1 riastrad
536 1.1 riastrad /* Store the first cipher block. */
537 1.1 riastrad le32enc(out + 4*0, q[2*0] ^ t0);
538 1.1 riastrad le32enc(out + 4*1, q[2*1] ^ t1);
539 1.1 riastrad le32enc(out + 4*2, q[2*2] ^ t2);
540 1.1 riastrad le32enc(out + 4*3, q[2*3] ^ t3);
541 1.1 riastrad
542 1.1 riastrad /* Advance to the next block. */
543 1.1 riastrad aesbear_xts_update(&t0, &t1, &t2, &t3);
544 1.1 riastrad if ((nbytes -= 16) == 0)
545 1.1 riastrad goto out;
546 1.1 riastrad in += 16;
547 1.1 riastrad out += 16;
548 1.1 riastrad }
549 1.1 riastrad
550 1.1 riastrad do {
551 1.1 riastrad KASSERT(nbytes >= 32);
552 1.1 riastrad
553 1.1 riastrad /* Compute the upper tweak. */
554 1.1 riastrad u0 = t0; u1 = t1; u2 = t2; u3 = t3;
555 1.1 riastrad aesbear_xts_update(&u0, &u1, &u2, &u3);
556 1.1 riastrad
557 1.1 riastrad /* Load lower and upper blocks. */
558 1.1 riastrad q[2*0] = le32dec(in + 4*0) ^ t0;
559 1.1 riastrad q[2*1] = le32dec(in + 4*1) ^ t1;
560 1.1 riastrad q[2*2] = le32dec(in + 4*2) ^ t2;
561 1.1 riastrad q[2*3] = le32dec(in + 4*3) ^ t3;
562 1.1 riastrad q[2*0 + 1] = le32dec(in + 16 + 4*0) ^ u0;
563 1.1 riastrad q[2*1 + 1] = le32dec(in + 16 + 4*1) ^ u1;
564 1.1 riastrad q[2*2 + 1] = le32dec(in + 16 + 4*2) ^ u2;
565 1.1 riastrad q[2*3 + 1] = le32dec(in + 16 + 4*3) ^ u3;
566 1.1 riastrad
567 1.1 riastrad /* Encrypt two blocks. */
568 1.1 riastrad br_aes_ct_ortho(q);
569 1.1 riastrad br_aes_ct_bitslice_decrypt(nrounds, sk_exp, q);
570 1.1 riastrad br_aes_ct_ortho(q);
571 1.1 riastrad
572 1.1 riastrad /* Store lower and upper blocks. */
573 1.1 riastrad le32enc(out + 4*0, q[2*0] ^ t0);
574 1.1 riastrad le32enc(out + 4*1, q[2*1] ^ t1);
575 1.1 riastrad le32enc(out + 4*2, q[2*2] ^ t2);
576 1.1 riastrad le32enc(out + 4*3, q[2*3] ^ t3);
577 1.1 riastrad le32enc(out + 16 + 4*0, q[2*0 + 1] ^ u0);
578 1.1 riastrad le32enc(out + 16 + 4*1, q[2*1 + 1] ^ u1);
579 1.1 riastrad le32enc(out + 16 + 4*2, q[2*2 + 1] ^ u2);
580 1.1 riastrad le32enc(out + 16 + 4*3, q[2*3 + 1] ^ u3);
581 1.1 riastrad
582 1.1 riastrad /* Advance to the next pair of blocks. */
583 1.1 riastrad t0 = u0; t1 = u1; t2 = u2; t3 = u3;
584 1.1 riastrad aesbear_xts_update(&t0, &t1, &t2, &t3);
585 1.1 riastrad in += 32;
586 1.1 riastrad out += 32;
587 1.1 riastrad } while (nbytes -= 32, nbytes);
588 1.1 riastrad
589 1.1 riastrad out: /* Store the updated tweak. */
590 1.1 riastrad le32enc(tweak + 4*0, t0);
591 1.1 riastrad le32enc(tweak + 4*1, t1);
592 1.1 riastrad le32enc(tweak + 4*2, t2);
593 1.1 riastrad le32enc(tweak + 4*3, t3);
594 1.1 riastrad
595 1.1 riastrad /* Paranoia: Zero temporary buffers. */
596 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
597 1.1 riastrad explicit_memset(q, 0, sizeof q);
598 1.1 riastrad }
599 1.1 riastrad
600 1.4 riastrad static void
601 1.4 riastrad aesbear_cbcmac_update1(const struct aesenc *enc, const uint8_t in[static 16],
602 1.4 riastrad size_t nbytes, uint8_t auth[static 16], uint32_t nrounds)
603 1.4 riastrad {
604 1.4 riastrad uint32_t sk_exp[120];
605 1.4 riastrad uint32_t q[8];
606 1.4 riastrad
607 1.4 riastrad KASSERT(nbytes);
608 1.4 riastrad KASSERT(nbytes % 16 == 0);
609 1.4 riastrad
610 1.4 riastrad /* Expand round keys for bitslicing. */
611 1.4 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk);
612 1.4 riastrad
613 1.4 riastrad /* Initialize garbage block. */
614 1.4 riastrad q[1] = q[3] = q[5] = q[7] = 0;
615 1.4 riastrad
616 1.4 riastrad /* Load initial authenticator. */
617 1.4 riastrad q[2*0] = le32dec(auth + 4*0);
618 1.4 riastrad q[2*1] = le32dec(auth + 4*1);
619 1.4 riastrad q[2*2] = le32dec(auth + 4*2);
620 1.4 riastrad q[2*3] = le32dec(auth + 4*3);
621 1.4 riastrad
622 1.4 riastrad for (; nbytes; nbytes -= 16, in += 16) {
623 1.4 riastrad /* Combine input block. */
624 1.4 riastrad q[2*0] ^= le32dec(in + 4*0);
625 1.4 riastrad q[2*1] ^= le32dec(in + 4*1);
626 1.4 riastrad q[2*2] ^= le32dec(in + 4*2);
627 1.4 riastrad q[2*3] ^= le32dec(in + 4*3);
628 1.4 riastrad
629 1.4 riastrad /* Transform to bitslice, encrypt, transform from bitslice. */
630 1.4 riastrad br_aes_ct_ortho(q);
631 1.4 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
632 1.4 riastrad br_aes_ct_ortho(q);
633 1.4 riastrad }
634 1.4 riastrad
635 1.4 riastrad /* Store updated authenticator. */
636 1.4 riastrad le32enc(auth + 4*0, q[2*0]);
637 1.4 riastrad le32enc(auth + 4*1, q[2*1]);
638 1.4 riastrad le32enc(auth + 4*2, q[2*2]);
639 1.4 riastrad le32enc(auth + 4*3, q[2*3]);
640 1.4 riastrad
641 1.4 riastrad /* Paranoia: Zero temporary buffers. */
642 1.4 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
643 1.4 riastrad explicit_memset(q, 0, sizeof q);
644 1.4 riastrad }
645 1.4 riastrad
646 1.4 riastrad static void
647 1.4 riastrad aesbear_ccm_enc1(const struct aesenc *enc, const uint8_t *in, uint8_t *out,
648 1.4 riastrad size_t nbytes, uint8_t authctr[32], uint32_t nrounds)
649 1.4 riastrad {
650 1.4 riastrad uint32_t sk_exp[120];
651 1.4 riastrad uint32_t q[8];
652 1.4 riastrad uint32_t c0, c1, c2, c3;
653 1.4 riastrad
654 1.4 riastrad KASSERT(nbytes);
655 1.4 riastrad KASSERT(nbytes % 16 == 0);
656 1.4 riastrad
657 1.4 riastrad /* Expand round keys for bitslicing. */
658 1.4 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk);
659 1.4 riastrad
660 1.4 riastrad /* Set first block to authenticator. */
661 1.4 riastrad q[2*0] = le32dec(authctr + 4*0);
662 1.4 riastrad q[2*1] = le32dec(authctr + 4*1);
663 1.4 riastrad q[2*2] = le32dec(authctr + 4*2);
664 1.4 riastrad q[2*3] = le32dec(authctr + 4*3);
665 1.4 riastrad
666 1.4 riastrad /* Load initial counter block, big-endian so we can increment it. */
667 1.4 riastrad c0 = le32dec(authctr + 16 + 4*0);
668 1.4 riastrad c1 = le32dec(authctr + 16 + 4*1);
669 1.4 riastrad c2 = le32dec(authctr + 16 + 4*2);
670 1.4 riastrad c3 = be32dec(authctr + 16 + 4*3);
671 1.4 riastrad
672 1.4 riastrad for (; nbytes; nbytes -= 16, in += 16, out += 16) {
673 1.4 riastrad /* Update authenticator. */
674 1.4 riastrad q[2*0] ^= le32dec(in + 4*0);
675 1.4 riastrad q[2*1] ^= le32dec(in + 4*1);
676 1.4 riastrad q[2*2] ^= le32dec(in + 4*2);
677 1.4 riastrad q[2*3] ^= le32dec(in + 4*3);
678 1.4 riastrad
679 1.4 riastrad /* Increment 32-bit counter. */
680 1.4 riastrad q[2*0 + 1] = c0;
681 1.4 riastrad q[2*1 + 1] = c1;
682 1.4 riastrad q[2*2 + 1] = c2;
683 1.4 riastrad q[2*3 + 1] = bswap32(++c3);
684 1.4 riastrad
685 1.4 riastrad /* Encrypt authenticator and counter. */
686 1.4 riastrad br_aes_ct_ortho(q);
687 1.4 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
688 1.4 riastrad br_aes_ct_ortho(q);
689 1.4 riastrad
690 1.4 riastrad /* Encrypt with CTR output. */
691 1.4 riastrad le32enc(out + 4*0, le32dec(in + 4*0) ^ q[2*0 + 1]);
692 1.4 riastrad le32enc(out + 4*1, le32dec(in + 4*1) ^ q[2*1 + 1]);
693 1.4 riastrad le32enc(out + 4*2, le32dec(in + 4*2) ^ q[2*2 + 1]);
694 1.4 riastrad le32enc(out + 4*3, le32dec(in + 4*3) ^ q[2*3 + 1]);
695 1.4 riastrad }
696 1.4 riastrad
697 1.4 riastrad /* Update authenticator. */
698 1.4 riastrad le32enc(authctr + 4*0, q[2*0]);
699 1.4 riastrad le32enc(authctr + 4*1, q[2*1]);
700 1.4 riastrad le32enc(authctr + 4*2, q[2*2]);
701 1.4 riastrad le32enc(authctr + 4*3, q[2*3]);
702 1.4 riastrad
703 1.4 riastrad /* Update counter. */
704 1.4 riastrad be32enc(authctr + 16 + 4*3, c3);
705 1.4 riastrad
706 1.4 riastrad /* Paranoia: Zero temporary buffers. */
707 1.4 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
708 1.4 riastrad explicit_memset(q, 0, sizeof q);
709 1.4 riastrad }
710 1.4 riastrad
711 1.4 riastrad static void
712 1.4 riastrad aesbear_ccm_dec1(const struct aesenc *enc, const uint8_t *in, uint8_t *out,
713 1.4 riastrad size_t nbytes, uint8_t authctr[32], uint32_t nrounds)
714 1.4 riastrad {
715 1.4 riastrad uint32_t sk_exp[120];
716 1.4 riastrad uint32_t q[8];
717 1.4 riastrad uint32_t c0, c1, c2, c3;
718 1.4 riastrad uint32_t b0, b1, b2, b3;
719 1.4 riastrad
720 1.4 riastrad KASSERT(nbytes);
721 1.4 riastrad KASSERT(nbytes % 16 == 0);
722 1.4 riastrad
723 1.4 riastrad /* Expand round keys for bitslicing. */
724 1.4 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk);
725 1.4 riastrad
726 1.4 riastrad /* Load initial counter block, big-endian so we can increment it. */
727 1.4 riastrad c0 = le32dec(authctr + 16 + 4*0);
728 1.4 riastrad c1 = le32dec(authctr + 16 + 4*1);
729 1.4 riastrad c2 = le32dec(authctr + 16 + 4*2);
730 1.4 riastrad c3 = be32dec(authctr + 16 + 4*3);
731 1.4 riastrad
732 1.4 riastrad /* Increment 32-bit counter. */
733 1.4 riastrad q[2*0] = c0;
734 1.4 riastrad q[2*1] = c1;
735 1.4 riastrad q[2*2] = c2;
736 1.4 riastrad q[2*3] = bswap32(++c3);
737 1.4 riastrad
738 1.4 riastrad /*
739 1.4 riastrad * Set the second block to garbage -- we don't have any
740 1.4 riastrad * plaintext to authenticate yet.
741 1.4 riastrad */
742 1.4 riastrad q[1] = q[3] = q[5] = q[7] = 0;
743 1.4 riastrad
744 1.4 riastrad /* Encrypt first CTR. */
745 1.4 riastrad br_aes_ct_ortho(q);
746 1.4 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
747 1.4 riastrad br_aes_ct_ortho(q);
748 1.4 riastrad
749 1.4 riastrad /* Load the initial authenticator. */
750 1.4 riastrad q[2*0 + 1] = le32dec(authctr + 4*0);
751 1.4 riastrad q[2*1 + 1] = le32dec(authctr + 4*1);
752 1.4 riastrad q[2*2 + 1] = le32dec(authctr + 4*2);
753 1.4 riastrad q[2*3 + 1] = le32dec(authctr + 4*3);
754 1.4 riastrad
755 1.4 riastrad for (;; in += 16, out += 16) {
756 1.4 riastrad /* Decrypt the block. */
757 1.4 riastrad b0 = le32dec(in + 4*0) ^ q[2*0];
758 1.4 riastrad b1 = le32dec(in + 4*1) ^ q[2*1];
759 1.4 riastrad b2 = le32dec(in + 4*2) ^ q[2*2];
760 1.4 riastrad b3 = le32dec(in + 4*3) ^ q[2*3];
761 1.4 riastrad
762 1.4 riastrad /* Update authenticator. */
763 1.4 riastrad q[2*0 + 1] ^= b0;
764 1.4 riastrad q[2*1 + 1] ^= b1;
765 1.4 riastrad q[2*2 + 1] ^= b2;
766 1.4 riastrad q[2*3 + 1] ^= b3;
767 1.4 riastrad
768 1.4 riastrad /* Store plaintext. */
769 1.4 riastrad le32enc(out + 4*0, b0);
770 1.4 riastrad le32enc(out + 4*1, b1);
771 1.4 riastrad le32enc(out + 4*2, b2);
772 1.4 riastrad le32enc(out + 4*3, b3);
773 1.4 riastrad
774 1.4 riastrad /* If this is the last block, stop. */
775 1.4 riastrad if ((nbytes -= 16) == 0)
776 1.4 riastrad break;
777 1.4 riastrad
778 1.4 riastrad /* Increment 32-bit counter. */
779 1.4 riastrad q[2*0] = c0;
780 1.4 riastrad q[2*1] = c1;
781 1.4 riastrad q[2*2] = c2;
782 1.4 riastrad q[2*3] = bswap32(++c3);
783 1.4 riastrad
784 1.4 riastrad /* Authenticate previous plaintext, encrypt next CTR. */
785 1.4 riastrad br_aes_ct_ortho(q);
786 1.4 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
787 1.4 riastrad br_aes_ct_ortho(q);
788 1.4 riastrad }
789 1.4 riastrad
790 1.4 riastrad /*
791 1.4 riastrad * Authenticate last plaintext. We're only doing this for the
792 1.4 riastrad * authenticator, not for the counter, so don't bother to
793 1.4 riastrad * initialize q[2*i]. (Even for the sake of sanitizers,
794 1.4 riastrad * they're already initialized to something by now.)
795 1.4 riastrad */
796 1.4 riastrad br_aes_ct_ortho(q);
797 1.4 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
798 1.4 riastrad br_aes_ct_ortho(q);
799 1.4 riastrad
800 1.4 riastrad /* Update authenticator. */
801 1.4 riastrad le32enc(authctr + 4*0, q[2*0 + 1]);
802 1.4 riastrad le32enc(authctr + 4*1, q[2*1 + 1]);
803 1.4 riastrad le32enc(authctr + 4*2, q[2*2 + 1]);
804 1.4 riastrad le32enc(authctr + 4*3, q[2*3 + 1]);
805 1.4 riastrad
806 1.4 riastrad /* Update counter. */
807 1.4 riastrad be32enc(authctr + 16 + 4*3, c3);
808 1.4 riastrad
809 1.4 riastrad /* Paranoia: Zero temporary buffers. */
810 1.4 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
811 1.4 riastrad explicit_memset(q, 0, sizeof q);
812 1.4 riastrad }
813 1.4 riastrad
814 1.1 riastrad static int
815 1.1 riastrad aesbear_probe(void)
816 1.1 riastrad {
817 1.1 riastrad
818 1.1 riastrad if (aesbear_xts_update_selftest())
819 1.1 riastrad return -1;
820 1.1 riastrad
821 1.1 riastrad /* XXX test br_aes_ct_bitslice_decrypt */
822 1.1 riastrad /* XXX test br_aes_ct_bitslice_encrypt */
823 1.1 riastrad /* XXX test br_aes_ct_keysched */
824 1.1 riastrad /* XXX test br_aes_ct_ortho */
825 1.1 riastrad /* XXX test br_aes_ct_skey_expand */
826 1.1 riastrad
827 1.1 riastrad return 0;
828 1.1 riastrad }
829 1.1 riastrad
830 1.1 riastrad struct aes_impl aes_bear_impl = {
831 1.1 riastrad .ai_name = "BearSSL aes_ct",
832 1.1 riastrad .ai_probe = aesbear_probe,
833 1.1 riastrad .ai_setenckey = aesbear_setenckey,
834 1.1 riastrad .ai_setdeckey = aesbear_setdeckey,
835 1.1 riastrad .ai_enc = aesbear_enc,
836 1.1 riastrad .ai_dec = aesbear_dec,
837 1.1 riastrad .ai_cbc_enc = aesbear_cbc_enc,
838 1.1 riastrad .ai_cbc_dec = aesbear_cbc_dec,
839 1.1 riastrad .ai_xts_enc = aesbear_xts_enc,
840 1.1 riastrad .ai_xts_dec = aesbear_xts_dec,
841 1.4 riastrad .ai_cbcmac_update1 = aesbear_cbcmac_update1,
842 1.4 riastrad .ai_ccm_enc1 = aesbear_ccm_enc1,
843 1.4 riastrad .ai_ccm_dec1 = aesbear_ccm_dec1,
844 1.1 riastrad };
845