@@ -38,6 +38,10 @@ NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
38
38
#include <string.h> // CBC mode, for memset
39
39
#include "aes.h"
40
40
41
+ #ifdef __AVR__
42
+ #include <avr/pgmspace.h> // For PROGMEM
43
+ #endif
44
+
41
45
/*****************************************************************************/
42
46
/* Defines: */
43
47
/*****************************************************************************/
@@ -76,7 +80,11 @@ typedef uint8_t state_t[4][4];
76
80
// The lookup-tables are marked const so they can be placed in read-only storage instead of RAM
77
81
// The numbers below can be computed dynamically trading ROM for RAM -
78
82
// This can be useful in (embedded) bootloader applications, where ROM is often limited.
83
+ #ifndef __AVR__
79
84
static const uint8_t sbox [256 ] = {
85
+ #else
86
+ static const uint8_t sbox [256 ] PROGMEM = {
87
+ #endif
80
88
//0 1 2 3 4 5 6 7 8 9 A B C D E F
81
89
0x63 , 0x7c , 0x77 , 0x7b , 0xf2 , 0x6b , 0x6f , 0xc5 , 0x30 , 0x01 , 0x67 , 0x2b , 0xfe , 0xd7 , 0xab , 0x76 ,
82
90
0xca , 0x82 , 0xc9 , 0x7d , 0xfa , 0x59 , 0x47 , 0xf0 , 0xad , 0xd4 , 0xa2 , 0xaf , 0x9c , 0xa4 , 0x72 , 0xc0 ,
@@ -96,7 +104,11 @@ static const uint8_t sbox[256] = {
96
104
0x8c , 0xa1 , 0x89 , 0x0d , 0xbf , 0xe6 , 0x42 , 0x68 , 0x41 , 0x99 , 0x2d , 0x0f , 0xb0 , 0x54 , 0xbb , 0x16 };
97
105
98
106
#if (defined(CBC ) && CBC == 1 ) || (defined(ECB ) && ECB == 1 )
107
+ #ifndef __AVR__
99
108
static const uint8_t rsbox [256 ] = {
109
+ #else
110
+ static const uint8_t rsbox [256 ] PROGMEM = {
111
+ #endif
100
112
0x52 , 0x09 , 0x6a , 0xd5 , 0x30 , 0x36 , 0xa5 , 0x38 , 0xbf , 0x40 , 0xa3 , 0x9e , 0x81 , 0xf3 , 0xd7 , 0xfb ,
101
113
0x7c , 0xe3 , 0x39 , 0x82 , 0x9b , 0x2f , 0xff , 0x87 , 0x34 , 0x8e , 0x43 , 0x44 , 0xc4 , 0xde , 0xe9 , 0xcb ,
102
114
0x54 , 0x7b , 0x94 , 0x32 , 0xa6 , 0xc2 , 0x23 , 0x3d , 0xee , 0x4c , 0x95 , 0x0b , 0x42 , 0xfa , 0xc3 , 0x4e ,
@@ -117,7 +129,11 @@ static const uint8_t rsbox[256] = {
117
129
118
130
// The round constant word array, Rcon[i], contains the values given by
119
131
// x to the power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8)
132
+ #ifndef __AVR__
120
133
static const uint8_t Rcon [11 ] = {
134
+ #else
135
+ static const uint8_t Rcon [11 ] PROGMEM = {
136
+ #endif
121
137
0x8d , 0x01 , 0x02 , 0x04 , 0x08 , 0x10 , 0x20 , 0x40 , 0x80 , 0x1b , 0x36 };
122
138
123
139
/*
@@ -140,7 +156,11 @@ static uint8_t getSBoxValue(uint8_t num)
140
156
return sbox[num];
141
157
}
142
158
*/
159
+ #ifndef __AVR__
143
160
#define getSBoxValue (num ) (sbox[(num)])
161
+ #else
162
+ #define getSBoxValue (num ) (pgm_read_byte(sbox + (num)))
163
+ #endif
144
164
145
165
// This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states.
146
166
static void KeyExpansion (uint8_t * RoundKey , const uint8_t * Key )
@@ -194,7 +214,11 @@ static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key)
194
214
tempa [3 ] = getSBoxValue (tempa [3 ]);
195
215
}
196
216
217
+ #ifndef __AVR__
197
218
tempa [0 ] = tempa [0 ] ^ Rcon [i /Nk ];
219
+ #else
220
+ tempa [0 ] = tempa [0 ] ^ (pgm_read_byte (Rcon + (i /Nk )));
221
+ #endif
198
222
}
199
223
#if defined(AES256 ) && (AES256 == 1 )
200
224
if (i % Nk == 4 )
@@ -342,7 +366,11 @@ static uint8_t getSBoxInvert(uint8_t num)
342
366
return rsbox[num];
343
367
}
344
368
*/
369
+ #ifndef __AVR__
345
370
#define getSBoxInvert (num ) (rsbox[(num)])
371
+ #else
372
+ #define getSBoxInvert (num ) (pgm_read_byte(rsbox + (num)))
373
+ #endif
346
374
347
375
// MixColumns function mixes the columns of the state matrix.
348
376
// The method used to multiply may be difficult to understand for the inexperienced.
0 commit comments