3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
34 #include <avr/pgmspace.h>
36 void aes_shiftcol(void *data, uint8_t shift){
38 tmp[0] = ((uint8_t*)data)[ 0];
39 tmp[1] = ((uint8_t*)data)[ 4];
40 tmp[2] = ((uint8_t*)data)[ 8];
41 tmp[3] = ((uint8_t*)data)[12];
42 ((uint8_t*)data)[ 0] = tmp[(shift+0)&3];
43 ((uint8_t*)data)[ 4] = tmp[(shift+1)&3];
44 ((uint8_t*)data)[ 8] = tmp[(shift+2)&3];
45 ((uint8_t*)data)[12] = tmp[(shift+3)&3];
48 #define GF256MUL_1(a) (a)
49 #define GF256MUL_2(a) (gf256mul(2, (a), 0x1b))
50 #define GF256MUL_3(a) (gf256mul(3, (a), 0x1b))
53 void aes_enc_round(aes_cipher_state_t *state, const aes_roundkey_t *k){
58 tmp[i] = pgm_read_byte(aes_sbox+state->s[i]);
61 aes_shiftcol(tmp+1, 1);
62 aes_shiftcol(tmp+2, 2);
63 aes_shiftcol(tmp+3, 3);
66 t = tmp[4*i+0] ^ tmp[4*i+1] ^ tmp[4*i+2] ^ tmp[4*i+3];
68 GF256MUL_2(tmp[4*i+0]^tmp[4*i+1])
72 GF256MUL_2(tmp[4*i+1]^tmp[4*i+2])
76 GF256MUL_2(tmp[4*i+2]^tmp[4*i+3])
80 GF256MUL_2(tmp[4*i+3]^tmp[4*i+0])
87 state->s[i] ^= k->ks[i];
93 void aes_enc_lastround(aes_cipher_state_t *state,const aes_roundkey_t *k){
97 state->s[i] = pgm_read_byte(aes_sbox+state->s[i]);
100 aes_shiftcol(state->s+1, 1);
101 aes_shiftcol(state->s+2, 2);
102 aes_shiftcol(state->s+3, 3);
105 state->s[i] ^= k->ks[i];
109 void aes_encrypt_core(aes_cipher_state_t *state, const aes_genctx_t *ks, uint8_t rounds){
112 state->s[i] ^= ks->key[0].ks[i];
115 for(;rounds>1;--rounds){
116 aes_enc_round(state, &(ks->key[i]));
119 aes_enc_lastround(state, &(ks->key[i]));