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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-10-11 20:26:50 +10:30 committed by Christian Decker
parent 399b5f61bc
commit 2394c9a2e7
11 changed files with 50 additions and 33 deletions

View File

@ -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 \

View File

@ -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 \

View File

@ -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 \

22
common/crypto_state.c Normal file
View File

@ -0,0 +1,22 @@
#include <common/crypto_state.h>
#include <wire/wire.h>
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);
}

20
common/crypto_state.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef LIGHTNING_COMMON_CRYPTO_STATE_H
#define LIGHTNING_COMMON_CRYPTO_STATE_H
#include "config.h"
#include <bitcoin/privkey.h>
#include <ccan/short_types/short_types.h>
#include <stddef.h>
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 */

View File

@ -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);
}

View File

@ -1,22 +1,13 @@
#ifndef LIGHTNING_COMMON_CRYPTOMSG_H
#define LIGHTNING_COMMON_CRYPTOMSG_H
#include "config.h"
#include <bitcoin/privkey.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <common/crypto_state.h>
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,

View File

@ -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 \

View File

@ -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 \

View File

@ -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 \

View File

@ -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 \