1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-08-12 20:49:16 +03:00

decrease RAM usage using PROGMEM

This commit is contained in:
AndreiD
2017-02-19 14:53:17 +02:00
committed by Ivan Grokhotkov
parent 8afe55267a
commit feed1ca219
9 changed files with 103 additions and 35 deletions

View File

@@ -4,8 +4,6 @@ AR := $(TOOLCHAIN_PREFIX)ar
LD := $(TOOLCHAIN_PREFIX)gcc LD := $(TOOLCHAIN_PREFIX)gcc
OBJCOPY := $(TOOLCHAIN_PREFIX)objcopy OBJCOPY := $(TOOLCHAIN_PREFIX)objcopy
MFORCE32 := $(shell $(CC) --help=target | grep mforce-l32)
XTENSA_LIBS ?= $(shell $(CC) -print-sysroot) XTENSA_LIBS ?= $(shell $(CC) -print-sysroot)
@@ -43,13 +41,17 @@ LDFLAGS += -L$(XTENSA_LIBS)/lib \
CFLAGS+=-std=c99 -DESP8266 CFLAGS+=-std=c99 -DESP8266
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 CFLAGS += -Wall -Os -g -O2 -Wpointer-arith -Wl,-EL -nostdlib -mlongcalls -mno-text-section-literals -D__ets__ -DICACHE_FLASH
MFORCE32 := $(shell $(CC) --help=target | grep mforce-l32)
ifneq ($(MFORCE32),) ifneq ($(MFORCE32),)
# Your compiler supports the -mforce-l32 flag which means that # If the compiler supports the -mforce-l32 flag, the compiler will generate correct code for loading
# constants can be stored in flash (program) memory instead of SRAM. # 16- and 8-bit constants from program memory. So in the code we can directly access the arrays
# See: https://www.arduino.cc/en/Reference/PROGMEM # placed into program memory.
CFLAGS += -DPROGMEM="__attribute__((aligned(4))) __attribute__((section(\".irom.text\")))" -mforce-l32 CFLAGS += -mforce-l32
else
# Otherwise we need to use a helper function to load 16- and 8-bit constants from program memory.
CFLAGS += -DWITH_PGM_READ_HELPER
endif endif
BIN_DIR := bin BIN_DIR := bin

View File

@@ -220,20 +220,20 @@ void AES_set_key(AES_CTX *ctx, const uint8_t *key,
if ((i % words) == 0) if ((i % words) == 0)
{ {
tmp2 =(uint32_t)aes_sbox[(tmp )&0xff]<< 8; tmp2 =(uint32_t)(ax_array_read_u8(aes_sbox, (tmp )&0xff))<< 8;
tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<<16; tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>> 8)&0xff))<<16;
tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<24; tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>16)&0xff))<<24;
tmp2|=(uint32_t)aes_sbox[(tmp>>24) ]; tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>24)));
tmp=tmp2^(((unsigned int)*ip)<<24); tmp=tmp2^(((unsigned int)*ip)<<24);
ip++; ip++;
} }
if ((words == 8) && ((i % words) == 4)) if ((words == 8) && ((i % words) == 4))
{ {
tmp2 =(uint32_t)aes_sbox[(tmp )&0xff] ; tmp2 =(uint32_t)(ax_array_read_u8(aes_sbox, (tmp )&0xff)) ;
tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<< 8; tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>> 8)&0xff))<< 8;
tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<16; tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>16)&0xff))<<16;
tmp2|=(uint32_t)aes_sbox[(tmp>>24) ]<<24; tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>24) ))<<24;
tmp=tmp2; tmp=tmp2;
} }
@@ -369,10 +369,10 @@ static void AES_encrypt(const AES_CTX *ctx, uint32_t *data)
/* Perform ByteSub and ShiftRow operations together */ /* Perform ByteSub and ShiftRow operations together */
for (row = 0; row < 4; row++) for (row = 0; row < 4; row++)
{ {
a0 = (uint32_t)aes_sbox[(data[row%4]>>24)&0xFF]; a0 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[row%4]>>24)&0xFF));
a1 = (uint32_t)aes_sbox[(data[(row+1)%4]>>16)&0xFF]; a1 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[(row+1)%4]>>16)&0xFF));
a2 = (uint32_t)aes_sbox[(data[(row+2)%4]>>8)&0xFF]; a2 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[(row+2)%4]>>8)&0xFF));
a3 = (uint32_t)aes_sbox[(data[(row+3)%4])&0xFF]; a3 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[(row+3)%4])&0xFF));
/* Perform MixColumn iff not last round */ /* Perform MixColumn iff not last round */
if (curr_rnd < (rounds - 1)) if (curr_rnd < (rounds - 1))
@@ -417,10 +417,10 @@ static void AES_decrypt(const AES_CTX *ctx, uint32_t *data)
/* Perform ByteSub and ShiftRow operations together */ /* Perform ByteSub and ShiftRow operations together */
for (row = 4; row > 0; row--) for (row = 4; row > 0; row--)
{ {
a0 = aes_isbox[(data[(row+3)%4]>>24)&0xFF]; a0 = ax_array_read_u8(aes_isbox, (data[(row+3)%4]>>24)&0xFF);
a1 = aes_isbox[(data[(row+2)%4]>>16)&0xFF]; a1 = ax_array_read_u8(aes_isbox, (data[(row+2)%4]>>16)&0xFF);
a2 = aes_isbox[(data[(row+1)%4]>>8)&0xFF]; a2 = ax_array_read_u8(aes_isbox, (data[(row+1)%4]>>8)&0xFF);
a3 = aes_isbox[(data[row%4])&0xFF]; a3 = ax_array_read_u8(aes_isbox, (data[row%4])&0xFF);
/* Perform MixColumn iff not last round */ /* Perform MixColumn iff not last round */
if (curr_rnd<(rounds-1)) if (curr_rnd<(rounds-1))

View File

@@ -334,7 +334,7 @@ EXP_FUNC int STDCALL base64_decode(const char *in, int len,
g = 3; g = 3;
for (x = y = z = t = 0; x < len; x++) for (x = y = z = t = 0; x < len; x++)
{ {
if ((c = map[in[x]&0x7F]) == 0xff) if ((c = ax_array_read_u8(map, in[x]&0x7F)) == 0xff)
continue; continue;
if (c == 254) /* this is the end... */ if (c == 254) /* this is the end... */

View File

@@ -60,7 +60,7 @@ static void MD5Transform(uint32_t state[4], const uint8_t block[64]);
static void Encode(uint8_t *output, uint32_t *input, uint32_t len); static void Encode(uint8_t *output, uint32_t *input, uint32_t len);
static void Decode(uint32_t *output, const uint8_t *input, uint32_t len); static void Decode(uint32_t *output, const uint8_t *input, uint32_t len);
static const uint8_t PADDING[64] = static const uint8_t PADDING[64] PROGMEM =
{ {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -158,7 +158,10 @@ EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *ctx)
{ {
uint8_t bits[8]; uint8_t bits[8];
uint32_t x, padLen; uint32_t x, padLen;
#ifdef WITH_PGM_READ_HELPER
uint8_t PADDING_stack[64];
memcpy(PADDING_stack, PADDING, 64);
#endif
/* Save number of bits */ /* Save number of bits */
Encode(bits, ctx->count, 8); Encode(bits, ctx->count, 8);
@@ -166,7 +169,11 @@ EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *ctx)
*/ */
x = (uint32_t)((ctx->count[0] >> 3) & 0x3f); x = (uint32_t)((ctx->count[0] >> 3) & 0x3f);
padLen = (x < 56) ? (56 - x) : (120 - x); padLen = (x < 56) ? (56 - x) : (120 - x);
#ifdef WITH_PGM_READ_HELPER
MD5_Update(ctx, PADDING_stack, padLen);
#else
MD5_Update(ctx, PADDING, padLen); MD5_Update(ctx, PADDING, padLen);
#endif
/* Append length (before padding) */ /* Append length (before padding) */
MD5_Update(ctx, bits, 8); MD5_Update(ctx, bits, 8);

View File

@@ -48,7 +48,7 @@
(b)[(i) + 3] = (uint8_t) ((n) ); \ (b)[(i) + 3] = (uint8_t) ((n) ); \
} }
static const uint8_t sha256_padding[64] = static const uint8_t sha256_padding[64] PROGMEM =
{ {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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)
uint32_t last, padn; uint32_t last, padn;
uint32_t high, low; uint32_t high, low;
uint8_t msglen[8]; uint8_t msglen[8];
#ifdef WITH_PGM_READ_HELPER
uint8_t sha256_padding_ram[64];
memcpy(sha256_padding_ram, sha256_padding, 64);
#endif
high = (ctx->total[0] >> 29) high = (ctx->total[0] >> 29)
| (ctx->total[1] << 3); | (ctx->total[1] << 3);
@@ -259,8 +263,11 @@ void SHA256_Final(uint8_t *digest, SHA256_CTX *ctx)
last = ctx->total[0] & 0x3F; last = ctx->total[0] & 0x3F;
padn = (last < 56) ? (56 - last) : (120 - last); padn = (last < 56) ? (56 - last) : (120 - last);
#ifdef WITH_PGM_READ_HELPER
SHA256_Update(ctx, sha256_padding_ram, padn);
#else
SHA256_Update(ctx, sha256_padding, padn); SHA256_Update(ctx, sha256_padding, padn);
#endif
SHA256_Update(ctx, msglen, 8); SHA256_Update(ctx, msglen, 8);
PUT_UINT32(ctx->state[0], digest, 0); PUT_UINT32(ctx->state[0], digest, 0);

View File

@@ -45,7 +45,7 @@
#define CONFIG_SSL_HAS_PEM 1 #define CONFIG_SSL_HAS_PEM 1
#undef CONFIG_SSL_USE_PKCS12 #undef CONFIG_SSL_USE_PKCS12
#define CONFIG_SSL_EXPIRY_TIME 24 #define CONFIG_SSL_EXPIRY_TIME 24
#define CONFIG_X509_MAX_CA_CERTS 150 #define CONFIG_X509_MAX_CA_CERTS 10
#define CONFIG_SSL_MAX_CERTS 1 #define CONFIG_SSL_MAX_CERTS 1
#undef CONFIG_SSL_CTX_MUTEXING #undef CONFIG_SSL_CTX_MUTEXING
#undef CONFIG_USE_DEV_URANDOM #undef CONFIG_USE_DEV_URANDOM

View File

@@ -98,6 +98,31 @@ extern "C" {
void ax_wdt_feed(); void ax_wdt_feed();
#ifndef PROGMEM
#define PROGMEM __attribute__((aligned(4))) __attribute__((section(".irom.text")))
#endif
#ifndef WITH_PGM_READ_HELPER
#define ax_array_read_u8(x, y) x[y]
#else
static inline uint8_t pgm_read_byte(const void* addr) {
register uint32_t res;
__asm__("extui %0, %1, 0, 2\n" /* Extract offset within word (in bytes) */
"sub %1, %1, %0\n" /* Subtract offset from addr, yielding an aligned address */
"l32i.n %1, %1, 0x0\n" /* Load word from aligned address */
"slli %0, %0, 3\n" /* Multiply offset by 8, yielding an offset in bits */
"ssr %0\n" /* Prepare to shift by offset (in bits) */
"srl %0, %1\n" /* Shift right; now the requested byte is the first one */
:"=r"(res), "=r"(addr)
:"1"(addr)
:);
return (uint8_t) res; /* This masks the lower byte from the returned word */
}
#define ax_array_read_u8(x, y) pgm_read_byte((x)+(y))
#endif //WITH_PGM_READ_HELPER
#elif defined(WIN32) #elif defined(WIN32)
/* Windows CE stuff */ /* Windows CE stuff */

View File

@@ -233,6 +233,20 @@ EXP_FUNC void STDCALL ssl_ctx_free(SSL_CTX *ssl_ctx);
*/ */
EXP_FUNC SSL_EXTENSIONS * STDCALL ssl_ext_new(); EXP_FUNC SSL_EXTENSIONS * STDCALL ssl_ext_new();
/**
* @brief Set the host name for SNI extension
* @param ssl_ext pointer returned by ssl_ext_new
* @param host_name pointer to a zero-terminated string containing host name
*/
EXP_FUNC void STDCALL ssl_ext_set_host_name(SSL_EXTENSIONS * ext, const char* host_name);
/**
* @brief Set the maximum fragment size for the fragment size negotiation extension
* @param ssl_ext pointer returned by ssl_ext_new
* @param fragment_size fragment size, allowed values: 2^9, 2^10 ... 2^14
*/
EXP_FUNC void STDCALL ssl_ext_set_max_fragment_size(SSL_EXTENSIONS * ext, unsigned fragment_size);
/** /**
* @brief Frees SSL extensions structure * @brief Frees SSL extensions structure
* *

View File

@@ -140,7 +140,7 @@ void DISPLAY_BYTES(SSL *ssl, const char *format,
#endif #endif
/** /**
* Allocates new SSL extensions structure and returns pointer to it * Allocate new SSL extensions structure and return pointer to it
* *
*/ */
EXP_FUNC SSL_EXTENSIONS * STDCALL ssl_ext_new() EXP_FUNC SSL_EXTENSIONS * STDCALL ssl_ext_new()
@@ -153,7 +153,7 @@ EXP_FUNC SSL_EXTENSIONS * STDCALL ssl_ext_new()
} }
/** /**
* Allocates new SSL extensions structure and returns pointer to it * Free SSL extensions structure
* *
*/ */
EXP_FUNC void STDCALL ssl_ext_free(SSL_EXTENSIONS *ssl_ext) 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)
free(ssl_ext); free(ssl_ext);
} }
EXP_FUNC void STDCALL ssl_ext_set_host_name(SSL_EXTENSIONS * ext, const char* host_name)
{
free(ext->host_name);
ext->host_name = NULL;
if (host_name) {
ext->host_name = strdup(host_name);
}
}
/**
* Set the maximum fragment size for the fragment size negotiation extension
*/
EXP_FUNC void STDCALL ssl_ext_set_max_fragment_size(SSL_EXTENSIONS * ext, unsigned fragment_size)
{
ext->max_fragment_size = fragment_size;
}
/** /**
* Establish a new client/server context. * Establish a new client/server context.
*/ */
@@ -2426,10 +2443,6 @@ EXP_FUNC void STDCALL ssl_display_error(int error_code)
printf("\n"); printf("\n");
} }
/**
* Debugging routine to display alerts.
*/
/** /**
* Debugging routine to display alerts. * Debugging routine to display alerts.
*/ */