• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.34
/src/lib/hash/mdx_hash/mdx_hash.cpp
1
/*
2
* Merkle-Damgard Hash Function
3
* (C) 1999-2008,2018 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/internal/mdx_hash.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/internal/bit_ops.h>
12
#include <botan/internal/loadstor.h>
13

14
namespace Botan {
15

16
/*
17
* MDx_HashFunction Constructor
18
*/
19
MDx_HashFunction::MDx_HashFunction(size_t block_len, bool byte_big_endian, bool bit_big_endian, uint8_t cnt_size) :
389,394✔
20
      m_pad_char(bit_big_endian == true ? 0x80 : 0x01),
389,394✔
21
      m_counter_size(cnt_size),
389,394✔
22
      m_block_bits(ceil_log2(block_len)),
389,394✔
23
      m_count_big_endian(byte_big_endian),
389,394✔
24
      m_count(0),
389,394✔
25
      m_buffer(block_len),
389,394✔
26
      m_position(0) {
389,394✔
27
   if(!is_power_of_2(block_len))
389,394✔
28
      throw Invalid_Argument("MDx_HashFunction block length must be a power of 2");
×
29
   if(m_block_bits < 3 || m_block_bits > 16)
389,394✔
30
      throw Invalid_Argument("MDx_HashFunction block size too large or too small");
×
31
   if(m_counter_size < 8 || m_counter_size > block_len)
389,394✔
32
      throw Invalid_State("MDx_HashFunction invalid counter length");
×
33
}
389,394✔
34

35
/*
36
* Clear memory of sensitive data
37
*/
38
void MDx_HashFunction::clear() {
154,877,055✔
39
   zeroise(m_buffer);
154,877,055✔
40
   m_count = m_position = 0;
154,877,055✔
41
}
154,877,055✔
42

43
/*
44
* Update the hash
45
*/
46
void MDx_HashFunction::add_data(const uint8_t input[], size_t length) {
607,657,417✔
47
   const size_t block_len = static_cast<size_t>(1) << m_block_bits;
607,657,417✔
48

49
   m_count += length;
607,657,417✔
50

51
   if(m_position) {
607,657,417✔
52
      buffer_insert(m_buffer, m_position, input, length);
291,911,362✔
53

54
      if(m_position + length >= block_len) {
291,911,362✔
55
         compress_n(m_buffer.data(), 1);
123,255,036✔
56
         input += (block_len - m_position);
123,255,036✔
57
         length -= (block_len - m_position);
123,255,036✔
58
         m_position = 0;
123,255,036✔
59
      }
60
   }
61

62
   // Just in case the compiler can't figure out block_len is a power of 2
63
   const size_t full_blocks = length >> m_block_bits;
607,657,417✔
64
   const size_t remaining = length & (block_len - 1);
607,657,417✔
65

66
   if(full_blocks > 0) {
607,657,417✔
67
      compress_n(input, full_blocks);
43,750,664✔
68
   }
69

70
   buffer_insert(m_buffer, m_position, input + full_blocks * block_len, remaining);
607,657,417✔
71
   m_position += remaining;
607,657,417✔
72
}
607,657,417✔
73

74
/*
75
* Finalize a hash
76
*/
77
void MDx_HashFunction::final_result(uint8_t output[]) {
154,219,447✔
78
   const size_t block_len = static_cast<size_t>(1) << m_block_bits;
154,219,447✔
79

80
   clear_mem(&m_buffer[m_position], block_len - m_position);
154,219,447✔
81
   m_buffer[m_position] = m_pad_char;
154,219,447✔
82

83
   if(m_position >= block_len - m_counter_size) {
154,219,447✔
84
      compress_n(m_buffer.data(), 1);
8,995,864✔
85
      zeroise(m_buffer);
8,995,864✔
86
   }
87

88
   BOTAN_ASSERT_NOMSG(m_counter_size <= output_length());
154,219,447✔
89
   BOTAN_ASSERT_NOMSG(m_counter_size >= 8);
154,219,447✔
90

91
   const uint64_t bit_count = m_count * 8;
154,219,447✔
92

93
   if(m_count_big_endian)
154,219,447✔
94
      store_be(bit_count, &m_buffer[block_len - 8]);
154,216,731✔
95
   else
96
      store_le(bit_count, &m_buffer[block_len - 8]);
2,716✔
97

98
   compress_n(m_buffer.data(), 1);
154,219,447✔
99
   copy_out(output);
154,219,447✔
100
   clear();
154,219,447✔
101
}
154,219,447✔
102

103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc