genesis 1
genesis 2
genesis 3
genesis 4
genesis 5 #include <openssl/aes.h>
genesis 6 #include <openssl/evp.h>
genesis 7 #include <vector>
genesis 8 #include <string>
genesis 9 #include "headers.h"
genesis 10
genesis 11 #include "crypter.h"
genesis 12 #include "main.h"
genesis 13 #include "util.h"
genesis 14
genesis 15 bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
genesis 16 {
genesis 17 if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
genesis 18 return false;
genesis 19
genesis 20
genesis 21
genesis 22
genesis 23 mlock(&chKey[0], sizeof chKey);
genesis 24 mlock(&chIV[0], sizeof chIV);
genesis 25
genesis 26 int i = 0;
genesis 27 if (nDerivationMethod == 0)
genesis 28 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
genesis 29 (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
genesis 30
genesis 31 if (i != WALLET_CRYPTO_KEY_SIZE)
genesis 32 {
genesis 33 memset(&chKey, 0, sizeof chKey);
genesis 34 memset(&chIV, 0, sizeof chIV);
genesis 35 return false;
genesis 36 }
genesis 37
genesis 38 fKeySet = true;
genesis 39 return true;
genesis 40 }
genesis 41
genesis 42 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
genesis 43 {
genesis 44 if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
genesis 45 return false;
genesis 46
genesis 47
genesis 48
genesis 49
genesis 50 mlock(&chKey[0], sizeof chKey);
genesis 51 mlock(&chIV[0], sizeof chIV);
genesis 52
genesis 53 memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
genesis 54 memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
genesis 55
genesis 56 fKeySet = true;
genesis 57 return true;
genesis 58 }
genesis 59
genesis 60 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
genesis 61 {
genesis 62 if (!fKeySet)
genesis 63 return false;
genesis 64
genesis 65
genesis 66
genesis 67 int nLen = vchPlaintext.size();
genesis 68 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
genesis 69 vchCiphertext = std::vector<unsigned char> (nCLen);
genesis 70
genesis 71 EVP_CIPHER_CTX ctx;
genesis 72
genesis 73 EVP_CIPHER_CTX_init(&ctx);
genesis 74 EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
genesis 75
genesis 76 EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
genesis 77 EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
genesis 78
genesis 79 EVP_CIPHER_CTX_cleanup(&ctx);
genesis 80
genesis 81 vchCiphertext.resize(nCLen + nFLen);
genesis 82 return true;
genesis 83 }
genesis 84
genesis 85 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
genesis 86 {
genesis 87 if (!fKeySet)
genesis 88 return false;
genesis 89
genesis 90
genesis 91 int nLen = vchCiphertext.size();
genesis 92 int nPLen = nLen, nFLen = 0;
genesis 93
genesis 94 vchPlaintext = CKeyingMaterial(nPLen);
genesis 95
genesis 96 EVP_CIPHER_CTX ctx;
genesis 97
genesis 98 EVP_CIPHER_CTX_init(&ctx);
genesis 99 EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
genesis 100
genesis 101 EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
genesis 102 EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
genesis 103
genesis 104 EVP_CIPHER_CTX_cleanup(&ctx);
genesis 105
genesis 106 vchPlaintext.resize(nPLen + nFLen);
genesis 107 return true;
genesis 108 }
genesis 109
genesis 110
genesis 111 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
genesis 112 {
genesis 113 CCrypter cKeyCrypter;
genesis 114 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
genesis 115 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
genesis 116 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
genesis 117 return false;
genesis 118 return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
genesis 119 }
genesis 120
genesis 121 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
genesis 122 {
genesis 123 CCrypter cKeyCrypter;
genesis 124 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
genesis 125 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
genesis 126 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
genesis 127 return false;
genesis 128 return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
genesis 129 }