Line data Source code
1 : // Copyright (c) 2009-2014 The Bitcoin Core 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 : class ecgroup_order
17 : {
18 : public:
19 4766 : static const EC_GROUP* get()
20 : {
21 4862 : static const ecgroup_order wrapper;
22 4766 : return wrapper.pgroup;
23 : }
24 :
25 : private:
26 : ecgroup_order()
27 96 : : pgroup(EC_GROUP_new_by_curve_name(NID_secp256k1))
28 : {
29 : }
30 :
31 96 : ~ecgroup_order()
32 : {
33 96 : EC_GROUP_free(pgroup);
34 96 : }
35 :
36 : EC_GROUP* pgroup;
37 : };
38 :
39 : /**
40 : * Perform ECDSA key recovery (see SEC1 4.1.6) for curves over (mod p)-fields
41 : * recid selects which key is recovered
42 : * if check is non-zero, additional checks are performed
43 : */
44 67 : int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check)
45 : {
46 67 : if (!eckey) return 0;
47 :
48 67 : int ret = 0;
49 67 : BN_CTX *ctx = NULL;
50 :
51 67 : BIGNUM *x = NULL;
52 67 : BIGNUM *e = NULL;
53 67 : BIGNUM *order = NULL;
54 67 : BIGNUM *sor = NULL;
55 67 : BIGNUM *eor = NULL;
56 67 : BIGNUM *field = NULL;
57 67 : EC_POINT *R = NULL;
58 67 : EC_POINT *O = NULL;
59 67 : EC_POINT *Q = NULL;
60 67 : BIGNUM *rr = NULL;
61 67 : BIGNUM *zero = NULL;
62 67 : int n = 0;
63 67 : int i = recid / 2;
64 :
65 67 : const EC_GROUP *group = EC_KEY_get0_group(eckey);
66 67 : if ((ctx = BN_CTX_new()) == NULL) { ret = -1; goto err; }
67 67 : BN_CTX_start(ctx);
68 67 : order = BN_CTX_get(ctx);
69 67 : if (!EC_GROUP_get_order(group, order, ctx)) { ret = -2; goto err; }
70 67 : x = BN_CTX_get(ctx);
71 67 : if (!BN_copy(x, order)) { ret=-1; goto err; }
72 67 : if (!BN_mul_word(x, i)) { ret=-1; goto err; }
73 67 : if (!BN_add(x, x, ecsig->r)) { ret=-1; goto err; }
74 67 : field = BN_CTX_get(ctx);
75 67 : if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; }
76 67 : if (BN_cmp(x, field) >= 0) { ret=0; goto err; }
77 67 : if ((R = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
78 67 : if (!EC_POINT_set_compressed_coordinates_GFp(group, R, x, recid % 2, ctx)) { ret=0; goto err; }
79 67 : if (check)
80 : {
81 0 : if ((O = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
82 0 : if (!EC_POINT_mul(group, O, NULL, R, order, ctx)) { ret=-2; goto err; }
83 0 : if (!EC_POINT_is_at_infinity(group, O)) { ret = 0; goto err; }
84 : }
85 67 : if ((Q = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
86 67 : n = EC_GROUP_get_degree(group);
87 67 : e = BN_CTX_get(ctx);
88 67 : if (!BN_bin2bn(msg, msglen, e)) { ret=-1; goto err; }
89 67 : if (8*msglen > n) BN_rshift(e, e, 8-(n & 7));
90 67 : zero = BN_CTX_get(ctx);
91 67 : if (!BN_zero(zero)) { ret=-1; goto err; }
92 67 : if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; }
93 67 : rr = BN_CTX_get(ctx);
94 67 : if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) { ret=-1; goto err; }
95 67 : sor = BN_CTX_get(ctx);
96 67 : if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) { ret=-1; goto err; }
97 67 : eor = BN_CTX_get(ctx);
98 67 : if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; }
99 67 : if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; }
100 67 : if (!EC_KEY_set_public_key(eckey, Q)) { ret=-2; goto err; }
101 :
102 67 : ret = 1;
103 :
104 : err:
105 67 : if (ctx) {
106 67 : BN_CTX_end(ctx);
107 67 : BN_CTX_free(ctx);
108 : }
109 67 : if (R != NULL) EC_POINT_free(R);
110 67 : if (O != NULL) EC_POINT_free(O);
111 67 : if (Q != NULL) EC_POINT_free(Q);
112 67 : return ret;
113 : }
114 :
115 : } // anon namespace
116 :
117 4671 : CECKey::CECKey() {
118 4671 : pkey = EC_KEY_new();
119 4671 : assert(pkey != NULL);
120 4671 : int result = EC_KEY_set_group(pkey, ecgroup_order::get());
121 4671 : assert(result);
122 4671 : }
123 :
124 4671 : CECKey::~CECKey() {
125 4671 : EC_KEY_free(pkey);
126 4671 : }
127 :
128 82 : void CECKey::GetPubKey(std::vector<unsigned char> &pubkey, bool fCompressed) {
129 82 : EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
130 82 : int nSize = i2o_ECPublicKey(pkey, NULL);
131 82 : assert(nSize);
132 82 : assert(nSize <= 65);
133 : pubkey.clear();
134 82 : pubkey.resize(nSize);
135 82 : unsigned char *pbegin(begin_ptr(pubkey));
136 82 : int nSize2 = i2o_ECPublicKey(pkey, &pbegin);
137 82 : assert(nSize == nSize2);
138 82 : }
139 :
140 4603 : bool CECKey::SetPubKey(const unsigned char* pubkey, size_t size) {
141 4603 : return o2i_ECPublicKey(&pkey, &pubkey, size) != NULL;
142 : }
143 :
144 4317 : bool CECKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
145 4317 : if (vchSig.empty())
146 : return false;
147 :
148 : // New versions of OpenSSL will reject non-canonical DER signatures. de/re-serialize first.
149 4309 : unsigned char *norm_der = NULL;
150 4309 : ECDSA_SIG *norm_sig = ECDSA_SIG_new();
151 4308 : const unsigned char* sigptr = &vchSig[0];
152 4308 : assert(norm_sig);
153 8616 : if (d2i_ECDSA_SIG(&norm_sig, &sigptr, vchSig.size()) == NULL)
154 : {
155 : /* As of OpenSSL 1.0.0p d2i_ECDSA_SIG frees and nulls the pointer on
156 : * error. But OpenSSL's own use of this function redundantly frees the
157 : * result. As ECDSA_SIG_free(NULL) is a no-op, and in the absence of a
158 : * clear contract for the function behaving the same way is more
159 : * conservative.
160 : */
161 8 : ECDSA_SIG_free(norm_sig);
162 : return false;
163 : }
164 4301 : int derlen = i2d_ECDSA_SIG(norm_sig, &norm_der);
165 4301 : ECDSA_SIG_free(norm_sig);
166 4301 : if (derlen <= 0)
167 : return false;
168 :
169 : // -1 = error, 0 = bad sig, 1 = good
170 4301 : bool ret = ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), norm_der, derlen, pkey) == 1;
171 4301 : OPENSSL_free(norm_der);
172 : return ret;
173 : }
174 :
175 67 : bool CECKey::Recover(const uint256 &hash, const unsigned char *p64, int rec)
176 : {
177 67 : if (rec<0 || rec>=3)
178 : return false;
179 67 : ECDSA_SIG *sig = ECDSA_SIG_new();
180 67 : BN_bin2bn(&p64[0], 32, sig->r);
181 67 : BN_bin2bn(&p64[32], 32, sig->s);
182 67 : bool ret = ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), rec, 0) == 1;
183 67 : ECDSA_SIG_free(sig);
184 67 : return ret;
185 : }
186 :
187 8 : bool CECKey::TweakPublic(const unsigned char vchTweak[32]) {
188 8 : bool ret = true;
189 8 : BN_CTX *ctx = BN_CTX_new();
190 8 : BN_CTX_start(ctx);
191 8 : BIGNUM *bnTweak = BN_CTX_get(ctx);
192 8 : BIGNUM *bnOrder = BN_CTX_get(ctx);
193 8 : BIGNUM *bnOne = BN_CTX_get(ctx);
194 8 : const EC_GROUP *group = EC_KEY_get0_group(pkey);
195 8 : EC_GROUP_get_order(group, bnOrder, ctx); // what a grossly inefficient way to get the (constant) group order...
196 8 : BN_bin2bn(vchTweak, 32, bnTweak);
197 8 : if (BN_cmp(bnTweak, bnOrder) >= 0)
198 0 : ret = false; // extremely unlikely
199 8 : EC_POINT *point = EC_POINT_dup(EC_KEY_get0_public_key(pkey), group);
200 8 : BN_one(bnOne);
201 8 : EC_POINT_mul(group, point, bnTweak, point, bnOne, ctx);
202 8 : if (EC_POINT_is_at_infinity(group, point))
203 0 : ret = false; // ridiculously unlikely
204 8 : EC_KEY_set_public_key(pkey, point);
205 8 : EC_POINT_free(point);
206 8 : BN_CTX_end(ctx);
207 8 : BN_CTX_free(ctx);
208 8 : return ret;
209 : }
210 :
211 95 : bool CECKey::SanityCheck()
212 : {
213 95 : const EC_GROUP *pgroup = ecgroup_order::get();
214 95 : if(pgroup == NULL)
215 : return false;
216 : // TODO Is there more EC functionality that could be missing?
217 95 : return true;
218 : }
|