2017-04-13 19:55:14 +02:00
|
|
|
#ifndef LIGHTNING_DAEMON_SPHINX_H
|
|
|
|
#define LIGHTNING_DAEMON_SPHINX_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "bitcoin/privkey.h"
|
|
|
|
#include "bitcoin/pubkey.h"
|
|
|
|
|
|
|
|
#include <ccan/short_types/short_types.h>
|
|
|
|
#include <ccan/tal/tal.h>
|
|
|
|
#include <secp256k1.h>
|
|
|
|
#include <sodium/randombytes.h>
|
2017-04-08 21:16:56 +02:00
|
|
|
#include <wire/wire.h>
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2017-04-14 01:22:15 +02:00
|
|
|
#define SECURITY_PARAMETER 32
|
2017-04-13 19:55:14 +02:00
|
|
|
#define NUM_MAX_HOPS 20
|
2017-04-14 01:22:15 +02:00
|
|
|
#define PAYLOAD_SIZE 32
|
|
|
|
#define HOP_DATA_SIZE (1 + SECURITY_PARAMETER + PAYLOAD_SIZE)
|
2017-04-08 21:16:56 +02:00
|
|
|
#define ROUTING_INFO_SIZE (HOP_DATA_SIZE * NUM_MAX_HOPS)
|
2017-04-13 20:12:07 +02:00
|
|
|
#define TOTAL_PACKET_SIZE (1 + 33 + SECURITY_PARAMETER + ROUTING_INFO_SIZE)
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
struct onionpacket {
|
|
|
|
/* Cleartext information */
|
|
|
|
u8 version;
|
2017-04-14 01:22:15 +02:00
|
|
|
u8 mac[SECURITY_PARAMETER];
|
2017-04-13 19:55:14 +02:00
|
|
|
secp256k1_pubkey ephemeralkey;
|
|
|
|
|
|
|
|
/* Encrypted information */
|
|
|
|
u8 routinginfo[ROUTING_INFO_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
enum route_next_case {
|
|
|
|
ONION_END = 0,
|
|
|
|
ONION_FORWARD = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The `hops_data` field is a structure that holds obfuscated versions of the
|
|
|
|
* next hop's address, transfer information and the associated HMAC. It is
|
|
|
|
* 1300 bytes long, and has the following structure:
|
|
|
|
*
|
|
|
|
* 1. type: `hops_data`
|
|
|
|
* 2. data:
|
|
|
|
* * [`1`:`realm`]
|
|
|
|
* * [`32`:`per_hop`]
|
|
|
|
* * [`32`:`HMAC`]
|
|
|
|
* * ...
|
|
|
|
* * `filler`
|
|
|
|
*
|
|
|
|
* Where the `realm`, `HMAC` and `per_hop` (whose contents depend on `realm`)
|
|
|
|
* are repeated for each hop, and `filler` consists of obfuscated
|
|
|
|
* deterministically generated padding. For details about how the `filler` is
|
|
|
|
* generated please see below. In addition, `hops_data` is incrementally
|
|
|
|
* obfuscated at each hop.
|
|
|
|
*
|
|
|
|
* The `realm` byte determines the format of the `per_hop`; so far only
|
|
|
|
* `realm` 0 is defined, and for that, the `per_hop` format is:
|
|
|
|
*
|
|
|
|
* 1. type: `per_hop` (for `realm` 0)
|
|
|
|
* 2. data:
|
|
|
|
* * [`8`:`channel_id`]
|
2017-06-06 01:49:10 +02:00
|
|
|
* * [`8`:`amt_to_forward`]
|
2017-06-06 01:48:10 +02:00
|
|
|
* * [`4`:`outgoing_cltv_value`]
|
2017-06-06 01:49:10 +02:00
|
|
|
* * [`12`:`padding`]
|
2017-06-06 01:48:10 +02:00
|
|
|
*/
|
2017-04-08 21:16:56 +02:00
|
|
|
struct hop_data {
|
2017-04-13 19:55:14 +02:00
|
|
|
u8 realm;
|
2017-04-08 21:16:56 +02:00
|
|
|
struct short_channel_id channel_id;
|
2017-07-12 18:34:31 +02:00
|
|
|
u64 amt_forward;
|
2017-04-08 21:16:56 +02:00
|
|
|
u32 outgoing_cltv;
|
|
|
|
/* Padding omitted, will be zeroed */
|
|
|
|
u8 hmac[SECURITY_PARAMETER];
|
2017-04-13 19:55:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct route_step {
|
|
|
|
enum route_next_case nextcase;
|
|
|
|
struct onionpacket *next;
|
2017-04-08 21:16:56 +02:00
|
|
|
struct hop_data hop_data;
|
2017-04-13 19:55:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create_onionpacket - Create a new onionpacket that can be routed
|
|
|
|
* over a path of intermediate nodes.
|
|
|
|
*
|
|
|
|
* @ctx: tal context to allocate from
|
|
|
|
* @path: public keys of nodes along the path.
|
|
|
|
* @hoppayloads: payloads destined for individual hosts (limited to
|
|
|
|
* HOP_PAYLOAD_SIZE bytes)
|
|
|
|
* @num_hops: path length in nodes
|
2017-04-14 01:22:15 +02:00
|
|
|
* @sessionkey: 32 byte random session key to derive secrets from
|
2017-04-13 19:55:14 +02:00
|
|
|
* @assocdata: associated data to commit to in HMACs
|
|
|
|
* @assocdatalen: length of the assocdata
|
2017-05-03 11:01:58 +02:00
|
|
|
* @path_secrets: (out) shared secrets generated for the entire path
|
2017-04-13 19:55:14 +02:00
|
|
|
*/
|
|
|
|
struct onionpacket *create_onionpacket(
|
|
|
|
const tal_t * ctx,
|
|
|
|
struct pubkey path[],
|
2017-04-08 21:16:56 +02:00
|
|
|
struct hop_data hops_data[],
|
2017-04-13 19:55:14 +02:00
|
|
|
const u8 * sessionkey,
|
|
|
|
const u8 *assocdata,
|
2017-05-03 11:01:58 +02:00
|
|
|
const size_t assocdatalen,
|
2017-05-06 04:19:44 +02:00
|
|
|
struct secret **path_secrets
|
2017-04-13 19:55:14 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* onion_shared_secret - calculate ECDH shared secret between nodes.
|
|
|
|
*
|
|
|
|
* @secret: the shared secret (32 bytes long)
|
|
|
|
* @pubkey: the public key of the other node
|
|
|
|
* @privkey: the private key of this node (32 bytes long)
|
|
|
|
*/
|
|
|
|
bool onion_shared_secret(
|
|
|
|
u8 *secret,
|
|
|
|
const struct onionpacket *packet,
|
|
|
|
const struct privkey *privkey);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* process_onionpacket - process an incoming packet by stripping one
|
|
|
|
* onion layer and return the packet for the next hop.
|
|
|
|
*
|
|
|
|
* @ctx: tal context to allocate from
|
|
|
|
* @packet: incoming packet being processed
|
|
|
|
* @shared_secret: the result of onion_shared_secret.
|
|
|
|
* @hoppayload: the per-hop payload destined for the processing node.
|
|
|
|
* @assocdata: associated data to commit to in HMACs
|
|
|
|
* @assocdatalen: length of the assocdata
|
|
|
|
*/
|
|
|
|
struct route_step *process_onionpacket(
|
|
|
|
const tal_t * ctx,
|
|
|
|
const struct onionpacket *packet,
|
|
|
|
const u8 *shared_secret,
|
|
|
|
const u8 *assocdata,
|
|
|
|
const size_t assocdatalen
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* serialize_onionpacket - Serialize an onionpacket to a buffer.
|
|
|
|
*
|
|
|
|
* @ctx: tal context to allocate from
|
|
|
|
* @packet: the packet to serialize
|
|
|
|
*/
|
|
|
|
u8 *serialize_onionpacket(
|
|
|
|
const tal_t *ctx,
|
|
|
|
const struct onionpacket *packet);
|
|
|
|
|
|
|
|
/**
|
2017-04-13 02:58:19 +02:00
|
|
|
* parse_onionpacket - Parse an onionpacket from a buffer.
|
2017-04-13 19:55:14 +02:00
|
|
|
*
|
|
|
|
* @ctx: tal context to allocate from
|
|
|
|
* @src: buffer to read the packet from
|
|
|
|
* @srclen: length of the @src
|
|
|
|
*/
|
|
|
|
struct onionpacket *parse_onionpacket(
|
|
|
|
const tal_t *ctx,
|
|
|
|
const void *src,
|
|
|
|
const size_t srclen
|
|
|
|
);
|
|
|
|
|
2017-04-13 02:58:19 +02:00
|
|
|
struct onionreply {
|
|
|
|
/* Node index in the path that is replying */
|
|
|
|
int origin_index;
|
|
|
|
u8 *msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create_onionreply - Format a failure message so we can return it
|
|
|
|
*
|
|
|
|
* @ctx: tal context to allocate from
|
|
|
|
* @shared_secret: The shared secret used in the forward direction, used for the
|
|
|
|
* HMAC
|
|
|
|
* @failure_msg: message (must support tal_len)
|
|
|
|
*/
|
2017-05-06 04:19:44 +02:00
|
|
|
u8 *create_onionreply(const tal_t *ctx, const struct secret *shared_secret,
|
|
|
|
const u8 *failure_msg);
|
2017-04-13 02:58:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* wrap_onionreply - Add another encryption layer to the reply.
|
|
|
|
*
|
|
|
|
* @ctx: tal context to allocate from
|
|
|
|
* @shared_secret: the shared secret associated with the HTLC, used for the
|
|
|
|
* encryption.
|
|
|
|
* @reply: the reply to wrap
|
|
|
|
*/
|
2017-05-06 04:19:44 +02:00
|
|
|
u8 *wrap_onionreply(const tal_t *ctx, const struct secret *shared_secret,
|
|
|
|
const u8 *reply);
|
2017-04-13 02:58:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* unwrap_onionreply - Remove layers, check integrity and parse reply
|
|
|
|
*
|
|
|
|
* @ctx: tal context to allocate from
|
|
|
|
* @shared_secrets: shared secrets from the forward path
|
|
|
|
* @numhops: path length and number of shared_secrets provided
|
|
|
|
* @reply: the incoming reply
|
|
|
|
*/
|
2017-05-06 04:19:44 +02:00
|
|
|
struct onionreply *unwrap_onionreply(const tal_t *ctx,
|
|
|
|
const struct secret *shared_secrets,
|
2017-04-13 02:58:19 +02:00
|
|
|
const int numhops, const u8 *reply);
|
|
|
|
|
2017-04-13 19:55:14 +02:00
|
|
|
#endif /* LIGHTNING_DAEMON_SPHINX_H */
|