2017-08-28 18:06:01 +02:00
|
|
|
#ifndef LIGHTNING_COMMON_SPHINX_H
|
|
|
|
#define LIGHTNING_COMMON_SPHINX_H
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "bitcoin/privkey.h"
|
|
|
|
#include "bitcoin/pubkey.h"
|
|
|
|
|
|
|
|
#include <ccan/short_types/short_types.h>
|
|
|
|
#include <ccan/tal/tal.h>
|
2020-03-20 02:29:24 +01:00
|
|
|
#include <common/hmac.h>
|
2017-04-13 19:55:14 +02:00
|
|
|
#include <secp256k1.h>
|
|
|
|
#include <sodium/randombytes.h>
|
2020-09-07 23:06:50 +02:00
|
|
|
#include <wire/onion_wire.h>
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2020-05-16 03:29:05 +02:00
|
|
|
struct node_id;
|
|
|
|
|
2019-02-12 21:17:09 +01:00
|
|
|
#define VERSION_SIZE 1
|
|
|
|
#define REALM_SIZE 1
|
|
|
|
#define HMAC_SIZE 32
|
|
|
|
#define PUBKEY_SIZE 33
|
|
|
|
#define FRAME_SIZE 65
|
2019-05-29 17:20:28 +02:00
|
|
|
#define ROUTING_INFO_SIZE 1300
|
2019-02-12 21:17:09 +01:00
|
|
|
#define TOTAL_PACKET_SIZE (VERSION_SIZE + PUBKEY_SIZE + HMAC_SIZE + ROUTING_INFO_SIZE)
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
struct onionpacket {
|
|
|
|
/* Cleartext information */
|
|
|
|
u8 version;
|
2020-03-20 02:29:24 +01:00
|
|
|
struct hmac hmac;
|
2019-01-08 01:53:26 +01:00
|
|
|
struct pubkey ephemeralkey;
|
2017-04-13 19:55:14 +02:00
|
|
|
|
|
|
|
/* Encrypted information */
|
|
|
|
u8 routinginfo[ROUTING_INFO_SIZE];
|
|
|
|
};
|
|
|
|
|
2020-03-02 15:00:25 +01:00
|
|
|
struct sphinx_compressed_onion {
|
|
|
|
u8 version;
|
|
|
|
struct pubkey ephemeralkey;
|
|
|
|
u8 *routinginfo;
|
2020-03-20 02:29:24 +01:00
|
|
|
struct hmac hmac;
|
2020-03-02 15:00:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-04-13 19:55:14 +02:00
|
|
|
enum route_next_case {
|
|
|
|
ONION_END = 0,
|
|
|
|
ONION_FORWARD = 1,
|
|
|
|
};
|
|
|
|
|
2019-02-14 14:54:17 +01:00
|
|
|
/**
|
|
|
|
* A sphinx payment path.
|
|
|
|
*
|
|
|
|
* This struct defines a path a payment is taking through the Lightning
|
|
|
|
* Network, including the session_key used to generate secrets, the associated
|
|
|
|
* data that'll be included in the HMACs and the payloads at each hop in the
|
|
|
|
* path. The struct is opaque since it should not be modified externally. Use
|
|
|
|
* `sphinx_path_new` or `sphinx_path_new_with_key` (testing only) to create a
|
|
|
|
* new instance.
|
|
|
|
*/
|
|
|
|
struct sphinx_path;
|
|
|
|
|
2017-04-13 19:55:14 +02:00
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2019-08-02 05:24:25 +02:00
|
|
|
* ## Legacy `hop_data` payload format
|
2017-06-06 01:48:10 +02:00
|
|
|
*
|
2019-08-02 05:24:25 +02:00
|
|
|
* The `hop_data` format is identified by a single `0x00`-byte length,
|
2019-08-02 05:24:25 +02:00
|
|
|
* for backward compatibility. Its payload is defined as:
|
2017-06-06 01:48:10 +02:00
|
|
|
*
|
2019-08-02 05:24:25 +02:00
|
|
|
* 1. type: `hop_data` (for `realm` 0)
|
2017-06-06 01:48:10 +02:00
|
|
|
* 2. data:
|
2019-07-16 01:20:37 +02:00
|
|
|
* * [`short_channel_id`:`short_channel_id`]
|
|
|
|
* * [`u64`:`amt_to_forward`]
|
|
|
|
* * [`u32`:`outgoing_cltv_value`]
|
|
|
|
* * [`12*byte`:`padding`]
|
2017-06-06 01:48:10 +02:00
|
|
|
*/
|
2019-11-14 01:13:53 +01:00
|
|
|
struct hop_data_legacy {
|
2017-04-13 19:55:14 +02:00
|
|
|
u8 realm;
|
2017-04-08 21:16:56 +02:00
|
|
|
struct short_channel_id channel_id;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat amt_forward;
|
2017-04-08 21:16:56 +02:00
|
|
|
u32 outgoing_cltv;
|
2019-02-20 14:27:37 +01:00
|
|
|
};
|
|
|
|
|
2019-11-24 18:30:30 +01:00
|
|
|
/*
|
|
|
|
* All the necessary information to generate a valid onion for this hop on a
|
|
|
|
* sphinx path. The payload is preserialized in order since the onion
|
|
|
|
* generation is payload agnostic. */
|
|
|
|
struct sphinx_hop {
|
|
|
|
struct pubkey pubkey;
|
2019-12-05 11:06:28 +01:00
|
|
|
const u8 *raw_payload;
|
2020-03-20 02:29:24 +01:00
|
|
|
struct hmac hmac;
|
2019-11-24 18:30:30 +01:00
|
|
|
};
|
|
|
|
|
2017-04-13 19:55:14 +02:00
|
|
|
struct route_step {
|
|
|
|
enum route_next_case nextcase;
|
|
|
|
struct onionpacket *next;
|
2019-02-21 14:39:50 +01:00
|
|
|
u8 *raw_payload;
|
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,
|
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
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* onion_shared_secret - calculate ECDH shared secret between nodes.
|
|
|
|
*
|
2020-01-23 06:54:10 +01:00
|
|
|
* @secret: the shared secret
|
2017-04-13 19:55:14 +02:00
|
|
|
* @pubkey: the public key of the other node
|
|
|
|
* @privkey: the private key of this node (32 bytes long)
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2020-03-12 00:59:01 +01:00
|
|
|
* @has_realm: used for HTLCs, where first byte 0 is magical.
|
2017-04-13 19:55:14 +02:00
|
|
|
*/
|
|
|
|
struct route_step *process_onionpacket(
|
|
|
|
const tal_t * ctx,
|
|
|
|
const struct onionpacket *packet,
|
2020-01-23 06:54:10 +01:00
|
|
|
const struct secret *shared_secret,
|
2017-04-13 19:55:14 +02:00
|
|
|
const u8 *assocdata,
|
2020-03-12 00:59:01 +01:00
|
|
|
const size_t assocdatalen,
|
|
|
|
bool has_realm
|
2017-04-13 19:55:14 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* @src: buffer to read the packet from
|
2019-01-08 01:21:50 +01:00
|
|
|
* @srclen: length of the @src (must be TOTAL_PACKET_SIZE)
|
2019-11-29 21:20:18 +01:00
|
|
|
* @dest: the destination into which we should parse the packet
|
2017-04-13 19:55:14 +02:00
|
|
|
*/
|
2020-08-31 03:13:25 +02:00
|
|
|
enum onion_wire parse_onionpacket(const u8 *src,
|
2019-11-29 21:20:18 +01:00
|
|
|
const size_t srclen,
|
|
|
|
struct onionpacket *dest);
|
2017-04-13 19:55:14 +02:00
|
|
|
|
2017-04-13 02:58:19 +02:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2020-01-22 06:59:00 +01:00
|
|
|
* @origin_index: the index in the path where the reply came from (-1 if unknown)
|
2020-01-23 00:38:04 +01:00
|
|
|
*
|
|
|
|
* Reverses create_onionreply and wrap_onionreply.
|
2017-04-13 02:58:19 +02:00
|
|
|
*/
|
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
|
|
|
|
2019-02-14 14:54:17 +01:00
|
|
|
/**
|
|
|
|
* Create a new empty sphinx_path.
|
|
|
|
*
|
|
|
|
* The sphinx_path instance can then be decorated with other functions and
|
|
|
|
* passed to `create_onionpacket` to generate the packet.
|
|
|
|
*/
|
|
|
|
struct sphinx_path *sphinx_path_new(const tal_t *ctx,
|
|
|
|
const u8 *associated_data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new empty sphinx_path with a given `session_key`.
|
|
|
|
*
|
|
|
|
* This MUST NOT be used outside of tests and tools as it may leak the path
|
|
|
|
* details if the `session_key` is not randomly generated.
|
|
|
|
*/
|
|
|
|
struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx,
|
|
|
|
const u8 *associated_data,
|
|
|
|
const struct secret *session_key);
|
|
|
|
|
2019-02-18 14:02:25 +01:00
|
|
|
/**
|
2019-12-05 11:06:28 +01:00
|
|
|
* Add a payload hop to the path.
|
2019-11-14 01:19:53 +01:00
|
|
|
*/
|
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-11-14 01:19:53 +01:00
|
|
|
|
2020-01-08 22:05:16 +01:00
|
|
|
/**
|
|
|
|
* Compute the size of the serialized payloads.
|
|
|
|
*/
|
|
|
|
size_t sphinx_path_payloads_size(const struct sphinx_path *path);
|
|
|
|
|
2020-02-28 14:56:59 +01:00
|
|
|
/**
|
|
|
|
* Set the rendez-vous node_id and make the onion generated from the
|
|
|
|
* sphinx_path compressible. To unset pass in a NULL rendezvous_id.
|
|
|
|
*
|
|
|
|
* Returns false if there was an error converting from the node_id to a public
|
|
|
|
* key.
|
|
|
|
*/
|
|
|
|
bool sphinx_path_set_rendezvous(struct sphinx_path *sp,
|
|
|
|
const struct node_id *rendezvous_id);
|
|
|
|
|
2020-02-28 17:55:43 +01:00
|
|
|
/**
|
|
|
|
* Given a compressed onion expand it by re-generating the prefiller and
|
|
|
|
* inserting it in the appropriate place.
|
|
|
|
*/
|
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 19:39:16 +01:00
|
|
|
/**
|
|
|
|
* Use ECDH to generate a shared secret from a privkey and a pubkey.
|
|
|
|
*
|
|
|
|
* Sphinx uses shared secrets derived from a private key and a public key
|
|
|
|
* using ECDH in a number of places. This is a simple wrapper around the
|
|
|
|
* secp256k1 functions, with our internal types.
|
|
|
|
*/
|
|
|
|
bool sphinx_create_shared_secret(struct secret *privkey,
|
|
|
|
const struct pubkey *pubkey,
|
|
|
|
const struct secret *secret);
|
|
|
|
|
2020-03-02 15:00:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a compressible onionpacket, return the compressed version.
|
|
|
|
*/
|
|
|
|
struct sphinx_compressed_onion *
|
|
|
|
sphinx_compress(const tal_t *ctx, const struct onionpacket *packet,
|
|
|
|
const struct sphinx_path *path);
|
|
|
|
|
|
|
|
u8 *sphinx_compressed_onion_serialize(
|
|
|
|
const tal_t *ctx, const struct sphinx_compressed_onion *onion);
|
|
|
|
|
|
|
|
struct sphinx_compressed_onion *
|
|
|
|
sphinx_compressed_onion_deserialize(const tal_t *ctx, const u8 *src);
|
|
|
|
|
2020-01-23 06:48:42 +01:00
|
|
|
#if DEVELOPER
|
|
|
|
/* Override to force us to reject valid onion packets */
|
|
|
|
extern bool dev_fail_process_onionpacket;
|
|
|
|
#endif
|
|
|
|
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_COMMON_SPHINX_H */
|