aes_neon_subr.c revision 1.7 1 1.7 riastrad /* $NetBSD: aes_neon_subr.c,v 1.7 2020/08/09 02:48:38 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.7 riastrad __KERNEL_RCSID(1, "$NetBSD: aes_neon_subr.c,v 1.7 2020/08/09 02:48:38 riastradh Exp $");
31 1.1 riastrad
32 1.2 riastrad #ifdef _KERNEL
33 1.1 riastrad #include <sys/systm.h>
34 1.1 riastrad #include <lib/libkern/libkern.h>
35 1.2 riastrad #else
36 1.2 riastrad #include <assert.h>
37 1.2 riastrad #include <inttypes.h>
38 1.2 riastrad #include <stdio.h>
39 1.5 riastrad #include <string.h>
40 1.2 riastrad #define KASSERT assert
41 1.2 riastrad #endif
42 1.1 riastrad
43 1.1 riastrad #include <crypto/aes/arch/arm/aes_neon.h>
44 1.1 riastrad
45 1.1 riastrad #include "aes_neon_impl.h"
46 1.1 riastrad
47 1.1 riastrad static inline uint8x16_t
48 1.1 riastrad loadblock(const void *in)
49 1.1 riastrad {
50 1.1 riastrad return vld1q_u8(in);
51 1.1 riastrad }
52 1.1 riastrad
53 1.1 riastrad static inline void
54 1.1 riastrad storeblock(void *out, uint8x16_t block)
55 1.1 riastrad {
56 1.1 riastrad vst1q_u8(out, block);
57 1.1 riastrad }
58 1.1 riastrad
59 1.1 riastrad void
60 1.1 riastrad aes_neon_enc(const struct aesenc *enc, const uint8_t in[static 16],
61 1.1 riastrad uint8_t out[static 16], uint32_t nrounds)
62 1.1 riastrad {
63 1.1 riastrad uint8x16_t block;
64 1.1 riastrad
65 1.1 riastrad block = loadblock(in);
66 1.1 riastrad block = aes_neon_enc1(enc, block, nrounds);
67 1.1 riastrad storeblock(out, block);
68 1.1 riastrad }
69 1.1 riastrad
70 1.1 riastrad void
71 1.1 riastrad aes_neon_dec(const struct aesdec *dec, const uint8_t in[static 16],
72 1.1 riastrad uint8_t out[static 16], uint32_t nrounds)
73 1.1 riastrad {
74 1.1 riastrad uint8x16_t block;
75 1.1 riastrad
76 1.1 riastrad block = loadblock(in);
77 1.1 riastrad block = aes_neon_dec1(dec, block, nrounds);
78 1.1 riastrad storeblock(out, block);
79 1.1 riastrad }
80 1.1 riastrad
81 1.1 riastrad void
82 1.1 riastrad aes_neon_cbc_enc(const struct aesenc *enc, const uint8_t in[static 16],
83 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
84 1.1 riastrad uint32_t nrounds)
85 1.1 riastrad {
86 1.1 riastrad uint8x16_t cv;
87 1.1 riastrad
88 1.1 riastrad KASSERT(nbytes);
89 1.1 riastrad
90 1.1 riastrad cv = loadblock(iv);
91 1.1 riastrad for (; nbytes; nbytes -= 16, in += 16, out += 16) {
92 1.1 riastrad cv ^= loadblock(in);
93 1.1 riastrad cv = aes_neon_enc1(enc, cv, nrounds);
94 1.1 riastrad storeblock(out, cv);
95 1.1 riastrad }
96 1.1 riastrad storeblock(iv, cv);
97 1.1 riastrad }
98 1.1 riastrad
99 1.1 riastrad void
100 1.1 riastrad aes_neon_cbc_dec(const struct aesdec *dec, const uint8_t in[static 16],
101 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
102 1.1 riastrad uint32_t nrounds)
103 1.1 riastrad {
104 1.1 riastrad uint8x16_t iv0, cv, b;
105 1.1 riastrad
106 1.1 riastrad KASSERT(nbytes);
107 1.1 riastrad KASSERT(nbytes % 16 == 0);
108 1.1 riastrad
109 1.1 riastrad iv0 = loadblock(iv);
110 1.1 riastrad cv = loadblock(in + nbytes - 16);
111 1.1 riastrad storeblock(iv, cv);
112 1.1 riastrad
113 1.4 riastrad if (nbytes % 32) {
114 1.4 riastrad KASSERT(nbytes % 32 == 16);
115 1.1 riastrad b = aes_neon_dec1(dec, cv, nrounds);
116 1.1 riastrad if ((nbytes -= 16) == 0)
117 1.4 riastrad goto out;
118 1.4 riastrad cv = loadblock(in + nbytes - 16);
119 1.4 riastrad storeblock(out + nbytes, cv ^ b);
120 1.4 riastrad }
121 1.4 riastrad
122 1.4 riastrad for (;;) {
123 1.4 riastrad uint8x16x2_t b2;
124 1.4 riastrad
125 1.4 riastrad KASSERT(nbytes >= 32);
126 1.4 riastrad
127 1.4 riastrad b2.val[1] = cv;
128 1.4 riastrad b2.val[0] = cv = loadblock(in + nbytes - 32);
129 1.4 riastrad b2 = aes_neon_dec2(dec, b2, nrounds);
130 1.4 riastrad storeblock(out + nbytes - 16, cv ^ b2.val[1]);
131 1.4 riastrad if ((nbytes -= 32) == 0) {
132 1.4 riastrad b = b2.val[0];
133 1.4 riastrad goto out;
134 1.4 riastrad }
135 1.1 riastrad cv = loadblock(in + nbytes - 16);
136 1.4 riastrad storeblock(out + nbytes, cv ^ b2.val[0]);
137 1.1 riastrad }
138 1.4 riastrad
139 1.4 riastrad out: storeblock(out, b ^ iv0);
140 1.1 riastrad }
141 1.1 riastrad
142 1.1 riastrad static inline uint8x16_t
143 1.1 riastrad aes_neon_xts_update(uint8x16_t t8)
144 1.1 riastrad {
145 1.1 riastrad const int32x4_t zero = vdupq_n_s32(0);
146 1.5 riastrad /* (0x87,1,1,1) */
147 1.5 riastrad const uint32x4_t carry = vsetq_lane_u32(0x87, vdupq_n_u32(1), 0);
148 1.1 riastrad int32x4_t t, t_;
149 1.1 riastrad uint32x4_t mask;
150 1.1 riastrad
151 1.1 riastrad t = vreinterpretq_s32_u8(t8);
152 1.1 riastrad mask = vcltq_s32(t, zero); /* -1 if high bit set else 0 */
153 1.1 riastrad mask = vextq_u32(mask, mask, 3); /* rotate quarters */
154 1.7 riastrad t_ = vshlq_n_s32(t, 1); /* shift */
155 1.1 riastrad t_ ^= carry & mask;
156 1.1 riastrad
157 1.1 riastrad return vreinterpretq_u8_s32(t_);
158 1.1 riastrad }
159 1.1 riastrad
160 1.1 riastrad static int
161 1.1 riastrad aes_neon_xts_update_selftest(void)
162 1.1 riastrad {
163 1.1 riastrad static const struct {
164 1.5 riastrad uint8_t in[16], out[16];
165 1.1 riastrad } cases[] = {
166 1.5 riastrad [0] = { {1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0},
167 1.5 riastrad {2,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0} },
168 1.5 riastrad [1] = { {0,0,0,0x80, 0,0,0,0, 0,0,0,0, 0,0,0,0},
169 1.5 riastrad {0,0,0,0, 1,0,0,0, 0,0,0,0, 0,0,0,0} },
170 1.5 riastrad [2] = { {0,0,0,0, 0,0,0,0x80, 0,0,0,0, 0,0,0,0},
171 1.5 riastrad {0,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0} },
172 1.5 riastrad [3] = { {0,0,0,0, 0,0,0,0, 0,0,0,0x80, 0,0,0,0},
173 1.5 riastrad {0,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0} },
174 1.5 riastrad [4] = { {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0x80},
175 1.5 riastrad {0x87,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0} },
176 1.5 riastrad [5] = { {0,0,0,0, 0,0,0,0x80, 0,0,0,0, 0,0,0,0x80},
177 1.5 riastrad {0x87,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0} },
178 1.1 riastrad };
179 1.1 riastrad unsigned i;
180 1.5 riastrad uint8_t t[16];
181 1.1 riastrad int result = 0;
182 1.1 riastrad
183 1.1 riastrad for (i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) {
184 1.5 riastrad storeblock(t, aes_neon_xts_update(loadblock(cases[i].in)));
185 1.5 riastrad if (memcmp(t, cases[i].out, 16)) {
186 1.5 riastrad char buf[33];
187 1.5 riastrad unsigned j;
188 1.5 riastrad
189 1.5 riastrad for (j = 0; j < 16; j++) {
190 1.5 riastrad snprintf(buf + 2*j, sizeof(buf) - 2*j,
191 1.5 riastrad " %02hhx", t[j]);
192 1.5 riastrad }
193 1.5 riastrad printf("%s %u: %s\n", __func__, i, buf);
194 1.1 riastrad result = -1;
195 1.1 riastrad }
196 1.1 riastrad }
197 1.1 riastrad
198 1.1 riastrad return result;
199 1.1 riastrad }
200 1.1 riastrad
201 1.1 riastrad void
202 1.1 riastrad aes_neon_xts_enc(const struct aesenc *enc, const uint8_t in[static 16],
203 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
204 1.1 riastrad uint32_t nrounds)
205 1.1 riastrad {
206 1.1 riastrad uint8x16_t t, b;
207 1.1 riastrad
208 1.1 riastrad KASSERT(nbytes);
209 1.1 riastrad KASSERT(nbytes % 16 == 0);
210 1.1 riastrad
211 1.1 riastrad t = loadblock(tweak);
212 1.4 riastrad if (nbytes % 32) {
213 1.4 riastrad KASSERT(nbytes % 32 == 16);
214 1.1 riastrad b = t ^ loadblock(in);
215 1.1 riastrad b = aes_neon_enc1(enc, b, nrounds);
216 1.1 riastrad storeblock(out, t ^ b);
217 1.1 riastrad t = aes_neon_xts_update(t);
218 1.4 riastrad nbytes -= 16;
219 1.4 riastrad in += 16;
220 1.4 riastrad out += 16;
221 1.4 riastrad }
222 1.4 riastrad for (; nbytes; nbytes -= 32, in += 32, out += 32) {
223 1.4 riastrad uint8x16_t t1;
224 1.4 riastrad uint8x16x2_t b2;
225 1.4 riastrad
226 1.4 riastrad t1 = aes_neon_xts_update(t);
227 1.4 riastrad b2.val[0] = t ^ loadblock(in);
228 1.4 riastrad b2.val[1] = t1 ^ loadblock(in + 16);
229 1.4 riastrad b2 = aes_neon_enc2(enc, b2, nrounds);
230 1.4 riastrad storeblock(out, b2.val[0] ^ t);
231 1.4 riastrad storeblock(out + 16, b2.val[1] ^ t1);
232 1.4 riastrad
233 1.4 riastrad t = aes_neon_xts_update(t1);
234 1.1 riastrad }
235 1.1 riastrad storeblock(tweak, t);
236 1.1 riastrad }
237 1.1 riastrad
238 1.1 riastrad void
239 1.1 riastrad aes_neon_xts_dec(const struct aesdec *dec, const uint8_t in[static 16],
240 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
241 1.1 riastrad uint32_t nrounds)
242 1.1 riastrad {
243 1.1 riastrad uint8x16_t t, b;
244 1.1 riastrad
245 1.1 riastrad KASSERT(nbytes);
246 1.1 riastrad KASSERT(nbytes % 16 == 0);
247 1.1 riastrad
248 1.1 riastrad t = loadblock(tweak);
249 1.4 riastrad if (nbytes % 32) {
250 1.4 riastrad KASSERT(nbytes % 32 == 16);
251 1.1 riastrad b = t ^ loadblock(in);
252 1.1 riastrad b = aes_neon_dec1(dec, b, nrounds);
253 1.1 riastrad storeblock(out, t ^ b);
254 1.1 riastrad t = aes_neon_xts_update(t);
255 1.4 riastrad nbytes -= 16;
256 1.4 riastrad in += 16;
257 1.4 riastrad out += 16;
258 1.4 riastrad }
259 1.4 riastrad for (; nbytes; nbytes -= 32, in += 32, out += 32) {
260 1.4 riastrad uint8x16_t t1;
261 1.4 riastrad uint8x16x2_t b2;
262 1.4 riastrad
263 1.4 riastrad t1 = aes_neon_xts_update(t);
264 1.4 riastrad b2.val[0] = t ^ loadblock(in);
265 1.4 riastrad b2.val[1] = t1 ^ loadblock(in + 16);
266 1.4 riastrad b2 = aes_neon_dec2(dec, b2, nrounds);
267 1.4 riastrad storeblock(out, b2.val[0] ^ t);
268 1.4 riastrad storeblock(out + 16, b2.val[1] ^ t1);
269 1.4 riastrad
270 1.4 riastrad t = aes_neon_xts_update(t1);
271 1.1 riastrad }
272 1.1 riastrad storeblock(tweak, t);
273 1.1 riastrad }
274 1.1 riastrad
275 1.3 riastrad void
276 1.3 riastrad aes_neon_cbcmac_update1(const struct aesenc *enc, const uint8_t in[static 16],
277 1.3 riastrad size_t nbytes, uint8_t auth0[static 16], uint32_t nrounds)
278 1.3 riastrad {
279 1.3 riastrad uint8x16_t auth;
280 1.3 riastrad
281 1.3 riastrad KASSERT(nbytes);
282 1.3 riastrad KASSERT(nbytes % 16 == 0);
283 1.3 riastrad
284 1.3 riastrad auth = loadblock(auth0);
285 1.3 riastrad for (; nbytes; nbytes -= 16, in += 16)
286 1.3 riastrad auth = aes_neon_enc1(enc, auth ^ loadblock(in), nrounds);
287 1.3 riastrad storeblock(auth0, auth);
288 1.3 riastrad }
289 1.3 riastrad
290 1.3 riastrad void
291 1.3 riastrad aes_neon_ccm_enc1(const struct aesenc *enc, const uint8_t in[static 16],
292 1.3 riastrad uint8_t out[static 16], size_t nbytes, uint8_t authctr[static 32],
293 1.3 riastrad uint32_t nrounds)
294 1.3 riastrad {
295 1.5 riastrad /* (0,0,0,1) */
296 1.5 riastrad const uint32x4_t ctr32_inc = vsetq_lane_u32(1, vdupq_n_u32(0), 3);
297 1.3 riastrad uint8x16_t auth, ptxt, ctr_be;
298 1.3 riastrad uint32x4_t ctr;
299 1.3 riastrad
300 1.3 riastrad KASSERT(nbytes);
301 1.3 riastrad KASSERT(nbytes % 16 == 0);
302 1.3 riastrad
303 1.3 riastrad auth = loadblock(authctr);
304 1.3 riastrad ctr_be = loadblock(authctr + 16);
305 1.5 riastrad ctr = vreinterpretq_u32_u8(vrev32q_u8(ctr_be));
306 1.3 riastrad for (; nbytes; nbytes -= 16, in += 16, out += 16) {
307 1.4 riastrad uint8x16x2_t b2;
308 1.3 riastrad ptxt = loadblock(in);
309 1.3 riastrad ctr = vaddq_u32(ctr, ctr32_inc);
310 1.5 riastrad ctr_be = vrev32q_u8(vreinterpretq_u8_u32(ctr));
311 1.4 riastrad
312 1.4 riastrad b2.val[0] = auth ^ ptxt;
313 1.4 riastrad b2.val[1] = ctr_be;
314 1.4 riastrad b2 = aes_neon_enc2(enc, b2, nrounds);
315 1.4 riastrad auth = b2.val[0];
316 1.4 riastrad storeblock(out, ptxt ^ b2.val[1]);
317 1.3 riastrad }
318 1.3 riastrad storeblock(authctr, auth);
319 1.3 riastrad storeblock(authctr + 16, ctr_be);
320 1.3 riastrad }
321 1.3 riastrad
322 1.3 riastrad void
323 1.3 riastrad aes_neon_ccm_dec1(const struct aesenc *enc, const uint8_t in[static 16],
324 1.3 riastrad uint8_t out[static 16], size_t nbytes, uint8_t authctr[static 32],
325 1.3 riastrad uint32_t nrounds)
326 1.3 riastrad {
327 1.5 riastrad /* (0,0,0,1) */
328 1.5 riastrad const uint32x4_t ctr32_inc = vsetq_lane_u32(1, vdupq_n_u32(0), 3);
329 1.4 riastrad uint8x16_t auth, ctr_be, ptxt, pad;
330 1.3 riastrad uint32x4_t ctr;
331 1.3 riastrad
332 1.3 riastrad KASSERT(nbytes);
333 1.3 riastrad KASSERT(nbytes % 16 == 0);
334 1.3 riastrad
335 1.3 riastrad ctr_be = loadblock(authctr + 16);
336 1.5 riastrad ctr = vreinterpretq_u32_u8(vrev32q_u8(ctr_be));
337 1.4 riastrad ctr = vaddq_u32(ctr, ctr32_inc);
338 1.5 riastrad ctr_be = vrev32q_u8(vreinterpretq_u8_u32(ctr));
339 1.4 riastrad pad = aes_neon_enc1(enc, ctr_be, nrounds);
340 1.4 riastrad auth = loadblock(authctr);
341 1.4 riastrad for (;; in += 16, out += 16) {
342 1.4 riastrad uint8x16x2_t b2;
343 1.4 riastrad
344 1.4 riastrad ptxt = loadblock(in) ^ pad;
345 1.4 riastrad auth ^= ptxt;
346 1.4 riastrad storeblock(out, ptxt);
347 1.4 riastrad
348 1.4 riastrad if ((nbytes -= 16) == 0)
349 1.4 riastrad break;
350 1.4 riastrad
351 1.3 riastrad ctr = vaddq_u32(ctr, ctr32_inc);
352 1.5 riastrad ctr_be = vrev32q_u8(vreinterpretq_u8_u32(ctr));
353 1.4 riastrad b2.val[0] = auth;
354 1.4 riastrad b2.val[1] = ctr_be;
355 1.4 riastrad b2 = aes_neon_enc2(enc, b2, nrounds);
356 1.4 riastrad auth = b2.val[0];
357 1.4 riastrad pad = b2.val[1];
358 1.3 riastrad }
359 1.4 riastrad auth = aes_neon_enc1(enc, auth, nrounds);
360 1.3 riastrad storeblock(authctr, auth);
361 1.3 riastrad storeblock(authctr + 16, ctr_be);
362 1.3 riastrad }
363 1.3 riastrad
364 1.1 riastrad int
365 1.1 riastrad aes_neon_selftest(void)
366 1.1 riastrad {
367 1.1 riastrad
368 1.1 riastrad if (aes_neon_xts_update_selftest())
369 1.1 riastrad return -1;
370 1.1 riastrad
371 1.1 riastrad return 0;
372 1.1 riastrad }
373