1 // Copyright (c) 2009-2010 Satoshi Nakamoto
\r
2 // Distributed under the MIT/X11 software license, see the accompanying
\r
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
\r
7 // const unsigned int PRIVATE_KEY_SIZE = 192;
\r
8 // const unsigned int PUBLIC_KEY_SIZE = 41;
\r
9 // const unsigned int SIGNATURE_SIZE = 48;
\r
12 // const unsigned int PRIVATE_KEY_SIZE = 222;
\r
13 // const unsigned int PUBLIC_KEY_SIZE = 49;
\r
14 // const unsigned int SIGNATURE_SIZE = 57;
\r
17 // const unsigned int PRIVATE_KEY_SIZE = 250;
\r
18 // const unsigned int PUBLIC_KEY_SIZE = 57;
\r
19 // const unsigned int SIGNATURE_SIZE = 66;
\r
22 // const unsigned int PRIVATE_KEY_SIZE = 279;
\r
23 // const unsigned int PUBLIC_KEY_SIZE = 65;
\r
24 // const unsigned int SIGNATURE_SIZE = 72;
\r
26 // see www.keylength.com
\r
27 // script supports up to 75 for single byte push
\r
31 class key_error : public std::runtime_error
\r
34 explicit key_error(const std::string& str) : std::runtime_error(str) {}
\r
38 // secure_allocator is defined in serialize.h
\r
39 typedef vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
\r
52 pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
\r
54 throw key_error("CKey::CKey() : EC_KEY_new_by_curve_name failed");
\r
60 pkey = EC_KEY_dup(b.pkey);
\r
62 throw key_error("CKey::CKey(const CKey&) : EC_KEY_dup failed");
\r
66 CKey& operator=(const CKey& b)
\r
68 if (!EC_KEY_copy(pkey, b.pkey))
\r
69 throw key_error("CKey::operator=(const CKey&) : EC_KEY_copy failed");
\r
86 if (!EC_KEY_generate_key(pkey))
\r
87 throw key_error("CKey::MakeNewKey() : EC_KEY_generate_key failed");
\r
91 bool SetPrivKey(const CPrivKey& vchPrivKey)
\r
93 const unsigned char* pbegin = &vchPrivKey[0];
\r
94 if (!d2i_ECPrivateKey(&pkey, &pbegin, vchPrivKey.size()))
\r
100 CPrivKey GetPrivKey() const
\r
102 unsigned int nSize = i2d_ECPrivateKey(pkey, NULL);
\r
104 throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey failed");
\r
105 CPrivKey vchPrivKey(nSize, 0);
\r
106 unsigned char* pbegin = &vchPrivKey[0];
\r
107 if (i2d_ECPrivateKey(pkey, &pbegin) != nSize)
\r
108 throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey returned unexpected size");
\r
112 bool SetPubKey(const vector<unsigned char>& vchPubKey)
\r
114 const unsigned char* pbegin = &vchPubKey[0];
\r
115 if (!o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.size()))
\r
121 vector<unsigned char> GetPubKey() const
\r
123 unsigned int nSize = i2o_ECPublicKey(pkey, NULL);
\r
125 throw key_error("CKey::GetPubKey() : i2o_ECPublicKey failed");
\r
126 vector<unsigned char> vchPubKey(nSize, 0);
\r
127 unsigned char* pbegin = &vchPubKey[0];
\r
128 if (i2o_ECPublicKey(pkey, &pbegin) != nSize)
\r
129 throw key_error("CKey::GetPubKey() : i2o_ECPublicKey returned unexpected size");
\r
133 bool Sign(uint256 hash, vector<unsigned char>& vchSig)
\r
136 unsigned char pchSig[10000];
\r
137 unsigned int nSize = 0;
\r
138 if (!ECDSA_sign(0, (unsigned char*)&hash, sizeof(hash), pchSig, &nSize, pkey))
\r
140 vchSig.resize(nSize);
\r
141 memcpy(&vchSig[0], pchSig, nSize);
\r
145 bool Verify(uint256 hash, const vector<unsigned char>& vchSig)
\r
147 // -1 = error, 0 = bad sig, 1 = good
\r
148 if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1)
\r
153 static bool Sign(const CPrivKey& vchPrivKey, uint256 hash, vector<unsigned char>& vchSig)
\r
156 if (!key.SetPrivKey(vchPrivKey))
\r
158 return key.Sign(hash, vchSig);
\r
161 static bool Verify(const vector<unsigned char>& vchPubKey, uint256 hash, const vector<unsigned char>& vchSig)
\r
164 if (!key.SetPubKey(vchPubKey))
\r
166 return key.Verify(hash, vchSig);
\r