]> git.cryptolib.org Git - arm-crypto-lib.git/blob - echo/aes_enc_round.c
fixing sha256
[arm-crypto-lib.git] / echo / aes_enc_round.c
1 /* aes-round.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19
20 #include <stdint.h>
21 #include "aes_enc_round.h"
22 #include "gf256mul.h"
23 #include "aes_sbox.h"
24
25 static
26 void aes_shiftcol(void* data, uint8_t shift){
27         uint8_t tmp[4];
28         tmp[0] = ((uint8_t*)data)[ 0];
29         tmp[1] = ((uint8_t*)data)[ 4];
30         tmp[2] = ((uint8_t*)data)[ 8];
31         tmp[3] = ((uint8_t*)data)[12];
32         ((uint8_t*)data)[ 0] = tmp[(shift+0)&3];
33         ((uint8_t*)data)[ 4] = tmp[(shift+1)&3];
34         ((uint8_t*)data)[ 8] = tmp[(shift+2)&3];
35         ((uint8_t*)data)[12] = tmp[(shift+3)&3];
36 }
37
38 #define GF256MUL_1(a) (a)
39 #define GF256MUL_2(a) (gf256mul(2, (a), 0x1b))
40 #define GF256MUL_3(a) (gf256mul(3, (a), 0x1b))
41
42 void aes_enc_round(aes_cipher_state_t* state, const aes_roundkey_t* k){
43         uint8_t tmp[16], t;
44         uint8_t i;
45         /* subBytes */
46         for(i=0; i<16; ++i){
47                 tmp[i] = aes_sbox[state->s[i]];
48         }
49         /* shiftRows */
50         aes_shiftcol(tmp+1, 1);
51         aes_shiftcol(tmp+2, 2);
52         aes_shiftcol(tmp+3, 3);
53         /* mixColums */
54         for(i=0; i<4; ++i){
55                 t = tmp[4*i+0] ^ tmp[4*i+1] ^ tmp[4*i+2] ^ tmp[4*i+3];
56                 state->s[4*i+0] =
57                           GF256MUL_2(tmp[4*i+0]^tmp[4*i+1])
58                         ^ tmp[4*i+0]
59                         ^ t;
60                 state->s[4*i+1] =
61                           GF256MUL_2(tmp[4*i+1]^tmp[4*i+2])
62                         ^ tmp[4*i+1]
63                         ^ t;
64                 state->s[4*i+2] =
65                           GF256MUL_2(tmp[4*i+2]^tmp[4*i+3])
66                         ^ tmp[4*i+2]
67                         ^ t;
68                 state->s[4*i+3] =
69                           GF256MUL_2(tmp[4*i+3]^tmp[4*i+0])
70                         ^ tmp[4*i+3]
71                         ^ t;
72         }
73
74         /* addKey */
75         for(i=0; i<16; ++i){
76                 state->s[i] ^= k->ks[i];
77         }
78 }