1/* $NetBSD: aes_sse2_4x32.c,v 1.1 2025/11/23 22:48:26 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_sse2_4x32.c,v 1.1 2025/11/23 22:48:26 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 "aes_sse2_4x32_impl.h" 39 40/* see inner.h */ 41void 42aes_sse2_4x32_bitslice_Sbox(__m128i q[static 8]) 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 __m128i x0, x1, x2, x3, x4, x5, x6, x7; 55 __m128i y1, y2, y3, y4, y5, y6, y7, y8, y9; 56 __m128i y10, y11, y12, y13, y14, y15, y16, y17, y18, y19; 57 __m128i y20, y21; 58 __m128i z0, z1, z2, z3, z4, z5, z6, z7, z8, z9; 59 __m128i z10, z11, z12, z13, z14, z15, z16, z17; 60 __m128i t0, t1, t2, t3, t4, t5, t6, t7, t8, t9; 61 __m128i t10, t11, t12, t13, t14, t15, t16, t17, t18, t19; 62 __m128i t20, t21, t22, t23, t24, t25, t26, t27, t28, t29; 63 __m128i t30, t31, t32, t33, t34, t35, t36, t37, t38, t39; 64 __m128i t40, t41, t42, t43, t44, t45, t46, t47, t48, t49; 65 __m128i t50, t51, t52, t53, t54, t55, t56, t57, t58, t59; 66 __m128i t60, t61, t62, t63, t64, t65, t66, t67; 67 __m128i 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 */ 218void 219aes_sse2_4x32_ortho(__m128i q[static 8]) 220{ 221#define SWAPN(cl, ch, s, x, y) do { \ 222 __m128i cl128 = _mm_set1_epi32(cl); \ 223 __m128i ch128 = _mm_set1_epi32(ch); \ 224 __m128i a, b; \ 225 a = _mm_load_si128(&(x)); \ 226 b = _mm_load_si128(&(y)); \ 227 _mm_store_si128(&(x), \ 228 (a & cl128) | _mm_slli_epi32((b & cl128), (s))); \ 229 _mm_store_si128(&(y), \ 230 _mm_srli_epi32((a & ch128), (s)) | (b & ch128)); \ 231 } while (0) 232 233#define SWAP2(x, y) SWAPN(0x55555555, 0xAAAAAAAA, 1, x, y) 234#define SWAP4(x, y) SWAPN(0x33333333, 0xCCCCCCCC, 2, x, y) 235#define SWAP8(x, y) SWAPN(0x0F0F0F0F, 0xF0F0F0F0, 4, x, y) 236 237 SWAP2(q[0], q[1]); 238 SWAP2(q[2], q[3]); 239 SWAP2(q[4], q[5]); 240 SWAP2(q[6], q[7]); 241 242 SWAP4(q[0], q[2]); 243 SWAP4(q[1], q[3]); 244 SWAP4(q[4], q[6]); 245 SWAP4(q[5], q[7]); 246 247 SWAP8(q[0], q[4]); 248 SWAP8(q[1], q[5]); 249 SWAP8(q[2], q[6]); 250 SWAP8(q[3], q[7]); 251} 252 253static const unsigned char Rcon[] = { 254 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36 255}; 256 257static uint32_t 258sub_word(uint32_t x) 259{ 260 __m128i q[8]; 261 uint32_t y; 262 263 memset(q, 0, sizeof(q)); 264 q[0] = _mm_loadu_si32(&x); 265 aes_sse2_4x32_ortho(q); 266 aes_sse2_4x32_bitslice_Sbox(q); 267 aes_sse2_4x32_ortho(q); 268 _mm_storeu_si32(&y, q[0]); 269 return y; 270} 271 272/* see inner.h */ 273unsigned 274aes_sse2_4x32_keysched(uint32_t comp_skey[static 60], const void *key, 275 size_t key_len) 276{ 277 unsigned num_rounds; 278 int i, j, k, nk, nkf; 279 uint32_t tmp; 280 uint32_t skey[120]; 281 282 switch (key_len) { 283 case 16: 284 num_rounds = 10; 285 break; 286 case 24: 287 num_rounds = 12; 288 break; 289 case 32: 290 num_rounds = 14; 291 break; 292 default: 293 /* abort(); */ 294 return 0; 295 } 296 nk = (int)(key_len >> 2); 297 nkf = (int)((num_rounds + 1) << 2); 298 tmp = 0; 299 for (i = 0; i < nk; i ++) { 300 tmp = br_dec32le((const unsigned char *)key + (i << 2)); 301 skey[(i << 1) + 0] = tmp; 302 skey[(i << 1) + 1] = tmp; 303 } 304 for (i = nk, j = 0, k = 0; i < nkf; i ++) { 305 if (j == 0) { 306 tmp = (tmp << 24) | (tmp >> 8); 307 tmp = sub_word(tmp) ^ Rcon[k]; 308 } else if (nk > 6 && j == 4) { 309 tmp = sub_word(tmp); 310 } 311 tmp ^= skey[(i - nk) << 1]; 312 skey[(i << 1) + 0] = tmp; 313 skey[(i << 1) + 1] = tmp; 314 if (++ j == nk) { 315 j = 0; 316 k ++; 317 } 318 } 319 for (i = 0; i < nkf; i += 4) { 320 __m128i q[8]; 321 322 for (j = 0; j < 8; j++) 323 q[j] = _mm_loadu_si32(&skey[(i << 1) + j]); 324 aes_sse2_4x32_ortho(q); 325 for (j = 0; j < 8; j++) 326 _mm_storeu_si32(&skey[(i << 1) + j], q[j]); 327 } 328 for (i = 0, j = 0; i < nkf; i ++, j += 2) { 329 comp_skey[i] = (skey[j + 0] & 0x55555555) 330 | (skey[j + 1] & 0xAAAAAAAA); 331 } 332 return num_rounds; 333} 334 335/* see inner.h */ 336void 337aes_sse2_4x32_skey_expand(uint32_t skey[static 120], 338 unsigned num_rounds, const uint32_t comp_skey[static 60]) 339{ 340 unsigned u, v, n; 341 342 n = (num_rounds + 1) << 2; 343 for (u = 0, v = 0; u < n; u ++, v += 2) { 344 uint32_t x, y; 345 346 x = y = comp_skey[u]; 347 x &= 0x55555555; 348 skey[v + 0] = x | (x << 1); 349 y &= 0xAAAAAAAA; 350 skey[v + 1] = y | (y >> 1); 351 } 352} 353