Full checking of all loaded keys
[novacoin.git] / src / key.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_KEY_H
6 #define BITCOIN_KEY_H
7
8 #include <stdexcept>
9 #include <vector>
10
11 #include <openssl/ec.h>
12 #include <openssl/ecdsa.h>
13 #include <openssl/obj_mac.h>
14
15 #include "serialize.h"
16 #include "uint256.h"
17
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
41 int extern EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key);
42 int extern ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check);
43
44 class key_error : public std::runtime_error
45 {
46 public:
47     explicit key_error(const std::string& str) : std::runtime_error(str) {}
48 };
49
50
51 // secure_allocator is defined in serialize.h
52 // CPrivKey is a serialized private key, with all parameters included (279 bytes)
53 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
54 // CSecret is a serialization of just the secret parameter (32 bytes)
55 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CSecret;
56
57 class CKey
58 {
59 protected:
60     EC_KEY* pkey;
61     bool fSet;
62     bool fCompressedPubKey;
63
64     void SetCompressedPubKey()
65     {
66         EC_KEY_set_conv_form(pkey, POINT_CONVERSION_COMPRESSED);
67         fCompressedPubKey = true;
68     }
69
70 public:
71
72     void Reset()
73     {
74         fCompressedPubKey = false;
75         pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
76         if (pkey == NULL)
77             throw key_error("CKey::CKey() : EC_KEY_new_by_curve_name failed");
78         fSet = false;
79     }
80
81     CKey()
82     {
83         Reset();
84     }
85
86     CKey(const CKey& b)
87     {
88         pkey = EC_KEY_dup(b.pkey);
89         if (pkey == NULL)
90             throw key_error("CKey::CKey(const CKey&) : EC_KEY_dup failed");
91         fSet = b.fSet;
92     }
93
94     CKey& operator=(const CKey& b)
95     {
96         if (!EC_KEY_copy(pkey, b.pkey))
97             throw key_error("CKey::operator=(const CKey&) : EC_KEY_copy failed");
98         fSet = b.fSet;
99         return (*this);
100     }
101
102     ~CKey()
103     {
104         EC_KEY_free(pkey);
105     }
106
107     bool IsNull() const
108     {
109         return !fSet;
110     }
111
112     bool IsCompressed() const
113     {
114         return fCompressedPubKey;
115     }
116
117     void MakeNewKey(bool fCompressed = true)
118     {
119         if (!EC_KEY_generate_key(pkey))
120             throw key_error("CKey::MakeNewKey() : EC_KEY_generate_key failed");
121         if (fCompressed)
122             SetCompressedPubKey();
123         fSet = true;
124     }
125
126     bool SetPrivKey(const CPrivKey& vchPrivKey)
127     {
128         const unsigned char* pbegin = &vchPrivKey[0];
129         if (!d2i_ECPrivateKey(&pkey, &pbegin, vchPrivKey.size()))
130             return false;
131         fSet = true;
132         return true;
133     }
134
135     bool SetSecret(const CSecret& vchSecret, bool fCompressed = false)
136     {
137         EC_KEY_free(pkey);
138         pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
139         if (pkey == NULL)
140             throw key_error("CKey::SetSecret() : EC_KEY_new_by_curve_name failed");
141         if (vchSecret.size() != 32)
142             throw key_error("CKey::SetSecret() : secret must be 32 bytes");
143         BIGNUM *bn = BN_bin2bn(&vchSecret[0],32,BN_new());
144         if (bn == NULL) 
145             throw key_error("CKey::SetSecret() : BN_bin2bn failed");
146         if (!EC_KEY_regenerate_key(pkey,bn))
147             throw key_error("CKey::SetSecret() : EC_KEY_regenerate_key failed");
148         BN_clear_free(bn);
149         fSet = true;
150         if (fCompressed || fCompressedPubKey)
151             SetCompressedPubKey();
152         return true;
153     }
154
155     CSecret GetSecret(bool &fCompressed) const
156     {
157         CSecret vchRet;
158         vchRet.resize(32);
159         const BIGNUM *bn = EC_KEY_get0_private_key(pkey);
160         int nBytes = BN_num_bytes(bn);
161         if (bn == NULL)
162             throw key_error("CKey::GetSecret() : EC_KEY_get0_private_key failed");
163         int n=BN_bn2bin(bn,&vchRet[32 - nBytes]);
164         if (n != nBytes) 
165             throw key_error("CKey::GetSecret(): BN_bn2bin failed");
166         fCompressed = fCompressedPubKey;
167         return vchRet;
168     }
169
170     CPrivKey GetPrivKey() const
171     {
172         unsigned int nSize = i2d_ECPrivateKey(pkey, NULL);
173         if (!nSize)
174             throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey failed");
175         CPrivKey vchPrivKey(nSize, 0);
176         unsigned char* pbegin = &vchPrivKey[0];
177         if (i2d_ECPrivateKey(pkey, &pbegin) != nSize)
178             throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey returned unexpected size");
179         return vchPrivKey;
180     }
181
182     bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
183     {
184         const unsigned char* pbegin = &vchPubKey[0];
185         if (!o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.size()))
186             return false;
187         fSet = true;
188         if (vchPubKey.size() == 33)
189             SetCompressedPubKey();
190         return true;
191     }
192
193     std::vector<unsigned char> GetPubKey() const
194     {
195         unsigned int nSize = i2o_ECPublicKey(pkey, NULL);
196         if (!nSize)
197             throw key_error("CKey::GetPubKey() : i2o_ECPublicKey failed");
198         std::vector<unsigned char> vchPubKey(nSize, 0);
199         unsigned char* pbegin = &vchPubKey[0];
200         if (i2o_ECPublicKey(pkey, &pbegin) != nSize)
201             throw key_error("CKey::GetPubKey() : i2o_ECPublicKey returned unexpected size");
202         return vchPubKey;
203     }
204
205     bool Sign(uint256 hash, std::vector<unsigned char>& vchSig)
206     {
207         unsigned int nSize = ECDSA_size(pkey);
208         vchSig.resize(nSize); // Make sure it is big enough
209         if (!ECDSA_sign(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], &nSize, pkey))
210         {
211             vchSig.clear();
212             return false;
213         }
214         vchSig.resize(nSize); // Shrink to fit actual size
215         return true;
216     }
217
218     // create a compact signature (65 bytes), which allows reconstructing the used public key
219     // The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
220     // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
221     //                  0x1D = second key with even y, 0x1E = second key with odd y
222     bool SignCompact(uint256 hash, std::vector<unsigned char>& vchSig)
223     {
224         bool fOk = false;
225         ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
226         if (sig==NULL)
227             return false;
228         vchSig.clear();
229         vchSig.resize(65,0);
230         int nBitsR = BN_num_bits(sig->r);
231         int nBitsS = BN_num_bits(sig->s);
232         if (nBitsR <= 256 && nBitsS <= 256)
233         {
234             int nRecId = -1;
235             for (int i=0; i<4; i++)
236             {
237                 CKey keyRec;
238                 keyRec.fSet = true;
239                 if (fCompressedPubKey)
240                     keyRec.SetCompressedPubKey();
241                 if (ECDSA_SIG_recover_key_GFp(keyRec.pkey, sig, (unsigned char*)&hash, sizeof(hash), i, 1) == 1)
242                     if (keyRec.GetPubKey() == this->GetPubKey())
243                     {
244                         nRecId = i;
245                         break;
246                     }
247             }
248
249             if (nRecId == -1)
250                 throw key_error("CKey::SignCompact() : unable to construct recoverable key");
251
252             vchSig[0] = nRecId+27+(fCompressedPubKey ? 4 : 0);
253             BN_bn2bin(sig->r,&vchSig[33-(nBitsR+7)/8]);
254             BN_bn2bin(sig->s,&vchSig[65-(nBitsS+7)/8]);
255             fOk = true;
256         }
257         ECDSA_SIG_free(sig);
258         return fOk;
259     }
260
261     // reconstruct public key from a compact signature
262     // This is only slightly more CPU intensive than just verifying it.
263     // If this function succeeds, the recovered public key is guaranteed to be valid
264     // (the signature is a valid signature of the given data for that key)
265     bool SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig)
266     {
267         if (vchSig.size() != 65)
268             return false;
269         int nV = vchSig[0];
270         if (nV<27 || nV>=35)
271             return false;
272         ECDSA_SIG *sig = ECDSA_SIG_new();
273         BN_bin2bn(&vchSig[1],32,sig->r);
274         BN_bin2bn(&vchSig[33],32,sig->s);
275
276         EC_KEY_free(pkey);
277         pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
278         if (nV >= 31)
279         {
280             SetCompressedPubKey();
281             nV -= 4;
282         }
283         if (ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), nV - 27, 0) == 1)
284         {
285             fSet = true;
286             ECDSA_SIG_free(sig);
287             return true;
288         }
289         return false;
290     }
291
292     bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig)
293     {
294         // -1 = error, 0 = bad sig, 1 = good
295         if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1)
296             return false;
297         return true;
298     }
299
300     // Verify a compact signature
301     bool VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig)
302     {
303         CKey key;
304         if (!key.SetCompactSignature(hash, vchSig))
305             return false;
306         if (GetPubKey() != key.GetPubKey())
307             return false;
308         return true;
309     }
310
311     bool IsValid()
312     {
313         if (!fSet)
314             return false;
315
316         bool fCompr;
317         CSecret secret = GetSecret(fCompr);
318         CKey key2;
319         key2.SetSecret(secret, fCompr);
320         return GetPubKey() == key2.GetPubKey();
321     }
322 };
323
324 #endif