From 2394c9a2e77a30ebf34765da8042d7da36208240 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 11 Oct 2017 20:26:50 +1030 Subject: [PATCH] crypto_state: move to its own file. In particular, the main daemon needs to pass it about (marshal/unmarshal) but it won't need to actually use it after the next patch. Signed-off-by: Rusty Russell --- channeld/Makefile | 1 + closingd/Makefile | 1 + common/Makefile | 1 + common/crypto_state.c | 22 ++++++++++++++++++++++ common/crypto_state.h | 20 ++++++++++++++++++++ common/cryptomsg.c | 20 -------------------- common/cryptomsg.h | 14 +------------- gossipd/Makefile | 1 + handshaked/Makefile | 1 + lightningd/Makefile | 1 + openingd/Makefile | 1 + 11 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 common/crypto_state.c create mode 100644 common/crypto_state.h diff --git a/channeld/Makefile b/channeld/Makefile index 5eee473ae..5e2f1b538 100644 --- a/channeld/Makefile +++ b/channeld/Makefile @@ -33,6 +33,7 @@ ALL_GEN_HEADERS += $(LIGHTNINGD_CHANNEL_HEADERS_GEN) # Common source we use. CHANNELD_COMMON_OBJS := \ common/channel_config.o \ + common/crypto_state.o \ common/crypto_sync.o \ common/cryptomsg.o \ common/daemon_conn.o \ diff --git a/closingd/Makefile b/closingd/Makefile index 632ad2c2f..d94bfe5a1 100644 --- a/closingd/Makefile +++ b/closingd/Makefile @@ -43,6 +43,7 @@ $(LIGHTNINGD_CLOSING_OBJS): $(LIGHTNINGD_HEADERS) # Common source we use. CLOSINGD_COMMON_OBJS := \ common/close_tx.o \ + common/crypto_state.o \ common/crypto_sync.o \ common/cryptomsg.o \ common/daemon_conn.o \ diff --git a/common/Makefile b/common/Makefile index e5076217e..07d701f00 100644 --- a/common/Makefile +++ b/common/Makefile @@ -3,6 +3,7 @@ COMMON_SRC := \ common/channel_config.c \ common/close_tx.c \ common/configdir.c \ + common/crypto_state.c \ common/crypto_sync.c \ common/cryptomsg.c \ common/daemon_conn.c \ diff --git a/common/crypto_state.c b/common/crypto_state.c new file mode 100644 index 000000000..f056afc64 --- /dev/null +++ b/common/crypto_state.c @@ -0,0 +1,22 @@ +#include +#include + +void towire_crypto_state(u8 **ptr, const struct crypto_state *cs) +{ + towire_u64(ptr, cs->rn); + towire_u64(ptr, cs->sn); + towire_secret(ptr, &cs->sk); + towire_secret(ptr, &cs->rk); + towire_secret(ptr, &cs->s_ck); + towire_secret(ptr, &cs->r_ck); +} + +void fromwire_crypto_state(const u8 **ptr, size_t *max, struct crypto_state *cs) +{ + cs->rn = fromwire_u64(ptr, max); + cs->sn = fromwire_u64(ptr, max); + fromwire_secret(ptr, max, &cs->sk); + fromwire_secret(ptr, max, &cs->rk); + fromwire_secret(ptr, max, &cs->s_ck); + fromwire_secret(ptr, max, &cs->r_ck); +} diff --git a/common/crypto_state.h b/common/crypto_state.h new file mode 100644 index 000000000..d5ec88cf3 --- /dev/null +++ b/common/crypto_state.h @@ -0,0 +1,20 @@ +#ifndef LIGHTNING_COMMON_CRYPTO_STATE_H +#define LIGHTNING_COMMON_CRYPTO_STATE_H +#include "config.h" +#include +#include +#include + +struct crypto_state { + /* Received and sent nonces. */ + u64 rn, sn; + /* Sending and receiving keys. */ + struct secret sk, rk; + /* Chaining key for re-keying */ + struct secret s_ck, r_ck; +}; + +void towire_crypto_state(u8 **pptr, const struct crypto_state *cs); +void fromwire_crypto_state(const u8 **ptr, size_t *max, struct crypto_state *cs); + +#endif /* LIGHTNING_COMMON_CRYPTO_STATE_H */ diff --git a/common/cryptomsg.c b/common/cryptomsg.c index 86a603077..617612d21 100644 --- a/common/cryptomsg.c +++ b/common/cryptomsg.c @@ -371,23 +371,3 @@ void init_peer_crypto_state(struct peer *peer, struct peer_crypto_state *pcs) pcs->peer = peer; pcs->out = pcs->in = NULL; } - -void towire_crypto_state(u8 **ptr, const struct crypto_state *cs) -{ - towire_u64(ptr, cs->rn); - towire_u64(ptr, cs->sn); - towire_secret(ptr, &cs->sk); - towire_secret(ptr, &cs->rk); - towire_secret(ptr, &cs->s_ck); - towire_secret(ptr, &cs->r_ck); -} - -void fromwire_crypto_state(const u8 **ptr, size_t *max, struct crypto_state *cs) -{ - cs->rn = fromwire_u64(ptr, max); - cs->sn = fromwire_u64(ptr, max); - fromwire_secret(ptr, max, &cs->sk); - fromwire_secret(ptr, max, &cs->rk); - fromwire_secret(ptr, max, &cs->s_ck); - fromwire_secret(ptr, max, &cs->r_ck); -} diff --git a/common/cryptomsg.h b/common/cryptomsg.h index 9c2564ff1..7d2de5d30 100644 --- a/common/cryptomsg.h +++ b/common/cryptomsg.h @@ -1,22 +1,13 @@ #ifndef LIGHTNING_COMMON_CRYPTOMSG_H #define LIGHTNING_COMMON_CRYPTOMSG_H #include "config.h" -#include #include #include +#include struct io_conn; struct peer; -struct crypto_state { - /* Received and sent nonces. */ - u64 rn, sn; - /* Sending and receiving keys. */ - struct secret sk, rk; - /* Chaining key for re-keying */ - struct secret s_ck, r_ck; -}; - struct peer_crypto_state { struct crypto_state cs; @@ -46,9 +37,6 @@ struct io_plan *peer_write_message(struct io_conn *conn, struct io_plan *(*next)(struct io_conn *, struct peer *)); -void towire_crypto_state(u8 **pptr, const struct crypto_state *cs); -void fromwire_crypto_state(const u8 **ptr, size_t *max, struct crypto_state *cs); - /* Low-level functions for sync comms: doesn't discard unknowns! */ u8 *cryptomsg_encrypt_msg(const tal_t *ctx, struct crypto_state *cs, diff --git a/gossipd/Makefile b/gossipd/Makefile index 3c4e57bec..e9d9c02c4 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -33,6 +33,7 @@ LIGHTNINGD_HEADERS_GEN += $(LIGHTNINGD_GOSSIP_HEADERS) # Common source we use. GOSSIPD_COMMON_OBJS := \ + common/crypto_state.o \ common/crypto_sync.o \ common/cryptomsg.o \ common/daemon_conn.o \ diff --git a/handshaked/Makefile b/handshaked/Makefile index 5fc672f0a..e45d93f34 100644 --- a/handshaked/Makefile +++ b/handshaked/Makefile @@ -36,6 +36,7 @@ $(LIGHTNINGD_HANDSHAKE_OBJS): $(LIGHTNINGD_HEADERS) # Common source we use. HANDSHAKED_COMMON_OBJS := \ + common/crypto_state.o \ common/crypto_sync.o \ common/cryptomsg.o \ common/daemon_conn.o \ diff --git a/lightningd/Makefile b/lightningd/Makefile index 0c6e2ca63..af9712c2b 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -17,6 +17,7 @@ LIGHTNINGD_COMMON_OBJS := \ common/bip32.o \ common/channel_config.o \ common/configdir.o \ + common/crypto_state.o \ common/cryptomsg.o \ common/derive_basepoints.o \ common/funding_tx.o \ diff --git a/openingd/Makefile b/openingd/Makefile index 458f9b1f8..c73ea61fd 100644 --- a/openingd/Makefile +++ b/openingd/Makefile @@ -37,6 +37,7 @@ LIGHTNINGD_HEADERS_NOGEN += $(LIGHTNINGD_OPENING_HEADERS_NOGEN) OPENINGD_COMMON_OBJS := \ common/bip32.o \ common/channel_config.o \ + common/crypto_state.o \ common/crypto_sync.o \ common/cryptomsg.o \ common/daemon_conn.o \