From 1661676bb51980c242cac1feb686a0a62a64a8e1 Mon Sep 17 00:00:00 2001 From: Lucas Jones Date: Mon, 21 Apr 2014 14:31:03 +0100 Subject: [PATCH] Allocate buffers on stack rather than heap --- groestl.c | 21 ++++++++------------- hefty1.c | 36 +++++++++++++++--------------------- qubit.c | 27 ++++++++++++--------------- shavite3.c | 15 ++++++--------- skein.c | 8 +++----- 5 files changed, 44 insertions(+), 63 deletions(-) diff --git a/groestl.c b/groestl.c index de736a0..5be47b8 100644 --- a/groestl.c +++ b/groestl.c @@ -9,37 +9,32 @@ void groestl_hash(const char* input, char* output, uint32_t len) { - char* hash1 = (char*) malloc(64); - char* hash2 = (char*) malloc(64); + char hash1[64]; + char hash2[64]; sph_groestl512_context ctx_groestl; sph_groestl512_init(&ctx_groestl); sph_groestl512(&ctx_groestl, input, len); - sph_groestl512_close(&ctx_groestl, hash1); + sph_groestl512_close(&ctx_groestl, &hash1); sph_groestl512(&ctx_groestl, hash1, 64); - sph_groestl512_close(&ctx_groestl, hash2); + sph_groestl512_close(&ctx_groestl, &hash2); - memcpy(output, hash2, 32); - - free(hash1); - free(hash2); + memcpy(output, &hash2, 32); } void groestl_myriad_hash(const char* input, char* output, uint32_t len) { - char* temp = (char*) malloc(64); + char temp[64]; sph_groestl512_context ctx_groestl; sph_groestl512_init(&ctx_groestl); sph_groestl512(&ctx_groestl, input, len); - sph_groestl512_close(&ctx_groestl, temp); + sph_groestl512_close(&ctx_groestl, &temp); SHA256_CTX ctx_sha256; SHA256_Init(&ctx_sha256); - SHA256_Update(&ctx_sha256, temp, 64); + SHA256_Update(&ctx_sha256, &temp, 64); SHA256_Final((unsigned char*) output, &ctx_sha256); - - free(temp); } diff --git a/hefty1.c b/hefty1.c index d73950a..c620be9 100644 --- a/hefty1.c +++ b/hefty1.c @@ -14,39 +14,39 @@ void hefty1_hash(const char* input, char* output, uint32_t len) sph_groestl512_context ctx_groestl; sph_blake512_context ctx_blake; - char* hash32_1 = (char*) malloc(32); - char* hash32_2 = (char*) malloc(32); - char* hash64_3 = (char*) malloc(64); - char* hash64_4 = (char*) malloc(64); - char* hash64_5 = (char*) malloc(64); + char hash32_1[32]; + char hash32_2[32]; + char hash64_3[64]; + char hash64_4[64]; + char hash64_5[64]; HEFTY1_Init(&ctx_hefty1); HEFTY1_Update(&ctx_hefty1, (const void*) input, len); - HEFTY1_Final((unsigned char*) hash32_1, &ctx_hefty1); // 1 + HEFTY1_Final((unsigned char*) &hash32_1, &ctx_hefty1); // 1 SHA256_Init(&ctx_sha256); SHA256_Update(&ctx_sha256, (const void*) input, len); - SHA256_Update(&ctx_sha256, (unsigned char*) hash32_1, 32); // 1 - SHA256_Final((unsigned char*) hash32_2, &ctx_sha256); // 2 + SHA256_Update(&ctx_sha256, (unsigned char*) &hash32_1, 32); // 1 + SHA256_Final((unsigned char*) &hash32_2, &ctx_sha256); // 2 sph_keccak512_init(&ctx_keccak); sph_keccak512(&ctx_keccak, (const void*) input, len); - sph_keccak512(&ctx_keccak, (unsigned char*) hash32_1, 32); //1 - sph_keccak512_close(&ctx_keccak, (void*) hash64_3); // 3 + sph_keccak512(&ctx_keccak, (unsigned char*) &hash32_1, 32); //1 + sph_keccak512_close(&ctx_keccak, (void*) &hash64_3); // 3 sph_groestl512_init(&ctx_groestl); sph_groestl512(&ctx_groestl, (const void*) input, len); - sph_groestl512(&ctx_groestl, (unsigned char*) hash32_1, 32); // 1 - sph_groestl512_close(&ctx_groestl, (void*) hash64_4); // 4 + sph_groestl512(&ctx_groestl, (unsigned char*) &hash32_1, 32); // 1 + sph_groestl512_close(&ctx_groestl, (void*) &hash64_4); // 4 sph_blake512_init(&ctx_blake); sph_blake512(&ctx_blake, (const void*) input, len); - sph_blake512(&ctx_blake, (unsigned char*) hash32_1, 32); // 1 - sph_blake512_close(&ctx_blake, (void*) hash64_5); // 5 + sph_blake512(&ctx_blake, (unsigned char*) &hash32_1, 32); // 1 + sph_blake512_close(&ctx_blake, (void*) &hash64_5); // 5 memset(output, 0, 32); - char* hash[4] = { hash32_2, hash64_3, hash64_4, hash64_5 }; + char* hash[4] = { &hash32_2, &hash64_3, &hash64_4, &hash64_5 }; uint32_t i; uint32_t j; @@ -59,11 +59,5 @@ void hefty1_hash(const char* input, char* output, uint32_t len) *(output + (OUTPUT_BIT / 8)) |= 0x80 >> (OUTPUT_BIT % 8); } } - - free(hash32_1); - free(hash32_2); - free(hash64_3); - free(hash64_4); - free(hash64_5); } diff --git a/qubit.c b/qubit.c index 68cc882..5f13b76 100644 --- a/qubit.c +++ b/qubit.c @@ -14,32 +14,29 @@ void qubit_hash(const char* input, char* output, uint32_t len) sph_simd512_context ctx_simd; sph_echo512_context ctx_echo; - char* hash1 = (char*) malloc(64); - char* hash2 = (char*) malloc(64); + char hash1[64]; + char hash2[64]; sph_luffa512_init(&ctx_luffa); sph_luffa512(&ctx_luffa, (const void*) input, len); - sph_luffa512_close(&ctx_luffa, (void*) hash1); // 1 + sph_luffa512_close(&ctx_luffa, (void*) &hash1); // 1 sph_cubehash512_init(&ctx_cubehash); - sph_cubehash512(&ctx_cubehash, (const void*) hash1, 64); // 1 - sph_cubehash512_close(&ctx_cubehash, (void*) hash2); // 2 + sph_cubehash512(&ctx_cubehash, (const void*) &hash1, 64); // 1 + sph_cubehash512_close(&ctx_cubehash, (void*) &hash2); // 2 sph_shavite512_init(&ctx_shavite); - sph_shavite512(&ctx_shavite, (const void*) hash2, 64); // 3 - sph_shavite512_close(&ctx_shavite, (void*) hash1); // 4 + sph_shavite512(&ctx_shavite, (const void*) &hash2, 64); // 3 + sph_shavite512_close(&ctx_shavite, (void*) &hash1); // 4 sph_simd512_init(&ctx_simd); - sph_simd512(&ctx_simd, (const void*) hash1, 64); // 4 - sph_simd512_close(&ctx_simd, (void*) hash2); // 5 + sph_simd512(&ctx_simd, (const void*) &hash1, 64); // 4 + sph_simd512_close(&ctx_simd, (void*) &hash2); // 5 sph_echo512_init(&ctx_echo); - sph_echo512(&ctx_echo, (const void*) hash2, 64); // 5 - sph_echo512_close(&ctx_echo, (void*) hash1); // 6 + sph_echo512(&ctx_echo, (const void*) &hash2, 64); // 5 + sph_echo512_close(&ctx_echo, (void*) &hash1); // 6 - memcpy(output, hash1, 32); - - free(hash1); - free(hash2); + memcpy(output, &hash1, 32); } diff --git a/shavite3.c b/shavite3.c index 494ea3b..d3c158e 100644 --- a/shavite3.c +++ b/shavite3.c @@ -4,21 +4,18 @@ void shavite3_hash(const char* input, char* output, uint32_t len) { - char* hash1 = (char*) malloc(64); - char* hash2 = (char*) malloc(64); + char hash1[64]; + char hash2[64]; sph_shavite512_context ctx_shavite; sph_shavite512_init(&ctx_shavite); sph_shavite512(&ctx_shavite, (const void*) input, len); - sph_shavite512_close(&ctx_shavite, (void*) hash1); + sph_shavite512_close(&ctx_shavite, (void*) &hash1); - sph_shavite512(&ctx_shavite, (const void*) hash1, 64); - sph_shavite512_close(&ctx_shavite, (void*) hash2); + sph_shavite512(&ctx_shavite, (const void*) &hash1, 64); + sph_shavite512_close(&ctx_shavite, (void*) &hash2); - memcpy(output, hash2, 32); - - free(hash1); - free(hash2); + memcpy(output, &hash2, 32); } diff --git a/skein.c b/skein.c index 4676a87..a31400b 100644 --- a/skein.c +++ b/skein.c @@ -11,18 +11,16 @@ void skein_hash(const char* input, char* output, uint32_t len) { - char* temp = (char*) malloc(64); + char temp[64]; sph_skein512_context ctx_skien; sph_skein512_init(&ctx_skien); sph_skein512(&ctx_skien, input, len); - sph_skein512_close(&ctx_skien, temp); + sph_skein512_close(&ctx_skien, &temp); SHA256_CTX ctx_sha256; SHA256_Init(&ctx_sha256); - SHA256_Update(&ctx_sha256, temp, 64); + SHA256_Update(&ctx_sha256, &temp, 64); SHA256_Final((unsigned char*) output, &ctx_sha256); - - free(temp); }