]> git.cryptolib.org Git - avr-crypto-lib.git/blob - aes_dec.c
48ad3b47986b61a1f6b764b3f40d136f20767d7f
[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, 2009  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 void aes_invshiftcol(void* data, uint8_t shift){
38         uint8_t tmp[4];
39         tmp[0] = ((uint8_t*)data)[ 0];
40         tmp[1] = ((uint8_t*)data)[ 4];
41         tmp[2] = ((uint8_t*)data)[ 8];
42         tmp[3] = ((uint8_t*)data)[12];
43         ((uint8_t*)data)[ 0] = tmp[(4-shift+0)&3];
44         ((uint8_t*)data)[ 4] = tmp[(4-shift+1)&3];
45         ((uint8_t*)data)[ 8] = tmp[(4-shift+2)&3];
46         ((uint8_t*)data)[12] = tmp[(4-shift+3)&3];
47 }
48 static
49 void aes_dec_round(aes_cipher_state_t* state, const aes_roundkey_t* k){
50         uint8_t tmp[16];
51         uint8_t i;
52         /* keyAdd */
53         for(i=0; i<16; ++i){
54                 tmp[i] = state->s[i] ^ k->ks[i];
55         }
56         /* mixColums */
57         for(i=0; i<4; ++i){
58                 state->s[4*i+0] =
59                           gf256mul(0xe, tmp[4*i+0], 0x1b)
60                         ^ gf256mul(0xb, tmp[4*i+1], 0x1b)
61                         ^ gf256mul(0xd, tmp[4*i+2], 0x1b)
62                         ^ gf256mul(0x9, tmp[4*i+3], 0x1b);
63                 state->s[4*i+1] =
64                           gf256mul(0x9, tmp[4*i+0], 0x1b)
65                         ^ gf256mul(0xe, tmp[4*i+1], 0x1b)
66                         ^ gf256mul(0xb, tmp[4*i+2], 0x1b)
67                         ^ gf256mul(0xd, tmp[4*i+3], 0x1b);
68                 state->s[4*i+2] =
69                           gf256mul(0xd, tmp[4*i+0], 0x1b)
70                         ^ gf256mul(0x9, tmp[4*i+1], 0x1b)
71                         ^ gf256mul(0xe, tmp[4*i+2], 0x1b)
72                         ^ gf256mul(0xb, tmp[4*i+3], 0x1b);
73                 state->s[4*i+3] =
74                           gf256mul(0xb, tmp[4*i+0], 0x1b)
75                         ^ gf256mul(0xd, tmp[4*i+1], 0x1b)
76                         ^ gf256mul(0x9, tmp[4*i+2], 0x1b)
77                         ^ gf256mul(0xe, tmp[4*i+3], 0x1b);
78         }       
79         /* shiftRows */
80         aes_invshiftcol(state->s+1, 1);
81         aes_invshiftcol(state->s+2, 2);
82         aes_invshiftcol(state->s+3, 3);         
83         /* subBytes */
84         for(i=0; i<16; ++i){
85                 state->s[i] = pgm_read_byte(aes_invsbox+state->s[i]);
86         }
87 }
88
89
90 static
91 void aes_dec_firstround(aes_cipher_state_t* state, const aes_roundkey_t* k){
92         uint8_t i;
93         /* keyAdd */
94         for(i=0; i<16; ++i){
95                 state->s[i] ^= k->ks[i];
96         }
97         /* shiftRows */
98         aes_invshiftcol(state->s+1, 1);
99         aes_invshiftcol(state->s+2, 2);
100         aes_invshiftcol(state->s+3, 3);         
101         /* subBytes */
102         for(i=0; i<16; ++i){
103                 state->s[i] = pgm_read_byte(aes_invsbox+state->s[i]);
104         }
105 }
106
107 void aes_decrypt_core(aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds){
108         uint8_t i;
109         aes_dec_firstround(state, &(ks->key[i=rounds]));
110         for(;rounds>1;--rounds){
111                 --i;
112                 aes_dec_round(state, &(ks->key[i]));
113         }
114         for(i=0; i<16; ++i){
115                 state->s[i] ^= ks->key[0].ks[i];
116         }
117 }