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