1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-14 03:30:02 +00:00

introduce ecdsa_verify_digest

This commit is contained in:
Pavol Rusnak 2014-02-04 19:12:43 +01:00
parent 8aaebe761b
commit fc144587e6
2 changed files with 21 additions and 8 deletions

27
ecdsa.c
View File

@ -423,18 +423,29 @@ int ecdsa_read_pubkey(const uint8_t *pub_key, curve_point *pub)
// signature - 64 bytes signature // signature - 64 bytes signature
// msg is a data that was signed // msg is a data that was signed
// msg_len is the message length // 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 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]; 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; curve_point pub, res;
bignum256 r, s, z; 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)) { if (!ecdsa_read_pubkey(pub_key, &pub)) {
return 1; 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, &r);
bn_read_be(sig + 32, &s); 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) || if (bn_is_zero(&r) || bn_is_zero(&s) ||
(!bn_is_less(&r, &order256k1)) || (!bn_is_less(&r, &order256k1)) ||

View File

@ -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_address_decode(const char *addr, uint8_t *out);
int ecdsa_read_pubkey(const uint8_t *pub_key, curve_point *pub); 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(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); int ecdsa_sig_to_der(const uint8_t *sig, uint8_t *der);
#endif #endif