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.

key.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef BITCOIN_KEY_H
  5. #define BITCOIN_KEY_H
  6. #include <openssl/ec.h>
  7. #include <openssl/ecdsa.h>
  8. #include <openssl/obj_mac.h>
  9. // secp160k1
  10. // const unsigned int PRIVATE_KEY_SIZE = 192;
  11. // const unsigned int PUBLIC_KEY_SIZE = 41;
  12. // const unsigned int SIGNATURE_SIZE = 48;
  13. //
  14. // secp192k1
  15. // const unsigned int PRIVATE_KEY_SIZE = 222;
  16. // const unsigned int PUBLIC_KEY_SIZE = 49;
  17. // const unsigned int SIGNATURE_SIZE = 57;
  18. //
  19. // secp224k1
  20. // const unsigned int PRIVATE_KEY_SIZE = 250;
  21. // const unsigned int PUBLIC_KEY_SIZE = 57;
  22. // const unsigned int SIGNATURE_SIZE = 66;
  23. //
  24. // secp256k1:
  25. // const unsigned int PRIVATE_KEY_SIZE = 279;
  26. // const unsigned int PUBLIC_KEY_SIZE = 65;
  27. // const unsigned int SIGNATURE_SIZE = 72;
  28. //
  29. // see www.keylength.com
  30. // script supports up to 75 for single byte push
  31. class key_error : public std::runtime_error
  32. {
  33. public:
  34. explicit key_error(const std::string& str) : std::runtime_error(str) {}
  35. };
  36. // secure_allocator is defined in serialize.h
  37. typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
  38. class CKey
  39. {
  40. protected:
  41. EC_KEY* pkey;
  42. bool fSet;
  43. public:
  44. CKey()
  45. {
  46. pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
  47. if (pkey == NULL)
  48. throw key_error("CKey::CKey() : EC_KEY_new_by_curve_name failed");
  49. fSet = false;
  50. }
  51. CKey(const CKey& b)
  52. {
  53. pkey = EC_KEY_dup(b.pkey);
  54. if (pkey == NULL)
  55. throw key_error("CKey::CKey(const CKey&) : EC_KEY_dup failed");
  56. fSet = b.fSet;
  57. }
  58. CKey& operator=(const CKey& b)
  59. {
  60. if (!EC_KEY_copy(pkey, b.pkey))
  61. throw key_error("CKey::operator=(const CKey&) : EC_KEY_copy failed");
  62. fSet = b.fSet;
  63. return (*this);
  64. }
  65. ~CKey()
  66. {
  67. EC_KEY_free(pkey);
  68. }
  69. bool IsNull() const
  70. {
  71. return !fSet;
  72. }
  73. void MakeNewKey()
  74. {
  75. if (!EC_KEY_generate_key(pkey))
  76. throw key_error("CKey::MakeNewKey() : EC_KEY_generate_key failed");
  77. fSet = true;
  78. }
  79. bool SetPrivKey(const CPrivKey& vchPrivKey)
  80. {
  81. const unsigned char* pbegin = &vchPrivKey[0];
  82. if (!d2i_ECPrivateKey(&pkey, &pbegin, vchPrivKey.size()))
  83. return false;
  84. fSet = true;
  85. return true;
  86. }
  87. CPrivKey GetPrivKey() const
  88. {
  89. unsigned int nSize = i2d_ECPrivateKey(pkey, NULL);
  90. if (!nSize)
  91. throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey failed");
  92. CPrivKey vchPrivKey(nSize, 0);
  93. unsigned char* pbegin = &vchPrivKey[0];
  94. if (i2d_ECPrivateKey(pkey, &pbegin) != nSize)
  95. throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey returned unexpected size");
  96. return vchPrivKey;
  97. }
  98. bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
  99. {
  100. const unsigned char* pbegin = &vchPubKey[0];
  101. if (!o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.size()))
  102. return false;
  103. fSet = true;
  104. return true;
  105. }
  106. std::vector<unsigned char> GetPubKey() const
  107. {
  108. unsigned int nSize = i2o_ECPublicKey(pkey, NULL);
  109. if (!nSize)
  110. throw key_error("CKey::GetPubKey() : i2o_ECPublicKey failed");
  111. std::vector<unsigned char> vchPubKey(nSize, 0);
  112. unsigned char* pbegin = &vchPubKey[0];
  113. if (i2o_ECPublicKey(pkey, &pbegin) != nSize)
  114. throw key_error("CKey::GetPubKey() : i2o_ECPublicKey returned unexpected size");
  115. return vchPubKey;
  116. }
  117. bool Sign(uint256 hash, std::vector<unsigned char>& vchSig)
  118. {
  119. vchSig.clear();
  120. unsigned char pchSig[10000];
  121. unsigned int nSize = 0;
  122. if (!ECDSA_sign(0, (unsigned char*)&hash, sizeof(hash), pchSig, &nSize, pkey))
  123. return false;
  124. vchSig.resize(nSize);
  125. memcpy(&vchSig[0], pchSig, nSize);
  126. return true;
  127. }
  128. bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig)
  129. {
  130. // -1 = error, 0 = bad sig, 1 = good
  131. if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1)
  132. return false;
  133. return true;
  134. }
  135. static bool Sign(const CPrivKey& vchPrivKey, uint256 hash, std::vector<unsigned char>& vchSig)
  136. {
  137. CKey key;
  138. if (!key.SetPrivKey(vchPrivKey))
  139. return false;
  140. return key.Sign(hash, vchSig);
  141. }
  142. static bool Verify(const std::vector<unsigned char>& vchPubKey, uint256 hash, const std::vector<unsigned char>& vchSig)
  143. {
  144. CKey key;
  145. if (!key.SetPubKey(vchPubKey))
  146. return false;
  147. return key.Verify(hash, vchSig);
  148. }
  149. };
  150. #endif