mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 06:43:45 +01:00
-BEGIN VERIFY SCRIPT- mkdir -p src/util git mv src/util.h src/util/system.h git mv src/util.cpp src/util/system.cpp git mv src/utilmemory.h src/util/memory.h git mv src/utilmoneystr.h src/util/moneystr.h git mv src/utilmoneystr.cpp src/util/moneystr.cpp git mv src/utilstrencodings.h src/util/strencodings.h git mv src/utilstrencodings.cpp src/util/strencodings.cpp git mv src/utiltime.h src/util/time.h git mv src/utiltime.cpp src/util/time.cpp sed -i 's/<util\.h>/<util\/system\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') sed -i 's/<utilmemory\.h>/<util\/memory\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') sed -i 's/<utilmoneystr\.h>/<util\/moneystr\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') sed -i 's/<utilstrencodings\.h>/<util\/strencodings\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') sed -i 's/<utiltime\.h>/<util\/time\.h>/g' $(git ls-files 'src/*.h' 'src/*.cpp') sed -i 's/BITCOIN_UTIL_H/BITCOIN_UTIL_SYSTEM_H/g' src/util/system.h sed -i 's/BITCOIN_UTILMEMORY_H/BITCOIN_UTIL_MEMORY_H/g' src/util/memory.h sed -i 's/BITCOIN_UTILMONEYSTR_H/BITCOIN_UTIL_MONEYSTR_H/g' src/util/moneystr.h sed -i 's/BITCOIN_UTILSTRENCODINGS_H/BITCOIN_UTIL_STRENCODINGS_H/g' src/util/strencodings.h sed -i 's/BITCOIN_UTILTIME_H/BITCOIN_UTIL_TIME_H/g' src/util/time.h sed -i 's/ util\.\(h\|cpp\)/ util\/system\.\1/g' src/Makefile.am sed -i 's/utilmemory\.\(h\|cpp\)/util\/memory\.\1/g' src/Makefile.am sed -i 's/utilmoneystr\.\(h\|cpp\)/util\/moneystr\.\1/g' src/Makefile.am sed -i 's/utilstrencodings\.\(h\|cpp\)/util\/strencodings\.\1/g' src/Makefile.am sed -i 's/utiltime\.\(h\|cpp\)/util\/time\.\1/g' src/Makefile.am sed -i 's/-> util ->/-> util\/system ->/' test/lint/lint-circular-dependencies.sh sed -i 's/src\/util\.cpp/src\/util\/system\.cpp/g' test/lint/lint-format-strings.py test/lint/lint-locale-dependence.sh sed -i 's/src\/utilmoneystr\.cpp/src\/util\/moneystr\.cpp/g' test/lint/lint-locale-dependence.sh sed -i 's/src\/utilstrencodings\.\(h\|cpp\)/src\/util\/strencodings\.\1/g' test/lint/lint-locale-dependence.sh sed -i 's/src\\utilstrencodings\.cpp/src\\util\\strencodings\.cpp/' build_msvc/libbitcoinconsensus/libbitcoinconsensus.vcxproj -END VERIFY SCRIPT-
127 lines
5.7 KiB
C++
127 lines
5.7 KiB
C++
// Copyright (c) 2014-2018 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <test/test_bitcoin.h>
|
|
#include <util/strencodings.h>
|
|
#include <wallet/crypter.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
|
|
|
|
class TestCrypter
|
|
{
|
|
public:
|
|
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>& correctIV=std::vector<unsigned char>())
|
|
{
|
|
CCrypter crypt;
|
|
crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
|
|
|
|
if(!correctKey.empty())
|
|
BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \
|
|
HexStr(crypt.vchKey.begin(), crypt.vchKey.end()) + std::string(" != ") + HexStr(correctKey.begin(), correctKey.end()));
|
|
if(!correctIV.empty())
|
|
BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
|
|
HexStr(crypt.vchIV.begin(), crypt.vchIV.end()) + std::string(" != ") + HexStr(correctIV.begin(), correctIV.end()));
|
|
}
|
|
|
|
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>& correctIV=std::vector<unsigned char>())
|
|
{
|
|
TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
|
|
for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
|
|
TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
|
|
}
|
|
|
|
static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, \
|
|
const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
|
|
{
|
|
CKeyingMaterial vchDecrypted;
|
|
crypt.Decrypt(vchCiphertext, vchDecrypted);
|
|
if (vchPlaintext.size())
|
|
BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted);
|
|
}
|
|
|
|
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
|
|
const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
|
|
{
|
|
std::vector<unsigned char> vchCiphertext;
|
|
crypt.Encrypt(vchPlaintext, vchCiphertext);
|
|
|
|
if (!vchCiphertextCorrect.empty())
|
|
BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
|
|
|
|
const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
|
|
TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
|
|
}
|
|
|
|
static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, \
|
|
const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
|
|
{
|
|
TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect);
|
|
for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
|
|
TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
|
|
}
|
|
|
|
};
|
|
|
|
BOOST_AUTO_TEST_CASE(passphrase) {
|
|
// These are expensive.
|
|
|
|
TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \
|
|
ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
|
|
ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
|
|
|
|
std::string hash(GetRandHash().ToString());
|
|
std::vector<unsigned char> vchSalt(8);
|
|
GetRandBytes(vchSalt.data(), vchSalt.size());
|
|
uint32_t rounds = InsecureRand32();
|
|
if (rounds > 30000)
|
|
rounds = 30000;
|
|
TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(encrypt) {
|
|
std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
|
|
BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
|
|
CCrypter crypt;
|
|
crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
|
|
TestCrypter::TestEncrypt(crypt, ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"));
|
|
|
|
for (int i = 0; i != 100; i++)
|
|
{
|
|
uint256 hash(GetRandHash());
|
|
TestCrypter::TestEncrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
|
|
}
|
|
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(decrypt) {
|
|
std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
|
|
BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
|
|
CCrypter crypt;
|
|
crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
|
|
|
|
// Some corner cases the came up while testing
|
|
TestCrypter::TestDecrypt(crypt,ParseHex("795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"));
|
|
TestCrypter::TestDecrypt(crypt,ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"));
|
|
TestCrypter::TestDecrypt(crypt,ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"));
|
|
TestCrypter::TestDecrypt(crypt,ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"));
|
|
TestCrypter::TestDecrypt(crypt,ParseHex("b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"));
|
|
TestCrypter::TestDecrypt(crypt,ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"));
|
|
|
|
for (int i = 0; i != 100; i++)
|
|
{
|
|
uint256 hash(GetRandHash());
|
|
TestCrypter::TestDecrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
|
|
}
|
|
}
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|