Skip to content

Commit 64df23f

Browse files
Chandra Prataprustyrussell
authored andcommitted
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()`. [ Changed from fuzz-hsm_encryption to fuzz-hsm_secret --RR ]
1 parent 1cc2e56 commit 64df23f

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

tests/fuzz/fuzz-hsm_secret.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ void init(int *argc, char ***argv)
2020

2121
void run(const uint8_t *data, size_t size)
2222
{
23-
/* 4294967295 is crypto_pwhash_argon2id_PASSWD_MAX. libfuzzer won't
24-
* generate inputs that large in practice, but hey. */
25-
if (size > 32 && size < 4294967295) {
23+
/* LibFuzzer won't generate inputs larger than
24+
* crypto_pwhash_argon2id_PASSWD_MAX in practice, but hey. */
25+
if (size > sizeof(struct secret) && size < crypto_pwhash_argon2id_PASSWD_MAX) {
2626
struct secret *hsm_secret, *encryption_key;
2727
char *passphrase;
2828
u8 encrypted_data[ENCRYPTED_HSM_SECRET_LEN];
@@ -31,8 +31,9 @@ void run(const uint8_t *data, size_t size)
3131

3232
/* Take the first 32 bytes as the plaintext hsm_secret seed,
3333
* and the remaining ones as the passphrase. */
34-
hsm_secret = (struct secret *)tal_dup_arr(NULL, u8, data, 32, 0);
35-
passphrase = to_string(NULL, data + 32, size - 32);
34+
hsm_secret = (struct secret *)tal_dup_arr(NULL, u8, data, sizeof(struct secret), 0);
35+
mlock_tal_memory(hsm_secret);
36+
passphrase = to_string(NULL, data + sizeof(struct secret), size - sizeof(struct secret));
3637

3738
/* A valid seed, a valid passphrase. This should not fail. */
3839
encryption_key = get_encryption_key(NULL, passphrase);

0 commit comments

Comments
 (0)