diff options
author | Gregory Maxwell <greg@xiph.org> | 2013-08-15 18:42:28 -0700 |
---|---|---|
committer | Gregory Maxwell <greg@xiph.org> | 2013-08-15 18:42:28 -0700 |
commit | 47491a90b6544fbb99dc00b3deb554da66c3a54a (patch) | |
tree | 337a390536a472307825c284525814143b6a89a2 /src/key.cpp | |
parent | 13d3b1c144756dfe31792209d09f5205f55f95bb (diff) | |
parent | a81cd96805ce6b65cca3a40ebbd3b2eb428abb7b (diff) |
Merge pull request #2131 from sipa/evens
Only create signatures with even S, and verification mode to check.
Diffstat (limited to 'src/key.cpp')
-rw-r--r-- | src/key.cpp | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/key.cpp b/src/key.cpp index 5d7ea93a0f..85dc9cda2b 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -195,9 +195,26 @@ public: } bool Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) { + vchSig.clear(); + ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey); + if (sig == NULL) + return false; + if (BN_is_odd(sig->s)) { + // enforce even S values, by negating the value (modulo the order) if odd + BN_CTX *ctx = BN_CTX_new(); + BN_CTX_start(ctx); + const EC_GROUP *group = EC_KEY_get0_group(pkey); + BIGNUM *order = BN_CTX_get(ctx); + EC_GROUP_get_order(group, order, ctx); + BN_sub(sig->s, order, sig->s); + BN_CTX_end(ctx); + BN_CTX_free(ctx); + } unsigned int nSize = ECDSA_size(pkey); vchSig.resize(nSize); // Make sure it is big enough - assert(ECDSA_sign(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], &nSize, pkey)); + unsigned char *pos = &vchSig[0]; + nSize = i2d_ECDSA_SIG(sig, &pos); + ECDSA_SIG_free(sig); vchSig.resize(nSize); // Shrink to fit actual size return true; } |