Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
crypter.h
Go to the documentation of this file.
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 #include "serialize.h"
10 
11 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
12 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
13 
14 /*
15 Private key encryption is done based on a CMasterKey,
16 which holds a salt and random encryption key.
17 
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).
23 
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]).
27 */
28 
31 {
32 public:
33  std::vector<unsigned char> vchCryptedKey;
34  std::vector<unsigned char> vchSalt;
35  // 0 = EVP_sha512()
36  // 1 = scrypt()
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;
42 
44  (
45  READWRITE(vchCryptedKey);
46  READWRITE(vchSalt);
47  READWRITE(nDerivationMethod);
48  READWRITE(nDeriveIterations);
49  READWRITE(vchOtherDerivationParameters);
50  )
51  CMasterKey()
52  {
53  // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
54  // ie slightly lower than the lowest hardware we need bother supporting
55  nDeriveIterations = 25000;
56  nDerivationMethod = 0;
57  vchOtherDerivationParameters = std::vector<unsigned char>(0);
58  }
59 };
60 
61 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
62 
64 class CCrypter
65 {
66 private:
67  unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
68  unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
69  bool fKeySet;
70 
71 public:
72  bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
73  bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext);
74  bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext);
75  bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
76 
77  void CleanKey()
78  {
79  OPENSSL_cleanse(chKey, sizeof(chKey));
80  OPENSSL_cleanse(chIV, sizeof(chIV));
81  fKeySet = false;
82  }
83 
85  {
86  fKeySet = false;
87 
88  // Try to keep the key data out of swap (and be a bit over-careful to keep the IV that we don't even use out of swap)
89  // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
90  // Note as well that at no point in this program is any attempt made to prevent stealing of keys by reading the memory of the running process.
91  LockedPageManager::instance.LockRange(&chKey[0], sizeof chKey);
92  LockedPageManager::instance.LockRange(&chIV[0], sizeof chIV);
93  }
94 
96  {
97  CleanKey();
98 
99  LockedPageManager::instance.UnlockRange(&chKey[0], sizeof chKey);
100  LockedPageManager::instance.UnlockRange(&chIV[0], sizeof chIV);
101  }
102 };
103 
104 bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
105 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
106 
107 #endif
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:15
unsigned int nDerivationMethod
Definition: crypter.h:37
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:48
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:11
#define READWRITE(obj)
Definition: serialize.h:93
static LockedPageManager instance
Definition: allocators.h:172
void LockRange(void *p, size_t size)
Definition: allocators.h:57
unsigned char chIV[WALLET_CRYPTO_KEY_SIZE]
Definition: crypter.h:68
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Definition: crypter.cpp:36
Encryption/decryption context with key information.
Definition: crypter.h:64
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:33
Master key for wallet encryption.
Definition: crypter.h:30
std::vector< unsigned char > vchOtherDerivationParameters
Definition: crypter.h:41
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:61
void CleanKey()
Definition: crypter.h:77
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:113
IMPLEMENT_SERIALIZE(READWRITE(vchCryptedKey);READWRITE(vchSalt);READWRITE(nDerivationMethod);READWRITE(nDeriveIterations);READWRITE(vchOtherDerivationParameters);) CMasterKey()
Definition: crypter.h:44
CCrypter()
Definition: crypter.h:84
bool fKeySet
Definition: crypter.h:69
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: allocators.h:269
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:75
unsigned char chKey[WALLET_CRYPTO_KEY_SIZE]
Definition: crypter.h:67
256-bit unsigned integer
Definition: uint256.h:537
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:12
void UnlockRange(void *p, size_t size)
Definition: allocators.h:80
~CCrypter()
Definition: crypter.h:95
std::vector< unsigned char > vchSalt
Definition: crypter.h:34
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:103
unsigned int nDeriveIterations
Definition: crypter.h:38