Files
palladum-lightning/tests/fuzz/fuzz-hsm_encryption.c
Rusty Russell 365add06f7 fuzz/fuzz-hsm_encryption: don't run as unit test under valgrind.
Thanks to Argon hashing, this is intolerably slow under valgrind, and
times out under CI.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-10-23 06:44:04 +10:30

51 lines
1.5 KiB
C

#include "config.h"
#include <assert.h>
#include <ccan/mem/mem.h>
#include <common/hsm_encryption.h>
#include <common/setup.h>
#include <stdlib.h>
#include <tests/fuzz/libfuzz.h>
void init(int *argc, char ***argv)
{
/* Don't run as a unit test under valgrind: too slow! */
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if (getenv("VALGRIND") && strcmp(getenv("VALGRIND"), "1") == 0) {
common_shutdown();
exit(0);
}
#endif
}
void run(const uint8_t *data, size_t size)
{
/* 4294967295 is crypto_pwhash_argon2id_PASSWD_MAX. libfuzzer won't
* generate inputs that large in practice, but hey. */
if (size > 32 && size < 4294967295) {
struct secret *hsm_secret, decrypted_hsm_secret, encryption_key;
char *passphrase;
struct encrypted_hsm_secret encrypted_secret;
const char *emsg;
/* Take the first 32 bytes as the plaintext hsm_secret seed,
* and the remaining ones as the passphrase. */
hsm_secret = (struct secret *)tal_dup_arr(NULL, u8, data, 32, 0);
passphrase = to_string(NULL, data + 32, size - 32);
/* A valid seed, a valid passphrase. This should not fail. */
assert(!hsm_secret_encryption_key_with_exitcode(passphrase, &encryption_key, &emsg));
/* Roundtrip */
assert(encrypt_hsm_secret(&encryption_key, hsm_secret,
&encrypted_secret));
assert(decrypt_hsm_secret(&encryption_key, &encrypted_secret,
&decrypted_hsm_secret));
assert(memeq(hsm_secret->data, sizeof(hsm_secret->data),
decrypted_hsm_secret.data,
sizeof(decrypted_hsm_secret.data)));
tal_free(hsm_secret);
tal_free(passphrase);
}
}