-
Notifications
You must be signed in to change notification settings - Fork 902
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/fuzz: add a fuzz target for hsm_encryption
Signed-off-by: Antoine Poinsot <[email protected]>
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <assert.h> | ||
#include <tests/fuzz/libfuzz.h> | ||
|
||
#include <bitcoin/privkey.h> | ||
#include <ccan/mem/mem.h> | ||
#include <ccan/short_types/short_types.h> | ||
#include <ccan/tal/tal.h> | ||
#include <common/hsm_encryption.h> | ||
#include <common/utils.h> | ||
|
||
void init(int *argc, char ***argv) | ||
{ | ||
} | ||
|
||
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; | ||
|
||
/* 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(passphrase, &encryption_key)); | ||
/* 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); | ||
} | ||
} |