OpenSSL API hacks
[novacoin.git] / src / key.cpp
index 1ed4e25..1f42533 100644 (file)
@@ -80,7 +80,12 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned ch
     x = BN_CTX_get(ctx);
     if (!BN_copy(x, order)) { ret=-1; goto err; }
     if (!BN_mul_word(x, i)) { ret=-1; goto err; }
-    if (!BN_add(x, x, ecsig->r)) { ret=-1; goto err; }
+
+    // Get internal R and S pointers
+    const BIGNUM *ecsig_r, *ecsig_s;
+    ECDSA_SIG_get0(ecsig, &ecsig_r, &ecsig_s);
+
+    if (!BN_add(x, x, ecsig_r)) { ret=-1; goto err; }
     field = BN_CTX_get(ctx);
     if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; }
     if (BN_cmp(x, field) >= 0) { ret=0; goto err; }
@@ -98,12 +103,12 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned ch
     if (!BN_bin2bn(msg, msglen, e)) { ret=-1; goto err; }
     if (8*msglen > n) BN_rshift(e, e, 8-(n & 7));
     zero = BN_CTX_get(ctx);
-    if (!BN_zero(zero)) { ret=-1; goto err; }
+    BN_zero(zero);
     if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; }
     rr = BN_CTX_get(ctx);
-    if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) { ret=-1; goto err; }
+    if (!BN_mod_inverse(rr, ecsig_r, order, ctx)) { ret=-1; goto err; }
     sor = BN_CTX_get(ctx);
-    if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) { ret=-1; goto err; }
+    if (!BN_mod_mul(sor, ecsig_s, rr, order, ctx)) { ret=-1; goto err; }
     eor = BN_CTX_get(ctx);
     if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; }
     if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; }
@@ -375,12 +380,20 @@ bool CKey::Sign(uint256 hash, std::vector<unsigned char>& vchSig)
         return false;
     const EC_GROUP *group = EC_KEY_get0_group(pkey);
     CBigNum order, halforder;
-    EC_GROUP_get_order(group, &order, NULL);
-    BN_rshift1(&halforder, &order);
+    EC_GROUP_get_order(group, order.get(), NULL);
+    BN_rshift1(halforder.get(), order.get());
+
+    // Get internal R and S pointers
+    const BIGNUM *current_s = ECDSA_SIG_get0_s(sig);
+
     // enforce low S values, by negating the value (modulo the order) if above order/2.
-    if (BN_cmp(sig->s, &halforder) > 0) {
-        BN_sub(sig->s, &order, sig->s);
+    if (BN_cmp(current_s, halforder.get()) > 0) {
+        BIGNUM *updated_s = BN_new();
+        BN_copy(updated_s, current_s);
+        BN_sub(updated_s, order.get(), updated_s);
+        ECDSA_SIG_set0(sig, NULL, updated_s);
     }
+
     unsigned int nSize = ECDSA_size(pkey);
     vchSig.resize(nSize); // Make sure it is big enough
     unsigned char *pos = &vchSig[0];
@@ -407,16 +420,24 @@ bool CKey::SignCompact(uint256 hash, std::vector<unsigned char>& vchSig)
         return false;
     const EC_GROUP *group = EC_KEY_get0_group(pkey);
     CBigNum order, halforder;
-    EC_GROUP_get_order(group, &order, NULL);
-    BN_rshift1(&halforder, &order);
+    EC_GROUP_get_order(group, order.get(), NULL);
+    BN_rshift1(halforder.get(), order.get());
+
+    // Get internal R and S pointers
+    const BIGNUM *current_s = ECDSA_SIG_get0_s(sig);
+
     // enforce low S values, by negating the value (modulo the order) if above order/2.
-    if (BN_cmp(sig->s, &halforder) > 0) {
-        BN_sub(sig->s, &order, sig->s);
+    if (BN_cmp(current_s, halforder.get()) > 0) {
+        BIGNUM *updated_s = BN_new();
+        BN_copy(updated_s, current_s);
+        BN_sub(updated_s, order.get(), updated_s);
+        ECDSA_SIG_set0(sig, NULL, updated_s);
     }
+
     vchSig.clear();
     vchSig.resize(65,0);
-    int nBitsR = BN_num_bits(sig->r);
-    int nBitsS = BN_num_bits(sig->s);
+    int nBitsR = BN_num_bits(ECDSA_SIG_get0_r(sig));
+    int nBitsS = BN_num_bits(ECDSA_SIG_get0_s(sig));
     bool fCompressedPubKey = IsCompressed();
     if (nBitsR <= 256 && nBitsS <= 256)
     {
@@ -441,8 +462,8 @@ bool CKey::SignCompact(uint256 hash, std::vector<unsigned char>& vchSig)
         }
 
         vchSig[0] = nRecId+27+(fCompressedPubKey ? 4 : 0);
-        BN_bn2bin(sig->r,&vchSig[33-(nBitsR+7)/8]);
-        BN_bn2bin(sig->s,&vchSig[65-(nBitsS+7)/8]);
+        BN_bn2bin(ECDSA_SIG_get0_r(sig),&vchSig[33-(nBitsR+7)/8]);
+        BN_bn2bin(ECDSA_SIG_get0_s(sig),&vchSig[65-(nBitsS+7)/8]);
         fOk = true;
     }
     ECDSA_SIG_free(sig);
@@ -461,8 +482,10 @@ bool CPubKey::SetCompactSignature(uint256 hash, const std::vector<unsigned char>
     if (nV<27 || nV>=35)
         return false;
     ECDSA_SIG *sig = ECDSA_SIG_new();
-    BN_bin2bn(&vchSig[1],32,sig->r);
-    BN_bin2bn(&vchSig[33],32,sig->s);
+    BIGNUM *sig_r, *sig_s;
+    BN_bin2bn(&vchSig[1],32,sig_r);
+    BN_bin2bn(&vchSig[33],32,sig_s);
+    ECDSA_SIG_set0(sig, sig_r, sig_s);
     bool fSuccessful = false;
     EC_KEY* pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
     if (nV >= 31)
@@ -631,7 +654,7 @@ bool CPoint::getBytes(std::vector<unsigned char> &vchBytes)
 // ECC multiplication by specified multiplier
 bool CPoint::ECMUL(const CBigNum &bnMultiplier)
 {
-    if (!EC_POINT_mul(group, point, NULL, point, &bnMultiplier, NULL)) {
+    if (!EC_POINT_mul(group, point, NULL, point, bnMultiplier.get(), NULL)) {
         printf("CPoint::ECMUL() : EC_POINT_mul failed");
         return false;
     }
@@ -642,7 +665,7 @@ bool CPoint::ECMUL(const CBigNum &bnMultiplier)
 // Calculate G*m + q
 bool CPoint::ECMULGEN(const CBigNum &bnMultiplier, const CPoint &qPoint)
 {
-    if (!EC_POINT_mul(group, point, &bnMultiplier, qPoint.point, BN_value_one(), NULL)) {
+    if (!EC_POINT_mul(group, point, bnMultiplier.get(), qPoint.point, BN_value_one(), NULL)) {
         printf("CPoint::ECMULGEN() : EC_POINT_mul failed.");
         return false;
     }