]> Git Repo - VerusCoin.git/blob - src/ecwrapper.cpp
Include missing config/bitcoin-config.h.
[VerusCoin.git] / src / ecwrapper.cpp
1 // Copyright (c) 2009-2014 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include "ecwrapper.h"
6
7 #include "serialize.h"
8 #include "uint256.h"
9
10 #include <openssl/bn.h>
11 #include <openssl/ecdsa.h>
12 #include <openssl/obj_mac.h>
13
14 namespace {
15
16 /**
17  * Perform ECDSA key recovery (see SEC1 4.1.6) for curves over (mod p)-fields
18  * recid selects which key is recovered
19  * if check is non-zero, additional checks are performed
20  */
21 int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check)
22 {
23     if (!eckey) return 0;
24
25     int ret = 0;
26     BN_CTX *ctx = NULL;
27
28     BIGNUM *x = NULL;
29     BIGNUM *e = NULL;
30     BIGNUM *order = NULL;
31     BIGNUM *sor = NULL;
32     BIGNUM *eor = NULL;
33     BIGNUM *field = NULL;
34     EC_POINT *R = NULL;
35     EC_POINT *O = NULL;
36     EC_POINT *Q = NULL;
37     BIGNUM *rr = NULL;
38     BIGNUM *zero = NULL;
39     int n = 0;
40     int i = recid / 2;
41
42     const EC_GROUP *group = EC_KEY_get0_group(eckey);
43     if ((ctx = BN_CTX_new()) == NULL) { ret = -1; goto err; }
44     BN_CTX_start(ctx);
45     order = BN_CTX_get(ctx);
46     if (!EC_GROUP_get_order(group, order, ctx)) { ret = -2; goto err; }
47     x = BN_CTX_get(ctx);
48     if (!BN_copy(x, order)) { ret=-1; goto err; }
49     if (!BN_mul_word(x, i)) { ret=-1; goto err; }
50     if (!BN_add(x, x, ecsig->r)) { ret=-1; goto err; }
51     field = BN_CTX_get(ctx);
52     if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; }
53     if (BN_cmp(x, field) >= 0) { ret=0; goto err; }
54     if ((R = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
55     if (!EC_POINT_set_compressed_coordinates_GFp(group, R, x, recid % 2, ctx)) { ret=0; goto err; }
56     if (check)
57     {
58         if ((O = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
59         if (!EC_POINT_mul(group, O, NULL, R, order, ctx)) { ret=-2; goto err; }
60         if (!EC_POINT_is_at_infinity(group, O)) { ret = 0; goto err; }
61     }
62     if ((Q = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
63     n = EC_GROUP_get_degree(group);
64     e = BN_CTX_get(ctx);
65     if (!BN_bin2bn(msg, msglen, e)) { ret=-1; goto err; }
66     if (8*msglen > n) BN_rshift(e, e, 8-(n & 7));
67     zero = BN_CTX_get(ctx);
68     if (!BN_zero(zero)) { ret=-1; goto err; }
69     if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; }
70     rr = BN_CTX_get(ctx);
71     if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) { ret=-1; goto err; }
72     sor = BN_CTX_get(ctx);
73     if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) { ret=-1; goto err; }
74     eor = BN_CTX_get(ctx);
75     if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; }
76     if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; }
77     if (!EC_KEY_set_public_key(eckey, Q)) { ret=-2; goto err; }
78
79     ret = 1;
80
81 err:
82     if (ctx) {
83         BN_CTX_end(ctx);
84         BN_CTX_free(ctx);
85     }
86     if (R != NULL) EC_POINT_free(R);
87     if (O != NULL) EC_POINT_free(O);
88     if (Q != NULL) EC_POINT_free(Q);
89     return ret;
90 }
91
92 } // anon namespace
93
94 CECKey::CECKey() {
95     pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
96     assert(pkey != NULL);
97 }
98
99 CECKey::~CECKey() {
100     EC_KEY_free(pkey);
101 }
102
103 void CECKey::GetPubKey(std::vector<unsigned char> &pubkey, bool fCompressed) {
104     EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
105     int nSize = i2o_ECPublicKey(pkey, NULL);
106     assert(nSize);
107     assert(nSize <= 65);
108     pubkey.clear();
109     pubkey.resize(nSize);
110     unsigned char *pbegin(begin_ptr(pubkey));
111     int nSize2 = i2o_ECPublicKey(pkey, &pbegin);
112     assert(nSize == nSize2);
113 }
114
115 bool CECKey::SetPubKey(const unsigned char* pubkey, size_t size) {
116     return o2i_ECPublicKey(&pkey, &pubkey, size) != NULL;
117 }
118
119 bool CECKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
120     // -1 = error, 0 = bad sig, 1 = good
121     if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1)
122         return false;
123     return true;
124 }
125
126 bool CECKey::Recover(const uint256 &hash, const unsigned char *p64, int rec)
127 {
128     if (rec<0 || rec>=3)
129         return false;
130     ECDSA_SIG *sig = ECDSA_SIG_new();
131     BN_bin2bn(&p64[0],  32, sig->r);
132     BN_bin2bn(&p64[32], 32, sig->s);
133     bool ret = ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), rec, 0) == 1;
134     ECDSA_SIG_free(sig);
135     return ret;
136 }
137
138 bool CECKey::TweakPublic(const unsigned char vchTweak[32]) {
139     bool ret = true;
140     BN_CTX *ctx = BN_CTX_new();
141     BN_CTX_start(ctx);
142     BIGNUM *bnTweak = BN_CTX_get(ctx);
143     BIGNUM *bnOrder = BN_CTX_get(ctx);
144     BIGNUM *bnOne = BN_CTX_get(ctx);
145     const EC_GROUP *group = EC_KEY_get0_group(pkey);
146     EC_GROUP_get_order(group, bnOrder, ctx); // what a grossly inefficient way to get the (constant) group order...
147     BN_bin2bn(vchTweak, 32, bnTweak);
148     if (BN_cmp(bnTweak, bnOrder) >= 0)
149         ret = false; // extremely unlikely
150     EC_POINT *point = EC_POINT_dup(EC_KEY_get0_public_key(pkey), group);
151     BN_one(bnOne);
152     EC_POINT_mul(group, point, bnTweak, point, bnOne, ctx);
153     if (EC_POINT_is_at_infinity(group, point))
154         ret = false; // ridiculously unlikely
155     EC_KEY_set_public_key(pkey, point);
156     EC_POINT_free(point);
157     BN_CTX_end(ctx);
158     BN_CTX_free(ctx);
159     return ret;
160 }
161
162 bool CECKey::SanityCheck()
163 {
164     EC_KEY *pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
165     if(pkey == NULL)
166         return false;
167     EC_KEY_free(pkey);
168
169     // TODO Is there more EC functionality that could be missing?
170     return true;
171 }
This page took 0.032848 seconds and 4 git commands to generate.