1/*	$NetBSD: aes_ct64.c,v 1.1 2025/11/23 22:44:13 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_ct64.c,v 1.1 2025/11/23 22:44:13 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_bear64.h>
39
40static void
41br_range_dec32le(uint32_t *p32, size_t nwords, const void *v)
42{
43	const uint8_t *p8 = v;
44
45	while (nwords --> 0) {
46		uint32_t x0 = *p8++;
47		uint32_t x1 = *p8++;
48		uint32_t x2 = *p8++;
49		uint32_t x3 = *p8++;
50
51		*p32++ = x0 | (x1 << 8) | (x2 << 16) | (x3 << 24);
52	}
53}
54
55/* see inner.h */
56void
57br_aes_ct64_bitslice_Sbox(uint64_t q[static 8])
58{
59	/*
60	 * This S-box implementation is a straightforward translation of
61	 * the circuit described by Boyar and Peralta in "A new
62	 * combinational logic minimization technique with applications
63	 * to cryptology" (https://eprint.iacr.org/2009/191.pdf).
64	 *
65	 * Note that variables x* (input) and s* (output) are numbered
66	 * in "reverse" order (x0 is the high bit, x7 is the low bit).
67	 */
68
69	uint64_t x0, x1, x2, x3, x4, x5, x6, x7;
70	uint64_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
71	uint64_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
72	uint64_t y20, y21;
73	uint64_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
74	uint64_t z10, z11, z12, z13, z14, z15, z16, z17;
75	uint64_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
76	uint64_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
77	uint64_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
78	uint64_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
79	uint64_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
80	uint64_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
81	uint64_t t60, t61, t62, t63, t64, t65, t66, t67;
82	uint64_t s0, s1, s2, s3, s4, s5, s6, s7;
83
84	x0 = q[7];
85	x1 = q[6];
86	x2 = q[5];
87	x3 = q[4];
88	x4 = q[3];
89	x5 = q[2];
90	x6 = q[1];
91	x7 = q[0];
92
93	/*
94	 * Top linear transformation.
95	 */
96	y14 = x3 ^ x5;
97	y13 = x0 ^ x6;
98	y9 = x0 ^ x3;
99	y8 = x0 ^ x5;
100	t0 = x1 ^ x2;
101	y1 = t0 ^ x7;
102	y4 = y1 ^ x3;
103	y12 = y13 ^ y14;
104	y2 = y1 ^ x0;
105	y5 = y1 ^ x6;
106	y3 = y5 ^ y8;
107	t1 = x4 ^ y12;
108	y15 = t1 ^ x5;
109	y20 = t1 ^ x1;
110	y6 = y15 ^ x7;
111	y10 = y15 ^ t0;
112	y11 = y20 ^ y9;
113	y7 = x7 ^ y11;
114	y17 = y10 ^ y11;
115	y19 = y10 ^ y8;
116	y16 = t0 ^ y11;
117	y21 = y13 ^ y16;
118	y18 = x0 ^ y16;
119
120	/*
121	 * Non-linear section.
122	 */
123	t2 = y12 & y15;
124	t3 = y3 & y6;
125	t4 = t3 ^ t2;
126	t5 = y4 & x7;
127	t6 = t5 ^ t2;
128	t7 = y13 & y16;
129	t8 = y5 & y1;
130	t9 = t8 ^ t7;
131	t10 = y2 & y7;
132	t11 = t10 ^ t7;
133	t12 = y9 & y11;
134	t13 = y14 & y17;
135	t14 = t13 ^ t12;
136	t15 = y8 & y10;
137	t16 = t15 ^ t12;
138	t17 = t4 ^ t14;
139	t18 = t6 ^ t16;
140	t19 = t9 ^ t14;
141	t20 = t11 ^ t16;
142	t21 = t17 ^ y20;
143	t22 = t18 ^ y19;
144	t23 = t19 ^ y21;
145	t24 = t20 ^ y18;
146
147	t25 = t21 ^ t22;
148	t26 = t21 & t23;
149	t27 = t24 ^ t26;
150	t28 = t25 & t27;
151	t29 = t28 ^ t22;
152	t30 = t23 ^ t24;
153	t31 = t22 ^ t26;
154	t32 = t31 & t30;
155	t33 = t32 ^ t24;
156	t34 = t23 ^ t33;
157	t35 = t27 ^ t33;
158	t36 = t24 & t35;
159	t37 = t36 ^ t34;
160	t38 = t27 ^ t36;
161	t39 = t29 & t38;
162	t40 = t25 ^ t39;
163
164	t41 = t40 ^ t37;
165	t42 = t29 ^ t33;
166	t43 = t29 ^ t40;
167	t44 = t33 ^ t37;
168	t45 = t42 ^ t41;
169	z0 = t44 & y15;
170	z1 = t37 & y6;
171	z2 = t33 & x7;
172	z3 = t43 & y16;
173	z4 = t40 & y1;
174	z5 = t29 & y7;
175	z6 = t42 & y11;
176	z7 = t45 & y17;
177	z8 = t41 & y10;
178	z9 = t44 & y12;
179	z10 = t37 & y3;
180	z11 = t33 & y4;
181	z12 = t43 & y13;
182	z13 = t40 & y5;
183	z14 = t29 & y2;
184	z15 = t42 & y9;
185	z16 = t45 & y14;
186	z17 = t41 & y8;
187
188	/*
189	 * Bottom linear transformation.
190	 */
191	t46 = z15 ^ z16;
192	t47 = z10 ^ z11;
193	t48 = z5 ^ z13;
194	t49 = z9 ^ z10;
195	t50 = z2 ^ z12;
196	t51 = z2 ^ z5;
197	t52 = z7 ^ z8;
198	t53 = z0 ^ z3;
199	t54 = z6 ^ z7;
200	t55 = z16 ^ z17;
201	t56 = z12 ^ t48;
202	t57 = t50 ^ t53;
203	t58 = z4 ^ t46;
204	t59 = z3 ^ t54;
205	t60 = t46 ^ t57;
206	t61 = z14 ^ t57;
207	t62 = t52 ^ t58;
208	t63 = t49 ^ t58;
209	t64 = z4 ^ t59;
210	t65 = t61 ^ t62;
211	t66 = z1 ^ t63;
212	s0 = t59 ^ t63;
213	s6 = t56 ^ ~t62;
214	s7 = t48 ^ ~t60;
215	t67 = t64 ^ t65;
216	s3 = t53 ^ t66;
217	s4 = t51 ^ t66;
218	s5 = t47 ^ t65;
219	s1 = t64 ^ ~s3;
220	s2 = t55 ^ ~t67;
221
222	q[7] = s0;
223	q[6] = s1;
224	q[5] = s2;
225	q[4] = s3;
226	q[3] = s4;
227	q[2] = s5;
228	q[1] = s6;
229	q[0] = s7;
230}
231
232/* see inner.h */
233void
234br_aes_ct64_ortho(uint64_t q[static 8])
235{
236#define SWAPN(cl, ch, s, x, y)   do { \
237		uint64_t a, b; \
238		a = (x); \
239		b = (y); \
240		(x) = (a & (uint64_t)cl) | ((b & (uint64_t)cl) << (s)); \
241		(y) = ((a & (uint64_t)ch) >> (s)) | (b & (uint64_t)ch); \
242	} while (0)
243
244#define SWAP2(x, y)    SWAPN(0x5555555555555555, 0xAAAAAAAAAAAAAAAA,  1, x, y)
245#define SWAP4(x, y)    SWAPN(0x3333333333333333, 0xCCCCCCCCCCCCCCCC,  2, x, y)
246#define SWAP8(x, y)    SWAPN(0x0F0F0F0F0F0F0F0F, 0xF0F0F0F0F0F0F0F0,  4, x, y)
247
248	SWAP2(q[0], q[1]);
249	SWAP2(q[2], q[3]);
250	SWAP2(q[4], q[5]);
251	SWAP2(q[6], q[7]);
252
253	SWAP4(q[0], q[2]);
254	SWAP4(q[1], q[3]);
255	SWAP4(q[4], q[6]);
256	SWAP4(q[5], q[7]);
257
258	SWAP8(q[0], q[4]);
259	SWAP8(q[1], q[5]);
260	SWAP8(q[2], q[6]);
261	SWAP8(q[3], q[7]);
262}
263
264/* see inner.h */
265void
266br_aes_ct64_interleave_in(uint64_t q0[static 1], uint64_t q1[static 1],
267	const uint32_t w[static 4])
268{
269	uint64_t x0, x1, x2, x3;
270
271	x0 = w[0];
272	x1 = w[1];
273	x2 = w[2];
274	x3 = w[3];
275	x0 |= (x0 << 16);
276	x1 |= (x1 << 16);
277	x2 |= (x2 << 16);
278	x3 |= (x3 << 16);
279	x0 &= (uint64_t)0x0000FFFF0000FFFF;
280	x1 &= (uint64_t)0x0000FFFF0000FFFF;
281	x2 &= (uint64_t)0x0000FFFF0000FFFF;
282	x3 &= (uint64_t)0x0000FFFF0000FFFF;
283	x0 |= (x0 << 8);
284	x1 |= (x1 << 8);
285	x2 |= (x2 << 8);
286	x3 |= (x3 << 8);
287	x0 &= (uint64_t)0x00FF00FF00FF00FF;
288	x1 &= (uint64_t)0x00FF00FF00FF00FF;
289	x2 &= (uint64_t)0x00FF00FF00FF00FF;
290	x3 &= (uint64_t)0x00FF00FF00FF00FF;
291	*q0 = x0 | (x2 << 8);
292	*q1 = x1 | (x3 << 8);
293}
294
295/* see inner.h */
296void
297br_aes_ct64_interleave_out(uint32_t w[static 4], uint64_t q0, uint64_t q1)
298{
299	uint64_t x0, x1, x2, x3;
300
301	x0 = q0 & (uint64_t)0x00FF00FF00FF00FF;
302	x1 = q1 & (uint64_t)0x00FF00FF00FF00FF;
303	x2 = (q0 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
304	x3 = (q1 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
305	x0 |= (x0 >> 8);
306	x1 |= (x1 >> 8);
307	x2 |= (x2 >> 8);
308	x3 |= (x3 >> 8);
309	x0 &= (uint64_t)0x0000FFFF0000FFFF;
310	x1 &= (uint64_t)0x0000FFFF0000FFFF;
311	x2 &= (uint64_t)0x0000FFFF0000FFFF;
312	x3 &= (uint64_t)0x0000FFFF0000FFFF;
313	w[0] = (uint32_t)x0 | (uint32_t)(x0 >> 16);
314	w[1] = (uint32_t)x1 | (uint32_t)(x1 >> 16);
315	w[2] = (uint32_t)x2 | (uint32_t)(x2 >> 16);
316	w[3] = (uint32_t)x3 | (uint32_t)(x3 >> 16);
317}
318
319static const unsigned char Rcon[] = {
320	0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
321};
322
323static uint32_t
324sub_word(uint32_t x)
325{
326	uint64_t q[8];
327
328	memset(q, 0, sizeof q);
329	q[0] = x;
330	br_aes_ct64_ortho(q);
331	br_aes_ct64_bitslice_Sbox(q);
332	br_aes_ct64_ortho(q);
333	return (uint32_t)q[0];
334}
335
336/* see inner.h */
337unsigned
338br_aes_ct64_keysched(uint64_t comp_skey[static 30],
339	const void *key, size_t key_len)
340{
341	unsigned num_rounds;
342	int i, j, k, nk, nkf;
343	uint32_t tmp;
344	uint32_t skey[60];
345
346	switch (key_len) {
347	case 16:
348		num_rounds = 10;
349		break;
350	case 24:
351		num_rounds = 12;
352		break;
353	case 32:
354		num_rounds = 14;
355		break;
356	default:
357		/* abort(); */
358		return 0;
359	}
360	nk = (int)(key_len >> 2);
361	nkf = (int)((num_rounds + 1) << 2);
362	br_range_dec32le(skey, (key_len >> 2), key);
363	tmp = skey[(key_len >> 2) - 1];
364	for (i = nk, j = 0, k = 0; i < nkf; i ++) {
365		if (j == 0) {
366			tmp = (tmp << 24) | (tmp >> 8);
367			tmp = sub_word(tmp) ^ Rcon[k];
368		} else if (nk > 6 && j == 4) {
369			tmp = sub_word(tmp);
370		}
371		tmp ^= skey[i - nk];
372		skey[i] = tmp;
373		if (++ j == nk) {
374			j = 0;
375			k ++;
376		}
377	}
378
379	for (i = 0, j = 0; i < nkf; i += 4, j += 2) {
380		uint64_t q[8];
381
382		br_aes_ct64_interleave_in(&q[0], &q[4], skey + i);
383		q[1] = q[0];
384		q[2] = q[0];
385		q[3] = q[0];
386		q[5] = q[4];
387		q[6] = q[4];
388		q[7] = q[4];
389		br_aes_ct64_ortho(q);
390		comp_skey[j + 0] =
391			  (q[0] & (uint64_t)0x1111111111111111)
392			| (q[1] & (uint64_t)0x2222222222222222)
393			| (q[2] & (uint64_t)0x4444444444444444)
394			| (q[3] & (uint64_t)0x8888888888888888);
395		comp_skey[j + 1] =
396			  (q[4] & (uint64_t)0x1111111111111111)
397			| (q[5] & (uint64_t)0x2222222222222222)
398			| (q[6] & (uint64_t)0x4444444444444444)
399			| (q[7] & (uint64_t)0x8888888888888888);
400	}
401	return num_rounds;
402}
403
404/* see inner.h */
405void
406br_aes_ct64_skey_expand(uint64_t skey[static 120],
407	unsigned num_rounds, const uint64_t comp_skey[static 30])
408{
409	unsigned u, v, n;
410
411	n = (num_rounds + 1) << 1;
412	for (u = 0, v = 0; u < n; u ++, v += 4) {
413		uint64_t x0, x1, x2, x3;
414
415		x0 = x1 = x2 = x3 = comp_skey[u];
416		x0 &= (uint64_t)0x1111111111111111;
417		x1 &= (uint64_t)0x2222222222222222;
418		x2 &= (uint64_t)0x4444444444444444;
419		x3 &= (uint64_t)0x8888888888888888;
420		x1 >>= 1;
421		x2 >>= 2;
422		x3 >>= 3;
423		skey[v + 0] = (x0 << 4) - x0;
424		skey[v + 1] = (x1 << 4) - x1;
425		skey[v + 2] = (x2 << 4) - x2;
426		skey[v + 3] = (x3 << 4) - x3;
427	}
428}
429
430/* NetBSD additions, for computing the standard AES key schedule */
431
432unsigned
433br_aes_ct64_keysched_stdenc(uint32_t *skey, const void *key, size_t key_len)
434{
435	unsigned num_rounds;
436	int i, j, k, nk, nkf;
437	uint32_t tmp;
438
439	switch (key_len) {
440	case 16:
441		num_rounds = 10;
442		break;
443	case 24:
444		num_rounds = 12;
445		break;
446	case 32:
447		num_rounds = 14;
448		break;
449	default:
450		/* abort(); */
451		return 0;
452	}
453	nk = (int)(key_len >> 2);
454	nkf = (int)((num_rounds + 1) << 2);
455	tmp = 0;
456	for (i = 0; i < nk; i ++) {
457		tmp = br_dec32le((const unsigned char *)key + (i << 2));
458		skey[i] = tmp;
459	}
460	for (i = nk, j = 0, k = 0; i < nkf; i ++) {
461		if (j == 0) {
462			tmp = (tmp << 24) | (tmp >> 8);
463			tmp = sub_word(tmp) ^ Rcon[k];
464		} else if (nk > 6 && j == 4) {
465			tmp = sub_word(tmp);
466		}
467		tmp ^= skey[i - nk];
468		skey[i] = tmp;
469		if (++ j == nk) {
470			j = 0;
471			k ++;
472		}
473	}
474	return num_rounds;
475}
476
477unsigned
478br_aes_ct64_keysched_stddec(uint32_t *skey, const void *key, size_t key_len)
479{
480	uint32_t tkey[60];
481	uint64_t q[8];
482	unsigned num_rounds;
483	unsigned i;
484
485	num_rounds = br_aes_ct64_keysched_stdenc(skey, key, key_len);
486	if (num_rounds == 0)
487		return 0;
488
489	q[1] = q[2] = q[3] = 0;
490	q[5] = q[6] = q[7] = 0;
491
492	tkey[0] = skey[4*num_rounds + 0];
493	tkey[1] = skey[4*num_rounds + 1];
494	tkey[2] = skey[4*num_rounds + 2];
495	tkey[3] = skey[4*num_rounds + 3];
496	for (i = 1; i < num_rounds; i++) {
497		br_aes_ct64_interleave_in(&q[0], &q[4], skey + 4*i);
498		br_aes_ct64_ortho(q);
499		br_aes_ct64_inv_mix_columns(q);
500		br_aes_ct64_ortho(q);
501		br_aes_ct64_interleave_out(&tkey[4*(num_rounds - i)],
502		    q[0], q[4]);
503	}
504	tkey[4*num_rounds + 0] = skey[0];
505	tkey[4*num_rounds + 1] = skey[1];
506	tkey[4*num_rounds + 2] = skey[2];
507	tkey[4*num_rounds + 3] = skey[3];
508
509	memcpy(skey, tkey, 4*(num_rounds + 1)*sizeof(uint32_t));
510	explicit_memset(tkey, 0, 4*(num_rounds + 1)*sizeof(uint32_t));
511	return num_rounds;
512}
513