raw
genesis                 1 // Copyright (c) 2011 The Bitcoin Developers
genesis 2 // Distributed under the MIT/X11 software license, see the accompanying
genesis 3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
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 // Try to keep the keydata out of swap (and be a bit over-careful to keep the IV that we don't even use out of swap)
genesis 21 // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
genesis 22 // 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.
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 // Try to keep the keydata out of swap
genesis 48 // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
genesis 49 // 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.
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 // max ciphertext len for a n bytes of plaintext is
genesis 66 // n + AES_BLOCK_SIZE - 1 bytes
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 // plaintext will always be equal to or lesser than length of ciphertext
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 }