Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

crypto_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <test/test_dash.h>
6 #include <utilstrencodings.h>
7 #include <wallet/crypter.h>
8 
9 #include <vector>
10 
11 #include <boost/test/unit_test.hpp>
12 
13 BOOST_FIXTURE_TEST_SUITE(wallet_crypto, BasicTestingSetup)
14 
15 void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, const std::string &hexin, const std::string &hexout)
16 {
17  std::vector<unsigned char> key = ParseHex(hexkey);
18  std::vector<unsigned char> iv = ParseHex(hexiv);
19  std::vector<unsigned char> in = ParseHex(hexin);
20  std::vector<unsigned char> correctout = ParseHex(hexout);
21 
22  SecureString sKey(key.begin(), key.end()), sPlaintextIn(in.begin(), in.end()), sPlaintextOut, sPlaintextOutOld;
23  std::string sIv(iv.begin(), iv.end()), sCiphertextIn(correctout.begin(), correctout.end()), sCiphertextOut, sCiphertextOutOld;
24  BOOST_CHECK_MESSAGE(EncryptAES256(sKey, sPlaintextIn, sIv, sCiphertextOut), "EncryptAES256: " + HexStr(sCiphertextOut) + std::string(" != ") + hexout);
25  BOOST_CHECK_MESSAGE(DecryptAES256(sKey, sCiphertextIn, sIv, sPlaintextOut), "DecryptAES256: " + HexStr(sPlaintextOut) + std::string(" != ") + hexin);
26 }
27 
29 {
30 public:
31 static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
32  const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
33  const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
34 {
35  CCrypter crypt;
36  crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
37 
38  if(!correctKey.empty())
39  BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \
40  HexStr(crypt.vchKey.begin(), crypt.vchKey.end()) + std::string(" != ") + HexStr(correctKey.begin(), correctKey.end()));
41  if(!correctIV.empty())
42  BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
43  HexStr(crypt.vchIV.begin(), crypt.vchIV.end()) + std::string(" != ") + HexStr(correctIV.begin(), correctIV.end()));
44 }
45 
46 static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
47  const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
48  const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
49 {
50  TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
51  for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
52  TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
53 }
54 
55 static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, \
56  const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
57 {
58  CKeyingMaterial vchDecrypted;
59  crypt.Decrypt(vchCiphertext, vchDecrypted);
60  if (vchPlaintext.size())
61  BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted);
62 }
63 
64 static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
65  const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
66 {
67  std::vector<unsigned char> vchCiphertext;
68  crypt.Encrypt(vchPlaintext, vchCiphertext);
69 
70  if (!vchCiphertextCorrect.empty())
71  BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
72 
73  const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
74  TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
75 }
76 
77 static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, \
78  const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
79 {
80  TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect);
81  for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
82  TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
83 }
84 
85 };
86 
87 BOOST_AUTO_TEST_CASE(passphrase) {
88  // These are expensive.
89 
90  TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \
91  ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
92  ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
93 
94  std::string hash(GetRandHash().ToString());
95  std::vector<unsigned char> vchSalt(8);
96  GetRandBytes(vchSalt.data(), vchSalt.size());
97  uint32_t rounds = InsecureRand32();
98  if (rounds > 30000)
99  rounds = 30000;
100  TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);
101 }
102 
104  std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
105  BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
106  CCrypter crypt;
107  crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
108  TestCrypter::TestEncrypt(crypt, ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"));
109 
110  for (int i = 0; i != 100; i++)
111  {
112  uint256 hash(GetRandHash());
113  TestCrypter::TestEncrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
114  }
115 
116 }
117 
119  std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
120  BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
121  CCrypter crypt;
122  crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
123 
124  // Some corner cases the came up while testing
125  TestCrypter::TestDecrypt(crypt,ParseHex("795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"));
126  TestCrypter::TestDecrypt(crypt,ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"));
127  TestCrypter::TestDecrypt(crypt,ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"));
128  TestCrypter::TestDecrypt(crypt,ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"));
129  TestCrypter::TestDecrypt(crypt,ParseHex("b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"));
130  TestCrypter::TestDecrypt(crypt,ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"));
131 
132  for (int i = 0; i != 100; i++)
133  {
134  uint256 hash(GetRandHash());
135  TestCrypter::TestDecrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
136  }
137 }
138 
139 BOOST_AUTO_TEST_CASE(aes_256_cbc_testvectors) {
140  // NIST AES CBC 256-bit encryption test-vectors with padding enabled
141  TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
142  "000102030405060708090A0B0C0D0E0F", "6bc1bee22e409f96e93d7e117393172a", \
143  "f58c4c04d6e5f1ba779eabfb5f7bfbd6485a5c81519cf378fa36d42b8547edc0");
144  TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
145  "F58C4C04D6E5F1BA779EABFB5F7BFBD6", "ae2d8a571e03ac9c9eb76fac45af8e51", \
146  "9cfc4e967edb808d679f777bc6702c7d3a3aa5e0213db1a9901f9036cf5102d2");
147  TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
148  "9CFC4E967EDB808D679F777BC6702C7D", "30c81c46a35ce411e5fbc1191a0a52ef",
149  "39f23369a9d9bacfa530e263042314612f8da707643c90a6f732b3de1d3f5cee");
150  TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
151  "39F23369A9D9BACFA530E26304231461", "f69f2445df4f9b17ad2b417be66c3710", \
152  "b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644");
153 }
154 
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:42
uint256 GetRandHash()
Definition: random.cpp:384
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
Definition: crypter.cpp:74
bool EncryptAES256(const SecureString &sKey, const SecureString &sPlaintext, const std::string &sIV, std::string &sCiphertext)
Definition: crypter.cpp:123
Encryption/decryption context with key information.
Definition: crypter.h:76
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
Definition: secure.h:57
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: keystore.h:85
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 >())
Definition: box.hpp:161
BOOST_AUTO_TEST_CASE(passphrase)
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:92
unsigned char * begin()
Definition: uint256.h:57
bool DecryptAES256(const SecureString &sKey, const std::string &sCiphertext, const std::string &sIV, SecureString &sPlaintext)
Definition: crypter.cpp:155
unsigned char * end()
Definition: uint256.h:62
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
Definition: crypter.h:80
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
Definition: crypter.h:81
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:15
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
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:273
void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, const std::string &hexin, const std::string &hexout)
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:17
std::vector< unsigned char > ParseHex(const char *psz)
Released under the MIT license