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