PRCYCoin  2.0.0.7rc1
P2P Digital Currency
key.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2015-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #ifndef BITCOIN_KEY_H
9 #define BITCOIN_KEY_H
10 
11 #include "allocators.h"
12 #include "serialize.h"
13 #include "uint256.h"
14 
15 #include <stdexcept>
16 #include <vector>
17 
18 class CPubKey;
19 
20 struct CExtPubKey;
21 
36 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
37 
39 class CKey
40 {
41 private:
44  bool fValid;
45 
48 
50  unsigned char vch[32];
51 
53  bool static Check(const unsigned char* vch);
54 
55 public:
57  CKey() : fValid(false), fCompressed(false)
58  {
59  LockObject(vch);
60  }
61 
63  CKey(const CKey& secret) : fValid(secret.fValid), fCompressed(secret.fCompressed)
64  {
65  LockObject(vch);
66  memcpy(vch, secret.vch, sizeof(vch));
67  }
68 
71  {
73  }
74 
75  friend bool operator==(const CKey& a, const CKey& b)
76  {
77  return a.fCompressed == b.fCompressed && a.size() == b.size() &&
78  memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
79  }
80 
82  template <typename T>
83  void Set(const T pbegin, const T pend, bool fCompressedIn)
84  {
85  if (pend - pbegin != 32) {
86  fValid = false;
87  return;
88  }
89  if (Check(&pbegin[0])) {
90  memcpy(vch, (unsigned char*)&pbegin[0], 32);
91  fValid = true;
92  fCompressed = fCompressedIn;
93  } else {
94  fValid = false;
95  }
96  }
97 
99  unsigned int size() const { return (fValid ? 32 : 0); }
100  const unsigned char* begin() const { return vch; }
101  const unsigned char* end() const { return vch + size(); }
102 
104  bool IsValid() const { return fValid; }
105 
107  bool IsCompressed() const { return fCompressed; }
108 
110  bool SetPrivKey(const CPrivKey& vchPrivKey, bool fCompressed);
111 
113  void MakeNewKey(bool fCompressed);
114 
116 
121  CPrivKey GetPrivKey() const;
122 
127  CPubKey GetPubKey() const;
128 
134  bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
135 
143  bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
144 
146  bool Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
147 
152  bool VerifyPubKey(const CPubKey& vchPubKey) const;
153 
155  bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
156 
158  static bool CheckSignatureElement(const unsigned char* vch, int len, bool half);
159 };
160 
161 struct CExtKey {
162  unsigned char nDepth;
163  unsigned char vchFingerprint[4];
164  unsigned int nChild;
165  unsigned char vchChainCode[32];
167 
168  friend bool operator==(const CExtKey& a, const CExtKey& b)
169  {
170  return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
171  memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.key == b.key;
172  }
173 
174  void Encode(unsigned char code[74]) const;
175  void Decode(const unsigned char code[74]);
176  bool Derive(CExtKey& out, unsigned int nChild) const;
177  CExtPubKey Neuter() const;
178  void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
179 };
180 
182 bool ECC_InitSanityCheck(void);
183 
184 #endif // BITCOIN_KEY_H
CKey::IsCompressed
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:107
UnlockObject
void UnlockObject(const T &t)
Definition: allocators.h:174
CKey::operator==
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:75
CKey::MakeNewKey
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:40
b
void const uint64_t * b
Definition: field_5x52_asm_impl.h:10
CKey::SetPrivKey
bool SetPrivKey(const CPrivKey &vchPrivKey, bool fCompressed)
Initialize from a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:49
CExtKey::Derive
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:182
CKey::GetPrivKey
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:66
CExtKey::vchFingerprint
unsigned char vchFingerprint[4]
Definition: key.h:163
uint256.h
CKey::Derive
bool Derive(CKey &keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const
Derive BIP32 child key.
Definition: key.cpp:159
CKey::Set
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:83
CPrivKey
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secp256k1: const unsigned int PRIVATE_KEY_SIZE = 279; const unsigned int PUBLIC_KEY_SIZE = 65; const ...
Definition: key.h:20
CKey::fCompressed
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:47
memcpy
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:15
CExtKey::nDepth
unsigned char nDepth
Definition: key.h:162
CKey::end
const unsigned char * end() const
Definition: key.h:101
CKey::CKey
CKey()
Construct an invalid private key.
Definition: key.h:57
CExtKey::operator==
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:168
CKey::vch
unsigned char vch[32]
The actual byte data.
Definition: key.h:50
CKey::begin
const unsigned char * begin() const
Definition: key.h:100
CKey::CKey
CKey(const CKey &secret)
Copy constructor. This is necessary because of memlocking.
Definition: key.h:63
CKey::GetPrivKey_256
uint256 GetPrivKey_256()
Definition: key.cpp:58
CExtKey
Definition: key.h:161
CExtKey::Decode
void Decode(const unsigned char code[74])
Definition: key.cpp:230
CKey::~CKey
~CKey()
Destructor (again necessary because of memlocking).
Definition: key.h:70
CKey::SignCompact
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key.
Definition: key.cpp:126
LockObject
void LockObject(const T &t)
Definition: allocators.h:168
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:104
allocators.h
CExtKey::vchChainCode
unsigned char vchChainCode[32]
Definition: key.h:165
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CExtKey::nChild
unsigned int nChild
Definition: key.h:164
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:79
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
CKey::size
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:99
CKey::Load
bool Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:146
CKey
An encapsulated private key.
Definition: key.h:39
CKey::VerifyPubKey
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:111
ECC_InitSanityCheck
bool ECC_InitSanityCheck(void)
Check that required EC support is available at runtime.
Definition: key.cpp:239
CKey::Sign
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:91
serialize.h
CKey::CheckSignatureElement
static bool CheckSignatureElement(const unsigned char *vch, int len, bool half)
Check whether an element of a signature (r or s) is valid.
CKey::fValid
bool fValid
Whether this private key is valid.
Definition: key.h:44
CExtKey::Neuter
CExtPubKey Neuter() const
Definition: key.cpp:205
CExtKey::Encode
void Encode(unsigned char code[74]) const
Definition: key.cpp:216
CKey::Check
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to be vch is valid keydata.
Definition: key.cpp:35
CExtPubKey
Definition: pubkey.h:205
CExtKey::key
CKey key
Definition: key.h:166
CExtKey::SetMaster
void SetMaster(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:191