Browse Source
This avoids duplication of both logic and error-prone values, such as the salt. Grouping all hsm encryption logic into a public API will also allow us to fuzz it. Signed-off-by: Antoine Poinsot <darosior@protonmail.com>ppa
Antoine Poinsot
4 years ago
committed by
Christian Decker
11 changed files with 93 additions and 44 deletions
@ -0,0 +1,38 @@ |
|||||
|
#include <common/hsm_encryption.h> |
||||
|
#include <sodium.h> |
||||
|
#include <sodium/utils.h> |
||||
|
|
||||
|
|
||||
|
char *hsm_secret_encryption_key(const char *pass, struct secret *key) |
||||
|
{ |
||||
|
u8 salt[16] = "c-lightning\0\0\0\0\0"; |
||||
|
|
||||
|
/* Don't swap the encryption key ! */ |
||||
|
if (sodium_mlock(key->data, sizeof(key->data)) != 0) |
||||
|
return "Could not lock hsm_secret encryption key memory."; |
||||
|
|
||||
|
/* Check bounds. */ |
||||
|
if (strlen(pass) < crypto_pwhash_argon2id_PASSWD_MIN) |
||||
|
return "Password too short to be able to derive a key from it."; |
||||
|
if (strlen(pass) > crypto_pwhash_argon2id_PASSWD_MAX) |
||||
|
return "Password too long to be able to derive a key from it."; |
||||
|
|
||||
|
/* Now derive the key. */ |
||||
|
if (crypto_pwhash(key->data, sizeof(key->data), pass, strlen(pass), salt, |
||||
|
/* INTERACTIVE needs 64 MiB of RAM, MODERATE needs 256,
|
||||
|
* and SENSITIVE needs 1024. */ |
||||
|
crypto_pwhash_argon2id_OPSLIMIT_MODERATE, |
||||
|
crypto_pwhash_argon2id_MEMLIMIT_MODERATE, |
||||
|
crypto_pwhash_ALG_ARGON2ID13) != 0) |
||||
|
return "Could not derive a key from the password."; |
||||
|
|
||||
|
return NULL; |
||||
|
} |
||||
|
|
||||
|
void discard_key(struct secret *key TAKES) |
||||
|
{ |
||||
|
/* sodium_munlock() also zeroes the memory. */ |
||||
|
sodium_munlock(key->data, sizeof(key->data)); |
||||
|
if (taken(key)) |
||||
|
tal_free(key); |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
#ifndef LIGHTNING_COMMON_HSM_ENCRYPTION_H |
||||
|
#define LIGHTNING_COMMON_HSM_ENCRYPTION_H |
||||
|
#include "config.h" |
||||
|
#include <bitcoin/privkey.h> |
||||
|
#include <ccan/short_types/short_types.h> |
||||
|
#include <ccan/tal/tal.h> |
||||
|
|
||||
|
|
||||
|
/** Derive the hsm_secret encryption key from a passphrase.
|
||||
|
* @pass: the passphrase string. |
||||
|
* @encryption_key: the output key derived from the passphrase. |
||||
|
* |
||||
|
* On success, NULL is returned. On error, a human-readable error is. |
||||
|
*/ |
||||
|
char *hsm_secret_encryption_key(const char *pass, struct secret *encryption_key); |
||||
|
|
||||
|
/** Unlock and zeroize the encryption key memory after use.
|
||||
|
* @key: the encryption key. If taken, it will be tal_free'd |
||||
|
*/ |
||||
|
void discard_key(struct secret *key TAKES); |
||||
|
|
||||
|
#endif /* LIGHTNING_COMMON_HSM_ENCRYPTION_H */ |
Loading…
Reference in new issue