PageRenderTime 28ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/wallet/test/crypto_tests.cpp

https://github.com/djpnewton/bitcoin
C++ | 238 lines | 175 code | 52 blank | 11 comment | 57 complexity | f6c662dde464556dc6bb7daf13ef31c9 MD5 | raw 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. #include "test/test_random.h"
  5. #include "utilstrencodings.h"
  6. #include "test/test_bitcoin.h"
  7. #include "wallet/crypter.h"
  8. #include <vector>
  9. #include <boost/test/unit_test.hpp>
  10. #include <openssl/aes.h>
  11. #include <openssl/evp.h>
  12. BOOST_FIXTURE_TEST_SUITE(wallet_crypto, BasicTestingSetup)
  13. 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)
  14. {
  15. if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
  16. return false;
  17. int i = 0;
  18. if (nDerivationMethod == 0)
  19. i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
  20. (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
  21. if (i != (int)WALLET_CRYPTO_KEY_SIZE)
  22. {
  23. memory_cleanse(chKey, sizeof(chKey));
  24. memory_cleanse(chIV, sizeof(chIV));
  25. return false;
  26. }
  27. return true;
  28. }
  29. bool OldEncrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext, const unsigned char chKey[32], const unsigned char chIV[16])
  30. {
  31. // max ciphertext len for a n bytes of plaintext is
  32. // n + AES_BLOCK_SIZE - 1 bytes
  33. int nLen = vchPlaintext.size();
  34. int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
  35. vchCiphertext = std::vector<unsigned char> (nCLen);
  36. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  37. if (!ctx) return false;
  38. bool fOk = true;
  39. EVP_CIPHER_CTX_init(ctx);
  40. if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
  41. if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
  42. if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
  43. EVP_CIPHER_CTX_cleanup(ctx);
  44. EVP_CIPHER_CTX_free(ctx);
  45. if (!fOk) return false;
  46. vchCiphertext.resize(nCLen + nFLen);
  47. return true;
  48. }
  49. bool OldDecrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext, const unsigned char chKey[32], const unsigned char chIV[16])
  50. {
  51. // plaintext will always be equal to or lesser than length of ciphertext
  52. int nLen = vchCiphertext.size();
  53. int nPLen = nLen, nFLen = 0;
  54. vchPlaintext = CKeyingMaterial(nPLen);
  55. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  56. if (!ctx) return false;
  57. bool fOk = true;
  58. EVP_CIPHER_CTX_init(ctx);
  59. if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
  60. if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
  61. if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
  62. EVP_CIPHER_CTX_cleanup(ctx);
  63. EVP_CIPHER_CTX_free(ctx);
  64. if (!fOk) return false;
  65. vchPlaintext.resize(nPLen + nFLen);
  66. return true;
  67. }
  68. class TestCrypter
  69. {
  70. public:
  71. static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
  72. const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
  73. const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
  74. {
  75. unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
  76. unsigned char chIV[WALLET_CRYPTO_IV_SIZE];
  77. CCrypter crypt;
  78. crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
  79. OldSetKeyFromPassphrase(passphrase, vchSalt, rounds, 0, chKey, chIV);
  80. BOOST_CHECK_MESSAGE(memcmp(chKey, crypt.vchKey.data(), crypt.vchKey.size()) == 0, \
  81. HexStr(chKey, chKey+sizeof(chKey)) + std::string(" != ") + HexStr(crypt.vchKey));
  82. BOOST_CHECK_MESSAGE(memcmp(chIV, crypt.vchIV.data(), crypt.vchIV.size()) == 0, \
  83. HexStr(chIV, chIV+sizeof(chIV)) + std::string(" != ") + HexStr(crypt.vchIV));
  84. if(!correctKey.empty())
  85. BOOST_CHECK_MESSAGE(memcmp(chKey, &correctKey[0], sizeof(chKey)) == 0, \
  86. HexStr(chKey, chKey+sizeof(chKey)) + std::string(" != ") + HexStr(correctKey.begin(), correctKey.end()));
  87. if(!correctIV.empty())
  88. BOOST_CHECK_MESSAGE(memcmp(chIV, &correctIV[0], sizeof(chIV)) == 0,
  89. HexStr(chIV, chIV+sizeof(chIV)) + std::string(" != ") + HexStr(correctIV.begin(), correctIV.end()));
  90. }
  91. static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
  92. const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
  93. const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
  94. {
  95. TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
  96. for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
  97. TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
  98. }
  99. static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, \
  100. const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
  101. {
  102. CKeyingMaterial vchDecrypted1;
  103. CKeyingMaterial vchDecrypted2;
  104. int result1, result2;
  105. result1 = crypt.Decrypt(vchCiphertext, vchDecrypted1);
  106. result2 = OldDecrypt(vchCiphertext, vchDecrypted2, crypt.vchKey.data(), crypt.vchIV.data());
  107. BOOST_CHECK(result1 == result2);
  108. // These two should be equal. However, OpenSSL 1.0.1j introduced a change
  109. // that would zero all padding except for the last byte for failed decrypts.
  110. // This behavior was reverted for 1.0.1k.
  111. if (vchDecrypted1 != vchDecrypted2 && vchDecrypted1.size() >= AES_BLOCK_SIZE && SSLeay() == 0x100010afL)
  112. {
  113. for(CKeyingMaterial::iterator it = vchDecrypted1.end() - AES_BLOCK_SIZE; it != vchDecrypted1.end() - 1; it++)
  114. *it = 0;
  115. }
  116. BOOST_CHECK_MESSAGE(vchDecrypted1 == vchDecrypted2, HexStr(vchDecrypted1.begin(), vchDecrypted1.end()) + " != " + HexStr(vchDecrypted2.begin(), vchDecrypted2.end()));
  117. if (vchPlaintext.size())
  118. BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted2);
  119. }
  120. static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
  121. const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
  122. {
  123. std::vector<unsigned char> vchCiphertext1;
  124. std::vector<unsigned char> vchCiphertext2;
  125. int result1 = crypt.Encrypt(vchPlaintext, vchCiphertext1);
  126. int result2 = OldEncrypt(vchPlaintext, vchCiphertext2, crypt.vchKey.data(), crypt.vchIV.data());
  127. BOOST_CHECK(result1 == result2);
  128. BOOST_CHECK(vchCiphertext1 == vchCiphertext2);
  129. if (!vchCiphertextCorrect.empty())
  130. BOOST_CHECK(vchCiphertext2 == vchCiphertextCorrect);
  131. const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
  132. if(vchCiphertext1 == vchCiphertext2)
  133. TestDecrypt(crypt, vchCiphertext1, vchPlaintext2);
  134. }
  135. static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, \
  136. const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
  137. {
  138. TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect);
  139. for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
  140. TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
  141. }
  142. };
  143. BOOST_AUTO_TEST_CASE(passphrase) {
  144. // These are expensive.
  145. TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \
  146. ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
  147. ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
  148. std::string hash(GetRandHash().ToString());
  149. std::vector<unsigned char> vchSalt(8);
  150. GetRandBytes(&vchSalt[0], vchSalt.size());
  151. uint32_t rounds = insecure_rand();
  152. if (rounds > 30000)
  153. rounds = 30000;
  154. TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);
  155. }
  156. BOOST_AUTO_TEST_CASE(encrypt) {
  157. std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
  158. BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
  159. CCrypter crypt;
  160. crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
  161. TestCrypter::TestEncrypt(crypt, ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"));
  162. for (int i = 0; i != 100; i++)
  163. {
  164. uint256 hash(GetRandHash());
  165. TestCrypter::TestEncrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
  166. }
  167. }
  168. BOOST_AUTO_TEST_CASE(decrypt) {
  169. std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
  170. BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
  171. CCrypter crypt;
  172. crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
  173. // Some corner cases the came up while testing
  174. TestCrypter::TestDecrypt(crypt,ParseHex("795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"));
  175. TestCrypter::TestDecrypt(crypt,ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"));
  176. TestCrypter::TestDecrypt(crypt,ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"));
  177. TestCrypter::TestDecrypt(crypt,ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"));
  178. TestCrypter::TestDecrypt(crypt,ParseHex("b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"));
  179. TestCrypter::TestDecrypt(crypt,ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"));
  180. for (int i = 0; i != 100; i++)
  181. {
  182. uint256 hash(GetRandHash());
  183. TestCrypter::TestDecrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
  184. }
  185. }
  186. BOOST_AUTO_TEST_SUITE_END()