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