Projects : bitcoin : bitcoin_dumpblock_no_losers
1 | // Copyright (c) 2011 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 | |
7 | #include "key.h" |
8 | |
9 | const unsigned int WALLET_CRYPTO_KEY_SIZE = 32; |
10 | const unsigned int WALLET_CRYPTO_SALT_SIZE = 8; |
11 | |
12 | /* |
13 | Private key encryption is done based on a CMasterKey, |
14 | which holds a salt and random encryption key. |
15 | |
16 | CMasterKeys are encrypted using AES-256-CBC using a key |
17 | derived using derivation method nDerivationMethod |
18 | (0 == EVP_sha512()) and derivation iterations nDeriveIterations. |
19 | vchOtherDerivationParameters is provided for alternative algorithms |
20 | which may require more parameters (such as scrypt). |
21 | |
22 | Wallet Private Keys are then encrypted using AES-256-CBC |
23 | with the double-sha256 of the public key as the IV, and the |
24 | master key's key as the encryption key (see keystore.[ch]). |
25 | */ |
26 | |
27 | class CMasterKey |
28 | { |
29 | public: |
30 | std::vector<unsigned char> vchCryptedKey; |
31 | std::vector<unsigned char> vchSalt; |
32 | // 0 = EVP_sha512() |
33 | // 1 = scrypt() |
34 | unsigned int nDerivationMethod; |
35 | unsigned int nDeriveIterations; |
36 | // Use this for more parameters to key derivation, |
37 | // such as the various parameters to scrypt |
38 | std::vector<unsigned char> vchOtherDerivationParameters; |
39 | |
40 | IMPLEMENT_SERIALIZE |
41 | ( |
42 | READWRITE(vchCryptedKey); |
43 | READWRITE(vchSalt); |
44 | READWRITE(nDerivationMethod); |
45 | READWRITE(nDeriveIterations); |
46 | READWRITE(vchOtherDerivationParameters); |
47 | ) |
48 | CMasterKey() |
49 | { |
50 | // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M |
51 | // ie slightly lower than the lowest hardware we need bother supporting |
52 | nDeriveIterations = 25000; |
53 | nDerivationMethod = 0; |
54 | vchOtherDerivationParameters = std::vector<unsigned char>(0); |
55 | } |
56 | }; |
57 | |
58 | typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial; |
59 | |
60 | class CCrypter |
61 | { |
62 | private: |
63 | unsigned char chKey[WALLET_CRYPTO_KEY_SIZE]; |
64 | unsigned char chIV[WALLET_CRYPTO_KEY_SIZE]; |
65 | bool fKeySet; |
66 | |
67 | public: |
68 | bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod); |
69 | bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext); |
70 | bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext); |
71 | bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV); |
72 | |
73 | void CleanKey() |
74 | { |
75 | memset(&chKey, 0, sizeof chKey); |
76 | memset(&chIV, 0, sizeof chIV); |
77 | munlock(&chKey, sizeof chKey); |
78 | munlock(&chIV, sizeof chIV); |
79 | fKeySet = false; |
80 | } |
81 | |
82 | CCrypter() |
83 | { |
84 | fKeySet = false; |
85 | } |
86 | |
87 | ~CCrypter() |
88 | { |
89 | CleanKey(); |
90 | } |
91 | }; |
92 | |
93 | bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext); |
94 | bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char> &vchCiphertext, const uint256& nIV, CSecret &vchPlaintext); |
95 | |
96 | #endif |