Skip to content

Commit 1661676

Browse files
committed
Allocate buffers on stack rather than heap
1 parent 1fe489f commit 1661676

File tree

5 files changed

+44
-63
lines changed

5 files changed

+44
-63
lines changed

groestl.c

+8-13
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,32 @@
99

1010
void groestl_hash(const char* input, char* output, uint32_t len)
1111
{
12-
char* hash1 = (char*) malloc(64);
13-
char* hash2 = (char*) malloc(64);
12+
char hash1[64];
13+
char hash2[64];
1414

1515
sph_groestl512_context ctx_groestl;
1616
sph_groestl512_init(&ctx_groestl);
1717
sph_groestl512(&ctx_groestl, input, len);
18-
sph_groestl512_close(&ctx_groestl, hash1);
18+
sph_groestl512_close(&ctx_groestl, &hash1);
1919

2020
sph_groestl512(&ctx_groestl, hash1, 64);
21-
sph_groestl512_close(&ctx_groestl, hash2);
21+
sph_groestl512_close(&ctx_groestl, &hash2);
2222

23-
memcpy(output, hash2, 32);
24-
25-
free(hash1);
26-
free(hash2);
23+
memcpy(output, &hash2, 32);
2724
}
2825

2926
void groestl_myriad_hash(const char* input, char* output, uint32_t len)
3027
{
31-
char* temp = (char*) malloc(64);
28+
char temp[64];
3229

3330
sph_groestl512_context ctx_groestl;
3431
sph_groestl512_init(&ctx_groestl);
3532
sph_groestl512(&ctx_groestl, input, len);
36-
sph_groestl512_close(&ctx_groestl, temp);
33+
sph_groestl512_close(&ctx_groestl, &temp);
3734

3835
SHA256_CTX ctx_sha256;
3936
SHA256_Init(&ctx_sha256);
40-
SHA256_Update(&ctx_sha256, temp, 64);
37+
SHA256_Update(&ctx_sha256, &temp, 64);
4138
SHA256_Final((unsigned char*) output, &ctx_sha256);
42-
43-
free(temp);
4439
}
4540

hefty1.c

+15-21
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,39 @@ void hefty1_hash(const char* input, char* output, uint32_t len)
1414
sph_groestl512_context ctx_groestl;
1515
sph_blake512_context ctx_blake;
1616

17-
char* hash32_1 = (char*) malloc(32);
18-
char* hash32_2 = (char*) malloc(32);
19-
char* hash64_3 = (char*) malloc(64);
20-
char* hash64_4 = (char*) malloc(64);
21-
char* hash64_5 = (char*) malloc(64);
17+
char hash32_1[32];
18+
char hash32_2[32];
19+
char hash64_3[64];
20+
char hash64_4[64];
21+
char hash64_5[64];
2222

2323
HEFTY1_Init(&ctx_hefty1);
2424
HEFTY1_Update(&ctx_hefty1, (const void*) input, len);
25-
HEFTY1_Final((unsigned char*) hash32_1, &ctx_hefty1); // 1
25+
HEFTY1_Final((unsigned char*) &hash32_1, &ctx_hefty1); // 1
2626

2727
SHA256_Init(&ctx_sha256);
2828
SHA256_Update(&ctx_sha256, (const void*) input, len);
29-
SHA256_Update(&ctx_sha256, (unsigned char*) hash32_1, 32); // 1
30-
SHA256_Final((unsigned char*) hash32_2, &ctx_sha256); // 2
29+
SHA256_Update(&ctx_sha256, (unsigned char*) &hash32_1, 32); // 1
30+
SHA256_Final((unsigned char*) &hash32_2, &ctx_sha256); // 2
3131

3232
sph_keccak512_init(&ctx_keccak);
3333
sph_keccak512(&ctx_keccak, (const void*) input, len);
34-
sph_keccak512(&ctx_keccak, (unsigned char*) hash32_1, 32); //1
35-
sph_keccak512_close(&ctx_keccak, (void*) hash64_3); // 3
34+
sph_keccak512(&ctx_keccak, (unsigned char*) &hash32_1, 32); //1
35+
sph_keccak512_close(&ctx_keccak, (void*) &hash64_3); // 3
3636

3737
sph_groestl512_init(&ctx_groestl);
3838
sph_groestl512(&ctx_groestl, (const void*) input, len);
39-
sph_groestl512(&ctx_groestl, (unsigned char*) hash32_1, 32); // 1
40-
sph_groestl512_close(&ctx_groestl, (void*) hash64_4); // 4
39+
sph_groestl512(&ctx_groestl, (unsigned char*) &hash32_1, 32); // 1
40+
sph_groestl512_close(&ctx_groestl, (void*) &hash64_4); // 4
4141

4242
sph_blake512_init(&ctx_blake);
4343
sph_blake512(&ctx_blake, (const void*) input, len);
44-
sph_blake512(&ctx_blake, (unsigned char*) hash32_1, 32); // 1
45-
sph_blake512_close(&ctx_blake, (void*) hash64_5); // 5
44+
sph_blake512(&ctx_blake, (unsigned char*) &hash32_1, 32); // 1
45+
sph_blake512_close(&ctx_blake, (void*) &hash64_5); // 5
4646

4747
memset(output, 0, 32);
4848

49-
char* hash[4] = { hash32_2, hash64_3, hash64_4, hash64_5 };
49+
char* hash[4] = { &hash32_2, &hash64_3, &hash64_4, &hash64_5 };
5050

5151
uint32_t i;
5252
uint32_t j;
@@ -59,11 +59,5 @@ void hefty1_hash(const char* input, char* output, uint32_t len)
5959
*(output + (OUTPUT_BIT / 8)) |= 0x80 >> (OUTPUT_BIT % 8);
6060
}
6161
}
62-
63-
free(hash32_1);
64-
free(hash32_2);
65-
free(hash64_3);
66-
free(hash64_4);
67-
free(hash64_5);
6862
}
6963

qubit.c

+12-15
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,29 @@ void qubit_hash(const char* input, char* output, uint32_t len)
1414
sph_simd512_context ctx_simd;
1515
sph_echo512_context ctx_echo;
1616

17-
char* hash1 = (char*) malloc(64);
18-
char* hash2 = (char*) malloc(64);
17+
char hash1[64];
18+
char hash2[64];
1919

2020
sph_luffa512_init(&ctx_luffa);
2121
sph_luffa512(&ctx_luffa, (const void*) input, len);
22-
sph_luffa512_close(&ctx_luffa, (void*) hash1); // 1
22+
sph_luffa512_close(&ctx_luffa, (void*) &hash1); // 1
2323

2424
sph_cubehash512_init(&ctx_cubehash);
25-
sph_cubehash512(&ctx_cubehash, (const void*) hash1, 64); // 1
26-
sph_cubehash512_close(&ctx_cubehash, (void*) hash2); // 2
25+
sph_cubehash512(&ctx_cubehash, (const void*) &hash1, 64); // 1
26+
sph_cubehash512_close(&ctx_cubehash, (void*) &hash2); // 2
2727

2828
sph_shavite512_init(&ctx_shavite);
29-
sph_shavite512(&ctx_shavite, (const void*) hash2, 64); // 3
30-
sph_shavite512_close(&ctx_shavite, (void*) hash1); // 4
29+
sph_shavite512(&ctx_shavite, (const void*) &hash2, 64); // 3
30+
sph_shavite512_close(&ctx_shavite, (void*) &hash1); // 4
3131

3232
sph_simd512_init(&ctx_simd);
33-
sph_simd512(&ctx_simd, (const void*) hash1, 64); // 4
34-
sph_simd512_close(&ctx_simd, (void*) hash2); // 5
33+
sph_simd512(&ctx_simd, (const void*) &hash1, 64); // 4
34+
sph_simd512_close(&ctx_simd, (void*) &hash2); // 5
3535

3636
sph_echo512_init(&ctx_echo);
37-
sph_echo512(&ctx_echo, (const void*) hash2, 64); // 5
38-
sph_echo512_close(&ctx_echo, (void*) hash1); // 6
37+
sph_echo512(&ctx_echo, (const void*) &hash2, 64); // 5
38+
sph_echo512_close(&ctx_echo, (void*) &hash1); // 6
3939

40-
memcpy(output, hash1, 32);
41-
42-
free(hash1);
43-
free(hash2);
40+
memcpy(output, &hash1, 32);
4441
}
4542

shavite3.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,18 @@
44

55
void shavite3_hash(const char* input, char* output, uint32_t len)
66
{
7-
char* hash1 = (char*) malloc(64);
8-
char* hash2 = (char*) malloc(64);
7+
char hash1[64];
8+
char hash2[64];
99

1010
sph_shavite512_context ctx_shavite;
1111

1212
sph_shavite512_init(&ctx_shavite);
1313
sph_shavite512(&ctx_shavite, (const void*) input, len);
14-
sph_shavite512_close(&ctx_shavite, (void*) hash1);
14+
sph_shavite512_close(&ctx_shavite, (void*) &hash1);
1515

16-
sph_shavite512(&ctx_shavite, (const void*) hash1, 64);
17-
sph_shavite512_close(&ctx_shavite, (void*) hash2);
16+
sph_shavite512(&ctx_shavite, (const void*) &hash1, 64);
17+
sph_shavite512_close(&ctx_shavite, (void*) &hash2);
1818

19-
memcpy(output, hash2, 32);
20-
21-
free(hash1);
22-
free(hash2);
19+
memcpy(output, &hash2, 32);
2320
}
2421

skein.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@
1111

1212
void skein_hash(const char* input, char* output, uint32_t len)
1313
{
14-
char* temp = (char*) malloc(64);
14+
char temp[64];
1515

1616
sph_skein512_context ctx_skien;
1717
sph_skein512_init(&ctx_skien);
1818
sph_skein512(&ctx_skien, input, len);
19-
sph_skein512_close(&ctx_skien, temp);
19+
sph_skein512_close(&ctx_skien, &temp);
2020

2121
SHA256_CTX ctx_sha256;
2222
SHA256_Init(&ctx_sha256);
23-
SHA256_Update(&ctx_sha256, temp, 64);
23+
SHA256_Update(&ctx_sha256, &temp, 64);
2424
SHA256_Final((unsigned char*) output, &ctx_sha256);
25-
26-
free(temp);
2725
}
2826

0 commit comments

Comments
 (0)