aes_bear.c revision 1.3 1 1.3 riastrad /* $NetBSD: aes_bear.c,v 1.3 2020/07/25 22:12:57 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.3 riastrad __KERNEL_RCSID(1, "$NetBSD: aes_bear.c,v 1.3 2020/07/25 22:12:57 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.1 riastrad static int
601 1.1 riastrad aesbear_probe(void)
602 1.1 riastrad {
603 1.1 riastrad
604 1.1 riastrad if (aesbear_xts_update_selftest())
605 1.1 riastrad return -1;
606 1.1 riastrad
607 1.1 riastrad /* XXX test br_aes_ct_bitslice_decrypt */
608 1.1 riastrad /* XXX test br_aes_ct_bitslice_encrypt */
609 1.1 riastrad /* XXX test br_aes_ct_keysched */
610 1.1 riastrad /* XXX test br_aes_ct_ortho */
611 1.1 riastrad /* XXX test br_aes_ct_skey_expand */
612 1.1 riastrad
613 1.1 riastrad return 0;
614 1.1 riastrad }
615 1.1 riastrad
616 1.1 riastrad struct aes_impl aes_bear_impl = {
617 1.1 riastrad .ai_name = "BearSSL aes_ct",
618 1.1 riastrad .ai_probe = aesbear_probe,
619 1.1 riastrad .ai_setenckey = aesbear_setenckey,
620 1.1 riastrad .ai_setdeckey = aesbear_setdeckey,
621 1.1 riastrad .ai_enc = aesbear_enc,
622 1.1 riastrad .ai_dec = aesbear_dec,
623 1.1 riastrad .ai_cbc_enc = aesbear_cbc_enc,
624 1.1 riastrad .ai_cbc_dec = aesbear_cbc_dec,
625 1.1 riastrad .ai_xts_enc = aesbear_xts_enc,
626 1.1 riastrad .ai_xts_dec = aesbear_xts_dec,
627 1.1 riastrad };
628