Raven Core  3.0.0
P2P Digital Currency
crypto_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "test/test_raven.h"
7 #include "utilstrencodings.h"
8 #include "wallet/crypter.h"
9 
10 #include <vector>
11 
12 #include <boost/test/unit_test.hpp>
13 
14 BOOST_FIXTURE_TEST_SUITE(wallet_crypto, BasicTestingSetup)
15 
17  {
18  public:
19  static void TestPassphraseSingle(const std::vector<unsigned char> &vchSalt, const SecureString &passphrase, uint32_t rounds,
20  const std::vector<unsigned char> &correctKey = std::vector<unsigned char>(),
21  const std::vector<unsigned char> &correctIV = std::vector<unsigned char>())
22  {
23  CCrypter crypt;
24  crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
25 
26  if (!correctKey.empty())
27  BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \
28  HexStr(crypt.vchKey.begin(), crypt.vchKey.end()) + std::string(" != ") + HexStr(correctKey.begin(), correctKey.end()));
29  if (!correctIV.empty())
30  BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
31  HexStr(crypt.vchIV.begin(), crypt.vchIV.end()) + std::string(" != ") + HexStr(correctIV.begin(), correctIV.end()));
32  }
33 
34  static void TestPassphrase(const std::vector<unsigned char> &vchSalt, const SecureString &passphrase, uint32_t rounds,
35  const std::vector<unsigned char> &correctKey = std::vector<unsigned char>(),
36  const std::vector<unsigned char> &correctIV = std::vector<unsigned char>())
37  {
38  TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
39  for (SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
40  TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
41  }
42 
43  static void TestDecrypt(const CCrypter &crypt, const std::vector<unsigned char> &vchCiphertext, \
44  const std::vector<unsigned char> &vchPlaintext = std::vector<unsigned char>())
45  {
46  CKeyingMaterial vchDecrypted;
47  crypt.Decrypt(vchCiphertext, vchDecrypted);
48  if (vchPlaintext.size())
49  BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted);
50  }
51 
52  static void TestEncryptSingle(const CCrypter &crypt, const CKeyingMaterial &vchPlaintext,
53  const std::vector<unsigned char> &vchCiphertextCorrect = std::vector<unsigned char>())
54  {
55  std::vector<unsigned char> vchCiphertext;
56  crypt.Encrypt(vchPlaintext, vchCiphertext);
57 
58  if (!vchCiphertextCorrect.empty())
59  BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
60 
61  const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
62  TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
63  }
64 
65  static void TestEncrypt(const CCrypter &crypt, const std::vector<unsigned char> &vchPlaintextIn, \
66  const std::vector<unsigned char> &vchCiphertextCorrect = std::vector<unsigned char>())
67  {
68  TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect);
69  for (std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
70  TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
71  }
72 
73  };
74 
75  BOOST_AUTO_TEST_CASE(passphrase_test)
76  {
77  BOOST_TEST_MESSAGE("Running PassPhrase Test");
78 
79  // These are expensive.
80  TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \
81  ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
82  ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
83 
84  std::string hash(GetRandHash().ToString());
85  std::vector<unsigned char> vchSalt(8);
86  GetRandBytes(vchSalt.data(), vchSalt.size());
87  uint32_t rounds = InsecureRand32();
88  if (rounds > 30000)
89  rounds = 30000;
90  TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);
91  }
92 
93  BOOST_AUTO_TEST_CASE(encrypt_test)
94  {
95  BOOST_TEST_MESSAGE("Running Encrypt Test");
96 
97  std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
98  BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
99  CCrypter crypt;
100  crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
101  TestCrypter::TestEncrypt(crypt, ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"));
102 
103  for (int i = 0; i != 100; i++)
104  {
105  uint256 hash(GetRandHash());
106  TestCrypter::TestEncrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
107  }
108 
109  }
110 
111  BOOST_AUTO_TEST_CASE(decrypt_test)
112  {
113  BOOST_TEST_MESSAGE("Running Decrypt Test");
114 
115  std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
116  BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
117  CCrypter crypt;
118  crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
119 
120  // Some corner cases the came up while testing
121  TestCrypter::TestDecrypt(crypt, ParseHex("795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"));
122  TestCrypter::TestDecrypt(crypt, ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"));
123  TestCrypter::TestDecrypt(crypt, ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"));
124  TestCrypter::TestDecrypt(crypt, ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"));
125  TestCrypter::TestDecrypt(crypt, ParseHex("b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"));
126  TestCrypter::TestDecrypt(crypt, ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"));
127 
128  for (int i = 0; i != 100; i++)
129  {
130  uint256 hash(GetRandHash());
131  TestCrypter::TestDecrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
132  }
133  }
134 
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:43
uint256 GetRandHash()
Definition: random.cpp:373
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
Definition: crypter.cpp:75
Encryption/decryption context with key information.
Definition: crypter.h:77
static void TestEncrypt(const CCrypter &crypt, const std::vector< unsigned char > &vchPlaintextIn, const std::vector< unsigned char > &vchCiphertextCorrect=std::vector< unsigned char >())
BOOST_AUTO_TEST_CASE(passphrase_test)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:57
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: keystore.h:107
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 >())
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:93
unsigned char * begin()
Definition: uint256.h:57
unsigned char * end()
Definition: uint256.h:62
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
Definition: crypter.h:81
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
Definition: crypter.h:82
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 >())
256-bit opaque blob.
Definition: uint256.h:123
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:16
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:15
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:17
static void TestDecrypt(const CCrypter &crypt, const std::vector< unsigned char > &vchCiphertext, const std::vector< unsigned char > &vchPlaintext=std::vector< unsigned char >())
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
Definition: random.cpp:274
static void TestEncryptSingle(const CCrypter &crypt, const CKeyingMaterial &vchPlaintext, const std::vector< unsigned char > &vchCiphertextCorrect=std::vector< unsigned char >())
#define BOOST_CHECK(expr)
Definition: object.cpp:18
std::vector< unsigned char > ParseHex(const char *psz)