aes_neon.c revision 1.4 1 1.4 riastrad /* $NetBSD: aes_neon.c,v 1.4 2020/07/28 20:11:09 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 /*
30 1.1 riastrad * Permutation-based AES using NEON, derived from Mike Hamburg's VPAES
31 1.1 riastrad * software, at <https://crypto.stanford.edu/vpaes/>, described in
32 1.1 riastrad *
33 1.1 riastrad * Mike Hamburg, `Accelerating AES with Vector Permute
34 1.1 riastrad * Instructions', in Christophe Clavier and Kris Gaj (eds.),
35 1.1 riastrad * Cryptographic Hardware and Embedded Systems -- CHES 2009,
36 1.1 riastrad * Springer LNCS 5747, pp. 18-32.
37 1.1 riastrad *
38 1.1 riastrad * https://link.springer.com/chapter/10.1007/978-3-642-04138-9_2
39 1.1 riastrad */
40 1.1 riastrad
41 1.1 riastrad #include <sys/cdefs.h>
42 1.4 riastrad __KERNEL_RCSID(1, "$NetBSD: aes_neon.c,v 1.4 2020/07/28 20:11:09 riastradh Exp $");
43 1.1 riastrad
44 1.1 riastrad #include <sys/types.h>
45 1.1 riastrad
46 1.3 riastrad #ifdef _KERNEL
47 1.1 riastrad #include <sys/systm.h>
48 1.3 riastrad #else
49 1.3 riastrad #include <err.h>
50 1.3 riastrad #define panic(fmt, args...) err(1, fmt, ##args)
51 1.3 riastrad #endif
52 1.1 riastrad
53 1.1 riastrad #include "aes_neon_impl.h"
54 1.1 riastrad
55 1.2 riastrad #ifdef __aarch64__
56 1.2 riastrad #define __aarch64_used
57 1.2 riastrad #else
58 1.2 riastrad #define __aarch64_used __unused
59 1.2 riastrad #endif
60 1.2 riastrad
61 1.1 riastrad static const uint8x16_t
62 1.1 riastrad mc_forward[4] = {
63 1.1 riastrad {0x01,0x02,0x03,0x00,0x05,0x06,0x07,0x04,
64 1.1 riastrad 0x09,0x0A,0x0B,0x08,0x0D,0x0E,0x0F,0x0C},
65 1.1 riastrad {0x05,0x06,0x07,0x04,0x09,0x0A,0x0B,0x08,
66 1.1 riastrad 0x0D,0x0E,0x0F,0x0C,0x01,0x02,0x03,0x00},
67 1.1 riastrad {0x09,0x0A,0x0B,0x08,0x0D,0x0E,0x0F,0x0C,
68 1.1 riastrad 0x01,0x02,0x03,0x00,0x05,0x06,0x07,0x04},
69 1.1 riastrad {0x0D,0x0E,0x0F,0x0C,0x01,0x02,0x03,0x00,
70 1.1 riastrad 0x05,0x06,0x07,0x04,0x09,0x0A,0x0B,0x08},
71 1.1 riastrad },
72 1.2 riastrad mc_backward[4] __aarch64_used = {
73 1.1 riastrad {0x03,0x00,0x01,0x02,0x07,0x04,0x05,0x06,
74 1.1 riastrad 0x0B,0x08,0x09,0x0A,0x0F,0x0C,0x0D,0x0E},
75 1.1 riastrad {0x0F,0x0C,0x0D,0x0E,0x03,0x00,0x01,0x02,
76 1.1 riastrad 0x07,0x04,0x05,0x06,0x0B,0x08,0x09,0x0A},
77 1.1 riastrad {0x0B,0x08,0x09,0x0A,0x0F,0x0C,0x0D,0x0E,
78 1.1 riastrad 0x03,0x00,0x01,0x02,0x07,0x04,0x05,0x06},
79 1.1 riastrad {0x07,0x04,0x05,0x06,0x0B,0x08,0x09,0x0A,
80 1.1 riastrad 0x0F,0x0C,0x0D,0x0E,0x03,0x00,0x01,0x02},
81 1.1 riastrad },
82 1.2 riastrad ipt[2] __aarch64_used = {
83 1.1 riastrad {0x00,0x70,0x2A,0x5A,0x98,0xE8,0xB2,0xC2,
84 1.1 riastrad 0x08,0x78,0x22,0x52,0x90,0xE0,0xBA,0xCA},
85 1.1 riastrad {0x00,0x4D,0x7C,0x31,0x7D,0x30,0x01,0x4C,
86 1.1 riastrad 0x81,0xCC,0xFD,0xB0,0xFC,0xB1,0x80,0xCD},
87 1.1 riastrad },
88 1.1 riastrad opt[2] = {
89 1.1 riastrad {0x00,0x60,0xB6,0xD6,0x29,0x49,0x9F,0xFF,
90 1.1 riastrad 0x08,0x68,0xBE,0xDE,0x21,0x41,0x97,0xF7},
91 1.1 riastrad {0x00,0xEC,0xBC,0x50,0x51,0xBD,0xED,0x01,
92 1.1 riastrad 0xE0,0x0C,0x5C,0xB0,0xB1,0x5D,0x0D,0xE1},
93 1.1 riastrad },
94 1.2 riastrad dipt[2] __aarch64_used = {
95 1.1 riastrad {0x00,0x5F,0x54,0x0B,0x04,0x5B,0x50,0x0F,
96 1.1 riastrad 0x1A,0x45,0x4E,0x11,0x1E,0x41,0x4A,0x15},
97 1.1 riastrad {0x00,0x65,0x05,0x60,0xE6,0x83,0xE3,0x86,
98 1.1 riastrad 0x94,0xF1,0x91,0xF4,0x72,0x17,0x77,0x12},
99 1.1 riastrad },
100 1.2 riastrad sb1[2] __aarch64_used = {
101 1.1 riastrad {0x00,0x3E,0x50,0xCB,0x8F,0xE1,0x9B,0xB1,
102 1.1 riastrad 0x44,0xF5,0x2A,0x14,0x6E,0x7A,0xDF,0xA5},
103 1.1 riastrad {0x00,0x23,0xE2,0xFA,0x15,0xD4,0x18,0x36,
104 1.1 riastrad 0xEF,0xD9,0x2E,0x0D,0xC1,0xCC,0xF7,0x3B},
105 1.1 riastrad },
106 1.2 riastrad sb2[2] __aarch64_used = {
107 1.1 riastrad {0x00,0x24,0x71,0x0B,0xC6,0x93,0x7A,0xE2,
108 1.1 riastrad 0xCD,0x2F,0x98,0xBC,0x55,0xE9,0xB7,0x5E},
109 1.1 riastrad {0x00,0x29,0xE1,0x0A,0x40,0x88,0xEB,0x69,
110 1.1 riastrad 0x4A,0x23,0x82,0xAB,0xC8,0x63,0xA1,0xC2},
111 1.1 riastrad },
112 1.2 riastrad sbo[2] __aarch64_used = {
113 1.1 riastrad {0x00,0xC7,0xBD,0x6F,0x17,0x6D,0xD2,0xD0,
114 1.1 riastrad 0x78,0xA8,0x02,0xC5,0x7A,0xBF,0xAA,0x15},
115 1.1 riastrad {0x00,0x6A,0xBB,0x5F,0xA5,0x74,0xE4,0xCF,
116 1.1 riastrad 0xFA,0x35,0x2B,0x41,0xD1,0x90,0x1E,0x8E},
117 1.1 riastrad },
118 1.2 riastrad dsb9[2] __aarch64_used = {
119 1.1 riastrad {0x00,0xD6,0x86,0x9A,0x53,0x03,0x1C,0x85,
120 1.1 riastrad 0xC9,0x4C,0x99,0x4F,0x50,0x1F,0xD5,0xCA},
121 1.1 riastrad {0x00,0x49,0xD7,0xEC,0x89,0x17,0x3B,0xC0,
122 1.1 riastrad 0x65,0xA5,0xFB,0xB2,0x9E,0x2C,0x5E,0x72},
123 1.1 riastrad },
124 1.2 riastrad dsbd[2] __aarch64_used = {
125 1.1 riastrad {0x00,0xA2,0xB1,0xE6,0xDF,0xCC,0x57,0x7D,
126 1.1 riastrad 0x39,0x44,0x2A,0x88,0x13,0x9B,0x6E,0xF5},
127 1.1 riastrad {0x00,0xCB,0xC6,0x24,0xF7,0xFA,0xE2,0x3C,
128 1.1 riastrad 0xD3,0xEF,0xDE,0x15,0x0D,0x18,0x31,0x29},
129 1.1 riastrad },
130 1.2 riastrad dsbb[2] __aarch64_used = {
131 1.1 riastrad {0x00,0x42,0xB4,0x96,0x92,0x64,0x22,0xD0,
132 1.1 riastrad 0x04,0xD4,0xF2,0xB0,0xF6,0x46,0x26,0x60},
133 1.1 riastrad {0x00,0x67,0x59,0xCD,0xA6,0x98,0x94,0xC1,
134 1.1 riastrad 0x6B,0xAA,0x55,0x32,0x3E,0x0C,0xFF,0xF3},
135 1.1 riastrad },
136 1.2 riastrad dsbe[2] __aarch64_used = {
137 1.1 riastrad {0x00,0xD0,0xD4,0x26,0x96,0x92,0xF2,0x46,
138 1.1 riastrad 0xB0,0xF6,0xB4,0x64,0x04,0x60,0x42,0x22},
139 1.1 riastrad {0x00,0xC1,0xAA,0xFF,0xCD,0xA6,0x55,0x0C,
140 1.1 riastrad 0x32,0x3E,0x59,0x98,0x6B,0xF3,0x67,0x94},
141 1.1 riastrad },
142 1.2 riastrad dsbo[2] __aarch64_used = {
143 1.1 riastrad {0x00,0x40,0xF9,0x7E,0x53,0xEA,0x87,0x13,
144 1.1 riastrad 0x2D,0x3E,0x94,0xD4,0xB9,0x6D,0xAA,0xC7},
145 1.1 riastrad {0x00,0x1D,0x44,0x93,0x0F,0x56,0xD7,0x12,
146 1.1 riastrad 0x9C,0x8E,0xC5,0xD8,0x59,0x81,0x4B,0xCA},
147 1.1 riastrad },
148 1.1 riastrad dks1[2] = {
149 1.1 riastrad {0x00,0xA7,0xD9,0x7E,0xC8,0x6F,0x11,0xB6,
150 1.1 riastrad 0xFC,0x5B,0x25,0x82,0x34,0x93,0xED,0x4A},
151 1.1 riastrad {0x00,0x33,0x14,0x27,0x62,0x51,0x76,0x45,
152 1.1 riastrad 0xCE,0xFD,0xDA,0xE9,0xAC,0x9F,0xB8,0x8B},
153 1.1 riastrad },
154 1.1 riastrad dks2[2] = {
155 1.1 riastrad {0x00,0x64,0xA8,0xCC,0xEB,0x8F,0x43,0x27,
156 1.1 riastrad 0x61,0x05,0xC9,0xAD,0x8A,0xEE,0x22,0x46},
157 1.1 riastrad {0x00,0xDD,0x92,0x4F,0xCE,0x13,0x5C,0x81,
158 1.1 riastrad 0xF2,0x2F,0x60,0xBD,0x3C,0xE1,0xAE,0x73},
159 1.1 riastrad },
160 1.1 riastrad dks3[2] = {
161 1.1 riastrad {0x00,0xC7,0xC6,0x01,0x02,0xC5,0xC4,0x03,
162 1.1 riastrad 0xFB,0x3C,0x3D,0xFA,0xF9,0x3E,0x3F,0xF8},
163 1.1 riastrad {0x00,0xF7,0xCF,0x38,0xD6,0x21,0x19,0xEE,
164 1.1 riastrad 0x4B,0xBC,0x84,0x73,0x9D,0x6A,0x52,0xA5},
165 1.1 riastrad },
166 1.1 riastrad dks4[2] = {
167 1.1 riastrad {0x00,0x20,0x73,0x53,0xB0,0x90,0xC3,0xE3,
168 1.1 riastrad 0x43,0x63,0x30,0x10,0xF3,0xD3,0x80,0xA0},
169 1.1 riastrad {0xE8,0x82,0x69,0x03,0x4B,0x21,0xCA,0xA0,
170 1.1 riastrad 0x67,0x0D,0xE6,0x8C,0xC4,0xAE,0x45,0x2F},
171 1.1 riastrad },
172 1.1 riastrad deskew[2] = {
173 1.1 riastrad {0x00,0xE3,0xA4,0x47,0x40,0xA3,0xE4,0x07,
174 1.1 riastrad 0x1A,0xF9,0xBE,0x5D,0x5A,0xB9,0xFE,0x1D},
175 1.1 riastrad {0x00,0x69,0xEA,0x83,0xDC,0xB5,0x36,0x5F,
176 1.1 riastrad 0x77,0x1E,0x9D,0xF4,0xAB,0xC2,0x41,0x28},
177 1.1 riastrad },
178 1.2 riastrad sr[4] __aarch64_used = {
179 1.1 riastrad {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
180 1.1 riastrad 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F},
181 1.1 riastrad {0x00,0x05,0x0A,0x0F,0x04,0x09,0x0E,0x03,
182 1.1 riastrad 0x08,0x0D,0x02,0x07,0x0C,0x01,0x06,0x0B},
183 1.1 riastrad {0x00,0x09,0x02,0x0B,0x04,0x0D,0x06,0x0F,
184 1.1 riastrad 0x08,0x01,0x0A,0x03,0x0C,0x05,0x0E,0x07},
185 1.1 riastrad {0x00,0x0D,0x0A,0x07,0x04,0x01,0x0E,0x0B,
186 1.1 riastrad 0x08,0x05,0x02,0x0F,0x0C,0x09,0x06,0x03},
187 1.1 riastrad },
188 1.1 riastrad rcon = {0xB6,0xEE,0x9D,0xAF,0xB9,0x91,0x83,0x1F,
189 1.1 riastrad 0x81,0x7D,0x7C,0x4D,0x08,0x98,0x2A,0x70},
190 1.1 riastrad s63 = {0x5B,0x5B,0x5B,0x5B,0x5B,0x5B,0x5B,0x5B,
191 1.1 riastrad 0x5B,0x5B,0x5B,0x5B,0x5B,0x5B,0x5B,0x5B},
192 1.1 riastrad of = {0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,
193 1.1 riastrad 0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F},
194 1.1 riastrad inv = {0x80,0x01,0x08,0x0D,0x0F,0x06,0x05,0x0E,
195 1.1 riastrad 0x02,0x0C,0x0B,0x0A,0x09,0x03,0x07,0x04},
196 1.1 riastrad inva = {0x80,0x07,0x0B,0x0F,0x06,0x0A,0x04,0x01,
197 1.1 riastrad 0x09,0x08,0x05,0x02,0x0C,0x0E,0x0D,0x03};
198 1.1 riastrad
199 1.1 riastrad static inline uint8x16_t
200 1.1 riastrad loadroundkey(const void *rkp)
201 1.1 riastrad {
202 1.1 riastrad return vld1q_u8(rkp);
203 1.1 riastrad }
204 1.1 riastrad
205 1.1 riastrad static inline void
206 1.1 riastrad storeroundkey(void *rkp, uint8x16_t rk)
207 1.1 riastrad {
208 1.1 riastrad vst1q_u8(rkp, rk);
209 1.1 riastrad }
210 1.1 riastrad
211 1.1 riastrad /* Given abcdefgh, set *lo = 0b0d0f0h and *hi = 0a0c0e0g. */
212 1.1 riastrad static inline void
213 1.1 riastrad bytes2nybbles(uint8x16_t *restrict lo, uint8x16_t *restrict hi, uint8x16_t x)
214 1.1 riastrad {
215 1.1 riastrad
216 1.1 riastrad *lo = of & x;
217 1.1 riastrad *hi = of & vshrq_n_u8(x, 4);
218 1.1 riastrad }
219 1.1 riastrad
220 1.1 riastrad /*
221 1.1 riastrad * t is a pair of maps respectively from low and high nybbles to bytes.
222 1.1 riastrad * Apply t the nybbles, and add the results in GF(2).
223 1.1 riastrad */
224 1.1 riastrad static uint8x16_t
225 1.1 riastrad aes_schedule_transform(uint8x16_t x, const uint8x16_t t[static 2])
226 1.1 riastrad {
227 1.1 riastrad uint8x16_t lo, hi;
228 1.1 riastrad
229 1.1 riastrad bytes2nybbles(&lo, &hi, x);
230 1.1 riastrad return vqtbl1q_u8(t[0], lo) ^ vqtbl1q_u8(t[1], hi);
231 1.1 riastrad }
232 1.1 riastrad
233 1.1 riastrad static inline void
234 1.1 riastrad subbytes(uint8x16_t *io, uint8x16_t *jo, uint8x16_t x, uint8x16_t inv_,
235 1.1 riastrad uint8x16_t inva_)
236 1.1 riastrad {
237 1.1 riastrad uint8x16_t k, i, ak, j;
238 1.1 riastrad
239 1.1 riastrad bytes2nybbles(&k, &i, x);
240 1.1 riastrad ak = vqtbl1q_u8(inva_, k);
241 1.1 riastrad j = i ^ k;
242 1.1 riastrad *io = j ^ vqtbl1q_u8(inv_, ak ^ vqtbl1q_u8(inv_, i));
243 1.1 riastrad *jo = i ^ vqtbl1q_u8(inv_, ak ^ vqtbl1q_u8(inv_, j));
244 1.1 riastrad }
245 1.1 riastrad
246 1.1 riastrad static uint8x16_t
247 1.1 riastrad aes_schedule_low_round(uint8x16_t rk, uint8x16_t prk)
248 1.1 riastrad {
249 1.1 riastrad uint8x16_t io, jo;
250 1.1 riastrad
251 1.1 riastrad /* smear prk */
252 1.1 riastrad prk ^= vextq_u8(vdupq_n_u8(0), prk, 12);
253 1.1 riastrad prk ^= vextq_u8(vdupq_n_u8(0), prk, 8);
254 1.1 riastrad prk ^= s63;
255 1.1 riastrad
256 1.1 riastrad /* subbytes */
257 1.1 riastrad subbytes(&io, &jo, rk, inv, inva);
258 1.1 riastrad rk = vqtbl1q_u8(sb1[0], io) ^ vqtbl1q_u8(sb1[1], jo);
259 1.1 riastrad
260 1.1 riastrad /* add in smeared stuff */
261 1.1 riastrad return rk ^ prk;
262 1.1 riastrad }
263 1.1 riastrad
264 1.1 riastrad static uint8x16_t
265 1.1 riastrad aes_schedule_round(uint8x16_t rk, uint8x16_t prk, uint8x16_t *rcon_rot)
266 1.1 riastrad {
267 1.1 riastrad uint32x4_t rk32;
268 1.1 riastrad
269 1.1 riastrad /* extract rcon from rcon_rot */
270 1.1 riastrad prk ^= vextq_u8(*rcon_rot, vdupq_n_u8(0), 15);
271 1.1 riastrad *rcon_rot = vextq_u8(*rcon_rot, *rcon_rot, 15);
272 1.1 riastrad
273 1.1 riastrad /* rotate */
274 1.1 riastrad rk32 = vreinterpretq_u32_u8(rk);
275 1.1 riastrad rk32 = vdupq_n_u32(vgetq_lane_u32(rk32, 3));
276 1.1 riastrad rk = vreinterpretq_u8_u32(rk32);
277 1.1 riastrad rk = vextq_u8(rk, rk, 1);
278 1.1 riastrad
279 1.1 riastrad return aes_schedule_low_round(rk, prk);
280 1.1 riastrad }
281 1.1 riastrad
282 1.1 riastrad static uint8x16_t
283 1.1 riastrad aes_schedule_mangle_enc(uint8x16_t x, uint8x16_t sr_i)
284 1.1 riastrad {
285 1.1 riastrad uint8x16_t y = vdupq_n_u8(0);
286 1.1 riastrad
287 1.1 riastrad x ^= s63;
288 1.1 riastrad
289 1.1 riastrad x = vqtbl1q_u8(x, mc_forward[0]);
290 1.1 riastrad y ^= x;
291 1.1 riastrad x = vqtbl1q_u8(x, mc_forward[0]);
292 1.1 riastrad y ^= x;
293 1.1 riastrad x = vqtbl1q_u8(x, mc_forward[0]);
294 1.1 riastrad y ^= x;
295 1.1 riastrad
296 1.1 riastrad return vqtbl1q_u8(y, sr_i);
297 1.1 riastrad }
298 1.1 riastrad
299 1.1 riastrad static uint8x16_t
300 1.1 riastrad aes_schedule_mangle_last_enc(uint8x16_t x, uint8x16_t sr_i)
301 1.1 riastrad {
302 1.1 riastrad
303 1.1 riastrad return aes_schedule_transform(vqtbl1q_u8(x, sr_i) ^ s63, opt);
304 1.1 riastrad }
305 1.1 riastrad
306 1.1 riastrad static uint8x16_t
307 1.1 riastrad aes_schedule_mangle_dec(uint8x16_t x, uint8x16_t sr_i)
308 1.1 riastrad {
309 1.1 riastrad uint8x16_t y = vdupq_n_u8(0);
310 1.1 riastrad
311 1.1 riastrad x = aes_schedule_transform(x, dks1);
312 1.1 riastrad y = vqtbl1q_u8(y ^ x, mc_forward[0]);
313 1.1 riastrad x = aes_schedule_transform(x, dks2);
314 1.1 riastrad y = vqtbl1q_u8(y ^ x, mc_forward[0]);
315 1.1 riastrad x = aes_schedule_transform(x, dks3);
316 1.1 riastrad y = vqtbl1q_u8(y ^ x, mc_forward[0]);
317 1.1 riastrad x = aes_schedule_transform(x, dks4);
318 1.1 riastrad y = vqtbl1q_u8(y ^ x, mc_forward[0]);
319 1.1 riastrad
320 1.1 riastrad return vqtbl1q_u8(y, sr_i);
321 1.1 riastrad }
322 1.1 riastrad
323 1.1 riastrad static uint8x16_t
324 1.1 riastrad aes_schedule_mangle_last_dec(uint8x16_t x)
325 1.1 riastrad {
326 1.1 riastrad
327 1.1 riastrad return aes_schedule_transform(x ^ s63, deskew);
328 1.1 riastrad }
329 1.1 riastrad
330 1.1 riastrad static uint8x16_t
331 1.1 riastrad aes_schedule_192_smear(uint8x16_t prkhi, uint8x16_t prk)
332 1.1 riastrad {
333 1.1 riastrad uint32x4_t prkhi32 = vreinterpretq_u32_u8(prkhi);
334 1.1 riastrad uint32x4_t prk32 = vreinterpretq_u32_u8(prk);
335 1.1 riastrad uint32x4_t rk32;
336 1.1 riastrad
337 1.1 riastrad rk32 = prkhi32;
338 1.1 riastrad rk32 ^= vsetq_lane_u32(vgetq_lane_u32(prkhi32, 2),
339 1.1 riastrad vdupq_n_u32(vgetq_lane_u32(prkhi32, 0)),
340 1.1 riastrad 3);
341 1.1 riastrad rk32 ^= vsetq_lane_u32(vgetq_lane_u32(prk32, 2),
342 1.1 riastrad vdupq_n_u32(vgetq_lane_u32(prk32, 3)),
343 1.1 riastrad 0);
344 1.1 riastrad
345 1.1 riastrad return vreinterpretq_u8_u32(rk32);
346 1.1 riastrad }
347 1.1 riastrad
348 1.1 riastrad static uint8x16_t
349 1.1 riastrad aes_schedule_192_smearhi(uint8x16_t rk)
350 1.1 riastrad {
351 1.1 riastrad uint64x2_t rk64 = vreinterpretq_u64_u8(rk);
352 1.1 riastrad
353 1.1 riastrad rk64 = vsetq_lane_u64(0, rk64, 0);
354 1.1 riastrad
355 1.1 riastrad return vreinterpretq_u8_u64(rk64);
356 1.1 riastrad }
357 1.1 riastrad
358 1.1 riastrad void
359 1.1 riastrad aes_neon_setenckey(struct aesenc *enc, const uint8_t *key, unsigned nrounds)
360 1.1 riastrad {
361 1.1 riastrad uint32_t *rk32 = enc->aese_aes.aes_rk;
362 1.1 riastrad uint8x16_t mrk; /* mangled round key */
363 1.1 riastrad uint8x16_t rk; /* round key */
364 1.1 riastrad uint8x16_t prk; /* previous round key */
365 1.1 riastrad uint8x16_t rcon_rot = rcon;
366 1.1 riastrad uint64_t i = 3;
367 1.1 riastrad
368 1.1 riastrad /* input transform */
369 1.1 riastrad rk = aes_schedule_transform(vld1q_u8(key), ipt);
370 1.1 riastrad storeroundkey(rk32, rk);
371 1.1 riastrad rk32 += 4;
372 1.1 riastrad
373 1.1 riastrad switch (nrounds) {
374 1.1 riastrad case 10:
375 1.1 riastrad for (;;) {
376 1.1 riastrad rk = aes_schedule_round(rk, rk, &rcon_rot);
377 1.1 riastrad if (--nrounds == 0)
378 1.1 riastrad break;
379 1.1 riastrad mrk = aes_schedule_mangle_enc(rk, sr[i-- % 4]);
380 1.1 riastrad storeroundkey(rk32, mrk);
381 1.1 riastrad rk32 += 4;
382 1.1 riastrad }
383 1.1 riastrad break;
384 1.1 riastrad case 12: {
385 1.1 riastrad uint8x16_t prkhi; /* high half of previous round key */
386 1.1 riastrad
387 1.1 riastrad prk = rk;
388 1.1 riastrad rk = aes_schedule_transform(vld1q_u8(key + 8), ipt);
389 1.1 riastrad prkhi = aes_schedule_192_smearhi(rk);
390 1.1 riastrad for (;;) {
391 1.1 riastrad prk = aes_schedule_round(rk, prk, &rcon_rot);
392 1.1 riastrad rk = vextq_u8(prkhi, prk, 8);
393 1.1 riastrad
394 1.1 riastrad mrk = aes_schedule_mangle_enc(rk, sr[i-- % 4]);
395 1.1 riastrad storeroundkey(rk32, mrk);
396 1.1 riastrad rk32 += 4;
397 1.1 riastrad rk = aes_schedule_192_smear(prkhi, prk);
398 1.1 riastrad prkhi = aes_schedule_192_smearhi(rk);
399 1.1 riastrad
400 1.1 riastrad mrk = aes_schedule_mangle_enc(rk, sr[i-- % 4]);
401 1.1 riastrad storeroundkey(rk32, mrk);
402 1.1 riastrad rk32 += 4;
403 1.1 riastrad rk = prk = aes_schedule_round(rk, prk, &rcon_rot);
404 1.1 riastrad if ((nrounds -= 3) == 0)
405 1.1 riastrad break;
406 1.1 riastrad
407 1.1 riastrad mrk = aes_schedule_mangle_enc(rk, sr[i-- % 4]);
408 1.1 riastrad storeroundkey(rk32, mrk);
409 1.1 riastrad rk32 += 4;
410 1.1 riastrad rk = aes_schedule_192_smear(prkhi, prk);
411 1.1 riastrad prkhi = aes_schedule_192_smearhi(rk);
412 1.1 riastrad }
413 1.1 riastrad break;
414 1.1 riastrad }
415 1.1 riastrad case 14: {
416 1.1 riastrad uint8x16_t pprk; /* previous previous round key */
417 1.1 riastrad
418 1.1 riastrad prk = rk;
419 1.1 riastrad rk = aes_schedule_transform(vld1q_u8(key + 16), ipt);
420 1.1 riastrad for (;;) {
421 1.1 riastrad mrk = aes_schedule_mangle_enc(rk, sr[i-- % 4]);
422 1.1 riastrad storeroundkey(rk32, mrk);
423 1.1 riastrad rk32 += 4;
424 1.1 riastrad pprk = rk;
425 1.1 riastrad
426 1.1 riastrad /* high round */
427 1.1 riastrad rk = prk = aes_schedule_round(rk, prk, &rcon_rot);
428 1.1 riastrad if ((nrounds -= 2) == 0)
429 1.1 riastrad break;
430 1.1 riastrad mrk = aes_schedule_mangle_enc(rk, sr[i-- % 4]);
431 1.1 riastrad storeroundkey(rk32, mrk);
432 1.1 riastrad rk32 += 4;
433 1.1 riastrad
434 1.1 riastrad /* low round */
435 1.1 riastrad rk = vreinterpretq_u8_u32(
436 1.1 riastrad vdupq_n_u32(
437 1.1 riastrad vgetq_lane_u32(vreinterpretq_u32_u8(rk),
438 1.1 riastrad 3)));
439 1.1 riastrad rk = aes_schedule_low_round(rk, pprk);
440 1.1 riastrad }
441 1.1 riastrad break;
442 1.1 riastrad }
443 1.1 riastrad default:
444 1.1 riastrad panic("invalid number of AES rounds: %u", nrounds);
445 1.1 riastrad }
446 1.1 riastrad storeroundkey(rk32, aes_schedule_mangle_last_enc(rk, sr[i-- % 4]));
447 1.1 riastrad }
448 1.1 riastrad
449 1.1 riastrad void
450 1.1 riastrad aes_neon_setdeckey(struct aesdec *dec, const uint8_t *key, unsigned nrounds)
451 1.1 riastrad {
452 1.1 riastrad uint32_t *rk32 = dec->aesd_aes.aes_rk;
453 1.1 riastrad uint8x16_t mrk; /* mangled round key */
454 1.1 riastrad uint8x16_t ork; /* original round key */
455 1.1 riastrad uint8x16_t rk; /* round key */
456 1.1 riastrad uint8x16_t prk; /* previous round key */
457 1.1 riastrad uint8x16_t rcon_rot = rcon;
458 1.1 riastrad unsigned i = nrounds == 12 ? 0 : 2;
459 1.1 riastrad
460 1.1 riastrad ork = vld1q_u8(key);
461 1.1 riastrad
462 1.1 riastrad /* input transform */
463 1.1 riastrad rk = aes_schedule_transform(ork, ipt);
464 1.1 riastrad
465 1.1 riastrad /* go from end */
466 1.1 riastrad rk32 += 4*nrounds;
467 1.1 riastrad storeroundkey(rk32, vqtbl1q_u8(ork, sr[i]));
468 1.1 riastrad rk32 -= 4;
469 1.1 riastrad i ^= 3;
470 1.1 riastrad
471 1.1 riastrad switch (nrounds) {
472 1.1 riastrad case 10:
473 1.1 riastrad for (;;) {
474 1.1 riastrad rk = aes_schedule_round(rk, rk, &rcon_rot);
475 1.1 riastrad if (--nrounds == 0)
476 1.1 riastrad break;
477 1.1 riastrad mrk = aes_schedule_mangle_dec(rk, sr[i-- % 4]);
478 1.1 riastrad storeroundkey(rk32, mrk);
479 1.1 riastrad rk32 -= 4;
480 1.1 riastrad }
481 1.1 riastrad break;
482 1.1 riastrad case 12: {
483 1.1 riastrad uint8x16_t prkhi; /* high half of previous round key */
484 1.1 riastrad
485 1.1 riastrad prk = rk;
486 1.1 riastrad rk = aes_schedule_transform(vld1q_u8(key + 8), ipt);
487 1.1 riastrad prkhi = aes_schedule_192_smearhi(rk);
488 1.1 riastrad for (;;) {
489 1.1 riastrad prk = aes_schedule_round(rk, prk, &rcon_rot);
490 1.1 riastrad rk = vextq_u8(prkhi, prk, 8);
491 1.1 riastrad
492 1.1 riastrad mrk = aes_schedule_mangle_dec(rk, sr[i-- % 4]);
493 1.1 riastrad storeroundkey(rk32, mrk);
494 1.1 riastrad rk32 -= 4;
495 1.1 riastrad rk = aes_schedule_192_smear(prkhi, prk);
496 1.1 riastrad prkhi = aes_schedule_192_smearhi(rk);
497 1.1 riastrad
498 1.1 riastrad mrk = aes_schedule_mangle_dec(rk, sr[i-- % 4]);
499 1.1 riastrad storeroundkey(rk32, mrk);
500 1.1 riastrad rk32 -= 4;
501 1.1 riastrad rk = prk = aes_schedule_round(rk, prk, &rcon_rot);
502 1.1 riastrad if ((nrounds -= 3) == 0)
503 1.1 riastrad break;
504 1.1 riastrad
505 1.1 riastrad mrk = aes_schedule_mangle_dec(rk, sr[i-- % 4]);
506 1.1 riastrad storeroundkey(rk32, mrk);
507 1.1 riastrad rk32 -= 4;
508 1.1 riastrad rk = aes_schedule_192_smear(prkhi, prk);
509 1.1 riastrad prkhi = aes_schedule_192_smearhi(rk);
510 1.1 riastrad }
511 1.1 riastrad break;
512 1.1 riastrad }
513 1.1 riastrad case 14: {
514 1.1 riastrad uint8x16_t pprk; /* previous previous round key */
515 1.1 riastrad
516 1.1 riastrad prk = rk;
517 1.1 riastrad rk = aes_schedule_transform(vld1q_u8(key + 16), ipt);
518 1.1 riastrad for (;;) {
519 1.1 riastrad mrk = aes_schedule_mangle_dec(rk, sr[i-- % 4]);
520 1.1 riastrad storeroundkey(rk32, mrk);
521 1.1 riastrad rk32 -= 4;
522 1.1 riastrad pprk = rk;
523 1.1 riastrad
524 1.1 riastrad /* high round */
525 1.1 riastrad rk = prk = aes_schedule_round(rk, prk, &rcon_rot);
526 1.1 riastrad if ((nrounds -= 2) == 0)
527 1.1 riastrad break;
528 1.1 riastrad mrk = aes_schedule_mangle_dec(rk, sr[i-- % 4]);
529 1.1 riastrad storeroundkey(rk32, mrk);
530 1.1 riastrad rk32 -= 4;
531 1.1 riastrad
532 1.1 riastrad /* low round */
533 1.1 riastrad rk = vreinterpretq_u8_u32(
534 1.1 riastrad vdupq_n_u32(
535 1.1 riastrad vgetq_lane_u32(vreinterpretq_u32_u8(rk),
536 1.1 riastrad 3)));
537 1.1 riastrad rk = aes_schedule_low_round(rk, pprk);
538 1.1 riastrad }
539 1.1 riastrad break;
540 1.1 riastrad }
541 1.1 riastrad default:
542 1.1 riastrad panic("invalid number of AES rounds: %u", nrounds);
543 1.1 riastrad }
544 1.1 riastrad storeroundkey(rk32, aes_schedule_mangle_last_dec(rk));
545 1.1 riastrad }
546 1.1 riastrad
547 1.2 riastrad #ifdef __aarch64__
548 1.2 riastrad
549 1.2 riastrad /*
550 1.2 riastrad * GCC does a lousy job of compiling NEON intrinsics for arm32, so we
551 1.2 riastrad * do the performance-critical parts -- encryption and decryption -- in
552 1.2 riastrad * hand-written assembly on arm32.
553 1.2 riastrad */
554 1.2 riastrad
555 1.1 riastrad uint8x16_t
556 1.1 riastrad aes_neon_enc1(const struct aesenc *enc, uint8x16_t x, unsigned nrounds)
557 1.1 riastrad {
558 1.1 riastrad const uint32_t *rk32 = enc->aese_aes.aes_rk;
559 1.1 riastrad uint8x16_t inv_ = *(const volatile uint8x16_t *)&inv;
560 1.1 riastrad uint8x16_t inva_ = *(const volatile uint8x16_t *)&inva;
561 1.1 riastrad uint8x16_t sb1_0 = ((const volatile uint8x16_t *)sb1)[0];
562 1.1 riastrad uint8x16_t sb1_1 = ((const volatile uint8x16_t *)sb1)[1];
563 1.1 riastrad uint8x16_t sb2_0 = ((const volatile uint8x16_t *)sb2)[0];
564 1.1 riastrad uint8x16_t sb2_1 = ((const volatile uint8x16_t *)sb2)[1];
565 1.1 riastrad uint8x16_t io, jo;
566 1.1 riastrad unsigned rmod4 = 0;
567 1.1 riastrad
568 1.1 riastrad x = aes_schedule_transform(x, ipt);
569 1.1 riastrad x ^= loadroundkey(rk32);
570 1.1 riastrad for (;;) {
571 1.1 riastrad uint8x16_t A, A2, A2_B, A2_B_D;
572 1.1 riastrad
573 1.1 riastrad subbytes(&io, &jo, x, inv_, inva_);
574 1.1 riastrad
575 1.1 riastrad rk32 += 4;
576 1.1 riastrad rmod4 = (rmod4 + 1) % 4;
577 1.1 riastrad if (--nrounds == 0)
578 1.1 riastrad break;
579 1.1 riastrad
580 1.1 riastrad A = vqtbl1q_u8(sb1_0, io) ^ vqtbl1q_u8(sb1_1, jo);
581 1.1 riastrad A ^= loadroundkey(rk32);
582 1.1 riastrad A2 = vqtbl1q_u8(sb2_0, io) ^ vqtbl1q_u8(sb2_1, jo);
583 1.1 riastrad A2_B = A2 ^ vqtbl1q_u8(A, mc_forward[rmod4]);
584 1.1 riastrad A2_B_D = A2_B ^ vqtbl1q_u8(A, mc_backward[rmod4]);
585 1.1 riastrad x = A2_B_D ^ vqtbl1q_u8(A2_B, mc_forward[rmod4]);
586 1.1 riastrad }
587 1.1 riastrad x = vqtbl1q_u8(sbo[0], io) ^ vqtbl1q_u8(sbo[1], jo);
588 1.1 riastrad x ^= loadroundkey(rk32);
589 1.1 riastrad return vqtbl1q_u8(x, sr[rmod4]);
590 1.1 riastrad }
591 1.1 riastrad
592 1.4 riastrad uint8x16x2_t
593 1.4 riastrad aes_neon_enc2(const struct aesenc *enc, uint8x16x2_t x, unsigned nrounds)
594 1.4 riastrad {
595 1.4 riastrad const uint32_t *rk32 = enc->aese_aes.aes_rk;
596 1.4 riastrad uint8x16_t inv_ = *(const volatile uint8x16_t *)&inv;
597 1.4 riastrad uint8x16_t inva_ = *(const volatile uint8x16_t *)&inva;
598 1.4 riastrad uint8x16_t sb1_0 = ((const volatile uint8x16_t *)sb1)[0];
599 1.4 riastrad uint8x16_t sb1_1 = ((const volatile uint8x16_t *)sb1)[1];
600 1.4 riastrad uint8x16_t sb2_0 = ((const volatile uint8x16_t *)sb2)[0];
601 1.4 riastrad uint8x16_t sb2_1 = ((const volatile uint8x16_t *)sb2)[1];
602 1.4 riastrad uint8x16_t x0 = x.val[0], x1 = x.val[1];
603 1.4 riastrad uint8x16_t io0, jo0, io1, jo1;
604 1.4 riastrad unsigned rmod4 = 0;
605 1.4 riastrad
606 1.4 riastrad x0 = aes_schedule_transform(x0, ipt);
607 1.4 riastrad x1 = aes_schedule_transform(x1, ipt);
608 1.4 riastrad x0 ^= loadroundkey(rk32);
609 1.4 riastrad x1 ^= loadroundkey(rk32);
610 1.4 riastrad for (;;) {
611 1.4 riastrad uint8x16_t A_0, A2_0, A2_B_0, A2_B_D_0;
612 1.4 riastrad uint8x16_t A_1, A2_1, A2_B_1, A2_B_D_1;
613 1.4 riastrad
614 1.4 riastrad subbytes(&io0, &jo0, x0, inv_, inva_);
615 1.4 riastrad subbytes(&io1, &jo1, x1, inv_, inva_);
616 1.4 riastrad
617 1.4 riastrad rk32 += 4;
618 1.4 riastrad rmod4 = (rmod4 + 1) % 4;
619 1.4 riastrad if (--nrounds == 0)
620 1.4 riastrad break;
621 1.4 riastrad
622 1.4 riastrad A_0 = vqtbl1q_u8(sb1_0, io0) ^ vqtbl1q_u8(sb1_1, jo0);
623 1.4 riastrad A_1 = vqtbl1q_u8(sb1_0, io1) ^ vqtbl1q_u8(sb1_1, jo1);
624 1.4 riastrad A_0 ^= loadroundkey(rk32);
625 1.4 riastrad A_1 ^= loadroundkey(rk32);
626 1.4 riastrad A2_0 = vqtbl1q_u8(sb2_0, io0) ^ vqtbl1q_u8(sb2_1, jo0);
627 1.4 riastrad A2_1 = vqtbl1q_u8(sb2_0, io1) ^ vqtbl1q_u8(sb2_1, jo1);
628 1.4 riastrad A2_B_0 = A2_0 ^ vqtbl1q_u8(A_0, mc_forward[rmod4]);
629 1.4 riastrad A2_B_1 = A2_1 ^ vqtbl1q_u8(A_1, mc_forward[rmod4]);
630 1.4 riastrad A2_B_D_0 = A2_B_0 ^ vqtbl1q_u8(A_0, mc_backward[rmod4]);
631 1.4 riastrad A2_B_D_1 = A2_B_1 ^ vqtbl1q_u8(A_1, mc_backward[rmod4]);
632 1.4 riastrad x0 = A2_B_D_0 ^ vqtbl1q_u8(A2_B_0, mc_forward[rmod4]);
633 1.4 riastrad x1 = A2_B_D_1 ^ vqtbl1q_u8(A2_B_1, mc_forward[rmod4]);
634 1.4 riastrad }
635 1.4 riastrad x0 = vqtbl1q_u8(sbo[0], io0) ^ vqtbl1q_u8(sbo[1], jo0);
636 1.4 riastrad x1 = vqtbl1q_u8(sbo[0], io1) ^ vqtbl1q_u8(sbo[1], jo1);
637 1.4 riastrad x0 ^= loadroundkey(rk32);
638 1.4 riastrad x1 ^= loadroundkey(rk32);
639 1.4 riastrad return (uint8x16x2_t) { .val = {
640 1.4 riastrad [0] = vqtbl1q_u8(x0, sr[rmod4]),
641 1.4 riastrad [1] = vqtbl1q_u8(x1, sr[rmod4]),
642 1.4 riastrad } };
643 1.4 riastrad }
644 1.4 riastrad
645 1.1 riastrad uint8x16_t
646 1.1 riastrad aes_neon_dec1(const struct aesdec *dec, uint8x16_t x, unsigned nrounds)
647 1.1 riastrad {
648 1.1 riastrad const uint32_t *rk32 = dec->aesd_aes.aes_rk;
649 1.1 riastrad unsigned i = 3 & ~(nrounds - 1);
650 1.1 riastrad uint8x16_t inv_ = *(const volatile uint8x16_t *)&inv;
651 1.1 riastrad uint8x16_t inva_ = *(const volatile uint8x16_t *)&inva;
652 1.1 riastrad uint8x16_t io, jo, mc;
653 1.1 riastrad
654 1.1 riastrad x = aes_schedule_transform(x, dipt);
655 1.1 riastrad x ^= loadroundkey(rk32);
656 1.1 riastrad rk32 += 4;
657 1.1 riastrad
658 1.1 riastrad mc = mc_forward[3];
659 1.1 riastrad for (;;) {
660 1.1 riastrad subbytes(&io, &jo, x, inv_, inva_);
661 1.1 riastrad if (--nrounds == 0)
662 1.1 riastrad break;
663 1.1 riastrad
664 1.1 riastrad x = vqtbl1q_u8(dsb9[0], io) ^ vqtbl1q_u8(dsb9[1], jo);
665 1.1 riastrad x ^= loadroundkey(rk32);
666 1.1 riastrad rk32 += 4; /* next round key */
667 1.1 riastrad
668 1.1 riastrad x = vqtbl1q_u8(x, mc);
669 1.1 riastrad x ^= vqtbl1q_u8(dsbd[0], io) ^ vqtbl1q_u8(dsbd[1], jo);
670 1.1 riastrad
671 1.1 riastrad x = vqtbl1q_u8(x, mc);
672 1.1 riastrad x ^= vqtbl1q_u8(dsbb[0], io) ^ vqtbl1q_u8(dsbb[1], jo);
673 1.1 riastrad
674 1.1 riastrad x = vqtbl1q_u8(x, mc);
675 1.1 riastrad x ^= vqtbl1q_u8(dsbe[0], io) ^ vqtbl1q_u8(dsbe[1], jo);
676 1.1 riastrad
677 1.1 riastrad mc = vextq_u8(mc, mc, 12);
678 1.1 riastrad }
679 1.1 riastrad x = vqtbl1q_u8(dsbo[0], io) ^ vqtbl1q_u8(dsbo[1], jo);
680 1.1 riastrad x ^= loadroundkey(rk32);
681 1.1 riastrad return vqtbl1q_u8(x, sr[i]);
682 1.1 riastrad }
683 1.2 riastrad
684 1.4 riastrad uint8x16x2_t
685 1.4 riastrad aes_neon_dec2(const struct aesdec *dec, uint8x16x2_t x, unsigned nrounds)
686 1.4 riastrad {
687 1.4 riastrad const uint32_t *rk32 = dec->aesd_aes.aes_rk;
688 1.4 riastrad unsigned i = 3 & ~(nrounds - 1);
689 1.4 riastrad uint8x16_t inv_ = *(const volatile uint8x16_t *)&inv;
690 1.4 riastrad uint8x16_t inva_ = *(const volatile uint8x16_t *)&inva;
691 1.4 riastrad uint8x16_t x0 = x.val[0], x1 = x.val[1];
692 1.4 riastrad uint8x16_t io0, jo0, io1, jo1, mc;
693 1.4 riastrad
694 1.4 riastrad x0 = aes_schedule_transform(x0, dipt);
695 1.4 riastrad x1 = aes_schedule_transform(x1, dipt);
696 1.4 riastrad x0 ^= loadroundkey(rk32);
697 1.4 riastrad x1 ^= loadroundkey(rk32);
698 1.4 riastrad rk32 += 4;
699 1.4 riastrad
700 1.4 riastrad mc = mc_forward[3];
701 1.4 riastrad for (;;) {
702 1.4 riastrad subbytes(&io0, &jo0, x0, inv_, inva_);
703 1.4 riastrad subbytes(&io1, &jo1, x1, inv_, inva_);
704 1.4 riastrad if (--nrounds == 0)
705 1.4 riastrad break;
706 1.4 riastrad
707 1.4 riastrad x0 = vqtbl1q_u8(dsb9[0], io0) ^ vqtbl1q_u8(dsb9[1], jo0);
708 1.4 riastrad x1 = vqtbl1q_u8(dsb9[0], io1) ^ vqtbl1q_u8(dsb9[1], jo1);
709 1.4 riastrad x0 ^= loadroundkey(rk32);
710 1.4 riastrad x1 ^= loadroundkey(rk32);
711 1.4 riastrad rk32 += 4; /* next round key */
712 1.4 riastrad
713 1.4 riastrad x0 = vqtbl1q_u8(x0, mc);
714 1.4 riastrad x1 = vqtbl1q_u8(x1, mc);
715 1.4 riastrad x0 ^= vqtbl1q_u8(dsbd[0], io0) ^ vqtbl1q_u8(dsbd[1], jo0);
716 1.4 riastrad x1 ^= vqtbl1q_u8(dsbd[0], io1) ^ vqtbl1q_u8(dsbd[1], jo1);
717 1.4 riastrad
718 1.4 riastrad x0 = vqtbl1q_u8(x0, mc);
719 1.4 riastrad x1 = vqtbl1q_u8(x1, mc);
720 1.4 riastrad x0 ^= vqtbl1q_u8(dsbb[0], io0) ^ vqtbl1q_u8(dsbb[1], jo0);
721 1.4 riastrad x1 ^= vqtbl1q_u8(dsbb[0], io1) ^ vqtbl1q_u8(dsbb[1], jo1);
722 1.4 riastrad
723 1.4 riastrad x0 = vqtbl1q_u8(x0, mc);
724 1.4 riastrad x1 = vqtbl1q_u8(x1, mc);
725 1.4 riastrad x0 ^= vqtbl1q_u8(dsbe[0], io0) ^ vqtbl1q_u8(dsbe[1], jo0);
726 1.4 riastrad x1 ^= vqtbl1q_u8(dsbe[0], io1) ^ vqtbl1q_u8(dsbe[1], jo1);
727 1.4 riastrad
728 1.4 riastrad mc = vextq_u8(mc, mc, 12);
729 1.4 riastrad }
730 1.4 riastrad x0 = vqtbl1q_u8(dsbo[0], io0) ^ vqtbl1q_u8(dsbo[1], jo0);
731 1.4 riastrad x1 = vqtbl1q_u8(dsbo[0], io1) ^ vqtbl1q_u8(dsbo[1], jo1);
732 1.4 riastrad x0 ^= loadroundkey(rk32);
733 1.4 riastrad x1 ^= loadroundkey(rk32);
734 1.4 riastrad return (uint8x16x2_t) { .val = {
735 1.4 riastrad [0] = vqtbl1q_u8(x0, sr[i]),
736 1.4 riastrad [1] = vqtbl1q_u8(x1, sr[i]),
737 1.4 riastrad } };
738 1.4 riastrad }
739 1.4 riastrad
740 1.2 riastrad #endif
741