2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2020-12-09 04:57:50 +01:00
|
|
|
#include <ccan/cast/cast.h>
|
|
|
|
#include <common/blindedpath.h>
|
|
|
|
#include <common/blinding.h>
|
|
|
|
#include <common/bolt11.h>
|
|
|
|
#include <common/hmac.h>
|
|
|
|
#include <secp256k1_ecdh.h>
|
|
|
|
#include <sodium.h>
|
|
|
|
#include <wire/onion_wire.h>
|
|
|
|
|
2021-09-21 23:17:16 +02:00
|
|
|
#ifndef SUPERVERBOSE
|
|
|
|
#define SUPERVERBOSE(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Blinds node_id and calculates next blinding factor. */
|
|
|
|
static bool blind_node(const struct privkey *blinding,
|
|
|
|
const struct secret *ss,
|
|
|
|
const struct pubkey *node,
|
|
|
|
struct pubkey *node_alias,
|
|
|
|
struct privkey *next_blinding)
|
|
|
|
{
|
|
|
|
struct pubkey blinding_pubkey;
|
|
|
|
struct sha256 h;
|
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
if (!blindedpath_get_alias(ss, node, node_alias))
|
2021-09-21 23:17:16 +02:00
|
|
|
return false;
|
|
|
|
SUPERVERBOSE("\t\"blinded_node_id\": \"%s\",\n",
|
|
|
|
type_to_string(tmpctx, struct pubkey, node_alias));
|
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
/* BOLT-route-blinding #4:
|
|
|
|
* - `E(i+1) = SHA256(E(i) || ss(i)) * E(i)`
|
|
|
|
* (NB: `N(i)` MUST NOT learn `e(i)`)
|
2021-09-21 23:17:16 +02:00
|
|
|
*/
|
|
|
|
if (!pubkey_from_privkey(blinding, &blinding_pubkey))
|
|
|
|
return false;
|
|
|
|
SUPERVERBOSE("\t\"E\": \"%s\",\n",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &blinding_pubkey));
|
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
/* BOLT-route-blinding #4:
|
|
|
|
* - `e(i+1) = SHA256(E(i) || ss(i)) * e(i)`
|
|
|
|
* (blinding ephemeral private key, only known by `N(r)`)
|
|
|
|
*/
|
2021-09-21 23:17:16 +02:00
|
|
|
blinding_hash_e_and_ss(&blinding_pubkey, ss, &h);
|
|
|
|
SUPERVERBOSE("\t\"H(E || ss)\": \"%s\",\n",
|
|
|
|
type_to_string(tmpctx, struct sha256, &h));
|
|
|
|
blinding_next_privkey(blinding, &h, next_blinding);
|
|
|
|
SUPERVERBOSE("\t\"next_e\": \"%s\",\n",
|
|
|
|
type_to_string(tmpctx, struct privkey, next_blinding));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-30 04:06:04 +01:00
|
|
|
static u8 *enctlv_from_encmsg_raw(const tal_t *ctx,
|
|
|
|
const struct privkey *blinding,
|
|
|
|
const struct pubkey *node,
|
|
|
|
const u8 *raw_encmsg TAKES,
|
|
|
|
struct privkey *next_blinding,
|
|
|
|
struct pubkey *node_alias)
|
2021-09-21 23:17:16 +02:00
|
|
|
{
|
|
|
|
struct secret ss, rho;
|
|
|
|
u8 *ret;
|
|
|
|
int ok;
|
|
|
|
/* All-zero npub */
|
|
|
|
static const unsigned char npub[crypto_aead_chacha20poly1305_ietf_NPUBBYTES];
|
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
/* BOLT-route-blinding #4:
|
|
|
|
* - `ss(i) = SHA256(e(i) * N(i)) = SHA256(k(i) * E(i))`
|
|
|
|
* (ECDH shared secret known only by `N(r)` and `N(i)`)
|
2021-09-21 23:17:16 +02:00
|
|
|
*/
|
|
|
|
if (secp256k1_ecdh(secp256k1_ctx, ss.data,
|
|
|
|
&node->pubkey, blinding->secret.data,
|
|
|
|
NULL, NULL) != 1)
|
|
|
|
return NULL;
|
|
|
|
SUPERVERBOSE("\t\"ss\": \"%s\",\n",
|
|
|
|
type_to_string(tmpctx, struct secret, &ss));
|
|
|
|
|
|
|
|
/* This calculates the node's alias, and next blinding */
|
|
|
|
if (!blind_node(blinding, &ss, node, node_alias, next_blinding))
|
|
|
|
return NULL;
|
|
|
|
|
2021-11-30 04:06:04 +01:00
|
|
|
ret = tal_dup_talarr(ctx, u8, raw_encmsg);
|
2021-09-21 23:17:16 +02:00
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
/* BOLT-route-blinding #4:
|
|
|
|
* - `rho(i) = HMAC256("rho", ss(i))`
|
|
|
|
* (key used to encrypt the payload for `N(i)` by `N(r)`)
|
2021-09-21 23:17:16 +02:00
|
|
|
*/
|
|
|
|
subkey_from_hmac("rho", &ss, &rho);
|
|
|
|
SUPERVERBOSE("\t\"rho\": \"%s\",\n",
|
|
|
|
type_to_string(tmpctx, struct secret, &rho));
|
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
/* BOLT-route-blinding #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
* - MUST encrypt each `encrypted_data_tlv(i)` with ChaCha20-Poly1305
|
|
|
|
* using the corresponding `rho(i)` key and an all-zero nonce to
|
|
|
|
* produce `encrypted_recipient_data(i)`
|
|
|
|
*/
|
2021-09-21 23:17:16 +02:00
|
|
|
/* Encrypt in place */
|
|
|
|
towire_pad(&ret, crypto_aead_chacha20poly1305_ietf_ABYTES);
|
|
|
|
ok = crypto_aead_chacha20poly1305_ietf_encrypt(ret, NULL,
|
|
|
|
ret,
|
|
|
|
tal_bytelen(ret)
|
|
|
|
- crypto_aead_chacha20poly1305_ietf_ABYTES,
|
|
|
|
NULL, 0,
|
|
|
|
NULL, npub,
|
|
|
|
rho.data);
|
|
|
|
assert(ok == 0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-11-09 02:30:10 +01:00
|
|
|
u8 *encrypt_tlv_encrypted_data(const tal_t *ctx,
|
|
|
|
const struct privkey *blinding,
|
|
|
|
const struct pubkey *node,
|
|
|
|
const struct tlv_encrypted_data_tlv *encmsg,
|
|
|
|
struct privkey *next_blinding,
|
|
|
|
struct pubkey *node_alias)
|
2021-11-30 04:06:04 +01:00
|
|
|
{
|
2022-11-09 02:30:10 +01:00
|
|
|
struct privkey unused;
|
2021-11-30 04:06:04 +01:00
|
|
|
u8 *encmsg_raw = tal_arr(NULL, u8, 0);
|
2022-03-23 00:31:14 +01:00
|
|
|
towire_tlv_encrypted_data_tlv(&encmsg_raw, encmsg);
|
2022-11-09 02:30:10 +01:00
|
|
|
|
|
|
|
/* last hop doesn't care about next_blinding */
|
|
|
|
if (!next_blinding)
|
|
|
|
next_blinding = &unused;
|
2021-11-30 04:06:04 +01:00
|
|
|
return enctlv_from_encmsg_raw(ctx, blinding, node, take(encmsg_raw),
|
|
|
|
next_blinding, node_alias);
|
|
|
|
}
|
|
|
|
|
2021-09-21 23:17:24 +02:00
|
|
|
bool unblind_onion(const struct pubkey *blinding,
|
|
|
|
void (*ecdh)(const struct pubkey *point, struct secret *ss),
|
|
|
|
struct pubkey *onion_key,
|
|
|
|
struct secret *ss)
|
|
|
|
{
|
|
|
|
struct secret hmac;
|
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
/* BOLT-route-blinding #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
* A reader:
|
|
|
|
*...
|
2022-10-17 02:43:07 +02:00
|
|
|
* - MUST compute:
|
|
|
|
* - `ss(i) = SHA256(k(i) * E(i))` (standard ECDH)
|
|
|
|
* - `b(i) = HMAC256("blinded_node_id", ss(i)) * k(i)`
|
|
|
|
*/
|
2021-09-21 23:17:24 +02:00
|
|
|
ecdh(blinding, ss);
|
|
|
|
subkey_from_hmac("blinded_node_id", ss, &hmac);
|
|
|
|
|
|
|
|
/* We instead tweak the *ephemeral* key from the onion and use
|
|
|
|
* our normal privkey: since hsmd knows only how to ECDH with
|
2022-10-17 02:43:07 +02:00
|
|
|
* our real key. IOW: */
|
|
|
|
/* BOLT-route-blinding #4:
|
|
|
|
* - MUST use `b(i)` instead of its private key `k(i)` to decrypt the onion. Note
|
|
|
|
* that the node may instead tweak the onion ephemeral key with
|
|
|
|
* `HMAC256("blinded_node_id", ss(i))` which achieves the same result.
|
|
|
|
*/
|
2021-09-21 23:17:24 +02:00
|
|
|
return secp256k1_ec_pubkey_tweak_mul(secp256k1_ctx,
|
|
|
|
&onion_key->pubkey,
|
|
|
|
hmac.data) == 1;
|
|
|
|
}
|
|
|
|
|
2021-11-30 04:06:04 +01:00
|
|
|
static u8 *decrypt_encmsg_raw(const tal_t *ctx,
|
|
|
|
const struct pubkey *blinding,
|
|
|
|
const struct secret *ss,
|
|
|
|
const u8 *enctlv)
|
2021-09-21 23:17:24 +02:00
|
|
|
{
|
|
|
|
struct secret rho;
|
|
|
|
u8 *dec;
|
|
|
|
/* All-zero npub */
|
|
|
|
static const unsigned char npub[crypto_aead_chacha20poly1305_ietf_NPUBBYTES];
|
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
/* BOLT-route-blinding #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
* A reader:
|
|
|
|
*...
|
|
|
|
*- MUST decrypt the `encrypted_data` field using `rho(i)` and use
|
|
|
|
* the decrypted fields to locate the next node
|
2022-10-17 02:43:07 +02:00
|
|
|
*/
|
2021-09-21 23:17:24 +02:00
|
|
|
subkey_from_hmac("rho", ss, &rho);
|
|
|
|
|
|
|
|
/* BOLT-onion-message #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
*- If the `encrypted_data` field is missing or cannot
|
|
|
|
* be decrypted:
|
|
|
|
* - MUST return an error
|
2021-09-21 23:17:24 +02:00
|
|
|
*/
|
|
|
|
/* Too short? */
|
|
|
|
if (tal_bytelen(enctlv) < crypto_aead_chacha20poly1305_ietf_ABYTES)
|
|
|
|
return NULL;
|
|
|
|
|
2021-11-30 04:06:04 +01:00
|
|
|
dec = tal_arr(ctx, u8, tal_bytelen(enctlv)
|
2021-09-21 23:17:24 +02:00
|
|
|
- crypto_aead_chacha20poly1305_ietf_ABYTES);
|
|
|
|
if (crypto_aead_chacha20poly1305_ietf_decrypt(dec, NULL,
|
|
|
|
NULL,
|
|
|
|
enctlv, tal_bytelen(enctlv),
|
|
|
|
NULL, 0,
|
|
|
|
npub,
|
|
|
|
rho.data) != 0)
|
2021-11-30 04:06:04 +01:00
|
|
|
return tal_free(dec);
|
|
|
|
|
|
|
|
return dec;
|
|
|
|
}
|
2021-09-21 23:17:24 +02:00
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
struct tlv_encrypted_data_tlv *decrypt_encrypted_data(const tal_t *ctx,
|
|
|
|
const struct pubkey *blinding,
|
|
|
|
const struct secret *ss,
|
|
|
|
const u8 *enctlv)
|
2021-11-30 04:06:04 +01:00
|
|
|
{
|
|
|
|
const u8 *cursor = decrypt_encmsg_raw(tmpctx, blinding, ss, enctlv);
|
|
|
|
size_t maxlen = tal_bytelen(cursor);
|
|
|
|
|
|
|
|
/* BOLT-onion-message #4:
|
|
|
|
*
|
2023-01-12 04:55:38 +01:00
|
|
|
* - MUST return an error if `encrypted_recipient_data` does not decrypt
|
|
|
|
* using the blinding point as described in
|
|
|
|
* [Route Blinding](#route-blinding).
|
2021-11-30 04:06:04 +01:00
|
|
|
*/
|
2022-10-17 02:37:05 +02:00
|
|
|
/* Note: our parser consider nothing is a valid TLV, but decrypt_encmsg_raw
|
|
|
|
* returns NULL if it couldn't decrypt. */
|
|
|
|
if (!cursor)
|
|
|
|
return NULL;
|
2022-03-23 03:44:36 +01:00
|
|
|
return fromwire_tlv_encrypted_data_tlv(ctx, &cursor, &maxlen);
|
2021-11-30 04:06:04 +01:00
|
|
|
}
|
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
bool blindedpath_get_alias(const struct secret *ss,
|
|
|
|
const struct pubkey *my_id,
|
|
|
|
struct pubkey *alias)
|
2021-11-30 04:06:04 +01:00
|
|
|
{
|
2022-10-17 02:43:07 +02:00
|
|
|
struct secret node_id_blinding;
|
2021-11-30 04:06:04 +01:00
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
/* BOLT-route-blinding #4:
|
|
|
|
* - `B(i) = HMAC256("blinded_node_id", ss(i)) * N(i)`
|
|
|
|
* (blinded `node_id` for `N(i)`, private key known only by `N(i)`)
|
2021-11-30 04:06:04 +01:00
|
|
|
*/
|
2022-10-17 02:43:07 +02:00
|
|
|
subkey_from_hmac("blinded_node_id", ss, &node_id_blinding);
|
|
|
|
SUPERVERBOSE("\t\"HMAC256('blinded_node_id', ss)\": \"%s\",\n",
|
|
|
|
type_to_string(tmpctx, struct secret,
|
|
|
|
&node_id_blinding));
|
2021-11-30 04:06:04 +01:00
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
*alias = *my_id;
|
|
|
|
return secp256k1_ec_pubkey_tweak_mul(secp256k1_ctx,
|
|
|
|
&alias->pubkey,
|
|
|
|
node_id_blinding.data) == 1;
|
|
|
|
}
|
2021-11-30 04:06:04 +01:00
|
|
|
|
2022-10-17 02:43:07 +02:00
|
|
|
void blindedpath_next_blinding(const struct tlv_encrypted_data_tlv *enc,
|
|
|
|
const struct pubkey *blinding,
|
|
|
|
const struct secret *ss,
|
|
|
|
struct pubkey *next_blinding)
|
|
|
|
{
|
|
|
|
/* BOLT-route
|
|
|
|
* - `E(1) = SHA256(E(0) || ss(0)) * E(0)`
|
|
|
|
* ...
|
|
|
|
* - If `encrypted_data` contains a `next_blinding_override`:
|
|
|
|
* - MUST use it as the next blinding point instead of `E(1)`
|
|
|
|
* - Otherwise:
|
|
|
|
* - MUST use `E(1)` as the next blinding point
|
2021-11-30 04:06:04 +01:00
|
|
|
*/
|
2022-10-17 02:43:07 +02:00
|
|
|
if (enc->next_blinding_override)
|
|
|
|
*next_blinding = *enc->next_blinding_override;
|
2021-11-30 04:06:04 +01:00
|
|
|
else {
|
|
|
|
/* E(i-1) = H(E(i) || ss(i)) * E(i) */
|
|
|
|
struct sha256 h;
|
|
|
|
blinding_hash_e_and_ss(blinding, ss, &h);
|
|
|
|
blinding_next_pubkey(blinding, &h, next_blinding);
|
|
|
|
}
|
|
|
|
}
|