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/evp.h>
12 bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
14 if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
18 if (nDerivationMethod == 0)
20 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
21 (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
24 if (i != (int)WALLET_CRYPTO_KEY_SIZE)
26 OPENSSL_cleanse(&chKey, sizeof chKey);
27 OPENSSL_cleanse(&chIV, sizeof chIV);
35 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
37 if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
40 memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
41 memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
47 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
52 // max ciphertext len for a n bytes of plaintext is
53 // n + AES_BLOCK_SIZE - 1 bytes
54 constexpr int AES_BLOCK_SIZE = 16; // from openssl/aes.h
55 int nLen = vchPlaintext.size();
56 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
57 vchCiphertext = std::vector<unsigned char> (nCLen);
63 ctx = EVP_CIPHER_CTX_new();
64 if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
65 if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
66 if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
67 EVP_CIPHER_CTX_free(ctx);
69 if (!fOk) return false;
71 vchCiphertext.resize(nCLen + nFLen);
75 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
80 // plaintext will always be equal to or lesser than length of ciphertext
81 int nLen = vchCiphertext.size();
82 int nPLen = nLen, nFLen = 0;
84 vchPlaintext = CKeyingMaterial(nPLen);
90 ctx = EVP_CIPHER_CTX_new();
91 if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
92 if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
93 if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
94 EVP_CIPHER_CTX_free(ctx);
96 if (!fOk) return false;
98 vchPlaintext.resize(nPLen + nFLen);
103 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
105 CCrypter cKeyCrypter;
106 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
107 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
108 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
110 return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
113 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
115 CCrypter cKeyCrypter;
116 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
117 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
118 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
120 return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));