From 73489fbd339f7b62a93b3fafd41843ac8bdaef91 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Sun, 2 Feb 2014 20:36:03 +0100 Subject: [PATCH] split signing into ecdsa_sign_digest and ecdsa_sign/ecdsa_sign_double --- ecdsa.c | 33 +++++++++++++++++++++++---------- ecdsa.h | 2 ++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/ecdsa.c b/ecdsa.c index 6954ac59d..3fbfe27ab 100644 --- a/ecdsa.c +++ b/ecdsa.c @@ -195,28 +195,41 @@ int generate_k_rfc6979(bignum256 *secret, const uint8_t *priv_key, const uint8_t return 1; } -// uses secp256k1 curve -// priv_key is a 32 byte big endian stored number // msg is a data to be signed // msg_len is the message length -// sig is 64 bytes long array for the signature int ecdsa_sign(const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig) { - uint32_t i; uint8_t hash[32]; + SHA256_Raw(msg, msg_len, hash); + return ecdsa_sign_digest(priv_key, hash, sig); +} + +// msg is a data to be signed +// msg_len is the message length +int ecdsa_sign_double(const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig) +{ + uint8_t hash[32]; + SHA256_Raw(msg, msg_len, hash); + SHA256_Raw(hash, 32, hash); + return ecdsa_sign_digest(priv_key, hash, sig); +} + +// uses secp256k1 curve +// priv_key is a 32 byte big endian stored number +// sig is 64 bytes long array for the signature +// digest is 32 bytes of digest +int ecdsa_sign_digest(const uint8_t *priv_key, const uint8_t *digest, uint8_t *sig) +{ + uint32_t i; curve_point R; bignum256 k, z; bignum256 *da = &R.y; - // 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); - bn_read_be(hash, &z); + bn_read_be(digest, &z); #if USE_RFC6979 // generate K deterministically - if (generate_k_rfc6979(&k, priv_key, hash) != 0) { + if (generate_k_rfc6979(&k, priv_key, digest) != 0) { return 1; } #else diff --git a/ecdsa.h b/ecdsa.h index e1e9d3ffb..4c0bdd9ce 100644 --- a/ecdsa.h +++ b/ecdsa.h @@ -37,6 +37,8 @@ void point_double(curve_point *cp); void scalar_multiply(bignum256 *k, curve_point *res); int ecdsa_sign(const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig); +int ecdsa_sign_double(const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig); +int ecdsa_sign_digest(const uint8_t *priv_key, const uint8_t *digest, uint8_t *sig); void ecdsa_get_public_key33(const uint8_t *priv_key, uint8_t *pub_key); void ecdsa_get_public_key65(const uint8_t *priv_key, uint8_t *pub_key); void ecdsa_get_address(const uint8_t *pub_key, uint8_t version, char *addr);