5 #include <test/test_fabcoin.h> 11 #include <boost/test/unit_test.hpp> 12 #include <openssl/aes.h> 13 #include <openssl/evp.h> 17 bool
OldSetKeyFromPassphrase(const
SecureString& strKeyData, const
std::vector<
unsigned char>& chSalt, const
unsigned int nRounds, const
unsigned int nDerivationMethod,
unsigned char* chKey,
unsigned char* chIV)
23 if (nDerivationMethod == 0)
24 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
25 (
unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
36 bool OldEncrypt(
const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext,
const unsigned char chKey[32],
const unsigned char chIV[16])
40 int nLen = vchPlaintext.size();
41 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
42 vchCiphertext = std::vector<unsigned char> (nCLen);
44 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
46 if (!ctx)
return false;
50 EVP_CIPHER_CTX_init(ctx);
51 if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(),
nullptr, chKey, chIV) != 0;
52 if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
53 if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
54 EVP_CIPHER_CTX_cleanup(ctx);
56 EVP_CIPHER_CTX_free(ctx);
58 if (!fOk)
return false;
60 vchCiphertext.resize(nCLen + nFLen);
64 bool OldDecrypt(
const std::vector<unsigned char>& vchCiphertext,
CKeyingMaterial& vchPlaintext,
const unsigned char chKey[32],
const unsigned char chIV[16])
67 int nLen = vchCiphertext.size();
68 int nPLen = nLen, nFLen = 0;
72 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
74 if (!ctx)
return false;
78 EVP_CIPHER_CTX_init(ctx);
79 if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(),
nullptr, chKey, chIV) != 0;
80 if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
81 if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
82 EVP_CIPHER_CTX_cleanup(ctx);
84 EVP_CIPHER_CTX_free(ctx);
86 if (!fOk)
return false;
88 vchPlaintext.resize(nPLen + nFLen);
96 const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
97 const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
107 BOOST_CHECK_MESSAGE(memcmp(chKey, crypt.
vchKey.data(), crypt.
vchKey.size()) == 0, \
109 BOOST_CHECK_MESSAGE(memcmp(chIV, crypt.
vchIV.data(), crypt.
vchIV.size()) == 0, \
112 if(!correctKey.empty())
113 BOOST_CHECK_MESSAGE(memcmp(chKey, &correctKey[0],
sizeof(chKey)) == 0,
\ 114 HexStr(chKey, chKey+
sizeof(chKey)) + std::string(
" != ") +
HexStr(correctKey.begin(), correctKey.end()));
115 if(!correctIV.empty())
116 BOOST_CHECK_MESSAGE(memcmp(chIV, &correctIV[0],
sizeof(chIV)) == 0,
117 HexStr(chIV, chIV+
sizeof(chIV)) + std::string(
" != ") +
HexStr(correctIV.begin(), correctIV.end()));
121 const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
122 const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
125 for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
131 const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
135 int result1, result2;
136 result1 = crypt.
Decrypt(vchCiphertext, vchDecrypted1);
143 if (vchDecrypted1 != vchDecrypted2 && vchDecrypted1.size() >= AES_BLOCK_SIZE && SSLeay() == 0x100010afL)
145 for(CKeyingMaterial::iterator it = vchDecrypted1.end() - AES_BLOCK_SIZE; it != vchDecrypted1.end() - 1; it++)
149 BOOST_CHECK_MESSAGE(vchDecrypted1 == vchDecrypted2,
HexStr(vchDecrypted1.begin(), vchDecrypted1.end()) +
" != " +
HexStr(vchDecrypted2.begin(), vchDecrypted2.end()));
151 if (vchPlaintext.size())
156 const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
158 std::vector<unsigned char> vchCiphertext1;
159 std::vector<unsigned char> vchCiphertext2;
160 int result1 = crypt.
Encrypt(vchPlaintext, vchCiphertext1);
166 if (!vchCiphertextCorrect.empty())
167 BOOST_CHECK(vchCiphertext2 == vchCiphertextCorrect);
169 const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
171 if(vchCiphertext1 == vchCiphertext2)
176 const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
179 for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
189 ParseHex(
"fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
190 ParseHex(
"cf2f2691526dd1aa220896fb8bf7c369"));
193 std::vector<unsigned char> vchSalt(8);
195 uint32_t rounds = InsecureRand32();
202 std::vector<unsigned char> vchSalt =
ParseHex(
"0000deadbeef0000");
208 for (
int i = 0; i != 100; i++)
217 std::vector<unsigned char> vchSalt =
ParseHex(
"0000deadbeef0000");
230 for (
int i = 0; i != 100; i++)
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
bool OldSetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod, unsigned char *chKey, unsigned char *chIV)
const unsigned int WALLET_CRYPTO_KEY_SIZE
Encryption/decryption context with key information.
static void TestEncrypt(const CCrypter &crypt, const std::vector< unsigned char > &vchPlaintextIn, const std::vector< unsigned char > &vchCiphertextCorrect=std::vector< unsigned char >())
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
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 >())
std::hash for asio::adress
BOOST_AUTO_TEST_CASE(passphrase)
const unsigned int WALLET_CRYPTO_IV_SIZE
void memory_cleanse(void *ptr, size_t len)
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
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 >())
bool OldEncrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext, const unsigned char chKey[32], const unsigned char chIV[16])
const unsigned int WALLET_CRYPTO_SALT_SIZE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
void encrypt(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypts plain text using Public key.
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
bool decrypt(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypts cipher using Secret key.
#define BOOST_AUTO_TEST_SUITE_END()
static void TestDecrypt(const CCrypter &crypt, const std::vector< unsigned char > &vchCiphertext, const std::vector< unsigned char > &vchPlaintext=std::vector< unsigned char >())
bool OldDecrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext, const unsigned char chKey[32], const unsigned char chIV[16])
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
static void TestEncryptSingle(const CCrypter &crypt, const CKeyingMaterial &vchPlaintext, const std::vector< unsigned char > &vchCiphertextCorrect=std::vector< unsigned char >())
#define BOOST_CHECK(expr)
std::vector< unsigned char > ParseHex(const char *psz)