You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ecwrapper.cpp 5.5KB

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