-
+ B559EB320AB8C5EA20BCC316649BB1B962DCCDEBC76B03289EAE91795A918553D8A19E44CCF3E2D29FB15E14CA8B8F4571AD7B1DB1516A8C1F9DA37702FC081F
bitcoin/src/crypter.h
(0 . 0)(1 . 127)
4027 // /****************************\
4028 // * EXPERIMENTAL BRANCH. *
4029 // * FOR LABORATORY USE ONLY. *
4030 // ********************************
4031 // ************
4032 // **************
4033 // ****************
4034 // **** **** ****
4035 // *** *** ***
4036 // *** *** ***
4037 // *** * * **
4038 // ******** ********
4039 // ******* ******
4040 // *** **
4041 // * ******* **
4042 // ** * * * * *
4043 // ** * * ***
4044 // **** * * * * ****
4045 // **** *** * * ** ***
4046 // **** ********* ******
4047 // ******* ***** *******
4048 // ********* ****** **
4049 // ** ****** ******
4050 // ** ******* **
4051 // ** ******* ***
4052 // **** ******** ************
4053 // ************ ************
4054 // ******** *******
4055 // ****** ****
4056 // *** ***
4057 // ********************************
4058 // Copyright (c) 2011 The Bitcoin Developers
4059 // Distributed under the MIT/X11 software license, see the accompanying
4060 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4061 #ifndef __CRYPTER_H__
4062 #define __CRYPTER_H__
4063
4064 #include "key.h"
4065
4066 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
4067 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
4068
4069 /*
4070 Private key encryption is done based on a CMasterKey,
4071 which holds a salt and random encryption key.
4072
4073 CMasterKeys are encrypted using AES-256-CBC using a key
4074 derived using derivation method nDerivationMethod
4075 (0 == EVP_sha512()) and derivation iterations nDeriveIterations.
4076 vchOtherDerivationParameters is provided for alternative algorithms
4077 which may require more parameters (such as scrypt).
4078
4079 Wallet Private Keys are then encrypted using AES-256-CBC
4080 with the double-sha256 of the public key as the IV, and the
4081 master key's key as the encryption key (see keystore.[ch]).
4082 */
4083
4084 class CMasterKey
4085 {
4086 public:
4087 std::vector<unsigned char> vchCryptedKey;
4088 std::vector<unsigned char> vchSalt;
4089 // 0 = EVP_sha512()
4090 // 1 = scrypt()
4091 unsigned int nDerivationMethod;
4092 unsigned int nDeriveIterations;
4093 // Use this for more parameters to key derivation,
4094 // such as the various parameters to scrypt
4095 std::vector<unsigned char> vchOtherDerivationParameters;
4096
4097 IMPLEMENT_SERIALIZE
4098 (
4099 READWRITE(vchCryptedKey);
4100 READWRITE(vchSalt);
4101 READWRITE(nDerivationMethod);
4102 READWRITE(nDeriveIterations);
4103 READWRITE(vchOtherDerivationParameters);
4104 )
4105 CMasterKey()
4106 {
4107 // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
4108 // ie slightly lower than the lowest hardware we need bother supporting
4109 nDeriveIterations = 25000;
4110 nDerivationMethod = 0;
4111 vchOtherDerivationParameters = std::vector<unsigned char>(0);
4112 }
4113 };
4114
4115 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
4116
4117 class CCrypter
4118 {
4119 private:
4120 unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
4121 unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
4122 bool fKeySet;
4123
4124 public:
4125 bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
4126 bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext);
4127 bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext);
4128 bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
4129
4130 void CleanKey()
4131 {
4132 memset(&chKey, 0, sizeof chKey);
4133 memset(&chIV, 0, sizeof chIV);
4134 munlock(&chKey, sizeof chKey);
4135 munlock(&chIV, sizeof chIV);
4136 fKeySet = false;
4137 }
4138
4139 CCrypter()
4140 {
4141 fKeySet = false;
4142 }
4143
4144 ~CCrypter()
4145 {
4146 CleanKey();
4147 }
4148 };
4149
4150 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
4151 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char> &vchCiphertext, const uint256& nIV, CSecret &vchPlaintext);
4152
4153 #endif