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