PRCYCoin  2.0.0.7rc1
P2P Digital Currency
ecwrapper.cpp
Go to the documentation of this file.
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 {
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 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
43  const BIGNUM *sig_r, *sig_s;
44  ECDSA_SIG_get0(ecsig, &sig_r, &sig_s);
45 #endif
46 
47  const EC_GROUP* group = EC_KEY_get0_group(eckey);
48  if ((ctx = BN_CTX_new()) == NULL) {
49  ret = -1;
50  goto err;
51  }
52  BN_CTX_start(ctx);
53  order = BN_CTX_get(ctx);
54  if (!EC_GROUP_get_order(group, order, ctx)) {
55  ret = -2;
56  goto err;
57  }
58  x = BN_CTX_get(ctx);
59  if (!BN_copy(x, order)) {
60  ret = -1;
61  goto err;
62  }
63  if (!BN_mul_word(x, i)) {
64  ret = -1;
65  goto err;
66  }
67 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
68  if (!BN_add(x, x, sig_r)) {
69 #else
70  if (!BN_add(x, x, ecsig->r)) {
71 #endif
72  ret = -1;
73  goto err;
74  }
75  field = BN_CTX_get(ctx);
76  if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) {
77  ret = -2;
78  goto err;
79  }
80  if (BN_cmp(x, field) >= 0) {
81  ret = 0;
82  goto err;
83  }
84  if ((R = EC_POINT_new(group)) == NULL) {
85  ret = -2;
86  goto err;
87  }
88  if (!EC_POINT_set_compressed_coordinates_GFp(group, R, x, recid % 2, ctx)) {
89  ret = 0;
90  goto err;
91  }
92  if (check) {
93  if ((O = EC_POINT_new(group)) == NULL) {
94  ret = -2;
95  goto err;
96  }
97  if (!EC_POINT_mul(group, O, NULL, R, order, ctx)) {
98  ret = -2;
99  goto err;
100  }
101  if (!EC_POINT_is_at_infinity(group, O)) {
102  ret = 0;
103  goto err;
104  }
105  }
106  if ((Q = EC_POINT_new(group)) == NULL) {
107  ret = -2;
108  goto err;
109  }
110  n = EC_GROUP_get_degree(group);
111  e = BN_CTX_get(ctx);
112  if (!BN_bin2bn(msg, msglen, e)) {
113  ret = -1;
114  goto err;
115  }
116  if (8 * msglen > n) BN_rshift(e, e, 8 - (n & 7));
117  zero = BN_CTX_get(ctx);
118  if (!BN_zero(zero)) {
119  ret = -1;
120  goto err;
121  }
122  if (!BN_mod_sub(e, zero, e, order, ctx)) {
123  ret = -1;
124  goto err;
125  }
126  rr = BN_CTX_get(ctx);
127 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
128  if (!BN_mod_inverse(rr, sig_r, order, ctx)) {
129 #else
130  if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) {
131 #endif
132  ret = -1;
133  goto err;
134  }
135  sor = BN_CTX_get(ctx);
136 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
137  if (!BN_mod_mul(sor, sig_s, rr, order, ctx)) {
138 #else
139  if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) {
140 #endif
141  ret = -1;
142  goto err;
143  }
144  eor = BN_CTX_get(ctx);
145  if (!BN_mod_mul(eor, e, rr, order, ctx)) {
146  ret = -1;
147  goto err;
148  }
149  if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) {
150  ret = -2;
151  goto err;
152  }
153  if (!EC_KEY_set_public_key(eckey, Q)) {
154  ret = -2;
155  goto err;
156  }
157 
158  ret = 1;
159 
160 err:
161  if (ctx) {
162  BN_CTX_end(ctx);
163  BN_CTX_free(ctx);
164  }
165  if (R != NULL) EC_POINT_free(R);
166  if (O != NULL) EC_POINT_free(O);
167  if (Q != NULL) EC_POINT_free(Q);
168  return ret;
169 }
170 
171 } // anon namespace
172 
174 {
175  pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
176  assert(pkey != NULL);
177 }
178 
180 {
181  EC_KEY_free(pkey);
182 }
183 
184 void CECKey::GetPubKey(std::vector<unsigned char>& pubkey, bool fCompressed)
185 {
186  EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
187  int nSize = i2o_ECPublicKey(pkey, NULL);
188  assert(nSize);
189  assert(nSize <= 65);
190  pubkey.clear();
191  pubkey.resize(nSize);
192  unsigned char* pbegin(pubkey.data());
193  int nSize2 = i2o_ECPublicKey(pkey, &pbegin);
194  assert(nSize == nSize2);
195 }
196 
197 bool CECKey::SetPubKey(const unsigned char* pubkey, size_t size)
198 {
199  return o2i_ECPublicKey(&pkey, &pubkey, size) != NULL;
200 }
201 
202 bool CECKey::Verify(const uint256& hash, const std::vector<unsigned char>& vchSig)
203 {
204  if (vchSig.empty())
205  return false;
206 
207  // New versions of OpenSSL will reject non-canonical DER signatures. de/re-serialize first.
208  unsigned char* norm_der = NULL;
209  ECDSA_SIG* norm_sig = ECDSA_SIG_new();
210  const unsigned char* sigptr = &vchSig[0];
211  assert(norm_sig);
212  if (d2i_ECDSA_SIG(&norm_sig, &sigptr, vchSig.size()) == NULL) {
213  /* As of OpenSSL 1.0.0p d2i_ECDSA_SIG frees and nulls the pointer on
214  * error. But OpenSSL's own use of this function redundantly frees the
215  * result. As ECDSA_SIG_free(NULL) is a no-op, and in the absence of a
216  * clear contract for the function behaving the same way is more
217  * conservative.
218  */
219  ECDSA_SIG_free(norm_sig);
220  return false;
221  }
222  int derlen = i2d_ECDSA_SIG(norm_sig, &norm_der);
223  ECDSA_SIG_free(norm_sig);
224  if (derlen <= 0)
225  return false;
226 
227  // -1 = error, 0 = bad sig, 1 = good
228  bool ret = ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), norm_der, derlen, pkey) == 1;
229  OPENSSL_free(norm_der);
230  return ret;
231 }
232 
233 bool CECKey::Recover(const uint256& hash, const unsigned char* p64, int rec)
234 {
235  if (rec < 0 || rec >= 3)
236  return false;
237  ECDSA_SIG* sig = ECDSA_SIG_new();
238 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
239  BIGNUM *sig_r = NULL;
240  BIGNUM *sig_s = NULL;
241  if (!(sig_r = BN_bin2bn(&p64[0], 32, nullptr)) ||
242  !(sig_s = BN_bin2bn(&p64[32], 32, nullptr)) ||
243  !ECDSA_SIG_set0(sig, sig_r, sig_s)) {
244  BN_free(sig_r);
245  BN_free(sig_s);
246  return false;
247  }
248 #else
249  BN_bin2bn(&p64[0], 32, sig->r);
250  BN_bin2bn(&p64[32], 32, sig->s);
251 #endif
252  bool ret = ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), rec, 0) == 1;
253  ECDSA_SIG_free(sig);
254  return ret;
255 }
256 
257 bool CECKey::TweakPublic(const unsigned char vchTweak[32])
258 {
259  bool ret = true;
260  BN_CTX* ctx = BN_CTX_new();
261  BN_CTX_start(ctx);
262  BIGNUM* bnTweak = BN_CTX_get(ctx);
263  BIGNUM* bnOrder = BN_CTX_get(ctx);
264  BIGNUM* bnOne = BN_CTX_get(ctx);
265  const EC_GROUP* group = EC_KEY_get0_group(pkey);
266  EC_GROUP_get_order(group, bnOrder, ctx); // what a grossly inefficient way to get the (constant) group order...
267  BN_bin2bn(vchTweak, 32, bnTweak);
268  if (BN_cmp(bnTweak, bnOrder) >= 0)
269  ret = false; // extremely unlikely
270  EC_POINT* point = EC_POINT_dup(EC_KEY_get0_public_key(pkey), group);
271  BN_one(bnOne);
272  EC_POINT_mul(group, point, bnTweak, point, bnOne, ctx);
273  if (EC_POINT_is_at_infinity(group, point))
274  ret = false; // ridiculously unlikely
275  EC_KEY_set_public_key(pkey, point);
276  EC_POINT_free(point);
277  BN_CTX_end(ctx);
278  BN_CTX_free(ctx);
279  return ret;
280 }
281 
283 {
284  EC_KEY* pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
285  if (pkey == NULL)
286  return false;
287  EC_KEY_free(pkey);
288 
289  // TODO Is there more EC functionality that could be missing?
290  return true;
291 }
CECKey::~CECKey
~CECKey()
Definition: ecwrapper.cpp:179
ecwrapper.h
CECKey::Recover
bool Recover(const uint256 &hash, const unsigned char *p64, int rec)
reconstruct public key from a compact signature This is only slightly more CPU intensive than just ve...
Definition: ecwrapper.cpp:233
CECKey::CECKey
CECKey()
Definition: ecwrapper.cpp:173
uint256.h
CECKey::Verify
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Definition: ecwrapper.cpp:202
CECKey::SetPubKey
bool SetPubKey(const unsigned char *pubkey, size_t size)
Definition: ecwrapper.cpp:197
CECKey::pkey
EC_KEY * pkey
Definition: ecwrapper.h:19
CECKey::GetPubKey
void GetPubKey(std::vector< unsigned char > &pubkey, bool fCompressed)
Definition: ecwrapper.cpp:184
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
R
#define R(a, b)
serialize.h
CECKey::SanityCheck
static bool SanityCheck()
Definition: ecwrapper.cpp:282
CECKey::TweakPublic
bool TweakPublic(const unsigned char vchTweak[32])
Definition: ecwrapper.cpp:257