From 93b5db89a8dd28e048a09cc26575b167dbbe3c77 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Jan 2016 06:41:45 +1030 Subject: [PATCH] signature: expose check_signed_hash() This is wanted for crypto communications to check signature. Signed-off-by: Rusty Russell --- bitcoin/signature.c | 12 ++++++------ bitcoin/signature.h | 10 +++++++--- test-cli/close-channel.c | 2 +- test-cli/create-anchor-tx.c | 2 +- test-cli/create-commit-spend-tx.c | 2 +- test-cli/create-commit-tx.c | 2 +- test-cli/create-htlc-spend-tx.c | 2 +- test-cli/create-steal-tx.c | 2 +- test-cli/open-anchor.c | 2 +- test-cli/open-commit-sig.c | 2 +- test-cli/update-channel-accept.c | 2 +- test-cli/update-channel-signature.c | 2 +- 12 files changed, 23 insertions(+), 19 deletions(-) diff --git a/bitcoin/signature.c b/bitcoin/signature.c index 28181a639..2fd1b404e 100644 --- a/bitcoin/signature.c +++ b/bitcoin/signature.c @@ -76,7 +76,7 @@ static void dump_tx(const char *msg, } #endif -bool sign_hash(const tal_t *ctx, const struct privkey *privkey, +bool sign_hash(const struct privkey *privkey, const struct sha256_double *h, struct signature *s) { @@ -133,7 +133,7 @@ static void sha256_tx_one_input(struct bitcoin_tx *tx, } /* Only does SIGHASH_ALL */ -bool sign_tx_input(const tal_t *ctx, struct bitcoin_tx *tx, +bool sign_tx_input(struct bitcoin_tx *tx, unsigned int in, const u8 *subscript, size_t subscript_len, const struct privkey *privkey, const struct pubkey *key, @@ -143,12 +143,12 @@ bool sign_tx_input(const tal_t *ctx, struct bitcoin_tx *tx, sha256_tx_one_input(tx, in, subscript, subscript_len, &hash); dump_tx("Signing", tx, in, subscript, subscript_len, key, &hash); - return sign_hash(ctx, privkey, &hash, sig); + return sign_hash(privkey, &hash, sig); } -static bool check_signed_hash(const struct sha256_double *hash, - const struct signature *signature, - const struct pubkey *key) +bool check_signed_hash(const struct sha256_double *hash, + const struct signature *signature, + const struct pubkey *key) { int ret; secp256k1_context *secpctx; diff --git a/bitcoin/signature.h b/bitcoin/signature.h index c90632278..2ff0c8c88 100644 --- a/bitcoin/signature.h +++ b/bitcoin/signature.h @@ -3,7 +3,7 @@ #include "config.h" #include "secp256k1.h" #include -#include +#include enum sighash_type { SIGHASH_ALL = 1, @@ -28,12 +28,16 @@ struct privkey; struct bitcoin_tx_output; struct bitcoin_signature; -bool sign_hash(const tal_t *ctx, const struct privkey *p, +bool sign_hash(const struct privkey *p, const struct sha256_double *h, struct signature *s); +bool check_signed_hash(const struct sha256_double *hash, + const struct signature *signature, + const struct pubkey *key); + /* All tx input scripts must be set to 0 len. */ -bool sign_tx_input(const tal_t *ctx, struct bitcoin_tx *tx, +bool sign_tx_input(struct bitcoin_tx *tx, unsigned int in, const u8 *subscript, size_t subscript_len, const struct privkey *privkey, const struct pubkey *pubkey, diff --git a/test-cli/close-channel.c b/test-cli/close-channel.c index fa2f29021..bc88be87e 100644 --- a/test-cli/close-channel.c +++ b/test-cli/close-channel.c @@ -92,7 +92,7 @@ int main(int argc, char *argv[]) cstate->b.pay_msat / 1000); /* Sign it for them. */ - sign_tx_input(ctx, close_tx, 0, redeemscript, tal_count(redeemscript), + sign_tx_input(close_tx, 0, redeemscript, tal_count(redeemscript), &privkey, &pubkey1, &sig); if (close_file) diff --git a/test-cli/create-anchor-tx.c b/test-cli/create-anchor-tx.c index 171e9c7ae..9a892106c 100644 --- a/test-cli/create-anchor-tx.c +++ b/test-cli/create-anchor-tx.c @@ -163,7 +163,7 @@ int main(int argc, char *argv[]) /* Now, sign each input. */ for (i = 0; i < tal_count(in); i++) { in[i].sig.stype = SIGHASH_ALL; - if (!sign_tx_input(ctx, anchor, i, in[i].in.script, + if (!sign_tx_input(anchor, i, in[i].in.script, in[i].in.script_length, &in[i].privkey, &in[i].pubkey, &in[i].sig.sig)) diff --git a/test-cli/create-commit-spend-tx.c b/test-cli/create-commit-spend-tx.c index eb90ea275..c5c939036 100644 --- a/test-cli/create-commit-spend-tx.c +++ b/test-cli/create-commit-spend-tx.c @@ -111,7 +111,7 @@ int main(int argc, char *argv[]) tx->output[0].script_length = tal_count(tx->output[0].script); /* Now get signature, to set up input script. */ - if (!sign_tx_input(tx, tx, 0, redeemscript, tal_count(redeemscript), + if (!sign_tx_input(tx, 0, redeemscript, tal_count(redeemscript), &privkey, &pubkey1, &sig.sig)) errx(1, "Could not sign tx"); sig.stype = SIGHASH_ALL; diff --git a/test-cli/create-commit-tx.c b/test-cli/create-commit-tx.c index 527c5827a..6f03794f3 100644 --- a/test-cli/create-commit-tx.c +++ b/test-cli/create-commit-tx.c @@ -81,7 +81,7 @@ int main(int argc, char *argv[]) /* We generate our signature. */ sig1.stype = SIGHASH_ALL; - sign_tx_input(ctx, commit, 0, redeemscript, tal_count(redeemscript), + sign_tx_input(commit, 0, redeemscript, tal_count(redeemscript), &privkey, &pubkey1, &sig1.sig); /* Check it works with theirs... */ diff --git a/test-cli/create-htlc-spend-tx.c b/test-cli/create-htlc-spend-tx.c index eecad265c..46565555a 100644 --- a/test-cli/create-htlc-spend-tx.c +++ b/test-cli/create-htlc-spend-tx.c @@ -185,7 +185,7 @@ int main(int argc, char *argv[]) tx->output[0].script_length = tal_count(tx->output[0].script); /* Now get signature, to set up input script. */ - if (!sign_tx_input(tx, tx, 0, redeemscript, tal_count(redeemscript), + if (!sign_tx_input(tx, 0, redeemscript, tal_count(redeemscript), &privkey, &key, &sig.sig)) errx(1, "Could not sign tx"); diff --git a/test-cli/create-steal-tx.c b/test-cli/create-steal-tx.c index 07268d25f..890fda4d0 100644 --- a/test-cli/create-steal-tx.c +++ b/test-cli/create-steal-tx.c @@ -112,7 +112,7 @@ int main(int argc, char *argv[]) tx->output[0].script_length = tal_count(tx->output[0].script); /* Now get signature, to set up input script. */ - if (!sign_tx_input(tx, tx, 0, redeemscript, tal_count(redeemscript), + if (!sign_tx_input(tx, 0, redeemscript, tal_count(redeemscript), &privkey, &pubkey1, &sig.sig)) errx(1, "Could not sign tx"); sig.stype = SIGHASH_ALL; diff --git a/test-cli/open-anchor.c b/test-cli/open-anchor.c index e0a000be3..e21848d1a 100644 --- a/test-cli/open-anchor.c +++ b/test-cli/open-anchor.c @@ -81,7 +81,7 @@ int main(int argc, char *argv[]) invert_cstate(cstate); commit = create_commit_tx(ctx, o2, o1, &oa, &rhash, cstate); - sign_tx_input(ctx, commit, 0, redeemscript, tal_count(redeemscript), + sign_tx_input(commit, 0, redeemscript, tal_count(redeemscript), &privkey, &pubkey1, &sig); oa.commit_sig = signature_to_proto(ctx, &sig); diff --git a/test-cli/open-commit-sig.c b/test-cli/open-commit-sig.c index 21425a419..6955db537 100644 --- a/test-cli/open-commit-sig.c +++ b/test-cli/open-commit-sig.c @@ -75,7 +75,7 @@ int main(int argc, char *argv[]) /* Sign it for them. */ subscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); - sign_tx_input(ctx, commit, 0, subscript, tal_count(subscript), + sign_tx_input(commit, 0, subscript, tal_count(subscript), &privkey, &pubkey1, &sig); pkt = open_commit_sig_pkt(ctx, &sig); diff --git a/test-cli/update-channel-accept.c b/test-cli/update-channel-accept.c index 482983a44..55f913e95 100644 --- a/test-cli/update-channel-accept.c +++ b/test-cli/update-channel-accept.c @@ -91,7 +91,7 @@ int main(int argc, char *argv[]) errx(1, "Delta too large"); /* Sign it for them. */ - sign_tx_input(ctx, commit, 0, redeemscript, tal_count(redeemscript), + sign_tx_input(commit, 0, redeemscript, tal_count(redeemscript), &privkey, &pubkey1, &sig.sig); pkt = update_accept_pkt(ctx, &sig.sig, &revocation_hash); diff --git a/test-cli/update-channel-signature.c b/test-cli/update-channel-signature.c index 81bf7a5a7..26f5c6cd9 100644 --- a/test-cli/update-channel-signature.c +++ b/test-cli/update-channel-signature.c @@ -106,7 +106,7 @@ int main(int argc, char *argv[]) errx(1, "Invalid public open-channel-file2"); /* Sign it for them. */ - sign_tx_input(ctx, commit, 0, redeemscript, tal_count(redeemscript), + sign_tx_input(commit, 0, redeemscript, tal_count(redeemscript), &privkey, &pubkey1, &sig.sig); pkt = update_signature_pkt(ctx, &sig.sig, &preimage);