5 #include <openssl/aes.h>
6 #include <openssl/evp.h>
21 if (nDerivationMethod == 0)
22 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
23 (
unsigned char *)&strKeyData[0], strKeyData.size(), nRounds,
chKey,
chIV);
27 OPENSSL_cleanse(chKey,
sizeof(chKey));
42 memcpy(&
chIV[0], &chNewIV[0],
sizeof chIV);
55 int nLen = vchPlaintext.size();
56 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
57 vchCiphertext = std::vector<unsigned char> (nCLen);
63 EVP_CIPHER_CTX_init(&ctx);
64 if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL,
chKey,
chIV);
65 if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
66 if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
67 EVP_CIPHER_CTX_cleanup(&ctx);
69 if (!fOk)
return false;
71 vchCiphertext.resize(nCLen + nFLen);
81 int nLen = vchCiphertext.size();
82 int nPLen = nLen, nFLen = 0;
90 EVP_CIPHER_CTX_init(&ctx);
91 if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL,
chKey,
chIV);
92 if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
93 if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
94 EVP_CIPHER_CTX_cleanup(&ctx);
96 if (!fOk)
return false;
98 vchPlaintext.resize(nPLen + nFLen);
108 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
118 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext)
const unsigned int WALLET_CRYPTO_KEY_SIZE
unsigned char chIV[WALLET_CRYPTO_KEY_SIZE]
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Encryption/decryption context with key information.
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext)
unsigned char chKey[WALLET_CRYPTO_KEY_SIZE]
const unsigned int WALLET_CRYPTO_SALT_SIZE
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)