cryptomsg: add towire/fromwire for crypto state.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-01-10 15:38:33 +10:30
parent 89a06734c4
commit 6a089ce112
2 changed files with 34 additions and 0 deletions

View File

@ -9,6 +9,7 @@
#include <sodium/crypto_aead_chacha20poly1305.h>
#include <status.h>
#include <utils.h>
#include <wire/wire.h>
#include <wire/wire_io.h>
struct crypto_state {
@ -320,3 +321,31 @@ struct crypto_state *crypto_state(struct peer *peer,
return cs;
}
void towire_crypto_state(u8 **ptr, const struct crypto_state *cs)
{
towire_u64(ptr, cs->rn);
towire_u64(ptr, cs->sn);
towire_sha256(ptr, &cs->sk);
towire_sha256(ptr, &cs->rk);
towire_sha256(ptr, &cs->s_ck);
towire_sha256(ptr, &cs->r_ck);
}
struct crypto_state *fromwire_crypto_state(const tal_t *ctx,
const u8 **ptr, size_t *max)
{
struct crypto_state *cs = tal(ctx, struct crypto_state);
cs->rn = fromwire_u64(ptr, max);
cs->sn = fromwire_u64(ptr, max);
fromwire_sha256(ptr, max, &cs->sk);
fromwire_sha256(ptr, max, &cs->rk);
fromwire_sha256(ptr, max, &cs->s_ck);
fromwire_sha256(ptr, max, &cs->r_ck);
cs->peer = (struct peer *)ctx;
if (!*ptr)
return tal_free(cs);
return cs;
}

View File

@ -2,6 +2,7 @@
#define LIGHTNING_LIGHTNINGD_CRYPTOMSG_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
struct io_conn;
struct peer;
@ -29,4 +30,8 @@ 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);
struct crypto_state *fromwire_crypto_state(const tal_t *ctx,
const u8 **ptr, size_t *max);
#endif /* LIGHTNING_LIGHTNINGD_CRYPTOMSG_H */