Home | History | Annotate | Line # | Download | only in aes
aes_ct.c revision 1.1
      1 /*	$NetBSD: aes_ct.c,v 1.1 2020/06/29 23:27:52 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2016 Thomas Pornin <pornin (at) 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.1 2020/06/29 23:27:52 riastradh Exp $");
     29 
     30 #include <sys/types.h>
     31 
     32 #include <crypto/aes/aes_bear.h>
     33 
     34 /* see inner.h */
     35 void
     36 br_aes_ct_bitslice_Sbox(uint32_t *q)
     37 {
     38 	/*
     39 	 * This S-box implementation is a straightforward translation of
     40 	 * the circuit described by Boyar and Peralta in "A new
     41 	 * combinational logic minimization technique with applications
     42 	 * to cryptology" (https://eprint.iacr.org/2009/191.pdf).
     43 	 *
     44 	 * Note that variables x* (input) and s* (output) are numbered
     45 	 * in "reverse" order (x0 is the high bit, x7 is the low bit).
     46 	 */
     47 
     48 	uint32_t x0, x1, x2, x3, x4, x5, x6, x7;
     49 	uint32_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
     50 	uint32_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
     51 	uint32_t y20, y21;
     52 	uint32_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
     53 	uint32_t z10, z11, z12, z13, z14, z15, z16, z17;
     54 	uint32_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
     55 	uint32_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
     56 	uint32_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
     57 	uint32_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
     58 	uint32_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
     59 	uint32_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
     60 	uint32_t t60, t61, t62, t63, t64, t65, t66, t67;
     61 	uint32_t s0, s1, s2, s3, s4, s5, s6, s7;
     62 
     63 	x0 = q[7];
     64 	x1 = q[6];
     65 	x2 = q[5];
     66 	x3 = q[4];
     67 	x4 = q[3];
     68 	x5 = q[2];
     69 	x6 = q[1];
     70 	x7 = q[0];
     71 
     72 	/*
     73 	 * Top linear transformation.
     74 	 */
     75 	y14 = x3 ^ x5;
     76 	y13 = x0 ^ x6;
     77 	y9 = x0 ^ x3;
     78 	y8 = x0 ^ x5;
     79 	t0 = x1 ^ x2;
     80 	y1 = t0 ^ x7;
     81 	y4 = y1 ^ x3;
     82 	y12 = y13 ^ y14;
     83 	y2 = y1 ^ x0;
     84 	y5 = y1 ^ x6;
     85 	y3 = y5 ^ y8;
     86 	t1 = x4 ^ y12;
     87 	y15 = t1 ^ x5;
     88 	y20 = t1 ^ x1;
     89 	y6 = y15 ^ x7;
     90 	y10 = y15 ^ t0;
     91 	y11 = y20 ^ y9;
     92 	y7 = x7 ^ y11;
     93 	y17 = y10 ^ y11;
     94 	y19 = y10 ^ y8;
     95 	y16 = t0 ^ y11;
     96 	y21 = y13 ^ y16;
     97 	y18 = x0 ^ y16;
     98 
     99 	/*
    100 	 * Non-linear section.
    101 	 */
    102 	t2 = y12 & y15;
    103 	t3 = y3 & y6;
    104 	t4 = t3 ^ t2;
    105 	t5 = y4 & x7;
    106 	t6 = t5 ^ t2;
    107 	t7 = y13 & y16;
    108 	t8 = y5 & y1;
    109 	t9 = t8 ^ t7;
    110 	t10 = y2 & y7;
    111 	t11 = t10 ^ t7;
    112 	t12 = y9 & y11;
    113 	t13 = y14 & y17;
    114 	t14 = t13 ^ t12;
    115 	t15 = y8 & y10;
    116 	t16 = t15 ^ t12;
    117 	t17 = t4 ^ t14;
    118 	t18 = t6 ^ t16;
    119 	t19 = t9 ^ t14;
    120 	t20 = t11 ^ t16;
    121 	t21 = t17 ^ y20;
    122 	t22 = t18 ^ y19;
    123 	t23 = t19 ^ y21;
    124 	t24 = t20 ^ y18;
    125 
    126 	t25 = t21 ^ t22;
    127 	t26 = t21 & t23;
    128 	t27 = t24 ^ t26;
    129 	t28 = t25 & t27;
    130 	t29 = t28 ^ t22;
    131 	t30 = t23 ^ t24;
    132 	t31 = t22 ^ t26;
    133 	t32 = t31 & t30;
    134 	t33 = t32 ^ t24;
    135 	t34 = t23 ^ t33;
    136 	t35 = t27 ^ t33;
    137 	t36 = t24 & t35;
    138 	t37 = t36 ^ t34;
    139 	t38 = t27 ^ t36;
    140 	t39 = t29 & t38;
    141 	t40 = t25 ^ t39;
    142 
    143 	t41 = t40 ^ t37;
    144 	t42 = t29 ^ t33;
    145 	t43 = t29 ^ t40;
    146 	t44 = t33 ^ t37;
    147 	t45 = t42 ^ t41;
    148 	z0 = t44 & y15;
    149 	z1 = t37 & y6;
    150 	z2 = t33 & x7;
    151 	z3 = t43 & y16;
    152 	z4 = t40 & y1;
    153 	z5 = t29 & y7;
    154 	z6 = t42 & y11;
    155 	z7 = t45 & y17;
    156 	z8 = t41 & y10;
    157 	z9 = t44 & y12;
    158 	z10 = t37 & y3;
    159 	z11 = t33 & y4;
    160 	z12 = t43 & y13;
    161 	z13 = t40 & y5;
    162 	z14 = t29 & y2;
    163 	z15 = t42 & y9;
    164 	z16 = t45 & y14;
    165 	z17 = t41 & y8;
    166 
    167 	/*
    168 	 * Bottom linear transformation.
    169 	 */
    170 	t46 = z15 ^ z16;
    171 	t47 = z10 ^ z11;
    172 	t48 = z5 ^ z13;
    173 	t49 = z9 ^ z10;
    174 	t50 = z2 ^ z12;
    175 	t51 = z2 ^ z5;
    176 	t52 = z7 ^ z8;
    177 	t53 = z0 ^ z3;
    178 	t54 = z6 ^ z7;
    179 	t55 = z16 ^ z17;
    180 	t56 = z12 ^ t48;
    181 	t57 = t50 ^ t53;
    182 	t58 = z4 ^ t46;
    183 	t59 = z3 ^ t54;
    184 	t60 = t46 ^ t57;
    185 	t61 = z14 ^ t57;
    186 	t62 = t52 ^ t58;
    187 	t63 = t49 ^ t58;
    188 	t64 = z4 ^ t59;
    189 	t65 = t61 ^ t62;
    190 	t66 = z1 ^ t63;
    191 	s0 = t59 ^ t63;
    192 	s6 = t56 ^ ~t62;
    193 	s7 = t48 ^ ~t60;
    194 	t67 = t64 ^ t65;
    195 	s3 = t53 ^ t66;
    196 	s4 = t51 ^ t66;
    197 	s5 = t47 ^ t65;
    198 	s1 = t64 ^ ~s3;
    199 	s2 = t55 ^ ~t67;
    200 
    201 	q[7] = s0;
    202 	q[6] = s1;
    203 	q[5] = s2;
    204 	q[4] = s3;
    205 	q[3] = s4;
    206 	q[2] = s5;
    207 	q[1] = s6;
    208 	q[0] = s7;
    209 }
    210 
    211 /* see inner.h */
    212 void
    213 br_aes_ct_ortho(uint32_t *q)
    214 {
    215 #define SWAPN(cl, ch, s, x, y)   do { \
    216 		uint32_t a, b; \
    217 		a = (x); \
    218 		b = (y); \
    219 		(x) = (a & (uint32_t)cl) | ((b & (uint32_t)cl) << (s)); \
    220 		(y) = ((a & (uint32_t)ch) >> (s)) | (b & (uint32_t)ch); \
    221 	} while (0)
    222 
    223 #define SWAP2(x, y)   SWAPN(0x55555555, 0xAAAAAAAA, 1, x, y)
    224 #define SWAP4(x, y)   SWAPN(0x33333333, 0xCCCCCCCC, 2, x, y)
    225 #define SWAP8(x, y)   SWAPN(0x0F0F0F0F, 0xF0F0F0F0, 4, x, y)
    226 
    227 	SWAP2(q[0], q[1]);
    228 	SWAP2(q[2], q[3]);
    229 	SWAP2(q[4], q[5]);
    230 	SWAP2(q[6], q[7]);
    231 
    232 	SWAP4(q[0], q[2]);
    233 	SWAP4(q[1], q[3]);
    234 	SWAP4(q[4], q[6]);
    235 	SWAP4(q[5], q[7]);
    236 
    237 	SWAP8(q[0], q[4]);
    238 	SWAP8(q[1], q[5]);
    239 	SWAP8(q[2], q[6]);
    240 	SWAP8(q[3], q[7]);
    241 }
    242 
    243 static const unsigned char Rcon[] = {
    244 	0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
    245 };
    246 
    247 static uint32_t
    248 sub_word(uint32_t x)
    249 {
    250 	uint32_t q[8];
    251 	int i;
    252 
    253 	for (i = 0; i < 8; i ++) {
    254 		q[i] = x;
    255 	}
    256 	br_aes_ct_ortho(q);
    257 	br_aes_ct_bitslice_Sbox(q);
    258 	br_aes_ct_ortho(q);
    259 	return q[0];
    260 }
    261 
    262 /* see inner.h */
    263 unsigned
    264 br_aes_ct_keysched(uint32_t *comp_skey, const void *key, size_t key_len)
    265 {
    266 	unsigned num_rounds;
    267 	int i, j, k, nk, nkf;
    268 	uint32_t tmp;
    269 	uint32_t skey[120];
    270 
    271 	switch (key_len) {
    272 	case 16:
    273 		num_rounds = 10;
    274 		break;
    275 	case 24:
    276 		num_rounds = 12;
    277 		break;
    278 	case 32:
    279 		num_rounds = 14;
    280 		break;
    281 	default:
    282 		/* abort(); */
    283 		return 0;
    284 	}
    285 	nk = (int)(key_len >> 2);
    286 	nkf = (int)((num_rounds + 1) << 2);
    287 	tmp = 0;
    288 	for (i = 0; i < nk; i ++) {
    289 		tmp = br_dec32le((const unsigned char *)key + (i << 2));
    290 		skey[(i << 1) + 0] = tmp;
    291 		skey[(i << 1) + 1] = tmp;
    292 	}
    293 	for (i = nk, j = 0, k = 0; i < nkf; i ++) {
    294 		if (j == 0) {
    295 			tmp = (tmp << 24) | (tmp >> 8);
    296 			tmp = sub_word(tmp) ^ Rcon[k];
    297 		} else if (nk > 6 && j == 4) {
    298 			tmp = sub_word(tmp);
    299 		}
    300 		tmp ^= skey[(i - nk) << 1];
    301 		skey[(i << 1) + 0] = tmp;
    302 		skey[(i << 1) + 1] = tmp;
    303 		if (++ j == nk) {
    304 			j = 0;
    305 			k ++;
    306 		}
    307 	}
    308 	for (i = 0; i < nkf; i += 4) {
    309 		br_aes_ct_ortho(skey + (i << 1));
    310 	}
    311 	for (i = 0, j = 0; i < nkf; i ++, j += 2) {
    312 		comp_skey[i] = (skey[j + 0] & 0x55555555)
    313 			| (skey[j + 1] & 0xAAAAAAAA);
    314 	}
    315 	return num_rounds;
    316 }
    317 
    318 /* see inner.h */
    319 void
    320 br_aes_ct_skey_expand(uint32_t *skey,
    321 	unsigned num_rounds, const uint32_t *comp_skey)
    322 {
    323 	unsigned u, v, n;
    324 
    325 	n = (num_rounds + 1) << 2;
    326 	for (u = 0, v = 0; u < n; u ++, v += 2) {
    327 		uint32_t x, y;
    328 
    329 		x = y = comp_skey[u];
    330 		x &= 0x55555555;
    331 		skey[v + 0] = x | (x << 1);
    332 		y &= 0xAAAAAAAA;
    333 		skey[v + 1] = y | (y >> 1);
    334 	}
    335 }
    336