LCOV - code coverage report
Current view: top level - src/wallet - crypter.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 95.6 % 68 65
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 8 8
Branches: 53.4 % 58 31

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

Generated by: LCOV version 2.0-1