]> git.cryptolib.org Git - avr-crypto-lib.git/blob - aes_dec.c
new AES in C, happy new year
[avr-crypto-lib.git] / aes_dec.c
1 /* aes.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 #include <stdint.h>
21 #include <string.h>
22 #include "gf256mul.h"
23 #include "aes.h"
24 #include "aes_invsbox.h"
25 #include "aes_dec.h"
26 #include <avr/pgmspace.h>
27
28 void aes_invshiftrow(void* data, uint8_t shift){
29         uint8_t tmp[4];
30         tmp[0] = ((uint8_t*)data)[(4+0-shift)&3];
31         tmp[1] = ((uint8_t*)data)[(4+1-shift)&3];
32         tmp[2] = ((uint8_t*)data)[(4+2-shift)&3];
33         tmp[3] = ((uint8_t*)data)[(4+3-shift)&3];
34         memcpy(data, tmp, 4);
35 }
36
37 static
38 void aes_dec_round(aes_cipher_state_t* state, const aes_roundkey_t* k){
39         uint8_t tmp[16];
40         uint8_t i;
41         /* keyAdd */
42         for(i=0; i<16; ++i){
43                 tmp[i] = state->s[i] ^ k->ks[i];
44         }
45         /* mixColums */
46         for(i=0; i<4; ++i){
47                 state->s[4*0+i] =
48                           gf256mul(0xe, tmp[4*0+i], 0x1b)
49                         ^ gf256mul(0xb, tmp[4*1+i], 0x1b)
50                         ^ gf256mul(0xd, tmp[4*2+i], 0x1b)
51                         ^ gf256mul(0x9, tmp[4*3+i], 0x1b);
52                 state->s[4*1+i] =
53                           gf256mul(0x9, tmp[4*0+i], 0x1b)
54                         ^ gf256mul(0xe, tmp[4*1+i], 0x1b)
55                         ^ gf256mul(0xb, tmp[4*2+i], 0x1b)
56                         ^ gf256mul(0xd, tmp[4*3+i], 0x1b);
57                 state->s[4*2+i] =
58                           gf256mul(0xd, tmp[4*0+i], 0x1b)
59                         ^ gf256mul(0x9, tmp[4*1+i], 0x1b)
60                         ^ gf256mul(0xe, tmp[4*2+i], 0x1b)
61                         ^ gf256mul(0xb, tmp[4*3+i], 0x1b);
62                 state->s[4*3+i] =
63                           gf256mul(0xb, tmp[4*0+i], 0x1b)
64                         ^ gf256mul(0xd, tmp[4*1+i], 0x1b)
65                         ^ gf256mul(0x9, tmp[4*2+i], 0x1b)
66                         ^ gf256mul(0xe, tmp[4*3+i], 0x1b);
67         }       
68         /* shiftRows */
69         aes_invshiftrow(state->s+4, 1);
70         aes_invshiftrow(state->s+8, 2);
71         aes_invshiftrow(state->s+12, 3);                
72         /* subBytes */
73         for(i=0; i<16; ++i){
74                 state->s[i] = pgm_read_byte(aes_invsbox+state->s[i]);
75         }
76 }
77
78
79 static
80 void aes_dec_firstround(aes_cipher_state_t* state, const aes_roundkey_t* k){
81         uint8_t i;
82         /* keyAdd */
83         for(i=0; i<16; ++i){
84                 state->s[i] ^= k->ks[i];
85         }
86         /* shiftRows */
87         aes_invshiftrow(state->s+4, 1);
88         aes_invshiftrow(state->s+8, 2);
89         aes_invshiftrow(state->s+12, 3);                
90         /* subBytes */
91         for(i=0; i<16; ++i){
92                 state->s[i] = pgm_read_byte(aes_invsbox+state->s[i]);
93         }
94 }
95
96 void aes_decrypt_core(aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds){
97         uint8_t i;
98         aes_dec_firstround(state, &(ks->key[i=rounds]));
99         for(;rounds>1;--rounds){
100                 --i;
101                 aes_dec_round(state, &(ks->key[i]));
102         }
103         for(i=0; i<16; ++i){
104                 state->s[i] ^= ks->key[0].ks[i];
105         }
106 }