11.1Sriastrad/* $NetBSD: aes_ct64.c,v 1.1 2025/11/23 22:44:13 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_ct64.c,v 1.1 2025/11/23 22:44:13 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 <crypto/aes/aes_bear64.h> 391.1Sriastrad 401.1Sriastradstatic void 411.1Sriastradbr_range_dec32le(uint32_t *p32, size_t nwords, const void *v) 421.1Sriastrad{ 431.1Sriastrad const uint8_t *p8 = v; 441.1Sriastrad 451.1Sriastrad while (nwords --> 0) { 461.1Sriastrad uint32_t x0 = *p8++; 471.1Sriastrad uint32_t x1 = *p8++; 481.1Sriastrad uint32_t x2 = *p8++; 491.1Sriastrad uint32_t x3 = *p8++; 501.1Sriastrad 511.1Sriastrad *p32++ = x0 | (x1 << 8) | (x2 << 16) | (x3 << 24); 521.1Sriastrad } 531.1Sriastrad} 541.1Sriastrad 551.1Sriastrad/* see inner.h */ 561.1Sriastradvoid 571.1Sriastradbr_aes_ct64_bitslice_Sbox(uint64_t q[static 8]) 581.1Sriastrad{ 591.1Sriastrad /* 601.1Sriastrad * This S-box implementation is a straightforward translation of 611.1Sriastrad * the circuit described by Boyar and Peralta in "A new 621.1Sriastrad * combinational logic minimization technique with applications 631.1Sriastrad * to cryptology" (https://eprint.iacr.org/2009/191.pdf). 641.1Sriastrad * 651.1Sriastrad * Note that variables x* (input) and s* (output) are numbered 661.1Sriastrad * in "reverse" order (x0 is the high bit, x7 is the low bit). 671.1Sriastrad */ 681.1Sriastrad 691.1Sriastrad uint64_t x0, x1, x2, x3, x4, x5, x6, x7; 701.1Sriastrad uint64_t y1, y2, y3, y4, y5, y6, y7, y8, y9; 711.1Sriastrad uint64_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19; 721.1Sriastrad uint64_t y20, y21; 731.1Sriastrad uint64_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9; 741.1Sriastrad uint64_t z10, z11, z12, z13, z14, z15, z16, z17; 751.1Sriastrad uint64_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9; 761.1Sriastrad uint64_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19; 771.1Sriastrad uint64_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29; 781.1Sriastrad uint64_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39; 791.1Sriastrad uint64_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49; 801.1Sriastrad uint64_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59; 811.1Sriastrad uint64_t t60, t61, t62, t63, t64, t65, t66, t67; 821.1Sriastrad uint64_t s0, s1, s2, s3, s4, s5, s6, s7; 831.1Sriastrad 841.1Sriastrad x0 = q[7]; 851.1Sriastrad x1 = q[6]; 861.1Sriastrad x2 = q[5]; 871.1Sriastrad x3 = q[4]; 881.1Sriastrad x4 = q[3]; 891.1Sriastrad x5 = q[2]; 901.1Sriastrad x6 = q[1]; 911.1Sriastrad x7 = q[0]; 921.1Sriastrad 931.1Sriastrad /* 941.1Sriastrad * Top linear transformation. 951.1Sriastrad */ 961.1Sriastrad y14 = x3 ^ x5; 971.1Sriastrad y13 = x0 ^ x6; 981.1Sriastrad y9 = x0 ^ x3; 991.1Sriastrad y8 = x0 ^ x5; 1001.1Sriastrad t0 = x1 ^ x2; 1011.1Sriastrad y1 = t0 ^ x7; 1021.1Sriastrad y4 = y1 ^ x3; 1031.1Sriastrad y12 = y13 ^ y14; 1041.1Sriastrad y2 = y1 ^ x0; 1051.1Sriastrad y5 = y1 ^ x6; 1061.1Sriastrad y3 = y5 ^ y8; 1071.1Sriastrad t1 = x4 ^ y12; 1081.1Sriastrad y15 = t1 ^ x5; 1091.1Sriastrad y20 = t1 ^ x1; 1101.1Sriastrad y6 = y15 ^ x7; 1111.1Sriastrad y10 = y15 ^ t0; 1121.1Sriastrad y11 = y20 ^ y9; 1131.1Sriastrad y7 = x7 ^ y11; 1141.1Sriastrad y17 = y10 ^ y11; 1151.1Sriastrad y19 = y10 ^ y8; 1161.1Sriastrad y16 = t0 ^ y11; 1171.1Sriastrad y21 = y13 ^ y16; 1181.1Sriastrad y18 = x0 ^ y16; 1191.1Sriastrad 1201.1Sriastrad /* 1211.1Sriastrad * Non-linear section. 1221.1Sriastrad */ 1231.1Sriastrad t2 = y12 & y15; 1241.1Sriastrad t3 = y3 & y6; 1251.1Sriastrad t4 = t3 ^ t2; 1261.1Sriastrad t5 = y4 & x7; 1271.1Sriastrad t6 = t5 ^ t2; 1281.1Sriastrad t7 = y13 & y16; 1291.1Sriastrad t8 = y5 & y1; 1301.1Sriastrad t9 = t8 ^ t7; 1311.1Sriastrad t10 = y2 & y7; 1321.1Sriastrad t11 = t10 ^ t7; 1331.1Sriastrad t12 = y9 & y11; 1341.1Sriastrad t13 = y14 & y17; 1351.1Sriastrad t14 = t13 ^ t12; 1361.1Sriastrad t15 = y8 & y10; 1371.1Sriastrad t16 = t15 ^ t12; 1381.1Sriastrad t17 = t4 ^ t14; 1391.1Sriastrad t18 = t6 ^ t16; 1401.1Sriastrad t19 = t9 ^ t14; 1411.1Sriastrad t20 = t11 ^ t16; 1421.1Sriastrad t21 = t17 ^ y20; 1431.1Sriastrad t22 = t18 ^ y19; 1441.1Sriastrad t23 = t19 ^ y21; 1451.1Sriastrad t24 = t20 ^ y18; 1461.1Sriastrad 1471.1Sriastrad t25 = t21 ^ t22; 1481.1Sriastrad t26 = t21 & t23; 1491.1Sriastrad t27 = t24 ^ t26; 1501.1Sriastrad t28 = t25 & t27; 1511.1Sriastrad t29 = t28 ^ t22; 1521.1Sriastrad t30 = t23 ^ t24; 1531.1Sriastrad t31 = t22 ^ t26; 1541.1Sriastrad t32 = t31 & t30; 1551.1Sriastrad t33 = t32 ^ t24; 1561.1Sriastrad t34 = t23 ^ t33; 1571.1Sriastrad t35 = t27 ^ t33; 1581.1Sriastrad t36 = t24 & t35; 1591.1Sriastrad t37 = t36 ^ t34; 1601.1Sriastrad t38 = t27 ^ t36; 1611.1Sriastrad t39 = t29 & t38; 1621.1Sriastrad t40 = t25 ^ t39; 1631.1Sriastrad 1641.1Sriastrad t41 = t40 ^ t37; 1651.1Sriastrad t42 = t29 ^ t33; 1661.1Sriastrad t43 = t29 ^ t40; 1671.1Sriastrad t44 = t33 ^ t37; 1681.1Sriastrad t45 = t42 ^ t41; 1691.1Sriastrad z0 = t44 & y15; 1701.1Sriastrad z1 = t37 & y6; 1711.1Sriastrad z2 = t33 & x7; 1721.1Sriastrad z3 = t43 & y16; 1731.1Sriastrad z4 = t40 & y1; 1741.1Sriastrad z5 = t29 & y7; 1751.1Sriastrad z6 = t42 & y11; 1761.1Sriastrad z7 = t45 & y17; 1771.1Sriastrad z8 = t41 & y10; 1781.1Sriastrad z9 = t44 & y12; 1791.1Sriastrad z10 = t37 & y3; 1801.1Sriastrad z11 = t33 & y4; 1811.1Sriastrad z12 = t43 & y13; 1821.1Sriastrad z13 = t40 & y5; 1831.1Sriastrad z14 = t29 & y2; 1841.1Sriastrad z15 = t42 & y9; 1851.1Sriastrad z16 = t45 & y14; 1861.1Sriastrad z17 = t41 & y8; 1871.1Sriastrad 1881.1Sriastrad /* 1891.1Sriastrad * Bottom linear transformation. 1901.1Sriastrad */ 1911.1Sriastrad t46 = z15 ^ z16; 1921.1Sriastrad t47 = z10 ^ z11; 1931.1Sriastrad t48 = z5 ^ z13; 1941.1Sriastrad t49 = z9 ^ z10; 1951.1Sriastrad t50 = z2 ^ z12; 1961.1Sriastrad t51 = z2 ^ z5; 1971.1Sriastrad t52 = z7 ^ z8; 1981.1Sriastrad t53 = z0 ^ z3; 1991.1Sriastrad t54 = z6 ^ z7; 2001.1Sriastrad t55 = z16 ^ z17; 2011.1Sriastrad t56 = z12 ^ t48; 2021.1Sriastrad t57 = t50 ^ t53; 2031.1Sriastrad t58 = z4 ^ t46; 2041.1Sriastrad t59 = z3 ^ t54; 2051.1Sriastrad t60 = t46 ^ t57; 2061.1Sriastrad t61 = z14 ^ t57; 2071.1Sriastrad t62 = t52 ^ t58; 2081.1Sriastrad t63 = t49 ^ t58; 2091.1Sriastrad t64 = z4 ^ t59; 2101.1Sriastrad t65 = t61 ^ t62; 2111.1Sriastrad t66 = z1 ^ t63; 2121.1Sriastrad s0 = t59 ^ t63; 2131.1Sriastrad s6 = t56 ^ ~t62; 2141.1Sriastrad s7 = t48 ^ ~t60; 2151.1Sriastrad t67 = t64 ^ t65; 2161.1Sriastrad s3 = t53 ^ t66; 2171.1Sriastrad s4 = t51 ^ t66; 2181.1Sriastrad s5 = t47 ^ t65; 2191.1Sriastrad s1 = t64 ^ ~s3; 2201.1Sriastrad s2 = t55 ^ ~t67; 2211.1Sriastrad 2221.1Sriastrad q[7] = s0; 2231.1Sriastrad q[6] = s1; 2241.1Sriastrad q[5] = s2; 2251.1Sriastrad q[4] = s3; 2261.1Sriastrad q[3] = s4; 2271.1Sriastrad q[2] = s5; 2281.1Sriastrad q[1] = s6; 2291.1Sriastrad q[0] = s7; 2301.1Sriastrad} 2311.1Sriastrad 2321.1Sriastrad/* see inner.h */ 2331.1Sriastradvoid 2341.1Sriastradbr_aes_ct64_ortho(uint64_t q[static 8]) 2351.1Sriastrad{ 2361.1Sriastrad#define SWAPN(cl, ch, s, x, y) do { \ 2371.1Sriastrad uint64_t a, b; \ 2381.1Sriastrad a = (x); \ 2391.1Sriastrad b = (y); \ 2401.1Sriastrad (x) = (a & (uint64_t)cl) | ((b & (uint64_t)cl) << (s)); \ 2411.1Sriastrad (y) = ((a & (uint64_t)ch) >> (s)) | (b & (uint64_t)ch); \ 2421.1Sriastrad } while (0) 2431.1Sriastrad 2441.1Sriastrad#define SWAP2(x, y) SWAPN(0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 1, x, y) 2451.1Sriastrad#define SWAP4(x, y) SWAPN(0x3333333333333333, 0xCCCCCCCCCCCCCCCC, 2, x, y) 2461.1Sriastrad#define SWAP8(x, y) SWAPN(0x0F0F0F0F0F0F0F0F, 0xF0F0F0F0F0F0F0F0, 4, x, y) 2471.1Sriastrad 2481.1Sriastrad SWAP2(q[0], q[1]); 2491.1Sriastrad SWAP2(q[2], q[3]); 2501.1Sriastrad SWAP2(q[4], q[5]); 2511.1Sriastrad SWAP2(q[6], q[7]); 2521.1Sriastrad 2531.1Sriastrad SWAP4(q[0], q[2]); 2541.1Sriastrad SWAP4(q[1], q[3]); 2551.1Sriastrad SWAP4(q[4], q[6]); 2561.1Sriastrad SWAP4(q[5], q[7]); 2571.1Sriastrad 2581.1Sriastrad SWAP8(q[0], q[4]); 2591.1Sriastrad SWAP8(q[1], q[5]); 2601.1Sriastrad SWAP8(q[2], q[6]); 2611.1Sriastrad SWAP8(q[3], q[7]); 2621.1Sriastrad} 2631.1Sriastrad 2641.1Sriastrad/* see inner.h */ 2651.1Sriastradvoid 2661.1Sriastradbr_aes_ct64_interleave_in(uint64_t q0[static 1], uint64_t q1[static 1], 2671.1Sriastrad const uint32_t w[static 4]) 2681.1Sriastrad{ 2691.1Sriastrad uint64_t x0, x1, x2, x3; 2701.1Sriastrad 2711.1Sriastrad x0 = w[0]; 2721.1Sriastrad x1 = w[1]; 2731.1Sriastrad x2 = w[2]; 2741.1Sriastrad x3 = w[3]; 2751.1Sriastrad x0 |= (x0 << 16); 2761.1Sriastrad x1 |= (x1 << 16); 2771.1Sriastrad x2 |= (x2 << 16); 2781.1Sriastrad x3 |= (x3 << 16); 2791.1Sriastrad x0 &= (uint64_t)0x0000FFFF0000FFFF; 2801.1Sriastrad x1 &= (uint64_t)0x0000FFFF0000FFFF; 2811.1Sriastrad x2 &= (uint64_t)0x0000FFFF0000FFFF; 2821.1Sriastrad x3 &= (uint64_t)0x0000FFFF0000FFFF; 2831.1Sriastrad x0 |= (x0 << 8); 2841.1Sriastrad x1 |= (x1 << 8); 2851.1Sriastrad x2 |= (x2 << 8); 2861.1Sriastrad x3 |= (x3 << 8); 2871.1Sriastrad x0 &= (uint64_t)0x00FF00FF00FF00FF; 2881.1Sriastrad x1 &= (uint64_t)0x00FF00FF00FF00FF; 2891.1Sriastrad x2 &= (uint64_t)0x00FF00FF00FF00FF; 2901.1Sriastrad x3 &= (uint64_t)0x00FF00FF00FF00FF; 2911.1Sriastrad *q0 = x0 | (x2 << 8); 2921.1Sriastrad *q1 = x1 | (x3 << 8); 2931.1Sriastrad} 2941.1Sriastrad 2951.1Sriastrad/* see inner.h */ 2961.1Sriastradvoid 2971.1Sriastradbr_aes_ct64_interleave_out(uint32_t w[static 4], uint64_t q0, uint64_t q1) 2981.1Sriastrad{ 2991.1Sriastrad uint64_t x0, x1, x2, x3; 3001.1Sriastrad 3011.1Sriastrad x0 = q0 & (uint64_t)0x00FF00FF00FF00FF; 3021.1Sriastrad x1 = q1 & (uint64_t)0x00FF00FF00FF00FF; 3031.1Sriastrad x2 = (q0 >> 8) & (uint64_t)0x00FF00FF00FF00FF; 3041.1Sriastrad x3 = (q1 >> 8) & (uint64_t)0x00FF00FF00FF00FF; 3051.1Sriastrad x0 |= (x0 >> 8); 3061.1Sriastrad x1 |= (x1 >> 8); 3071.1Sriastrad x2 |= (x2 >> 8); 3081.1Sriastrad x3 |= (x3 >> 8); 3091.1Sriastrad x0 &= (uint64_t)0x0000FFFF0000FFFF; 3101.1Sriastrad x1 &= (uint64_t)0x0000FFFF0000FFFF; 3111.1Sriastrad x2 &= (uint64_t)0x0000FFFF0000FFFF; 3121.1Sriastrad x3 &= (uint64_t)0x0000FFFF0000FFFF; 3131.1Sriastrad w[0] = (uint32_t)x0 | (uint32_t)(x0 >> 16); 3141.1Sriastrad w[1] = (uint32_t)x1 | (uint32_t)(x1 >> 16); 3151.1Sriastrad w[2] = (uint32_t)x2 | (uint32_t)(x2 >> 16); 3161.1Sriastrad w[3] = (uint32_t)x3 | (uint32_t)(x3 >> 16); 3171.1Sriastrad} 3181.1Sriastrad 3191.1Sriastradstatic const unsigned char Rcon[] = { 3201.1Sriastrad 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36 3211.1Sriastrad}; 3221.1Sriastrad 3231.1Sriastradstatic uint32_t 3241.1Sriastradsub_word(uint32_t x) 3251.1Sriastrad{ 3261.1Sriastrad uint64_t q[8]; 3271.1Sriastrad 3281.1Sriastrad memset(q, 0, sizeof q); 3291.1Sriastrad q[0] = x; 3301.1Sriastrad br_aes_ct64_ortho(q); 3311.1Sriastrad br_aes_ct64_bitslice_Sbox(q); 3321.1Sriastrad br_aes_ct64_ortho(q); 3331.1Sriastrad return (uint32_t)q[0]; 3341.1Sriastrad} 3351.1Sriastrad 3361.1Sriastrad/* see inner.h */ 3371.1Sriastradunsigned 3381.1Sriastradbr_aes_ct64_keysched(uint64_t comp_skey[static 30], 3391.1Sriastrad const void *key, size_t key_len) 3401.1Sriastrad{ 3411.1Sriastrad unsigned num_rounds; 3421.1Sriastrad int i, j, k, nk, nkf; 3431.1Sriastrad uint32_t tmp; 3441.1Sriastrad uint32_t skey[60]; 3451.1Sriastrad 3461.1Sriastrad switch (key_len) { 3471.1Sriastrad case 16: 3481.1Sriastrad num_rounds = 10; 3491.1Sriastrad break; 3501.1Sriastrad case 24: 3511.1Sriastrad num_rounds = 12; 3521.1Sriastrad break; 3531.1Sriastrad case 32: 3541.1Sriastrad num_rounds = 14; 3551.1Sriastrad break; 3561.1Sriastrad default: 3571.1Sriastrad /* abort(); */ 3581.1Sriastrad return 0; 3591.1Sriastrad } 3601.1Sriastrad nk = (int)(key_len >> 2); 3611.1Sriastrad nkf = (int)((num_rounds + 1) << 2); 3621.1Sriastrad br_range_dec32le(skey, (key_len >> 2), key); 3631.1Sriastrad tmp = skey[(key_len >> 2) - 1]; 3641.1Sriastrad for (i = nk, j = 0, k = 0; i < nkf; i ++) { 3651.1Sriastrad if (j == 0) { 3661.1Sriastrad tmp = (tmp << 24) | (tmp >> 8); 3671.1Sriastrad tmp = sub_word(tmp) ^ Rcon[k]; 3681.1Sriastrad } else if (nk > 6 && j == 4) { 3691.1Sriastrad tmp = sub_word(tmp); 3701.1Sriastrad } 3711.1Sriastrad tmp ^= skey[i - nk]; 3721.1Sriastrad skey[i] = tmp; 3731.1Sriastrad if (++ j == nk) { 3741.1Sriastrad j = 0; 3751.1Sriastrad k ++; 3761.1Sriastrad } 3771.1Sriastrad } 3781.1Sriastrad 3791.1Sriastrad for (i = 0, j = 0; i < nkf; i += 4, j += 2) { 3801.1Sriastrad uint64_t q[8]; 3811.1Sriastrad 3821.1Sriastrad br_aes_ct64_interleave_in(&q[0], &q[4], skey + i); 3831.1Sriastrad q[1] = q[0]; 3841.1Sriastrad q[2] = q[0]; 3851.1Sriastrad q[3] = q[0]; 3861.1Sriastrad q[5] = q[4]; 3871.1Sriastrad q[6] = q[4]; 3881.1Sriastrad q[7] = q[4]; 3891.1Sriastrad br_aes_ct64_ortho(q); 3901.1Sriastrad comp_skey[j + 0] = 3911.1Sriastrad (q[0] & (uint64_t)0x1111111111111111) 3921.1Sriastrad | (q[1] & (uint64_t)0x2222222222222222) 3931.1Sriastrad | (q[2] & (uint64_t)0x4444444444444444) 3941.1Sriastrad | (q[3] & (uint64_t)0x8888888888888888); 3951.1Sriastrad comp_skey[j + 1] = 3961.1Sriastrad (q[4] & (uint64_t)0x1111111111111111) 3971.1Sriastrad | (q[5] & (uint64_t)0x2222222222222222) 3981.1Sriastrad | (q[6] & (uint64_t)0x4444444444444444) 3991.1Sriastrad | (q[7] & (uint64_t)0x8888888888888888); 4001.1Sriastrad } 4011.1Sriastrad return num_rounds; 4021.1Sriastrad} 4031.1Sriastrad 4041.1Sriastrad/* see inner.h */ 4051.1Sriastradvoid 4061.1Sriastradbr_aes_ct64_skey_expand(uint64_t skey[static 120], 4071.1Sriastrad unsigned num_rounds, const uint64_t comp_skey[static 30]) 4081.1Sriastrad{ 4091.1Sriastrad unsigned u, v, n; 4101.1Sriastrad 4111.1Sriastrad n = (num_rounds + 1) << 1; 4121.1Sriastrad for (u = 0, v = 0; u < n; u ++, v += 4) { 4131.1Sriastrad uint64_t x0, x1, x2, x3; 4141.1Sriastrad 4151.1Sriastrad x0 = x1 = x2 = x3 = comp_skey[u]; 4161.1Sriastrad x0 &= (uint64_t)0x1111111111111111; 4171.1Sriastrad x1 &= (uint64_t)0x2222222222222222; 4181.1Sriastrad x2 &= (uint64_t)0x4444444444444444; 4191.1Sriastrad x3 &= (uint64_t)0x8888888888888888; 4201.1Sriastrad x1 >>= 1; 4211.1Sriastrad x2 >>= 2; 4221.1Sriastrad x3 >>= 3; 4231.1Sriastrad skey[v + 0] = (x0 << 4) - x0; 4241.1Sriastrad skey[v + 1] = (x1 << 4) - x1; 4251.1Sriastrad skey[v + 2] = (x2 << 4) - x2; 4261.1Sriastrad skey[v + 3] = (x3 << 4) - x3; 4271.1Sriastrad } 4281.1Sriastrad} 4291.1Sriastrad 4301.1Sriastrad/* NetBSD additions, for computing the standard AES key schedule */ 4311.1Sriastrad 4321.1Sriastradunsigned 4331.1Sriastradbr_aes_ct64_keysched_stdenc(uint32_t *skey, const void *key, size_t key_len) 4341.1Sriastrad{ 4351.1Sriastrad unsigned num_rounds; 4361.1Sriastrad int i, j, k, nk, nkf; 4371.1Sriastrad uint32_t tmp; 4381.1Sriastrad 4391.1Sriastrad switch (key_len) { 4401.1Sriastrad case 16: 4411.1Sriastrad num_rounds = 10; 4421.1Sriastrad break; 4431.1Sriastrad case 24: 4441.1Sriastrad num_rounds = 12; 4451.1Sriastrad break; 4461.1Sriastrad case 32: 4471.1Sriastrad num_rounds = 14; 4481.1Sriastrad break; 4491.1Sriastrad default: 4501.1Sriastrad /* abort(); */ 4511.1Sriastrad return 0; 4521.1Sriastrad } 4531.1Sriastrad nk = (int)(key_len >> 2); 4541.1Sriastrad nkf = (int)((num_rounds + 1) << 2); 4551.1Sriastrad tmp = 0; 4561.1Sriastrad for (i = 0; i < nk; i ++) { 4571.1Sriastrad tmp = br_dec32le((const unsigned char *)key + (i << 2)); 4581.1Sriastrad skey[i] = tmp; 4591.1Sriastrad } 4601.1Sriastrad for (i = nk, j = 0, k = 0; i < nkf; i ++) { 4611.1Sriastrad if (j == 0) { 4621.1Sriastrad tmp = (tmp << 24) | (tmp >> 8); 4631.1Sriastrad tmp = sub_word(tmp) ^ Rcon[k]; 4641.1Sriastrad } else if (nk > 6 && j == 4) { 4651.1Sriastrad tmp = sub_word(tmp); 4661.1Sriastrad } 4671.1Sriastrad tmp ^= skey[i - nk]; 4681.1Sriastrad skey[i] = tmp; 4691.1Sriastrad if (++ j == nk) { 4701.1Sriastrad j = 0; 4711.1Sriastrad k ++; 4721.1Sriastrad } 4731.1Sriastrad } 4741.1Sriastrad return num_rounds; 4751.1Sriastrad} 4761.1Sriastrad 4771.1Sriastradunsigned 4781.1Sriastradbr_aes_ct64_keysched_stddec(uint32_t *skey, const void *key, size_t key_len) 4791.1Sriastrad{ 4801.1Sriastrad uint32_t tkey[60]; 4811.1Sriastrad uint64_t q[8]; 4821.1Sriastrad unsigned num_rounds; 4831.1Sriastrad unsigned i; 4841.1Sriastrad 4851.1Sriastrad num_rounds = br_aes_ct64_keysched_stdenc(skey, key, key_len); 4861.1Sriastrad if (num_rounds == 0) 4871.1Sriastrad return 0; 4881.1Sriastrad 4891.1Sriastrad q[1] = q[2] = q[3] = 0; 4901.1Sriastrad q[5] = q[6] = q[7] = 0; 4911.1Sriastrad 4921.1Sriastrad tkey[0] = skey[4*num_rounds + 0]; 4931.1Sriastrad tkey[1] = skey[4*num_rounds + 1]; 4941.1Sriastrad tkey[2] = skey[4*num_rounds + 2]; 4951.1Sriastrad tkey[3] = skey[4*num_rounds + 3]; 4961.1Sriastrad for (i = 1; i < num_rounds; i++) { 4971.1Sriastrad br_aes_ct64_interleave_in(&q[0], &q[4], skey + 4*i); 4981.1Sriastrad br_aes_ct64_ortho(q); 4991.1Sriastrad br_aes_ct64_inv_mix_columns(q); 5001.1Sriastrad br_aes_ct64_ortho(q); 5011.1Sriastrad br_aes_ct64_interleave_out(&tkey[4*(num_rounds - i)], 5021.1Sriastrad q[0], q[4]); 5031.1Sriastrad } 5041.1Sriastrad tkey[4*num_rounds + 0] = skey[0]; 5051.1Sriastrad tkey[4*num_rounds + 1] = skey[1]; 5061.1Sriastrad tkey[4*num_rounds + 2] = skey[2]; 5071.1Sriastrad tkey[4*num_rounds + 3] = skey[3]; 5081.1Sriastrad 5091.1Sriastrad memcpy(skey, tkey, 4*(num_rounds + 1)*sizeof(uint32_t)); 5101.1Sriastrad explicit_memset(tkey, 0, 4*(num_rounds + 1)*sizeof(uint32_t)); 5111.1Sriastrad return num_rounds; 5121.1Sriastrad} 513