mirror of
https://github.com/esp8266/Arduino.git
synced 2025-08-14 08:03:09 +03:00
decrease RAM usage using PROGMEM
This commit is contained in:
32
crypto/aes.c
32
crypto/aes.c
@@ -220,20 +220,20 @@ void AES_set_key(AES_CTX *ctx, const uint8_t *key,
|
||||
|
||||
if ((i % words) == 0)
|
||||
{
|
||||
tmp2 =(uint32_t)aes_sbox[(tmp )&0xff]<< 8;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<<16;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<24;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>>24) ];
|
||||
tmp2 =(uint32_t)(ax_array_read_u8(aes_sbox, (tmp )&0xff))<< 8;
|
||||
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>> 8)&0xff))<<16;
|
||||
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>16)&0xff))<<24;
|
||||
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>24)));
|
||||
tmp=tmp2^(((unsigned int)*ip)<<24);
|
||||
ip++;
|
||||
}
|
||||
|
||||
if ((words == 8) && ((i % words) == 4))
|
||||
{
|
||||
tmp2 =(uint32_t)aes_sbox[(tmp )&0xff] ;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<< 8;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<16;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>>24) ]<<24;
|
||||
tmp2 =(uint32_t)(ax_array_read_u8(aes_sbox, (tmp )&0xff)) ;
|
||||
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>> 8)&0xff))<< 8;
|
||||
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>16)&0xff))<<16;
|
||||
tmp2|=(uint32_t)(ax_array_read_u8(aes_sbox, (tmp>>24) ))<<24;
|
||||
tmp=tmp2;
|
||||
}
|
||||
|
||||
@@ -369,10 +369,10 @@ static void AES_encrypt(const AES_CTX *ctx, uint32_t *data)
|
||||
/* Perform ByteSub and ShiftRow operations together */
|
||||
for (row = 0; row < 4; row++)
|
||||
{
|
||||
a0 = (uint32_t)aes_sbox[(data[row%4]>>24)&0xFF];
|
||||
a1 = (uint32_t)aes_sbox[(data[(row+1)%4]>>16)&0xFF];
|
||||
a2 = (uint32_t)aes_sbox[(data[(row+2)%4]>>8)&0xFF];
|
||||
a3 = (uint32_t)aes_sbox[(data[(row+3)%4])&0xFF];
|
||||
a0 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[row%4]>>24)&0xFF));
|
||||
a1 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[(row+1)%4]>>16)&0xFF));
|
||||
a2 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[(row+2)%4]>>8)&0xFF));
|
||||
a3 = (uint32_t)(ax_array_read_u8(aes_sbox, (data[(row+3)%4])&0xFF));
|
||||
|
||||
/* Perform MixColumn iff not last round */
|
||||
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 */
|
||||
for (row = 4; row > 0; row--)
|
||||
{
|
||||
a0 = aes_isbox[(data[(row+3)%4]>>24)&0xFF];
|
||||
a1 = aes_isbox[(data[(row+2)%4]>>16)&0xFF];
|
||||
a2 = aes_isbox[(data[(row+1)%4]>>8)&0xFF];
|
||||
a3 = aes_isbox[(data[row%4])&0xFF];
|
||||
a0 = ax_array_read_u8(aes_isbox, (data[(row+3)%4]>>24)&0xFF);
|
||||
a1 = ax_array_read_u8(aes_isbox, (data[(row+2)%4]>>16)&0xFF);
|
||||
a2 = ax_array_read_u8(aes_isbox, (data[(row+1)%4]>>8)&0xFF);
|
||||
a3 = ax_array_read_u8(aes_isbox, (data[row%4])&0xFF);
|
||||
|
||||
/* Perform MixColumn iff not last round */
|
||||
if (curr_rnd<(rounds-1))
|
||||
|
@@ -334,7 +334,7 @@ EXP_FUNC int STDCALL base64_decode(const char *in, int len,
|
||||
g = 3;
|
||||
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;
|
||||
|
||||
if (c == 254) /* this is the end... */
|
||||
|
11
crypto/md5.c
11
crypto/md5.c
@@ -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 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,
|
||||
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];
|
||||
uint32_t x, padLen;
|
||||
|
||||
#ifdef WITH_PGM_READ_HELPER
|
||||
uint8_t PADDING_stack[64];
|
||||
memcpy(PADDING_stack, PADDING, 64);
|
||||
#endif
|
||||
/* Save number of bits */
|
||||
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);
|
||||
padLen = (x < 56) ? (56 - x) : (120 - x);
|
||||
#ifdef WITH_PGM_READ_HELPER
|
||||
MD5_Update(ctx, PADDING_stack, padLen);
|
||||
#else
|
||||
MD5_Update(ctx, PADDING, padLen);
|
||||
#endif
|
||||
|
||||
/* Append length (before padding) */
|
||||
MD5_Update(ctx, bits, 8);
|
||||
|
@@ -48,7 +48,7 @@
|
||||
(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,
|
||||
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 high, low;
|
||||
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)
|
||||
| (ctx->total[1] << 3);
|
||||
@@ -259,8 +263,11 @@ void SHA256_Final(uint8_t *digest, SHA256_CTX *ctx)
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
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);
|
||||
#endif
|
||||
SHA256_Update(ctx, msglen, 8);
|
||||
|
||||
PUT_UINT32(ctx->state[0], digest, 0);
|
||||
|
Reference in New Issue
Block a user