2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2022-11-09 03:32:00 +01:00
|
|
|
#include <assert.h>
|
2021-07-05 13:26:39 +02:00
|
|
|
#include <ccan/cast/cast.h>
|
|
|
|
#include <ccan/ilog/ilog.h>
|
2020-12-05 03:23:54 +01:00
|
|
|
#include <ccan/mem/mem.h>
|
|
|
|
#include <common/bolt12_merkle.h>
|
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
#ifndef SUPERVERBOSE
|
|
|
|
#define SUPERVERBOSE(...)
|
|
|
|
#endif
|
|
|
|
|
2020-12-05 03:23:54 +01:00
|
|
|
/* BOLT-offers #12:
|
2022-11-09 03:32:01 +01:00
|
|
|
* Each form is signed using one or more *signature TLV elements*: TLV
|
|
|
|
* types 240 through 1000 (inclusive).
|
2020-12-05 03:23:54 +01:00
|
|
|
*/
|
|
|
|
static bool is_signature_field(const struct tlv_field *field)
|
|
|
|
{
|
|
|
|
return field->numtype >= 240 && field->numtype <= 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sha256_update_bigsize(struct sha256_ctx *ctx, u64 bigsize)
|
|
|
|
{
|
|
|
|
u8 buf[BIGSIZE_MAX_LEN];
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = bigsize_put(buf, bigsize);
|
2021-07-05 13:26:39 +02:00
|
|
|
SUPERVERBOSE("%s", tal_hexstr(tmpctx, buf, len));
|
2020-12-05 03:23:54 +01:00
|
|
|
sha256_update(ctx, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sha256_update_tlvfield(struct sha256_ctx *ctx,
|
|
|
|
const struct tlv_field *field)
|
|
|
|
{
|
|
|
|
/* We don't keep it raw, so reconstruct. */
|
|
|
|
sha256_update_bigsize(ctx, field->numtype);
|
|
|
|
sha256_update_bigsize(ctx, field->length);
|
2021-07-05 13:26:39 +02:00
|
|
|
SUPERVERBOSE("%s", tal_hexstr(tmpctx, field->value, field->length));
|
2020-12-05 03:23:54 +01:00
|
|
|
sha256_update(ctx, field->value, field->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT-offers #12:
|
2021-07-05 13:26:39 +02:00
|
|
|
* Thus we define H(`tag`,`msg`) as SHA256(SHA256(`tag`) || SHA256(`tag`) || `msg`)*/
|
|
|
|
/* Create a sha256_ctx which has the tag part done. */
|
|
|
|
static void h_simpletag_ctx(struct sha256_ctx *sctx, const char *tag)
|
|
|
|
{
|
|
|
|
struct sha256 sha;
|
|
|
|
sha256(&sha, tag, strlen(tag));
|
|
|
|
sha256_init(sctx);
|
|
|
|
sha256_update(sctx, &sha, sizeof(sha));
|
|
|
|
sha256_update(sctx, &sha, sizeof(sha));
|
|
|
|
SUPERVERBOSE("tag=SHA256(%s) -> %s",
|
|
|
|
tal_hexstr(tmpctx, tag, strlen(tag)),
|
|
|
|
type_to_string(tmpctx, struct sha256, &sha));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* BOLT-offers #12:
|
|
|
|
* The Merkle tree's leaves are, in TLV-ascending order for each tlv:
|
2022-11-09 03:32:00 +01:00
|
|
|
* 1. The H("LnLeaf",tlv).
|
|
|
|
* 2. The H("LnNonce"||first-tlv,tlv-type) where first-tlv is the numerically-first TLV entry in the stream, and tlv-type is the "type" field (1-9 bytes) of the current tlv.
|
2020-12-05 03:23:54 +01:00
|
|
|
*/
|
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
/* Create a sha256_ctx which has the tag part done. */
|
2022-11-09 03:32:00 +01:00
|
|
|
static void h_lnnonce_ctx(struct sha256_ctx *sctx, const struct tlv_field *fields)
|
2020-12-05 03:23:54 +01:00
|
|
|
{
|
2021-07-05 13:26:39 +02:00
|
|
|
struct sha256_ctx inner_sctx;
|
|
|
|
struct sha256 sha;
|
2020-12-05 03:23:54 +01:00
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
sha256_init(&inner_sctx);
|
2022-11-09 03:32:00 +01:00
|
|
|
sha256_update(&inner_sctx, "LnNonce", 7);
|
|
|
|
SUPERVERBOSE("tag=SHA256(%s", tal_hexstr(tmpctx, "LnNonce", 7));
|
|
|
|
assert(tal_count(fields));
|
|
|
|
sha256_update_tlvfield(&inner_sctx, &fields[0]);
|
2021-07-05 13:26:39 +02:00
|
|
|
sha256_done(&inner_sctx, &sha);
|
|
|
|
SUPERVERBOSE(") -> %s\n",
|
|
|
|
type_to_string(tmpctx, struct sha256, &sha));
|
|
|
|
|
|
|
|
sha256_init(sctx);
|
|
|
|
sha256_update(sctx, &sha, sizeof(sha));
|
|
|
|
sha256_update(sctx, &sha, sizeof(sha));
|
|
|
|
}
|
|
|
|
|
2022-11-09 03:32:00 +01:00
|
|
|
/* Use h_lnnonce_ctx to create nonce */
|
|
|
|
static void calc_nonce(const struct sha256_ctx *lnnonce_ctx,
|
2021-07-05 13:26:39 +02:00
|
|
|
const struct tlv_field *field,
|
|
|
|
struct sha256 *hash)
|
|
|
|
{
|
|
|
|
/* Copy context, to add field */
|
2022-11-09 03:32:00 +01:00
|
|
|
struct sha256_ctx ctx = *lnnonce_ctx;
|
2021-07-05 13:26:39 +02:00
|
|
|
|
|
|
|
SUPERVERBOSE("nonce: H(noncetag,");
|
2022-11-09 03:32:00 +01:00
|
|
|
sha256_update_bigsize(&ctx, field->numtype);
|
2021-07-05 13:26:39 +02:00
|
|
|
|
|
|
|
sha256_done(&ctx, hash);
|
|
|
|
SUPERVERBOSE(") = %s\n", type_to_string(tmpctx, struct sha256, hash));
|
2020-12-05 03:23:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void calc_lnleaf(const struct tlv_field *field, struct sha256 *hash)
|
|
|
|
{
|
|
|
|
struct sha256_ctx sctx;
|
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
SUPERVERBOSE("leaf: H(");
|
|
|
|
h_simpletag_ctx(&sctx, "LnLeaf");
|
|
|
|
SUPERVERBOSE(",");
|
2020-12-05 03:23:54 +01:00
|
|
|
sha256_update_tlvfield(&sctx, field);
|
|
|
|
sha256_done(&sctx, hash);
|
2021-07-05 13:26:39 +02:00
|
|
|
SUPERVERBOSE(") -> %s\n", type_to_string(tmpctx, struct sha256, hash));
|
2020-12-05 03:23:54 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
/* BOLT-offers #12:
|
2022-11-09 03:32:00 +01:00
|
|
|
* The Merkle tree inner nodes are H("LnBranch", lesser-SHA256||greater-SHA256)
|
2021-07-05 13:26:39 +02:00
|
|
|
*/
|
|
|
|
static struct sha256 *merkle_pair(const tal_t *ctx,
|
|
|
|
const struct sha256 *a, const struct sha256 *b)
|
2020-12-05 03:23:54 +01:00
|
|
|
{
|
2021-07-05 13:26:39 +02:00
|
|
|
struct sha256 *res;
|
2020-12-05 03:23:54 +01:00
|
|
|
struct sha256_ctx sctx;
|
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
/* Make sure a < b */
|
|
|
|
if (memcmp(a->u.u8, b->u.u8, sizeof(a->u.u8)) > 0)
|
|
|
|
return merkle_pair(ctx, b, a);
|
|
|
|
|
|
|
|
SUPERVERBOSE("branch: H(");
|
|
|
|
h_simpletag_ctx(&sctx, "LnBranch");
|
|
|
|
SUPERVERBOSE(",%s %s",
|
|
|
|
tal_hexstr(tmpctx, a->u.u8, sizeof(a->u.u8)),
|
|
|
|
tal_hexstr(tmpctx, b->u.u8, sizeof(b->u.u8)));
|
|
|
|
sha256_update(&sctx, a->u.u8, sizeof(a->u.u8));
|
|
|
|
sha256_update(&sctx, b->u.u8, sizeof(b->u.u8));
|
|
|
|
|
|
|
|
res = tal(ctx, struct sha256);
|
|
|
|
sha256_done(&sctx, res);
|
|
|
|
SUPERVERBOSE(") -> %s\n", type_to_string(tmpctx, struct sha256, res));
|
2020-12-05 03:23:54 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
static const struct sha256 *merkle_recurse(const struct sha256 **base,
|
|
|
|
const struct sha256 **arr, size_t len)
|
2020-12-05 03:23:54 +01:00
|
|
|
{
|
2021-07-05 13:26:39 +02:00
|
|
|
const struct sha256 *left, *right;
|
2020-12-05 03:23:54 +01:00
|
|
|
if (len == 1)
|
|
|
|
return arr[0];
|
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
SUPERVERBOSE("Merkle recurse [%zu - %zu] and [%zu - %zu]\n",
|
|
|
|
arr - base, arr + len / 2 - 1 - base,
|
2021-07-29 08:29:53 +02:00
|
|
|
arr + len / 2 - base, arr + len - 1 - base);
|
2021-07-05 13:26:39 +02:00
|
|
|
left = merkle_recurse(base, arr, len / 2);
|
|
|
|
right = merkle_recurse(base, arr + len / 2, len / 2);
|
|
|
|
/* left is never NULL if right is not NULL */
|
2021-07-29 08:29:53 +02:00
|
|
|
if (!right) {
|
|
|
|
SUPERVERBOSE("[%zu - %zu] is NULL!\n",
|
|
|
|
arr + len / 2 - base, arr + len - base);
|
2021-07-05 13:26:39 +02:00
|
|
|
return left;
|
2021-07-29 08:29:53 +02:00
|
|
|
}
|
2021-07-05 13:26:39 +02:00
|
|
|
return merkle_pair(base, left, right);
|
2020-12-05 03:23:54 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
/* This is not the fastest way, but it is the most intuitive. */
|
2020-12-05 03:23:54 +01:00
|
|
|
void merkle_tlv(const struct tlv_field *fields, struct sha256 *merkle)
|
|
|
|
{
|
2021-07-05 13:26:39 +02:00
|
|
|
struct sha256 **arr;
|
2022-11-09 03:32:00 +01:00
|
|
|
struct sha256_ctx lnnonce_ctx;
|
2020-12-05 03:23:54 +01:00
|
|
|
size_t n;
|
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
SUPERVERBOSE("nonce tag:");
|
2022-11-09 03:32:00 +01:00
|
|
|
h_lnnonce_ctx(&lnnonce_ctx, fields);
|
2021-07-29 08:29:53 +02:00
|
|
|
|
|
|
|
/* We build an oversized power-of-2 symmentic tree, but with
|
|
|
|
* NULL nodes at the end. When we recurse, we pass through
|
|
|
|
* NULL. This is less efficient than calculating the
|
|
|
|
* power-of-2 split as we recurse, but simpler. */
|
2021-07-05 13:26:39 +02:00
|
|
|
arr = tal_arrz(NULL, struct sha256 *,
|
|
|
|
1ULL << (ilog64(tal_count(fields)) + 1));
|
2020-12-05 03:23:54 +01:00
|
|
|
|
|
|
|
n = 0;
|
|
|
|
for (size_t i = 0; i < tal_count(fields); i++) {
|
2021-07-05 13:26:39 +02:00
|
|
|
struct sha256 leaf, nonce;
|
2020-12-05 03:23:54 +01:00
|
|
|
if (is_signature_field(&fields[i]))
|
|
|
|
continue;
|
2021-07-05 13:26:39 +02:00
|
|
|
calc_lnleaf(&fields[i], &leaf);
|
2022-11-09 03:32:00 +01:00
|
|
|
calc_nonce(&lnnonce_ctx, &fields[i], &nonce);
|
2021-07-05 13:26:39 +02:00
|
|
|
arr[n++] = merkle_pair(arr, &leaf, &nonce);
|
2020-12-05 03:23:54 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 13:26:39 +02:00
|
|
|
/* This should never happen, but define it a distinctive all-zeroes */
|
2021-07-05 08:23:29 +02:00
|
|
|
if (n == 0)
|
2021-07-05 13:26:39 +02:00
|
|
|
memset(merkle, 0, sizeof(*merkle));
|
2021-07-05 08:23:29 +02:00
|
|
|
else
|
2021-07-05 13:26:39 +02:00
|
|
|
*merkle = *merkle_recurse(cast_const2(const struct sha256 **, arr),
|
|
|
|
cast_const2(const struct sha256 **, arr),
|
|
|
|
tal_count(arr));
|
2020-12-05 03:23:54 +01:00
|
|
|
tal_free(arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT-offers #12:
|
|
|
|
* All signatures are created as per
|
2022-11-09 03:32:01 +01:00
|
|
|
* [BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki)
|
2021-07-05 13:26:39 +02:00
|
|
|
* and tagged as recommended there. Thus we define H(`tag`,`msg`) as
|
|
|
|
* SHA256(SHA256(`tag`) || SHA256(`tag`) || `msg`), and SIG(`tag`,`msg`,`key`)
|
|
|
|
* as the signature of H(`tag`,`msg`) using `key`.
|
2020-12-05 03:23:54 +01:00
|
|
|
*
|
2022-11-09 03:32:01 +01:00
|
|
|
* Each form is signed using one or more *signature TLV elements*: TLV types
|
|
|
|
* 240 through 1000 (inclusive). For these, the tag is "lightning" ||
|
|
|
|
* `messagename` || `fieldname`, and `msg` is the Merkle-root; "lightning" is
|
|
|
|
* the literal 9-byte ASCII string, `messagename` is the name of the TLV
|
|
|
|
* stream being signed (i.e. "invoice_request" or "invoice") and the
|
|
|
|
* `fieldname` is the TLV field containing the signature (e.g. "signature").
|
2020-12-05 03:23:54 +01:00
|
|
|
*/
|
|
|
|
void sighash_from_merkle(const char *messagename,
|
|
|
|
const char *fieldname,
|
|
|
|
const struct sha256 *merkle,
|
|
|
|
struct sha256 *sighash)
|
|
|
|
{
|
|
|
|
struct sha256_ctx sctx;
|
|
|
|
|
|
|
|
bip340_sighash_init(&sctx, "lightning", messagename, fieldname);
|
|
|
|
sha256_update(&sctx, merkle, sizeof(*merkle));
|
|
|
|
sha256_done(&sctx, sighash);
|
|
|
|
}
|
2020-12-16 04:13:37 +01:00
|
|
|
|
|
|
|
/* We use the SHA(pubkey | publictweak); so reader cannot figure out the
|
2022-10-17 02:35:31 +02:00
|
|
|
* tweak and derive the base key.
|
|
|
|
*/
|
2022-10-17 02:35:41 +02:00
|
|
|
void payer_key_tweak(const struct pubkey *bolt12,
|
2020-12-16 04:13:37 +01:00
|
|
|
const u8 *publictweak, size_t publictweaklen,
|
|
|
|
struct sha256 *tweak)
|
|
|
|
{
|
2022-10-17 02:35:31 +02:00
|
|
|
u8 rawkey[PUBKEY_CMPR_LEN];
|
2020-12-16 04:13:37 +01:00
|
|
|
struct sha256_ctx sha;
|
2022-10-17 02:35:31 +02:00
|
|
|
|
2022-10-17 02:35:41 +02:00
|
|
|
pubkey_to_der(rawkey, bolt12);
|
2020-12-16 04:13:37 +01:00
|
|
|
|
|
|
|
sha256_init(&sha);
|
2022-10-17 02:36:41 +02:00
|
|
|
sha256_update(&sha, rawkey, sizeof(rawkey));
|
2020-12-16 04:13:37 +01:00
|
|
|
sha256_update(&sha,
|
|
|
|
memcheck(publictweak, publictweaklen),
|
|
|
|
publictweaklen);
|
|
|
|
sha256_done(&sha, tweak);
|
|
|
|
}
|