aes_bear.c revision 1.2 1 1.2 riastrad /* $NetBSD: aes_bear.c,v 1.2 2020/06/30 20:32:11 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.2 riastrad __KERNEL_RCSID(1, "$NetBSD: aes_bear.c,v 1.2 2020/06/30 20:32:11 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.1 riastrad
48 1.1 riastrad static void
49 1.1 riastrad aesbear_setkey(uint32_t rk[static 60], const void *key, uint32_t nrounds)
50 1.1 riastrad {
51 1.1 riastrad size_t key_len;
52 1.1 riastrad
53 1.1 riastrad switch (nrounds) {
54 1.1 riastrad case 10:
55 1.1 riastrad key_len = 16;
56 1.1 riastrad break;
57 1.1 riastrad case 12:
58 1.1 riastrad key_len = 24;
59 1.1 riastrad break;
60 1.1 riastrad case 14:
61 1.1 riastrad key_len = 32;
62 1.1 riastrad break;
63 1.1 riastrad default:
64 1.1 riastrad panic("invalid AES nrounds: %u", nrounds);
65 1.1 riastrad }
66 1.1 riastrad
67 1.1 riastrad br_aes_ct_keysched(rk, key, key_len);
68 1.1 riastrad }
69 1.1 riastrad
70 1.1 riastrad static void
71 1.1 riastrad aesbear_setenckey(struct aesenc *enc, const uint8_t *key, uint32_t nrounds)
72 1.1 riastrad {
73 1.1 riastrad
74 1.1 riastrad aesbear_setkey(enc->aese_aes.aes_rk, key, nrounds);
75 1.1 riastrad }
76 1.1 riastrad
77 1.1 riastrad static void
78 1.1 riastrad aesbear_setdeckey(struct aesdec *dec, const uint8_t *key, uint32_t nrounds)
79 1.1 riastrad {
80 1.1 riastrad
81 1.1 riastrad /*
82 1.1 riastrad * BearSSL computes InvMixColumns on the fly -- no need for
83 1.1 riastrad * distinct decryption round keys.
84 1.1 riastrad */
85 1.1 riastrad aesbear_setkey(dec->aesd_aes.aes_rk, key, nrounds);
86 1.1 riastrad }
87 1.1 riastrad
88 1.1 riastrad static void
89 1.1 riastrad aesbear_enc(const struct aesenc *enc, const uint8_t in[static 16],
90 1.1 riastrad uint8_t out[static 16], uint32_t nrounds)
91 1.1 riastrad {
92 1.1 riastrad uint32_t sk_exp[120];
93 1.1 riastrad uint32_t q[8];
94 1.1 riastrad
95 1.1 riastrad /* Expand round keys for bitslicing. */
96 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk);
97 1.1 riastrad
98 1.1 riastrad /* Load input block interleaved with garbage block. */
99 1.1 riastrad q[2*0] = le32dec(in + 4*0);
100 1.1 riastrad q[2*1] = le32dec(in + 4*1);
101 1.1 riastrad q[2*2] = le32dec(in + 4*2);
102 1.1 riastrad q[2*3] = le32dec(in + 4*3);
103 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
104 1.1 riastrad
105 1.1 riastrad /* Transform to bitslice, decrypt, transform from bitslice. */
106 1.1 riastrad br_aes_ct_ortho(q);
107 1.1 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
108 1.1 riastrad br_aes_ct_ortho(q);
109 1.1 riastrad
110 1.1 riastrad /* Store output block. */
111 1.1 riastrad le32enc(out + 4*0, q[2*0]);
112 1.1 riastrad le32enc(out + 4*1, q[2*1]);
113 1.1 riastrad le32enc(out + 4*2, q[2*2]);
114 1.1 riastrad le32enc(out + 4*3, q[2*3]);
115 1.1 riastrad
116 1.1 riastrad /* Paranoia: Zero temporary buffers. */
117 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
118 1.1 riastrad explicit_memset(q, 0, sizeof q);
119 1.1 riastrad }
120 1.1 riastrad
121 1.1 riastrad static void
122 1.1 riastrad aesbear_dec(const struct aesdec *dec, const uint8_t in[static 16],
123 1.1 riastrad uint8_t out[static 16], uint32_t nrounds)
124 1.1 riastrad {
125 1.1 riastrad uint32_t sk_exp[120];
126 1.1 riastrad uint32_t q[8];
127 1.1 riastrad
128 1.1 riastrad /* Expand round keys for bitslicing. */
129 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, dec->aesd_aes.aes_rk);
130 1.1 riastrad
131 1.1 riastrad /* Load input block interleaved with garbage. */
132 1.1 riastrad q[2*0] = le32dec(in + 4*0);
133 1.1 riastrad q[2*1] = le32dec(in + 4*1);
134 1.1 riastrad q[2*2] = le32dec(in + 4*2);
135 1.1 riastrad q[2*3] = le32dec(in + 4*3);
136 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
137 1.1 riastrad
138 1.1 riastrad /* Transform to bitslice, decrypt, transform from bitslice. */
139 1.1 riastrad br_aes_ct_ortho(q);
140 1.1 riastrad br_aes_ct_bitslice_decrypt(nrounds, sk_exp, q);
141 1.1 riastrad br_aes_ct_ortho(q);
142 1.1 riastrad
143 1.1 riastrad /* Store output block. */
144 1.1 riastrad le32enc(out + 4*0, q[2*0]);
145 1.1 riastrad le32enc(out + 4*1, q[2*1]);
146 1.1 riastrad le32enc(out + 4*2, q[2*2]);
147 1.1 riastrad le32enc(out + 4*3, q[2*3]);
148 1.1 riastrad
149 1.1 riastrad /* Paranoia: Zero temporary buffers. */
150 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
151 1.1 riastrad explicit_memset(q, 0, sizeof q);
152 1.1 riastrad }
153 1.1 riastrad
154 1.1 riastrad static void
155 1.1 riastrad aesbear_cbc_enc(const struct aesenc *enc, const uint8_t in[static 16],
156 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
157 1.1 riastrad uint32_t nrounds)
158 1.1 riastrad {
159 1.1 riastrad uint32_t sk_exp[120];
160 1.1 riastrad uint32_t q[8];
161 1.1 riastrad uint32_t cv0, cv1, cv2, cv3;
162 1.1 riastrad
163 1.1 riastrad KASSERT(nbytes % 16 == 0);
164 1.1 riastrad
165 1.1 riastrad /* Skip if there's nothing to do. */
166 1.1 riastrad if (nbytes == 0)
167 1.1 riastrad return;
168 1.1 riastrad
169 1.1 riastrad /* Expand round keys for bitslicing. */
170 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk);
171 1.1 riastrad
172 1.1 riastrad /* Initialize garbage block. */
173 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
174 1.1 riastrad
175 1.1 riastrad /* Load IV. */
176 1.1 riastrad cv0 = le32dec(iv + 4*0);
177 1.1 riastrad cv1 = le32dec(iv + 4*1);
178 1.1 riastrad cv2 = le32dec(iv + 4*2);
179 1.1 riastrad cv3 = le32dec(iv + 4*3);
180 1.1 riastrad
181 1.1 riastrad for (; nbytes; nbytes -= 16, in += 16, out += 16) {
182 1.1 riastrad /* Load input block and apply CV. */
183 1.1 riastrad q[2*0] = cv0 ^ le32dec(in + 4*0);
184 1.1 riastrad q[2*1] = cv1 ^ le32dec(in + 4*1);
185 1.1 riastrad q[2*2] = cv2 ^ le32dec(in + 4*2);
186 1.1 riastrad q[2*3] = cv3 ^ le32dec(in + 4*3);
187 1.1 riastrad
188 1.1 riastrad /* Transform to bitslice, encrypt, transform from bitslice. */
189 1.1 riastrad br_aes_ct_ortho(q);
190 1.1 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
191 1.1 riastrad br_aes_ct_ortho(q);
192 1.1 riastrad
193 1.1 riastrad /* Remember ciphertext as CV and store output block. */
194 1.1 riastrad cv0 = q[2*0];
195 1.1 riastrad cv1 = q[2*1];
196 1.1 riastrad cv2 = q[2*2];
197 1.1 riastrad cv3 = q[2*3];
198 1.1 riastrad le32enc(out + 4*0, cv0);
199 1.1 riastrad le32enc(out + 4*1, cv1);
200 1.1 riastrad le32enc(out + 4*2, cv2);
201 1.1 riastrad le32enc(out + 4*3, cv3);
202 1.1 riastrad }
203 1.1 riastrad
204 1.1 riastrad /* Store updated IV. */
205 1.1 riastrad le32enc(iv + 4*0, cv0);
206 1.1 riastrad le32enc(iv + 4*1, cv1);
207 1.1 riastrad le32enc(iv + 4*2, cv2);
208 1.1 riastrad le32enc(iv + 4*3, cv3);
209 1.1 riastrad
210 1.1 riastrad /* Paranoia: Zero temporary buffers. */
211 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
212 1.1 riastrad explicit_memset(q, 0, sizeof q);
213 1.1 riastrad }
214 1.1 riastrad
215 1.1 riastrad static void
216 1.1 riastrad aesbear_cbc_dec(const struct aesdec *dec, const uint8_t in[static 16],
217 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
218 1.1 riastrad uint32_t nrounds)
219 1.1 riastrad {
220 1.1 riastrad uint32_t sk_exp[120];
221 1.1 riastrad uint32_t q[8];
222 1.1 riastrad uint32_t cv0, cv1, cv2, cv3, iv0, iv1, iv2, iv3;
223 1.1 riastrad
224 1.1 riastrad KASSERT(nbytes % 16 == 0);
225 1.1 riastrad
226 1.1 riastrad /* Skip if there's nothing to do. */
227 1.1 riastrad if (nbytes == 0)
228 1.1 riastrad return;
229 1.1 riastrad
230 1.1 riastrad /* Expand round keys for bitslicing. */
231 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, dec->aesd_aes.aes_rk);
232 1.1 riastrad
233 1.1 riastrad /* Load the IV. */
234 1.1 riastrad iv0 = le32dec(iv + 4*0);
235 1.1 riastrad iv1 = le32dec(iv + 4*1);
236 1.1 riastrad iv2 = le32dec(iv + 4*2);
237 1.1 riastrad iv3 = le32dec(iv + 4*3);
238 1.1 riastrad
239 1.1 riastrad /* Load the last cipher block. */
240 1.1 riastrad cv0 = le32dec(in + nbytes - 16 + 4*0);
241 1.1 riastrad cv1 = le32dec(in + nbytes - 16 + 4*1);
242 1.1 riastrad cv2 = le32dec(in + nbytes - 16 + 4*2);
243 1.1 riastrad cv3 = le32dec(in + nbytes - 16 + 4*3);
244 1.1 riastrad
245 1.1 riastrad /* Store the updated IV. */
246 1.1 riastrad le32enc(iv + 4*0, cv0);
247 1.1 riastrad le32enc(iv + 4*1, cv1);
248 1.1 riastrad le32enc(iv + 4*2, cv2);
249 1.1 riastrad le32enc(iv + 4*3, cv3);
250 1.1 riastrad
251 1.1 riastrad /* Handle the last cipher block separately if odd number. */
252 1.1 riastrad if (nbytes % 32) {
253 1.1 riastrad KASSERT(nbytes % 32 == 16);
254 1.1 riastrad
255 1.1 riastrad /* Set up the last cipher block and a garbage block. */
256 1.1 riastrad q[2*0] = cv0;
257 1.1 riastrad q[2*1] = cv1;
258 1.1 riastrad q[2*2] = cv2;
259 1.1 riastrad q[2*3] = cv3;
260 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
261 1.1 riastrad
262 1.1 riastrad /* Decrypt. */
263 1.1 riastrad br_aes_ct_ortho(q);
264 1.1 riastrad br_aes_ct_bitslice_decrypt(nrounds, sk_exp, q);
265 1.1 riastrad br_aes_ct_ortho(q);
266 1.1 riastrad
267 1.1 riastrad /* If this was the only cipher block, we're done. */
268 1.1 riastrad nbytes -= 16;
269 1.1 riastrad if (nbytes == 0)
270 1.1 riastrad goto out;
271 1.1 riastrad
272 1.1 riastrad /*
273 1.1 riastrad * Otherwise, load up the penultimate cipher block, and
274 1.1 riastrad * store the output block.
275 1.1 riastrad */
276 1.1 riastrad cv0 = le32dec(in + nbytes - 16 + 4*0);
277 1.1 riastrad cv1 = le32dec(in + nbytes - 16 + 4*1);
278 1.1 riastrad cv2 = le32dec(in + nbytes - 16 + 4*2);
279 1.1 riastrad cv3 = le32dec(in + nbytes - 16 + 4*3);
280 1.1 riastrad le32enc(out + nbytes + 4*0, cv0 ^ q[2*0]);
281 1.1 riastrad le32enc(out + nbytes + 4*1, cv1 ^ q[2*1]);
282 1.1 riastrad le32enc(out + nbytes + 4*2, cv2 ^ q[2*2]);
283 1.1 riastrad le32enc(out + nbytes + 4*3, cv3 ^ q[2*3]);
284 1.1 riastrad }
285 1.1 riastrad
286 1.1 riastrad for (;;) {
287 1.1 riastrad KASSERT(nbytes >= 32);
288 1.1 riastrad
289 1.1 riastrad /*
290 1.1 riastrad * 1. Set up upper cipher block from cvN.
291 1.1 riastrad * 2. Load lower cipher block into cvN and set it up.
292 1.1 riastrad * 3. Decrypt.
293 1.1 riastrad */
294 1.1 riastrad q[2*0 + 1] = cv0;
295 1.1 riastrad q[2*1 + 1] = cv1;
296 1.1 riastrad q[2*2 + 1] = cv2;
297 1.1 riastrad q[2*3 + 1] = cv3;
298 1.1 riastrad cv0 = q[2*0] = le32dec(in + nbytes - 32 + 4*0);
299 1.1 riastrad cv1 = q[2*1] = le32dec(in + nbytes - 32 + 4*1);
300 1.1 riastrad cv2 = q[2*2] = le32dec(in + nbytes - 32 + 4*2);
301 1.1 riastrad cv3 = q[2*3] = le32dec(in + nbytes - 32 + 4*3);
302 1.1 riastrad
303 1.1 riastrad br_aes_ct_ortho(q);
304 1.1 riastrad br_aes_ct_bitslice_decrypt(nrounds, sk_exp, q);
305 1.1 riastrad br_aes_ct_ortho(q);
306 1.1 riastrad
307 1.1 riastrad /* Store the upper output block. */
308 1.1 riastrad le32enc(out + nbytes - 16 + 4*0, q[2*0 + 1] ^ cv0);
309 1.1 riastrad le32enc(out + nbytes - 16 + 4*1, q[2*1 + 1] ^ cv1);
310 1.1 riastrad le32enc(out + nbytes - 16 + 4*2, q[2*2 + 1] ^ cv2);
311 1.1 riastrad le32enc(out + nbytes - 16 + 4*3, q[2*3 + 1] ^ cv3);
312 1.1 riastrad
313 1.1 riastrad /* Stop if we've reached the first output block. */
314 1.1 riastrad nbytes -= 32;
315 1.1 riastrad if (nbytes == 0)
316 1.1 riastrad goto out;
317 1.1 riastrad
318 1.1 riastrad /*
319 1.1 riastrad * Load the preceding cipher block, and apply it as the
320 1.1 riastrad * chaining value to this one.
321 1.1 riastrad */
322 1.1 riastrad cv0 = le32dec(in + nbytes - 16 + 4*0);
323 1.1 riastrad cv1 = le32dec(in + nbytes - 16 + 4*1);
324 1.1 riastrad cv2 = le32dec(in + nbytes - 16 + 4*2);
325 1.1 riastrad cv3 = le32dec(in + nbytes - 16 + 4*3);
326 1.1 riastrad le32enc(out + nbytes + 4*0, q[2*0] ^ cv0);
327 1.1 riastrad le32enc(out + nbytes + 4*1, q[2*1] ^ cv1);
328 1.1 riastrad le32enc(out + nbytes + 4*2, q[2*2] ^ cv2);
329 1.1 riastrad le32enc(out + nbytes + 4*3, q[2*3] ^ cv3);
330 1.1 riastrad }
331 1.1 riastrad
332 1.1 riastrad out: /* Store the first output block. */
333 1.1 riastrad le32enc(out + 4*0, q[2*0] ^ iv0);
334 1.1 riastrad le32enc(out + 4*1, q[2*1] ^ iv1);
335 1.1 riastrad le32enc(out + 4*2, q[2*2] ^ iv2);
336 1.1 riastrad le32enc(out + 4*3, q[2*3] ^ iv3);
337 1.1 riastrad
338 1.1 riastrad /* Paranoia: Zero temporary buffers. */
339 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
340 1.1 riastrad explicit_memset(q, 0, sizeof q);
341 1.1 riastrad }
342 1.1 riastrad
343 1.1 riastrad static inline void
344 1.1 riastrad aesbear_xts_update(uint32_t *t0, uint32_t *t1, uint32_t *t2, uint32_t *t3)
345 1.1 riastrad {
346 1.1 riastrad uint32_t s0, s1, s2, s3;
347 1.1 riastrad
348 1.1 riastrad s0 = *t0 >> 31;
349 1.1 riastrad s1 = *t1 >> 31;
350 1.1 riastrad s2 = *t2 >> 31;
351 1.1 riastrad s3 = *t3 >> 31;
352 1.1 riastrad *t0 = (*t0 << 1) ^ (-s3 & 0x87);
353 1.1 riastrad *t1 = (*t1 << 1) ^ s0;
354 1.1 riastrad *t2 = (*t2 << 1) ^ s1;
355 1.1 riastrad *t3 = (*t3 << 1) ^ s2;
356 1.1 riastrad }
357 1.1 riastrad
358 1.1 riastrad static int
359 1.1 riastrad aesbear_xts_update_selftest(void)
360 1.1 riastrad {
361 1.1 riastrad static const struct {
362 1.1 riastrad uint32_t in[4], out[4];
363 1.1 riastrad } cases[] = {
364 1.1 riastrad { {1}, {2} },
365 1.1 riastrad { {0x80000000U,0,0,0}, {0,1,0,0} },
366 1.1 riastrad { {0,0x80000000U,0,0}, {0,0,1,0} },
367 1.1 riastrad { {0,0,0x80000000U,0}, {0,0,0,1} },
368 1.1 riastrad { {0,0,0,0x80000000U}, {0x87,0,0,0} },
369 1.1 riastrad { {0,0x80000000U,0,0x80000000U}, {0x87,0,1,0} },
370 1.1 riastrad };
371 1.1 riastrad unsigned i;
372 1.1 riastrad uint32_t t0, t1, t2, t3;
373 1.1 riastrad
374 1.1 riastrad for (i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) {
375 1.1 riastrad t0 = cases[i].in[0];
376 1.1 riastrad t1 = cases[i].in[1];
377 1.1 riastrad t2 = cases[i].in[2];
378 1.1 riastrad t3 = cases[i].in[3];
379 1.1 riastrad aesbear_xts_update(&t0, &t1, &t2, &t3);
380 1.1 riastrad if (t0 != cases[i].out[0] ||
381 1.1 riastrad t1 != cases[i].out[1] ||
382 1.1 riastrad t2 != cases[i].out[2] ||
383 1.1 riastrad t3 != cases[i].out[3])
384 1.1 riastrad return -1;
385 1.1 riastrad }
386 1.1 riastrad
387 1.1 riastrad /* Success! */
388 1.1 riastrad return 0;
389 1.1 riastrad }
390 1.1 riastrad
391 1.1 riastrad static void
392 1.1 riastrad aesbear_xts_enc(const struct aesenc *enc, const uint8_t in[static 16],
393 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
394 1.1 riastrad uint32_t nrounds)
395 1.1 riastrad {
396 1.1 riastrad uint32_t sk_exp[120];
397 1.1 riastrad uint32_t q[8];
398 1.1 riastrad uint32_t t0, t1, t2, t3, u0, u1, u2, u3;
399 1.1 riastrad
400 1.1 riastrad KASSERT(nbytes % 16 == 0);
401 1.1 riastrad
402 1.1 riastrad /* Skip if there's nothing to do. */
403 1.1 riastrad if (nbytes == 0)
404 1.1 riastrad return;
405 1.1 riastrad
406 1.1 riastrad /* Expand round keys for bitslicing. */
407 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk);
408 1.1 riastrad
409 1.1 riastrad /* Load tweak. */
410 1.1 riastrad t0 = le32dec(tweak + 4*0);
411 1.1 riastrad t1 = le32dec(tweak + 4*1);
412 1.1 riastrad t2 = le32dec(tweak + 4*2);
413 1.1 riastrad t3 = le32dec(tweak + 4*3);
414 1.1 riastrad
415 1.1 riastrad /* Handle the first block separately if odd number. */
416 1.1 riastrad if (nbytes % 32) {
417 1.1 riastrad KASSERT(nbytes % 32 == 16);
418 1.1 riastrad
419 1.1 riastrad /* Load up the first block and a garbage block. */
420 1.1 riastrad q[2*0] = le32dec(in + 4*0) ^ t0;
421 1.1 riastrad q[2*1] = le32dec(in + 4*1) ^ t1;
422 1.1 riastrad q[2*2] = le32dec(in + 4*2) ^ t2;
423 1.1 riastrad q[2*3] = le32dec(in + 4*3) ^ t3;
424 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
425 1.1 riastrad
426 1.1 riastrad /* Encrypt two blocks. */
427 1.1 riastrad br_aes_ct_ortho(q);
428 1.1 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
429 1.1 riastrad br_aes_ct_ortho(q);
430 1.1 riastrad
431 1.1 riastrad /* Store the first cipher block. */
432 1.1 riastrad le32enc(out + 4*0, q[2*0] ^ t0);
433 1.1 riastrad le32enc(out + 4*1, q[2*1] ^ t1);
434 1.1 riastrad le32enc(out + 4*2, q[2*2] ^ t2);
435 1.1 riastrad le32enc(out + 4*3, q[2*3] ^ t3);
436 1.1 riastrad
437 1.1 riastrad /* Advance to the next block. */
438 1.1 riastrad aesbear_xts_update(&t0, &t1, &t2, &t3);
439 1.1 riastrad if ((nbytes -= 16) == 0)
440 1.1 riastrad goto out;
441 1.1 riastrad in += 16;
442 1.1 riastrad out += 16;
443 1.1 riastrad }
444 1.1 riastrad
445 1.1 riastrad do {
446 1.1 riastrad KASSERT(nbytes >= 32);
447 1.1 riastrad
448 1.1 riastrad /* Compute the upper tweak. */
449 1.1 riastrad u0 = t0; u1 = t1; u2 = t2; u3 = t3;
450 1.1 riastrad aesbear_xts_update(&u0, &u1, &u2, &u3);
451 1.1 riastrad
452 1.1 riastrad /* Load lower and upper blocks. */
453 1.1 riastrad q[2*0] = le32dec(in + 4*0) ^ t0;
454 1.1 riastrad q[2*1] = le32dec(in + 4*1) ^ t1;
455 1.1 riastrad q[2*2] = le32dec(in + 4*2) ^ t2;
456 1.1 riastrad q[2*3] = le32dec(in + 4*3) ^ t3;
457 1.1 riastrad q[2*0 + 1] = le32dec(in + 16 + 4*0) ^ u0;
458 1.1 riastrad q[2*1 + 1] = le32dec(in + 16 + 4*1) ^ u1;
459 1.1 riastrad q[2*2 + 1] = le32dec(in + 16 + 4*2) ^ u2;
460 1.1 riastrad q[2*3 + 1] = le32dec(in + 16 + 4*3) ^ u3;
461 1.1 riastrad
462 1.1 riastrad /* Encrypt two blocks. */
463 1.1 riastrad br_aes_ct_ortho(q);
464 1.1 riastrad br_aes_ct_bitslice_encrypt(nrounds, sk_exp, q);
465 1.1 riastrad br_aes_ct_ortho(q);
466 1.1 riastrad
467 1.1 riastrad /* Store lower and upper blocks. */
468 1.1 riastrad le32enc(out + 4*0, q[2*0] ^ t0);
469 1.1 riastrad le32enc(out + 4*1, q[2*1] ^ t1);
470 1.1 riastrad le32enc(out + 4*2, q[2*2] ^ t2);
471 1.1 riastrad le32enc(out + 4*3, q[2*3] ^ t3);
472 1.1 riastrad le32enc(out + 16 + 4*0, q[2*0 + 1] ^ u0);
473 1.1 riastrad le32enc(out + 16 + 4*1, q[2*1 + 1] ^ u1);
474 1.1 riastrad le32enc(out + 16 + 4*2, q[2*2 + 1] ^ u2);
475 1.1 riastrad le32enc(out + 16 + 4*3, q[2*3 + 1] ^ u3);
476 1.1 riastrad
477 1.1 riastrad /* Advance to the next pair of blocks. */
478 1.1 riastrad t0 = u0; t1 = u1; t2 = u2; t3 = u3;
479 1.1 riastrad aesbear_xts_update(&t0, &t1, &t2, &t3);
480 1.1 riastrad in += 32;
481 1.1 riastrad out += 32;
482 1.1 riastrad } while (nbytes -= 32, nbytes);
483 1.1 riastrad
484 1.1 riastrad out: /* Store the updated tweak. */
485 1.1 riastrad le32enc(tweak + 4*0, t0);
486 1.1 riastrad le32enc(tweak + 4*1, t1);
487 1.1 riastrad le32enc(tweak + 4*2, t2);
488 1.1 riastrad le32enc(tweak + 4*3, t3);
489 1.1 riastrad
490 1.1 riastrad /* Paranoia: Zero temporary buffers. */
491 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
492 1.1 riastrad explicit_memset(q, 0, sizeof q);
493 1.1 riastrad }
494 1.1 riastrad
495 1.1 riastrad static void
496 1.1 riastrad aesbear_xts_dec(const struct aesdec *dec, const uint8_t in[static 16],
497 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
498 1.1 riastrad uint32_t nrounds)
499 1.1 riastrad {
500 1.1 riastrad uint32_t sk_exp[120];
501 1.1 riastrad uint32_t q[8];
502 1.1 riastrad uint32_t t0, t1, t2, t3, u0, u1, u2, u3;
503 1.1 riastrad
504 1.1 riastrad KASSERT(nbytes % 16 == 0);
505 1.1 riastrad
506 1.1 riastrad /* Skip if there's nothing to do. */
507 1.1 riastrad if (nbytes == 0)
508 1.1 riastrad return;
509 1.1 riastrad
510 1.1 riastrad /* Expand round keys for bitslicing. */
511 1.1 riastrad br_aes_ct_skey_expand(sk_exp, nrounds, dec->aesd_aes.aes_rk);
512 1.1 riastrad
513 1.1 riastrad /* Load tweak. */
514 1.1 riastrad t0 = le32dec(tweak + 4*0);
515 1.1 riastrad t1 = le32dec(tweak + 4*1);
516 1.1 riastrad t2 = le32dec(tweak + 4*2);
517 1.1 riastrad t3 = le32dec(tweak + 4*3);
518 1.1 riastrad
519 1.1 riastrad /* Handle the first block separately if odd number. */
520 1.1 riastrad if (nbytes % 32) {
521 1.1 riastrad KASSERT(nbytes % 32 == 16);
522 1.1 riastrad
523 1.1 riastrad /* Load up the first block and a garbage block. */
524 1.1 riastrad q[2*0] = le32dec(in + 4*0) ^ t0;
525 1.1 riastrad q[2*1] = le32dec(in + 4*1) ^ t1;
526 1.1 riastrad q[2*2] = le32dec(in + 4*2) ^ t2;
527 1.1 riastrad q[2*3] = le32dec(in + 4*3) ^ t3;
528 1.1 riastrad q[1] = q[3] = q[5] = q[7] = 0;
529 1.1 riastrad
530 1.1 riastrad /* Decrypt two blocks. */
531 1.1 riastrad br_aes_ct_ortho(q);
532 1.1 riastrad br_aes_ct_bitslice_decrypt(nrounds, sk_exp, q);
533 1.1 riastrad br_aes_ct_ortho(q);
534 1.1 riastrad
535 1.1 riastrad /* Store the first cipher block. */
536 1.1 riastrad le32enc(out + 4*0, q[2*0] ^ t0);
537 1.1 riastrad le32enc(out + 4*1, q[2*1] ^ t1);
538 1.1 riastrad le32enc(out + 4*2, q[2*2] ^ t2);
539 1.1 riastrad le32enc(out + 4*3, q[2*3] ^ t3);
540 1.1 riastrad
541 1.1 riastrad /* Advance to the next block. */
542 1.1 riastrad aesbear_xts_update(&t0, &t1, &t2, &t3);
543 1.1 riastrad if ((nbytes -= 16) == 0)
544 1.1 riastrad goto out;
545 1.1 riastrad in += 16;
546 1.1 riastrad out += 16;
547 1.1 riastrad }
548 1.1 riastrad
549 1.1 riastrad do {
550 1.1 riastrad KASSERT(nbytes >= 32);
551 1.1 riastrad
552 1.1 riastrad /* Compute the upper tweak. */
553 1.1 riastrad u0 = t0; u1 = t1; u2 = t2; u3 = t3;
554 1.1 riastrad aesbear_xts_update(&u0, &u1, &u2, &u3);
555 1.1 riastrad
556 1.1 riastrad /* Load lower and upper blocks. */
557 1.1 riastrad q[2*0] = le32dec(in + 4*0) ^ t0;
558 1.1 riastrad q[2*1] = le32dec(in + 4*1) ^ t1;
559 1.1 riastrad q[2*2] = le32dec(in + 4*2) ^ t2;
560 1.1 riastrad q[2*3] = le32dec(in + 4*3) ^ t3;
561 1.1 riastrad q[2*0 + 1] = le32dec(in + 16 + 4*0) ^ u0;
562 1.1 riastrad q[2*1 + 1] = le32dec(in + 16 + 4*1) ^ u1;
563 1.1 riastrad q[2*2 + 1] = le32dec(in + 16 + 4*2) ^ u2;
564 1.1 riastrad q[2*3 + 1] = le32dec(in + 16 + 4*3) ^ u3;
565 1.1 riastrad
566 1.1 riastrad /* Encrypt two blocks. */
567 1.1 riastrad br_aes_ct_ortho(q);
568 1.1 riastrad br_aes_ct_bitslice_decrypt(nrounds, sk_exp, q);
569 1.1 riastrad br_aes_ct_ortho(q);
570 1.1 riastrad
571 1.1 riastrad /* Store lower and upper blocks. */
572 1.1 riastrad le32enc(out + 4*0, q[2*0] ^ t0);
573 1.1 riastrad le32enc(out + 4*1, q[2*1] ^ t1);
574 1.1 riastrad le32enc(out + 4*2, q[2*2] ^ t2);
575 1.1 riastrad le32enc(out + 4*3, q[2*3] ^ t3);
576 1.1 riastrad le32enc(out + 16 + 4*0, q[2*0 + 1] ^ u0);
577 1.1 riastrad le32enc(out + 16 + 4*1, q[2*1 + 1] ^ u1);
578 1.1 riastrad le32enc(out + 16 + 4*2, q[2*2 + 1] ^ u2);
579 1.1 riastrad le32enc(out + 16 + 4*3, q[2*3 + 1] ^ u3);
580 1.1 riastrad
581 1.1 riastrad /* Advance to the next pair of blocks. */
582 1.1 riastrad t0 = u0; t1 = u1; t2 = u2; t3 = u3;
583 1.1 riastrad aesbear_xts_update(&t0, &t1, &t2, &t3);
584 1.1 riastrad in += 32;
585 1.1 riastrad out += 32;
586 1.1 riastrad } while (nbytes -= 32, nbytes);
587 1.1 riastrad
588 1.1 riastrad out: /* Store the updated tweak. */
589 1.1 riastrad le32enc(tweak + 4*0, t0);
590 1.1 riastrad le32enc(tweak + 4*1, t1);
591 1.1 riastrad le32enc(tweak + 4*2, t2);
592 1.1 riastrad le32enc(tweak + 4*3, t3);
593 1.1 riastrad
594 1.1 riastrad /* Paranoia: Zero temporary buffers. */
595 1.1 riastrad explicit_memset(sk_exp, 0, sizeof sk_exp);
596 1.1 riastrad explicit_memset(q, 0, sizeof q);
597 1.1 riastrad }
598 1.1 riastrad
599 1.1 riastrad static int
600 1.1 riastrad aesbear_probe(void)
601 1.1 riastrad {
602 1.1 riastrad
603 1.1 riastrad if (aesbear_xts_update_selftest())
604 1.1 riastrad return -1;
605 1.1 riastrad
606 1.1 riastrad /* XXX test br_aes_ct_bitslice_decrypt */
607 1.1 riastrad /* XXX test br_aes_ct_bitslice_encrypt */
608 1.1 riastrad /* XXX test br_aes_ct_keysched */
609 1.1 riastrad /* XXX test br_aes_ct_ortho */
610 1.1 riastrad /* XXX test br_aes_ct_skey_expand */
611 1.1 riastrad
612 1.1 riastrad return 0;
613 1.1 riastrad }
614 1.1 riastrad
615 1.1 riastrad struct aes_impl aes_bear_impl = {
616 1.1 riastrad .ai_name = "BearSSL aes_ct",
617 1.1 riastrad .ai_probe = aesbear_probe,
618 1.1 riastrad .ai_setenckey = aesbear_setenckey,
619 1.1 riastrad .ai_setdeckey = aesbear_setdeckey,
620 1.1 riastrad .ai_enc = aesbear_enc,
621 1.1 riastrad .ai_dec = aesbear_dec,
622 1.1 riastrad .ai_cbc_enc = aesbear_cbc_enc,
623 1.1 riastrad .ai_cbc_dec = aesbear_cbc_dec,
624 1.1 riastrad .ai_xts_enc = aesbear_xts_enc,
625 1.1 riastrad .ai_xts_dec = aesbear_xts_dec,
626 1.1 riastrad };
627