]> git.cryptolib.org Git - arm-crypto-lib.git/blob - aes/aes_enc.c
AES added
[arm-crypto-lib.git] / aes / aes_enc.c
1 /* aes_enc.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  * \file     aes_enc.c
21  * \email    daniel.otte@rub.de
22  * \author   Daniel Otte 
23  * \date     2008-12-30
24  * \license  GPLv3 or later
25  * 
26  */
27
28 #include <stdint.h>
29 #include <string.h>
30 #include "aes.h"
31 #include "gf256mul.h"
32 #include "aes_sbox.h"
33 #include "aes_enc.h"
34
35 void aes_shiftcol(void* data, uint8_t shift){
36         uint8_t tmp[4];
37         tmp[0] = ((uint8_t*)data)[ 0];
38         tmp[1] = ((uint8_t*)data)[ 4];
39         tmp[2] = ((uint8_t*)data)[ 8];
40         tmp[3] = ((uint8_t*)data)[12];
41         ((uint8_t*)data)[ 0] = tmp[(shift+0)&3];
42         ((uint8_t*)data)[ 4] = tmp[(shift+1)&3];
43         ((uint8_t*)data)[ 8] = tmp[(shift+2)&3];
44         ((uint8_t*)data)[12] = tmp[(shift+3)&3];
45 }
46
47 #define GF256MUL_1(a) (a)
48 #define GF256MUL_2(a) (gf256mul(2, (a), 0x1b))
49 #define GF256MUL_3(a) (gf256mul(3, (a), 0x1b))
50
51 static
52 void aes_enc_round(aes_cipher_state_t* state, const aes_roundkey_t* k){
53         uint8_t tmp[16], t;
54         uint8_t i;
55         /* subBytes */
56         for(i=0; i<16; ++i){
57                 tmp[i] = aes_sbox[state->s[i]];
58         }
59         /* shiftRows */
60         aes_shiftcol(tmp+1, 1);
61         aes_shiftcol(tmp+2, 2);
62         aes_shiftcol(tmp+3, 3);
63         /* mixColums */
64         for(i=0; i<4; ++i){
65                 t = tmp[4*i+0] ^ tmp[4*i+1] ^ tmp[4*i+2] ^ tmp[4*i+3];
66                 state->s[4*i+0] =
67                           GF256MUL_2(tmp[4*i+0]^tmp[4*i+1])
68                         ^ tmp[4*i+0]
69                         ^ t;
70                 state->s[4*i+1] =
71                           GF256MUL_2(tmp[4*i+1]^tmp[4*i+2])
72                         ^ tmp[4*i+1]
73                         ^ t;
74                 state->s[4*i+2] =
75                           GF256MUL_2(tmp[4*i+2]^tmp[4*i+3])
76                         ^ tmp[4*i+2]
77                         ^ t;
78                 state->s[4*i+3] =
79                           GF256MUL_2(tmp[4*i+3]^tmp[4*i+0])
80                         ^ tmp[4*i+3]
81                         ^ t;
82         }
83
84         /* addKey */
85         for(i=0; i<16; ++i){
86                 state->s[i] ^= k->ks[i];
87         }
88 }
89
90
91 static
92 void aes_enc_lastround(aes_cipher_state_t* state,const aes_roundkey_t* k){
93         uint8_t i;
94         /* subBytes */
95         for(i=0; i<16; ++i){
96                 state->s[i] = aes_sbox[state->s[i]];
97         }
98         /* shiftRows */
99         aes_shiftcol(state->s+1, 1);
100         aes_shiftcol(state->s+2, 2);
101         aes_shiftcol(state->s+3, 3);
102         /* keyAdd */
103         for(i=0; i<16; ++i){
104                 state->s[i] ^= k->ks[i];
105         }
106 }
107
108 void aes_encrypt_core(aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds){
109         uint8_t i;
110         for(i=0; i<16; ++i){
111                 state->s[i] ^= ks->key[0].ks[i];
112         }
113         i=1;
114         for(;rounds>1;--rounds){
115                 aes_enc_round(state, &(ks->key[i]));
116                 ++i;
117         }
118         aes_enc_lastround(state, &(ks->key[i]));
119 }