experimental-genesis 1
experimental-genesis 2
experimental-genesis 3
experimental-genesis 4
experimental-genesis 5
experimental-genesis 6
experimental-genesis 7
experimental-genesis 8
experimental-genesis 9
experimental-genesis 10
experimental-genesis 11
experimental-genesis 12
experimental-genesis 13
experimental-genesis 14
experimental-genesis 15
experimental-genesis 16
experimental-genesis 17
experimental-genesis 18
experimental-genesis 19
experimental-genesis 20
experimental-genesis 21
experimental-genesis 22
experimental-genesis 23
experimental-genesis 24
experimental-genesis 25
experimental-genesis 26
experimental-genesis 27
experimental-genesis 28
experimental-genesis 29
experimental-genesis 30
experimental-genesis 31
experimental-genesis 32
experimental-genesis 33
experimental-genesis 34
experimental-genesis 35 #ifndef __CRYPTER_H__
experimental-genesis 36 #define __CRYPTER_H__
experimental-genesis 37
experimental-genesis 38 #include "key.h"
experimental-genesis 39
experimental-genesis 40 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
experimental-genesis 41 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
experimental-genesis 42
experimental-genesis 43
experimental-genesis 44 Private key encryption is done based on a CMasterKey,
experimental-genesis 45 which holds a salt and random encryption key.
experimental-genesis 46
experimental-genesis 47 CMasterKeys are encrypted using AES-256-CBC using a key
experimental-genesis 48 derived using derivation method nDerivationMethod
experimental-genesis 49 (0 == EVP_sha512()) and derivation iterations nDeriveIterations.
experimental-genesis 50 vchOtherDerivationParameters is provided for alternative algorithms
experimental-genesis 51 which may require more parameters (such as scrypt).
experimental-genesis 52
experimental-genesis 53 Wallet Private Keys are then encrypted using AES-256-CBC
experimental-genesis 54 with the double-sha256 of the public key as the IV, and the
experimental-genesis 55 master key's key as the encryption key (see keystore.[ch]).
experimental-genesis 56 */
experimental-genesis 57
experimental-genesis 58 class CMasterKey
experimental-genesis 59 {
experimental-genesis 60 public:
experimental-genesis 61 std::vector<unsigned char> vchCryptedKey;
experimental-genesis 62 std::vector<unsigned char> vchSalt;
experimental-genesis 63
experimental-genesis 64
experimental-genesis 65 unsigned int nDerivationMethod;
experimental-genesis 66 unsigned int nDeriveIterations;
experimental-genesis 67
experimental-genesis 68
experimental-genesis 69 std::vector<unsigned char> vchOtherDerivationParameters;
experimental-genesis 70
experimental-genesis 71 IMPLEMENT_SERIALIZE
experimental-genesis 72 (
experimental-genesis 73 READWRITE(vchCryptedKey);
experimental-genesis 74 READWRITE(vchSalt);
experimental-genesis 75 READWRITE(nDerivationMethod);
experimental-genesis 76 READWRITE(nDeriveIterations);
experimental-genesis 77 READWRITE(vchOtherDerivationParameters);
experimental-genesis 78 )
experimental-genesis 79 CMasterKey()
experimental-genesis 80 {
experimental-genesis 81
experimental-genesis 82
experimental-genesis 83 nDeriveIterations = 25000;
experimental-genesis 84 nDerivationMethod = 0;
experimental-genesis 85 vchOtherDerivationParameters = std::vector<unsigned char>(0);
experimental-genesis 86 }
experimental-genesis 87 };
experimental-genesis 88
experimental-genesis 89 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
experimental-genesis 90
experimental-genesis 91 class CCrypter
experimental-genesis 92 {
experimental-genesis 93 private:
experimental-genesis 94 unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
experimental-genesis 95 unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
experimental-genesis 96 bool fKeySet;
experimental-genesis 97
experimental-genesis 98 public:
experimental-genesis 99 bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
experimental-genesis 100 bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext);
experimental-genesis 101 bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext);
experimental-genesis 102 bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
experimental-genesis 103
experimental-genesis 104 void CleanKey()
experimental-genesis 105 {
experimental-genesis 106 memset(&chKey, 0, sizeof chKey);
experimental-genesis 107 memset(&chIV, 0, sizeof chIV);
experimental-genesis 108 munlock(&chKey, sizeof chKey);
experimental-genesis 109 munlock(&chIV, sizeof chIV);
experimental-genesis 110 fKeySet = false;
experimental-genesis 111 }
experimental-genesis 112
experimental-genesis 113 CCrypter()
experimental-genesis 114 {
experimental-genesis 115 fKeySet = false;
experimental-genesis 116 }
experimental-genesis 117
experimental-genesis 118 ~CCrypter()
experimental-genesis 119 {
experimental-genesis 120 CleanKey();
experimental-genesis 121 }
experimental-genesis 122 };
experimental-genesis 123
experimental-genesis 124 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
experimental-genesis 125 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char> &vchCiphertext, const uint256& nIV, CSecret &vchPlaintext);
experimental-genesis 126
experimental-genesis 127 #endif