-
+ 03B2BADB8A68951E41FDDEA52AA38E5FB3838F1C7F6A9EB7C7297A32455176FA1F75DF422DD6A9E41E8E75D61D368D5EF62CC7E558F4D4C32E3CB437A685BE2A
bitcoin/src/crypter.h
(0 . 0)(1 . 96)
3748 // Copyright (c) 2011 The Bitcoin Developers
3749 // Distributed under the MIT/X11 software license, see the accompanying
3750 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
3751 #ifndef __CRYPTER_H__
3752 #define __CRYPTER_H__
3753
3754 #include "key.h"
3755
3756 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
3757 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
3758
3759 /*
3760 Private key encryption is done based on a CMasterKey,
3761 which holds a salt and random encryption key.
3762
3763 CMasterKeys are encrypted using AES-256-CBC using a key
3764 derived using derivation method nDerivationMethod
3765 (0 == EVP_sha512()) and derivation iterations nDeriveIterations.
3766 vchOtherDerivationParameters is provided for alternative algorithms
3767 which may require more parameters (such as scrypt).
3768
3769 Wallet Private Keys are then encrypted using AES-256-CBC
3770 with the double-sha256 of the public key as the IV, and the
3771 master key's key as the encryption key (see keystore.[ch]).
3772 */
3773
3774 class CMasterKey
3775 {
3776 public:
3777 std::vector<unsigned char> vchCryptedKey;
3778 std::vector<unsigned char> vchSalt;
3779 // 0 = EVP_sha512()
3780 // 1 = scrypt()
3781 unsigned int nDerivationMethod;
3782 unsigned int nDeriveIterations;
3783 // Use this for more parameters to key derivation,
3784 // such as the various parameters to scrypt
3785 std::vector<unsigned char> vchOtherDerivationParameters;
3786
3787 IMPLEMENT_SERIALIZE
3788 (
3789 READWRITE(vchCryptedKey);
3790 READWRITE(vchSalt);
3791 READWRITE(nDerivationMethod);
3792 READWRITE(nDeriveIterations);
3793 READWRITE(vchOtherDerivationParameters);
3794 )
3795 CMasterKey()
3796 {
3797 // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
3798 // ie slightly lower than the lowest hardware we need bother supporting
3799 nDeriveIterations = 25000;
3800 nDerivationMethod = 0;
3801 vchOtherDerivationParameters = std::vector<unsigned char>(0);
3802 }
3803 };
3804
3805 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
3806
3807 class CCrypter
3808 {
3809 private:
3810 unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
3811 unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
3812 bool fKeySet;
3813
3814 public:
3815 bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
3816 bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext);
3817 bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext);
3818 bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
3819
3820 void CleanKey()
3821 {
3822 memset(&chKey, 0, sizeof chKey);
3823 memset(&chIV, 0, sizeof chIV);
3824 munlock(&chKey, sizeof chKey);
3825 munlock(&chIV, sizeof chIV);
3826 fKeySet = false;
3827 }
3828
3829 CCrypter()
3830 {
3831 fKeySet = false;
3832 }
3833
3834 ~CCrypter()
3835 {
3836 CleanKey();
3837 }
3838 };
3839
3840 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
3841 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char> &vchCiphertext, const uint256& nIV, CSecret &vchPlaintext);
3842
3843 #endif