crypter.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include <openssl/aes.h>
  3. #include <openssl/evp.h>
  4. #include <vector>
  5. #include <string>
  6. #ifdef WIN32
  7. #include <windows.h>
  8. #endif
  9. #include "crypter.h"
  10. #include "scrypt.h"
  11. bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
  12. {
  13. if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
  14. return false;
  15. int i = 0;
  16. if (nDerivationMethod == 0)
  17. {
  18. i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
  19. (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
  20. }
  21. if (nDerivationMethod == 1)
  22. {
  23. uint256 scryptHash = scrypt_salted_multiround_hash((const void*)strKeyData.c_str(), strKeyData.size(), &chSalt[0], 8, nRounds);
  24. i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
  25. (unsigned char *)&scryptHash, sizeof scryptHash, nRounds, chKey, chIV);
  26. OPENSSL_cleanse(&scryptHash, sizeof scryptHash);
  27. }
  28. if (i != (int)WALLET_CRYPTO_KEY_SIZE)
  29. {
  30. OPENSSL_cleanse(&chKey, sizeof chKey);
  31. OPENSSL_cleanse(&chIV, sizeof chIV);
  32. return false;
  33. }
  34. fKeySet = true;
  35. return true;
  36. }
  37. bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
  38. {
  39. if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
  40. return false;
  41. memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
  42. memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
  43. fKeySet = true;
  44. return true;
  45. }
  46. bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
  47. {
  48. if (!fKeySet)
  49. return false;
  50. int nLen = vchPlaintext.size();
  51. int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
  52. vchCiphertext = std::vector<unsigned char> (nCLen);
  53. EVP_CIPHER_CTX *ctx;
  54. bool fOk = true;
  55. /* EVP_CIPHER_CTX_init(&ctx);
  56. if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
  57. if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
  58. if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
  59. EVP_CIPHER_CTX_cleanup(&ctx);
  60. */
  61. EVP_CIPHER_CTX_init(ctx);
  62. if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
  63. if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
  64. if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0])+nCLen, &nFLen);
  65. EVP_CIPHER_CTX_cleanup(ctx);
  66. if (!fOk) return false;
  67. vchCiphertext.resize(nCLen + nFLen);
  68. return true;
  69. }
  70. bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
  71. {
  72. if (!fKeySet)
  73. return false;
  74. int nLen = vchCiphertext.size();
  75. int nPLen = nLen, nFLen = 0;
  76. vchPlaintext = CKeyingMaterial(nPLen);
  77. //EVP_CIPHER_CTX ctx;
  78. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  79. bool fOk = true;
  80. /* EVP_CIPHER_CTX_init(&ctx);
  81. if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
  82. if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
  83. if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
  84. EVP_CIPHER_CTX_cleanup(&ctx);
  85. */
  86. EVP_CIPHER_CTX_init(ctx);
  87. if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
  88. if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
  89. if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0])+nPLen, &nFLen);
  90. EVP_CIPHER_CTX_cleanup(ctx);
  91. if (!fOk) return false;
  92. vchPlaintext.resize(nPLen + nFLen);
  93. return true;
  94. }
  95. bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
  96. {
  97. CCrypter cKeyCrypter;
  98. std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
  99. memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
  100. if(!cKeyCrypter.SetKey(vMasterKey, chIV))
  101. return false;
  102. return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
  103. }
  104. bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
  105. {
  106. CCrypter cKeyCrypter;
  107. std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
  108. memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
  109. if(!cKeyCrypter.SetKey(vMasterKey, chIV))
  110. return false;
  111. return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
  112. }