11.1Sriastrad/*	$NetBSD: aes_sse2_4x32.c,v 1.1 2025/11/23 22:48:26 riastradh Exp $	*/
21.1Sriastrad
31.1Sriastrad/*
41.1Sriastrad * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
51.1Sriastrad *
61.1Sriastrad * Permission is hereby granted, free of charge, to any person obtaining
71.1Sriastrad * a copy of this software and associated documentation files (the
81.1Sriastrad * "Software"), to deal in the Software without restriction, including
91.1Sriastrad * without limitation the rights to use, copy, modify, merge, publish,
101.1Sriastrad * distribute, sublicense, and/or sell copies of the Software, and to
111.1Sriastrad * permit persons to whom the Software is furnished to do so, subject to
121.1Sriastrad * the following conditions:
131.1Sriastrad *
141.1Sriastrad * The above copyright notice and this permission notice shall be
151.1Sriastrad * included in all copies or substantial portions of the Software.
161.1Sriastrad *
171.1Sriastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
181.1Sriastrad * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
191.1Sriastrad * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
201.1Sriastrad * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
211.1Sriastrad * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
221.1Sriastrad * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
231.1Sriastrad * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
241.1Sriastrad * SOFTWARE.
251.1Sriastrad */
261.1Sriastrad
271.1Sriastrad#include <sys/cdefs.h>
281.1Sriastrad__KERNEL_RCSID(1, "$NetBSD: aes_sse2_4x32.c,v 1.1 2025/11/23 22:48:26 riastradh Exp $");
291.1Sriastrad
301.1Sriastrad#include <sys/types.h>
311.1Sriastrad
321.1Sriastrad#ifdef _KERNEL
331.1Sriastrad#include <lib/libkern/libkern.h>
341.1Sriastrad#else
351.1Sriastrad#include <string.h>
361.1Sriastrad#endif
371.1Sriastrad
381.1Sriastrad#include "aes_sse2_4x32_impl.h"
391.1Sriastrad
401.1Sriastrad/* see inner.h */
411.1Sriastradvoid
421.1Sriastradaes_sse2_4x32_bitslice_Sbox(__m128i q[static 8])
431.1Sriastrad{
441.1Sriastrad	/*
451.1Sriastrad	 * This S-box implementation is a straightforward translation of
461.1Sriastrad	 * the circuit described by Boyar and Peralta in "A new
471.1Sriastrad	 * combinational logic minimization technique with applications
481.1Sriastrad	 * to cryptology" (https://eprint.iacr.org/2009/191.pdf).
491.1Sriastrad	 *
501.1Sriastrad	 * Note that variables x* (input) and s* (output) are numbered
511.1Sriastrad	 * in "reverse" order (x0 is the high bit, x7 is the low bit).
521.1Sriastrad	 */
531.1Sriastrad
541.1Sriastrad	__m128i x0, x1, x2, x3, x4, x5, x6, x7;
551.1Sriastrad	__m128i y1, y2, y3, y4, y5, y6, y7, y8, y9;
561.1Sriastrad	__m128i y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
571.1Sriastrad	__m128i y20, y21;
581.1Sriastrad	__m128i z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
591.1Sriastrad	__m128i z10, z11, z12, z13, z14, z15, z16, z17;
601.1Sriastrad	__m128i t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
611.1Sriastrad	__m128i t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
621.1Sriastrad	__m128i t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
631.1Sriastrad	__m128i t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
641.1Sriastrad	__m128i t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
651.1Sriastrad	__m128i t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
661.1Sriastrad	__m128i t60, t61, t62, t63, t64, t65, t66, t67;
671.1Sriastrad	__m128i s0, s1, s2, s3, s4, s5, s6, s7;
681.1Sriastrad
691.1Sriastrad	x0 = q[7];
701.1Sriastrad	x1 = q[6];
711.1Sriastrad	x2 = q[5];
721.1Sriastrad	x3 = q[4];
731.1Sriastrad	x4 = q[3];
741.1Sriastrad	x5 = q[2];
751.1Sriastrad	x6 = q[1];
761.1Sriastrad	x7 = q[0];
771.1Sriastrad
781.1Sriastrad	/*
791.1Sriastrad	 * Top linear transformation.
801.1Sriastrad	 */
811.1Sriastrad	y14 = x3 ^ x5;
821.1Sriastrad	y13 = x0 ^ x6;
831.1Sriastrad	y9 = x0 ^ x3;
841.1Sriastrad	y8 = x0 ^ x5;
851.1Sriastrad	t0 = x1 ^ x2;
861.1Sriastrad	y1 = t0 ^ x7;
871.1Sriastrad	y4 = y1 ^ x3;
881.1Sriastrad	y12 = y13 ^ y14;
891.1Sriastrad	y2 = y1 ^ x0;
901.1Sriastrad	y5 = y1 ^ x6;
911.1Sriastrad	y3 = y5 ^ y8;
921.1Sriastrad	t1 = x4 ^ y12;
931.1Sriastrad	y15 = t1 ^ x5;
941.1Sriastrad	y20 = t1 ^ x1;
951.1Sriastrad	y6 = y15 ^ x7;
961.1Sriastrad	y10 = y15 ^ t0;
971.1Sriastrad	y11 = y20 ^ y9;
981.1Sriastrad	y7 = x7 ^ y11;
991.1Sriastrad	y17 = y10 ^ y11;
1001.1Sriastrad	y19 = y10 ^ y8;
1011.1Sriastrad	y16 = t0 ^ y11;
1021.1Sriastrad	y21 = y13 ^ y16;
1031.1Sriastrad	y18 = x0 ^ y16;
1041.1Sriastrad
1051.1Sriastrad	/*
1061.1Sriastrad	 * Non-linear section.
1071.1Sriastrad	 */
1081.1Sriastrad	t2 = y12 & y15;
1091.1Sriastrad	t3 = y3 & y6;
1101.1Sriastrad	t4 = t3 ^ t2;
1111.1Sriastrad	t5 = y4 & x7;
1121.1Sriastrad	t6 = t5 ^ t2;
1131.1Sriastrad	t7 = y13 & y16;
1141.1Sriastrad	t8 = y5 & y1;
1151.1Sriastrad	t9 = t8 ^ t7;
1161.1Sriastrad	t10 = y2 & y7;
1171.1Sriastrad	t11 = t10 ^ t7;
1181.1Sriastrad	t12 = y9 & y11;
1191.1Sriastrad	t13 = y14 & y17;
1201.1Sriastrad	t14 = t13 ^ t12;
1211.1Sriastrad	t15 = y8 & y10;
1221.1Sriastrad	t16 = t15 ^ t12;
1231.1Sriastrad	t17 = t4 ^ t14;
1241.1Sriastrad	t18 = t6 ^ t16;
1251.1Sriastrad	t19 = t9 ^ t14;
1261.1Sriastrad	t20 = t11 ^ t16;
1271.1Sriastrad	t21 = t17 ^ y20;
1281.1Sriastrad	t22 = t18 ^ y19;
1291.1Sriastrad	t23 = t19 ^ y21;
1301.1Sriastrad	t24 = t20 ^ y18;
1311.1Sriastrad
1321.1Sriastrad	t25 = t21 ^ t22;
1331.1Sriastrad	t26 = t21 & t23;
1341.1Sriastrad	t27 = t24 ^ t26;
1351.1Sriastrad	t28 = t25 & t27;
1361.1Sriastrad	t29 = t28 ^ t22;
1371.1Sriastrad	t30 = t23 ^ t24;
1381.1Sriastrad	t31 = t22 ^ t26;
1391.1Sriastrad	t32 = t31 & t30;
1401.1Sriastrad	t33 = t32 ^ t24;
1411.1Sriastrad	t34 = t23 ^ t33;
1421.1Sriastrad	t35 = t27 ^ t33;
1431.1Sriastrad	t36 = t24 & t35;
1441.1Sriastrad	t37 = t36 ^ t34;
1451.1Sriastrad	t38 = t27 ^ t36;
1461.1Sriastrad	t39 = t29 & t38;
1471.1Sriastrad	t40 = t25 ^ t39;
1481.1Sriastrad
1491.1Sriastrad	t41 = t40 ^ t37;
1501.1Sriastrad	t42 = t29 ^ t33;
1511.1Sriastrad	t43 = t29 ^ t40;
1521.1Sriastrad	t44 = t33 ^ t37;
1531.1Sriastrad	t45 = t42 ^ t41;
1541.1Sriastrad	z0 = t44 & y15;
1551.1Sriastrad	z1 = t37 & y6;
1561.1Sriastrad	z2 = t33 & x7;
1571.1Sriastrad	z3 = t43 & y16;
1581.1Sriastrad	z4 = t40 & y1;
1591.1Sriastrad	z5 = t29 & y7;
1601.1Sriastrad	z6 = t42 & y11;
1611.1Sriastrad	z7 = t45 & y17;
1621.1Sriastrad	z8 = t41 & y10;
1631.1Sriastrad	z9 = t44 & y12;
1641.1Sriastrad	z10 = t37 & y3;
1651.1Sriastrad	z11 = t33 & y4;
1661.1Sriastrad	z12 = t43 & y13;
1671.1Sriastrad	z13 = t40 & y5;
1681.1Sriastrad	z14 = t29 & y2;
1691.1Sriastrad	z15 = t42 & y9;
1701.1Sriastrad	z16 = t45 & y14;
1711.1Sriastrad	z17 = t41 & y8;
1721.1Sriastrad
1731.1Sriastrad	/*
1741.1Sriastrad	 * Bottom linear transformation.
1751.1Sriastrad	 */
1761.1Sriastrad	t46 = z15 ^ z16;
1771.1Sriastrad	t47 = z10 ^ z11;
1781.1Sriastrad	t48 = z5 ^ z13;
1791.1Sriastrad	t49 = z9 ^ z10;
1801.1Sriastrad	t50 = z2 ^ z12;
1811.1Sriastrad	t51 = z2 ^ z5;
1821.1Sriastrad	t52 = z7 ^ z8;
1831.1Sriastrad	t53 = z0 ^ z3;
1841.1Sriastrad	t54 = z6 ^ z7;
1851.1Sriastrad	t55 = z16 ^ z17;
1861.1Sriastrad	t56 = z12 ^ t48;
1871.1Sriastrad	t57 = t50 ^ t53;
1881.1Sriastrad	t58 = z4 ^ t46;
1891.1Sriastrad	t59 = z3 ^ t54;
1901.1Sriastrad	t60 = t46 ^ t57;
1911.1Sriastrad	t61 = z14 ^ t57;
1921.1Sriastrad	t62 = t52 ^ t58;
1931.1Sriastrad	t63 = t49 ^ t58;
1941.1Sriastrad	t64 = z4 ^ t59;
1951.1Sriastrad	t65 = t61 ^ t62;
1961.1Sriastrad	t66 = z1 ^ t63;
1971.1Sriastrad	s0 = t59 ^ t63;
1981.1Sriastrad	s6 = t56 ^ ~t62;
1991.1Sriastrad	s7 = t48 ^ ~t60;
2001.1Sriastrad	t67 = t64 ^ t65;
2011.1Sriastrad	s3 = t53 ^ t66;
2021.1Sriastrad	s4 = t51 ^ t66;
2031.1Sriastrad	s5 = t47 ^ t65;
2041.1Sriastrad	s1 = t64 ^ ~s3;
2051.1Sriastrad	s2 = t55 ^ ~t67;
2061.1Sriastrad
2071.1Sriastrad	q[7] = s0;
2081.1Sriastrad	q[6] = s1;
2091.1Sriastrad	q[5] = s2;
2101.1Sriastrad	q[4] = s3;
2111.1Sriastrad	q[3] = s4;
2121.1Sriastrad	q[2] = s5;
2131.1Sriastrad	q[1] = s6;
2141.1Sriastrad	q[0] = s7;
2151.1Sriastrad}
2161.1Sriastrad
2171.1Sriastrad/* see inner.h */
2181.1Sriastradvoid
2191.1Sriastradaes_sse2_4x32_ortho(__m128i q[static 8])
2201.1Sriastrad{
2211.1Sriastrad#define SWAPN(cl, ch, s, x, y)   do { \
2221.1Sriastrad		__m128i cl128 = _mm_set1_epi32(cl); \
2231.1Sriastrad		__m128i ch128 = _mm_set1_epi32(ch); \
2241.1Sriastrad		__m128i a, b; \
2251.1Sriastrad		a = _mm_load_si128(&(x)); \
2261.1Sriastrad		b = _mm_load_si128(&(y)); \
2271.1Sriastrad		_mm_store_si128(&(x), \
2281.1Sriastrad		    (a & cl128) | _mm_slli_epi32((b & cl128), (s))); \
2291.1Sriastrad		_mm_store_si128(&(y), \
2301.1Sriastrad		    _mm_srli_epi32((a & ch128), (s)) | (b & ch128)); \
2311.1Sriastrad	} while (0)
2321.1Sriastrad
2331.1Sriastrad#define SWAP2(x, y)   SWAPN(0x55555555, 0xAAAAAAAA, 1, x, y)
2341.1Sriastrad#define SWAP4(x, y)   SWAPN(0x33333333, 0xCCCCCCCC, 2, x, y)
2351.1Sriastrad#define SWAP8(x, y)   SWAPN(0x0F0F0F0F, 0xF0F0F0F0, 4, x, y)
2361.1Sriastrad
2371.1Sriastrad	SWAP2(q[0], q[1]);
2381.1Sriastrad	SWAP2(q[2], q[3]);
2391.1Sriastrad	SWAP2(q[4], q[5]);
2401.1Sriastrad	SWAP2(q[6], q[7]);
2411.1Sriastrad
2421.1Sriastrad	SWAP4(q[0], q[2]);
2431.1Sriastrad	SWAP4(q[1], q[3]);
2441.1Sriastrad	SWAP4(q[4], q[6]);
2451.1Sriastrad	SWAP4(q[5], q[7]);
2461.1Sriastrad
2471.1Sriastrad	SWAP8(q[0], q[4]);
2481.1Sriastrad	SWAP8(q[1], q[5]);
2491.1Sriastrad	SWAP8(q[2], q[6]);
2501.1Sriastrad	SWAP8(q[3], q[7]);
2511.1Sriastrad}
2521.1Sriastrad
2531.1Sriastradstatic const unsigned char Rcon[] = {
2541.1Sriastrad	0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
2551.1Sriastrad};
2561.1Sriastrad
2571.1Sriastradstatic uint32_t
2581.1Sriastradsub_word(uint32_t x)
2591.1Sriastrad{
2601.1Sriastrad	__m128i q[8];
2611.1Sriastrad	uint32_t y;
2621.1Sriastrad
2631.1Sriastrad	memset(q, 0, sizeof(q));
2641.1Sriastrad	q[0] = _mm_loadu_si32(&x);
2651.1Sriastrad	aes_sse2_4x32_ortho(q);
2661.1Sriastrad	aes_sse2_4x32_bitslice_Sbox(q);
2671.1Sriastrad	aes_sse2_4x32_ortho(q);
2681.1Sriastrad	_mm_storeu_si32(&y, q[0]);
2691.1Sriastrad	return y;
2701.1Sriastrad}
2711.1Sriastrad
2721.1Sriastrad/* see inner.h */
2731.1Sriastradunsigned
2741.1Sriastradaes_sse2_4x32_keysched(uint32_t comp_skey[static 60], const void *key,
2751.1Sriastrad    size_t key_len)
2761.1Sriastrad{
2771.1Sriastrad	unsigned num_rounds;
2781.1Sriastrad	int i, j, k, nk, nkf;
2791.1Sriastrad	uint32_t tmp;
2801.1Sriastrad	uint32_t skey[120];
2811.1Sriastrad
2821.1Sriastrad	switch (key_len) {
2831.1Sriastrad	case 16:
2841.1Sriastrad		num_rounds = 10;
2851.1Sriastrad		break;
2861.1Sriastrad	case 24:
2871.1Sriastrad		num_rounds = 12;
2881.1Sriastrad		break;
2891.1Sriastrad	case 32:
2901.1Sriastrad		num_rounds = 14;
2911.1Sriastrad		break;
2921.1Sriastrad	default:
2931.1Sriastrad		/* abort(); */
2941.1Sriastrad		return 0;
2951.1Sriastrad	}
2961.1Sriastrad	nk = (int)(key_len >> 2);
2971.1Sriastrad	nkf = (int)((num_rounds + 1) << 2);
2981.1Sriastrad	tmp = 0;
2991.1Sriastrad	for (i = 0; i < nk; i ++) {
3001.1Sriastrad		tmp = br_dec32le((const unsigned char *)key + (i << 2));
3011.1Sriastrad		skey[(i << 1) + 0] = tmp;
3021.1Sriastrad		skey[(i << 1) + 1] = tmp;
3031.1Sriastrad	}
3041.1Sriastrad	for (i = nk, j = 0, k = 0; i < nkf; i ++) {
3051.1Sriastrad		if (j == 0) {
3061.1Sriastrad			tmp = (tmp << 24) | (tmp >> 8);
3071.1Sriastrad			tmp = sub_word(tmp) ^ Rcon[k];
3081.1Sriastrad		} else if (nk > 6 && j == 4) {
3091.1Sriastrad			tmp = sub_word(tmp);
3101.1Sriastrad		}
3111.1Sriastrad		tmp ^= skey[(i - nk) << 1];
3121.1Sriastrad		skey[(i << 1) + 0] = tmp;
3131.1Sriastrad		skey[(i << 1) + 1] = tmp;
3141.1Sriastrad		if (++ j == nk) {
3151.1Sriastrad			j = 0;
3161.1Sriastrad			k ++;
3171.1Sriastrad		}
3181.1Sriastrad	}
3191.1Sriastrad	for (i = 0; i < nkf; i += 4) {
3201.1Sriastrad		__m128i q[8];
3211.1Sriastrad
3221.1Sriastrad		for (j = 0; j < 8; j++)
3231.1Sriastrad			q[j] = _mm_loadu_si32(&skey[(i << 1) + j]);
3241.1Sriastrad		aes_sse2_4x32_ortho(q);
3251.1Sriastrad		for (j = 0; j < 8; j++)
3261.1Sriastrad			_mm_storeu_si32(&skey[(i << 1) + j], q[j]);
3271.1Sriastrad	}
3281.1Sriastrad	for (i = 0, j = 0; i < nkf; i ++, j += 2) {
3291.1Sriastrad		comp_skey[i] = (skey[j + 0] & 0x55555555)
3301.1Sriastrad			| (skey[j + 1] & 0xAAAAAAAA);
3311.1Sriastrad	}
3321.1Sriastrad	return num_rounds;
3331.1Sriastrad}
3341.1Sriastrad
3351.1Sriastrad/* see inner.h */
3361.1Sriastradvoid
3371.1Sriastradaes_sse2_4x32_skey_expand(uint32_t skey[static 120],
3381.1Sriastrad	unsigned num_rounds, const uint32_t comp_skey[static 60])
3391.1Sriastrad{
3401.1Sriastrad	unsigned u, v, n;
3411.1Sriastrad
3421.1Sriastrad	n = (num_rounds + 1) << 2;
3431.1Sriastrad	for (u = 0, v = 0; u < n; u ++, v += 2) {
3441.1Sriastrad		uint32_t x, y;
3451.1Sriastrad
3461.1Sriastrad		x = y = comp_skey[u];
3471.1Sriastrad		x &= 0x55555555;
3481.1Sriastrad		skey[v + 0] = x | (x << 1);
3491.1Sriastrad		y &= 0xAAAAAAAA;
3501.1Sriastrad		skey[v + 1] = y | (y >> 1);
3511.1Sriastrad	}
3521.1Sriastrad}
353