2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
30 add_round_key(unsigned *state, const uint32_t *skeys)
34 for (i = 0; i < 16; i += 4) {
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;
46 sub_bytes(unsigned *state)
50 for (i = 0; i < 16; i ++) {
51 state[i] = S[state[i]];
56 shift_rows(unsigned *state)
74 state[15] = state[11];
81 mix_columns(unsigned *state)
85 for (i = 0; i < 16; i += 4) {
86 unsigned s0, s1, s2, s3;
87 unsigned t0, t1, t2, t3;
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);
106 br_aes_small_encrypt(unsigned num_rounds, const uint32_t *skey, void *data)
113 for (u = 0; u < 16; u ++) {
116 add_round_key(state, skey);
117 for (u = 1; u < num_rounds; u ++) {
121 add_round_key(state, skey + (u << 2));
125 add_round_key(state, skey + (num_rounds << 2));
126 for (u = 0; u < 16; u ++) {