Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2021 The Bitcoin Core developers
# 2 : : // Distributed under the MIT software license, see the accompanying
# 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 4 : :
# 5 : : #include <wallet/crypter.h>
# 6 : :
# 7 : : #include <crypto/aes.h>
# 8 : : #include <crypto/sha512.h>
# 9 : : #include <util/system.h>
# 10 : :
# 11 : : #include <vector>
# 12 : :
# 13 : : namespace wallet {
# 14 : : int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const
# 15 : 283 : {
# 16 : : // This mimics the behavior of openssl's EVP_BytesToKey with an aes256cbc
# 17 : : // cipher and sha512 message digest. Because sha512's output size (64b) is
# 18 : : // greater than the aes256 block size (16b) + aes256 key size (32b),
# 19 : : // there's no need to process more than once (D_0).
# 20 : :
# 21 [ - + ][ - + ]: 283 : if(!count || !key || !iv)
# [ - + ]
# 22 : 0 : return 0;
# 23 : :
# 24 : 283 : unsigned char buf[CSHA512::OUTPUT_SIZE];
# 25 : 283 : CSHA512 di;
# 26 : :
# 27 : 283 : di.Write((const unsigned char*)strKeyData.data(), strKeyData.size());
# 28 : 283 : di.Write(chSalt.data(), chSalt.size());
# 29 : 283 : di.Finalize(buf);
# 30 : :
# 31 [ + + ]: 9776707 : for(int i = 0; i != count - 1; i++)
# 32 : 9776424 : di.Reset().Write(buf, sizeof(buf)).Finalize(buf);
# 33 : :
# 34 : 283 : memcpy(key, buf, WALLET_CRYPTO_KEY_SIZE);
# 35 : 283 : memcpy(iv, buf + WALLET_CRYPTO_KEY_SIZE, WALLET_CRYPTO_IV_SIZE);
# 36 : 283 : memory_cleanse(buf, sizeof(buf));
# 37 : 283 : return WALLET_CRYPTO_KEY_SIZE;
# 38 : 283 : }
# 39 : :
# 40 : : bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
# 41 : 283 : {
# 42 [ - + ][ - + ]: 283 : if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
# 43 : 0 : return false;
# 44 : :
# 45 : 283 : int i = 0;
# 46 [ + - ]: 283 : if (nDerivationMethod == 0)
# 47 : 283 : i = BytesToKeySHA512AES(chSalt, strKeyData, nRounds, vchKey.data(), vchIV.data());
# 48 : :
# 49 [ - + ]: 283 : if (i != (int)WALLET_CRYPTO_KEY_SIZE)
# 50 : 0 : {
# 51 : 0 : memory_cleanse(vchKey.data(), vchKey.size());
# 52 : 0 : memory_cleanse(vchIV.data(), vchIV.size());
# 53 : 0 : return false;
# 54 : 0 : }
# 55 : :
# 56 : 283 : fKeySet = true;
# 57 : 283 : return true;
# 58 : 283 : }
# 59 : :
# 60 : : bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
# 61 : 6454 : {
# 62 [ - + ][ - + ]: 6454 : if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_IV_SIZE)
# 63 : 0 : return false;
# 64 : :
# 65 : 6454 : memcpy(vchKey.data(), chNewKey.data(), chNewKey.size());
# 66 : 6454 : memcpy(vchIV.data(), chNewIV.data(), chNewIV.size());
# 67 : :
# 68 : 6454 : fKeySet = true;
# 69 : 6454 : return true;
# 70 : 6454 : }
# 71 : :
# 72 : : bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const
# 73 : 4656 : {
# 74 [ - + ]: 4656 : if (!fKeySet)
# 75 : 0 : return false;
# 76 : :
# 77 : : // max ciphertext len for a n bytes of plaintext is
# 78 : : // n + AES_BLOCKSIZE bytes
# 79 : 4656 : vchCiphertext.resize(vchPlaintext.size() + AES_BLOCKSIZE);
# 80 : :
# 81 : 4656 : AES256CBCEncrypt enc(vchKey.data(), vchIV.data(), true);
# 82 : 4656 : size_t nLen = enc.Encrypt(vchPlaintext.data(), vchPlaintext.size(), vchCiphertext.data());
# 83 [ - + ]: 4656 : if(nLen < vchPlaintext.size())
# 84 : 0 : return false;
# 85 : 4656 : vchCiphertext.resize(nLen);
# 86 : :
# 87 : 4656 : return true;
# 88 : 4656 : }
# 89 : :
# 90 : : bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const
# 91 : 8711 : {
# 92 [ - + ]: 8711 : if (!fKeySet)
# 93 : 0 : return false;
# 94 : :
# 95 : : // plaintext will always be equal to or lesser than length of ciphertext
# 96 : 8711 : int nLen = vchCiphertext.size();
# 97 : :
# 98 : 8711 : vchPlaintext.resize(nLen);
# 99 : :
# 100 : 8711 : AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true);
# 101 : 8711 : nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data());
# 102 [ + + ]: 8711 : if(nLen == 0)
# 103 : 110 : return false;
# 104 : 8601 : vchPlaintext.resize(nLen);
# 105 : 8601 : return true;
# 106 : 8711 : }
# 107 : :
# 108 : : bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
# 109 : 1288 : {
# 110 : 1288 : CCrypter cKeyCrypter;
# 111 : 1288 : std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
# 112 : 1288 : memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);
# 113 [ - + ]: 1288 : if(!cKeyCrypter.SetKey(vMasterKey, chIV))
# 114 : 0 : return false;
# 115 : 1288 : return cKeyCrypter.Encrypt(*((const CKeyingMaterial*)&vchPlaintext), vchCiphertext);
# 116 : 1288 : }
# 117 : :
# 118 : : bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
# 119 : 5166 : {
# 120 : 5166 : CCrypter cKeyCrypter;
# 121 : 5166 : std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
# 122 : 5166 : memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);
# 123 [ - + ]: 5166 : if(!cKeyCrypter.SetKey(vMasterKey, chIV))
# 124 : 0 : return false;
# 125 : 5166 : return cKeyCrypter.Decrypt(vchCiphertext, vchPlaintext);
# 126 : 5166 : }
# 127 : :
# 128 : : bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
# 129 : 5166 : {
# 130 : 5166 : CKeyingMaterial vchSecret;
# 131 [ - + ]: 5166 : if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
# 132 : 0 : return false;
# 133 : :
# 134 [ - + ]: 5166 : if (vchSecret.size() != 32)
# 135 : 0 : return false;
# 136 : :
# 137 : 5166 : key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
# 138 : 5166 : return key.VerifyPubKey(vchPubKey);
# 139 : 5166 : }
# 140 : : } // namespace wallet
|