aes_via.c revision 1.1 1 1.1 riastrad /* $NetBSD: aes_via.c,v 1.1 2020/06/29 23:39:30 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_via.c,v 1.1 2020/06/29 23:39:30 riastradh Exp $");
31 1.1 riastrad
32 1.1 riastrad #include <sys/types.h>
33 1.1 riastrad #include <sys/evcnt.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 #include <x86/cpufunc.h>
40 1.1 riastrad #include <x86/cpuvar.h>
41 1.1 riastrad #include <x86/fpu.h>
42 1.1 riastrad #include <x86/specialreg.h>
43 1.1 riastrad #include <x86/via_padlock.h>
44 1.1 riastrad
45 1.1 riastrad static void
46 1.1 riastrad aesvia_reload_keys(void)
47 1.1 riastrad {
48 1.1 riastrad
49 1.1 riastrad asm volatile("pushf; popf");
50 1.1 riastrad }
51 1.1 riastrad
52 1.1 riastrad static uint32_t
53 1.1 riastrad aesvia_keylen_cw0(unsigned nrounds)
54 1.1 riastrad {
55 1.1 riastrad
56 1.1 riastrad /*
57 1.1 riastrad * Determine the control word bits for the key size / number of
58 1.1 riastrad * rounds. For AES-128, the hardware can do key expansion on
59 1.1 riastrad * the fly; for AES-192 and AES-256, software must do it.
60 1.1 riastrad */
61 1.1 riastrad switch (nrounds) {
62 1.1 riastrad case AES_128_NROUNDS:
63 1.1 riastrad return C3_CRYPT_CWLO_KEY128;
64 1.1 riastrad case AES_192_NROUNDS:
65 1.1 riastrad return C3_CRYPT_CWLO_KEY192 | C3_CRYPT_CWLO_KEYGEN_SW;
66 1.1 riastrad case AES_256_NROUNDS:
67 1.1 riastrad return C3_CRYPT_CWLO_KEY256 | C3_CRYPT_CWLO_KEYGEN_SW;
68 1.1 riastrad default:
69 1.1 riastrad panic("invalid AES nrounds: %u", nrounds);
70 1.1 riastrad }
71 1.1 riastrad }
72 1.1 riastrad
73 1.1 riastrad static void
74 1.1 riastrad aesvia_setenckey(struct aesenc *enc, const uint8_t *key, uint32_t nrounds)
75 1.1 riastrad {
76 1.1 riastrad size_t key_len;
77 1.1 riastrad
78 1.1 riastrad switch (nrounds) {
79 1.1 riastrad case AES_128_NROUNDS:
80 1.1 riastrad enc->aese_aes.aes_rk[0] = le32dec(key + 4*0);
81 1.1 riastrad enc->aese_aes.aes_rk[1] = le32dec(key + 4*1);
82 1.1 riastrad enc->aese_aes.aes_rk[2] = le32dec(key + 4*2);
83 1.1 riastrad enc->aese_aes.aes_rk[3] = le32dec(key + 4*3);
84 1.1 riastrad return;
85 1.1 riastrad case AES_192_NROUNDS:
86 1.1 riastrad key_len = 24;
87 1.1 riastrad break;
88 1.1 riastrad case AES_256_NROUNDS:
89 1.1 riastrad key_len = 32;
90 1.1 riastrad break;
91 1.1 riastrad default:
92 1.1 riastrad panic("invalid AES nrounds: %u", nrounds);
93 1.1 riastrad }
94 1.1 riastrad br_aes_ct_keysched_stdenc(enc->aese_aes.aes_rk, key, key_len);
95 1.1 riastrad }
96 1.1 riastrad
97 1.1 riastrad static void
98 1.1 riastrad aesvia_setdeckey(struct aesdec *dec, const uint8_t *key, uint32_t nrounds)
99 1.1 riastrad {
100 1.1 riastrad size_t key_len;
101 1.1 riastrad
102 1.1 riastrad switch (nrounds) {
103 1.1 riastrad case AES_128_NROUNDS:
104 1.1 riastrad dec->aesd_aes.aes_rk[0] = le32dec(key + 4*0);
105 1.1 riastrad dec->aesd_aes.aes_rk[1] = le32dec(key + 4*1);
106 1.1 riastrad dec->aesd_aes.aes_rk[2] = le32dec(key + 4*2);
107 1.1 riastrad dec->aesd_aes.aes_rk[3] = le32dec(key + 4*3);
108 1.1 riastrad return;
109 1.1 riastrad case AES_192_NROUNDS:
110 1.1 riastrad key_len = 24;
111 1.1 riastrad break;
112 1.1 riastrad case AES_256_NROUNDS:
113 1.1 riastrad key_len = 32;
114 1.1 riastrad break;
115 1.1 riastrad default:
116 1.1 riastrad panic("invalid AES nrounds: %u", nrounds);
117 1.1 riastrad }
118 1.1 riastrad br_aes_ct_keysched_stddec(dec->aesd_aes.aes_rk, key, key_len);
119 1.1 riastrad }
120 1.1 riastrad
121 1.1 riastrad static inline void
122 1.1 riastrad aesvia_enc1(const struct aesenc *enc, const uint8_t in[static 16],
123 1.1 riastrad uint8_t out[static 16], uint32_t cw0)
124 1.1 riastrad {
125 1.1 riastrad const uint32_t cw[4] __aligned(16) = {
126 1.1 riastrad [0] = (cw0
127 1.1 riastrad | C3_CRYPT_CWLO_ALG_AES
128 1.1 riastrad | C3_CRYPT_CWLO_ENCRYPT
129 1.1 riastrad | C3_CRYPT_CWLO_NORMAL),
130 1.1 riastrad };
131 1.1 riastrad size_t nblocks = 1;
132 1.1 riastrad
133 1.1 riastrad KASSERT(((uintptr_t)enc & 0xf) == 0);
134 1.1 riastrad KASSERT(((uintptr_t)in & 0xf) == 0);
135 1.1 riastrad KASSERT(((uintptr_t)out & 0xf) == 0);
136 1.1 riastrad
137 1.1 riastrad asm volatile("rep xcryptecb"
138 1.1 riastrad : "+c"(nblocks), "+S"(in), "+D"(out)
139 1.1 riastrad : "b"(enc), "d"(cw)
140 1.1 riastrad : "memory", "cc");
141 1.1 riastrad }
142 1.1 riastrad
143 1.1 riastrad static inline void
144 1.1 riastrad aesvia_dec1(const struct aesdec *dec, const uint8_t in[static 16],
145 1.1 riastrad uint8_t out[static 16], uint32_t cw0)
146 1.1 riastrad {
147 1.1 riastrad const uint32_t cw[4] __aligned(16) = {
148 1.1 riastrad [0] = (cw0
149 1.1 riastrad | C3_CRYPT_CWLO_ALG_AES
150 1.1 riastrad | C3_CRYPT_CWLO_DECRYPT
151 1.1 riastrad | C3_CRYPT_CWLO_NORMAL),
152 1.1 riastrad };
153 1.1 riastrad size_t nblocks = 1;
154 1.1 riastrad
155 1.1 riastrad KASSERT(((uintptr_t)dec & 0xf) == 0);
156 1.1 riastrad KASSERT(((uintptr_t)in & 0xf) == 0);
157 1.1 riastrad KASSERT(((uintptr_t)out & 0xf) == 0);
158 1.1 riastrad
159 1.1 riastrad asm volatile("rep xcryptecb"
160 1.1 riastrad : "+c"(nblocks), "+S"(in), "+D"(out)
161 1.1 riastrad : "b"(dec), "d"(cw)
162 1.1 riastrad : "memory", "cc");
163 1.1 riastrad }
164 1.1 riastrad
165 1.1 riastrad static struct evcnt enc_aligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
166 1.1 riastrad NULL, "aesvia", "enc aligned");
167 1.1 riastrad EVCNT_ATTACH_STATIC(enc_aligned_evcnt);
168 1.1 riastrad static struct evcnt enc_unaligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
169 1.1 riastrad NULL, "aesvia", "dec unaligned");
170 1.1 riastrad EVCNT_ATTACH_STATIC(enc_unaligned_evcnt);
171 1.1 riastrad
172 1.1 riastrad static void
173 1.1 riastrad aesvia_enc(const struct aesenc *enc, const uint8_t in[static 16],
174 1.1 riastrad uint8_t out[static 16], uint32_t nrounds)
175 1.1 riastrad {
176 1.1 riastrad const uint32_t cw0 = aesvia_keylen_cw0(nrounds);
177 1.1 riastrad
178 1.1 riastrad fpu_kern_enter();
179 1.1 riastrad aesvia_reload_keys();
180 1.1 riastrad if ((((uintptr_t)in | (uintptr_t)out) & 0xf) == 0 &&
181 1.1 riastrad ((uintptr_t)in & 0xff0) != 0xff0) {
182 1.1 riastrad enc_aligned_evcnt.ev_count++;
183 1.1 riastrad aesvia_enc1(enc, in, out, cw0);
184 1.1 riastrad } else {
185 1.1 riastrad enc_unaligned_evcnt.ev_count++;
186 1.1 riastrad /*
187 1.1 riastrad * VIA requires 16-byte/128-bit alignment, and
188 1.1 riastrad * xcrypt-ecb reads one block past the one we're
189 1.1 riastrad * working on -- which may go past the end of the page
190 1.1 riastrad * into unmapped territory. Use a bounce buffer if
191 1.1 riastrad * either constraint is violated.
192 1.1 riastrad */
193 1.1 riastrad uint8_t inbuf[16] __aligned(16);
194 1.1 riastrad uint8_t outbuf[16] __aligned(16);
195 1.1 riastrad
196 1.1 riastrad memcpy(inbuf, in, 16);
197 1.1 riastrad aesvia_enc1(enc, inbuf, outbuf, cw0);
198 1.1 riastrad memcpy(out, outbuf, 16);
199 1.1 riastrad
200 1.1 riastrad explicit_memset(inbuf, 0, sizeof inbuf);
201 1.1 riastrad explicit_memset(outbuf, 0, sizeof outbuf);
202 1.1 riastrad }
203 1.1 riastrad fpu_kern_leave();
204 1.1 riastrad }
205 1.1 riastrad
206 1.1 riastrad static struct evcnt dec_aligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
207 1.1 riastrad NULL, "aesvia", "dec aligned");
208 1.1 riastrad EVCNT_ATTACH_STATIC(dec_aligned_evcnt);
209 1.1 riastrad static struct evcnt dec_unaligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
210 1.1 riastrad NULL, "aesvia", "dec unaligned");
211 1.1 riastrad EVCNT_ATTACH_STATIC(dec_unaligned_evcnt);
212 1.1 riastrad
213 1.1 riastrad static void
214 1.1 riastrad aesvia_dec(const struct aesdec *dec, const uint8_t in[static 16],
215 1.1 riastrad uint8_t out[static 16], uint32_t nrounds)
216 1.1 riastrad {
217 1.1 riastrad const uint32_t cw0 = aesvia_keylen_cw0(nrounds);
218 1.1 riastrad
219 1.1 riastrad fpu_kern_enter();
220 1.1 riastrad aesvia_reload_keys();
221 1.1 riastrad if ((((uintptr_t)in | (uintptr_t)out) & 0xf) == 0 &&
222 1.1 riastrad ((uintptr_t)in & 0xff0) != 0xff0) {
223 1.1 riastrad dec_aligned_evcnt.ev_count++;
224 1.1 riastrad aesvia_dec1(dec, in, out, cw0);
225 1.1 riastrad } else {
226 1.1 riastrad dec_unaligned_evcnt.ev_count++;
227 1.1 riastrad /*
228 1.1 riastrad * VIA requires 16-byte/128-bit alignment, and
229 1.1 riastrad * xcrypt-ecb reads one block past the one we're
230 1.1 riastrad * working on -- which may go past the end of the page
231 1.1 riastrad * into unmapped territory. Use a bounce buffer if
232 1.1 riastrad * either constraint is violated.
233 1.1 riastrad */
234 1.1 riastrad uint8_t inbuf[16] __aligned(16);
235 1.1 riastrad uint8_t outbuf[16] __aligned(16);
236 1.1 riastrad
237 1.1 riastrad memcpy(inbuf, in, 16);
238 1.1 riastrad aesvia_dec1(dec, inbuf, outbuf, cw0);
239 1.1 riastrad memcpy(out, outbuf, 16);
240 1.1 riastrad
241 1.1 riastrad explicit_memset(inbuf, 0, sizeof inbuf);
242 1.1 riastrad explicit_memset(outbuf, 0, sizeof outbuf);
243 1.1 riastrad }
244 1.1 riastrad fpu_kern_leave();
245 1.1 riastrad }
246 1.1 riastrad
247 1.1 riastrad static inline void
248 1.1 riastrad aesvia_cbc_enc1(const struct aesenc *enc, const uint8_t in[static 16],
249 1.1 riastrad uint8_t out[static 16], size_t nblocks, uint8_t **ivp, uint32_t cw0)
250 1.1 riastrad {
251 1.1 riastrad const uint32_t cw[4] __aligned(16) = {
252 1.1 riastrad [0] = (cw0
253 1.1 riastrad | C3_CRYPT_CWLO_ALG_AES
254 1.1 riastrad | C3_CRYPT_CWLO_ENCRYPT
255 1.1 riastrad | C3_CRYPT_CWLO_NORMAL),
256 1.1 riastrad };
257 1.1 riastrad
258 1.1 riastrad KASSERT(((uintptr_t)enc & 0xf) == 0);
259 1.1 riastrad KASSERT(((uintptr_t)in & 0xf) == 0);
260 1.1 riastrad KASSERT(((uintptr_t)out & 0xf) == 0);
261 1.1 riastrad KASSERT(((uintptr_t)*ivp & 0xf) == 0);
262 1.1 riastrad
263 1.1 riastrad /*
264 1.1 riastrad * Register effects:
265 1.1 riastrad * - Counts nblocks down to zero.
266 1.1 riastrad * - Advances in by nblocks (units of blocks).
267 1.1 riastrad * - Advances out by nblocks (units of blocks).
268 1.1 riastrad * - Updates *ivp to point at the last block of out.
269 1.1 riastrad */
270 1.1 riastrad asm volatile("rep xcryptcbc"
271 1.1 riastrad : "+c"(nblocks), "+S"(in), "+D"(out), "+a"(*ivp)
272 1.1 riastrad : "b"(enc), "d"(cw)
273 1.1 riastrad : "memory", "cc");
274 1.1 riastrad }
275 1.1 riastrad
276 1.1 riastrad static inline void
277 1.1 riastrad aesvia_cbc_dec1(const struct aesdec *dec, const uint8_t in[static 16],
278 1.1 riastrad uint8_t out[static 16], size_t nblocks, uint8_t iv[static 16],
279 1.1 riastrad uint32_t cw0)
280 1.1 riastrad {
281 1.1 riastrad const uint32_t cw[4] __aligned(16) = {
282 1.1 riastrad [0] = (cw0
283 1.1 riastrad | C3_CRYPT_CWLO_ALG_AES
284 1.1 riastrad | C3_CRYPT_CWLO_DECRYPT
285 1.1 riastrad | C3_CRYPT_CWLO_NORMAL),
286 1.1 riastrad };
287 1.1 riastrad
288 1.1 riastrad KASSERT(((uintptr_t)dec & 0xf) == 0);
289 1.1 riastrad KASSERT(((uintptr_t)in & 0xf) == 0);
290 1.1 riastrad KASSERT(((uintptr_t)out & 0xf) == 0);
291 1.1 riastrad KASSERT(((uintptr_t)iv & 0xf) == 0);
292 1.1 riastrad
293 1.1 riastrad /*
294 1.1 riastrad * Register effects:
295 1.1 riastrad * - Counts nblocks down to zero.
296 1.1 riastrad * - Advances in by nblocks (units of blocks).
297 1.1 riastrad * - Advances out by nblocks (units of blocks).
298 1.1 riastrad * Memory side effects:
299 1.1 riastrad * - Writes what was the last block of in at the address iv.
300 1.1 riastrad */
301 1.1 riastrad asm volatile("rep xcryptcbc"
302 1.1 riastrad : "+c"(nblocks), "+S"(in), "+D"(out)
303 1.1 riastrad : "a"(iv), "b"(dec), "d"(cw)
304 1.1 riastrad : "memory", "cc");
305 1.1 riastrad }
306 1.1 riastrad
307 1.1 riastrad static inline void
308 1.1 riastrad xor128(void *x, const void *a, const void *b)
309 1.1 riastrad {
310 1.1 riastrad uint32_t *x32 = x;
311 1.1 riastrad const uint32_t *a32 = a;
312 1.1 riastrad const uint32_t *b32 = b;
313 1.1 riastrad
314 1.1 riastrad x32[0] = a32[0] ^ b32[0];
315 1.1 riastrad x32[1] = a32[1] ^ b32[1];
316 1.1 riastrad x32[2] = a32[2] ^ b32[2];
317 1.1 riastrad x32[3] = a32[3] ^ b32[3];
318 1.1 riastrad }
319 1.1 riastrad
320 1.1 riastrad static struct evcnt cbcenc_aligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
321 1.1 riastrad NULL, "aesvia", "cbcenc aligned");
322 1.1 riastrad EVCNT_ATTACH_STATIC(cbcenc_aligned_evcnt);
323 1.1 riastrad static struct evcnt cbcenc_unaligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
324 1.1 riastrad NULL, "aesvia", "cbcenc unaligned");
325 1.1 riastrad EVCNT_ATTACH_STATIC(cbcenc_unaligned_evcnt);
326 1.1 riastrad
327 1.1 riastrad static void
328 1.1 riastrad aesvia_cbc_enc(const struct aesenc *enc, const uint8_t in[static 16],
329 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
330 1.1 riastrad uint32_t nrounds)
331 1.1 riastrad {
332 1.1 riastrad const uint32_t cw0 = aesvia_keylen_cw0(nrounds);
333 1.1 riastrad
334 1.1 riastrad KASSERT(nbytes % 16 == 0);
335 1.1 riastrad if (nbytes == 0)
336 1.1 riastrad return;
337 1.1 riastrad
338 1.1 riastrad fpu_kern_enter();
339 1.1 riastrad aesvia_reload_keys();
340 1.1 riastrad if ((((uintptr_t)in | (uintptr_t)out | (uintptr_t)iv) & 0xf) == 0) {
341 1.1 riastrad cbcenc_aligned_evcnt.ev_count++;
342 1.1 riastrad uint8_t *ivp = iv;
343 1.1 riastrad aesvia_cbc_enc1(enc, in, out, nbytes/16, &ivp, cw0);
344 1.1 riastrad memcpy(iv, ivp, 16);
345 1.1 riastrad } else {
346 1.1 riastrad cbcenc_unaligned_evcnt.ev_count++;
347 1.1 riastrad uint8_t cv[16] __aligned(16);
348 1.1 riastrad uint8_t tmp[16] __aligned(16);
349 1.1 riastrad
350 1.1 riastrad memcpy(cv, iv, 16);
351 1.1 riastrad for (; nbytes; nbytes -= 16, in += 16, out += 16) {
352 1.1 riastrad memcpy(tmp, in, 16);
353 1.1 riastrad xor128(tmp, tmp, cv);
354 1.1 riastrad aesvia_enc1(enc, tmp, cv, cw0);
355 1.1 riastrad memcpy(out, cv, 16);
356 1.1 riastrad }
357 1.1 riastrad memcpy(iv, cv, 16);
358 1.1 riastrad }
359 1.1 riastrad fpu_kern_leave();
360 1.1 riastrad }
361 1.1 riastrad
362 1.1 riastrad static struct evcnt cbcdec_aligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
363 1.1 riastrad NULL, "aesvia", "cbcdec aligned");
364 1.1 riastrad EVCNT_ATTACH_STATIC(cbcdec_aligned_evcnt);
365 1.1 riastrad static struct evcnt cbcdec_unaligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
366 1.1 riastrad NULL, "aesvia", "cbcdec unaligned");
367 1.1 riastrad EVCNT_ATTACH_STATIC(cbcdec_unaligned_evcnt);
368 1.1 riastrad
369 1.1 riastrad static void
370 1.1 riastrad aesvia_cbc_dec(const struct aesdec *dec, const uint8_t in[static 16],
371 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
372 1.1 riastrad uint32_t nrounds)
373 1.1 riastrad {
374 1.1 riastrad const uint32_t cw0 = aesvia_keylen_cw0(nrounds);
375 1.1 riastrad
376 1.1 riastrad KASSERT(nbytes % 16 == 0);
377 1.1 riastrad if (nbytes == 0)
378 1.1 riastrad return;
379 1.1 riastrad
380 1.1 riastrad fpu_kern_enter();
381 1.1 riastrad aesvia_reload_keys();
382 1.1 riastrad if ((((uintptr_t)in | (uintptr_t)out | (uintptr_t)iv) & 0xf) == 0) {
383 1.1 riastrad cbcdec_aligned_evcnt.ev_count++;
384 1.1 riastrad aesvia_cbc_dec1(dec, in, out, nbytes/16, iv, cw0);
385 1.1 riastrad } else {
386 1.1 riastrad cbcdec_unaligned_evcnt.ev_count++;
387 1.1 riastrad uint8_t iv0[16] __aligned(16);
388 1.1 riastrad uint8_t cv[16] __aligned(16);
389 1.1 riastrad uint8_t tmp[16] __aligned(16);
390 1.1 riastrad
391 1.1 riastrad memcpy(iv0, iv, 16);
392 1.1 riastrad memcpy(cv, in + nbytes - 16, 16);
393 1.1 riastrad memcpy(iv, cv, 16);
394 1.1 riastrad
395 1.1 riastrad for (;;) {
396 1.1 riastrad aesvia_dec1(dec, cv, tmp, cw0);
397 1.1 riastrad if ((nbytes -= 16) == 0)
398 1.1 riastrad break;
399 1.1 riastrad memcpy(cv, in + nbytes - 16, 16);
400 1.1 riastrad xor128(tmp, tmp, cv);
401 1.1 riastrad memcpy(out + nbytes, tmp, 16);
402 1.1 riastrad }
403 1.1 riastrad
404 1.1 riastrad xor128(tmp, tmp, iv0);
405 1.1 riastrad memcpy(out, tmp, 16);
406 1.1 riastrad explicit_memset(tmp, 0, sizeof tmp);
407 1.1 riastrad }
408 1.1 riastrad fpu_kern_leave();
409 1.1 riastrad }
410 1.1 riastrad
411 1.1 riastrad static inline void
412 1.1 riastrad aesvia_xts_update(uint32_t *t0, uint32_t *t1, uint32_t *t2, uint32_t *t3)
413 1.1 riastrad {
414 1.1 riastrad uint32_t s0, s1, s2, s3;
415 1.1 riastrad
416 1.1 riastrad s0 = *t0 >> 31;
417 1.1 riastrad s1 = *t1 >> 31;
418 1.1 riastrad s2 = *t2 >> 31;
419 1.1 riastrad s3 = *t3 >> 31;
420 1.1 riastrad *t0 = (*t0 << 1) ^ (-s3 & 0x87);
421 1.1 riastrad *t1 = (*t1 << 1) ^ s0;
422 1.1 riastrad *t2 = (*t2 << 1) ^ s1;
423 1.1 riastrad *t3 = (*t3 << 1) ^ s2;
424 1.1 riastrad }
425 1.1 riastrad
426 1.1 riastrad static int
427 1.1 riastrad aesvia_xts_update_selftest(void)
428 1.1 riastrad {
429 1.1 riastrad static const struct {
430 1.1 riastrad uint32_t in[4], out[4];
431 1.1 riastrad } cases[] = {
432 1.1 riastrad { {1}, {2} },
433 1.1 riastrad { {0x80000000U,0,0,0}, {0,1,0,0} },
434 1.1 riastrad { {0,0x80000000U,0,0}, {0,0,1,0} },
435 1.1 riastrad { {0,0,0x80000000U,0}, {0,0,0,1} },
436 1.1 riastrad { {0,0,0,0x80000000U}, {0x87,0,0,0} },
437 1.1 riastrad { {0,0x80000000U,0,0x80000000U}, {0x87,0,1,0} },
438 1.1 riastrad };
439 1.1 riastrad unsigned i;
440 1.1 riastrad uint32_t t0, t1, t2, t3;
441 1.1 riastrad
442 1.1 riastrad for (i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) {
443 1.1 riastrad t0 = cases[i].in[0];
444 1.1 riastrad t1 = cases[i].in[1];
445 1.1 riastrad t2 = cases[i].in[2];
446 1.1 riastrad t3 = cases[i].in[3];
447 1.1 riastrad aesvia_xts_update(&t0, &t1, &t2, &t3);
448 1.1 riastrad if (t0 != cases[i].out[0] ||
449 1.1 riastrad t1 != cases[i].out[1] ||
450 1.1 riastrad t2 != cases[i].out[2] ||
451 1.1 riastrad t3 != cases[i].out[3])
452 1.1 riastrad return -1;
453 1.1 riastrad }
454 1.1 riastrad
455 1.1 riastrad /* Success! */
456 1.1 riastrad return 0;
457 1.1 riastrad }
458 1.1 riastrad
459 1.1 riastrad static struct evcnt xtsenc_aligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
460 1.1 riastrad NULL, "aesvia", "xtsenc aligned");
461 1.1 riastrad EVCNT_ATTACH_STATIC(xtsenc_aligned_evcnt);
462 1.1 riastrad static struct evcnt xtsenc_unaligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
463 1.1 riastrad NULL, "aesvia", "xtsenc unaligned");
464 1.1 riastrad EVCNT_ATTACH_STATIC(xtsenc_unaligned_evcnt);
465 1.1 riastrad
466 1.1 riastrad static void
467 1.1 riastrad aesvia_xts_enc(const struct aesenc *enc, const uint8_t in[static 16],
468 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
469 1.1 riastrad uint32_t nrounds)
470 1.1 riastrad {
471 1.1 riastrad const uint32_t cw0 = aesvia_keylen_cw0(nrounds);
472 1.1 riastrad uint32_t t[4];
473 1.1 riastrad
474 1.1 riastrad KASSERT(nbytes % 16 == 0);
475 1.1 riastrad
476 1.1 riastrad memcpy(t, tweak, 16);
477 1.1 riastrad
478 1.1 riastrad fpu_kern_enter();
479 1.1 riastrad aesvia_reload_keys();
480 1.1 riastrad if ((((uintptr_t)in | (uintptr_t)out) & 0xf) == 0) {
481 1.1 riastrad xtsenc_aligned_evcnt.ev_count++;
482 1.1 riastrad unsigned lastblock = 0;
483 1.1 riastrad
484 1.1 riastrad /*
485 1.1 riastrad * Make sure the last block is not the last block of a
486 1.1 riastrad * page. (Note that we store the AES input in `out' as
487 1.1 riastrad * a temporary buffer, rather than reading it directly
488 1.1 riastrad * from `in', since we have to combine the tweak
489 1.1 riastrad * first.)
490 1.1 riastrad */
491 1.1 riastrad lastblock = 16*(((uintptr_t)(out + nbytes) & 0xfff) == 0);
492 1.1 riastrad nbytes -= lastblock;
493 1.1 riastrad
494 1.1 riastrad for (; nbytes; nbytes -= 16, in += 16, out += 16) {
495 1.1 riastrad xor128(out, in, t);
496 1.1 riastrad aesvia_enc1(enc, out, out, cw0);
497 1.1 riastrad xor128(out, out, t);
498 1.1 riastrad aesvia_xts_update(&t[0], &t[1], &t[2], &t[3]);
499 1.1 riastrad }
500 1.1 riastrad
501 1.1 riastrad /* Handle the last block of a page, if necessary. */
502 1.1 riastrad if (lastblock) {
503 1.1 riastrad uint8_t buf[16] __aligned(16);
504 1.1 riastrad xor128(buf, in, t);
505 1.1 riastrad aesvia_enc1(enc, buf, out, cw0);
506 1.1 riastrad explicit_memset(buf, 0, sizeof buf);
507 1.1 riastrad }
508 1.1 riastrad } else {
509 1.1 riastrad xtsenc_unaligned_evcnt.ev_count++;
510 1.1 riastrad uint8_t buf[16] __aligned(16);
511 1.1 riastrad
512 1.1 riastrad for (; nbytes; nbytes -= 16, in += 16, out += 16) {
513 1.1 riastrad memcpy(buf, in, 16);
514 1.1 riastrad xor128(buf, buf, t);
515 1.1 riastrad aesvia_enc1(enc, buf, buf, cw0);
516 1.1 riastrad xor128(buf, buf, t);
517 1.1 riastrad memcpy(out, buf, 16);
518 1.1 riastrad aesvia_xts_update(&t[0], &t[1], &t[2], &t[3]);
519 1.1 riastrad }
520 1.1 riastrad
521 1.1 riastrad explicit_memset(buf, 0, sizeof buf);
522 1.1 riastrad }
523 1.1 riastrad fpu_kern_leave();
524 1.1 riastrad
525 1.1 riastrad memcpy(tweak, t, 16);
526 1.1 riastrad explicit_memset(t, 0, sizeof t);
527 1.1 riastrad }
528 1.1 riastrad
529 1.1 riastrad static struct evcnt xtsdec_aligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
530 1.1 riastrad NULL, "aesvia", "xtsdec aligned");
531 1.1 riastrad EVCNT_ATTACH_STATIC(xtsdec_aligned_evcnt);
532 1.1 riastrad static struct evcnt xtsdec_unaligned_evcnt = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
533 1.1 riastrad NULL, "aesvia", "xtsdec unaligned");
534 1.1 riastrad EVCNT_ATTACH_STATIC(xtsdec_unaligned_evcnt);
535 1.1 riastrad
536 1.1 riastrad static void
537 1.1 riastrad aesvia_xts_dec(const struct aesdec *dec, const uint8_t in[static 16],
538 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
539 1.1 riastrad uint32_t nrounds)
540 1.1 riastrad {
541 1.1 riastrad const uint32_t cw0 = aesvia_keylen_cw0(nrounds);
542 1.1 riastrad uint32_t t[4];
543 1.1 riastrad
544 1.1 riastrad KASSERT(nbytes % 16 == 0);
545 1.1 riastrad
546 1.1 riastrad memcpy(t, tweak, 16);
547 1.1 riastrad
548 1.1 riastrad fpu_kern_enter();
549 1.1 riastrad aesvia_reload_keys();
550 1.1 riastrad if ((((uintptr_t)in | (uintptr_t)out) & 0xf) == 0) {
551 1.1 riastrad xtsdec_aligned_evcnt.ev_count++;
552 1.1 riastrad unsigned lastblock = 0;
553 1.1 riastrad
554 1.1 riastrad /*
555 1.1 riastrad * Make sure the last block is not the last block of a
556 1.1 riastrad * page. (Note that we store the AES input in `out' as
557 1.1 riastrad * a temporary buffer, rather than reading it directly
558 1.1 riastrad * from `in', since we have to combine the tweak
559 1.1 riastrad * first.)
560 1.1 riastrad */
561 1.1 riastrad lastblock = 16*(((uintptr_t)(out + nbytes) & 0xfff) == 0);
562 1.1 riastrad nbytes -= lastblock;
563 1.1 riastrad
564 1.1 riastrad for (; nbytes; nbytes -= 16, in += 16, out += 16) {
565 1.1 riastrad xor128(out, in, t);
566 1.1 riastrad aesvia_dec1(dec, out, out, cw0);
567 1.1 riastrad xor128(out, out, t);
568 1.1 riastrad aesvia_xts_update(&t[0], &t[1], &t[2], &t[3]);
569 1.1 riastrad }
570 1.1 riastrad
571 1.1 riastrad /* Handle the last block of a page, if necessary. */
572 1.1 riastrad if (lastblock) {
573 1.1 riastrad uint8_t buf[16] __aligned(16);
574 1.1 riastrad xor128(buf, in, t);
575 1.1 riastrad aesvia_dec1(dec, buf, out, cw0);
576 1.1 riastrad explicit_memset(buf, 0, sizeof buf);
577 1.1 riastrad }
578 1.1 riastrad } else {
579 1.1 riastrad xtsdec_unaligned_evcnt.ev_count++;
580 1.1 riastrad uint8_t buf[16] __aligned(16);
581 1.1 riastrad
582 1.1 riastrad for (; nbytes; nbytes -= 16, in += 16, out += 16) {
583 1.1 riastrad memcpy(buf, in, 16);
584 1.1 riastrad xor128(buf, buf, t);
585 1.1 riastrad aesvia_dec1(dec, buf, buf, cw0);
586 1.1 riastrad xor128(buf, buf, t);
587 1.1 riastrad memcpy(out, buf, 16);
588 1.1 riastrad aesvia_xts_update(&t[0], &t[1], &t[2], &t[3]);
589 1.1 riastrad }
590 1.1 riastrad
591 1.1 riastrad explicit_memset(buf, 0, sizeof buf);
592 1.1 riastrad }
593 1.1 riastrad fpu_kern_leave();
594 1.1 riastrad
595 1.1 riastrad memcpy(tweak, t, 16);
596 1.1 riastrad explicit_memset(t, 0, sizeof t);
597 1.1 riastrad }
598 1.1 riastrad
599 1.1 riastrad static int
600 1.1 riastrad aesvia_probe(void)
601 1.1 riastrad {
602 1.1 riastrad
603 1.1 riastrad /* Verify that the CPU advertises VIA ACE support. */
604 1.1 riastrad if ((cpu_feature[4] & CPUID_VIA_HAS_ACE) == 0)
605 1.1 riastrad return -1;
606 1.1 riastrad
607 1.1 riastrad /* Verify that our XTS tweak update logic works. */
608 1.1 riastrad if (aesvia_xts_update_selftest())
609 1.1 riastrad return -1;
610 1.1 riastrad
611 1.1 riastrad /* Success! */
612 1.1 riastrad return 0;
613 1.1 riastrad }
614 1.1 riastrad
615 1.1 riastrad struct aes_impl aes_via_impl = {
616 1.1 riastrad .ai_name = "VIA ACE",
617 1.1 riastrad .ai_probe = aesvia_probe,
618 1.1 riastrad .ai_setenckey = aesvia_setenckey,
619 1.1 riastrad .ai_setdeckey = aesvia_setdeckey,
620 1.1 riastrad .ai_enc = aesvia_enc,
621 1.1 riastrad .ai_dec = aesvia_dec,
622 1.1 riastrad .ai_cbc_enc = aesvia_cbc_enc,
623 1.1 riastrad .ai_cbc_dec = aesvia_cbc_dec,
624 1.1 riastrad .ai_xts_enc = aesvia_xts_enc,
625 1.1 riastrad .ai_xts_dec = aesvia_xts_dec,
626 1.1 riastrad };
627