Skip to content

Commit feed1ca

Browse files
ADieaigrr
authored andcommitted
decrease RAM usage using PROGMEM
1 parent 8afe552 commit feed1ca

File tree

9 files changed

+103
-35
lines changed

9 files changed

+103
-35
lines changed

Makefile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ AR := $(TOOLCHAIN_PREFIX)ar
44
LD := $(TOOLCHAIN_PREFIX)gcc
55
OBJCOPY := $(TOOLCHAIN_PREFIX)objcopy
66

7-
MFORCE32 := $(shell $(CC) --help=target | grep mforce-l32)
8-
97
XTENSA_LIBS ?= $(shell $(CC) -print-sysroot)
108

119

@@ -43,13 +41,17 @@ LDFLAGS += -L$(XTENSA_LIBS)/lib \
4341

4442
CFLAGS+=-std=c99 -DESP8266
4543

46-
CFLAGS += -Wall -Os -g -O2 -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mno-text-section-literals -D__ets__ -DICACHE_FLASH
44+
CFLAGS += -Wall -Os -g -O2 -Wpointer-arith -Wl,-EL -nostdlib -mlongcalls -mno-text-section-literals -D__ets__ -DICACHE_FLASH
4745

46+
MFORCE32 := $(shell $(CC) --help=target | grep mforce-l32)
4847
ifneq ($(MFORCE32),)
49-
# Your compiler supports the -mforce-l32 flag which means that
50-
# constants can be stored in flash (program) memory instead of SRAM.
51-
# See: https://www.arduino.cc/en/Reference/PROGMEM
52-
CFLAGS += -DPROGMEM="__attribute__((aligned(4))) __attribute__((section(\".irom.text\")))" -mforce-l32
48+
# If the compiler supports the -mforce-l32 flag, the compiler will generate correct code for loading
49+
# 16- and 8-bit constants from program memory. So in the code we can directly access the arrays
50+
# placed into program memory.
51+
CFLAGS += -mforce-l32
52+
else
53+
# Otherwise we need to use a helper function to load 16- and 8-bit constants from program memory.
54+
CFLAGS += -DWITH_PGM_READ_HELPER
5355
endif
5456

5557
BIN_DIR := bin

crypto/aes.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,20 @@ void AES_set_key(AES_CTX *ctx, const uint8_t *key,
220220

221221
if ((i % words) == 0)
222222
{
223-
tmp2 =(uint32_t)aes_sbox[(tmp )&0xff]<< 8;
224-
tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<<16;
225-
tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<24;
226-
tmp2|=(uint32_t)aes_sbox[(tmp>>24) ];
223+
tmp2 =(uint32_t)(ax_array_read_u8(aes_sbox, (tmp )&0xff))<< 8;
224+
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>> 8)&0xff))<<16;
225+
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>16)&0xff))<<24;
226+
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>24)));
227227
tmp=tmp2^(((unsigned int)*ip)<<24);
228228
ip++;
229229
}
230230

231231
if ((words == 8) && ((i % words) == 4))
232232
{
233-
tmp2 =(uint32_t)aes_sbox[(tmp )&0xff] ;
234-
tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<< 8;
235-
tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<16;
236-
tmp2|=(uint32_t)aes_sbox[(tmp>>24) ]<<24;
233+
tmp2 =(uint32_t)(ax_array_read_u8(aes_sbox, (tmp )&0xff)) ;
234+
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>> 8)&0xff))<< 8;
235+
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>16)&0xff))<<16;
236+
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>24) ))<<24;
237237
tmp=tmp2;
238238
}
239239

@@ -369,10 +369,10 @@ static void AES_encrypt(const AES_CTX *ctx, uint32_t *data)
369369
/* Perform ByteSub and ShiftRow operations together */
370370
for (row = 0; row < 4; row++)
371371
{
372-
a0 = (uint32_t)aes_sbox[(data[row%4]>>24)&0xFF];
373-
a1 = (uint32_t)aes_sbox[(data[(row+1)%4]>>16)&0xFF];
374-
a2 = (uint32_t)aes_sbox[(data[(row+2)%4]>>8)&0xFF];
375-
a3 = (uint32_t)aes_sbox[(data[(row+3)%4])&0xFF];
372+
a0 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[row%4]>>24)&0xFF));
373+
a1 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[(row+1)%4]>>16)&0xFF));
374+
a2 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[(row+2)%4]>>8)&0xFF));
375+
a3 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[(row+3)%4])&0xFF));
376376

377377
/* Perform MixColumn iff not last round */
378378
if (curr_rnd < (rounds - 1))
@@ -417,10 +417,10 @@ static void AES_decrypt(const AES_CTX *ctx, uint32_t *data)
417417
/* Perform ByteSub and ShiftRow operations together */
418418
for (row = 4; row > 0; row--)
419419
{
420-
a0 = aes_isbox[(data[(row+3)%4]>>24)&0xFF];
421-
a1 = aes_isbox[(data[(row+2)%4]>>16)&0xFF];
422-
a2 = aes_isbox[(data[(row+1)%4]>>8)&0xFF];
423-
a3 = aes_isbox[(data[row%4])&0xFF];
420+
a0 = ax_array_read_u8(aes_isbox, (data[(row+3)%4]>>24)&0xFF);
421+
a1 = ax_array_read_u8(aes_isbox, (data[(row+2)%4]>>16)&0xFF);
422+
a2 = ax_array_read_u8(aes_isbox, (data[(row+1)%4]>>8)&0xFF);
423+
a3 = ax_array_read_u8(aes_isbox, (data[row%4])&0xFF);
424424

425425
/* Perform MixColumn iff not last round */
426426
if (curr_rnd<(rounds-1))

crypto/crypto_misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ EXP_FUNC int STDCALL base64_decode(const char *in, int len,
334334
g = 3;
335335
for (x = y = z = t = 0; x < len; x++)
336336
{
337-
if ((c = map[in[x]&0x7F]) == 0xff)
337+
if ((c = ax_array_read_u8(map, in[x]&0x7F)) == 0xff)
338338
continue;
339339

340340
if (c == 254) /* this is the end... */

crypto/md5.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static void MD5Transform(uint32_t state[4], const uint8_t block[64]);
6060
static void Encode(uint8_t *output, uint32_t *input, uint32_t len);
6161
static void Decode(uint32_t *output, const uint8_t *input, uint32_t len);
6262

63-
static const uint8_t PADDING[64] =
63+
static const uint8_t PADDING[64] PROGMEM =
6464
{
6565
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6666
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -158,15 +158,22 @@ EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *ctx)
158158
{
159159
uint8_t bits[8];
160160
uint32_t x, padLen;
161-
161+
#ifdef WITH_PGM_READ_HELPER
162+
uint8_t PADDING_stack[64];
163+
memcpy(PADDING_stack, PADDING, 64);
164+
#endif
162165
/* Save number of bits */
163166
Encode(bits, ctx->count, 8);
164167

165168
/* Pad out to 56 mod 64.
166169
*/
167170
x = (uint32_t)((ctx->count[0] >> 3) & 0x3f);
168171
padLen = (x < 56) ? (56 - x) : (120 - x);
172+
#ifdef WITH_PGM_READ_HELPER
173+
MD5_Update(ctx, PADDING_stack, padLen);
174+
#else
169175
MD5_Update(ctx, PADDING, padLen);
176+
#endif
170177

171178
/* Append length (before padding) */
172179
MD5_Update(ctx, bits, 8);

crypto/sha256.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
(b)[(i) + 3] = (uint8_t) ((n) ); \
4949
}
5050

51-
static const uint8_t sha256_padding[64] =
51+
static const uint8_t sha256_padding[64] PROGMEM =
5252
{
5353
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5454
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -249,6 +249,10 @@ void SHA256_Final(uint8_t *digest, SHA256_CTX *ctx)
249249
uint32_t last, padn;
250250
uint32_t high, low;
251251
uint8_t msglen[8];
252+
#ifdef WITH_PGM_READ_HELPER
253+
uint8_t sha256_padding_ram[64];
254+
memcpy(sha256_padding_ram, sha256_padding, 64);
255+
#endif
252256

253257
high = (ctx->total[0] >> 29)
254258
| (ctx->total[1] << 3);
@@ -259,8 +263,11 @@ void SHA256_Final(uint8_t *digest, SHA256_CTX *ctx)
259263

260264
last = ctx->total[0] & 0x3F;
261265
padn = (last < 56) ? (56 - last) : (120 - last);
262-
266+
#ifdef WITH_PGM_READ_HELPER
267+
SHA256_Update(ctx, sha256_padding_ram, padn);
268+
#else
263269
SHA256_Update(ctx, sha256_padding, padn);
270+
#endif
264271
SHA256_Update(ctx, msglen, 8);
265272

266273
PUT_UINT32(ctx->state[0], digest, 0);

ssl/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#define CONFIG_SSL_HAS_PEM 1
4646
#undef CONFIG_SSL_USE_PKCS12
4747
#define CONFIG_SSL_EXPIRY_TIME 24
48-
#define CONFIG_X509_MAX_CA_CERTS 150
48+
#define CONFIG_X509_MAX_CA_CERTS 10
4949
#define CONFIG_SSL_MAX_CERTS 1
5050
#undef CONFIG_SSL_CTX_MUTEXING
5151
#undef CONFIG_USE_DEV_URANDOM

ssl/os_port.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ extern "C" {
9898

9999
void ax_wdt_feed();
100100

101+
#ifndef PROGMEM
102+
#define PROGMEM __attribute__((aligned(4))) __attribute__((section(".irom.text")))
103+
#endif
104+
105+
#ifndef WITH_PGM_READ_HELPER
106+
#define ax_array_read_u8(x, y) x[y]
107+
#else
108+
109+
static inline uint8_t pgm_read_byte(const void* addr) {
110+
register uint32_t res;
111+
__asm__("extui %0, %1, 0, 2\n" /* Extract offset within word (in bytes) */
112+
"sub %1, %1, %0\n" /* Subtract offset from addr, yielding an aligned address */
113+
"l32i.n %1, %1, 0x0\n" /* Load word from aligned address */
114+
"slli %0, %0, 3\n" /* Multiply offset by 8, yielding an offset in bits */
115+
"ssr %0\n" /* Prepare to shift by offset (in bits) */
116+
"srl %0, %1\n" /* Shift right; now the requested byte is the first one */
117+
:"=r"(res), "=r"(addr)
118+
:"1"(addr)
119+
:);
120+
return (uint8_t) res; /* This masks the lower byte from the returned word */
121+
}
122+
123+
#define ax_array_read_u8(x, y) pgm_read_byte((x)+(y))
124+
#endif //WITH_PGM_READ_HELPER
125+
101126
#elif defined(WIN32)
102127

103128
/* Windows CE stuff */

ssl/ssl.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,20 @@ EXP_FUNC void STDCALL ssl_ctx_free(SSL_CTX *ssl_ctx);
233233
*/
234234
EXP_FUNC SSL_EXTENSIONS * STDCALL ssl_ext_new();
235235

236+
/**
237+
* @brief Set the host name for SNI extension
238+
* @param ssl_ext pointer returned by ssl_ext_new
239+
* @param host_name pointer to a zero-terminated string containing host name
240+
*/
241+
EXP_FUNC void STDCALL ssl_ext_set_host_name(SSL_EXTENSIONS * ext, const char* host_name);
242+
243+
/**
244+
* @brief Set the maximum fragment size for the fragment size negotiation extension
245+
* @param ssl_ext pointer returned by ssl_ext_new
246+
* @param fragment_size fragment size, allowed values: 2^9, 2^10 ... 2^14
247+
*/
248+
EXP_FUNC void STDCALL ssl_ext_set_max_fragment_size(SSL_EXTENSIONS * ext, unsigned fragment_size);
249+
236250
/**
237251
* @brief Frees SSL extensions structure
238252
*

ssl/tls1.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void DISPLAY_BYTES(SSL *ssl, const char *format,
140140
#endif
141141

142142
/**
143-
* Allocates new SSL extensions structure and returns pointer to it
143+
* Allocate new SSL extensions structure and return pointer to it
144144
*
145145
*/
146146
EXP_FUNC SSL_EXTENSIONS * STDCALL ssl_ext_new()
@@ -153,7 +153,7 @@ EXP_FUNC SSL_EXTENSIONS * STDCALL ssl_ext_new()
153153
}
154154

155155
/**
156-
* Allocates new SSL extensions structure and returns pointer to it
156+
* Free SSL extensions structure
157157
*
158158
*/
159159
EXP_FUNC void STDCALL ssl_ext_free(SSL_EXTENSIONS *ssl_ext)
@@ -168,6 +168,23 @@ EXP_FUNC void STDCALL ssl_ext_free(SSL_EXTENSIONS *ssl_ext)
168168
free(ssl_ext);
169169
}
170170

171+
EXP_FUNC void STDCALL ssl_ext_set_host_name(SSL_EXTENSIONS * ext, const char* host_name)
172+
{
173+
free(ext->host_name);
174+
ext->host_name = NULL;
175+
if (host_name) {
176+
ext->host_name = strdup(host_name);
177+
}
178+
}
179+
180+
/**
181+
* Set the maximum fragment size for the fragment size negotiation extension
182+
*/
183+
EXP_FUNC void STDCALL ssl_ext_set_max_fragment_size(SSL_EXTENSIONS * ext, unsigned fragment_size)
184+
{
185+
ext->max_fragment_size = fragment_size;
186+
}
187+
171188
/**
172189
* Establish a new client/server context.
173190
*/
@@ -2426,10 +2443,6 @@ EXP_FUNC void STDCALL ssl_display_error(int error_code)
24262443
printf("\n");
24272444
}
24282445

2429-
/**
2430-
* Debugging routine to display alerts.
2431-
*/
2432-
24332446
/**
24342447
* Debugging routine to display alerts.
24352448
*/

0 commit comments

Comments
 (0)