Skip to content

Commit 89a0673

Browse files
committed
lightningd/cryptomsg: test routines.
This creates output similar to the BOLT #8 test vectors. Signed-off-by: Rusty Russell <[email protected]>
1 parent a7f682c commit 89a0673

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

lightningd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ clean: lightningd-clean
5858
lightningd-clean:
5959
$(RM) $(LIGHTNINGD_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_JSMN_OBJS)
6060

61+
include lightningd/test/Makefile

lightningd/cryptomsg.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ struct io_plan *peer_write_message(struct io_conn *conn,
259259
cs->sk.u.u8);
260260
assert(ret == 0);
261261
assert(clen == sizeof(l) + 16);
262+
#ifdef SUPERVERBOSE
263+
status_trace("# encrypt l: cleartext=0x%s, AD=NULL, sn=0x%s, sk=0x%s => 0x%s",
264+
tal_hexstr(trc, &l, sizeof(l)),
265+
tal_hexstr(trc, npub, sizeof(npub)),
266+
tal_hexstr(trc, &cs->sk, sizeof(cs->sk)),
267+
tal_hexstr(trc, cs->out, clen));
268+
#endif
262269

263270
/* BOLT #8:
264271
*
@@ -277,6 +284,13 @@ struct io_plan *peer_write_message(struct io_conn *conn,
277284
cs->sk.u.u8);
278285
assert(ret == 0);
279286
assert(clen == mlen + 16);
287+
#ifdef SUPERVERBOSE
288+
status_trace("# encrypt m: cleartext=0x%s, AD=NULL, sn=0x%s, sk=0x%s => 0x%s",
289+
tal_hexstr(trc, msg, mlen),
290+
tal_hexstr(trc, npub, sizeof(npub)),
291+
tal_hexstr(trc, &cs->sk, sizeof(cs->sk)),
292+
tal_hexstr(trc, cs->out + 18, clen));
293+
#endif
280294

281295
maybe_rotate_key(&cs->sn, &cs->sk, &cs->s_ck);
282296

lightningd/test/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
check: lightningd/tests
2+
3+
# Note that these actually #include everything they need, except ccan/ and bitcoin/.
4+
# That allows for unit testing of statics, and special effects.
5+
LIGHTNINGD_TEST_SRC := $(wildcard lightningd/test/run-*.c)
6+
LIGHTNINGD_TEST_OBJS := $(LIGHTNINGD_TEST_SRC:.c=.o)
7+
LIGHTNINGD_TEST_PROGRAMS := $(LIGHTNINGD_TEST_OBJS:.o=)
8+
9+
update-mocks: $(LIGHTNINGD_TEST_SRC:%=update-mocks/%)
10+
11+
$(LIGHTNINGD_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a utils.o $(LIGHTNINGD_HANDSHAKE_GEN_SRC:.c=.o)
12+
13+
$(LIGHTNINGD_TEST_OBJS): $(LIGHTNINGD_HEADERS) $(BITCOIN_HEADERS) $(CORE_HEADERS) $(GEN_HEADERS) $(WIRE_HEADERS) $(CCAN_HEADERS) $(LIBBASE58_HEADERS)
14+
15+
lightningd/tests: $(LIGHTNINGD_TEST_PROGRAMS:%=unittest/%)
16+

lightningd/test/run-cryptomsg.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include <assert.h>
2+
#include <ccan/str/hex/hex.h>
3+
#include <ccan/tal/str/str.h>
4+
#include <status.h>
5+
#include <stdio.h>
6+
#include <wire/wire_io.h>
7+
8+
#undef io_read
9+
#undef io_write
10+
11+
static char *read_buf;
12+
static size_t read_buf_len;
13+
14+
static void do_read(void *buf, size_t len)
15+
{
16+
assert(len <= read_buf_len);
17+
memcpy(buf, read_buf, len);
18+
read_buf += len;
19+
read_buf_len -= len;
20+
}
21+
22+
#define io_read(conn, p, len, next, arg) \
23+
(do_read((p), (len)), (next)((conn), (arg)), NULL)
24+
25+
static char *write_buf;
26+
27+
static void do_write(const void *buf, size_t len)
28+
{
29+
size_t oldlen = tal_count(write_buf);
30+
tal_resize(&write_buf, oldlen + len);
31+
memcpy(write_buf + oldlen, buf, len);
32+
}
33+
34+
#define io_write(conn, p, len, next, arg) \
35+
(do_write((p), (len)), (next)((conn), (arg)), NULL)
36+
37+
#define status_trace(fmt, ...) \
38+
printf(fmt "\n", __VA_ARGS__)
39+
40+
#include "../cryptomsg.c"
41+
42+
const void *trc;
43+
44+
static struct io_plan *check_msg_write(struct io_conn *conn, struct peer *peer)
45+
{
46+
assert(tal_count(write_buf) == 2 + 16 + 5 + 16);
47+
return NULL;
48+
}
49+
50+
static struct io_plan *check_msg_read(struct io_conn *conn, struct peer *peer,
51+
u8 *msg)
52+
{
53+
assert(tal_count(msg) == 5);
54+
assert(memcmp(msg, "hello", 5) == 0);
55+
return NULL;
56+
}
57+
58+
static struct sha256 sha256_from_hex(const char *hex)
59+
{
60+
struct sha256 sha256;
61+
hex += 2;
62+
if (!hex_decode(hex, strlen(hex), &sha256, sizeof(sha256)))
63+
abort();
64+
return sha256;
65+
}
66+
67+
int main(void)
68+
{
69+
tal_t *tmpctx = tal_tmpctx(NULL);
70+
struct crypto_state *cs_out, *cs_in;
71+
struct sha256 sk, rk, ck;
72+
const void *msg = tal_dup_arr(tmpctx, char, "hello", 5, 0);
73+
size_t i;
74+
75+
trc = tal_tmpctx(tmpctx);
76+
77+
/* BOLT #8:
78+
*
79+
* name: transport-initiator successful handshake
80+
*...
81+
* # ck,temp_k3=0x919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01,0x981a46c820fb7a241bc8184ba4bb1f01bcdfafb00dde80098cb8c38db9141520
82+
* # encryptWithAD(0x981a46c820fb7a241bc8184ba4bb1f01bcdfafb00dde80098cb8c38db9141520, 0x000000000000000000000000, 0x5dcb5ea9b4ccc755e0e3456af3990641276e1d5dc9afd82f974d90a47c918660, <empty>)
83+
* # t=0x8dc68b1c466263b47fdf31e560e139ba
84+
* output: 0x00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba
85+
* # HKDF(0x919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01,zero)
86+
* output: sk,rk=0x969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9,0xbb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442
87+
*/
88+
ck = sha256_from_hex("0x919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01");
89+
sk = sha256_from_hex("0x969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9");
90+
rk = sha256_from_hex("0xbb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442");
91+
92+
cs_out = crypto_state(tmpctx, &sk, &rk, &ck, &ck, 0, 0);
93+
cs_in = crypto_state(tmpctx, &rk, &sk, &ck, &ck, 0, 0);
94+
95+
for (i = 0; i < 1002; i++) {
96+
write_buf = tal_arr(tmpctx, char, 0);
97+
98+
peer_write_message(NULL, cs_out, msg, check_msg_write);
99+
if ((i % 500) < 2)
100+
status_trace("output %zu: 0x%s", i,
101+
tal_hex(tmpctx, write_buf));
102+
103+
read_buf = write_buf;
104+
read_buf_len = tal_count(read_buf);
105+
write_buf = tal_arr(tmpctx, char, 0);
106+
107+
peer_read_message(NULL, cs_in, check_msg_read);
108+
assert(read_buf_len == 0);
109+
}
110+
tal_free(tmpctx);
111+
return 0;
112+
}

0 commit comments

Comments
 (0)