7 #include "test/test_prcycoin.h"
12 #include <boost/test/unit_test.hpp>
13 #include <openssl/aes.h>
14 #include <openssl/evp.h>
18 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)
24 if (nDerivationMethod == 0)
25 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
26 (
unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
37 bool OldEncrypt(
const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext,
const unsigned char chKey[32],
const unsigned char chIV[16])
41 int nLen = vchPlaintext.size();
42 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
43 vchCiphertext = std::vector<unsigned char> (nCLen);
45 EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
49 EVP_CIPHER_CTX_init(ctx);
50 if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
51 if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
52 if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
53 EVP_CIPHER_CTX_cleanup(ctx);
55 if (!fOk)
return false;
57 vchCiphertext.resize(nCLen + nFLen);
61 bool OldDecrypt(
const std::vector<unsigned char>& vchCiphertext,
CKeyingMaterial& vchPlaintext,
const unsigned char chKey[32],
const unsigned char chIV[16])
64 int nLen = vchCiphertext.size();
65 int nPLen = nLen, nFLen = 0;
69 EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
73 EVP_CIPHER_CTX_init(ctx);
74 if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
75 if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
76 if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
77 EVP_CIPHER_CTX_cleanup(ctx);
79 if (!fOk)
return false;
81 vchPlaintext.resize(nPLen + nFLen);
89 const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
90 const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
100 BOOST_CHECK_MESSAGE(memcmp(chKey, crypt.
chKey,
sizeof(chKey)) == 0, \
102 BOOST_CHECK_MESSAGE(memcmp(chIV, crypt.
chIV,
sizeof(chIV)) == 0, \
105 if(!correctKey.empty())
106 BOOST_CHECK_MESSAGE(memcmp(chKey, &correctKey[0],
sizeof(chKey)) == 0, \
107 HexStr(chKey, chKey+
sizeof(chKey)) + std::string(
" != ") +
HexStr(correctKey.begin(), correctKey.end()));
108 if(!correctIV.empty())
109 BOOST_CHECK_MESSAGE(memcmp(chIV, &correctIV[0],
sizeof(chIV)) == 0,
110 HexStr(chIV, chIV+
sizeof(chIV)) + std::string(
" != ") +
HexStr(correctIV.begin(), correctIV.end()));
114 const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
115 const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
118 for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
124 const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
128 int result1, result2;
129 result1 = crypt.
Decrypt(vchCiphertext, vchDecrypted1);
136 if (vchDecrypted1 != vchDecrypted2 && vchDecrypted1.size() >= AES_BLOCK_SIZE && SSLeay() == 0x100010afL)
138 for(CKeyingMaterial::iterator it = vchDecrypted1.end() - AES_BLOCK_SIZE; it != vchDecrypted1.end() - 1; it++)
142 BOOST_CHECK_MESSAGE(vchDecrypted1 == vchDecrypted2,
HexStr(vchDecrypted1.begin(), vchDecrypted1.end()) +
" != " +
HexStr(vchDecrypted2.begin(), vchDecrypted2.end()));
144 if (vchPlaintext.size())
149 const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
151 std::vector<unsigned char> vchCiphertext1;
152 std::vector<unsigned char> vchCiphertext2;
153 int result1 = crypt.
Encrypt(vchPlaintext, vchCiphertext1);
159 if (!vchCiphertextCorrect.empty())
160 BOOST_CHECK(vchCiphertext2 == vchCiphertextCorrect);
162 const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
164 if(vchCiphertext1 == vchCiphertext2)
169 const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
172 for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
182 ParseHex(
"fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
183 ParseHex(
"cf2f2691526dd1aa220896fb8bf7c369"));
186 std::vector<unsigned char> vchSalt(8);
195 std::vector<unsigned char> vchSalt =
ParseHex(
"0000deadbeef0000");
201 for (
int i = 0; i != 100; i++)
210 std::vector<unsigned char> vchSalt =
ParseHex(
"0000deadbeef0000");
223 for (
int i = 0; i != 100; i++)