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>
17 bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
19 if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
23 if (nDerivationMethod == 0)
25 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
26 (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
29 if (nDerivationMethod == 1)
31 // Passphrase conversion
32 uint256 scryptHash = scrypt_salted_multiround_hash((const void*)strKeyData.c_str(), strKeyData.size(), &chSalt[0], 8, nRounds);
34 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
35 (unsigned char *)&scryptHash, sizeof scryptHash, nRounds, chKey, chIV);
36 OPENSSL_cleanse(&scryptHash, sizeof scryptHash);
40 if (i != (int)WALLET_CRYPTO_KEY_SIZE)
42 OPENSSL_cleanse(&chKey, sizeof chKey);
43 OPENSSL_cleanse(&chIV, sizeof chIV);
51 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
53 if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
56 memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
57 memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
63 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
68 // max ciphertext len for a n bytes of plaintext is
69 // n + AES_BLOCK_SIZE - 1 bytes
70 int nLen = vchPlaintext.size();
71 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
72 vchCiphertext = std::vector<unsigned char> (nCLen);
78 EVP_CIPHER_CTX_init(&ctx);
79 if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
80 if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
81 if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
82 EVP_CIPHER_CTX_cleanup(&ctx);
84 if (!fOk) return false;
86 vchCiphertext.resize(nCLen + nFLen);
90 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
95 // plaintext will always be equal to or lesser than length of ciphertext
96 int nLen = vchCiphertext.size();
97 int nPLen = nLen, nFLen = 0;
99 vchPlaintext = CKeyingMaterial(nPLen);
105 EVP_CIPHER_CTX_init(&ctx);
106 if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
107 if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
108 if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
109 EVP_CIPHER_CTX_cleanup(&ctx);
111 if (!fOk) return false;
113 vchPlaintext.resize(nPLen + nFLen);
118 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
120 CCrypter cKeyCrypter;
121 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
122 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
123 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
125 return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
128 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
130 CCrypter cKeyCrypter;
131 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
132 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
133 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
135 return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));