1 // Copyright (c) 2009-2012 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 #include <openssl/aes.h>
6 #include <openssl/evp.h>
20 CMasterKey::CMasterKey()
22 // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
23 // ie slightly lower than the lowest hardware we need bother supporting
24 nDeriveIterations = 25000;
25 nDerivationMethod = 0;
26 vchOtherDerivationParameters = std::vector<unsigned char>(0);
33 bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
35 if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
39 if (nDerivationMethod == 0)
41 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
42 (const unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
45 if (i != (int)WALLET_CRYPTO_KEY_SIZE)
47 OPENSSL_cleanse(&chKey, sizeof chKey);
48 OPENSSL_cleanse(&chIV, sizeof chIV);
56 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
58 if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
61 memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
62 memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
68 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
73 // max ciphertext len for a n bytes of plaintext is
74 // n + AES_BLOCK_SIZE - 1 bytes
75 int nLen = vchPlaintext.size();
76 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
77 vchCiphertext = std::vector<unsigned char> (nCLen);
83 EVP_CIPHER_CTX_init(&ctx);
84 if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
85 if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
86 if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
87 EVP_CIPHER_CTX_cleanup(&ctx);
89 if (!fOk) return false;
91 vchCiphertext.resize(nCLen + nFLen);
95 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
100 // plaintext will always be equal to or lesser than length of ciphertext
101 int nLen = vchCiphertext.size();
102 int nPLen = nLen, nFLen = 0;
104 vchPlaintext = CKeyingMaterial(nPLen);
110 EVP_CIPHER_CTX_init(&ctx);
111 if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
112 if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
113 if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
114 EVP_CIPHER_CTX_cleanup(&ctx);
116 if (!fOk) return false;
118 vchPlaintext.resize(nPLen + nFLen);
122 void CCrypter::CleanKey()
124 OPENSSL_cleanse(&chKey, sizeof chKey);
125 OPENSSL_cleanse(&chIV, sizeof chIV);
133 // 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)
134 // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
135 // 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.
136 LockedPageManager::instance.LockRange(&chKey[0], sizeof chKey);
137 LockedPageManager::instance.LockRange(&chIV[0], sizeof chIV);
140 CCrypter::~CCrypter()
144 LockedPageManager::instance.UnlockRange(&chKey[0], sizeof chKey);
145 LockedPageManager::instance.UnlockRange(&chIV[0], sizeof chIV);
148 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
150 CCrypter cKeyCrypter;
151 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
152 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
153 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
155 return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
158 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
160 CCrypter cKeyCrypter;
161 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
162 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
163 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
165 return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));