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