2017-04-13 19:55:14 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2019-01-08 01:53:26 +01:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2017-04-13 19:55:14 +02:00
|
|
|
#include <ccan/crypto/ripemd160/ripemd160.h>
|
|
|
|
#include <ccan/crypto/sha256/sha256.h>
|
|
|
|
#include <ccan/mem/mem.h>
|
2019-02-14 16:59:17 +01:00
|
|
|
#include <common/node_id.h>
|
2019-12-05 11:06:28 +01:00
|
|
|
#include <common/onion.h>
|
2020-01-23 00:38:04 +01:00
|
|
|
#include <common/onionreply.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/sphinx.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
|
|
|
|
#include <secp256k1_ecdh.h>
|
|
|
|
|
|
|
|
#include <sodium/crypto_auth_hmacsha256.h>
|
|
|
|
#include <sodium/crypto_stream_chacha20.h>
|
|
|
|
|
|
|
|
#define BLINDING_FACTOR_SIZE 32
|
|
|
|
#define KEY_LEN 32
|
2019-02-12 21:17:09 +01:00
|
|
|
|
|
|
|
#define NUM_STREAM_BYTES (2*ROUTING_INFO_SIZE)
|
2017-08-22 05:46:13 +02:00
|
|
|
#define ONION_REPLY_SIZE 256
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2019-02-18 13:22:07 +01:00
|
|
|
#define RHO_KEYTYPE "rho"
|
|
|
|
|
2017-04-13 19:55:14 +02:00
|
|
|
struct hop_params {
|
2020-01-23 06:54:10 +01:00
|
|
|
struct secret secret;
|
2017-04-13 19:55:14 +02:00
|
|
|
u8 blind[BLINDING_FACTOR_SIZE];
|
2019-01-08 01:53:26 +01:00
|
|
|
struct pubkey ephemeralkey;
|
2017-04-13 19:55:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct keyset {
|
|
|
|
u8 pi[KEY_LEN];
|
|
|
|
u8 mu[KEY_LEN];
|
|
|
|
u8 rho[KEY_LEN];
|
|
|
|
u8 gamma[KEY_LEN];
|
|
|
|
};
|
|
|
|
|
2019-02-14 14:54:17 +01:00
|
|
|
/* Encapsulates the information about a given payment path for the the onion
|
|
|
|
* routing algorithm.
|
|
|
|
*/
|
|
|
|
struct sphinx_path {
|
|
|
|
/* The session_key used to generate the shared secrets along the
|
|
|
|
* path. This MUST be generated in a cryptographically secure manner,
|
|
|
|
* and is exposed solely for testing, i.e., it can be set to known
|
|
|
|
* values in unit tests. If unset it'll be generated during the packet
|
|
|
|
* generation. */
|
|
|
|
struct secret *session_key;
|
|
|
|
|
|
|
|
/* The associated data is appended to the packet when generating the
|
|
|
|
* HMAC, but is not passed along as part of the packet. It is used to
|
|
|
|
* ensure some external data (HTLC payment_hash) is not modified along
|
|
|
|
* the way. */
|
|
|
|
u8 *associated_data;
|
|
|
|
|
|
|
|
/* The individual hops on this route. */
|
|
|
|
struct sphinx_hop *hops;
|
2020-02-20 15:53:34 +01:00
|
|
|
|
|
|
|
/* If this is a rendez-vous onion, then the following node_id tells us
|
|
|
|
* which node will be processing this onion and decompressing the
|
|
|
|
* onion. It is used to generate the prefill obfuscation stream to
|
|
|
|
* hide the fact that the onion was compressed from the next
|
|
|
|
* node. NULL if this is not a rendez-vous onion, and shouldn't be
|
|
|
|
* compressible. */
|
|
|
|
struct pubkey *rendezvous_id;
|
2019-02-14 14:54:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sphinx_path *sphinx_path_new(const tal_t *ctx, const u8 *associated_data)
|
|
|
|
{
|
|
|
|
struct sphinx_path *sp = tal(ctx, struct sphinx_path);
|
2020-02-27 03:17:01 +01:00
|
|
|
sp->associated_data = tal_dup_talarr(sp, u8, associated_data);
|
2019-02-14 14:54:17 +01:00
|
|
|
sp->session_key = NULL;
|
2020-02-20 15:53:34 +01:00
|
|
|
sp->rendezvous_id = NULL;
|
2019-02-14 14:54:17 +01:00
|
|
|
sp->hops = tal_arr(sp, struct sphinx_hop, 0);
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx,
|
|
|
|
const u8 *associated_data,
|
|
|
|
const struct secret *session_key)
|
|
|
|
{
|
|
|
|
struct sphinx_path *sp = sphinx_path_new(ctx, associated_data);
|
|
|
|
sp->session_key = tal_dup(sp, struct secret, session_key);
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
|
2020-02-28 14:56:59 +01:00
|
|
|
bool sphinx_path_set_rendezvous(struct sphinx_path *sp,
|
|
|
|
const struct node_id *rendezvous_id)
|
|
|
|
{
|
|
|
|
if (rendezvous_id == NULL) {
|
|
|
|
sp->rendezvous_id = tal_free(sp->rendezvous_id);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
sp->rendezvous_id = tal_free(sp->rendezvous_id);
|
|
|
|
sp->rendezvous_id = tal(sp, struct pubkey);
|
|
|
|
return pubkey_from_node_id(sp->rendezvous_id, rendezvous_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 16:59:17 +01:00
|
|
|
static size_t sphinx_hop_size(const struct sphinx_hop *hop)
|
|
|
|
{
|
2019-12-05 11:06:28 +01:00
|
|
|
return tal_bytelen(hop->raw_payload) + HMAC_SIZE;
|
2019-02-14 16:59:17 +01:00
|
|
|
}
|
|
|
|
|
2020-01-08 22:05:16 +01:00
|
|
|
size_t sphinx_path_payloads_size(const struct sphinx_path *path)
|
2019-02-14 16:59:17 +01:00
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
for (size_t i=0; i<tal_count(path->hops); i++)
|
|
|
|
size += sphinx_hop_size(&path->hops[i]);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-12-05 11:06:28 +01:00
|
|
|
void sphinx_add_hop(struct sphinx_path *path, const struct pubkey *pubkey,
|
|
|
|
const u8 *payload TAKES)
|
2019-02-14 16:59:17 +01:00
|
|
|
{
|
|
|
|
struct sphinx_hop sp;
|
2020-02-27 03:17:01 +01:00
|
|
|
sp.raw_payload = tal_dup_talarr(path, u8, payload);
|
2019-02-14 16:59:17 +01:00
|
|
|
sp.pubkey = *pubkey;
|
|
|
|
tal_arr_expand(&path->hops, sp);
|
|
|
|
}
|
|
|
|
|
2017-04-13 19:55:14 +02:00
|
|
|
/* Small helper to append data to a buffer and update the position
|
|
|
|
* into the buffer
|
|
|
|
*/
|
|
|
|
static void write_buffer(u8 *dst, const void *src, const size_t len, int *pos)
|
|
|
|
{
|
|
|
|
memcpy(dst + *pos, src, len);
|
|
|
|
*pos += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read len bytes from the source at position pos into dst and update
|
|
|
|
* the position pos accordingly.
|
|
|
|
*/
|
|
|
|
static void read_buffer(void *dst, const u8 *src, const size_t len, int *pos)
|
|
|
|
{
|
|
|
|
memcpy(dst, src + *pos, len);
|
|
|
|
*pos += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 *serialize_onionpacket(
|
|
|
|
const tal_t *ctx,
|
|
|
|
const struct onionpacket *m)
|
|
|
|
{
|
|
|
|
u8 *dst = tal_arr(ctx, u8, TOTAL_PACKET_SIZE);
|
|
|
|
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
u8 der[PUBKEY_CMPR_LEN];
|
2017-04-13 19:55:14 +02:00
|
|
|
int p = 0;
|
|
|
|
|
2019-01-08 01:53:26 +01:00
|
|
|
pubkey_to_der(der, &m->ephemeralkey);
|
2017-04-13 19:55:14 +02:00
|
|
|
write_buffer(dst, &m->version, 1, &p);
|
2019-01-08 01:53:26 +01:00
|
|
|
write_buffer(dst, der, sizeof(der), &p);
|
2017-04-13 19:55:14 +02:00
|
|
|
write_buffer(dst, m->routinginfo, ROUTING_INFO_SIZE, &p);
|
2017-04-13 18:35:11 +02:00
|
|
|
write_buffer(dst, m->mac, sizeof(m->mac), &p);
|
2017-04-13 19:55:14 +02:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2020-02-28 14:56:59 +01:00
|
|
|
u8 *serialize_compressed_onion(const tal_t *ctx,
|
|
|
|
const struct sphinx_path *sp,
|
|
|
|
const struct onionpacket *packet)
|
|
|
|
{
|
|
|
|
u8 *dst;
|
|
|
|
u8 der[PUBKEY_CMPR_LEN];
|
|
|
|
size_t payloads_size = sphinx_path_payloads_size(sp);
|
|
|
|
size_t max_prefill = ROUTING_INFO_SIZE - payloads_size;
|
|
|
|
size_t rv_onion_size = TOTAL_PACKET_SIZE - max_prefill;
|
|
|
|
int p = 0;
|
|
|
|
|
|
|
|
assert(sp->rendezvous_id != NULL);
|
|
|
|
|
|
|
|
dst = tal_arr(ctx, u8, rv_onion_size);
|
|
|
|
|
|
|
|
pubkey_to_der(der, &packet->ephemeralkey);
|
|
|
|
write_buffer(dst, &packet->version, 1, &p);
|
|
|
|
write_buffer(dst, der, sizeof(der), &p);
|
|
|
|
write_buffer(dst, packet->routinginfo, ROUTING_INFO_SIZE - max_prefill, &p);
|
|
|
|
write_buffer(dst, packet->mac, sizeof(packet->mac), &p);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-29 21:20:18 +01:00
|
|
|
enum onion_type parse_onionpacket(const u8 *src,
|
|
|
|
const size_t srclen,
|
|
|
|
struct onionpacket *dest)
|
2017-04-13 19:55:14 +02:00
|
|
|
{
|
|
|
|
int p = 0;
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
u8 rawEphemeralkey[PUBKEY_CMPR_LEN];
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2019-01-08 01:21:50 +01:00
|
|
|
assert(srclen == TOTAL_PACKET_SIZE);
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2019-11-29 21:20:18 +01:00
|
|
|
read_buffer(&dest->version, src, 1, &p);
|
|
|
|
if (dest->version != 0x00) {
|
2017-04-13 19:55:14 +02:00
|
|
|
// FIXME add logging
|
2019-11-29 21:20:18 +01:00
|
|
|
return WIRE_INVALID_ONION_VERSION;
|
2017-04-13 19:55:14 +02:00
|
|
|
}
|
2019-01-08 01:53:26 +01:00
|
|
|
read_buffer(rawEphemeralkey, src, sizeof(rawEphemeralkey), &p);
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2019-01-08 01:53:26 +01:00
|
|
|
if (!pubkey_from_der(rawEphemeralkey, sizeof(rawEphemeralkey),
|
2019-11-29 21:20:18 +01:00
|
|
|
&dest->ephemeralkey)) {
|
|
|
|
return WIRE_INVALID_ONION_KEY;
|
2019-01-08 01:21:50 +01:00
|
|
|
}
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2019-11-29 21:20:18 +01:00
|
|
|
read_buffer(&dest->routinginfo, src, ROUTING_INFO_SIZE, &p);
|
|
|
|
read_buffer(&dest->mac, src, HMAC_SIZE, &p);
|
|
|
|
return 0;
|
2017-04-13 19:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xorbytes(uint8_t *d, const uint8_t *a, const uint8_t *b, size_t len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
d[i] = a[i] ^ b[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a pseudo-random byte stream of length `dstlen` from key `k` and
|
|
|
|
* store it in `dst`. `dst must be at least `dstlen` bytes long.
|
|
|
|
*/
|
|
|
|
static void generate_cipher_stream(void *dst, const u8 *k, size_t dstlen)
|
|
|
|
{
|
2020-02-27 03:17:21 +01:00
|
|
|
const u8 nonce[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
crypto_stream_chacha20(dst, dstlen, nonce, k);
|
|
|
|
}
|
|
|
|
|
2020-02-27 03:17:21 +01:00
|
|
|
/* xor cipher stream into dst */
|
|
|
|
static void xor_cipher_stream(void *dst, const u8 *k, size_t dstlen)
|
|
|
|
{
|
|
|
|
const u8 nonce[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
|
|
|
crypto_stream_chacha20_xor(dst, dst, dstlen, nonce, k);
|
|
|
|
}
|
|
|
|
|
2017-04-13 19:55:14 +02:00
|
|
|
static bool compute_hmac(
|
|
|
|
void *dst,
|
|
|
|
const void *src,
|
|
|
|
size_t len,
|
|
|
|
const void *key,
|
|
|
|
size_t keylen)
|
|
|
|
{
|
|
|
|
crypto_auth_hmacsha256_state state;
|
|
|
|
|
|
|
|
crypto_auth_hmacsha256_init(&state, key, keylen);
|
|
|
|
crypto_auth_hmacsha256_update(&state, memcheck(src, len), len);
|
|
|
|
crypto_auth_hmacsha256_final(&state, dst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void compute_packet_hmac(const struct onionpacket *packet,
|
|
|
|
const u8 *assocdata, const size_t assocdatalen,
|
|
|
|
u8 *mukey, u8 *hmac)
|
|
|
|
{
|
2017-04-13 20:12:07 +02:00
|
|
|
u8 mactemp[ROUTING_INFO_SIZE + assocdatalen];
|
2017-04-13 19:55:14 +02:00
|
|
|
u8 mac[32];
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
write_buffer(mactemp, packet->routinginfo, ROUTING_INFO_SIZE, &pos);
|
|
|
|
write_buffer(mactemp, assocdata, assocdatalen, &pos);
|
|
|
|
|
|
|
|
compute_hmac(mac, mactemp, sizeof(mactemp), mukey, KEY_LEN);
|
2019-02-12 21:17:09 +01:00
|
|
|
memcpy(hmac, mac, HMAC_SIZE);
|
2017-04-13 19:55:14 +02:00
|
|
|
}
|
|
|
|
|
2020-01-23 06:54:10 +01:00
|
|
|
static bool generate_key(void *k, const char *t, u8 tlen,
|
|
|
|
const struct secret *s)
|
2017-04-13 19:55:14 +02:00
|
|
|
{
|
2020-01-23 06:54:10 +01:00
|
|
|
return compute_hmac(k, s->data, KEY_LEN, t, tlen);
|
2017-04-13 19:55:14 +02:00
|
|
|
}
|
|
|
|
|
2019-02-18 13:22:07 +01:00
|
|
|
static bool generate_header_padding(void *dst, size_t dstlen,
|
|
|
|
const struct sphinx_path *path,
|
|
|
|
struct hop_params *params)
|
2017-04-13 19:55:14 +02:00
|
|
|
{
|
2019-02-18 13:22:07 +01:00
|
|
|
u8 stream[2 * ROUTING_INFO_SIZE];
|
2017-04-13 19:55:14 +02:00
|
|
|
u8 key[KEY_LEN];
|
2019-02-18 13:22:07 +01:00
|
|
|
size_t fillerStart, fillerEnd, fillerSize;
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
memset(dst, 0, dstlen);
|
2019-02-18 13:22:07 +01:00
|
|
|
for (int i = 0; i < tal_count(path->hops) - 1; i++) {
|
|
|
|
if (!generate_key(&key, RHO_KEYTYPE, strlen(RHO_KEYTYPE),
|
2020-01-23 06:54:10 +01:00
|
|
|
¶ms[i].secret))
|
2017-04-13 19:55:14 +02:00
|
|
|
return false;
|
|
|
|
|
2019-02-18 13:22:07 +01:00
|
|
|
generate_cipher_stream(stream, key, sizeof(stream));
|
|
|
|
|
|
|
|
/* Sum up how many bytes have been used by previous hops,
|
|
|
|
* that gives us the start in the stream */
|
|
|
|
fillerSize = 0;
|
|
|
|
for (int j = 0; j < i; j++)
|
2019-02-18 14:02:25 +01:00
|
|
|
fillerSize += sphinx_hop_size(&path->hops[j]);
|
2019-02-18 13:22:07 +01:00
|
|
|
fillerStart = ROUTING_INFO_SIZE - fillerSize;
|
|
|
|
|
|
|
|
/* The filler will dangle off of the end by the current
|
|
|
|
* hop-size, we'll make sure to copy it into the correct
|
|
|
|
* position in the next step. */
|
2019-02-18 14:02:25 +01:00
|
|
|
fillerEnd = ROUTING_INFO_SIZE + sphinx_hop_size(&path->hops[i]);
|
2019-02-18 13:22:07 +01:00
|
|
|
|
|
|
|
/* Apply the cipher-stream to the part of the filler that'll
|
|
|
|
* be added by this hop */
|
|
|
|
xorbytes(dst, dst, stream + fillerStart,
|
|
|
|
fillerEnd - fillerStart);
|
2017-04-13 19:55:14 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:53:34 +01:00
|
|
|
static bool generate_prefill(void *dst, size_t dstlen,
|
|
|
|
const struct sphinx_path *path,
|
|
|
|
struct hop_params *params)
|
|
|
|
{
|
|
|
|
u8 stream[2 * ROUTING_INFO_SIZE];
|
|
|
|
u8 key[KEY_LEN];
|
|
|
|
size_t fillerStart, fillerSize;
|
|
|
|
|
|
|
|
memset(dst, 0, dstlen);
|
|
|
|
for (int i = 0; i < tal_count(path->hops); i++) {
|
|
|
|
if (!generate_key(&key, RHO_KEYTYPE, strlen(RHO_KEYTYPE),
|
|
|
|
¶ms[i].secret))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
generate_cipher_stream(stream, key, sizeof(stream));
|
|
|
|
|
|
|
|
/* Sum up how many bytes have been used by previous hops,
|
|
|
|
* that gives us the start in the stream */
|
|
|
|
fillerSize = 0;
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
fillerSize += sphinx_hop_size(&path->hops[j]);
|
|
|
|
fillerStart = ROUTING_INFO_SIZE - fillerSize - dstlen;
|
|
|
|
|
|
|
|
/* Apply the cipher-stream to the part of the filler that'll
|
|
|
|
* be added by this hop */
|
|
|
|
xorbytes(dst, dst, stream + fillerStart, dstlen);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-08 01:53:26 +01:00
|
|
|
static void compute_blinding_factor(const struct pubkey *key,
|
2020-01-23 06:54:10 +01:00
|
|
|
const struct secret *sharedsecret,
|
2017-04-13 19:55:14 +02:00
|
|
|
u8 res[BLINDING_FACTOR_SIZE])
|
|
|
|
{
|
|
|
|
struct sha256_ctx ctx;
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
u8 der[PUBKEY_CMPR_LEN];
|
2017-04-13 19:55:14 +02:00
|
|
|
struct sha256 temp;
|
|
|
|
|
2019-01-08 01:53:26 +01:00
|
|
|
pubkey_to_der(der, key);
|
2017-04-13 19:55:14 +02:00
|
|
|
sha256_init(&ctx);
|
|
|
|
sha256_update(&ctx, der, sizeof(der));
|
2020-01-23 06:54:10 +01:00
|
|
|
sha256_update(&ctx, sharedsecret->data, sizeof(sharedsecret->data));
|
2017-04-13 19:55:14 +02:00
|
|
|
sha256_done(&ctx, &temp);
|
|
|
|
memcpy(res, &temp, 32);
|
|
|
|
}
|
|
|
|
|
2019-02-14 16:59:17 +01:00
|
|
|
static bool blind_group_element(struct pubkey *blindedelement,
|
|
|
|
const struct pubkey *pubkey,
|
|
|
|
const u8 blind[BLINDING_FACTOR_SIZE])
|
2017-04-13 19:55:14 +02:00
|
|
|
{
|
|
|
|
/* tweak_mul is inplace so copy first. */
|
|
|
|
if (pubkey != blindedelement)
|
|
|
|
*blindedelement = *pubkey;
|
2019-02-14 16:59:17 +01:00
|
|
|
if (secp256k1_ec_pubkey_tweak_mul(secp256k1_ctx,
|
|
|
|
&blindedelement->pubkey, blind) != 1)
|
2017-04-13 19:55:14 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-02 19:39:16 +01:00
|
|
|
bool sphinx_create_shared_secret(struct secret *privkey,
|
2020-01-23 06:54:10 +01:00
|
|
|
const struct pubkey *pubkey,
|
2020-03-02 19:39:16 +01:00
|
|
|
const struct secret *secret)
|
2017-04-13 19:55:14 +02:00
|
|
|
{
|
2020-03-02 19:39:16 +01:00
|
|
|
if (secp256k1_ecdh(secp256k1_ctx, privkey->data, &pubkey->pubkey,
|
|
|
|
secret->data, NULL, NULL) != 1)
|
2017-04-13 19:55:14 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onion_shared_secret(
|
2020-01-23 06:54:10 +01:00
|
|
|
struct secret *secret,
|
2017-04-13 19:55:14 +02:00
|
|
|
const struct onionpacket *packet,
|
|
|
|
const struct privkey *privkey)
|
|
|
|
{
|
2020-03-02 19:39:16 +01:00
|
|
|
return sphinx_create_shared_secret(secret, &packet->ephemeralkey,
|
|
|
|
&privkey->secret);
|
2017-04-13 19:55:14 +02:00
|
|
|
}
|
|
|
|
|
2020-01-23 06:54:10 +01:00
|
|
|
static void generate_key_set(const struct secret *secret,
|
2017-04-13 19:55:14 +02:00
|
|
|
struct keyset *keys)
|
|
|
|
{
|
|
|
|
generate_key(keys->rho, "rho", 3, secret);
|
|
|
|
generate_key(keys->pi, "pi", 2, secret);
|
|
|
|
generate_key(keys->mu, "mu", 2, secret);
|
|
|
|
generate_key(keys->gamma, "gamma", 5, secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hop_params *generate_hop_params(
|
|
|
|
const tal_t *ctx,
|
|
|
|
const u8 *sessionkey,
|
2019-02-14 16:59:17 +01:00
|
|
|
struct sphinx_path *path)
|
2017-04-13 19:55:14 +02:00
|
|
|
{
|
2019-02-14 16:59:17 +01:00
|
|
|
int i, j, num_hops = tal_count(path->hops);
|
2019-01-08 01:53:26 +01:00
|
|
|
struct pubkey temp;
|
2017-04-13 19:55:14 +02:00
|
|
|
u8 blind[BLINDING_FACTOR_SIZE];
|
|
|
|
struct hop_params *params = tal_arr(ctx, struct hop_params, num_hops);
|
|
|
|
|
|
|
|
/* Initialize the first hop with the raw information */
|
2019-02-14 16:59:17 +01:00
|
|
|
if (secp256k1_ec_pubkey_create(secp256k1_ctx,
|
|
|
|
¶ms[0].ephemeralkey.pubkey,
|
|
|
|
path->session_key->data) != 1)
|
2017-04-13 19:55:14 +02:00
|
|
|
return NULL;
|
|
|
|
|
2020-03-02 19:39:16 +01:00
|
|
|
if (!sphinx_create_shared_secret(
|
|
|
|
¶ms[0].secret, &path->hops[0].pubkey, path->session_key))
|
2017-04-13 19:55:14 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
compute_blinding_factor(
|
2020-01-23 06:54:10 +01:00
|
|
|
¶ms[0].ephemeralkey, ¶ms[0].secret,
|
2017-04-13 19:55:14 +02:00
|
|
|
params[0].blind);
|
|
|
|
|
|
|
|
/* Recursively compute all following ephemeral public keys,
|
|
|
|
* secrets and blinding factors
|
|
|
|
*/
|
|
|
|
for (i = 1; i < num_hops; i++) {
|
|
|
|
if (!blind_group_element(
|
|
|
|
¶ms[i].ephemeralkey,
|
|
|
|
¶ms[i - 1].ephemeralkey,
|
|
|
|
params[i - 1].blind))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Blind this hop's point with all previous blinding factors
|
|
|
|
* Order is indifferent, multiplication is commutative.
|
|
|
|
*/
|
|
|
|
memcpy(&blind, sessionkey, 32);
|
2019-02-14 16:59:17 +01:00
|
|
|
temp = path->hops[i].pubkey;
|
2017-04-13 19:55:14 +02:00
|
|
|
if (!blind_group_element(&temp, &temp, blind))
|
|
|
|
return NULL;
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
if (!blind_group_element(
|
|
|
|
&temp,
|
|
|
|
&temp,
|
|
|
|
params[j].blind))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Now hash temp and store it. This requires us to
|
|
|
|
* DER-serialize first and then skip the sign byte.
|
|
|
|
*/
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
u8 der[PUBKEY_CMPR_LEN];
|
2019-01-08 01:53:26 +01:00
|
|
|
pubkey_to_der(der, &temp);
|
2017-04-13 19:55:14 +02:00
|
|
|
struct sha256 h;
|
|
|
|
sha256(&h, der, sizeof(der));
|
|
|
|
memcpy(¶ms[i].secret, &h, sizeof(h));
|
|
|
|
|
|
|
|
compute_blinding_factor(
|
|
|
|
¶ms[i].ephemeralkey,
|
2020-01-23 06:54:10 +01:00
|
|
|
¶ms[i].secret, params[i].blind);
|
2017-04-13 19:55:14 +02:00
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2019-12-05 11:06:28 +01:00
|
|
|
static void sphinx_write_frame(u8 *dest, const struct sphinx_hop *hop)
|
2019-02-20 14:27:37 +01:00
|
|
|
{
|
2019-12-05 11:06:28 +01:00
|
|
|
memcpy(dest, hop->raw_payload, tal_bytelen(hop->raw_payload));
|
|
|
|
memcpy(dest + tal_bytelen(hop->raw_payload), hop->hmac, HMAC_SIZE);
|
2019-02-20 14:27:37 +01:00
|
|
|
}
|
|
|
|
|
2020-02-20 15:53:34 +01:00
|
|
|
static void sphinx_prefill_stream_xor(u8 *dst, size_t dstlen,
|
|
|
|
const struct secret *shared_secret)
|
|
|
|
{
|
|
|
|
u8 padkey[KEY_LEN];
|
|
|
|
generate_key(padkey, "prefill", 7, shared_secret);
|
|
|
|
xor_cipher_stream(dst, padkey, dstlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sphinx_prefill(u8 *routinginfo, const struct sphinx_path *sp,
|
|
|
|
size_t prefill_size, struct hop_params *params)
|
|
|
|
{
|
|
|
|
int num_hops = tal_count(sp->hops);
|
|
|
|
size_t fillerSize = sphinx_path_payloads_size(sp) -
|
|
|
|
sphinx_hop_size(&sp->hops[num_hops - 1]);
|
|
|
|
size_t last_hop_size = sphinx_hop_size(&sp->hops[num_hops - 1]);
|
|
|
|
int prefill_offset =
|
|
|
|
ROUTING_INFO_SIZE - fillerSize - last_hop_size - prefill_size;
|
|
|
|
u8 prefill[prefill_size];
|
|
|
|
struct secret shared_secret;
|
|
|
|
|
|
|
|
/* Generate the prefill stream, which cancels out the layers of
|
|
|
|
* encryption that will be applied while wrapping the onion. This
|
|
|
|
* leaves the middle, unused, section with all 0x00 bytes after
|
|
|
|
* encrypting. */
|
|
|
|
generate_prefill(prefill, prefill_size, sp, params);
|
|
|
|
memcpy(routinginfo + prefill_offset, prefill, prefill_size);
|
|
|
|
|
|
|
|
/* Now fill in the obfuscation stream, which can be regenerated by the
|
|
|
|
* node processing this onion. */
|
2020-03-02 19:39:16 +01:00
|
|
|
sphinx_create_shared_secret(&shared_secret, sp->rendezvous_id, sp->session_key);
|
2020-02-20 15:53:34 +01:00
|
|
|
sphinx_prefill_stream_xor(routinginfo + prefill_offset, prefill_size, &shared_secret);
|
|
|
|
}
|
|
|
|
|
2017-04-13 19:55:14 +02:00
|
|
|
struct onionpacket *create_onionpacket(
|
|
|
|
const tal_t *ctx,
|
2019-02-14 16:59:17 +01:00
|
|
|
struct sphinx_path *sp,
|
2017-05-06 04:19:44 +02:00
|
|
|
struct secret **path_secrets
|
2017-04-13 19:55:14 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
struct onionpacket *packet = talz(ctx, struct onionpacket);
|
2019-02-14 16:59:17 +01:00
|
|
|
int i, num_hops = tal_count(sp->hops);
|
2019-02-18 14:02:25 +01:00
|
|
|
size_t fillerSize = sphinx_path_payloads_size(sp) -
|
|
|
|
sphinx_hop_size(&sp->hops[num_hops - 1]);
|
|
|
|
u8 filler[fillerSize];
|
2017-04-13 19:55:14 +02:00
|
|
|
struct keyset keys;
|
2019-11-06 03:54:50 +01:00
|
|
|
u8 padkey[KEY_LEN];
|
2019-02-12 21:17:09 +01:00
|
|
|
u8 nexthmac[HMAC_SIZE];
|
2019-02-14 16:59:17 +01:00
|
|
|
struct hop_params *params;
|
2017-05-06 04:19:44 +02:00
|
|
|
struct secret *secrets = tal_arr(ctx, struct secret, num_hops);
|
2020-02-20 15:53:34 +01:00
|
|
|
size_t payloads_size = sphinx_path_payloads_size(sp);
|
|
|
|
size_t max_prefill = ROUTING_INFO_SIZE - payloads_size;
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2020-01-08 22:05:53 +01:00
|
|
|
if (sphinx_path_payloads_size(sp) > ROUTING_INFO_SIZE) {
|
|
|
|
tal_free(packet);
|
|
|
|
tal_free(secrets);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-02-14 16:59:17 +01:00
|
|
|
if (sp->session_key == NULL) {
|
|
|
|
sp->session_key = tal(sp, struct secret);
|
|
|
|
randombytes_buf(sp->session_key, sizeof(struct secret));
|
|
|
|
}
|
|
|
|
|
|
|
|
params = generate_hop_params(ctx, sp->session_key->data, sp);
|
2017-12-15 11:29:28 +01:00
|
|
|
if (!params) {
|
|
|
|
tal_free(packet);
|
|
|
|
tal_free(secrets);
|
2017-04-13 19:55:14 +02:00
|
|
|
return NULL;
|
2017-12-15 11:29:28 +01:00
|
|
|
}
|
2017-09-06 01:33:19 +02:00
|
|
|
packet->version = 0;
|
2019-02-12 21:17:09 +01:00
|
|
|
memset(nexthmac, 0, HMAC_SIZE);
|
2019-11-06 03:54:50 +01:00
|
|
|
|
|
|
|
/* BOLT-e116441ee836447ac3f24cdca62bac1e0f223d5f #4:
|
|
|
|
*
|
|
|
|
* The packet is initialized with 1366 _random_ bytes derived from a
|
|
|
|
* CSPRNG.
|
|
|
|
*/
|
|
|
|
/* Note that this is just hop_payloads: the rest of the packet is
|
|
|
|
* overwritten below or above anyway. */
|
2020-01-23 06:54:10 +01:00
|
|
|
generate_key(padkey, "pad", 3, sp->session_key);
|
2020-01-21 16:21:00 +01:00
|
|
|
generate_cipher_stream(packet->routinginfo, padkey, ROUTING_INFO_SIZE);
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2019-02-18 13:22:07 +01:00
|
|
|
generate_header_padding(filler, sizeof(filler), sp, params);
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2020-02-20 15:53:34 +01:00
|
|
|
if (sp->rendezvous_id != NULL)
|
|
|
|
/* FIXME: Fuzz this or expose to the caller to hide encoded
|
|
|
|
* route length. */
|
|
|
|
sphinx_prefill(packet->routinginfo, sp, max_prefill, params);
|
|
|
|
|
2017-04-13 19:55:14 +02:00
|
|
|
for (i = num_hops - 1; i >= 0; i--) {
|
2019-02-14 16:59:17 +01:00
|
|
|
memcpy(sp->hops[i].hmac, nexthmac, HMAC_SIZE);
|
2020-01-23 06:54:10 +01:00
|
|
|
generate_key_set(¶ms[i].secret, &keys);
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2019-02-14 16:59:17 +01:00
|
|
|
/* Rightshift mix-header by FRAME_SIZE */
|
2019-02-18 14:02:25 +01:00
|
|
|
size_t shiftSize = sphinx_hop_size(&sp->hops[i]);
|
|
|
|
memmove(packet->routinginfo + shiftSize, packet->routinginfo,
|
|
|
|
ROUTING_INFO_SIZE-shiftSize);
|
2019-12-05 11:06:28 +01:00
|
|
|
sphinx_write_frame(packet->routinginfo, &sp->hops[i]);
|
2020-02-27 03:17:21 +01:00
|
|
|
xor_cipher_stream(packet->routinginfo, keys.rho,
|
|
|
|
ROUTING_INFO_SIZE);
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
if (i == num_hops - 1) {
|
2019-02-18 14:02:25 +01:00
|
|
|
memcpy(packet->routinginfo + ROUTING_INFO_SIZE - fillerSize, filler, fillerSize);
|
2017-04-13 19:55:14 +02:00
|
|
|
}
|
|
|
|
|
2019-02-14 16:59:17 +01:00
|
|
|
compute_packet_hmac(packet, sp->associated_data, tal_bytelen(sp->associated_data), keys.mu,
|
2017-04-13 19:55:14 +02:00
|
|
|
nexthmac);
|
|
|
|
}
|
|
|
|
memcpy(packet->mac, nexthmac, sizeof(nexthmac));
|
|
|
|
memcpy(&packet->ephemeralkey, ¶ms[0].ephemeralkey, sizeof(secp256k1_pubkey));
|
2017-05-03 11:01:58 +02:00
|
|
|
|
|
|
|
for (i=0; i<num_hops; i++) {
|
2020-01-23 06:54:10 +01:00
|
|
|
secrets[i] = params[i].secret;
|
2017-05-03 11:01:58 +02:00
|
|
|
}
|
2017-05-03 12:40:03 +02:00
|
|
|
|
|
|
|
*path_secrets = secrets;
|
2017-04-13 19:55:14 +02:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
2020-01-23 06:48:42 +01:00
|
|
|
#if DEVELOPER
|
|
|
|
bool dev_fail_process_onionpacket;
|
|
|
|
#endif
|
|
|
|
|
2017-04-13 19:55:14 +02:00
|
|
|
/*
|
2017-12-28 14:41:54 +01:00
|
|
|
* Given an onionpacket msg extract the information for the current
|
2017-04-13 19:55:14 +02:00
|
|
|
* node and unwrap the remainder so that the node can forward it.
|
|
|
|
*/
|
|
|
|
struct route_step *process_onionpacket(
|
|
|
|
const tal_t *ctx,
|
|
|
|
const struct onionpacket *msg,
|
2020-01-23 06:54:10 +01:00
|
|
|
const struct secret *shared_secret,
|
2017-04-13 19:55:14 +02:00
|
|
|
const u8 *assocdata,
|
|
|
|
const size_t assocdatalen
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct route_step *step = talz(ctx, struct route_step);
|
2019-02-12 21:17:09 +01:00
|
|
|
u8 hmac[HMAC_SIZE];
|
2017-04-13 19:55:14 +02:00
|
|
|
struct keyset keys;
|
|
|
|
u8 blind[BLINDING_FACTOR_SIZE];
|
2019-02-20 14:27:37 +01:00
|
|
|
u8 paddedheader[2*ROUTING_INFO_SIZE];
|
2019-12-05 11:06:28 +01:00
|
|
|
size_t payload_size;
|
2019-07-30 07:25:03 +02:00
|
|
|
bigsize_t shift_size;
|
2019-12-05 11:06:28 +01:00
|
|
|
bool valid;
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
step->next = talz(step, struct onionpacket);
|
|
|
|
step->next->version = msg->version;
|
|
|
|
generate_key_set(shared_secret, &keys);
|
|
|
|
|
|
|
|
compute_packet_hmac(msg, assocdata, assocdatalen, keys.mu, hmac);
|
|
|
|
|
2020-01-23 06:48:42 +01:00
|
|
|
if (memcmp(msg->mac, hmac, sizeof(hmac)) != 0
|
|
|
|
|| IFDEV(dev_fail_process_onionpacket, false)) {
|
2017-09-07 22:08:25 +02:00
|
|
|
/* Computed MAC does not match expected MAC, the message was modified. */
|
2017-04-13 19:55:14 +02:00
|
|
|
return tal_free(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME:store seen secrets to avoid replay attacks
|
|
|
|
memset(paddedheader, 0, sizeof(paddedheader));
|
|
|
|
memcpy(paddedheader, msg->routinginfo, ROUTING_INFO_SIZE);
|
2020-02-27 03:17:21 +01:00
|
|
|
xor_cipher_stream(paddedheader, keys.rho, sizeof(paddedheader));
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
compute_blinding_factor(&msg->ephemeralkey, shared_secret, blind);
|
|
|
|
if (!blind_group_element(&step->next->ephemeralkey, &msg->ephemeralkey, blind))
|
|
|
|
return tal_free(step);
|
|
|
|
|
2019-12-05 11:06:28 +01:00
|
|
|
payload_size = onion_payload_length(paddedheader, ROUTING_INFO_SIZE,
|
|
|
|
&valid, NULL);
|
2019-02-20 14:27:37 +01:00
|
|
|
|
2019-12-05 11:06:28 +01:00
|
|
|
/* Can't decode? Treat it as terminal. */
|
|
|
|
if (!valid) {
|
|
|
|
shift_size = payload_size;
|
|
|
|
memset(step->next->mac, 0, sizeof(step->next->mac));
|
2019-02-20 14:27:37 +01:00
|
|
|
} else {
|
2019-12-05 11:06:28 +01:00
|
|
|
assert(payload_size <= ROUTING_INFO_SIZE - HMAC_SIZE);
|
|
|
|
/* Copy hmac */
|
|
|
|
shift_size = payload_size + HMAC_SIZE;
|
|
|
|
memcpy(step->next->mac, paddedheader + payload_size, HMAC_SIZE);
|
2019-02-20 14:27:37 +01:00
|
|
|
}
|
2019-12-05 11:06:28 +01:00
|
|
|
step->raw_payload = tal_dup_arr(step, u8, paddedheader, payload_size, 0);
|
2017-04-08 21:16:56 +02:00
|
|
|
|
2019-02-20 14:27:37 +01:00
|
|
|
/* Left shift the current payload out and make the remainder the new onion */
|
2019-12-05 11:06:28 +01:00
|
|
|
memcpy(&step->next->routinginfo, paddedheader + shift_size,
|
|
|
|
ROUTING_INFO_SIZE);
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
if (memeqzero(step->next->mac, sizeof(step->next->mac))) {
|
|
|
|
step->nextcase = ONION_END;
|
|
|
|
} else {
|
|
|
|
step->nextcase = ONION_FORWARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return step;
|
|
|
|
}
|
2017-04-13 02:58:19 +02:00
|
|
|
|
2020-01-23 00:38:04 +01:00
|
|
|
struct onionreply *create_onionreply(const tal_t *ctx,
|
|
|
|
const struct secret *shared_secret,
|
|
|
|
const u8 *failure_msg)
|
2017-04-13 02:58:19 +02:00
|
|
|
{
|
2018-07-28 08:00:16 +02:00
|
|
|
size_t msglen = tal_count(failure_msg);
|
2017-04-13 02:58:19 +02:00
|
|
|
size_t padlen = ONION_REPLY_SIZE - msglen;
|
2020-01-23 00:38:04 +01:00
|
|
|
struct onionreply *reply = tal(ctx, struct onionreply);
|
|
|
|
u8 *payload = tal_arr(ctx, u8, 0);
|
2017-04-13 02:58:19 +02:00
|
|
|
u8 key[KEY_LEN];
|
2017-05-03 19:38:13 +02:00
|
|
|
u8 hmac[HMAC_SIZE];
|
2017-04-13 02:58:19 +02:00
|
|
|
|
2017-06-06 05:08:30 +02:00
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2018-06-17 12:12:53 +02:00
|
|
|
* The node generating the error message (_erring node_) builds a return
|
|
|
|
* packet consisting of
|
2017-06-06 05:08:30 +02:00
|
|
|
* the following fields:
|
|
|
|
*
|
|
|
|
* 1. data:
|
2019-07-16 01:20:37 +02:00
|
|
|
* * [`32*byte`:`hmac`]
|
|
|
|
* * [`u16`:`failure_len`]
|
|
|
|
* * [`failure_len*byte`:`failuremsg`]
|
|
|
|
* * [`u16`:`pad_len`]
|
|
|
|
* * [`pad_len*byte`:`pad`]
|
2017-06-06 05:08:30 +02:00
|
|
|
*/
|
2017-04-13 02:58:19 +02:00
|
|
|
towire_u16(&payload, msglen);
|
|
|
|
towire(&payload, failure_msg, msglen);
|
|
|
|
towire_u16(&payload, padlen);
|
|
|
|
towire_pad(&payload, padlen);
|
2017-06-06 05:08:30 +02:00
|
|
|
|
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2018-06-17 12:12:53 +02:00
|
|
|
* The _erring node_:
|
|
|
|
* - SHOULD set `pad` such that the `failure_len` plus `pad_len` is
|
|
|
|
* equal to 256.
|
|
|
|
* - Note: this value is 118 bytes longer than the longest
|
|
|
|
* currently-defined message.
|
2017-06-06 05:08:30 +02:00
|
|
|
*/
|
2018-07-28 08:00:16 +02:00
|
|
|
assert(tal_count(payload) == ONION_REPLY_SIZE + 4);
|
2017-04-13 02:58:19 +02:00
|
|
|
|
2017-06-06 05:08:30 +02:00
|
|
|
/* BOLT #4:
|
|
|
|
*
|
|
|
|
* Where `hmac` is an HMAC authenticating the remainder of the packet,
|
2018-06-17 12:12:53 +02:00
|
|
|
* with a key generated using the above process, with key type `um`
|
2017-06-06 05:08:30 +02:00
|
|
|
*/
|
2020-01-23 06:54:10 +01:00
|
|
|
generate_key(key, "um", 2, shared_secret);
|
2017-04-13 02:58:19 +02:00
|
|
|
|
2018-07-28 08:00:16 +02:00
|
|
|
compute_hmac(hmac, payload, tal_count(payload), key, KEY_LEN);
|
2020-01-23 00:38:04 +01:00
|
|
|
reply->contents = tal_arr(reply, u8, 0),
|
|
|
|
towire(&reply->contents, hmac, sizeof(hmac));
|
2017-06-06 05:08:30 +02:00
|
|
|
|
2020-01-23 00:38:04 +01:00
|
|
|
towire(&reply->contents, payload, tal_count(payload));
|
2017-04-13 02:58:19 +02:00
|
|
|
tal_free(payload);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2020-01-23 00:38:04 +01:00
|
|
|
struct onionreply *wrap_onionreply(const tal_t *ctx,
|
|
|
|
const struct secret *shared_secret,
|
|
|
|
const struct onionreply *reply)
|
2017-04-13 02:58:19 +02:00
|
|
|
{
|
|
|
|
u8 key[KEY_LEN];
|
2020-01-23 00:38:04 +01:00
|
|
|
struct onionreply *result = tal(ctx, struct onionreply);
|
2017-06-06 05:08:30 +02:00
|
|
|
|
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2018-06-17 12:12:53 +02:00
|
|
|
* The erring node then generates a new key, using the key type `ammag`.
|
2017-06-06 05:08:30 +02:00
|
|
|
* This key is then used to generate a pseudo-random stream, which is
|
2018-06-17 12:12:53 +02:00
|
|
|
* in turn applied to the packet using `XOR`.
|
2017-06-06 05:08:30 +02:00
|
|
|
*
|
2018-06-17 12:12:53 +02:00
|
|
|
* The obfuscation step is repeated by every hop along the return path.
|
2017-06-06 05:08:30 +02:00
|
|
|
*/
|
2020-01-23 06:54:10 +01:00
|
|
|
generate_key(key, "ammag", 5, shared_secret);
|
2020-02-27 03:17:21 +01:00
|
|
|
result->contents = tal_dup_talarr(result, u8, reply->contents);
|
|
|
|
xor_cipher_stream(result->contents, key, tal_bytelen(result->contents));
|
2017-04-13 02:58:19 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-22 06:59:00 +01:00
|
|
|
u8 *unwrap_onionreply(const tal_t *ctx,
|
|
|
|
const struct secret *shared_secrets,
|
2020-01-23 00:38:04 +01:00
|
|
|
const int numhops,
|
|
|
|
const struct onionreply *reply,
|
2020-01-22 06:59:00 +01:00
|
|
|
int *origin_index)
|
2017-04-13 02:58:19 +02:00
|
|
|
{
|
2020-01-23 00:38:04 +01:00
|
|
|
struct onionreply *r;
|
2017-05-03 19:38:13 +02:00
|
|
|
u8 key[KEY_LEN], hmac[HMAC_SIZE];
|
2017-04-13 02:58:19 +02:00
|
|
|
const u8 *cursor;
|
2020-01-23 00:38:04 +01:00
|
|
|
u8 *final;
|
2017-04-13 02:58:19 +02:00
|
|
|
size_t max;
|
|
|
|
u16 msglen;
|
|
|
|
|
2020-01-23 00:38:04 +01:00
|
|
|
if (tal_count(reply->contents) != ONION_REPLY_SIZE + sizeof(hmac) + 4) {
|
2018-03-15 07:10:20 +01:00
|
|
|
return NULL;
|
2017-05-03 12:40:03 +02:00
|
|
|
}
|
|
|
|
|
2020-01-23 00:38:04 +01:00
|
|
|
r = new_onionreply(tmpctx, reply->contents);
|
2020-01-22 06:59:00 +01:00
|
|
|
*origin_index = -1;
|
2017-04-13 02:58:19 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < numhops; i++) {
|
|
|
|
/* Since the encryption is just XORing with the cipher
|
|
|
|
* stream encryption is identical to decryption */
|
2020-01-23 00:38:04 +01:00
|
|
|
r = wrap_onionreply(tmpctx, &shared_secrets[i], r);
|
2017-04-13 02:58:19 +02:00
|
|
|
|
|
|
|
/* Check if the HMAC matches, this means that this is
|
|
|
|
* the origin */
|
2020-01-23 06:54:10 +01:00
|
|
|
generate_key(key, "um", 2, &shared_secrets[i]);
|
2020-01-23 00:38:04 +01:00
|
|
|
compute_hmac(hmac, r->contents + sizeof(hmac),
|
|
|
|
tal_count(r->contents) - sizeof(hmac),
|
|
|
|
key, KEY_LEN);
|
|
|
|
if (memcmp(hmac, r->contents, sizeof(hmac)) == 0) {
|
2020-01-22 06:59:00 +01:00
|
|
|
*origin_index = i;
|
2017-04-13 02:58:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 06:59:00 +01:00
|
|
|
if (*origin_index == -1) {
|
2018-03-15 07:10:20 +01:00
|
|
|
return NULL;
|
2017-04-13 02:58:19 +02:00
|
|
|
}
|
|
|
|
|
2020-01-23 00:38:04 +01:00
|
|
|
cursor = r->contents + sizeof(hmac);
|
|
|
|
max = tal_count(r->contents) - sizeof(hmac);
|
2017-04-13 02:58:19 +02:00
|
|
|
msglen = fromwire_u16(&cursor, &max);
|
|
|
|
|
|
|
|
if (msglen > ONION_REPLY_SIZE) {
|
2018-03-15 07:10:20 +01:00
|
|
|
return NULL;
|
2017-04-13 02:58:19 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 06:59:00 +01:00
|
|
|
final = tal_arr(ctx, u8, msglen);
|
2020-01-23 00:38:04 +01:00
|
|
|
if (!fromwire(&cursor, &max, final, msglen))
|
|
|
|
return tal_free(final);
|
2020-01-22 06:59:00 +01:00
|
|
|
return final;
|
2017-04-13 02:58:19 +02:00
|
|
|
}
|
2020-02-28 17:55:43 +01:00
|
|
|
|
2020-03-02 15:00:25 +01:00
|
|
|
struct onionpacket *sphinx_decompress(const tal_t *ctx,
|
|
|
|
const struct sphinx_compressed_onion *src,
|
|
|
|
const struct secret *shared_secret)
|
2020-02-28 17:55:43 +01:00
|
|
|
{
|
2020-03-02 15:00:25 +01:00
|
|
|
struct onionpacket *res = tal(ctx, struct onionpacket);
|
|
|
|
size_t srclen = tal_bytelen(src->routinginfo);
|
|
|
|
size_t prefill_size = ROUTING_INFO_SIZE - srclen;
|
|
|
|
|
|
|
|
res->version = src->version;
|
|
|
|
res->ephemeralkey = src->ephemeralkey;
|
|
|
|
memcpy(res->mac, src->mac, HMAC_SIZE);
|
|
|
|
|
|
|
|
/* Decompress routinginfo by copying the unmodified prefix, setting
|
|
|
|
* the compressed suffix to 0x00 bytes and then xoring the obfuscation
|
|
|
|
* stream in place. */
|
|
|
|
memset(res->routinginfo, 0, ROUTING_INFO_SIZE);
|
|
|
|
memcpy(res->routinginfo, src->routinginfo, srclen);
|
|
|
|
sphinx_prefill_stream_xor(res->routinginfo + srclen, prefill_size,
|
|
|
|
shared_secret);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sphinx_compressed_onion *
|
|
|
|
sphinx_compress(const tal_t *ctx, const struct onionpacket *packet,
|
|
|
|
const struct sphinx_path *path)
|
|
|
|
{
|
|
|
|
struct sphinx_compressed_onion *res;
|
|
|
|
size_t payloads_size = sphinx_path_payloads_size(path);
|
|
|
|
|
|
|
|
/* We can't compress an onion that doesn't have a rendez-vous node. */
|
|
|
|
if (path->rendezvous_id)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
res = tal(ctx, struct sphinx_compressed_onion);
|
|
|
|
res->version = packet->version;
|
|
|
|
res->ephemeralkey = packet->ephemeralkey;
|
|
|
|
memcpy(res->mac, packet->mac, HMAC_SIZE);
|
|
|
|
|
|
|
|
res->routinginfo = tal_arr(res, u8, payloads_size);
|
|
|
|
memcpy(res->routinginfo, packet->routinginfo, payloads_size);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 *sphinx_compressed_onion_serialize(const tal_t *ctx, const struct sphinx_compressed_onion *onion)
|
|
|
|
{
|
|
|
|
size_t routelen = tal_bytelen(onion->routinginfo);
|
|
|
|
size_t len = VERSION_SIZE + PUBKEY_SIZE + routelen + HMAC_SIZE;
|
|
|
|
u8 *dst = tal_arr(ctx, u8, len);
|
|
|
|
u8 der[PUBKEY_CMPR_LEN];
|
|
|
|
int p = 0;
|
|
|
|
|
|
|
|
pubkey_to_der(der, &onion->ephemeralkey);
|
|
|
|
|
|
|
|
write_buffer(dst, &onion->version, VERSION_SIZE, &p);
|
|
|
|
write_buffer(dst, der, PUBKEY_SIZE, &p);
|
|
|
|
write_buffer(dst, onion->routinginfo, routelen, &p);
|
|
|
|
write_buffer(dst, onion->mac, HMAC_SIZE, &p);
|
|
|
|
|
|
|
|
assert(p == len);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sphinx_compressed_onion *
|
|
|
|
sphinx_compressed_onion_deserialize(const tal_t *ctx, const u8 *src)
|
|
|
|
{
|
|
|
|
size_t srclen = tal_bytelen(src);
|
|
|
|
size_t routelen = srclen - VERSION_SIZE - PUBKEY_SIZE - HMAC_SIZE;
|
|
|
|
struct sphinx_compressed_onion *dst =
|
|
|
|
tal(ctx, struct sphinx_compressed_onion);
|
2020-02-28 17:55:43 +01:00
|
|
|
int p = 0;
|
2020-03-02 15:00:25 +01:00
|
|
|
u8 ephkey[PUBKEY_SIZE];
|
|
|
|
|
|
|
|
assert(srclen <= TOTAL_PACKET_SIZE);
|
2020-02-28 17:55:43 +01:00
|
|
|
|
2020-03-02 15:00:25 +01:00
|
|
|
read_buffer(&dst->version, src, 1, &p);
|
|
|
|
if (dst->version != 0x00)
|
|
|
|
return tal_free(dst);
|
|
|
|
|
|
|
|
read_buffer(ephkey, src, PUBKEY_SIZE, &p);
|
|
|
|
|
|
|
|
if (!pubkey_from_der(ephkey, PUBKEY_SIZE, &dst->ephemeralkey)) {
|
|
|
|
return tal_free(dst);
|
|
|
|
}
|
2020-02-28 17:55:43 +01:00
|
|
|
|
2020-03-02 15:00:25 +01:00
|
|
|
dst->routinginfo = tal_arr(dst, u8, routelen);
|
|
|
|
read_buffer(dst->routinginfo, src, routelen, &p);
|
|
|
|
read_buffer(&dst->mac, src, HMAC_SIZE, &p);
|
|
|
|
assert(p == srclen);
|
2020-02-28 17:55:43 +01:00
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|