crypter.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // ECOin - Copyright (c) - 2014/2021 - 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. if (!fOk) return false;
  61. vchCiphertext.resize(nCLen + nFLen);
  62. return true;
  63. }
  64. bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
  65. {
  66. if (!fKeySet)
  67. return false;
  68. int nLen = vchCiphertext.size();
  69. int nPLen = nLen, nFLen = 0;
  70. vchPlaintext = CKeyingMaterial(nPLen);
  71. EVP_CIPHER_CTX ctx;
  72. bool fOk = true;
  73. EVP_CIPHER_CTX_init(&ctx);
  74. if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
  75. if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
  76. if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
  77. EVP_CIPHER_CTX_cleanup(&ctx);
  78. if (!fOk) return false;
  79. vchPlaintext.resize(nPLen + nFLen);
  80. return true;
  81. }
  82. bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
  83. {
  84. CCrypter cKeyCrypter;
  85. std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
  86. memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
  87. if(!cKeyCrypter.SetKey(vMasterKey, chIV))
  88. return false;
  89. return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
  90. }
  91. bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
  92. {
  93. CCrypter cKeyCrypter;
  94. std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
  95. memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
  96. if(!cKeyCrypter.SetKey(vMasterKey, chIV))
  97. return false;
  98. return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
  99. }