Allocate buffers on stack rather than heap

master
Lucas Jones 2014-04-21 14:31:03 +01:00
parent 1fe489fb3e
commit 1661676bb5
5 changed files with 44 additions and 63 deletions

View File

@ -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);
}

View File

@ -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);
}

27
qubit.c
View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}