From bbe67d8b29f31b140d6987d82912f48539c8bcb7 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 18 Nov 2014 12:32:53 +0100 Subject: [PATCH] Make secp256k1_eckey_pubkey_serialize fail for infinity --- src/eckey.h | 2 +- src/eckey_impl.h | 16 +++++++++++++--- src/secp256k1.c | 13 +++++-------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/eckey.h b/src/eckey.h index 725a5494d6..024c8b821b 100644 --- a/src/eckey.h +++ b/src/eckey.h @@ -12,7 +12,7 @@ #include "num.h" static int secp256k1_eckey_pubkey_parse(secp256k1_ge_t *elem, const unsigned char *pub, int size); -static void secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, int *size, int compressed); +static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, int *size, int compressed); static int secp256k1_eckey_privkey_parse(secp256k1_scalar_t *key, const unsigned char *privkey, int privkeylen); static int secp256k1_eckey_privkey_serialize(unsigned char *privkey, int *privkeylen, const secp256k1_scalar_t *key, int compressed); diff --git a/src/eckey_impl.h b/src/eckey_impl.h index 59b191d1bd..290b1f0900 100644 --- a/src/eckey_impl.h +++ b/src/eckey_impl.h @@ -32,7 +32,10 @@ static int secp256k1_eckey_pubkey_parse(secp256k1_ge_t *elem, const unsigned cha } } -static void secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, int *size, int compressed) { +static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, int *size, int compressed) { + if (secp256k1_ge_is_infinity(elem)) { + return 0; + } secp256k1_fe_normalize(&elem->x); secp256k1_fe_normalize(&elem->y); secp256k1_fe_get_b32(&pub[1], &elem->x); @@ -44,6 +47,7 @@ static void secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char pub[0] = 0x04; secp256k1_fe_get_b32(&pub[33], &elem->y); } + return 1; } static int secp256k1_eckey_privkey_parse(secp256k1_scalar_t *key, const unsigned char *privkey, int privkeylen) { @@ -107,7 +111,10 @@ static int secp256k1_eckey_privkey_serialize(unsigned char *privkey, int *privke secp256k1_scalar_get_b32(ptr, key); ptr += 32; memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle); int pubkeylen = 0; - secp256k1_eckey_pubkey_serialize(&r, ptr, &pubkeylen, 1); ptr += pubkeylen; + if (!secp256k1_eckey_pubkey_serialize(&r, ptr, &pubkeylen, 1)) { + return 0; + } + ptr += pubkeylen; *privkeylen = ptr - privkey; } else { static const unsigned char begin[] = { @@ -131,7 +138,10 @@ static int secp256k1_eckey_privkey_serialize(unsigned char *privkey, int *privke secp256k1_scalar_get_b32(ptr, key); ptr += 32; memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle); int pubkeylen = 0; - secp256k1_eckey_pubkey_serialize(&r, ptr, &pubkeylen, 0); ptr += pubkeylen; + if (!secp256k1_eckey_pubkey_serialize(&r, ptr, &pubkeylen, 0)) { + return 0; + } + ptr += pubkeylen; *privkeylen = ptr - privkey; } return 1; diff --git a/src/secp256k1.c b/src/secp256k1.c index a7d9d16c62..1ab5b3722c 100644 --- a/src/secp256k1.c +++ b/src/secp256k1.c @@ -150,8 +150,7 @@ int secp256k1_ecdsa_recover_compact(const unsigned char *msg, int msglen, const secp256k1_ge_t q; if (secp256k1_ecdsa_sig_recover(&sig, &q, &m, recid)) { - secp256k1_eckey_pubkey_serialize(&q, pubkey, pubkeylen, compressed); - ret = 1; + ret = secp256k1_eckey_pubkey_serialize(&q, pubkey, pubkeylen, compressed); } return ret; } @@ -187,8 +186,7 @@ int secp256k1_ec_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsi secp256k1_scalar_clear(&sec); secp256k1_ge_t p; secp256k1_ge_set_gej(&p, &pj); - secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, compressed); - return 1; + return secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, compressed); } int secp256k1_ec_pubkey_decompress(unsigned char *pubkey, int *pubkeylen) { @@ -198,8 +196,7 @@ int secp256k1_ec_pubkey_decompress(unsigned char *pubkey, int *pubkeylen) { secp256k1_ge_t p; if (!secp256k1_eckey_pubkey_parse(&p, pubkey, *pubkeylen)) return 0; - secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, 0); - return 1; + return secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, 0); } int secp256k1_ec_privkey_tweak_add(unsigned char *seckey, const unsigned char *tweak) { @@ -236,7 +233,7 @@ int secp256k1_ec_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const un } if (ret) { int oldlen = pubkeylen; - secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33); + ret = secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33); VERIFY_CHECK(pubkeylen == oldlen); } @@ -276,7 +273,7 @@ int secp256k1_ec_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const un } if (ret) { int oldlen = pubkeylen; - secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33); + ret = secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33); VERIFY_CHECK(pubkeylen == oldlen); }