]> Git Repo - VerusCoin.git/blob - src/key.h
boost: moveonly: split CPubKey and friends to new files
[VerusCoin.git] / src / key.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #ifndef BITCOIN_KEY_H
7 #define BITCOIN_KEY_H
8
9 #include "allocators.h"
10 #include "serialize.h"
11 #include "uint256.h"
12
13 #include <stdexcept>
14 #include <vector>
15
16 class CPubKey;
17 class CExtPubKey;
18
19 /** 
20  * secp256k1:
21  * const unsigned int PRIVATE_KEY_SIZE = 279;
22  * const unsigned int PUBLIC_KEY_SIZE  = 65;
23  * const unsigned int SIGNATURE_SIZE   = 72;
24  *
25  * see www.keylength.com
26  * script supports up to 75 for single byte push
27  */
28
29 /**
30  * secure_allocator is defined in allocators.h
31  * CPrivKey is a serialized private key, with all parameters included (279 bytes)
32  */
33 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
34
35 /** An encapsulated private key. */
36 class CKey
37 {
38 private:
39     //! Whether this private key is valid. We check for correctness when modifying the key
40     //! data, so fValid should always correspond to the actual state.
41     bool fValid;
42
43     //! Whether the public key corresponding to this private key is (to be) compressed.
44     bool fCompressed;
45
46     //! The actual byte data
47     unsigned char vch[32];
48
49     //! Check whether the 32-byte array pointed to be vch is valid keydata.
50     bool static Check(const unsigned char* vch);
51
52 public:
53     //! Construct an invalid private key.
54     CKey() : fValid(false), fCompressed(false)
55     {
56         LockObject(vch);
57     }
58
59     //! Copy constructor. This is necessary because of memlocking.
60     CKey(const CKey& secret) : fValid(secret.fValid), fCompressed(secret.fCompressed)
61     {
62         LockObject(vch);
63         memcpy(vch, secret.vch, sizeof(vch));
64     }
65
66     //! Destructor (again necessary because of memlocking).
67     ~CKey()
68     {
69         UnlockObject(vch);
70     }
71
72     friend bool operator==(const CKey& a, const CKey& b)
73     {
74         return a.fCompressed == b.fCompressed && a.size() == b.size() &&
75                memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
76     }
77
78     //! Initialize using begin and end iterators to byte data.
79     template <typename T>
80     void Set(const T pbegin, const T pend, bool fCompressedIn)
81     {
82         if (pend - pbegin != 32) {
83             fValid = false;
84             return;
85         }
86         if (Check(&pbegin[0])) {
87             memcpy(vch, (unsigned char*)&pbegin[0], 32);
88             fValid = true;
89             fCompressed = fCompressedIn;
90         } else {
91             fValid = false;
92         }
93     }
94
95     //! Simple read-only vector-like interface.
96     unsigned int size() const { return (fValid ? 32 : 0); }
97     const unsigned char* begin() const { return vch; }
98     const unsigned char* end() const { return vch + size(); }
99
100     //! Check whether this private key is valid.
101     bool IsValid() const { return fValid; }
102
103     //! Check whether the public key corresponding to this private key is (to be) compressed.
104     bool IsCompressed() const { return fCompressed; }
105
106     //! Initialize from a CPrivKey (serialized OpenSSL private key data).
107     bool SetPrivKey(const CPrivKey& vchPrivKey, bool fCompressed);
108
109     //! Generate a new private key using a cryptographic PRNG.
110     void MakeNewKey(bool fCompressed);
111
112     /**
113      * Convert the private key to a CPrivKey (serialized OpenSSL private key data).
114      * This is expensive. 
115      */
116     CPrivKey GetPrivKey() const;
117
118     /**
119      * Compute the public key from a private key.
120      * This is expensive.
121      */
122     CPubKey GetPubKey() const;
123
124     //! Create a DER-serialized signature.
125     bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, bool lowS = true) const;
126
127     /**
128      * Create a compact signature (65 bytes), which allows reconstructing the used public key.
129      * The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
130      * The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
131      *                  0x1D = second key with even y, 0x1E = second key with odd y,
132      *                  add 0x04 for compressed keys.
133      */
134     bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
135
136     //! Derive BIP32 child key.
137     bool Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
138
139     //! Load private key and check that public key matches.
140     bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
141
142     //! Check whether an element of a signature (r or s) is valid.
143     static bool CheckSignatureElement(const unsigned char* vch, int len, bool half);
144 };
145
146 struct CExtKey {
147     unsigned char nDepth;
148     unsigned char vchFingerprint[4];
149     unsigned int nChild;
150     unsigned char vchChainCode[32];
151     CKey key;
152
153     friend bool operator==(const CExtKey& a, const CExtKey& b)
154     {
155         return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
156                memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.key == b.key;
157     }
158
159     void Encode(unsigned char code[74]) const;
160     void Decode(const unsigned char code[74]);
161     bool Derive(CExtKey& out, unsigned int nChild) const;
162     CExtPubKey Neuter() const;
163     void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
164 };
165
166 /** Check that required EC support is available at runtime */
167 bool ECC_InitSanityCheck(void);
168
169 #endif // BITCOIN_KEY_H
This page took 0.031817 seconds and 4 git commands to generate.