Skip to content

Commit 4d94a11

Browse files
author
Chandra Pratap
committed
fuzz-tests: get rid of magic numbers in fuzz-hsm_encryption.c
Changelog-None: `fuzz-hsm_encryption.c` hard codes the lengths sizeof(struct secret) as 32 and crypto_pwhash_argon2id_PASSWD_MAX as 4294967295. Replace the latter with the former to improve readability and maintainability. While at it, replace the `tal_free()` call on our secret key with `discard_key()`. This has the benefit of testing `discard_key()`.
1 parent 8a0e473 commit 4d94a11

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

tests/fuzz/fuzz-hsm_encryption.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ void init(int *argc, char ***argv)
1111

1212
void run(const uint8_t *data, size_t size)
1313
{
14-
/* 4294967295 is crypto_pwhash_argon2id_PASSWD_MAX. libfuzzer won't
15-
* generate inputs that large in practice, but hey. */
16-
if (size > 32 && size < 4294967295) {
14+
/* LibFuzzer won't generate inputs larger than
15+
* crypto_pwhash_argon2id_PASSWD_MAX in practice, but hey. */
16+
if (size > sizeof(struct secret) && size < crypto_pwhash_argon2id_PASSWD_MAX) {
1717
struct secret *hsm_secret, decrypted_hsm_secret, encryption_key;
1818
char *passphrase;
1919
struct encrypted_hsm_secret encrypted_secret;
2020
const char *emsg;
2121

2222
/* Take the first 32 bytes as the plaintext hsm_secret seed,
2323
* and the remaining ones as the passphrase. */
24-
hsm_secret = (struct secret *)tal_dup_arr(NULL, u8, data, 32, 0);
25-
passphrase = to_string(NULL, data + 32, size - 32);
24+
hsm_secret = (struct secret *)tal_dup_arr(NULL, u8, data, sizeof(struct secret), 0);
25+
passphrase = to_string(NULL, data + sizeof(struct secret), size - sizeof(struct secret));
2626

2727
/* A valid seed, a valid passphrase. This should not fail. */
2828
assert(!hsm_secret_encryption_key_with_exitcode(passphrase, &encryption_key, &emsg));
@@ -35,7 +35,7 @@ void run(const uint8_t *data, size_t size)
3535
decrypted_hsm_secret.data,
3636
sizeof(decrypted_hsm_secret.data)));
3737

38-
tal_free(hsm_secret);
38+
discard_key(hsm_secret);
3939
tal_free(passphrase);
4040
}
4141
}

0 commit comments

Comments
 (0)