Malleable keys: remove version byte
[novacoin.git] / src / key.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING 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 "allocators.h"
12 #include "serialize.h"
13 #include "uint256.h"
14 #include "hash.h"
15 #include "bignum.h"
16 #include "ies.h"
17
18 #include <openssl/ec.h> // for EC_KEY definition
19
20 // secp160k1
21 // const unsigned int PRIVATE_KEY_SIZE = 192;
22 // const unsigned int PUBLIC_KEY_SIZE  = 41;
23 // const unsigned int SIGNATURE_SIZE   = 48;
24 //
25 // secp192k1
26 // const unsigned int PRIVATE_KEY_SIZE = 222;
27 // const unsigned int PUBLIC_KEY_SIZE  = 49;
28 // const unsigned int SIGNATURE_SIZE   = 57;
29 //
30 // secp224k1
31 // const unsigned int PRIVATE_KEY_SIZE = 250;
32 // const unsigned int PUBLIC_KEY_SIZE  = 57;
33 // const unsigned int SIGNATURE_SIZE   = 66;
34 //
35 // secp256k1:
36 // const unsigned int PRIVATE_KEY_SIZE = 279;
37 // const unsigned int PUBLIC_KEY_SIZE  = 65;
38 // const unsigned int SIGNATURE_SIZE   = 72;
39 //
40 // see www.keylength.com
41 // script supports up to 75 for single byte push
42
43 class key_error : public std::runtime_error
44 {
45 public:
46     explicit key_error(const std::string& str) : std::runtime_error(str) {}
47 };
48
49 /** A reference to a CKey: the Hash160 of its serialized public key */
50 class CKeyID : public uint160
51 {
52 public:
53     CKeyID() : uint160(0) { }
54     CKeyID(const uint160 &in) : uint160(in) { }
55 };
56
57 /** A reference to a CScript: the Hash160 of its serialization (see script.h) */
58 class CScriptID : public uint160
59 {
60 public:
61     CScriptID() : uint160(0) { }
62     CScriptID(const uint160 &in) : uint160(in) { }
63 };
64
65 /** An encapsulated public key. */
66 class CPubKey {
67 private:
68     std::vector<unsigned char> vchPubKey;
69     friend class CKey;
70
71 public:
72     CPubKey() { }
73     CPubKey(const std::vector<unsigned char> &vchPubKeyIn) : vchPubKey(vchPubKeyIn) { }
74     friend bool operator==(const CPubKey &a, const CPubKey &b) { return a.vchPubKey == b.vchPubKey; }
75     friend bool operator!=(const CPubKey &a, const CPubKey &b) { return a.vchPubKey != b.vchPubKey; }
76     friend bool operator<(const CPubKey &a, const CPubKey &b) { return a.vchPubKey < b.vchPubKey; }
77
78     IMPLEMENT_SERIALIZE(
79         READWRITE(vchPubKey);
80     )
81
82     CKeyID GetID() const {
83         return CKeyID(Hash160(vchPubKey));
84     }
85
86     uint256 GetHash() const {
87         return Hash(vchPubKey.begin(), vchPubKey.end());
88     }
89
90     bool IsValid() const {
91         return vchPubKey.size() == 33 || vchPubKey.size() == 65;
92     }
93
94     bool IsCompressed() const {
95         return vchPubKey.size() == 33;
96     }
97
98     std::vector<unsigned char> Raw() const {
99         return vchPubKey;
100     }
101
102     // Encrypt data
103     void EncryptData(const std::vector<unsigned char>& data, std::vector<unsigned char>& encrypted);
104 };
105
106
107 // secure_allocator is defined in allocators.h
108 // CPrivKey is a serialized private key, with all parameters included (279 bytes)
109 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
110 // CSecret is a serialization of just the secret parameter (32 bytes)
111 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CSecret;
112
113 /** An encapsulated OpenSSL Elliptic Curve key (public and/or private) */
114 class CKey
115 {
116 protected:
117     EC_KEY* pkey;
118     bool fSet;
119     bool fCompressedPubKey;
120
121     void SetCompressedPubKey();
122
123 public:
124
125     void Reset();
126
127     CKey();
128     CKey(const CKey& b);
129     CKey(const CSecret& b, bool fCompressed=true);
130
131     CKey& operator=(const CKey& b);
132
133     ~CKey();
134
135     bool IsNull() const;
136     bool IsCompressed() const;
137
138     void MakeNewKey(bool fCompressed=true);
139     bool SetPrivKey(const CPrivKey& vchPrivKey);
140     bool SetSecret(const CSecret& vchSecret, bool fCompressed = true);
141     CSecret GetSecret(bool &fCompressed) const;
142     CSecret GetSecret() const;
143     CPrivKey GetPrivKey() const;
144     bool SetPubKey(const CPubKey& vchPubKey);
145     CPubKey GetPubKey() const;
146
147     bool Sign(uint256 hash, std::vector<unsigned char>& vchSig);
148
149     // create a compact signature (65 bytes), which allows reconstructing the used public key
150     // The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
151     // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
152     //                  0x1D = second key with even y, 0x1E = second key with odd y
153     bool SignCompact(uint256 hash, std::vector<unsigned char>& vchSig);
154
155     // reconstruct public key from a compact signature
156     // This is only slightly more CPU intensive than just verifying it.
157     // If this function succeeds, the recovered public key is guaranteed to be valid
158     // (the signature is a valid signature of the given data for that key)
159     bool SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig);
160
161     bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig);
162
163     // Verify a compact signature
164     bool VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig);
165
166     bool IsValid();
167
168     // Check whether an element of a signature (r or s) is valid.
169     static bool CheckSignatureElement(const unsigned char *vch, int len, bool half);
170
171     // Reserialize to DER
172     static bool ReserealizeSignature(std::vector<unsigned char>& vchSig);
173
174     // Encrypt data
175     void EncryptData(const std::vector<unsigned char>& data, std::vector<unsigned char>& encrypted);
176
177     // Decrypt data
178     void DecryptData(const std::vector<unsigned char>& encrypted, std::vector<unsigned char>& data);
179 };
180
181 class CPoint
182 {
183 private:
184     EC_POINT *point;
185     EC_GROUP* group;
186     BN_CTX* ctx;
187
188 public:
189     CPoint();
190     bool operator!=(const CPoint &a);
191     ~CPoint();
192
193     // Initialize from octets stream
194     bool setBytes(const std::vector<unsigned char> &vchBytes);
195
196     // Initialize from pubkey
197     bool setPubKey(const CPubKey &vchPubKey);
198
199     // Serialize to octets stream
200     bool getBytes(std::vector<unsigned char> &vchBytes);
201
202     // ECC multiplication by specified multiplier
203     bool ECMUL(const CBigNum &bnMultiplier);
204
205     // Calculate G*m + q
206     bool ECMULGEN(const CBigNum &bnMultiplier, const CPoint &qPoint);
207
208     bool IsInfinity() { return EC_POINT_is_at_infinity(group, point) != 0; }
209 };
210
211 class CMalleablePubKey
212 {
213 private:
214     CPubKey pubKeyL;
215     CPubKey pubKeyH;
216     friend class CMalleableKey;
217
218     static const unsigned char CURRENT_VERSION = 1;
219
220 public:
221     CMalleablePubKey() { }
222     CMalleablePubKey(const CMalleablePubKey& mpk)
223     {
224         pubKeyL = mpk.pubKeyL;
225         pubKeyH = mpk.pubKeyH;
226     }
227     CMalleablePubKey(const std::string& strMalleablePubKey) { SetString(strMalleablePubKey); }
228     CMalleablePubKey(const CPubKey &pubKeyInL, const CPubKey &pubKeyInH) : pubKeyL(pubKeyInL), pubKeyH(pubKeyInH) { }
229
230     IMPLEMENT_SERIALIZE(
231         READWRITE(pubKeyL);
232         READWRITE(pubKeyH);
233     )
234
235     bool IsValid() const {
236         return pubKeyL.IsValid() && pubKeyH.IsValid();
237     }
238
239     bool operator==(const CMalleablePubKey &b);
240     bool operator!=(const CMalleablePubKey &b) { return !(*this == b); }
241     CMalleablePubKey& operator=(const CMalleablePubKey& mpk) {
242         pubKeyL = mpk.pubKeyL;
243         pubKeyH = mpk.pubKeyH;
244         return *this;
245     }
246
247     std::string ToString() const;
248     bool SetString(const std::string& strMalleablePubKey);
249
250     CKeyID GetID() const {
251         return pubKeyL.GetID();
252     }
253
254     std::vector<unsigned char> Raw() const;
255
256     CPubKey& GetL() { return pubKeyL; }
257     CPubKey& GetH() { return pubKeyH; }
258     void GetVariant(CPubKey &R, CPubKey &vchPubKeyVariant);
259 };
260
261 class CMalleableKey
262 {
263 private:
264     CSecret vchSecretL;
265     CSecret vchSecretH;
266
267     friend class CMalleableKeyView;
268
269 public:
270     CMalleableKey();
271     CMalleableKey(const CMalleableKey &b);
272     CMalleableKey(const CSecret &L, const CSecret &H);
273     ~CMalleableKey();
274
275     IMPLEMENT_SERIALIZE(
276         READWRITE(vchSecretL);
277         READWRITE(vchSecretH);
278     )
279
280     std::string ToString() const;
281     bool SetString(const std::string& strMalleablePubKey);
282     std::vector<unsigned char> Raw() const;
283     CMalleableKey& operator=(const CMalleableKey& mk) {
284         vchSecretL = mk.vchSecretL;
285         vchSecretH = mk.vchSecretH;
286         return *this;
287     }
288
289     void Reset();
290     void MakeNewKeys();
291     bool IsNull() const;
292     bool IsValid() const { return !IsNull() && GetMalleablePubKey().IsValid(); }
293     bool SetSecrets(const CSecret &pvchSecretL, const CSecret &pvchSecretH);
294
295     CSecret GetSecretL() const { return vchSecretL; }
296     CSecret GetSecretH() const { return vchSecretH; }
297
298     CKeyID GetID() const {
299         return GetMalleablePubKey().GetID();
300     }
301     CMalleablePubKey GetMalleablePubKey() const;
302     bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const;
303     bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant, CKey &privKeyVariant) const;
304 };
305
306 class CMalleableKeyView
307 {
308 private:
309     CSecret vchSecretL;
310     CPubKey vchPubKeyH;
311
312 public:
313     CMalleableKeyView() { };
314     CMalleableKeyView(const CMalleableKey &b);
315     CMalleableKeyView(const std::string &strMalleableKey);
316
317     CMalleableKeyView(const CMalleableKeyView &b);
318     CMalleableKeyView& operator=(const CMalleableKey &b);
319     ~CMalleableKeyView();
320
321     IMPLEMENT_SERIALIZE(
322         READWRITE(vchSecretL);
323         READWRITE(vchPubKeyH);
324     )
325
326     bool IsValid() const;
327     std::string ToString() const;
328     bool SetString(const std::string& strMalleablePubKey);
329     std::vector<unsigned char> Raw() const;
330     CMalleableKeyView& operator=(const CMalleableKeyView& mkv) {
331         vchSecretL = mkv.vchSecretL;
332         vchPubKeyH = mkv.vchPubKeyH;
333         return *this;
334     }
335
336     CKeyID GetID() const {
337         return GetMalleablePubKey().GetID();
338     }
339     CMalleablePubKey GetMalleablePubKey() const;
340     CMalleableKey GetMalleableKey(const CSecret &vchSecretH) const { return CMalleableKey(vchSecretL, vchSecretH); }
341     bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const;
342
343     bool operator <(const CMalleableKeyView& kv) const { return vchPubKeyH.GetID() < kv.vchPubKeyH.GetID(); }
344 };
345
346 #endif