2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2024-03-20 01:47:55 +01:00
|
|
|
#include <bitcoin/tx.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. */
|
2024-10-14 18:03:49 +02:00
|
|
|
static bool blind_node(const struct privkey *path_privkey,
|
2021-09-21 23:17:16 +02:00
|
|
|
const struct secret *ss,
|
|
|
|
const struct pubkey *node,
|
|
|
|
struct pubkey *node_alias,
|
2024-10-14 18:03:49 +02:00
|
|
|
struct privkey *next_path_privkey)
|
2021-09-21 23:17:16 +02:00
|
|
|
{
|
2024-10-14 18:03:49 +02:00
|
|
|
struct pubkey path_pubkey;
|
2021-09-21 23:17:16 +02:00
|
|
|
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",
|
2024-03-20 01:47:52 +01:00
|
|
|
fmt_pubkey(tmpctx, node_alias));
|
2021-09-21 23:17:16 +02:00
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2024-06-19 01:59:01 +02:00
|
|
|
* - $`E_{i+1} = SHA256(E_i || ss_i) * E_i`$
|
2024-10-14 18:03:49 +02:00
|
|
|
* (`path_key`. NB: $`N_i`$ MUST NOT learn $`e_i`$)
|
2021-09-21 23:17:16 +02:00
|
|
|
*/
|
2024-10-14 18:03:49 +02:00
|
|
|
if (!pubkey_from_privkey(path_privkey, &path_pubkey))
|
2021-09-21 23:17:16 +02:00
|
|
|
return false;
|
|
|
|
SUPERVERBOSE("\t\"E\": \"%s\",\n",
|
2024-10-14 18:03:49 +02:00
|
|
|
fmt_pubkey(tmpctx, &path_pubkey));
|
2021-09-21 23:17:16 +02:00
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2024-06-19 01:59:01 +02:00
|
|
|
* - $`e_{i+1} = SHA256(E_i || ss_i) * e_i`$
|
2024-10-14 18:03:49 +02:00
|
|
|
* (ephemeral private path key, only known by $`N_r`$)
|
2022-10-17 02:43:07 +02:00
|
|
|
*/
|
2024-10-14 18:03:49 +02:00
|
|
|
blinding_hash_e_and_ss(&path_pubkey, ss, &h);
|
2021-09-21 23:17:16 +02:00
|
|
|
SUPERVERBOSE("\t\"H(E || ss)\": \"%s\",\n",
|
2024-03-20 01:47:52 +01:00
|
|
|
fmt_sha256(tmpctx, &h));
|
2024-10-14 18:03:49 +02:00
|
|
|
blinding_next_path_privkey(path_privkey, &h, next_path_privkey);
|
2021-09-21 23:17:16 +02:00
|
|
|
SUPERVERBOSE("\t\"next_e\": \"%s\",\n",
|
2024-10-14 18:03:49 +02:00
|
|
|
fmt_privkey(tmpctx, next_path_privkey));
|
2021-09-21 23:17:16 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-30 04:06:04 +01:00
|
|
|
static u8 *enctlv_from_encmsg_raw(const tal_t *ctx,
|
2024-10-14 18:03:49 +02:00
|
|
|
const struct privkey *path_privkey,
|
2021-11-30 04:06:04 +01:00
|
|
|
const struct pubkey *node,
|
|
|
|
const u8 *raw_encmsg TAKES,
|
2024-10-14 18:03:49 +02:00
|
|
|
struct privkey *next_path_privkey,
|
2021-11-30 04:06:04 +01:00
|
|
|
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];
|
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2024-10-14 18:03:49 +02:00
|
|
|
* - $`ss_i = SHA256(e_i * N_i) = SHA256(k_i * E_i)`$
|
2024-06-19 01:59:01 +02:00
|
|
|
* (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,
|
2024-10-14 18:03:49 +02:00
|
|
|
&node->pubkey, path_privkey->secret.data,
|
2021-09-21 23:17:16 +02:00
|
|
|
NULL, NULL) != 1)
|
|
|
|
return NULL;
|
|
|
|
SUPERVERBOSE("\t\"ss\": \"%s\",\n",
|
2024-03-20 01:47:52 +01:00
|
|
|
fmt_secret(tmpctx, &ss));
|
2021-09-21 23:17:16 +02:00
|
|
|
|
2024-10-14 18:03:49 +02:00
|
|
|
/* This calculates the node's alias, and next path_key */
|
|
|
|
if (!blind_node(path_privkey, &ss, node, node_alias, next_path_privkey))
|
2021-09-21 23:17:16 +02:00
|
|
|
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
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2024-06-19 01:59:01 +02:00
|
|
|
* - $`rho_i = HMAC256(\text{"rho"}, ss_i)`$
|
2024-10-14 18:03:49 +02:00
|
|
|
* (key used to encrypt `encrypted_recipient_data` 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",
|
2024-03-20 01:47:52 +01:00
|
|
|
fmt_secret(tmpctx, &rho));
|
2021-09-21 23:17:16 +02:00
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2024-06-19 01:59:01 +02:00
|
|
|
* - MUST encrypt each `encrypted_data_tlv[i]` with ChaCha20-Poly1305 using
|
2024-10-14 18:03:49 +02:00
|
|
|
* the corresponding $`rho_i`$ key and an all-zero nonce to produce
|
2024-06-19 01:59:01 +02:00
|
|
|
* `encrypted_recipient_data[i]`
|
2022-11-09 02:30:10 +01:00
|
|
|
*/
|
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,
|
2024-10-14 18:03:49 +02:00
|
|
|
const struct privkey *path_privkey,
|
2022-11-09 02:30:10 +01:00
|
|
|
const struct pubkey *node,
|
2024-10-14 18:03:49 +02:00
|
|
|
const struct tlv_encrypted_data_tlv *tlv,
|
|
|
|
struct privkey *next_path_privkey,
|
2022-11-09 02:30:10 +01:00
|
|
|
struct pubkey *node_alias)
|
2021-11-30 04:06:04 +01:00
|
|
|
{
|
2022-11-09 02:30:10 +01:00
|
|
|
struct privkey unused;
|
2024-10-14 18:03:49 +02:00
|
|
|
u8 *tlv_wire = tal_arr(NULL, u8, 0);
|
|
|
|
towire_tlv_encrypted_data_tlv(&tlv_wire, tlv);
|
2022-11-09 02:30:10 +01:00
|
|
|
|
2024-10-14 18:03:49 +02:00
|
|
|
/* last hop doesn't care about next path_key */
|
|
|
|
if (!next_path_privkey)
|
|
|
|
next_path_privkey = &unused;
|
|
|
|
return enctlv_from_encmsg_raw(ctx, path_privkey, node, take(tlv_wire),
|
|
|
|
next_path_privkey, node_alias);
|
2021-11-30 04:06:04 +01:00
|
|
|
}
|
|
|
|
|
2024-10-14 18:03:49 +02:00
|
|
|
bool unblind_onion(const struct pubkey *path_key,
|
2021-09-21 23:17:24 +02:00
|
|
|
void (*ecdh)(const struct pubkey *point, struct secret *ss),
|
|
|
|
struct pubkey *onion_key,
|
|
|
|
struct secret *ss)
|
|
|
|
{
|
|
|
|
struct secret hmac;
|
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
* A reader:
|
|
|
|
*...
|
2024-10-15 01:24:39 +02:00
|
|
|
* - if `path_key` is specified:
|
|
|
|
* - Calculate the `blinding_ss` as ECDH(`path_key`, `node_privkey`).
|
|
|
|
* - Either:
|
|
|
|
* - Tweak `public_key` by multiplying by $`HMAC256(\text{"blinded\_node\_id"}, blinding\_ss)`$.
|
|
|
|
* - or (equivalently):
|
|
|
|
* - Tweak its own `node_privkey` below by multiplying by $`HMAC256(\text{"blinded\_node\_id"}, blinding\_ss)`$.
|
2022-10-17 02:43:07 +02:00
|
|
|
*/
|
2024-10-14 18:03:49 +02:00
|
|
|
ecdh(path_key, ss);
|
2021-09-21 23:17:24 +02:00
|
|
|
subkey_from_hmac("blinded_node_id", ss, &hmac);
|
|
|
|
|
2024-10-15 01:24:39 +02:00
|
|
|
/* We tweak the *ephemeral* key from the onion and use
|
2021-09-21 23:17:24 +02:00
|
|
|
* our normal privkey: since hsmd knows only how to ECDH with
|
2024-10-15 01:24:39 +02:00
|
|
|
* our real key. */
|
2021-09-21 23:17:24 +02:00
|
|
|
return secp256k1_ec_pubkey_tweak_mul(secp256k1_ctx,
|
|
|
|
&onion_key->pubkey,
|
|
|
|
hmac.data) == 1;
|
|
|
|
}
|
|
|
|
|
2024-07-17 11:00:11 +02:00
|
|
|
u8 *decrypt_encmsg_raw(const tal_t *ctx,
|
|
|
|
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];
|
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2024-10-15 01:24:39 +02:00
|
|
|
* The reader of the `encrypted_recipient_data`:
|
2022-11-09 02:30:10 +01:00
|
|
|
*...
|
2024-10-15 01:24:39 +02:00
|
|
|
*- $`rho_i = HMAC256(\text{"rho"}, ss_i)`$
|
|
|
|
*- MUST decrypt the `encrypted_recipient_data` field using $`rho_i`$
|
|
|
|
* as a key using ChaCha20-Poly1305 and an all-zero nonce key.
|
2022-10-17 02:43:07 +02:00
|
|
|
*/
|
2021-09-21 23:17:24 +02:00
|
|
|
subkey_from_hmac("rho", ss, &rho);
|
|
|
|
|
2024-03-18 04:01:06 +01:00
|
|
|
/* BOLT #4:
|
2024-10-15 01:24:39 +02:00
|
|
|
* - If the `encrypted_recipient_data` field is missing, cannot be
|
|
|
|
* decrypted into an `encrypted_data_tlv` or contains unknown even
|
|
|
|
* fields:
|
|
|
|
* - 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 secret *ss,
|
|
|
|
const u8 *enctlv)
|
2021-11-30 04:06:04 +01:00
|
|
|
{
|
2024-10-14 18:03:49 +02:00
|
|
|
const u8 *cursor = decrypt_encmsg_raw(tmpctx, ss, enctlv);
|
2021-11-30 04:06:04 +01:00
|
|
|
size_t maxlen = tal_bytelen(cursor);
|
|
|
|
|
2024-03-18 04:01:06 +01:00
|
|
|
/* BOLT #4:
|
2024-10-15 01:24:39 +02:00
|
|
|
* - If the `encrypted_recipient_data` field is missing, cannot be
|
|
|
|
* decrypted into an `encrypted_data_tlv` or contains unknown even
|
|
|
|
* fields:
|
|
|
|
* - MUST return an error
|
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
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2024-06-19 01:59:01 +02:00
|
|
|
* - $`B_i = HMAC256(\text{"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",
|
2024-03-20 01:47:52 +01:00
|
|
|
fmt_secret(tmpctx, &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
|
|
|
|
2024-10-14 18:03:49 +02:00
|
|
|
void blindedpath_next_path_key(const struct tlv_encrypted_data_tlv *enc,
|
|
|
|
const struct pubkey *path_key,
|
2022-10-17 02:43:07 +02:00
|
|
|
const struct secret *ss,
|
2024-10-14 18:03:49 +02:00
|
|
|
struct pubkey *next_path_key)
|
2022-10-17 02:43:07 +02:00
|
|
|
{
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2024-06-19 01:59:01 +02:00
|
|
|
* - $`E_{i+1} = SHA256(E_i || ss_i) * E_i`$
|
2022-10-17 02:43:07 +02:00
|
|
|
* ...
|
2024-10-15 01:25:01 +02:00
|
|
|
* - If the `encrypted_data_tlv` contains a `next_path_key_override`:
|
2024-10-15 01:24:39 +02:00
|
|
|
* - MUST use it as the next `path_key`.
|
|
|
|
* - Otherwise:
|
|
|
|
* - MUST use $`E_{i+1} = SHA256(E_i || ss_i) * E_i`$ as the next `path_key`
|
2021-11-30 04:06:04 +01:00
|
|
|
*/
|
2024-10-14 18:03:49 +02:00
|
|
|
if (enc->next_path_key_override)
|
|
|
|
*next_path_key = *enc->next_path_key_override;
|
2021-11-30 04:06:04 +01:00
|
|
|
else {
|
|
|
|
struct sha256 h;
|
2024-10-14 18:03:49 +02:00
|
|
|
blinding_hash_e_and_ss(path_key, ss, &h);
|
|
|
|
blinding_next_path_key(path_key, &h, next_path_key);
|
2021-11-30 04:06:04 +01:00
|
|
|
}
|
|
|
|
}
|