Home | History | Annotate | Line # | Download | only in aes
      1  1.1  riastrad /*	$NetBSD: aes_ct_enc.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_enc.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 static inline void
     35  1.1  riastrad add_round_key(uint32_t *q, const uint32_t *sk)
     36  1.1  riastrad {
     37  1.1  riastrad 	q[0] ^= sk[0];
     38  1.1  riastrad 	q[1] ^= sk[1];
     39  1.1  riastrad 	q[2] ^= sk[2];
     40  1.1  riastrad 	q[3] ^= sk[3];
     41  1.1  riastrad 	q[4] ^= sk[4];
     42  1.1  riastrad 	q[5] ^= sk[5];
     43  1.1  riastrad 	q[6] ^= sk[6];
     44  1.1  riastrad 	q[7] ^= sk[7];
     45  1.1  riastrad }
     46  1.1  riastrad 
     47  1.1  riastrad static inline void
     48  1.1  riastrad shift_rows(uint32_t *q)
     49  1.1  riastrad {
     50  1.1  riastrad 	int i;
     51  1.1  riastrad 
     52  1.1  riastrad 	for (i = 0; i < 8; i ++) {
     53  1.1  riastrad 		uint32_t x;
     54  1.1  riastrad 
     55  1.1  riastrad 		x = q[i];
     56  1.1  riastrad 		q[i] = (x & 0x000000FF)
     57  1.1  riastrad 			| ((x & 0x0000FC00) >> 2) | ((x & 0x00000300) << 6)
     58  1.1  riastrad 			| ((x & 0x00F00000) >> 4) | ((x & 0x000F0000) << 4)
     59  1.1  riastrad 			| ((x & 0xC0000000) >> 6) | ((x & 0x3F000000) << 2);
     60  1.1  riastrad 	}
     61  1.1  riastrad }
     62  1.1  riastrad 
     63  1.1  riastrad static inline uint32_t
     64  1.1  riastrad rotr16(uint32_t x)
     65  1.1  riastrad {
     66  1.1  riastrad 	return (x << 16) | (x >> 16);
     67  1.1  riastrad }
     68  1.1  riastrad 
     69  1.1  riastrad static inline void
     70  1.1  riastrad mix_columns(uint32_t *q)
     71  1.1  riastrad {
     72  1.1  riastrad 	uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
     73  1.1  riastrad 	uint32_t r0, r1, r2, r3, r4, r5, r6, r7;
     74  1.1  riastrad 
     75  1.1  riastrad 	q0 = q[0];
     76  1.1  riastrad 	q1 = q[1];
     77  1.1  riastrad 	q2 = q[2];
     78  1.1  riastrad 	q3 = q[3];
     79  1.1  riastrad 	q4 = q[4];
     80  1.1  riastrad 	q5 = q[5];
     81  1.1  riastrad 	q6 = q[6];
     82  1.1  riastrad 	q7 = q[7];
     83  1.1  riastrad 	r0 = (q0 >> 8) | (q0 << 24);
     84  1.1  riastrad 	r1 = (q1 >> 8) | (q1 << 24);
     85  1.1  riastrad 	r2 = (q2 >> 8) | (q2 << 24);
     86  1.1  riastrad 	r3 = (q3 >> 8) | (q3 << 24);
     87  1.1  riastrad 	r4 = (q4 >> 8) | (q4 << 24);
     88  1.1  riastrad 	r5 = (q5 >> 8) | (q5 << 24);
     89  1.1  riastrad 	r6 = (q6 >> 8) | (q6 << 24);
     90  1.1  riastrad 	r7 = (q7 >> 8) | (q7 << 24);
     91  1.1  riastrad 
     92  1.1  riastrad 	q[0] = q7 ^ r7 ^ r0 ^ rotr16(q0 ^ r0);
     93  1.1  riastrad 	q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr16(q1 ^ r1);
     94  1.1  riastrad 	q[2] = q1 ^ r1 ^ r2 ^ rotr16(q2 ^ r2);
     95  1.1  riastrad 	q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr16(q3 ^ r3);
     96  1.1  riastrad 	q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr16(q4 ^ r4);
     97  1.1  riastrad 	q[5] = q4 ^ r4 ^ r5 ^ rotr16(q5 ^ r5);
     98  1.1  riastrad 	q[6] = q5 ^ r5 ^ r6 ^ rotr16(q6 ^ r6);
     99  1.1  riastrad 	q[7] = q6 ^ r6 ^ r7 ^ rotr16(q7 ^ r7);
    100  1.1  riastrad }
    101  1.1  riastrad 
    102  1.1  riastrad /* see inner.h */
    103  1.1  riastrad void
    104  1.1  riastrad br_aes_ct_bitslice_encrypt(unsigned num_rounds,
    105  1.1  riastrad 	const uint32_t *skey, uint32_t *q)
    106  1.1  riastrad {
    107  1.1  riastrad 	unsigned u;
    108  1.1  riastrad 
    109  1.1  riastrad 	add_round_key(q, skey);
    110  1.1  riastrad 	for (u = 1; u < num_rounds; u ++) {
    111  1.1  riastrad 		br_aes_ct_bitslice_Sbox(q);
    112  1.1  riastrad 		shift_rows(q);
    113  1.1  riastrad 		mix_columns(q);
    114  1.1  riastrad 		add_round_key(q, skey + (u << 3));
    115  1.1  riastrad 	}
    116  1.1  riastrad 	br_aes_ct_bitslice_Sbox(q);
    117  1.1  riastrad 	shift_rows(q);
    118  1.1  riastrad 	add_round_key(q, skey + (num_rounds << 3));
    119  1.1  riastrad }
    120