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