From f87e7064071b0f730922f533f565038c7973ebf6 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 3 Oct 2013 18:19:30 +0200 Subject: [PATCH] add define for RFC6979 --- ecdsa.c | 18 ++++++++++-------- ecdsa.h | 4 ++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ecdsa.c b/ecdsa.c index 5b3a213d5..0ac4ef28b 100644 --- a/ecdsa.c +++ b/ecdsa.c @@ -144,9 +144,9 @@ int generate_k_random(bignum256 *k) { } k->val[8] = random32() & 0xFFFF; // if k is too big or too small, we don't like it - if (k->val[5] == 0x3FFFFFFF && k->val[6] == 0x3FFFFFFF && k->val[7] == 0x3FFFFFFF && k->val[8] == 0xFFFF) continue; - if (k->val[5] == 0x0 && k->val[6] == 0x0 && k->val[7] == 0x0 && k->val[8] == 0x0) continue; - return 0; // good number - no error + if ( !bn_is_zero(k) && bn_is_less(k, &order256k1) ) { + return 0; // good number - no error + } } // we generated 10000 numbers, none of them is good -> fail return 1; @@ -214,15 +214,17 @@ int ecdsa_sign(const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, ui bn_read_be(hash, &z); - // generate random number k - //if (generate_k_random(&k) != 0) { - // return 1; - //} - +#if USE_RFC6979 // generate K deterministically if (generate_k_rfc6979(&k, priv_key, hash) != 0) { return 1; } +#else + // generate random number k + if (generate_k_random(&k) != 0) { + return 1; + } +#endif // compute k*G scalar_multiply(&k, &R); diff --git a/ecdsa.h b/ecdsa.h index 9df72d1a9..fc0acbba6 100644 --- a/ecdsa.h +++ b/ecdsa.h @@ -28,6 +28,10 @@ #include "secp256k1.h" +#ifndef USE_RFC6979 +#define USE_RFC6979 1 +#endif + // all functions use secp256k1 curve int ecdsa_sign(const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig); void ecdsa_get_public_key33(const uint8_t *priv_key, uint8_t *pub_key);