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.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef __CRYPTER_H__
  5. #define __CRYPTER_H__
  6. #include "allocators.h"
  7. #include "serialize.h"
  8. class uint256;
  9. const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
  10. const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
  11. /*
  12. Private key encryption is done based on a CMasterKey,
  13. which holds a salt and random encryption key.
  14. CMasterKeys are encrypted using AES-256-CBC using a key
  15. derived using derivation method nDerivationMethod
  16. (0 == EVP_sha512()) and derivation iterations nDeriveIterations.
  17. vchOtherDerivationParameters is provided for alternative algorithms
  18. which may require more parameters (such as scrypt).
  19. Wallet Private Keys are then encrypted using AES-256-CBC
  20. with the double-sha256 of the public key as the IV, and the
  21. master key's key as the encryption key (see keystore.[ch]).
  22. */
  23. /** Master key for wallet encryption */
  24. class CMasterKey
  25. {
  26. public:
  27. std::vector<unsigned char> vchCryptedKey;
  28. std::vector<unsigned char> vchSalt;
  29. // 0 = EVP_sha512()
  30. // 1 = scrypt()
  31. unsigned int nDerivationMethod;
  32. unsigned int nDeriveIterations;
  33. // Use this for more parameters to key derivation,
  34. // such as the various parameters to scrypt
  35. std::vector<unsigned char> vchOtherDerivationParameters;
  36. IMPLEMENT_SERIALIZE
  37. (
  38. READWRITE(vchCryptedKey);
  39. READWRITE(vchSalt);
  40. READWRITE(nDerivationMethod);
  41. READWRITE(nDeriveIterations);
  42. READWRITE(vchOtherDerivationParameters);
  43. )
  44. CMasterKey()
  45. {
  46. // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
  47. // ie slightly lower than the lowest hardware we need bother supporting
  48. nDeriveIterations = 25000;
  49. nDerivationMethod = 0;
  50. vchOtherDerivationParameters = std::vector<unsigned char>(0);
  51. }
  52. };
  53. typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
  54. /** Encryption/decryption context with key information */
  55. class CCrypter
  56. {
  57. private:
  58. unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
  59. unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
  60. bool fKeySet;
  61. public:
  62. bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
  63. bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext);
  64. bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext);
  65. bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
  66. void CleanKey()
  67. {
  68. OPENSSL_cleanse(chKey, sizeof(chKey));
  69. OPENSSL_cleanse(chIV, sizeof(chIV));
  70. fKeySet = false;
  71. }
  72. CCrypter()
  73. {
  74. fKeySet = false;
  75. // Try to keep the key data out of swap (and be a bit over-careful to keep the IV that we don't even use out of swap)
  76. // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
  77. // Note as well that at no point in this program is any attempt made to prevent stealing of keys by reading the memory of the running process.
  78. LockedPageManager::Instance().LockRange(&chKey[0], sizeof chKey);
  79. LockedPageManager::Instance().LockRange(&chIV[0], sizeof chIV);
  80. }
  81. ~CCrypter()
  82. {
  83. CleanKey();
  84. LockedPageManager::Instance().UnlockRange(&chKey[0], sizeof chKey);
  85. LockedPageManager::Instance().UnlockRange(&chIV[0], sizeof chIV);
  86. }
  87. };
  88. bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
  89. bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
  90. #endif