From fc144587e6bcdbeb23dd6b15bf036c1f818294b6 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Tue, 4 Feb 2014 19:12:43 +0100 Subject: [PATCH] introduce ecdsa_verify_digest --- ecdsa.c | 27 +++++++++++++++++++-------- ecdsa.h | 2 ++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ecdsa.c b/ecdsa.c index 958ba2a52..7bb830ada 100644 --- a/ecdsa.c +++ b/ecdsa.c @@ -423,18 +423,29 @@ int ecdsa_read_pubkey(const uint8_t *pub_key, curve_point *pub) // signature - 64 bytes signature // msg is a data that was signed // msg_len is the message length -// returns 0 if verification succeeded -// it is assumed that public key is valid otherwise calling this does not make much sense + int ecdsa_verify(const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len) { - int i, j; uint8_t hash[32]; + SHA256_Raw(msg, msg_len, hash); + return ecdsa_verify_digest(pub_key, sig, hash); +} + +int ecdsa_verify_double(const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len) +{ + uint8_t hash[32]; + SHA256_Raw(msg, msg_len, hash); + SHA256_Raw(hash, 32, hash); + return ecdsa_verify_digest(pub_key, sig, hash); +} + +// returns 0 if verification succeeded +// it is assumed that public key is valid otherwise calling this does not make much sense +int ecdsa_verify_digest(const uint8_t *pub_key, const uint8_t *sig, const uint8_t *digest) +{ + int i, j; curve_point pub, res; bignum256 r, s, z; - // compute hash function of message - SHA256_Raw(msg, msg_len, hash); - // if double hash is required uncomment the following line: - // SHA256_Raw(hash, 32, hash); if (!ecdsa_read_pubkey(pub_key, &pub)) { return 1; @@ -443,7 +454,7 @@ int ecdsa_verify(const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, bn_read_be(sig, &r); bn_read_be(sig + 32, &s); - bn_read_be(hash, &z); + bn_read_be(digest, &z); if (bn_is_zero(&r) || bn_is_zero(&s) || (!bn_is_less(&r, &order256k1)) || diff --git a/ecdsa.h b/ecdsa.h index 8ff95cd19..97661c4aa 100644 --- a/ecdsa.h +++ b/ecdsa.h @@ -46,6 +46,8 @@ void ecdsa_get_address(const uint8_t *pub_key, uint8_t version, char *addr); int ecdsa_address_decode(const char *addr, uint8_t *out); int ecdsa_read_pubkey(const uint8_t *pub_key, curve_point *pub); int ecdsa_verify(const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len); +int ecdsa_verify_double(const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len); +int ecdsa_verify_digest(const uint8_t *pub_key, const uint8_t *sig, const uint8_t *digest); int ecdsa_sig_to_der(const uint8_t *sig, uint8_t *der); #endif