You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypter.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2009-2013 The Bitcoin developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "crypter.h"
  5. #include <string>
  6. #include <vector>
  7. #include <openssl/aes.h>
  8. #include <openssl/evp.h>
  9. bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
  10. {
  11. if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
  12. return false;
  13. int i = 0;
  14. if (nDerivationMethod == 0)
  15. i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
  16. (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
  17. if (i != (int)WALLET_CRYPTO_KEY_SIZE)
  18. {
  19. OPENSSL_cleanse(chKey, sizeof(chKey));
  20. OPENSSL_cleanse(chIV, sizeof(chIV));
  21. return false;
  22. }
  23. fKeySet = true;
  24. return true;
  25. }
  26. bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
  27. {
  28. if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
  29. return false;
  30. memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
  31. memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
  32. fKeySet = true;
  33. return true;
  34. }
  35. bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
  36. {
  37. if (!fKeySet)
  38. return false;
  39. // max ciphertext len for a n bytes of plaintext is
  40. // n + AES_BLOCK_SIZE - 1 bytes
  41. int nLen = vchPlaintext.size();
  42. int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
  43. vchCiphertext = std::vector<unsigned char> (nCLen);
  44. EVP_CIPHER_CTX ctx;
  45. bool fOk = true;
  46. EVP_CIPHER_CTX_init(&ctx);
  47. if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
  48. if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
  49. if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
  50. EVP_CIPHER_CTX_cleanup(&ctx);
  51. if (!fOk) return false;
  52. vchCiphertext.resize(nCLen + nFLen);
  53. return true;
  54. }
  55. bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
  56. {
  57. if (!fKeySet)
  58. return false;
  59. // plaintext will always be equal to or lesser than length of ciphertext
  60. int nLen = vchCiphertext.size();
  61. int nPLen = nLen, nFLen = 0;
  62. vchPlaintext = CKeyingMaterial(nPLen);
  63. EVP_CIPHER_CTX ctx;
  64. bool fOk = true;
  65. EVP_CIPHER_CTX_init(&ctx);
  66. if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
  67. if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
  68. if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
  69. EVP_CIPHER_CTX_cleanup(&ctx);
  70. if (!fOk) return false;
  71. vchPlaintext.resize(nPLen + nFLen);
  72. return true;
  73. }
  74. bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
  75. {
  76. CCrypter cKeyCrypter;
  77. std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
  78. memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
  79. if(!cKeyCrypter.SetKey(vMasterKey, chIV))
  80. return false;
  81. return cKeyCrypter.Encrypt(*((const CKeyingMaterial*)&vchPlaintext), vchCiphertext);
  82. }
  83. bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
  84. {
  85. CCrypter cKeyCrypter;
  86. std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
  87. memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
  88. if(!cKeyCrypter.SetKey(vMasterKey, chIV))
  89. return false;
  90. return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
  91. }