Basically, `devtools/reduce-includes.sh */*.c`. Build time from make clean (RUST=0) (includes building external libs): Before: real 0m38.944000-40.416000(40.1131+/-0.4)s user 3m6.790000-17.159000(15.0571+/-2.8)s sys 0m35.304000-37.336000(36.8942+/-0.57)s After: real 0m37.872000-39.974000(39.5466+/-0.59)s user 3m1.211000-14.968000(12.4556+/-3.9)s sys 0m35.008000-36.830000(36.4143+/-0.5)s Build time after touch config.vars (RUST=0): Before: real 0m19.831000-21.862000(21.5528+/-0.58)s user 2m15.361000-30.731000(28.4798+/-4.4)s sys 0m21.056000-22.339000(22.0346+/-0.35)s After: real 0m18.384000-21.307000(20.8605+/-0.92)s user 2m5.585000-26.843000(23.6017+/-6.7)s sys 0m19.650000-22.003000(21.4943+/-0.69)s Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
165 lines
5.4 KiB
C
165 lines
5.4 KiB
C
#include "config.h"
|
|
#include <bitcoin/shadouble.h>
|
|
#include <bitcoin/signature.h>
|
|
#include <ccan/tal/str/str.h>
|
|
#include <common/utils.h>
|
|
#include <gossipd/sigcheck.h>
|
|
|
|
/* Verify the signature of a channel_update message */
|
|
const char *sigcheck_channel_update(const tal_t *ctx,
|
|
const struct node_id *node_id,
|
|
const secp256k1_ecdsa_signature *node_sig,
|
|
const u8 *update)
|
|
{
|
|
/* BOLT #7:
|
|
* 1. type: 258 (`channel_update`)
|
|
* 2. data:
|
|
* * [`signature`:`signature`]
|
|
* * [`chain_hash`:`chain_hash`]
|
|
* * [`short_channel_id`:`short_channel_id`]
|
|
* * [`u32`:`timestamp`]
|
|
* * [`byte`:`message_flags`]
|
|
* * [`byte`:`channel_flags`]
|
|
* * [`u16`:`cltv_expiry_delta`]
|
|
* * [`u64`:`htlc_minimum_msat`]
|
|
* * [`u32`:`fee_base_msat`]
|
|
* * [`u32`:`fee_proportional_millionths`]
|
|
* * [`u64`:`htlc_maximum_msat`]
|
|
*/
|
|
/* 2 byte msg type + 64 byte signatures */
|
|
int offset = 66;
|
|
struct sha256_double hash;
|
|
|
|
sha256_double(&hash, update + offset, tal_count(update) - offset);
|
|
|
|
if (!check_signed_hash_nodeid(&hash, node_sig, node_id))
|
|
return tal_fmt(ctx,
|
|
"Bad signature for %s hash %s"
|
|
" on channel_update %s",
|
|
fmt_secp256k1_ecdsa_signature(tmpctx, node_sig),
|
|
fmt_sha256_double(tmpctx, &hash),
|
|
tal_hex(tmpctx, update));
|
|
return NULL;
|
|
}
|
|
|
|
const char *sigcheck_channel_announcement(const tal_t *ctx,
|
|
const struct node_id *node1_id,
|
|
const struct node_id *node2_id,
|
|
const struct pubkey *bitcoin1_key,
|
|
const struct pubkey *bitcoin2_key,
|
|
const secp256k1_ecdsa_signature *node1_sig,
|
|
const secp256k1_ecdsa_signature *node2_sig,
|
|
const secp256k1_ecdsa_signature *bitcoin1_sig,
|
|
const secp256k1_ecdsa_signature *bitcoin2_sig,
|
|
const u8 *announcement)
|
|
{
|
|
/* BOLT #7:
|
|
* 1. type: 256 (`channel_announcement`)
|
|
* 2. data:
|
|
* * [`signature`:`node_signature_1`]
|
|
* * [`signature`:`node_signature_2`]
|
|
* * [`signature`:`bitcoin_signature_1`]
|
|
* * [`signature`:`bitcoin_signature_2`]
|
|
* * [`u16`:`len`]
|
|
* * [`len*byte`:`features`]
|
|
* * [`chain_hash`:`chain_hash`]
|
|
* * [`short_channel_id`:`short_channel_id`]
|
|
* * [`point`:`node_id_1`]
|
|
* * [`point`:`node_id_2`]
|
|
* * [`point`:`bitcoin_key_1`]
|
|
* * [`point`:`bitcoin_key_2`]
|
|
*/
|
|
/* 2 byte msg type + 256 byte signatures */
|
|
int offset = 258;
|
|
struct sha256_double hash;
|
|
sha256_double(&hash, announcement + offset,
|
|
tal_count(announcement) - offset);
|
|
|
|
if (!check_signed_hash_nodeid(&hash, node1_sig, node1_id)) {
|
|
return tal_fmt(ctx,
|
|
"Bad node_signature_1 %s hash %s"
|
|
" on channel_announcement %s",
|
|
fmt_secp256k1_ecdsa_signature(tmpctx,
|
|
node1_sig),
|
|
fmt_sha256_double(tmpctx, &hash),
|
|
tal_hex(tmpctx, announcement));
|
|
}
|
|
if (!check_signed_hash_nodeid(&hash, node2_sig, node2_id)) {
|
|
return tal_fmt(ctx,
|
|
"Bad node_signature_2 %s hash %s"
|
|
" on channel_announcement %s",
|
|
fmt_secp256k1_ecdsa_signature(tmpctx,
|
|
node2_sig),
|
|
fmt_sha256_double(tmpctx, &hash),
|
|
tal_hex(tmpctx, announcement));
|
|
}
|
|
if (!check_signed_hash(&hash, bitcoin1_sig, bitcoin1_key)) {
|
|
return tal_fmt(ctx,
|
|
"Bad bitcoin_signature_1 %s hash %s"
|
|
" on channel_announcement %s",
|
|
fmt_secp256k1_ecdsa_signature(tmpctx,
|
|
bitcoin1_sig),
|
|
fmt_sha256_double(tmpctx, &hash),
|
|
tal_hex(tmpctx, announcement));
|
|
}
|
|
if (!check_signed_hash(&hash, bitcoin2_sig, bitcoin2_key)) {
|
|
return tal_fmt(ctx,
|
|
"Bad bitcoin_signature_2 %s hash %s"
|
|
" on channel_announcement %s",
|
|
fmt_secp256k1_ecdsa_signature(tmpctx,
|
|
bitcoin2_sig),
|
|
fmt_sha256_double(tmpctx, &hash),
|
|
tal_hex(tmpctx, announcement));
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* Returns warning msg if signature wrong, else NULL */
|
|
const char *sigcheck_node_announcement(const tal_t *ctx,
|
|
const struct node_id *node_id,
|
|
const secp256k1_ecdsa_signature *signature,
|
|
const u8 *node_announcement)
|
|
{
|
|
/* BOLT #7:
|
|
*
|
|
* 1. type: 257 (`node_announcement`)
|
|
* 2. data:
|
|
* * [`signature`:`signature`]
|
|
* * [`u16`:`flen`]
|
|
* * [`flen*byte`:`features`]
|
|
* * [`u32`:`timestamp`]
|
|
* * [`point`:`node_id`]
|
|
* * [`3*byte`:`rgb_color`]
|
|
* * [`32*byte`:`alias`]
|
|
* * [`u16`:`addrlen`]
|
|
* * [`addrlen*byte`:`addresses`]
|
|
*/
|
|
/* 2 byte msg type + 64 byte signatures */
|
|
int offset = 66;
|
|
struct sha256_double hash;
|
|
|
|
sha256_double(&hash, node_announcement + offset, tal_count(node_announcement) - offset);
|
|
/* If node_id is invalid, it fails here */
|
|
if (!check_signed_hash_nodeid(&hash, signature, node_id)) {
|
|
/* BOLT #7:
|
|
*
|
|
* - if `signature` is not a valid signature, using
|
|
* `node_id` of the double-SHA256 of the entire
|
|
* message following the `signature` field
|
|
* (including unknown fields following
|
|
* `fee_proportional_millionths`):
|
|
* - SHOULD send a `warning` and close the connection.
|
|
* - MUST NOT process the message further.
|
|
*/
|
|
return tal_fmt(ctx,
|
|
"Bad signature for %s hash %s"
|
|
" on node_announcement %s",
|
|
fmt_secp256k1_ecdsa_signature(tmpctx,
|
|
signature),
|
|
fmt_sha256_double(tmpctx, &hash),
|
|
tal_hex(tmpctx, node_announcement));
|
|
}
|
|
|
|
return NULL;
|
|
}
|