1 /*
2  * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "inner.h"
26 
27 #define S   br_aes_S
28 
29 static void
30 add_round_key(unsigned *state, const uint32_t *skeys)
31 {
32 	int i;
33 
34 	for (i = 0; i < 16; i += 4) {
35 		uint32_t k;
36 
37 		k = *skeys ++;
38 		state[i + 0] ^= (unsigned)(k >> 24);
39 		state[i + 1] ^= (unsigned)(k >> 16) & 0xFF;
40 		state[i + 2] ^= (unsigned)(k >> 8) & 0xFF;
41 		state[i + 3] ^= (unsigned)k & 0xFF;
42 	}
43 }
44 
45 static void
46 sub_bytes(unsigned *state)
47 {
48 	int i;
49 
50 	for (i = 0; i < 16; i ++) {
51 		state[i] = S[state[i]];
52 	}
53 }
54 
55 static void
56 shift_rows(unsigned *state)
57 {
58 	unsigned tmp;
59 
60 	tmp = state[1];
61 	state[1] = state[5];
62 	state[5] = state[9];
63 	state[9] = state[13];
64 	state[13] = tmp;
65 
66 	tmp = state[2];
67 	state[2] = state[10];
68 	state[10] = tmp;
69 	tmp = state[6];
70 	state[6] = state[14];
71 	state[14] = tmp;
72 
73 	tmp = state[15];
74 	state[15] = state[11];
75 	state[11] = state[7];
76 	state[7] = state[3];
77 	state[3] = tmp;
78 }
79 
80 static void
81 mix_columns(unsigned *state)
82 {
83 	int i;
84 
85 	for (i = 0; i < 16; i += 4) {
86 		unsigned s0, s1, s2, s3;
87 		unsigned t0, t1, t2, t3;
88 
89 		s0 = state[i + 0];
90 		s1 = state[i + 1];
91 		s2 = state[i + 2];
92 		s3 = state[i + 3];
93 		t0 = (s0 << 1) ^ s1 ^ (s1 << 1) ^ s2 ^ s3;
94 		t1 = s0 ^ (s1 << 1) ^ s2 ^ (s2 << 1) ^ s3;
95 		t2 = s0 ^ s1 ^ (s2 << 1) ^ s3 ^ (s3 << 1);
96 		t3 = s0 ^ (s0 << 1) ^ s1 ^ s2 ^ (s3 << 1);
97 		state[i + 0] = t0 ^ ((unsigned)(-(int)(t0 >> 8)) & 0x11B);
98 		state[i + 1] = t1 ^ ((unsigned)(-(int)(t1 >> 8)) & 0x11B);
99 		state[i + 2] = t2 ^ ((unsigned)(-(int)(t2 >> 8)) & 0x11B);
100 		state[i + 3] = t3 ^ ((unsigned)(-(int)(t3 >> 8)) & 0x11B);
101 	}
102 }
103 
104 /* see inner.h */
105 void
106 br_aes_small_encrypt(unsigned num_rounds, const uint32_t *skey, void *data)
107 {
108 	unsigned char *buf;
109 	unsigned state[16];
110 	unsigned u;
111 
112 	buf = data;
113 	for (u = 0; u < 16; u ++) {
114 		state[u] = buf[u];
115 	}
116 	add_round_key(state, skey);
117 	for (u = 1; u < num_rounds; u ++) {
118 		sub_bytes(state);
119 		shift_rows(state);
120 		mix_columns(state);
121 		add_round_key(state, skey + (u << 2));
122 	}
123 	sub_bytes(state);
124 	shift_rows(state);
125 	add_round_key(state, skey + (num_rounds << 2));
126 	for (u = 0; u < 16; u ++) {
127 		buf[u] = state[u];
128 	}
129 }
130