1 /* $NetBSD: aes_ct.c,v 1.3 2020/06/30 20:32:11 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(1, "$NetBSD: aes_ct.c,v 1.3 2020/06/30 20:32:11 riastradh Exp $");
29
30 #include <sys/types.h>
31
32 #ifdef _KERNEL
33 #include <lib/libkern/libkern.h>
34 #else
35 #include <string.h>
36 #endif
37
38 #include <crypto/aes/aes_bear.h>
39
40 /* see inner.h */
41 void
br_aes_ct_bitslice_Sbox(uint32_t * q)42 br_aes_ct_bitslice_Sbox(uint32_t *q)
43 {
44 /*
45 * This S-box implementation is a straightforward translation of
46 * the circuit described by Boyar and Peralta in "A new
47 * combinational logic minimization technique with applications
48 * to cryptology" (https://eprint.iacr.org/2009/191.pdf).
49 *
50 * Note that variables x* (input) and s* (output) are numbered
51 * in "reverse" order (x0 is the high bit, x7 is the low bit).
52 */
53
54 uint32_t x0, x1, x2, x3, x4, x5, x6, x7;
55 uint32_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
56 uint32_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
57 uint32_t y20, y21;
58 uint32_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
59 uint32_t z10, z11, z12, z13, z14, z15, z16, z17;
60 uint32_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
61 uint32_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
62 uint32_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
63 uint32_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
64 uint32_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
65 uint32_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
66 uint32_t t60, t61, t62, t63, t64, t65, t66, t67;
67 uint32_t s0, s1, s2, s3, s4, s5, s6, s7;
68
69 x0 = q[7];
70 x1 = q[6];
71 x2 = q[5];
72 x3 = q[4];
73 x4 = q[3];
74 x5 = q[2];
75 x6 = q[1];
76 x7 = q[0];
77
78 /*
79 * Top linear transformation.
80 */
81 y14 = x3 ^ x5;
82 y13 = x0 ^ x6;
83 y9 = x0 ^ x3;
84 y8 = x0 ^ x5;
85 t0 = x1 ^ x2;
86 y1 = t0 ^ x7;
87 y4 = y1 ^ x3;
88 y12 = y13 ^ y14;
89 y2 = y1 ^ x0;
90 y5 = y1 ^ x6;
91 y3 = y5 ^ y8;
92 t1 = x4 ^ y12;
93 y15 = t1 ^ x5;
94 y20 = t1 ^ x1;
95 y6 = y15 ^ x7;
96 y10 = y15 ^ t0;
97 y11 = y20 ^ y9;
98 y7 = x7 ^ y11;
99 y17 = y10 ^ y11;
100 y19 = y10 ^ y8;
101 y16 = t0 ^ y11;
102 y21 = y13 ^ y16;
103 y18 = x0 ^ y16;
104
105 /*
106 * Non-linear section.
107 */
108 t2 = y12 & y15;
109 t3 = y3 & y6;
110 t4 = t3 ^ t2;
111 t5 = y4 & x7;
112 t6 = t5 ^ t2;
113 t7 = y13 & y16;
114 t8 = y5 & y1;
115 t9 = t8 ^ t7;
116 t10 = y2 & y7;
117 t11 = t10 ^ t7;
118 t12 = y9 & y11;
119 t13 = y14 & y17;
120 t14 = t13 ^ t12;
121 t15 = y8 & y10;
122 t16 = t15 ^ t12;
123 t17 = t4 ^ t14;
124 t18 = t6 ^ t16;
125 t19 = t9 ^ t14;
126 t20 = t11 ^ t16;
127 t21 = t17 ^ y20;
128 t22 = t18 ^ y19;
129 t23 = t19 ^ y21;
130 t24 = t20 ^ y18;
131
132 t25 = t21 ^ t22;
133 t26 = t21 & t23;
134 t27 = t24 ^ t26;
135 t28 = t25 & t27;
136 t29 = t28 ^ t22;
137 t30 = t23 ^ t24;
138 t31 = t22 ^ t26;
139 t32 = t31 & t30;
140 t33 = t32 ^ t24;
141 t34 = t23 ^ t33;
142 t35 = t27 ^ t33;
143 t36 = t24 & t35;
144 t37 = t36 ^ t34;
145 t38 = t27 ^ t36;
146 t39 = t29 & t38;
147 t40 = t25 ^ t39;
148
149 t41 = t40 ^ t37;
150 t42 = t29 ^ t33;
151 t43 = t29 ^ t40;
152 t44 = t33 ^ t37;
153 t45 = t42 ^ t41;
154 z0 = t44 & y15;
155 z1 = t37 & y6;
156 z2 = t33 & x7;
157 z3 = t43 & y16;
158 z4 = t40 & y1;
159 z5 = t29 & y7;
160 z6 = t42 & y11;
161 z7 = t45 & y17;
162 z8 = t41 & y10;
163 z9 = t44 & y12;
164 z10 = t37 & y3;
165 z11 = t33 & y4;
166 z12 = t43 & y13;
167 z13 = t40 & y5;
168 z14 = t29 & y2;
169 z15 = t42 & y9;
170 z16 = t45 & y14;
171 z17 = t41 & y8;
172
173 /*
174 * Bottom linear transformation.
175 */
176 t46 = z15 ^ z16;
177 t47 = z10 ^ z11;
178 t48 = z5 ^ z13;
179 t49 = z9 ^ z10;
180 t50 = z2 ^ z12;
181 t51 = z2 ^ z5;
182 t52 = z7 ^ z8;
183 t53 = z0 ^ z3;
184 t54 = z6 ^ z7;
185 t55 = z16 ^ z17;
186 t56 = z12 ^ t48;
187 t57 = t50 ^ t53;
188 t58 = z4 ^ t46;
189 t59 = z3 ^ t54;
190 t60 = t46 ^ t57;
191 t61 = z14 ^ t57;
192 t62 = t52 ^ t58;
193 t63 = t49 ^ t58;
194 t64 = z4 ^ t59;
195 t65 = t61 ^ t62;
196 t66 = z1 ^ t63;
197 s0 = t59 ^ t63;
198 s6 = t56 ^ ~t62;
199 s7 = t48 ^ ~t60;
200 t67 = t64 ^ t65;
201 s3 = t53 ^ t66;
202 s4 = t51 ^ t66;
203 s5 = t47 ^ t65;
204 s1 = t64 ^ ~s3;
205 s2 = t55 ^ ~t67;
206
207 q[7] = s0;
208 q[6] = s1;
209 q[5] = s2;
210 q[4] = s3;
211 q[3] = s4;
212 q[2] = s5;
213 q[1] = s6;
214 q[0] = s7;
215 }
216
217 /* see inner.h */
218 void
br_aes_ct_ortho(uint32_t * q)219 br_aes_ct_ortho(uint32_t *q)
220 {
221 #define SWAPN(cl, ch, s, x, y) do { \
222 uint32_t a, b; \
223 a = (x); \
224 b = (y); \
225 (x) = (a & (uint32_t)cl) | ((b & (uint32_t)cl) << (s)); \
226 (y) = ((a & (uint32_t)ch) >> (s)) | (b & (uint32_t)ch); \
227 } while (0)
228
229 #define SWAP2(x, y) SWAPN(0x55555555, 0xAAAAAAAA, 1, x, y)
230 #define SWAP4(x, y) SWAPN(0x33333333, 0xCCCCCCCC, 2, x, y)
231 #define SWAP8(x, y) SWAPN(0x0F0F0F0F, 0xF0F0F0F0, 4, x, y)
232
233 SWAP2(q[0], q[1]);
234 SWAP2(q[2], q[3]);
235 SWAP2(q[4], q[5]);
236 SWAP2(q[6], q[7]);
237
238 SWAP4(q[0], q[2]);
239 SWAP4(q[1], q[3]);
240 SWAP4(q[4], q[6]);
241 SWAP4(q[5], q[7]);
242
243 SWAP8(q[0], q[4]);
244 SWAP8(q[1], q[5]);
245 SWAP8(q[2], q[6]);
246 SWAP8(q[3], q[7]);
247 }
248
249 static const unsigned char Rcon[] = {
250 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
251 };
252
253 static uint32_t
sub_word(uint32_t x)254 sub_word(uint32_t x)
255 {
256 uint32_t q[8];
257 int i;
258
259 for (i = 0; i < 8; i ++) {
260 q[i] = x;
261 }
262 br_aes_ct_ortho(q);
263 br_aes_ct_bitslice_Sbox(q);
264 br_aes_ct_ortho(q);
265 return q[0];
266 }
267
268 /* see inner.h */
269 unsigned
br_aes_ct_keysched(uint32_t * comp_skey,const void * key,size_t key_len)270 br_aes_ct_keysched(uint32_t *comp_skey, const void *key, size_t key_len)
271 {
272 unsigned num_rounds;
273 int i, j, k, nk, nkf;
274 uint32_t tmp;
275 uint32_t skey[120];
276
277 switch (key_len) {
278 case 16:
279 num_rounds = 10;
280 break;
281 case 24:
282 num_rounds = 12;
283 break;
284 case 32:
285 num_rounds = 14;
286 break;
287 default:
288 /* abort(); */
289 return 0;
290 }
291 nk = (int)(key_len >> 2);
292 nkf = (int)((num_rounds + 1) << 2);
293 tmp = 0;
294 for (i = 0; i < nk; i ++) {
295 tmp = br_dec32le((const unsigned char *)key + (i << 2));
296 skey[(i << 1) + 0] = tmp;
297 skey[(i << 1) + 1] = tmp;
298 }
299 for (i = nk, j = 0, k = 0; i < nkf; i ++) {
300 if (j == 0) {
301 tmp = (tmp << 24) | (tmp >> 8);
302 tmp = sub_word(tmp) ^ Rcon[k];
303 } else if (nk > 6 && j == 4) {
304 tmp = sub_word(tmp);
305 }
306 tmp ^= skey[(i - nk) << 1];
307 skey[(i << 1) + 0] = tmp;
308 skey[(i << 1) + 1] = tmp;
309 if (++ j == nk) {
310 j = 0;
311 k ++;
312 }
313 }
314 for (i = 0; i < nkf; i += 4) {
315 br_aes_ct_ortho(skey + (i << 1));
316 }
317 for (i = 0, j = 0; i < nkf; i ++, j += 2) {
318 comp_skey[i] = (skey[j + 0] & 0x55555555)
319 | (skey[j + 1] & 0xAAAAAAAA);
320 }
321 return num_rounds;
322 }
323
324 /* see inner.h */
325 void
br_aes_ct_skey_expand(uint32_t * skey,unsigned num_rounds,const uint32_t * comp_skey)326 br_aes_ct_skey_expand(uint32_t *skey,
327 unsigned num_rounds, const uint32_t *comp_skey)
328 {
329 unsigned u, v, n;
330
331 n = (num_rounds + 1) << 2;
332 for (u = 0, v = 0; u < n; u ++, v += 2) {
333 uint32_t x, y;
334
335 x = y = comp_skey[u];
336 x &= 0x55555555;
337 skey[v + 0] = x | (x << 1);
338 y &= 0xAAAAAAAA;
339 skey[v + 1] = y | (y >> 1);
340 }
341 }
342
343 /* NetBSD additions, for computing the standard AES key schedule */
344
345 unsigned
br_aes_ct_keysched_stdenc(uint32_t * skey,const void * key,size_t key_len)346 br_aes_ct_keysched_stdenc(uint32_t *skey, const void *key, size_t key_len)
347 {
348 unsigned num_rounds;
349 int i, j, k, nk, nkf;
350 uint32_t tmp;
351
352 switch (key_len) {
353 case 16:
354 num_rounds = 10;
355 break;
356 case 24:
357 num_rounds = 12;
358 break;
359 case 32:
360 num_rounds = 14;
361 break;
362 default:
363 /* abort(); */
364 return 0;
365 }
366 nk = (int)(key_len >> 2);
367 nkf = (int)((num_rounds + 1) << 2);
368 tmp = 0;
369 for (i = 0; i < nk; i ++) {
370 tmp = br_dec32le((const unsigned char *)key + (i << 2));
371 skey[i] = tmp;
372 }
373 for (i = nk, j = 0, k = 0; i < nkf; i ++) {
374 if (j == 0) {
375 tmp = (tmp << 24) | (tmp >> 8);
376 tmp = sub_word(tmp) ^ Rcon[k];
377 } else if (nk > 6 && j == 4) {
378 tmp = sub_word(tmp);
379 }
380 tmp ^= skey[i - nk];
381 skey[i] = tmp;
382 if (++ j == nk) {
383 j = 0;
384 k ++;
385 }
386 }
387 return num_rounds;
388 }
389
390 unsigned
br_aes_ct_keysched_stddec(uint32_t * skey,const void * key,size_t key_len)391 br_aes_ct_keysched_stddec(uint32_t *skey, const void *key, size_t key_len)
392 {
393 uint32_t tkey[60];
394 uint32_t q[8];
395 unsigned num_rounds;
396 unsigned i;
397
398 num_rounds = br_aes_ct_keysched_stdenc(skey, key, key_len);
399 if (num_rounds == 0)
400 return 0;
401
402 tkey[0] = skey[4*num_rounds + 0];
403 tkey[1] = skey[4*num_rounds + 1];
404 tkey[2] = skey[4*num_rounds + 2];
405 tkey[3] = skey[4*num_rounds + 3];
406 for (i = 1; i < num_rounds; i++) {
407 q[2*0] = skey[4*i + 0];
408 q[2*1] = skey[4*i + 1];
409 q[2*2] = skey[4*i + 2];
410 q[2*3] = skey[4*i + 3];
411 q[1] = q[3] = q[5] = q[7] = 0;
412
413 br_aes_ct_ortho(q);
414 br_aes_ct_inv_mix_columns(q);
415 br_aes_ct_ortho(q);
416
417 tkey[4*(num_rounds - i) + 0] = q[2*0];
418 tkey[4*(num_rounds - i) + 1] = q[2*1];
419 tkey[4*(num_rounds - i) + 2] = q[2*2];
420 tkey[4*(num_rounds - i) + 3] = q[2*3];
421 }
422 tkey[4*num_rounds + 0] = skey[0];
423 tkey[4*num_rounds + 1] = skey[1];
424 tkey[4*num_rounds + 2] = skey[2];
425 tkey[4*num_rounds + 3] = skey[3];
426
427 memcpy(skey, tkey, 4*(num_rounds + 1)*sizeof(uint32_t));
428 explicit_memset(tkey, 0, 4*(num_rounds + 1)*sizeof(uint32_t));
429 return num_rounds;
430 }
431