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