mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-26 11:13:23 -03:00
refactor: Improve CCrypter related lines
Lines will be touched in next 2 commits.
This commit is contained in:
parent
7e1d9a8468
commit
d99c816971
2 changed files with 21 additions and 22 deletions
|
@ -8,6 +8,7 @@
|
||||||
#include <crypto/aes.h>
|
#include <crypto/aes.h>
|
||||||
#include <crypto/sha512.h>
|
#include <crypto/sha512.h>
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace wallet {
|
namespace wallet {
|
||||||
|
@ -24,7 +25,7 @@ int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, cons
|
||||||
unsigned char buf[CSHA512::OUTPUT_SIZE];
|
unsigned char buf[CSHA512::OUTPUT_SIZE];
|
||||||
CSHA512 di;
|
CSHA512 di;
|
||||||
|
|
||||||
di.Write((const unsigned char*)strKeyData.data(), strKeyData.size());
|
di.Write(UCharCast(strKeyData.data()), strKeyData.size());
|
||||||
di.Write(chSalt.data(), chSalt.size());
|
di.Write(chSalt.data(), chSalt.size());
|
||||||
di.Finalize(buf);
|
di.Finalize(buf);
|
||||||
|
|
||||||
|
@ -93,12 +94,10 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// plaintext will always be equal to or lesser than length of ciphertext
|
// plaintext will always be equal to or lesser than length of ciphertext
|
||||||
int nLen = vchCiphertext.size();
|
vchPlaintext.resize(vchCiphertext.size());
|
||||||
|
|
||||||
vchPlaintext.resize(nLen);
|
|
||||||
|
|
||||||
AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true);
|
AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true);
|
||||||
nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data());
|
int nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data());
|
||||||
if(nLen == 0)
|
if(nLen == 0)
|
||||||
return false;
|
return false;
|
||||||
vchPlaintext.resize(nLen);
|
vchPlaintext.resize(nLen);
|
||||||
|
@ -118,8 +117,8 @@ bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vch
|
||||||
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
|
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
|
||||||
{
|
{
|
||||||
CCrypter cKeyCrypter;
|
CCrypter cKeyCrypter;
|
||||||
std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
|
static_assert(WALLET_CRYPTO_IV_SIZE <= std::remove_reference_t<decltype(nIV)>::size());
|
||||||
memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);
|
std::vector<unsigned char> chIV{nIV.begin(), nIV.begin() + WALLET_CRYPTO_IV_SIZE};
|
||||||
if(!cKeyCrypter.SetKey(vMasterKey, chIV))
|
if(!cKeyCrypter.SetKey(vMasterKey, chIV))
|
||||||
return false;
|
return false;
|
||||||
return cKeyCrypter.Decrypt(vchCiphertext, vchPlaintext);
|
return cKeyCrypter.Decrypt(vchCiphertext, vchPlaintext);
|
||||||
|
|
|
@ -18,14 +18,14 @@ class TestCrypter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
|
static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
|
||||||
const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
|
const std::vector<unsigned char>& correctKey = {},
|
||||||
const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
|
const std::vector<unsigned char>& correctIV = {})
|
||||||
{
|
{
|
||||||
CCrypter crypt;
|
CCrypter crypt;
|
||||||
crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
|
crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
|
||||||
|
|
||||||
if(!correctKey.empty())
|
if(!correctKey.empty())
|
||||||
BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \
|
BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0,
|
||||||
HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correctKey));
|
HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correctKey));
|
||||||
if(!correctIV.empty())
|
if(!correctIV.empty())
|
||||||
BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
|
BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
|
||||||
|
@ -33,40 +33,40 @@ static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, cons
|
||||||
}
|
}
|
||||||
|
|
||||||
static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
|
static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
|
||||||
const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
|
const std::vector<unsigned char>& correctKey = {},
|
||||||
const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
|
const std::vector<unsigned char>& correctIV = {})
|
||||||
{
|
{
|
||||||
TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
|
TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
|
||||||
for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
|
for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
|
||||||
TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
|
TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, \
|
static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext,
|
||||||
const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
|
const std::vector<unsigned char>& vchCorrectPlaintext = {})
|
||||||
{
|
{
|
||||||
CKeyingMaterial vchDecrypted;
|
CKeyingMaterial vchDecrypted;
|
||||||
crypt.Decrypt(vchCiphertext, vchDecrypted);
|
crypt.Decrypt(vchCiphertext, vchDecrypted);
|
||||||
if (vchPlaintext.size())
|
if (vchPlaintext.size())
|
||||||
BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted);
|
BOOST_CHECK_EQUAL_COLLECTIONS(vchDecrypted.begin(), vchDecrypted.end(), vchCorrectPlaintext.begin(), vchCorrectPlaintext.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
|
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
|
||||||
const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
|
const std::vector<unsigned char>& vchCiphertextCorrect = {})
|
||||||
{
|
{
|
||||||
std::vector<unsigned char> vchCiphertext;
|
std::vector<unsigned char> vchCiphertext;
|
||||||
crypt.Encrypt(vchPlaintext, vchCiphertext);
|
crypt.Encrypt(vchPlaintext, vchCiphertext);
|
||||||
|
|
||||||
if (!vchCiphertextCorrect.empty())
|
if (!vchCiphertextCorrect.empty())
|
||||||
BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
|
BOOST_CHECK_EQUAL_COLLECTIONS(vchCiphertext.begin(), vchCiphertext.end(), vchCiphertextCorrect.begin(), vchCiphertextCorrect.end());
|
||||||
|
|
||||||
const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
|
const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
|
||||||
TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
|
TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, \
|
static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn,
|
||||||
const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
|
const std::vector<unsigned char>& vchCiphertextCorrect = {})
|
||||||
{
|
{
|
||||||
TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect);
|
TestEncryptSingle(crypt, CKeyingMaterial{vchPlaintextIn.begin(), vchPlaintextIn.end()}, vchCiphertextCorrect);
|
||||||
for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
|
for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
|
||||||
TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
|
TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
|
||||||
}
|
}
|
||||||
|
@ -76,8 +76,8 @@ static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>&
|
||||||
BOOST_AUTO_TEST_CASE(passphrase) {
|
BOOST_AUTO_TEST_CASE(passphrase) {
|
||||||
// These are expensive.
|
// These are expensive.
|
||||||
|
|
||||||
TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \
|
TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000,
|
||||||
ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
|
ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"),
|
||||||
ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
|
ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
|
||||||
|
|
||||||
std::string hash(GetRandHash().ToString());
|
std::string hash(GetRandHash().ToString());
|
||||||
|
|
Loading…
Add table
Reference in a new issue