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.
7 #include "script/script.h"
12 #include <boost/foreach.hpp>
13 #include <openssl/aes.h>
14 #include <openssl/evp.h>
16 bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
18 if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
22 if (nDerivationMethod == 0)
23 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
24 (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
26 if (i != (int)WALLET_CRYPTO_KEY_SIZE)
28 OPENSSL_cleanse(chKey, sizeof(chKey));
29 OPENSSL_cleanse(chIV, sizeof(chIV));
37 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
39 if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
42 memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
43 memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
49 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
54 // max ciphertext len for a n bytes of plaintext is
55 // n + AES_BLOCK_SIZE - 1 bytes
56 int nLen = vchPlaintext.size();
57 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
58 vchCiphertext = std::vector<unsigned char> (nCLen);
64 EVP_CIPHER_CTX_init(&ctx);
65 if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
66 if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
67 if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
68 EVP_CIPHER_CTX_cleanup(&ctx);
70 if (!fOk) return false;
72 vchCiphertext.resize(nCLen + nFLen);
76 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
81 // plaintext will always be equal to or lesser than length of ciphertext
82 int nLen = vchCiphertext.size();
83 int nPLen = nLen, nFLen = 0;
85 vchPlaintext = CKeyingMaterial(nPLen);
91 EVP_CIPHER_CTX_init(&ctx);
92 if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
93 if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
94 if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
95 EVP_CIPHER_CTX_cleanup(&ctx);
97 if (!fOk) return false;
99 vchPlaintext.resize(nPLen + nFLen);
104 bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
106 CCrypter cKeyCrypter;
107 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
108 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
109 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
111 return cKeyCrypter.Encrypt(*((const CKeyingMaterial*)&vchPlaintext), vchCiphertext);
114 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
116 CCrypter cKeyCrypter;
117 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
118 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
119 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
121 return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
124 bool CCryptoKeyStore::SetCrypted()
129 if (!mapKeys.empty())
135 bool CCryptoKeyStore::Lock()
145 NotifyStatusChanged(this);
149 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
156 bool keyPass = false;
157 bool keyFail = false;
158 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
159 for (; mi != mapCryptedKeys.end(); ++mi)
161 const CPubKey &vchPubKey = (*mi).second.first;
162 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
163 CKeyingMaterial vchSecret;
164 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
169 if (vchSecret.size() != 32)
175 key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
176 if (key.GetPubKey() != vchPubKey)
182 if (fDecryptionThoroughlyChecked)
185 if (keyPass && keyFail)
187 LogPrintf("The wallet is probably corrupted: Some keys decrypt but not all.");
190 if (keyFail || !keyPass)
192 vMasterKey = vMasterKeyIn;
193 fDecryptionThoroughlyChecked = true;
195 NotifyStatusChanged(this);
199 bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
204 return CBasicKeyStore::AddKeyPubKey(key, pubkey);
209 std::vector<unsigned char> vchCryptedSecret;
210 CKeyingMaterial vchSecret(key.begin(), key.end());
211 if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret))
214 if (!AddCryptedKey(pubkey, vchCryptedSecret))
221 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
228 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
233 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
238 return CBasicKeyStore::GetKey(address, keyOut);
240 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
241 if (mi != mapCryptedKeys.end())
243 const CPubKey &vchPubKey = (*mi).second.first;
244 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
245 CKeyingMaterial vchSecret;
246 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
248 if (vchSecret.size() != 32)
250 keyOut.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
257 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
262 return CKeyStore::GetPubKey(address, vchPubKeyOut);
264 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
265 if (mi != mapCryptedKeys.end())
267 vchPubKeyOut = (*mi).second.first;
274 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
278 if (!mapCryptedKeys.empty() || IsCrypted())
282 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
284 const CKey &key = mKey.second;
285 CPubKey vchPubKey = key.GetPubKey();
286 CKeyingMaterial vchSecret(key.begin(), key.end());
287 std::vector<unsigned char> vchCryptedSecret;
288 if (!EncryptSecret(vMasterKeyIn, vchSecret, vchPubKey.GetHash(), vchCryptedSecret))
290 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))