From 07e6ed0c161df0e5fd2b43ede602c932f2c37b2b Mon Sep 17 00:00:00 2001 From: sstone Date: Thu, 23 Jun 2016 16:38:35 +0200 Subject: [PATCH 1/2] save received revocation preimages in shachain. make sure that preimages are generated in reverse order. --- daemon/packets.c | 4 +++- daemon/peer.h | 4 ++++ daemon/secrets.c | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/daemon/packets.c b/daemon/packets.c index e3279fe9a..8e6a6adf0 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -809,7 +809,6 @@ Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt) * SHA256 hash of `revocation_preimage` matches the previous commitment * transaction, and MUST fail if it does not. */ - /* FIXME: Save preimage in shachain too. */ if (!check_preimage(r->revocation_preimage, &ci->revocation_hash)) return pkt_err(peer, "complete preimage incorrect"); @@ -819,6 +818,9 @@ Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt) proto_to_sha256(r->revocation_preimage, ci->revocation_preimage); + // save revocation preimages in shachain + shachain_add_hash(&peer->their_preimages, 0xFFFFFFFFFFFFFFFFL - ci->commit_num, ci->revocation_preimage); + /* Save next revocation hash. */ proto_to_sha256(r->next_revocation_hash, &peer->remote.next_revocation_hash); diff --git a/daemon/peer.h b/daemon/peer.h index 01394cd71..c1fc45d1c 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -11,6 +11,7 @@ #include "netaddr.h" #include "state.h" #include +#include #include #include @@ -214,6 +215,9 @@ struct peer { /* Stuff we have in common. */ struct peer_visible_state local, remote; + + /* this is where we will store their revocation preimages*/ + struct shachain their_preimages; }; void setup_listeners(struct lightningd_state *dstate, unsigned int portnum); diff --git a/daemon/secrets.c b/daemon/secrets.c index 74c535d41..e1ae2332f 100644 --- a/daemon/secrets.c +++ b/daemon/secrets.c @@ -147,12 +147,15 @@ void peer_secrets_init(struct peer *peer) if (RAND_bytes(peer->secrets->revocation_seed.u.u8, sizeof(peer->secrets->revocation_seed.u.u8)) != 1) fatal("Could not get random bytes for revocation seed"); + + shachain_init(&peer->their_preimages); } void peer_get_revocation_preimage(const struct peer *peer, u64 index, struct sha256 *preimage) { - shachain_from_seed(&peer->secrets->revocation_seed, index, preimage); + // generate hashes in reverse order, otherwise the first hash gives away everything + shachain_from_seed(&peer->secrets->revocation_seed, 0xFFFFFFFFFFFFFFFFL - index, preimage); } void peer_get_revocation_hash(const struct peer *peer, u64 index, From be1a230ae8442910ae43758e51fa7acd8450fe86 Mon Sep 17 00:00:00 2001 From: sstone Date: Thu, 23 Jun 2016 17:11:10 +0200 Subject: [PATCH 2/2] fix formatting issues --- daemon/packets.c | 4 ++-- daemon/peer.h | 4 ++-- daemon/secrets.c | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/daemon/packets.c b/daemon/packets.c index 8e6a6adf0..a16c1e104 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -818,8 +818,8 @@ Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt) proto_to_sha256(r->revocation_preimage, ci->revocation_preimage); - // save revocation preimages in shachain - shachain_add_hash(&peer->their_preimages, 0xFFFFFFFFFFFFFFFFL - ci->commit_num, ci->revocation_preimage); + // save revocation preimages in shachain + shachain_add_hash(&peer->their_preimages, 0xFFFFFFFFFFFFFFFFL - ci->commit_num, ci->revocation_preimage); /* Save next revocation hash. */ proto_to_sha256(r->next_revocation_hash, diff --git a/daemon/peer.h b/daemon/peer.h index c1fc45d1c..97c2f5058 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -216,8 +216,8 @@ struct peer { /* Stuff we have in common. */ struct peer_visible_state local, remote; - /* this is where we will store their revocation preimages*/ - struct shachain their_preimages; + /* this is where we will store their revocation preimages*/ + struct shachain their_preimages; }; void setup_listeners(struct lightningd_state *dstate, unsigned int portnum); diff --git a/daemon/secrets.c b/daemon/secrets.c index e1ae2332f..7c6135101 100644 --- a/daemon/secrets.c +++ b/daemon/secrets.c @@ -148,14 +148,14 @@ void peer_secrets_init(struct peer *peer) sizeof(peer->secrets->revocation_seed.u.u8)) != 1) fatal("Could not get random bytes for revocation seed"); - shachain_init(&peer->their_preimages); + shachain_init(&peer->their_preimages); } void peer_get_revocation_preimage(const struct peer *peer, u64 index, struct sha256 *preimage) { - // generate hashes in reverse order, otherwise the first hash gives away everything - shachain_from_seed(&peer->secrets->revocation_seed, 0xFFFFFFFFFFFFFFFFL - index, preimage); + // generate hashes in reverse order, otherwise the first hash gives away everything + shachain_from_seed(&peer->secrets->revocation_seed, 0xFFFFFFFFFFFFFFFFL - index, preimage); } void peer_get_revocation_hash(const struct peer *peer, u64 index,