Skip to content

Commit d6e4832

Browse files
committed
WIP: HSM_VERSION 6: no old_secret from get_per_commitment_point
1 parent ab4ea82 commit d6e4832

File tree

7 files changed

+41
-79
lines changed

7 files changed

+41
-79
lines changed

channeld/channeld.c

Lines changed: 29 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,75 +1429,51 @@ static void start_commit_timer(struct peer *peer)
14291429
send_commit_if_not_stfu, peer);
14301430
}
14311431

1432-
/* If old_secret is NULL, we don't care, otherwise it is filled in. */
1433-
static void get_per_commitment_point(u64 index, struct pubkey *point,
1434-
struct secret *old_secret)
1432+
static void get_per_commitment_point(u64 index, struct pubkey *point)
14351433
{
1436-
struct secret *s;
1434+
struct secret *unused;
14371435
const u8 *msg;
14381436

14391437
msg = hsm_req(tmpctx,
14401438
take(towire_hsmd_get_per_commitment_point(NULL, index)));
14411439

14421440
if (!fromwire_hsmd_get_per_commitment_point_reply(tmpctx, msg,
14431441
point,
1444-
&s))
1442+
&unused))
14451443
status_failed(STATUS_FAIL_HSM_IO,
14461444
"Bad per_commitment_point reply %s",
14471445
tal_hex(tmpctx, msg));
1446+
}
14481447

1449-
if (old_secret) {
1450-
if (!s)
1451-
status_failed(STATUS_FAIL_HSM_IO,
1452-
"No secret in per_commitment_point_reply %"
1453-
PRIu64,
1454-
index);
1455-
*old_secret = *s;
1456-
}
1448+
static void revoke_commitment(u64 index, struct pubkey *point, struct secret *old_secret)
1449+
{
1450+
const u8 *msg;
1451+
msg = hsm_req(tmpctx, take(towire_hsmd_revoke_commitment_tx(tmpctx, index)));
1452+
if (!fromwire_hsmd_revoke_commitment_tx_reply(msg, old_secret, point))
1453+
status_failed(STATUS_FAIL_HSM_IO,
1454+
"Reading revoke_commitment_tx reply: %s",
1455+
tal_hex(tmpctx, msg));
14571456
}
14581457

14591458
/* revoke_index == current index - 1 (usually; not for retransmission) */
14601459
static u8 *make_revocation_msg(const struct peer *peer, u64 revoke_index,
14611460
struct pubkey *point)
14621461
{
1463-
const u8 *msg;
14641462
struct secret old_commit_secret;
14651463

14661464
/* Now that the master has persisted the new commitment advance the HSMD
1467-
* and fetch the revocation secret for the old one. */
1468-
if (!hsm_is_capable(peer->hsm_capabilities, WIRE_HSMD_REVOKE_COMMITMENT_TX)) {
1469-
/* Prior to HSM_VERSION 5 we call get_per_commitment_point to
1470-
* get the old_secret and next point.
1471-
*/
1472-
get_per_commitment_point(revoke_index+2, point, &old_commit_secret);
1473-
} else {
1474-
/* After HSM_VERSION 5 we explicitly revoke the commitment in case
1475-
* the original revoke didn't complete. The hsmd_revoke_commitment_tx
1476-
* call is idempotent ...
1477-
*/
1478-
msg = towire_hsmd_revoke_commitment_tx(tmpctx, revoke_index);
1479-
msg = hsm_req(tmpctx, take(msg));
1480-
if (!fromwire_hsmd_revoke_commitment_tx_reply(msg, &old_commit_secret, point))
1481-
status_failed(STATUS_FAIL_HSM_IO,
1482-
"Reading revoke_commitment_tx reply: %s",
1483-
tal_hex(tmpctx, msg));
1484-
}
1465+
* and fetch the revocation secret for the old one.
1466+
*
1467+
* After HSM_VERSION 5 we explicitly revoke the commitment in case
1468+
* the original revoke didn't complete. The hsmd_revoke_commitment_tx
1469+
* call is idempotent ...
1470+
*/
1471+
revoke_commitment(revoke_index, point, &old_commit_secret);
14851472

14861473
return towire_revoke_and_ack(peer, &peer->channel_id, &old_commit_secret,
14871474
point);
14881475
}
14891476

1490-
static u8 *make_revocation_msg_from_secret(const struct peer *peer,
1491-
u64 revoke_index,
1492-
struct pubkey *point,
1493-
const struct secret *old_commit_secret,
1494-
const struct pubkey *next_point)
1495-
{
1496-
*point = *next_point;
1497-
return towire_revoke_and_ack(peer, &peer->channel_id,
1498-
old_commit_secret, next_point);
1499-
}
1500-
15011477
/* Convert changed htlcs into parts which lightningd expects. */
15021478
static void marshall_htlc_info(const tal_t *ctx,
15031479
const struct htlc **changed_htlcs,
@@ -1564,8 +1540,6 @@ static void send_revocation(struct peer *peer,
15641540
struct added_htlc *added;
15651541
const u8 *msg;
15661542
const u8 *msg_for_master;
1567-
struct secret old_secret2;
1568-
struct pubkey next_point2;
15691543

15701544
/* Marshall it now before channel_sending_revoke_and_ack changes htlcs */
15711545
/* FIXME: Make infrastructure handle state post-revoke_and_ack! */
@@ -1608,24 +1582,10 @@ static void send_revocation(struct peer *peer,
16081582

16091583
/* Now that the master has persisted the new commitment advance the HSMD
16101584
* and fetch the revocation secret for the old one. */
1611-
if (!hsm_is_capable(peer->hsm_capabilities, WIRE_HSMD_REVOKE_COMMITMENT_TX)) {
1612-
/* Prior to HSM_VERSION 5 we use the old_secret
1613-
* received earlier from validate_commitment_tx. */
1614-
old_secret2 = *old_secret;
1615-
next_point2 = *next_point;
1616-
} else {
1617-
msg = towire_hsmd_revoke_commitment_tx(tmpctx, peer->next_index[LOCAL] - 2);
1618-
msg = hsm_req(tmpctx, take(msg));
1619-
if (!fromwire_hsmd_revoke_commitment_tx_reply(msg, &old_secret2, &next_point2))
1620-
status_failed(STATUS_FAIL_HSM_IO,
1621-
"Reading revoke_commitment_tx reply: %s",
1622-
tal_hex(tmpctx, msg));
1623-
}
16241585

16251586
/* Revoke previous commit, get new point. */
1626-
msg = make_revocation_msg_from_secret(peer, peer->next_index[LOCAL]-2,
1627-
&peer->next_local_per_commit,
1628-
&old_secret2, &next_point2);
1587+
msg = make_revocation_msg(peer, peer->next_index[LOCAL]-2,
1588+
&peer->next_local_per_commit);
16291589

16301590
/* Now we can finally send revoke_and_ack to peer */
16311591
peer_write(peer->pps, take(msg));
@@ -2061,7 +2021,7 @@ static struct commitsig_info *handle_peer_commit_sig(struct peer *peer,
20612021
"error");
20622022
}
20632023

2064-
/* Validate the counterparty's signatures, returns prior per_commitment_secret. */
2024+
/* As of HSM_VERSION 5 returned old_secret is always NULL (revoke returns it instead) */
20652025
htlcs = collect_htlcs(NULL, htlc_map);
20662026
msg2 = towire_hsmd_validate_commitment_tx(NULL,
20672027
txs[0],
@@ -2123,10 +2083,8 @@ static struct commitsig_info *handle_peer_commit_sig(struct peer *peer,
21232083
tal_steal(commitsigs, result);
21242084
}
21252085

2126-
// If the HSM doesn't support WIRE_HSMD_REVOKE_COMMITMENT_TX we'd better
2127-
// have the old_secret at this point.
2128-
if (!hsm_is_capable(peer->hsm_capabilities, WIRE_HSMD_REVOKE_COMMITMENT_TX))
2129-
assert(old_secret);
2086+
/* After HSM_VERSION 5 old_secret is always NULL */
2087+
assert(!old_secret);
21302088

21312089
send_revocation(peer, &commit_sig, htlc_sigs, changed_htlcs, txs[0],
21322090
old_secret, &next_point, commitsigs);
@@ -2778,7 +2736,7 @@ static struct commitsig *interactive_send_commitments(struct peer *peer,
27782736
/* Funding counts as 0th commit so we do inflight_index + 1 */
27792737
if (fromwire_peektype(msg) == WIRE_COMMITMENT_SIGNED) {
27802738
get_per_commitment_point(next_index_local - 1,
2781-
&my_current_per_commitment_point, NULL);
2739+
&my_current_per_commitment_point);
27822740

27832741
result = handle_peer_commit_sig(peer, msg,
27842742
inflight_index + 1,
@@ -4672,10 +4630,7 @@ static void check_current_dataloss_fields(struct peer *peer,
46724630
memset(&old_commit_secret, 0, sizeof(old_commit_secret));
46734631
else {
46744632
struct pubkey unused;
4675-
/* This gets previous revocation number, since asking for
4676-
* commitment point N gives secret for N-2 */
4677-
get_per_commitment_point(next_revocation_number+1,
4678-
&unused, &old_commit_secret);
4633+
revoke_commitment(next_revocation_number - 1, &unused, &old_commit_secret);
46794634
}
46804635

46814636
if (!secret_eq_consttime(&old_commit_secret,
@@ -4810,7 +4765,7 @@ static void peer_reconnect(struct peer *peer,
48104765
/* Our current per-commitment point is the commitment point in the last
48114766
* received signed commitment */
48124767
get_per_commitment_point(peer->next_index[LOCAL] - 1,
4813-
&my_current_per_commitment_point, NULL);
4768+
&my_current_per_commitment_point);
48144769

48154770
send_tlvs = NULL;
48164771

@@ -5419,7 +5374,7 @@ static void handle_funding_depth(struct peer *peer, const u8 *msg)
54195374
/* Need to retrieve the first point again, even if we
54205375
* moved on, as channel_ready explicitly includes the
54215376
* first one. */
5422-
get_per_commitment_point(1, &point, NULL);
5377+
get_per_commitment_point(1, &point);
54235378

54245379
msg = towire_channel_ready(NULL, &peer->channel_id,
54255380
&point, tlvs);
@@ -6006,7 +5961,7 @@ static void init_channel(struct peer *peer)
60065961
assert(peer->next_index[REMOTE] > 0);
60075962

60085963
get_per_commitment_point(peer->next_index[LOCAL],
6009-
&peer->next_local_per_commit, NULL);
5964+
&peer->next_local_per_commit);
60105965

60115966
peer->channel = new_full_channel(peer, &peer->channel_id,
60125967
&funding,

common/hsm_version.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
* v5 with hsmd_revoke_commitment_tx: 5742538f87ef5d5bf55b66dc19e52c8683cfeb1b887d3e64ba530ba9a4d8e638
2424
* v5 with sign_any_cannouncement: 5fdb9068c43a21887dc03f7dce410d2e3eeff6277f0d49b4fc56595a798fd4a4
2525
* v5 drop init v2: 5024454532fe5a78bb7558000cb344190888b9915360d3d56ddca22eaba9b872
26+
* v6 no secret from get_per_commitment_point: 5024454532fe5a78bb7558000cb344190888b9915360d3d56ddca22eaba9b872
2627
*/
2728
#define HSM_MIN_VERSION 5
28-
#define HSM_MAX_VERSION 5
29+
#define HSM_MAX_VERSION 6
2930
#endif /* LIGHTNING_COMMON_HSM_VERSION_H */

hsmd/hsmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ static struct io_plan *init_hsm(struct io_conn *conn,
436436
struct secret *hsm_encryption_key;
437437
struct bip32_key_version bip32_key_version;
438438
u32 minversion, maxversion;
439-
const u32 our_minversion = 4, our_maxversion = 5;
439+
const u32 our_minversion = 4, our_maxversion = 6;
440440

441441
/* This must be lightningd. */
442442
assert(is_lightningd(c));

hsmd/hsmd_wire.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,11 @@ msgdata,hsmd_sign_splice_tx,input_index,u32,
294294
msgtype,hsmd_sign_tx_reply,112
295295
msgdata,hsmd_sign_tx_reply,sig,bitcoin_signature,
296296

297-
# Openingd/channeld/onchaind asks for Nth per_commitment_point, if > 2, gets N-2 secret.
297+
# Openingd/channeld/onchaind asks for Nth per_commitment_point
298298
msgtype,hsmd_get_per_commitment_point,18
299299
msgdata,hsmd_get_per_commitment_point,n,u64,
300300

301+
# IMPORTANT - Beginning HSM_VERSION 6 we never return an old_commitment_secret
301302
msgtype,hsmd_get_per_commitment_point_reply,118
302303
msgdata,hsmd_get_per_commitment_point_reply,per_commitment_point,pubkey,
303304
msgdata,hsmd_get_per_commitment_point_reply,old_commitment_secret,?secret,

hsmd/libhsmd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,8 @@ static u8 *handle_get_per_commitment_point(struct hsmd_client *c, const u8 *msg_
11851185
return hsmd_status_bad_request_fmt(
11861186
c, msg_in, "bad per_commit_point %" PRIu64, n);
11871187

1188+
// FIXME(ksedgwic) - no return secret if HSM_VERSION >= 6
1189+
11881190
if (n >= 2) {
11891191
old_secret = tal(tmpctx, struct secret);
11901192
if (!per_commit_secret(&shaseed, old_secret, n - 2)) {

openingd/common.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,8 @@ void validate_initial_commitment_signature(int hsm_fd,
289289
status_failed(STATUS_FAIL_HSM_IO,
290290
"Reading validate_commitment_tx reply: %s",
291291
tal_hex(tmpctx, msg));
292+
293+
// FIXME(ksedgwic) after HSM_VERSION 6 old_secret is always NULL
294+
// assert(!old_secret);
292295
}
293296

openingd/dualopend.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3883,15 +3883,15 @@ static void rbf_remote_start(struct state *state, const u8 *rbf_msg)
38833883

38843884
static void hsm_per_commitment_point(u64 index, struct pubkey *point)
38853885
{
3886-
struct secret *s;
3886+
struct secret *unused;
38873887
const u8 *msg;
38883888

38893889
msg = towire_hsmd_get_per_commitment_point(NULL, index);
38903890
wire_sync_write(HSM_FD, take(msg));
38913891
msg = wire_sync_read(tmpctx, HSM_FD);
38923892

38933893
if (!fromwire_hsmd_get_per_commitment_point_reply(tmpctx, msg,
3894-
point, &s))
3894+
point, &unused))
38953895
status_failed(STATUS_FAIL_HSM_IO,
38963896
"Bad per_commitment_point reply %s",
38973897
tal_hex(tmpctx, msg));

0 commit comments

Comments
 (0)