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