Skip to content

Commit fdad317

Browse files
onvej-slprusnak
authored andcommitted
crypto: explicitly initialize variables
1 parent fa9d349 commit fdad317

38 files changed

+397
-397
lines changed

crypto/address.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ void ethereum_address_checksum(const uint8_t *addr, char *address, bool rskip60,
6666
}
6767
address[40] = 0;
6868

69-
SHA3_CTX ctx;
70-
uint8_t hash[32];
69+
SHA3_CTX ctx = {0};
70+
uint8_t hash[32] = {0};
7171
keccak_256_Init(&ctx);
7272
if (rskip60) {
73-
char prefix[16];
73+
char prefix[16] = {0};
7474
int prefix_size = bn_format_uint64(chain_id, NULL, "0x", 0, 0, false,
7575
prefix, sizeof(prefix));
7676
keccak_Update(&ctx, (const uint8_t *)prefix, prefix_size);

crypto/aes/aes_modes.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ aligned_array(unsigned long, dec_hybrid_table, 12, 16) = NEH_DEC_HYBRID_DATA;
108108

109109
AES_RETURN aes_test_alignment_detection(unsigned int n) /* 4 <= n <= 16 */
110110
{ uint8_t p[16];
111-
uint32_t i, count_eq = 0, count_neq = 0;
111+
uint32_t i = 0, count_eq = 0, count_neq = 0;
112112

113113
if(n < 4 || n > 16)
114114
return EXIT_FAILURE;
@@ -156,7 +156,7 @@ AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
156156
}
157157
else
158158
{ aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
159-
uint8_t *ip, *op;
159+
uint8_t *ip = NULL, *op = NULL;
160160

161161
while(nb)
162162
{
@@ -218,7 +218,7 @@ AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
218218
}
219219
else
220220
{ aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
221-
uint8_t *ip, *op;
221+
uint8_t *ip = NULL, *op = NULL;
222222

223223
while(nb)
224224
{
@@ -287,7 +287,7 @@ AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
287287
}
288288
else
289289
{ aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
290-
uint8_t *ip, *op;
290+
uint8_t *ip = NULL, *op = NULL;
291291

292292
while(nb)
293293
{
@@ -385,7 +385,7 @@ AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
385385
}
386386
else
387387
{ aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
388-
uint8_t *ip, *op;
388+
uint8_t *ip = NULL, *op = NULL;
389389

390390
while(nb)
391391
{
@@ -497,7 +497,7 @@ AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
497497
}
498498
else /* input, output or both are unaligned */
499499
{ aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
500-
uint8_t *ip, *op;
500+
uint8_t *ip = NULL, *op = NULL;
501501

502502
while(nb)
503503
{
@@ -625,7 +625,7 @@ AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
625625
}
626626
else /* input, output or both are unaligned */
627627
{ aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
628-
uint8_t *ip, *op;
628+
uint8_t *ip = NULL, *op = NULL;
629629

630630
while(nb)
631631
{
@@ -763,7 +763,7 @@ AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
763763
}
764764
else /* input, output or both are unaligned */
765765
{ aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
766-
uint8_t *ip, *op;
766+
uint8_t *ip = NULL, *op = NULL;
767767

768768
while(nb)
769769
{
@@ -850,14 +850,14 @@ AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
850850
AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
851851
int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx ctx[1])
852852
{ unsigned char *ip;
853-
int i, blen, b_pos = (int)(ctx->inf.b[2]);
853+
int i = 0, blen = 0, b_pos = (int)(ctx->inf.b[2]);
854854

855855
#if defined( USE_VIA_ACE_IF_PRESENT )
856856
aligned_auto(uint8_t, buf, BFR_LENGTH, 16);
857857
if(ctx->inf.b[1] == 0xff && ALIGN_OFFSET( ctx, 16 ))
858858
return EXIT_FAILURE;
859859
#else
860-
uint8_t buf[BFR_LENGTH];
860+
uint8_t buf[BFR_LENGTH] = {0};
861861
#endif
862862

863863
if(b_pos)

crypto/aes/aescrypt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ extern "C"
9696

9797
AES_RETURN aes_xi(encrypt)(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1])
9898
{ uint32_t locals(b0, b1);
99-
const uint32_t *kp;
99+
const uint32_t *kp = NULL;
100100
#if defined( dec_fmvars )
101101
dec_fmvars; /* declare variables for fwd_mcol() if needed */
102102
#endif
@@ -234,7 +234,7 @@ AES_RETURN aes_xi(decrypt)(const unsigned char *in, unsigned char *out, const ae
234234
#if defined( dec_imvars )
235235
dec_imvars; /* declare variables for inv_mcol() if needed */
236236
#endif
237-
const uint32_t *kp;
237+
const uint32_t *kp = NULL;
238238

239239
if(cx->inf.b[0] != 10 * AES_BLOCK_SIZE && cx->inf.b[0] != 12 * AES_BLOCK_SIZE && cx->inf.b[0] != 14 * AES_BLOCK_SIZE)
240240
return EXIT_FAILURE;

crypto/aes/aestab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ AES_RETURN aes_init(void)
272272

273273
#if defined(FF_TABLES)
274274

275-
uint8_t pow[512], log[256];
275+
uint8_t pow[512] = {0}, log[256] = {0};
276276

277277
if(init)
278278
return EXIT_SUCCESS;

crypto/base32.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void base32_encode_unsafe(const uint8_t *in, size_t inlen, uint8_t *out) {
7777
uint8_t remainder = inlen % 5;
7878
size_t limit = inlen - remainder;
7979

80-
size_t i, j;
80+
size_t i = 0, j = 0;
8181
for (i = 0, j = 0; i < limit; i += 5, j += 8) {
8282
base32_5to8(&in[i], 5, &out[j]);
8383
}
@@ -90,7 +90,7 @@ bool base32_decode_unsafe(const uint8_t *in, size_t inlen, uint8_t *out,
9090
uint8_t remainder = inlen % 8;
9191
size_t limit = inlen - remainder;
9292

93-
size_t i, j;
93+
size_t i = 0, j = 0;
9494
for (i = 0, j = 0; i < limit; i += 8, j += 5) {
9595
if (!base32_8to5(&in[i], 8, &out[j], alphabet)) {
9696
return false;

crypto/base58.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) {
5858
size_t outisz =
5959
(binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t);
6060
b58_almostmaxint_t outi[outisz];
61-
b58_maxint_t t;
62-
b58_almostmaxint_t c;
63-
size_t i, j;
61+
b58_maxint_t t = 0;
62+
b58_almostmaxint_t c = 0;
63+
size_t i = 0, j = 0;
6464
uint8_t bytesleft = binsz % sizeof(b58_almostmaxint_t);
6565
b58_almostmaxint_t zeromask =
6666
bytesleft ? (b58_almostmaxint_mask << (bytesleft * 8)) : 0;
@@ -128,9 +128,9 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) {
128128

129129
int b58check(const void *bin, size_t binsz, HasherType hasher_type,
130130
const char *base58str) {
131-
unsigned char buf[32];
131+
unsigned char buf[32] = {0};
132132
const uint8_t *binc = bin;
133-
unsigned i;
133+
unsigned i = 0;
134134
if (binsz < 4) return -4;
135135
hasher_Raw(hasher_type, bin, binsz - 4, buf);
136136
if (memcmp(&binc[binsz - 4], buf, 4)) return -1;
@@ -146,9 +146,9 @@ int b58check(const void *bin, size_t binsz, HasherType hasher_type,
146146

147147
bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) {
148148
const uint8_t *bin = data;
149-
int carry;
150-
size_t i, j, high, zcount = 0;
151-
size_t size;
149+
int carry = 0;
150+
size_t i = 0, j = 0, high = 0, zcount = 0;
151+
size_t size = 0;
152152

153153
while (zcount < binsz && !bin[zcount]) ++zcount;
154154

@@ -219,9 +219,9 @@ int base58_decode_check(const char *str, HasherType hasher_type, uint8_t *data,
219219

220220
#if USE_GRAPHENE
221221
int b58gphcheck(const void *bin, size_t binsz, const char *base58str) {
222-
unsigned char buf[32];
222+
unsigned char buf[32] = {0};
223223
const uint8_t *binc = bin;
224-
unsigned i;
224+
unsigned i = 0;
225225
if (binsz < 4) return -4;
226226
ripemd160(bin, binsz - 4, buf); // No double SHA256, but a single RIPEMD160
227227
if (memcmp(&binc[binsz - 4], buf, 4)) return -1;

crypto/bip32.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ int hdnode_from_xprv(uint32_t depth, uint32_t child_num,
127127
if (info == 0) {
128128
failed = true;
129129
} else if (info->params) {
130-
bignum256 a;
130+
bignum256 a = {0};
131131
bn_read_be(private_key, &a);
132132
if (bn_is_zero(&a)) { // == 0
133133
failed = true;
@@ -170,7 +170,7 @@ int hdnode_from_seed(const uint8_t *seed, int seed_len, const char *curve,
170170
hmac_sha512_Final(&ctx, I);
171171

172172
if (out->curve->params) {
173-
bignum256 a;
173+
bignum256 a = {0};
174174
while (true) {
175175
bn_read_be(I, &a);
176176
if (!bn_is_zero(&a) // != 0
@@ -192,8 +192,8 @@ int hdnode_from_seed(const uint8_t *seed, int seed_len, const char *curve,
192192
}
193193

194194
uint32_t hdnode_fingerprint(HDNode *node) {
195-
uint8_t digest[32];
196-
uint32_t fingerprint;
195+
uint8_t digest[32] = {0};
196+
uint32_t fingerprint = 0;
197197

198198
hdnode_fill_public_key(node);
199199
hasher_Raw(node->curve->hasher_pubkey, node->public_key, 33, digest);
@@ -422,9 +422,9 @@ int hdnode_from_entropy_cardano_icarus(const uint8_t *pass, int pass_len,
422422
int hdnode_public_ckd_cp(const ecdsa_curve *curve, const curve_point *parent,
423423
const uint8_t *parent_chain_code, uint32_t i,
424424
curve_point *child, uint8_t *child_chain_code) {
425-
uint8_t data[1 + 32 + 4];
426-
uint8_t I[32 + 32];
427-
bignum256 c;
425+
uint8_t data[(1 + 32) + 4] = {0};
426+
uint8_t I[32 + 32] = {0};
427+
bignum256 c = {0};
428428

429429
if (i & 0x80000000) { // private derivation
430430
return 0;
@@ -459,7 +459,7 @@ int hdnode_public_ckd_cp(const ecdsa_curve *curve, const curve_point *parent,
459459
}
460460

461461
int hdnode_public_ckd(HDNode *inout, uint32_t i) {
462-
curve_point parent, child;
462+
curve_point parent = {0}, child = {0};
463463

464464
if (!ecdsa_read_pubkey(inout->curve->params, inout->public_key, &parent)) {
465465
return 0;
@@ -487,8 +487,8 @@ void hdnode_public_ckd_address_optimized(const curve_point *pub,
487487
HasherType hasher_pubkey,
488488
HasherType hasher_base58, char *addr,
489489
int addrsize, int addrformat) {
490-
uint8_t child_pubkey[33];
491-
curve_point b;
490+
uint8_t child_pubkey[33] = {0};
491+
curve_point b = {0};
492492

493493
hdnode_public_ckd_cp(&secp256k1, pub, chain_code, i, &b, NULL);
494494
child_pubkey[0] = 0x02 | (b.y.val[0] & 0x01);
@@ -544,7 +544,7 @@ int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count,
544544
private_ckd_cache_root_set = true;
545545
} else {
546546
// try to find parent
547-
int j;
547+
int j = 0;
548548
for (j = 0; j < BIP32_CACHE_SIZE; j++) {
549549
if (private_ckd_cache[j].set &&
550550
private_ckd_cache[j].depth == i_count - 1 &&
@@ -560,7 +560,7 @@ int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count,
560560

561561
// else derive parent
562562
if (!found) {
563-
size_t k;
563+
size_t k = 0;
564564
for (k = 0; k < i_count - 1; k++) {
565565
if (hdnode_private_ckd(inout, i[k]) == 0) return 0;
566566
}
@@ -633,8 +633,8 @@ void hdnode_fill_public_key(HDNode *node) {
633633

634634
#if USE_ETHEREUM
635635
int hdnode_get_ethereum_pubkeyhash(const HDNode *node, uint8_t *pubkeyhash) {
636-
uint8_t buf[65];
637-
SHA3_CTX ctx;
636+
uint8_t buf[65] = {0};
637+
SHA3_CTX ctx = {0};
638638

639639
/* get uncompressed public key */
640640
ecdsa_get_public_key65(node->curve->params, node->private_key, buf);
@@ -687,7 +687,7 @@ int hdnode_get_nem_shared_key(const HDNode *node,
687687
int hdnode_nem_encrypt(const HDNode *node, const ed25519_public_key public_key,
688688
const uint8_t *iv_immut, const uint8_t *salt,
689689
const uint8_t *payload, size_t size, uint8_t *buffer) {
690-
uint8_t last_block[AES_BLOCK_SIZE];
690+
uint8_t last_block[AES_BLOCK_SIZE] = {0};
691691
uint8_t remainder = size % AES_BLOCK_SIZE;
692692

693693
// Round down to last whole block
@@ -699,15 +699,15 @@ int hdnode_nem_encrypt(const HDNode *node, const ed25519_public_key public_key,
699699
AES_BLOCK_SIZE - remainder);
700700

701701
// the IV gets mutated, so we make a copy not to touch the original
702-
uint8_t iv[AES_BLOCK_SIZE];
702+
uint8_t iv[AES_BLOCK_SIZE] = {0};
703703
memcpy(iv, iv_immut, AES_BLOCK_SIZE);
704704

705-
uint8_t shared_key[SHA3_256_DIGEST_LENGTH];
705+
uint8_t shared_key[SHA3_256_DIGEST_LENGTH] = {0};
706706
if (!hdnode_get_nem_shared_key(node, public_key, salt, NULL, shared_key)) {
707707
return 0;
708708
}
709709

710-
aes_encrypt_ctx ctx;
710+
aes_encrypt_ctx ctx = {0};
711711

712712
int ret = aes_encrypt_key256(shared_key, &ctx);
713713
memzero(shared_key, sizeof(shared_key));
@@ -731,13 +731,13 @@ int hdnode_nem_encrypt(const HDNode *node, const ed25519_public_key public_key,
731731
int hdnode_nem_decrypt(const HDNode *node, const ed25519_public_key public_key,
732732
uint8_t *iv, const uint8_t *salt, const uint8_t *payload,
733733
size_t size, uint8_t *buffer) {
734-
uint8_t shared_key[SHA3_256_DIGEST_LENGTH];
734+
uint8_t shared_key[SHA3_256_DIGEST_LENGTH] = {0};
735735

736736
if (!hdnode_get_nem_shared_key(node, public_key, salt, NULL, shared_key)) {
737737
return 0;
738738
}
739739

740-
aes_decrypt_ctx ctx;
740+
aes_decrypt_ctx ctx = {0};
741741

742742
int ret = aes_decrypt_key256(shared_key, &ctx);
743743
memzero(shared_key, sizeof(shared_key));
@@ -822,7 +822,7 @@ int hdnode_get_shared_key(const HDNode *node, const uint8_t *peer_public_key,
822822
static int hdnode_serialize(const HDNode *node, uint32_t fingerprint,
823823
uint32_t version, char use_public, char *str,
824824
int strsize) {
825-
uint8_t node_data[78];
825+
uint8_t node_data[78] = {0};
826826
write_be(node_data, version);
827827
node_data[4] = node->depth;
828828
write_be(node_data + 5, fingerprint);
@@ -854,7 +854,7 @@ int hdnode_serialize_private(const HDNode *node, uint32_t fingerprint,
854854
int hdnode_deserialize(const char *str, uint32_t version_public,
855855
uint32_t version_private, const char *curve,
856856
HDNode *node, uint32_t *fingerprint) {
857-
uint8_t node_data[78];
857+
uint8_t node_data[78] = {0};
858858
memzero(node, sizeof(HDNode));
859859
node->curve = get_curve_by_name(curve);
860860
if (base58_decode_check(str, node->curve->hasher_base58, node_data,

0 commit comments

Comments
 (0)