LCOV - code coverage report
Current view: top level - src/crypto - hmac_sha256.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 21 21
Test Date: 2026-06-25 07:19:11 Functions: 100.0 % 2 2
Branches: 100.0 % 6 6

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2014-present 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 <crypto/hmac_sha256.h>
       6                 :             : 
       7                 :             : #include <crypto/sha256.h>
       8                 :             : #include <support/cleanse.h>
       9                 :             : 
      10                 :             : #include <cstring>
      11                 :             : 
      12                 :        1723 : CHMAC_SHA256::CHMAC_SHA256(const unsigned char* key, size_t keylen)
      13                 :             : {
      14                 :        1723 :     unsigned char rkey[64];
      15         [ +  + ]:        1723 :     if (keylen <= 64) {
      16                 :        1719 :         memcpy(rkey, key, keylen);
      17                 :        1719 :         memset(rkey + keylen, 0, 64 - keylen);
      18                 :             :     } else {
      19                 :           4 :         CSHA256().Write(key, keylen).Finalize(rkey);
      20                 :           4 :         memset(rkey + 32, 0, 32);
      21                 :             :     }
      22                 :             : 
      23         [ +  + ]:      111995 :     for (int n = 0; n < 64; n++)
      24                 :      110272 :         rkey[n] ^= 0x5c;
      25                 :        1723 :     outer.Write(rkey, 64);
      26                 :             : 
      27         [ +  + ]:      111995 :     for (int n = 0; n < 64; n++)
      28                 :      110272 :         rkey[n] ^= 0x5c ^ 0x36;
      29                 :        1723 :     inner.Write(rkey, 64);
      30                 :             : 
      31                 :        1723 :     memory_cleanse(rkey, sizeof(rkey));
      32                 :        1723 : }
      33                 :             : 
      34                 :        5181 : void CHMAC_SHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
      35                 :             : {
      36                 :        5181 :     unsigned char temp[32];
      37                 :        5181 :     inner.Finalize(temp);
      38                 :        5181 :     outer.Write(temp, 32).Finalize(hash);
      39                 :        5181 :     memory_cleanse(temp, sizeof(temp));
      40                 :        5181 : }
        

Generated by: LCOV version 2.0-1