1 // Copyright (c) 2009-2012 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.
7 #include "allocators.h" /* for SecureString */
11 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
12 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
15 Private key encryption is done based on a CMasterKey,
16 which holds a salt and random encryption key.
18 CMasterKeys are encrypted using AES-256-CBC using a key
19 derived using derivation method nDerivationMethod
20 (0 == EVP_sha512()) and derivation iterations nDeriveIterations.
21 vchOtherDerivationParameters is provided for alternative algorithms
22 which may require more parameters (such as scrypt).
24 Wallet Private Keys are then encrypted using AES-256-CBC
25 with the double-sha256 of the public key as the IV, and the
26 master key's key as the encryption key (see keystore.[ch]).
29 /** Master key for wallet encryption */
33 std::vector<unsigned char> vchCryptedKey;
34 std::vector<unsigned char> vchSalt;
37 unsigned int nDerivationMethod;
38 unsigned int nDeriveIterations;
39 // Use this for more parameters to key derivation,
40 // such as the various parameters to scrypt
41 std::vector<unsigned char> vchOtherDerivationParameters;
45 READWRITE(vchCryptedKey);
47 READWRITE(nDerivationMethod);
48 READWRITE(nDeriveIterations);
49 READWRITE(vchOtherDerivationParameters);
54 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
56 /** Encryption/decryption context with key information */
60 unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
61 unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
65 bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
66 bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext);
67 bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext);
68 bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
74 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
75 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char> &vchCiphertext, const uint256& nIV, CSecret &vchPlaintext);