Dont run full check every time we decrypt wallet.
[bitcoinplatinum.git] / src / crypter.h
blobf16fcef9c70237da378546ab712d13975e6b41c9
1 // Copyright (c) 2009-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef __CRYPTER_H__
6 #define __CRYPTER_H__
8 #include "allocators.h"
9 #include "serialize.h"
10 #include "keystore.h"
12 class uint256;
14 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
15 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
18 Private key encryption is done based on a CMasterKey,
19 which holds a salt and random encryption key.
21 CMasterKeys are encrypted using AES-256-CBC using a key
22 derived using derivation method nDerivationMethod
23 (0 == EVP_sha512()) and derivation iterations nDeriveIterations.
24 vchOtherDerivationParameters is provided for alternative algorithms
25 which may require more parameters (such as scrypt).
27 Wallet Private Keys are then encrypted using AES-256-CBC
28 with the double-sha256 of the public key as the IV, and the
29 master key's key as the encryption key (see keystore.[ch]).
32 /** Master key for wallet encryption */
33 class CMasterKey
35 public:
36 std::vector<unsigned char> vchCryptedKey;
37 std::vector<unsigned char> vchSalt;
38 // 0 = EVP_sha512()
39 // 1 = scrypt()
40 unsigned int nDerivationMethod;
41 unsigned int nDeriveIterations;
42 // Use this for more parameters to key derivation,
43 // such as the various parameters to scrypt
44 std::vector<unsigned char> vchOtherDerivationParameters;
46 IMPLEMENT_SERIALIZE
48 READWRITE(vchCryptedKey);
49 READWRITE(vchSalt);
50 READWRITE(nDerivationMethod);
51 READWRITE(nDeriveIterations);
52 READWRITE(vchOtherDerivationParameters);
54 CMasterKey()
56 // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
57 // ie slightly lower than the lowest hardware we need bother supporting
58 nDeriveIterations = 25000;
59 nDerivationMethod = 0;
60 vchOtherDerivationParameters = std::vector<unsigned char>(0);
64 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
66 /** Encryption/decryption context with key information */
67 class CCrypter
69 private:
70 unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
71 unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
72 bool fKeySet;
74 public:
75 bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
76 bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext);
77 bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext);
78 bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
80 void CleanKey()
82 OPENSSL_cleanse(chKey, sizeof(chKey));
83 OPENSSL_cleanse(chIV, sizeof(chIV));
84 fKeySet = false;
87 CCrypter()
89 fKeySet = false;
91 // Try to keep the key data out of swap (and be a bit over-careful to keep the IV that we don't even use out of swap)
92 // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
93 // Note as well that at no point in this program is any attempt made to prevent stealing of keys by reading the memory of the running process.
94 LockedPageManager::Instance().LockRange(&chKey[0], sizeof chKey);
95 LockedPageManager::Instance().LockRange(&chIV[0], sizeof chIV);
98 ~CCrypter()
100 CleanKey();
102 LockedPageManager::Instance().UnlockRange(&chKey[0], sizeof chKey);
103 LockedPageManager::Instance().UnlockRange(&chIV[0], sizeof chIV);
107 bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
108 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
110 /** Keystore which keeps the private keys encrypted.
111 * It derives from the basic key store, which is used if no encryption is active.
113 class CCryptoKeyStore : public CBasicKeyStore
115 private:
116 CryptedKeyMap mapCryptedKeys;
118 CKeyingMaterial vMasterKey;
120 // if fUseCrypto is true, mapKeys must be empty
121 // if fUseCrypto is false, vMasterKey must be empty
122 bool fUseCrypto;
124 // keeps track of whether Unlock has run a thourough check before
125 bool fDecryptionThoroughlyChecked;
127 protected:
128 bool SetCrypted();
130 // will encrypt previously unencrypted keys
131 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
133 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
135 public:
136 CCryptoKeyStore() : fUseCrypto(false), fDecryptionThoroughlyChecked(false)
140 bool IsCrypted() const
142 return fUseCrypto;
145 bool IsLocked() const
147 if (!IsCrypted())
148 return false;
149 bool result;
151 LOCK(cs_KeyStore);
152 result = vMasterKey.empty();
154 return result;
157 bool Lock();
159 virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
160 bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
161 bool HaveKey(const CKeyID &address) const
164 LOCK(cs_KeyStore);
165 if (!IsCrypted())
166 return CBasicKeyStore::HaveKey(address);
167 return mapCryptedKeys.count(address) > 0;
169 return false;
171 bool GetKey(const CKeyID &address, CKey& keyOut) const;
172 bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
173 void GetKeys(std::set<CKeyID> &setAddress) const
175 if (!IsCrypted())
177 CBasicKeyStore::GetKeys(setAddress);
178 return;
180 setAddress.clear();
181 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
182 while (mi != mapCryptedKeys.end())
184 setAddress.insert((*mi).first);
185 mi++;
189 /* Wallet status (encrypted, locked) changed.
190 * Note: Called without locks held.
192 boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
195 #endif