]> git.cryptolib.org Git - arm-crypto-lib.git/blob - aes/aes_dec.c
fixing bugs reported by Christian Dernehl
[arm-crypto-lib.git] / aes / aes_dec.c
1 /* aes.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 <string.h>
22 #include "gf256mul.h"
23 #include "aes.h"
24 #include "aes_invsbox.h"
25 #include "aes_dec.h"
26
27 void aes_invshiftrow(void* data, uint8_t shift){
28         uint8_t tmp[4];
29         tmp[0] = ((uint8_t*)data)[(4+0-shift)&3];
30         tmp[1] = ((uint8_t*)data)[(4+1-shift)&3];
31         tmp[2] = ((uint8_t*)data)[(4+2-shift)&3];
32         tmp[3] = ((uint8_t*)data)[(4+3-shift)&3];
33         memcpy(data, tmp, 4);
34 }
35
36 void aes_invshiftcol(void* data, uint8_t shift){
37         uint8_t tmp[4];
38         tmp[0] = ((uint8_t*)data)[ 0];
39         tmp[1] = ((uint8_t*)data)[ 4];
40         tmp[2] = ((uint8_t*)data)[ 8];
41         tmp[3] = ((uint8_t*)data)[12];
42         ((uint8_t*)data)[ 0] = tmp[(4-shift+0)&3];
43         ((uint8_t*)data)[ 4] = tmp[(4-shift+1)&3];
44         ((uint8_t*)data)[ 8] = tmp[(4-shift+2)&3];
45         ((uint8_t*)data)[12] = tmp[(4-shift+3)&3];
46 }
47 static
48 void aes_dec_round(aes_cipher_state_t* state, const aes_roundkey_t* k){
49         uint8_t tmp[16];
50         uint8_t i;
51         uint8_t t,u,v,w;
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                 t = tmp[4*i+3] ^ tmp[4*i+2];
59                 u = tmp[4*i+1] ^ tmp[4*i+0];
60                 v = t ^ u;
61                 v = gf256mul(0x09, v, 0x1b);
62                 w = v ^ gf256mul(0x04, tmp[4*i+2] ^ tmp[4*i+0], 0x1b);
63                 v = v ^ gf256mul(0x04, tmp[4*i+3] ^ tmp[4*i+1], 0x1b);
64                 state->s[4*i+3] = tmp[4*i+3] ^ v ^ gf256mul(0x02, tmp[4*i+0] ^ tmp[4*i+3], 0x1b);
65                 state->s[4*i+2] = tmp[4*i+2] ^ w ^ gf256mul(0x02, t, 0x1b);
66                 state->s[4*i+1] = tmp[4*i+1] ^ v ^ gf256mul(0x02, tmp[4*i+2] ^ tmp[4*i+1], 0x1b);
67                 state->s[4*i+0] = tmp[4*i+0] ^ w ^ gf256mul(0x02, u, 0x1b);
68                 
69                 /*
70                 state->s[4*i+0] =
71                           gf256mul(0xe, tmp[4*i+0], 0x1b)
72                         ^ gf256mul(0xb, tmp[4*i+1], 0x1b)
73                         ^ gf256mul(0xd, tmp[4*i+2], 0x1b)
74                         ^ gf256mul(0x9, tmp[4*i+3], 0x1b);
75                 state->s[4*i+1] =
76                           gf256mul(0x9, tmp[4*i+0], 0x1b)
77                         ^ gf256mul(0xe, tmp[4*i+1], 0x1b)
78                         ^ gf256mul(0xb, tmp[4*i+2], 0x1b)
79                         ^ gf256mul(0xd, tmp[4*i+3], 0x1b);
80                 state->s[4*i+2] =
81                           gf256mul(0xd, tmp[4*i+0], 0x1b)
82                         ^ gf256mul(0x9, tmp[4*i+1], 0x1b)
83                         ^ gf256mul(0xe, tmp[4*i+2], 0x1b)
84                         ^ gf256mul(0xb, tmp[4*i+3], 0x1b);
85                 state->s[4*i+3] =
86                           gf256mul(0xb, tmp[4*i+0], 0x1b)
87                         ^ gf256mul(0xd, tmp[4*i+1], 0x1b)
88                         ^ gf256mul(0x9, tmp[4*i+2], 0x1b)
89                         ^ gf256mul(0xe, tmp[4*i+3], 0x1b);
90                 */
91         }       
92         /* shiftRows */
93         aes_invshiftcol(state->s+1, 1);
94         aes_invshiftcol(state->s+2, 2);
95         aes_invshiftcol(state->s+3, 3);         
96         /* subBytes */
97         for(i=0; i<16; ++i){
98                 state->s[i] = aes_invsbox[state->s[i]];
99         }
100 }
101
102
103 static
104 void aes_dec_firstround(aes_cipher_state_t* state, const aes_roundkey_t* k){
105         uint8_t i;
106         /* keyAdd */
107         for(i=0; i<16; ++i){
108                 state->s[i] ^= k->ks[i];
109         }
110         /* shiftRows */
111         aes_invshiftcol(state->s+1, 1);
112         aes_invshiftcol(state->s+2, 2);
113         aes_invshiftcol(state->s+3, 3);         
114         /* subBytes */
115         for(i=0; i<16; ++i){
116                 state->s[i] = aes_invsbox[state->s[i]];
117         }
118 }
119
120 void aes_decrypt_core(aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds){
121         uint8_t i;
122         aes_dec_firstround(state, &(ks->key[i=rounds]));
123         for(;rounds>1;--rounds){
124                 --i;
125                 aes_dec_round(state, &(ks->key[i]));
126         }
127         for(i=0; i<16; ++i){
128                 state->s[i] ^= ks->key[0].ks[i];
129         }
130 }