LCOV - code coverage report
Current view: top level - src/wallet - crypter.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 83 0.0 %
Date: 2023-09-26 12:08:55 Functions: 0 8 0.0 %

          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 <common/system.h>
       8             : #include <crypto/aes.h>
       9             : #include <crypto/sha512.h>
      10             : 
      11             : #include <vector>
      12             : 
      13             : namespace wallet {
      14           0 : int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const
      15             : {
      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           0 :     if(!count || !key || !iv)
      22           0 :         return 0;
      23             : 
      24             :     unsigned char buf[CSHA512::OUTPUT_SIZE];
      25           0 :     CSHA512 di;
      26             : 
      27           0 :     di.Write((const unsigned char*)strKeyData.data(), strKeyData.size());
      28           0 :     di.Write(chSalt.data(), chSalt.size());
      29           0 :     di.Finalize(buf);
      30             : 
      31           0 :     for(int i = 0; i != count - 1; i++)
      32           0 :         di.Reset().Write(buf, sizeof(buf)).Finalize(buf);
      33             : 
      34           0 :     memcpy(key, buf, WALLET_CRYPTO_KEY_SIZE);
      35           0 :     memcpy(iv, buf + WALLET_CRYPTO_KEY_SIZE, WALLET_CRYPTO_IV_SIZE);
      36           0 :     memory_cleanse(buf, sizeof(buf));
      37           0 :     return WALLET_CRYPTO_KEY_SIZE;
      38           0 : }
      39             : 
      40           0 : bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
      41             : {
      42           0 :     if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
      43           0 :         return false;
      44             : 
      45           0 :     int i = 0;
      46           0 :     if (nDerivationMethod == 0)
      47           0 :         i = BytesToKeySHA512AES(chSalt, strKeyData, nRounds, vchKey.data(), vchIV.data());
      48             : 
      49           0 :     if (i != (int)WALLET_CRYPTO_KEY_SIZE)
      50             :     {
      51           0 :         memory_cleanse(vchKey.data(), vchKey.size());
      52           0 :         memory_cleanse(vchIV.data(), vchIV.size());
      53           0 :         return false;
      54             :     }
      55             : 
      56           0 :     fKeySet = true;
      57           0 :     return true;
      58           0 : }
      59             : 
      60           0 : bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
      61             : {
      62           0 :     if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_IV_SIZE)
      63           0 :         return false;
      64             : 
      65           0 :     memcpy(vchKey.data(), chNewKey.data(), chNewKey.size());
      66           0 :     memcpy(vchIV.data(), chNewIV.data(), chNewIV.size());
      67             : 
      68           0 :     fKeySet = true;
      69           0 :     return true;
      70           0 : }
      71             : 
      72           0 : bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const
      73             : {
      74           0 :     if (!fKeySet)
      75           0 :         return false;
      76             : 
      77             :     // max ciphertext len for a n bytes of plaintext is
      78             :     // n + AES_BLOCKSIZE bytes
      79           0 :     vchCiphertext.resize(vchPlaintext.size() + AES_BLOCKSIZE);
      80             : 
      81           0 :     AES256CBCEncrypt enc(vchKey.data(), vchIV.data(), true);
      82           0 :     size_t nLen = enc.Encrypt(vchPlaintext.data(), vchPlaintext.size(), vchCiphertext.data());
      83           0 :     if(nLen < vchPlaintext.size())
      84           0 :         return false;
      85           0 :     vchCiphertext.resize(nLen);
      86             : 
      87           0 :     return true;
      88           0 : }
      89             : 
      90           0 : bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const
      91             : {
      92           0 :     if (!fKeySet)
      93           0 :         return false;
      94             : 
      95             :     // plaintext will always be equal to or lesser than length of ciphertext
      96           0 :     int nLen = vchCiphertext.size();
      97             : 
      98           0 :     vchPlaintext.resize(nLen);
      99             : 
     100           0 :     AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true);
     101           0 :     nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data());
     102           0 :     if(nLen == 0)
     103           0 :         return false;
     104           0 :     vchPlaintext.resize(nLen);
     105           0 :     return true;
     106           0 : }
     107             : 
     108           0 : bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
     109             : {
     110           0 :     CCrypter cKeyCrypter;
     111           0 :     std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
     112           0 :     memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);
     113           0 :     if(!cKeyCrypter.SetKey(vMasterKey, chIV))
     114           0 :         return false;
     115           0 :     return cKeyCrypter.Encrypt(vchPlaintext, vchCiphertext);
     116           0 : }
     117             : 
     118           0 : bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
     119             : {
     120           0 :     CCrypter cKeyCrypter;
     121           0 :     std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
     122           0 :     memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);
     123           0 :     if(!cKeyCrypter.SetKey(vMasterKey, chIV))
     124           0 :         return false;
     125           0 :     return cKeyCrypter.Decrypt(vchCiphertext, vchPlaintext);
     126           0 : }
     127             : 
     128           0 : bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
     129             : {
     130           0 :     CKeyingMaterial vchSecret;
     131           0 :     if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
     132           0 :         return false;
     133             : 
     134           0 :     if (vchSecret.size() != 32)
     135           0 :         return false;
     136             : 
     137           0 :     key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
     138           0 :     return key.VerifyPubKey(vchPubKey);
     139           0 : }
     140             : } // namespace wallet

Generated by: LCOV version 1.14