Move crypto things to irom0.text segment.

This releases 1168 bytes back to the heap.
This commit is contained in:
Johny Mattsson 2015-06-05 13:03:08 +10:00
parent 223a936779
commit 7867fd083c
4 changed files with 23 additions and 26 deletions

View File

@ -55,7 +55,7 @@ typedef char ensure_int_and_size_t_same[(sizeof(int)==sizeof(size_t)) ? 0 : -1];
ds, \ ds, \
bs } bs }
static const digest_mech_info_t hash_mechs[] = static const digest_mech_info_t hash_mechs[] ICACHE_RODATA_ATTR =
{ {
#ifdef MD2_ENABLE #ifdef MD2_ENABLE
MECH(MD2, _ , MD2_SIZE, 16), MECH(MD2, _ , MD2_SIZE, 16),
@ -71,7 +71,7 @@ static const digest_mech_info_t hash_mechs[] =
#undef MECH #undef MECH
const digest_mech_info_t *crypto_digest_mech (const char *mech) const digest_mech_info_t *ICACHE_FLASH_ATTR crypto_digest_mech (const char *mech)
{ {
if (!mech) if (!mech)
return 0; return 0;
@ -86,28 +86,22 @@ const digest_mech_info_t *crypto_digest_mech (const char *mech)
return 0; return 0;
} }
static const char hex[] = "0123456789abcdef"; const char crypto_hexbytes[] = "0123456789abcdef";
// note: supports in-place encoding // note: supports in-place encoding
void crypto_encode_asciihex (const char *bin, size_t binlen, char *outbuf) void ICACHE_FLASH_ATTR crypto_encode_asciihex (const char *bin, size_t binlen, char *outbuf)
{ {
size_t aidx = binlen * 2 -1; size_t aidx = binlen * 2 -1;
int i; int i;
for (i = binlen -1; i >= 0; --i) for (i = binlen -1; i >= 0; --i)
{ {
outbuf[aidx--] = hex[bin[i] & 0xf]; outbuf[aidx--] = crypto_hexbytes[bin[i] & 0xf];
outbuf[aidx--] = hex[bin[i] >> 4]; outbuf[aidx--] = crypto_hexbytes[bin[i] >> 4];
} }
} }
size_t crypto_digest_size (const char *mech) int ICACHE_FLASH_ATTR crypto_hash (const digest_mech_info_t *mi,
{
const digest_mech_info_t *mi = crypto_digest_mech (mech);
return mi ? mi->digest_size : 0;
}
int crypto_hash (const digest_mech_info_t *mi,
const char *data, size_t data_len, const char *data, size_t data_len,
uint8_t *digest) uint8_t *digest)
{ {
@ -127,7 +121,7 @@ int crypto_hash (const digest_mech_info_t *mi,
} }
int crypto_hmac (const digest_mech_info_t *mi, int ICACHE_FLASH_ATTR crypto_hmac (const digest_mech_info_t *mi,
const char *data, size_t data_len, const char *data, size_t data_len,
const char *key, size_t key_len, const char *key, size_t key_len,
uint8_t *digest) uint8_t *digest)

View File

@ -24,13 +24,14 @@ typedef void (*finalize_ctx_fn)(uint8_t *digest, void *ctx);
*/ */
typedef struct typedef struct
{ {
/* Note: All entries are 32bit to enable placement using ICACHE_RODATA_ATTR.*/
const char * name; const char * name;
create_ctx_fn create; create_ctx_fn create;
update_ctx_fn update; update_ctx_fn update;
finalize_ctx_fn finalize; finalize_ctx_fn finalize;
uint16_t ctx_size; uint32_t ctx_size;
uint16_t digest_size; uint32_t digest_size;
uint16_t block_size; uint32_t block_size;
} digest_mech_info_t; } digest_mech_info_t;
@ -78,4 +79,7 @@ int crypto_hmac (const digest_mech_info_t *mi, const char *data, size_t data_len
void crypto_encode_asciihex (const char *bin, size_t bin_len, char *outbuf); void crypto_encode_asciihex (const char *bin, size_t bin_len, char *outbuf);
/** Text string "0123456789abcdef" */
const char crypto_hexbytes[17];
#endif #endif

View File

@ -172,7 +172,7 @@ void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
/* Hash constant words K for SHA-256: */ /* Hash constant words K for SHA-256: */
const static sha2_word32 K256[64] = { const static sha2_word32 K256[64] ICACHE_RODATA_ATTR = {
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
@ -192,7 +192,7 @@ const static sha2_word32 K256[64] = {
}; };
/* Initial hash value H for SHA-256: */ /* Initial hash value H for SHA-256: */
const static sha2_word32 sha256_initial_hash_value[8] = { const static sha2_word32 sha256_initial_hash_value[8] ICACHE_RODATA_ATTR = {
0x6a09e667UL, 0x6a09e667UL,
0xbb67ae85UL, 0xbb67ae85UL,
0x3c6ef372UL, 0x3c6ef372UL,
@ -204,7 +204,7 @@ const static sha2_word32 sha256_initial_hash_value[8] = {
}; };
/* Hash constant words K for SHA-384 and SHA-512: */ /* Hash constant words K for SHA-384 and SHA-512: */
const static sha2_word64 K512[80] = { const static sha2_word64 K512[80] ICACHE_RODATA_ATTR = {
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
@ -248,7 +248,7 @@ const static sha2_word64 K512[80] = {
}; };
/* Initial hash value H for SHA-384 */ /* Initial hash value H for SHA-384 */
const static sha2_word64 sha384_initial_hash_value[8] = { const static sha2_word64 sha384_initial_hash_value[8] ICACHE_RODATA_ATTR = {
0xcbbb9d5dc1059ed8ULL, 0xcbbb9d5dc1059ed8ULL,
0x629a292a367cd507ULL, 0x629a292a367cd507ULL,
0x9159015a3070dd17ULL, 0x9159015a3070dd17ULL,
@ -260,7 +260,7 @@ const static sha2_word64 sha384_initial_hash_value[8] = {
}; };
/* Initial hash value H for SHA-512 */ /* Initial hash value H for SHA-512 */
const static sha2_word64 sha512_initial_hash_value[8] = { const static sha2_word64 sha512_initial_hash_value[8] ICACHE_RODATA_ATTR = {
0x6a09e667f3bcc908ULL, 0x6a09e667f3bcc908ULL,
0xbb67ae8584caa73bULL, 0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL, 0x3c6ef372fe94f82bULL,

View File

@ -65,7 +65,6 @@ static int crypto_base64_encode( lua_State* L )
return 1; return 1;
} }
static const char* byteshex = "0123456789abcdef";
/** /**
* encoded = crypto.toHex(raw) * encoded = crypto.toHex(raw)
* *
@ -78,8 +77,8 @@ static int crypto_hex_encode( lua_State* L)
char* out = (char*)c_malloc(len * 2); char* out = (char*)c_malloc(len * 2);
int i, j = 0; int i, j = 0;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
out[j++] = byteshex[msg[i] >> 4]; out[j++] = crypto_hexbytes[msg[i] >> 4];
out[j++] = byteshex[msg[i] & 0xf]; out[j++] = crypto_hexbytes[msg[i] & 0xf];
} }
lua_pushlstring(L, out, len*2); lua_pushlstring(L, out, len*2);
c_free(out); c_free(out);