forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes_dec.cpp
135 lines (125 loc) · 3.87 KB
/
aes_dec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* aes.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008, 2009 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <string.h>
#include "gf256mul.h"
#include "aes.h"
#include "aes_invsbox.h"
#include "aes_dec.h"
#ifdef ARDUINO_ARCH_ESP32
#include <pgmspace.h>
#else
#include <avr/pgmspace.h>
#endif
void aes_invshiftrow(void *data, uint8_t shift){
uint8_t tmp[4];
tmp[0] = ((uint8_t*)data)[(4+0-shift)&3];
tmp[1] = ((uint8_t*)data)[(4+1-shift)&3];
tmp[2] = ((uint8_t*)data)[(4+2-shift)&3];
tmp[3] = ((uint8_t*)data)[(4+3-shift)&3];
memcpy(data, tmp, 4);
}
void aes_invshiftcol(void *data, uint8_t shift){
uint8_t tmp[4];
tmp[0] = ((uint8_t*)data)[ 0];
tmp[1] = ((uint8_t*)data)[ 4];
tmp[2] = ((uint8_t*)data)[ 8];
tmp[3] = ((uint8_t*)data)[12];
((uint8_t*)data)[ 0] = tmp[(4-shift+0)&3];
((uint8_t*)data)[ 4] = tmp[(4-shift+1)&3];
((uint8_t*)data)[ 8] = tmp[(4-shift+2)&3];
((uint8_t*)data)[12] = tmp[(4-shift+3)&3];
}
static
void aes_dec_round(aes_cipher_state_t *state, const aes_roundkey_t *k){
uint8_t tmp[16];
uint8_t i;
uint8_t t,u,v,w;
/* keyAdd */
for(i=0; i<16; ++i){
tmp[i] = state->s[i] ^ k->ks[i];
}
/* mixColums */
for(i=0; i<4; ++i){
t = tmp[4*i+3] ^ tmp[4*i+2];
u = tmp[4*i+1] ^ tmp[4*i+0];
v = t ^ u;
v = gf256mul(0x09, v, 0x1b);
w = v ^ gf256mul(0x04, tmp[4*i+2] ^ tmp[4*i+0], 0x1b);
v = v ^ gf256mul(0x04, tmp[4*i+3] ^ tmp[4*i+1], 0x1b);
state->s[4*i+3] = tmp[4*i+3] ^ v ^ gf256mul(0x02, tmp[4*i+0] ^ tmp[4*i+3], 0x1b);
state->s[4*i+2] = tmp[4*i+2] ^ w ^ gf256mul(0x02, t, 0x1b);
state->s[4*i+1] = tmp[4*i+1] ^ v ^ gf256mul(0x02, tmp[4*i+2] ^ tmp[4*i+1], 0x1b);
state->s[4*i+0] = tmp[4*i+0] ^ w ^ gf256mul(0x02, u, 0x1b);
/*
state->s[4*i+0] =
gf256mul(0xe, tmp[4*i+0], 0x1b)
^ gf256mul(0xb, tmp[4*i+1], 0x1b)
^ gf256mul(0xd, tmp[4*i+2], 0x1b)
^ gf256mul(0x9, tmp[4*i+3], 0x1b);
state->s[4*i+1] =
gf256mul(0x9, tmp[4*i+0], 0x1b)
^ gf256mul(0xe, tmp[4*i+1], 0x1b)
^ gf256mul(0xb, tmp[4*i+2], 0x1b)
^ gf256mul(0xd, tmp[4*i+3], 0x1b);
state->s[4*i+2] =
gf256mul(0xd, tmp[4*i+0], 0x1b)
^ gf256mul(0x9, tmp[4*i+1], 0x1b)
^ gf256mul(0xe, tmp[4*i+2], 0x1b)
^ gf256mul(0xb, tmp[4*i+3], 0x1b);
state->s[4*i+3] =
gf256mul(0xb, tmp[4*i+0], 0x1b)
^ gf256mul(0xd, tmp[4*i+1], 0x1b)
^ gf256mul(0x9, tmp[4*i+2], 0x1b)
^ gf256mul(0xe, tmp[4*i+3], 0x1b);
*/
}
/* shiftRows */
aes_invshiftcol(state->s+1, 1);
aes_invshiftcol(state->s+2, 2);
aes_invshiftcol(state->s+3, 3);
/* subBytes */
for(i=0; i<16; ++i){
state->s[i] = pgm_read_byte(aes_invsbox+state->s[i]);
}
}
static
void aes_dec_firstround(aes_cipher_state_t *state, const aes_roundkey_t *k){
uint8_t i;
/* keyAdd */
for(i=0; i<16; ++i){
state->s[i] ^= k->ks[i];
}
/* shiftRows */
aes_invshiftcol(state->s+1, 1);
aes_invshiftcol(state->s+2, 2);
aes_invshiftcol(state->s+3, 3);
/* subBytes */
for(i=0; i<16; ++i){
state->s[i] = pgm_read_byte(aes_invsbox+state->s[i]);
}
}
void aes_decrypt_core(aes_cipher_state_t *state, const aes_genctx_t *ks, uint8_t rounds){
uint8_t i;
aes_dec_firstround(state, &(ks->key[i=rounds]));
for(;rounds>1;--rounds){
--i;
aes_dec_round(state, &(ks->key[i]));
}
for(i=0; i<16; ++i){
state->s[i] ^= ks->key[0].ks[i];
}
}