aboutsummaryrefslogtreecommitdiff
path: root/src/key.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2014-12-01 14:57:49 +0100
committerWladimir J. van der Laan <laanwj@gmail.com>2014-12-01 14:57:58 +0100
commitf0877f8b6200dcf6df12df5e69d498d365f81f8b (patch)
treedc5f63386a9c0752a62301a815d6a976286a1b42 /src/key.cpp
parent89151d9f29870cc9246f76baba75b75d3a7528d7 (diff)
parent4cdaa95a209808276992dc1eb0ed0773f7927073 (diff)
Merge pull request #5227
4cdaa95 Resize after succesful result (Pieter Wuille) 9d8604f Header define style cleanups (Pieter Wuille) a53fd41 Deterministic signing (Pieter Wuille) 3060e36 Add the RFC6979 PRNG (Pieter Wuille) a8f5087 Add HMAC-SHA256 (Pieter Wuille) 36fa4a7 Split up crypto/sha2 (Pieter Wuille)
Diffstat (limited to 'src/key.cpp')
-rw-r--r--src/key.cpp32
1 files changed, 20 insertions, 12 deletions
diff --git a/src/key.cpp b/src/key.cpp
index a91ed1cc1d..acf62360a4 100644
--- a/src/key.cpp
+++ b/src/key.cpp
@@ -4,7 +4,8 @@
#include "key.h"
-#include "crypto/sha2.h"
+#include "crypto/hmac_sha512.h"
+#include "crypto/rfc6979_hmac_sha256.h"
#include "eccryptoverify.h"
#include "pubkey.h"
#include "random.h"
@@ -71,19 +72,23 @@ CPubKey CKey::GetPubKey() const {
return result;
}
-bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
+bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
if (!fValid)
return false;
vchSig.resize(72);
- int nSigLen = 72;
- CKey nonce;
+ RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
do {
- nonce.MakeNewKey(true);
- if (secp256k1_ecdsa_sign((const unsigned char*)&hash, 32, (unsigned char*)&vchSig[0], &nSigLen, begin(), nonce.begin()))
- break;
+ uint256 nonce;
+ prng.Generate((unsigned char*)&nonce, 32);
+ nonce += test_case;
+ int nSigLen = 72;
+ int ret = secp256k1_ecdsa_sign((const unsigned char*)&hash, 32, (unsigned char*)&vchSig[0], &nSigLen, begin(), (unsigned char*)&nonce);
+ nonce = 0;
+ if (ret) {
+ vchSig.resize(nSigLen);
+ return true;
+ }
} while(true);
- vchSig.resize(nSigLen);
- return true;
}
bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
@@ -105,10 +110,13 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
return false;
vchSig.resize(65);
int rec = -1;
- CKey nonce;
+ RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
do {
- nonce.MakeNewKey(true);
- if (secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, 32, &vchSig[1], begin(), nonce.begin(), &rec))
+ uint256 nonce;
+ prng.Generate((unsigned char*)&nonce, 32);
+ int ret = secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, 32, &vchSig[1], begin(), (unsigned char*)&nonce, &rec);
+ nonce = 0;
+ if (ret)
break;
} while(true);
assert(rec != -1);