]> git.cryptolib.org Git - avr-crypto-lib.git/blob - aes_enc.c
some mor ciphers for the blockcipher abstraction layer
[avr-crypto-lib.git] / aes_enc.c
1 /* aes_enc.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  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 #include <avr/pgmspace.h>
35
36
37 void aes_shiftrow(void* data, uint8_t shift){
38         uint8_t tmp[4];
39         tmp[0] = ((uint8_t*)data)[(0+shift)&3];
40         tmp[1] = ((uint8_t*)data)[(1+shift)&3];
41         tmp[2] = ((uint8_t*)data)[(2+shift)&3];
42         tmp[3] = ((uint8_t*)data)[(3+shift)&3];
43         memcpy(data, tmp, 4);
44 }
45
46 #define GF256MUL_1(a) (a)
47 #define GF256MUL_2(a) (gf256mul(2, (a), 0x1b))
48 #define GF256MUL_3(a) (gf256mul(3, (a), 0x1b))
49
50 static
51 void aes_enc_round(aes_cipher_state_t* state, const aes_roundkey_t* k){
52         uint8_t tmp[16];
53         uint8_t i;
54         /* subBytes */
55         for(i=0; i<16; ++i){
56                 tmp[i] = pgm_read_byte(aes_sbox+state->s[i]);
57         }
58         /* shiftRows */
59         aes_shiftrow(tmp+4, 1);
60         aes_shiftrow(tmp+8, 2);
61         aes_shiftrow(tmp+12, 3);
62         /* mixColums */
63         for(i=0; i<4; ++i){
64                 state->s[4*0+i] =
65                           GF256MUL_2(tmp[4*0+i])
66                         ^ GF256MUL_3(tmp[4*1+i])
67                         ^ GF256MUL_1(tmp[4*2+i])
68                         ^ GF256MUL_1(tmp[4*3+i]);
69                 state->s[4*1+i] =
70                           GF256MUL_1(tmp[4*0+i])
71                         ^ GF256MUL_2(tmp[4*1+i])
72                         ^ GF256MUL_3(tmp[4*2+i])
73                         ^ GF256MUL_1(tmp[4*3+i]);
74                 state->s[4*2+i] =
75                           GF256MUL_1(tmp[4*0+i])
76                         ^ GF256MUL_1(tmp[4*1+i])
77                         ^ GF256MUL_2(tmp[4*2+i])
78                         ^ GF256MUL_3(tmp[4*3+i]);
79                 state->s[4*3+i] =
80                           GF256MUL_3(tmp[4*0+i])
81                         ^ GF256MUL_1(tmp[4*1+i])
82                         ^ GF256MUL_1(tmp[4*2+i])
83                         ^ GF256MUL_2(tmp[4*3+i]);               
84         }
85
86         /* addKey */
87         for(i=0; i<16; ++i){
88                 state->s[i] ^= k->ks[i];
89         }
90 }
91
92
93 static
94 void aes_enc_lastround(aes_cipher_state_t* state,const aes_roundkey_t* k){
95         uint8_t i;
96         /* subBytes */
97         for(i=0; i<16; ++i){
98                 state->s[i] = pgm_read_byte(aes_sbox+state->s[i]);
99         }
100         /* shiftRows */
101         aes_shiftrow(state->s+4, 1);
102         aes_shiftrow(state->s+8, 2);
103         aes_shiftrow(state->s+12, 3);
104         /* keyAdd */
105         for(i=0; i<16; ++i){
106                 state->s[i] ^= k->ks[i];
107         }
108 }
109
110 void aes_encrypt_core(aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds){
111         uint8_t i;
112         for(i=0; i<16; ++i){
113                 state->s[i] ^= ks->key[0].ks[i];
114         }
115         i=1;
116         for(;rounds>1;--rounds){
117                 aes_enc_round(state, &(ks->key[i]));
118                 ++i;
119         }
120         aes_enc_lastround(state, &(ks->key[i]));
121 }