Skip to content

Commit 16ae5a4

Browse files
committed
common: trivial changes from review.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 6c15f1e commit 16ae5a4

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

common/hsm_secret.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ enum hsm_secret_type detect_hsm_secret_type(const u8 *hsm_secret, size_t len)
107107
/* Legacy 73-byte encrypted format */
108108
if (len == ENCRYPTED_HSM_SECRET_LEN)
109109
return HSM_SECRET_ENCRYPTED;
110-
assert(len > sizeof(struct sha256));
111-
/* Check if it starts with our type bytes (mnemonic formats) */
112-
if (memeqzero(hsm_secret, 32))
110+
111+
/* Since HSM_SECRET_PLAIN_SIZE == 32, this must be true! */
112+
assert(len >= sizeof(struct sha256));
113+
114+
/* First 32 bytes are the hash of the resulting seed: all 0
115+
* for "no passphrase" */
116+
if (memeqzero(hsm_secret, sizeof(struct sha256)))
113117
return HSM_SECRET_MNEMONIC_NO_PASS;
114118
else
115119
return HSM_SECRET_MNEMONIC_WITH_PASS;
@@ -200,8 +204,6 @@ const char *hsm_secret_error_str(enum hsm_secret_error err)
200204
return "Invalid hsm_secret format";
201205
case HSM_SECRET_ERR_TERMINAL:
202206
return "Terminal error";
203-
case HSM_SECRET_ERR_MEMORY:
204-
return "Memory error";
205207
}
206208
return "Unknown error";
207209
}
@@ -403,7 +405,7 @@ static void restore_echo(const struct termios *saved_term)
403405
}
404406

405407
/* Read line from stdin (uses tal allocation) */
406-
static char *read_line(const tal_t *ctx)
408+
static const char *read_line(const tal_t *ctx)
407409
{
408410
char *line = NULL;
409411
size_t size = 0;
@@ -433,7 +435,7 @@ const char *read_stdin_pass(const tal_t *ctx, enum hsm_secret_error *err)
433435
return NULL;
434436
}
435437

436-
char *input = read_line(ctx);
438+
const char *input = read_line(ctx);
437439
if (!input) {
438440
if (echo_disabled)
439441
restore_echo(&saved_term);
@@ -455,7 +457,7 @@ const char *read_stdin_mnemonic(const tal_t *ctx, enum hsm_secret_error *err)
455457
printf("Introduce your BIP39 word list separated by space (at least 12 words):\n");
456458
fflush(stdout);
457459

458-
char *line = read_line(ctx);
460+
const char *line = read_line(ctx);
459461
if (!line) {
460462
*err = HSM_SECRET_ERR_INVALID_FORMAT;
461463
return NULL;

common/hsm_secret.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ enum hsm_secret_error {
3232
HSM_SECRET_ERR_SEED_DERIVATION_FAILED,
3333
HSM_SECRET_ERR_INVALID_FORMAT,
3434
HSM_SECRET_ERR_TERMINAL,
35-
HSM_SECRET_ERR_MEMORY
3635
};
3736

3837
/**
3938
* Represents the content of the hsm_secret file, either a raw seed or a mnemonic.
4039
*/
4140
struct hsm_secret {
4241
enum hsm_secret_type type;
43-
u8 *secret_data; /* Variable length: 32 bytes (legacy) or 64 bytes (mnemonic) */
42+
const u8 *secret_data; /* Variable length: 32 bytes (legacy) or 64 bytes (mnemonic) */
4443
const char *mnemonic; /* NULL if not derived from mnemonic */
4544
};
4645

@@ -71,7 +70,7 @@ bool hsm_secret_needs_passphrase(const u8 *hsm_secret, size_t len);
7170
* @hsm_secret - raw file contents
7271
* @len - length of file
7372
* @passphrase - passphrase, or NULL if not needed
74-
* @err - optional pointer to set error code on failure
73+
* @err - pointer to set error code on failure
7574
*
7675
* Returns parsed `struct hsm_secret` or NULL on error.
7776
*/
@@ -100,8 +99,8 @@ struct secret *get_encryption_key(const tal_t *ctx, const char *passphrase);
10099
* Returns true on success.
101100
*/
102101
bool encrypt_legacy_hsm_secret(const struct secret *encryption_key,
103-
const struct secret *hsm_secret,
104-
u8 *output);
102+
const struct secret *hsm_secret,
103+
u8 *output);
105104

106105
/**
107106
* Reads a passphrase from stdin, disabling terminal echo.

hsmd/hsmd.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ static void hsmd_send_init_reply_failure(enum hsm_secret_error error_code, enum
301301
static void create_hsm(int fd, const char *passphrase)
302302
{
303303
u8 *hsm_secret_data;
304-
size_t hsm_secret_len;
305304
int ret;
306305
/* Always create a mnemonic-based hsm_secret */
307306
u8 entropy[BIP39_ENTROPY_LEN_128];
@@ -313,7 +312,6 @@ static void create_hsm(int fd, const char *passphrase)
313312
/* Generate random entropy for new mnemonic */
314313
randombytes_buf(entropy, sizeof(entropy));
315314

316-
317315
/* Generate mnemonic from entropy */
318316
tal_wally_start();
319317
ret = bip39_mnemonic_from_bytes(NULL, entropy, sizeof(entropy), &mnemonic);
@@ -322,27 +320,26 @@ static void create_hsm(int fd, const char *passphrase)
322320
if (ret != WALLY_OK) {
323321
unlink_noerr("hsm_secret");
324322
hsmd_send_init_reply_failure(HSM_SECRET_ERR_SEED_DERIVATION_FAILED, STATUS_FAIL_INTERNAL_ERROR,
325-
"Failed to generate mnemonic from entropy");
323+
"Failed to generate mnemonic from entropy");
326324
}
327325

328326
if (!mnemonic) {
329327
unlink_noerr("hsm_secret");
330328
hsmd_send_init_reply_failure(HSM_SECRET_ERR_SEED_DERIVATION_FAILED, STATUS_FAIL_INTERNAL_ERROR,
331-
"Failed to get generated mnemonic");
329+
"Failed to get generated mnemonic");
332330
}
333331

334332
/* Derive seed hash from mnemonic + passphrase (or zero if no passphrase) */
335333
if (!derive_seed_hash(mnemonic, passphrase, &seed_hash)) {
336334
unlink_noerr("hsm_secret");
337335
hsmd_send_init_reply_failure(HSM_SECRET_ERR_SEED_DERIVATION_FAILED, STATUS_FAIL_INTERNAL_ERROR,
338-
"Failed to derive seed hash from mnemonic");
336+
"Failed to derive seed hash from mnemonic");
339337
}
340338

341339
/* Create hsm_secret format: seed_hash (32 bytes) + mnemonic */
342340
hsm_secret_data = tal_arr(tmpctx, u8, 0);
343341
towire_sha256(&hsm_secret_data, &seed_hash);
344342
towire(&hsm_secret_data, mnemonic, strlen(mnemonic));
345-
hsm_secret_len = tal_count(hsm_secret_data);
346343

347344
/* Derive the actual secret from mnemonic + passphrase for our global hsm_secret */
348345
u8 bip32_seed[BIP39_SEED_LEN_512];
@@ -358,7 +355,7 @@ static void create_hsm(int fd, const char *passphrase)
358355
}
359356

360357
/* Write the hsm_secret data to file */
361-
if (!write_all(fd, hsm_secret_data, hsm_secret_len)) {
358+
if (!write_all(fd, hsm_secret_data, tal_count(hsm_secret_data))) {
362359
unlink_noerr("hsm_secret");
363360
status_failed(STATUS_FAIL_INTERNAL_ERROR,
364361
"writing: %s", strerror(errno));
@@ -494,7 +491,7 @@ static struct io_plan *init_hsm(struct io_conn *conn,
494491
u32 minversion, maxversion;
495492
struct tlv_hsmd_init_tlvs *tlvs;
496493
const u32 our_minversion = 4, our_maxversion = 6;
497-
const char *hsm_passphrase = NULL; /* Initialize to NULL */
494+
const char *hsm_passphrase;
498495

499496
/* This must be lightningd. */
500497
assert(is_lightningd(c));
@@ -531,6 +528,8 @@ static struct io_plan *init_hsm(struct io_conn *conn,
531528
* never sets that anymore), and we use the TLV instead. */
532529
if (tlvs->hsm_passphrase)
533530
hsm_passphrase = tlvs->hsm_passphrase;
531+
else
532+
hsm_passphrase = NULL;
534533

535534
if (!developer) {
536535
assert(!dev_force_privkey);
@@ -637,6 +636,7 @@ static struct io_plan *handle_memleak(struct io_conn *conn,
637636

638637
memleak_ptr(memtable, dev_force_privkey);
639638
memleak_ptr(memtable, dev_force_bip32_seed);
639+
640640
found_leak = dump_memleak(memtable, memleak_status_broken, NULL);
641641
reply = towire_hsmd_dev_memleak_reply(NULL, found_leak);
642642
return req_reply(conn, c, take(reply));
@@ -768,15 +768,16 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
768768
case WIRE_HSMD_CLIENT_HSMFD:
769769
return pass_client_hsmfd(conn, c, c->msg_in);
770770

771-
case WIRE_HSMD_DEV_MEMLEAK:
772-
if (developer)
773-
return handle_memleak(conn, c, c->msg_in);
774-
/* fall thru */
775-
776771
case WIRE_HSMD_DERIVE_BIP86_KEY:
777772
return handle_derive_bip86_key(conn, c, c->msg_in);
778773
case WIRE_HSMD_CHECK_BIP86_PUBKEY:
779774
return handle_check_bip86_pubkey(conn, c, c->msg_in);
775+
776+
case WIRE_HSMD_DEV_MEMLEAK:
777+
if (!developer)
778+
break;
779+
return handle_memleak(conn, c, c->msg_in);
780+
780781
case WIRE_HSMD_NEW_CHANNEL:
781782
case WIRE_HSMD_SETUP_CHANNEL:
782783
case WIRE_HSMD_CHECK_OUTPOINT:

0 commit comments

Comments
 (0)