@@ -17,58 +17,64 @@ BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
1717class TestCrypter
1818{
1919public:
20- static void TestPassphraseSingle (const std::vector<unsigned char >& vchSalt , const SecureString& passphrase, uint32_t rounds,
21- const std::vector<unsigned char >& correctKey = {},
22- const std::vector<unsigned char >& correctIV = {})
20+ static void TestPassphraseSingle (const std::vector<unsigned char >& salt , const SecureString& passphrase, uint32_t rounds,
21+ const std::vector<unsigned char >& correct_key = {},
22+ const std::vector<unsigned char >& correct_iv = {})
2323{
2424 CCrypter crypt;
25- crypt.SetKeyFromPassphrase (passphrase, vchSalt, rounds, 0 );
26-
27- if (!correctKey.empty ())
28- BOOST_CHECK_MESSAGE (memcmp (crypt.vchKey .data (), correctKey.data (), crypt.vchKey .size ()) == 0 ,
29- HexStr (crypt.vchKey ) + std::string (" != " ) + HexStr (correctKey));
30- if (!correctIV.empty ())
31- BOOST_CHECK_MESSAGE (memcmp (crypt.vchIV .data (), correctIV.data (), crypt.vchIV .size ()) == 0 ,
32- HexStr (crypt.vchIV ) + std::string (" != " ) + HexStr (correctIV));
25+ crypt.SetKeyFromPassphrase (passphrase, salt, rounds, 0 );
26+
27+ if (!correct_key.empty ()) {
28+ BOOST_CHECK_MESSAGE (memcmp (crypt.vchKey .data (), correct_key.data (), crypt.vchKey .size ()) == 0 ,
29+ HexStr (crypt.vchKey ) + std::string (" != " ) + HexStr (correct_key));
30+ }
31+ if (!correct_iv.empty ()) {
32+ BOOST_CHECK_MESSAGE (memcmp (crypt.vchIV .data (), correct_iv.data (), crypt.vchIV .size ()) == 0 ,
33+ HexStr (crypt.vchIV ) + std::string (" != " ) + HexStr (correct_iv));
34+ }
3335}
3436
35- static void TestPassphrase (const std::vector<unsigned char >& vchSalt , const SecureString& passphrase, uint32_t rounds,
36- const std::vector<unsigned char >& correctKey = {},
37- const std::vector<unsigned char >& correctIV = {})
37+ static void TestPassphrase (const std::vector<unsigned char >& salt , const SecureString& passphrase, uint32_t rounds,
38+ const std::vector<unsigned char >& correct_key = {},
39+ const std::vector<unsigned char >& correct_iv = {})
3840{
39- TestPassphraseSingle (vchSalt, passphrase, rounds, correctKey, correctIV);
40- for (SecureString::const_iterator i (passphrase.begin ()); i != passphrase.end (); ++i)
41- TestPassphraseSingle (vchSalt, SecureString (i, passphrase.end ()), rounds);
41+ TestPassphraseSingle (salt, passphrase, rounds, correct_key, correct_iv);
42+ for (SecureString::const_iterator it{passphrase.begin ()}; it != passphrase.end (); ++it) {
43+ TestPassphraseSingle (salt, SecureString{it, passphrase.end ()}, rounds);
44+ }
4245}
4346
44- static void TestDecrypt (const CCrypter& crypt, const std::vector<unsigned char >& vchCiphertext ,
45- const std::vector<unsigned char >& vchCorrectPlaintext = {})
47+ static void TestDecrypt (const CCrypter& crypt, const std::vector<unsigned char >& ciphertext ,
48+ const std::vector<unsigned char >& correct_plaintext = {})
4649{
47- CKeyingMaterial vchDecrypted;
48- crypt.Decrypt (vchCiphertext, vchDecrypted);
49- if (vchPlaintext.size ())
50- BOOST_CHECK_EQUAL_COLLECTIONS (vchDecrypted.begin (), vchDecrypted.end (), vchCorrectPlaintext.begin (), vchCorrectPlaintext.end ());
50+ CKeyingMaterial decrypted;
51+ crypt.Decrypt (ciphertext, decrypted);
52+ if (!correct_plaintext.empty ()) {
53+ BOOST_CHECK_EQUAL_COLLECTIONS (decrypted.begin (), decrypted.end (), correct_plaintext.begin (), correct_plaintext.end ());
54+ }
5155}
5256
53- static void TestEncryptSingle (const CCrypter& crypt, const CKeyingMaterial& vchPlaintext ,
54- const std::vector<unsigned char >& vchCiphertextCorrect = {})
57+ static void TestEncryptSingle (const CCrypter& crypt, const CKeyingMaterial& plaintext ,
58+ const std::vector<unsigned char >& correct_ciphertext = {})
5559{
56- std::vector<unsigned char > vchCiphertext ;
57- crypt.Encrypt (vchPlaintext, vchCiphertext );
60+ std::vector<unsigned char > ciphertext ;
61+ crypt.Encrypt (plaintext, ciphertext );
5862
59- if (!vchCiphertextCorrect.empty ())
60- BOOST_CHECK_EQUAL_COLLECTIONS (vchCiphertext.begin (), vchCiphertext.end (), vchCiphertextCorrect.begin (), vchCiphertextCorrect.end ());
63+ if (!correct_ciphertext.empty ()) {
64+ BOOST_CHECK_EQUAL_COLLECTIONS (ciphertext.begin (), ciphertext.end (), correct_ciphertext.begin (), correct_ciphertext.end ());
65+ }
6166
62- const std::vector<unsigned char > vchPlaintext2 (vchPlaintext .begin (), vchPlaintext .end ());
63- TestDecrypt (crypt, vchCiphertext, vchPlaintext2 );
67+ const std::vector<unsigned char > plaintext2 (plaintext .begin (), plaintext .end ());
68+ TestDecrypt (crypt, ciphertext, /* correct_plaintext= */ plaintext2 );
6469}
6570
66- static void TestEncrypt (const CCrypter& crypt, const std::vector<unsigned char >& vchPlaintextIn ,
67- const std::vector<unsigned char >& vchCiphertextCorrect = {})
71+ static void TestEncrypt (const CCrypter& crypt, const std::vector<unsigned char >& plaintext ,
72+ const std::vector<unsigned char >& correct_ciphertext = {})
6873{
69- TestEncryptSingle (crypt, CKeyingMaterial{vchPlaintextIn.begin (), vchPlaintextIn.end ()}, vchCiphertextCorrect);
70- for (std::vector<unsigned char >::const_iterator i (vchPlaintextIn.begin ()); i != vchPlaintextIn.end (); ++i)
71- TestEncryptSingle (crypt, CKeyingMaterial (i, vchPlaintextIn.end ()));
74+ TestEncryptSingle (crypt, CKeyingMaterial{plaintext.begin (), plaintext.end ()}, correct_ciphertext);
75+ for (auto it{plaintext.begin ()}; it != plaintext.end (); ++it) {
76+ TestEncryptSingle (crypt, CKeyingMaterial{it, plaintext.end ()});
77+ }
7278}
7379
7480};
0 commit comments