Skip to content

Commit 2ed7c0f

Browse files
committed
Use PROGMEM for lookup tables on AVR platforms
1 parent 12e7744 commit 2ed7c0f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

aes.c

+28
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
3838
#include <string.h> // CBC mode, for memset
3939
#include "aes.h"
4040

41+
#ifdef __AVR__
42+
#include <avr/pgmspace.h> // For PROGMEM
43+
#endif
44+
4145
/*****************************************************************************/
4246
/* Defines: */
4347
/*****************************************************************************/
@@ -76,7 +80,11 @@ typedef uint8_t state_t[4][4];
7680
// The lookup-tables are marked const so they can be placed in read-only storage instead of RAM
7781
// The numbers below can be computed dynamically trading ROM for RAM -
7882
// This can be useful in (embedded) bootloader applications, where ROM is often limited.
83+
#ifndef __AVR__
7984
static const uint8_t sbox[256] = {
85+
#else
86+
static const uint8_t sbox[256] PROGMEM = {
87+
#endif
8088
//0 1 2 3 4 5 6 7 8 9 A B C D E F
8189
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
8290
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] = {
96104
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
97105

98106
#if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
107+
#ifndef __AVR__
99108
static const uint8_t rsbox[256] = {
109+
#else
110+
static const uint8_t rsbox[256] PROGMEM = {
111+
#endif
100112
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
101113
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
102114
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] = {
117129

118130
// The round constant word array, Rcon[i], contains the values given by
119131
// x to the power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8)
132+
#ifndef __AVR__
120133
static const uint8_t Rcon[11] = {
134+
#else
135+
static const uint8_t Rcon[11] PROGMEM = {
136+
#endif
121137
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
122138

123139
/*
@@ -140,7 +156,11 @@ static uint8_t getSBoxValue(uint8_t num)
140156
return sbox[num];
141157
}
142158
*/
159+
#ifndef __AVR__
143160
#define getSBoxValue(num) (sbox[(num)])
161+
#else
162+
#define getSBoxValue(num) (pgm_read_byte(sbox + (num)))
163+
#endif
144164

145165
// This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states.
146166
static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key)
@@ -194,7 +214,11 @@ static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key)
194214
tempa[3] = getSBoxValue(tempa[3]);
195215
}
196216

217+
#ifndef __AVR__
197218
tempa[0] = tempa[0] ^ Rcon[i/Nk];
219+
#else
220+
tempa[0] = tempa[0] ^ (pgm_read_byte(Rcon + (i/Nk)));
221+
#endif
198222
}
199223
#if defined(AES256) && (AES256 == 1)
200224
if (i % Nk == 4)
@@ -342,7 +366,11 @@ static uint8_t getSBoxInvert(uint8_t num)
342366
return rsbox[num];
343367
}
344368
*/
369+
#ifndef __AVR__
345370
#define getSBoxInvert(num) (rsbox[(num)])
371+
#else
372+
#define getSBoxInvert(num) (pgm_read_byte(rsbox + (num)))
373+
#endif
346374

347375
// MixColumns function mixes the columns of the state matrix.
348376
// The method used to multiply may be difficult to understand for the inexperienced.

0 commit comments

Comments
 (0)