hsm_encryption: delete hsm_encryption

Changelog-Removed: Remove hsm_encryption files as they have now been replaced by hsm_secret
This commit is contained in:
Sangbida Chaudhuri
2025-10-24 13:57:49 +10:30
committed by Rusty Russell
parent 631a8ccdc1
commit b4984fd94e
65 changed files with 0 additions and 312 deletions

View File

@@ -45,7 +45,6 @@ COMMON_SRC_NOGEN := \
common/hash_u5.c \
common/hmac.c \
common/hsm_capable.c \
common/hsm_encryption.c \
common/hsm_secret.c \
common/htlc_state.c \
common/htlc_trim.c \

View File

@@ -1,158 +0,0 @@
#include "config.h"
#include <common/errcode.h>
#include <common/hsm_encryption.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
int hsm_secret_encryption_key_with_exitcode(const char *pass, struct secret *key,
const char **err_msg)
{
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) {
if (err_msg)
*err_msg = "Could not lock hsm_secret encryption key memory.";
return EXITCODE_HSM_GENERIC_ERROR;
}
/* Check bounds. */
if (strlen(pass) < crypto_pwhash_argon2id_PASSWD_MIN) {
if (err_msg)
*err_msg = "Password too short to be able to derive a key from it.";
return EXITCODE_HSM_BAD_PASSWORD;
} else if (strlen(pass) > crypto_pwhash_argon2id_PASSWD_MAX) {
if (err_msg)
*err_msg = "Password too long to be able to derive a key from it.";
return EXITCODE_HSM_BAD_PASSWORD;
}
/* 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) {
if (err_msg)
*err_msg = "Could not derive a key from the password.";
return EXITCODE_HSM_BAD_PASSWORD;
}
return 0;
}
bool encrypt_hsm_secret(const struct secret *encryption_key,
const struct secret *hsm_secret,
struct encrypted_hsm_secret *output)
{
crypto_secretstream_xchacha20poly1305_state crypto_state;
if (crypto_secretstream_xchacha20poly1305_init_push(&crypto_state, output->data,
encryption_key->data) != 0)
return false;
if (crypto_secretstream_xchacha20poly1305_push(&crypto_state,
output->data + HS_HEADER_LEN,
NULL, hsm_secret->data,
sizeof(hsm_secret->data),
/* Additional data and tag */
NULL, 0, 0))
return false;
return true;
}
bool decrypt_hsm_secret(const struct secret *encryption_key,
const struct encrypted_hsm_secret *cipher,
struct secret *output)
{
crypto_secretstream_xchacha20poly1305_state crypto_state;
/* The header part */
if (crypto_secretstream_xchacha20poly1305_init_pull(&crypto_state, cipher->data,
encryption_key->data) != 0)
return false;
/* The ciphertext part */
if (crypto_secretstream_xchacha20poly1305_pull(&crypto_state, output->data,
NULL, 0,
cipher->data + HS_HEADER_LEN,
HS_CIPHERTEXT_LEN,
NULL, 0) != 0)
return false;
return true;
}
/* Returns -1 on error (and sets errno), 0 if not encrypted, 1 if it is */
int is_hsm_secret_encrypted(const char *path)
{
struct stat st;
if (stat(path, &st) != 0)
return -1;
return st.st_size == ENCRYPTED_HSM_SECRET_LEN;
}
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);
}
/* Read a line from stdin, do not take the newline character into account. */
static bool getline_stdin_pass(char **passwd, size_t *passwd_size)
{
if (getline(passwd, passwd_size, stdin) < 0)
return false;
if ((*passwd)[strlen(*passwd) - 1] == '\n')
(*passwd)[strlen(*passwd) - 1] = '\0';
return true;
}
char *read_stdin_pass_with_exit_code(const char **reason, int *exit_code)
{
struct termios current_term, temp_term;
char *passwd = NULL;
size_t passwd_size = 0;
if (isatty(fileno(stdin))) {
/* Set a temporary term, same as current but with ECHO disabled. */
if (tcgetattr(fileno(stdin), &current_term) != 0) {
*reason = "Could not get current terminal options.";
*exit_code = EXITCODE_HSM_PASSWORD_INPUT_ERR;
return NULL;
}
temp_term = current_term;
temp_term.c_lflag &= ~ECHO;
if (tcsetattr(fileno(stdin), TCSANOW, &temp_term) != 0) {
*reason = "Could not disable pass echoing.";
*exit_code = EXITCODE_HSM_PASSWORD_INPUT_ERR;
return NULL;
}
if (!getline_stdin_pass(&passwd, &passwd_size)) {
*reason = "Could not read pass from stdin.";
*exit_code = EXITCODE_HSM_PASSWORD_INPUT_ERR;
return NULL;
}
/* Restore the original terminal */
if (tcsetattr(fileno(stdin), TCSANOW, &current_term) != 0) {
*reason = "Could not restore terminal options.";
free(passwd);
*exit_code = EXITCODE_HSM_PASSWORD_INPUT_ERR;
return NULL;
}
} else if (!getline_stdin_pass(&passwd, &passwd_size)) {
*reason = "Could not read pass from stdin.";
*exit_code = EXITCODE_HSM_PASSWORD_INPUT_ERR;
return NULL;
}
return passwd;
}

View File

@@ -1,67 +0,0 @@
#ifndef LIGHTNING_COMMON_HSM_ENCRYPTION_H
#define LIGHTNING_COMMON_HSM_ENCRYPTION_H
#include "config.h"
#include <bitcoin/privkey.h>
#include <sodium.h>
/* Length of the encrypted hsm secret header. */
#define HS_HEADER_LEN crypto_secretstream_xchacha20poly1305_HEADERBYTES
/* From libsodium: "The ciphertext length is guaranteed to always be message
* length + ABYTES" */
#define HS_CIPHERTEXT_LEN \
(sizeof(struct secret) + crypto_secretstream_xchacha20poly1305_ABYTES)
/* Total length of an encrypted hsm_secret */
#define ENCRYPTED_HSM_SECRET_LEN (HS_HEADER_LEN + HS_CIPHERTEXT_LEN)
struct encrypted_hsm_secret {
u8 data[ENCRYPTED_HSM_SECRET_LEN];
};
/** Derive the hsm_secret encryption key from a passphrase.
* @pass: the passphrase string.
* @encryption_key: the output key derived from the passphrase.
* @err_msg: if not NULL the error message contains the reason of the failure.
*
* On success, 0 is returned, on error a value > 0 is returned and it can be used as exit code.
*/
int hsm_secret_encryption_key_with_exitcode(const char *pass, struct secret *key,
const char **err_msg);
/** Encrypt the hsm_secret using a previously derived encryption key.
* @encryption_key: the key derived from the passphrase.
* @hsm_secret: the plaintext hsm_secret to encrypt.
* @output: the resulting encrypted hsm_secret.
*
* Return false on encryption failure.
*/
bool encrypt_hsm_secret(const struct secret *encryption_key,
const struct secret *hsm_secret,
struct encrypted_hsm_secret *output);
/** Decrypt the hsm_secret using a previously derived encryption key.
* @encryption_key: the key derived from the passphrase.
* @cipher: the encrypted hsm_secret to decrypt.
* @output: the resulting hsm_secret.
*
* Return false on decryption failure.
*/
bool decrypt_hsm_secret(const struct secret *encryption_key,
const struct encrypted_hsm_secret *cipher,
struct secret *output);
/** 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);
/** Read hsm_secret encryption pass from stdin, disabling echoing.
* @reason: if NULL is returned, will point to the human-readable error,
* and the correct exit code is returned by the exit_code parameter.
*
* Caller must free the string as it does tal-reallocate getline's output.
*/
char *read_stdin_pass_with_exit_code(const char **reason, int *exit_code);
/** Returns -1 on error (and sets errno), 0 if not encrypted, 1 if it is */
int is_hsm_secret_encrypted(const char *path);
#endif /* LIGHTNING_COMMON_HSM_ENCRYPTION_H */

View File

@@ -1,3 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD>
2

View File

@@ -1,2 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
QQQQQQQQQQ€€€€€QQQQQQQQ<EFBFBD><EFBFBD>

View File

@@ -1,2 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>

View File

@@ -1,2 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><7F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <0B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>

View File

@@ -1,2 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><7F><EFBFBD><EFBFBD><EFBFBD> <0B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD>

View File

@@ -1,2 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
YYY<EFBFBD><EFBFBD>

View File

@@ -1,2 +0,0 @@
-˙˙-˙˙˙˙˙˙˙˙˙;˙˙˙˙˙˙˙˙˙;˙˙˙˙˙˙˙˙˙
˙˙iiiiiiiii˙˙˙

View File

@@ -1 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><>拻拻拻<E68BBB><E68BBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>

View File

@@ -1,2 +0,0 @@
-<2D><><EFBFBD><EFBFBD>蝌蝌蝌蝌蝌蝌蝌8蝌蝌蝌蝌<E89D8C><E89D8C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD>

View File

@@ -1 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⅱⅱⅱⅱⅱⅱ<E285B1><E285B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>&<26><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>

View File

@@ -1 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><7F>;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><05><>

View File

@@ -1 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;<3B>

View File

@@ -1 +0,0 @@
`<60><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>

View File

@@ -1,2 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD>

View File

@@ -1,2 +0,0 @@
-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>

View File

@@ -1,2 +0,0 @@
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD>

View File

@@ -1,50 +0,0 @@
#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);
}
}