]> Git Repo - VerusCoin.git/blame - src/key.h
Use std::numeric_limits<> for typesafe INT_MAX/etc
[VerusCoin.git] / src / key.h
CommitLineData
8bd66202 1// Copyright (c) 2009-2010 Satoshi Nakamoto
b2120e22 2// Copyright (c) 2011 The Bitcoin developers
8bd66202
GA
3// Distributed under the MIT/X11 software license, see the accompanying
4// file license.txt or http://www.opensource.org/licenses/mit-license.php.
223b6f1b
WL
5#ifndef BITCOIN_KEY_H
6#define BITCOIN_KEY_H
8bd66202 7
fcedd45c
VN
8#include <stdexcept>
9#include <vector>
10
223b6f1b
WL
11#include <openssl/ec.h>
12#include <openssl/ecdsa.h>
13#include <openssl/obj_mac.h>
8bd66202 14
fcedd45c
VN
15#include "serialize.h"
16#include "uint256.h"
fcedd45c 17
8bd66202
GA
18// secp160k1
19// const unsigned int PRIVATE_KEY_SIZE = 192;
20// const unsigned int PUBLIC_KEY_SIZE = 41;
21// const unsigned int SIGNATURE_SIZE = 48;
22//
23// secp192k1
24// const unsigned int PRIVATE_KEY_SIZE = 222;
25// const unsigned int PUBLIC_KEY_SIZE = 49;
26// const unsigned int SIGNATURE_SIZE = 57;
27//
28// secp224k1
29// const unsigned int PRIVATE_KEY_SIZE = 250;
30// const unsigned int PUBLIC_KEY_SIZE = 57;
31// const unsigned int SIGNATURE_SIZE = 66;
32//
33// secp256k1:
34// const unsigned int PRIVATE_KEY_SIZE = 279;
35// const unsigned int PUBLIC_KEY_SIZE = 65;
36// const unsigned int SIGNATURE_SIZE = 72;
37//
38// see www.keylength.com
39// script supports up to 75 for single byte push
40
93db3fce
PW
41int extern EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key);
42int extern ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check);
8bd66202
GA
43
44class key_error : public std::runtime_error
45{
46public:
47 explicit key_error(const std::string& str) : std::runtime_error(str) {}
48};
49
50
51// secure_allocator is defined in serialize.h
d825e6a3 52// CPrivKey is a serialized private key, with all parameters included (279 bytes)
223b6f1b 53typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
d825e6a3 54// CSecret is a serialization of just the secret parameter (32 bytes)
acd65016 55typedef std::vector<unsigned char, secure_allocator<unsigned char> > CSecret;
8bd66202
GA
56
57class CKey
58{
59protected:
60 EC_KEY* pkey;
61 bool fSet;
62
63public:
64 CKey()
65 {
66 pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
67 if (pkey == NULL)
68 throw key_error("CKey::CKey() : EC_KEY_new_by_curve_name failed");
69 fSet = false;
70 }
71
72 CKey(const CKey& b)
73 {
74 pkey = EC_KEY_dup(b.pkey);
75 if (pkey == NULL)
76 throw key_error("CKey::CKey(const CKey&) : EC_KEY_dup failed");
77 fSet = b.fSet;
78 }
79
80 CKey& operator=(const CKey& b)
81 {
82 if (!EC_KEY_copy(pkey, b.pkey))
83 throw key_error("CKey::operator=(const CKey&) : EC_KEY_copy failed");
84 fSet = b.fSet;
85 return (*this);
86 }
87
88 ~CKey()
89 {
90 EC_KEY_free(pkey);
91 }
92
93 bool IsNull() const
94 {
95 return !fSet;
96 }
97
98 void MakeNewKey()
99 {
100 if (!EC_KEY_generate_key(pkey))
101 throw key_error("CKey::MakeNewKey() : EC_KEY_generate_key failed");
102 fSet = true;
103 }
104
105 bool SetPrivKey(const CPrivKey& vchPrivKey)
106 {
107 const unsigned char* pbegin = &vchPrivKey[0];
108 if (!d2i_ECPrivateKey(&pkey, &pbegin, vchPrivKey.size()))
109 return false;
110 fSet = true;
111 return true;
112 }
113
acd65016
PW
114 bool SetSecret(const CSecret& vchSecret)
115 {
116 EC_KEY_free(pkey);
117 pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
118 if (pkey == NULL)
119 throw key_error("CKey::SetSecret() : EC_KEY_new_by_curve_name failed");
120 if (vchSecret.size() != 32)
121 throw key_error("CKey::SetSecret() : secret must be 32 bytes");
122 BIGNUM *bn = BN_bin2bn(&vchSecret[0],32,BN_new());
123 if (bn == NULL)
124 throw key_error("CKey::SetSecret() : BN_bin2bn failed");
125 if (!EC_KEY_regenerate_key(pkey,bn))
126 throw key_error("CKey::SetSecret() : EC_KEY_regenerate_key failed");
127 BN_clear_free(bn);
128 fSet = true;
129 return true;
130 }
131
132 CSecret GetSecret() const
133 {
134 CSecret vchRet;
135 vchRet.resize(32);
136 const BIGNUM *bn = EC_KEY_get0_private_key(pkey);
137 int nBytes = BN_num_bytes(bn);
138 if (bn == NULL)
139 throw key_error("CKey::GetSecret() : EC_KEY_get0_private_key failed");
140 int n=BN_bn2bin(bn,&vchRet[32 - nBytes]);
141 if (n != nBytes)
142 throw key_error("CKey::GetSecret(): BN_bn2bin failed");
143 return vchRet;
144 }
145
8bd66202
GA
146 CPrivKey GetPrivKey() const
147 {
148 unsigned int nSize = i2d_ECPrivateKey(pkey, NULL);
149 if (!nSize)
150 throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey failed");
151 CPrivKey vchPrivKey(nSize, 0);
152 unsigned char* pbegin = &vchPrivKey[0];
153 if (i2d_ECPrivateKey(pkey, &pbegin) != nSize)
154 throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey returned unexpected size");
155 return vchPrivKey;
156 }
157
223b6f1b 158 bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
8bd66202
GA
159 {
160 const unsigned char* pbegin = &vchPubKey[0];
161 if (!o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.size()))
162 return false;
163 fSet = true;
164 return true;
165 }
166
223b6f1b 167 std::vector<unsigned char> GetPubKey() const
8bd66202
GA
168 {
169 unsigned int nSize = i2o_ECPublicKey(pkey, NULL);
170 if (!nSize)
171 throw key_error("CKey::GetPubKey() : i2o_ECPublicKey failed");
223b6f1b 172 std::vector<unsigned char> vchPubKey(nSize, 0);
8bd66202
GA
173 unsigned char* pbegin = &vchPubKey[0];
174 if (i2o_ECPublicKey(pkey, &pbegin) != nSize)
175 throw key_error("CKey::GetPubKey() : i2o_ECPublicKey returned unexpected size");
176 return vchPubKey;
177 }
178
223b6f1b 179 bool Sign(uint256 hash, std::vector<unsigned char>& vchSig)
8bd66202
GA
180 {
181 vchSig.clear();
182 unsigned char pchSig[10000];
183 unsigned int nSize = 0;
184 if (!ECDSA_sign(0, (unsigned char*)&hash, sizeof(hash), pchSig, &nSize, pkey))
185 return false;
186 vchSig.resize(nSize);
187 memcpy(&vchSig[0], pchSig, nSize);
188 return true;
189 }
190
01cc5263 191 // create a compact signature (65 bytes), which allows reconstructing the used public key
d825e6a3
PW
192 // The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
193 // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
194 // 0x1D = second key with even y, 0x1E = second key with odd y
01cc5263
PW
195 bool SignCompact(uint256 hash, std::vector<unsigned char>& vchSig)
196 {
197 bool fOk = false;
198 ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
199 if (sig==NULL)
200 return false;
201 vchSig.clear();
202 vchSig.resize(65,0);
203 int nBitsR = BN_num_bits(sig->r);
204 int nBitsS = BN_num_bits(sig->s);
205 if (nBitsR <= 256 && nBitsS <= 256)
206 {
207 int nRecId = -1;
208 for (int i=0; i<4; i++)
209 {
210 CKey keyRec;
211 keyRec.fSet = true;
212 if (ECDSA_SIG_recover_key_GFp(keyRec.pkey, sig, (unsigned char*)&hash, sizeof(hash), i, 1) == 1)
213 if (keyRec.GetPubKey() == this->GetPubKey())
214 {
215 nRecId = i;
216 break;
217 }
218 }
219
220 if (nRecId == -1)
d825e6a3 221 throw key_error("CKey::SignCompact() : unable to construct recoverable key");
01cc5263
PW
222
223 vchSig[0] = nRecId+27;
224 BN_bn2bin(sig->r,&vchSig[33-(nBitsR+7)/8]);
225 BN_bn2bin(sig->s,&vchSig[65-(nBitsS+7)/8]);
226 fOk = true;
227 }
228 ECDSA_SIG_free(sig);
229 return fOk;
230 }
231
232 // reconstruct public key from a compact signature
d825e6a3
PW
233 // This is only slightly more CPU intensive than just verifying it.
234 // If this function succeeds, the recovered public key is guaranteed to be valid
235 // (the signature is a valid signature of the given data for that key)
01cc5263
PW
236 bool SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig)
237 {
238 if (vchSig.size() != 65)
239 return false;
240 if (vchSig[0]<27 || vchSig[0]>=31)
241 return false;
242 ECDSA_SIG *sig = ECDSA_SIG_new();
243 BN_bin2bn(&vchSig[1],32,sig->r);
244 BN_bin2bn(&vchSig[33],32,sig->s);
245
246 EC_KEY_free(pkey);
247 pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
248 if (ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), vchSig[0] - 27, 0) == 1)
249 {
250 fSet = true;
251 ECDSA_SIG_free(sig);
252 return true;
253 }
254 return false;
255 }
256
223b6f1b 257 bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig)
8bd66202
GA
258 {
259 // -1 = error, 0 = bad sig, 1 = good
260 if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1)
261 return false;
262 return true;
263 }
2ffba736 264
d825e6a3 265 // Verify a compact signature
01cc5263
PW
266 bool VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig)
267 {
268 CKey key;
269 if (!key.SetCompactSignature(hash, vchSig))
270 return false;
271 if (GetPubKey() != key.GetPubKey())
272 return false;
273 return true;
274 }
8bd66202 275};
223b6f1b
WL
276
277#endif
This page took 0.069742 seconds and 4 git commands to generate.