PageRenderTime 23ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/key.cpp

https://gitlab.com/yenny.prathivi/bitcoin
C++ | 330 lines | 290 code | 30 blank | 10 comment | 44 complexity | d5dd61f1ebe9f54f42a409d7469a6d29 MD5 | raw file
  1. // Copyright (c) 2009-2015 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. #include "key.h"
  5. #include "arith_uint256.h"
  6. #include "crypto/common.h"
  7. #include "crypto/hmac_sha512.h"
  8. #include "pubkey.h"
  9. #include "random.h"
  10. #include <secp256k1.h>
  11. #include <secp256k1_recovery.h>
  12. static secp256k1_context* secp256k1_context_sign = NULL;
  13. /** These functions are taken from the libsecp256k1 distribution and are very ugly. */
  14. static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) {
  15. const unsigned char *end = privkey + privkeylen;
  16. int lenb = 0;
  17. int len = 0;
  18. memset(out32, 0, 32);
  19. /* sequence header */
  20. if (end < privkey+1 || *privkey != 0x30) {
  21. return 0;
  22. }
  23. privkey++;
  24. /* sequence length constructor */
  25. if (end < privkey+1 || !(*privkey & 0x80)) {
  26. return 0;
  27. }
  28. lenb = *privkey & ~0x80; privkey++;
  29. if (lenb < 1 || lenb > 2) {
  30. return 0;
  31. }
  32. if (end < privkey+lenb) {
  33. return 0;
  34. }
  35. /* sequence length */
  36. len = privkey[lenb-1] | (lenb > 1 ? privkey[lenb-2] << 8 : 0);
  37. privkey += lenb;
  38. if (end < privkey+len) {
  39. return 0;
  40. }
  41. /* sequence element 0: version number (=1) */
  42. if (end < privkey+3 || privkey[0] != 0x02 || privkey[1] != 0x01 || privkey[2] != 0x01) {
  43. return 0;
  44. }
  45. privkey += 3;
  46. /* sequence element 1: octet string, up to 32 bytes */
  47. if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
  48. return 0;
  49. }
  50. memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
  51. if (!secp256k1_ec_seckey_verify(ctx, out32)) {
  52. memset(out32, 0, 32);
  53. return 0;
  54. }
  55. return 1;
  56. }
  57. static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) {
  58. secp256k1_pubkey pubkey;
  59. size_t pubkeylen = 0;
  60. if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
  61. *privkeylen = 0;
  62. return 0;
  63. }
  64. if (compressed) {
  65. static const unsigned char begin[] = {
  66. 0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
  67. };
  68. static const unsigned char middle[] = {
  69. 0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
  70. 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  71. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  72. 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
  73. 0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
  74. 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
  75. 0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  76. 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
  77. 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
  78. };
  79. unsigned char *ptr = privkey;
  80. memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
  81. memcpy(ptr, key32, 32); ptr += 32;
  82. memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
  83. pubkeylen = 33;
  84. secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
  85. ptr += pubkeylen;
  86. *privkeylen = ptr - privkey;
  87. } else {
  88. static const unsigned char begin[] = {
  89. 0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
  90. };
  91. static const unsigned char middle[] = {
  92. 0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
  93. 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  94. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  95. 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
  96. 0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
  97. 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
  98. 0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,
  99. 0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,
  100. 0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  101. 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
  102. 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
  103. };
  104. unsigned char *ptr = privkey;
  105. memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
  106. memcpy(ptr, key32, 32); ptr += 32;
  107. memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
  108. pubkeylen = 65;
  109. secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
  110. ptr += pubkeylen;
  111. *privkeylen = ptr - privkey;
  112. }
  113. return 1;
  114. }
  115. bool CKey::Check(const unsigned char *vch) {
  116. return secp256k1_ec_seckey_verify(secp256k1_context_sign, vch);
  117. }
  118. void CKey::MakeNewKey(bool fCompressedIn) {
  119. RandAddSeedPerfmon();
  120. do {
  121. GetRandBytes(vch, sizeof(vch));
  122. } while (!Check(vch));
  123. fValid = true;
  124. fCompressed = fCompressedIn;
  125. }
  126. bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
  127. if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size()))
  128. return false;
  129. fCompressed = fCompressedIn;
  130. fValid = true;
  131. return true;
  132. }
  133. CPrivKey CKey::GetPrivKey() const {
  134. assert(fValid);
  135. CPrivKey privkey;
  136. int ret;
  137. size_t privkeylen;
  138. privkey.resize(279);
  139. privkeylen = 279;
  140. ret = ec_privkey_export_der(secp256k1_context_sign, (unsigned char*)&privkey[0], &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
  141. assert(ret);
  142. privkey.resize(privkeylen);
  143. return privkey;
  144. }
  145. CPubKey CKey::GetPubKey() const {
  146. assert(fValid);
  147. secp256k1_pubkey pubkey;
  148. size_t clen = 65;
  149. CPubKey result;
  150. int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
  151. assert(ret);
  152. secp256k1_ec_pubkey_serialize(secp256k1_context_sign, (unsigned char*)result.begin(), &clen, &pubkey, fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
  153. assert(result.size() == clen);
  154. assert(result.IsValid());
  155. return result;
  156. }
  157. bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
  158. if (!fValid)
  159. return false;
  160. vchSig.resize(72);
  161. size_t nSigLen = 72;
  162. unsigned char extra_entropy[32] = {0};
  163. WriteLE32(extra_entropy, test_case);
  164. secp256k1_ecdsa_signature sig;
  165. int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL);
  166. assert(ret);
  167. secp256k1_ecdsa_signature_serialize_der(secp256k1_context_sign, (unsigned char*)&vchSig[0], &nSigLen, &sig);
  168. vchSig.resize(nSigLen);
  169. return true;
  170. }
  171. bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
  172. if (pubkey.IsCompressed() != fCompressed) {
  173. return false;
  174. }
  175. unsigned char rnd[8];
  176. std::string str = "Bitcoin key verification\n";
  177. GetRandBytes(rnd, sizeof(rnd));
  178. uint256 hash;
  179. CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
  180. std::vector<unsigned char> vchSig;
  181. Sign(hash, vchSig);
  182. return pubkey.Verify(hash, vchSig);
  183. }
  184. bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
  185. if (!fValid)
  186. return false;
  187. vchSig.resize(65);
  188. int rec = -1;
  189. secp256k1_ecdsa_recoverable_signature sig;
  190. int ret = secp256k1_ecdsa_sign_recoverable(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, NULL);
  191. assert(ret);
  192. secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_context_sign, (unsigned char*)&vchSig[1], &rec, &sig);
  193. assert(ret);
  194. assert(rec != -1);
  195. vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
  196. return true;
  197. }
  198. bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
  199. if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size()))
  200. return false;
  201. fCompressed = vchPubKey.IsCompressed();
  202. fValid = true;
  203. if (fSkipCheck)
  204. return true;
  205. return VerifyPubKey(vchPubKey);
  206. }
  207. bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
  208. assert(IsValid());
  209. assert(IsCompressed());
  210. unsigned char out[64];
  211. LockObject(out);
  212. if ((nChild >> 31) == 0) {
  213. CPubKey pubkey = GetPubKey();
  214. assert(pubkey.begin() + 33 == pubkey.end());
  215. BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
  216. } else {
  217. assert(begin() + 32 == end());
  218. BIP32Hash(cc, nChild, 0, begin(), out);
  219. }
  220. memcpy(ccChild.begin(), out+32, 32);
  221. memcpy((unsigned char*)keyChild.begin(), begin(), 32);
  222. bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), out);
  223. UnlockObject(out);
  224. keyChild.fCompressed = true;
  225. keyChild.fValid = ret;
  226. return ret;
  227. }
  228. bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
  229. out.nDepth = nDepth + 1;
  230. CKeyID id = key.GetPubKey().GetID();
  231. memcpy(&out.vchFingerprint[0], &id, 4);
  232. out.nChild = nChild;
  233. return key.Derive(out.key, out.chaincode, nChild, chaincode);
  234. }
  235. void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
  236. static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
  237. unsigned char out[64];
  238. LockObject(out);
  239. CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
  240. key.Set(&out[0], &out[32], true);
  241. memcpy(chaincode.begin(), &out[32], 32);
  242. UnlockObject(out);
  243. nDepth = 0;
  244. nChild = 0;
  245. memset(vchFingerprint, 0, sizeof(vchFingerprint));
  246. }
  247. CExtPubKey CExtKey::Neuter() const {
  248. CExtPubKey ret;
  249. ret.nDepth = nDepth;
  250. memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
  251. ret.nChild = nChild;
  252. ret.pubkey = key.GetPubKey();
  253. ret.chaincode = chaincode;
  254. return ret;
  255. }
  256. void CExtKey::Encode(unsigned char code[74]) const {
  257. code[0] = nDepth;
  258. memcpy(code+1, vchFingerprint, 4);
  259. code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
  260. code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
  261. memcpy(code+9, chaincode.begin(), 32);
  262. code[41] = 0;
  263. assert(key.size() == 32);
  264. memcpy(code+42, key.begin(), 32);
  265. }
  266. void CExtKey::Decode(const unsigned char code[74]) {
  267. nDepth = code[0];
  268. memcpy(vchFingerprint, code+1, 4);
  269. nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
  270. memcpy(chaincode.begin(), code+9, 32);
  271. key.Set(code+42, code+74, true);
  272. }
  273. bool ECC_InitSanityCheck() {
  274. CKey key;
  275. key.MakeNewKey(true);
  276. CPubKey pubkey = key.GetPubKey();
  277. return key.VerifyPubKey(pubkey);
  278. }
  279. void ECC_Start() {
  280. assert(secp256k1_context_sign == NULL);
  281. secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
  282. assert(ctx != NULL);
  283. {
  284. // Pass in a random blinding seed to the secp256k1 context.
  285. unsigned char seed[32];
  286. LockObject(seed);
  287. GetRandBytes(seed, 32);
  288. bool ret = secp256k1_context_randomize(ctx, seed);
  289. assert(ret);
  290. UnlockObject(seed);
  291. }
  292. secp256k1_context_sign = ctx;
  293. }
  294. void ECC_Stop() {
  295. secp256k1_context *ctx = secp256k1_context_sign;
  296. secp256k1_context_sign = NULL;
  297. if (ctx) {
  298. secp256k1_context_destroy(ctx);
  299. }
  300. }