crypter.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef __CRYPTER_H__
  3. #define __CRYPTER_H__
  4. #include "allocators.h" /* for SecureString */
  5. #include "key.h"
  6. #include "serialize.h"
  7. const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
  8. const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
  9. class CMasterKey
  10. {
  11. public:
  12. std::vector<unsigned char> vchCryptedKey;
  13. std::vector<unsigned char> vchSalt;
  14. // 0 = EVP_sha512()
  15. // 1 = scrypt()
  16. unsigned int nDerivationMethod;
  17. unsigned int nDeriveIterations;
  18. std::vector<unsigned char> vchOtherDerivationParameters;
  19. IMPLEMENT_SERIALIZE
  20. (
  21. READWRITE(vchCryptedKey);
  22. READWRITE(vchSalt);
  23. READWRITE(nDerivationMethod);
  24. READWRITE(nDeriveIterations);
  25. READWRITE(vchOtherDerivationParameters);
  26. )
  27. CMasterKey()
  28. {
  29. // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
  30. // ie slightly lower than the lowest hardware we need bother supporting
  31. nDeriveIterations = 25000;
  32. nDerivationMethod = 1;
  33. vchOtherDerivationParameters = std::vector<unsigned char>(0);
  34. }
  35. CMasterKey(unsigned int nDerivationMethodIndex)
  36. {
  37. switch (nDerivationMethodIndex)
  38. {
  39. case 0: // sha512
  40. default:
  41. nDeriveIterations = 25000;
  42. nDerivationMethod = 0;
  43. vchOtherDerivationParameters = std::vector<unsigned char>(0);
  44. break;
  45. case 1: // scrypt+sha512
  46. nDeriveIterations = 10000;
  47. nDerivationMethod = 1;
  48. vchOtherDerivationParameters = std::vector<unsigned char>(0);
  49. break;
  50. }
  51. }
  52. };
  53. typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
  54. class CCrypter
  55. {
  56. private:
  57. unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
  58. unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
  59. bool fKeySet;
  60. public:
  61. bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
  62. bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext);
  63. bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext);
  64. bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
  65. void CleanKey()
  66. {
  67. OPENSSL_cleanse(&chKey, sizeof chKey);
  68. OPENSSL_cleanse(&chIV, sizeof chIV);
  69. fKeySet = false;
  70. }
  71. CCrypter()
  72. {
  73. fKeySet = false;
  74. LockedPageManager::instance.LockRange(&chKey[0], sizeof chKey);
  75. LockedPageManager::instance.LockRange(&chIV[0], sizeof chIV);
  76. }
  77. ~CCrypter()
  78. {
  79. CleanKey();
  80. LockedPageManager::instance.UnlockRange(&chKey[0], sizeof chKey);
  81. LockedPageManager::instance.UnlockRange(&chIV[0], sizeof chIV);
  82. }
  83. };
  84. bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
  85. bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char> &vchCiphertext, const uint256& nIV, CSecret &vchPlaintext);
  86. #endif