LCOV - code coverage report
Current view: top level - src/wallet - crypter.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 71 86 82.6 %
Date: 2021-06-29 14:35:33 Functions: 8 8 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 20 36 55.6 %

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

Generated by: LCOV version 1.14