/src/botan/src/lib/hash/mdx_hash/mdx_hash.cpp
Line | Count | Source (jump to first uncovered line) |
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/mdx_hash.h> |
9 | | #include <botan/exceptn.h> |
10 | | #include <botan/loadstor.h> |
11 | | #include <botan/internal/bit_ops.h> |
12 | | |
13 | | namespace Botan { |
14 | | |
15 | | /* |
16 | | * MDx_HashFunction Constructor |
17 | | */ |
18 | | MDx_HashFunction::MDx_HashFunction(size_t block_len, |
19 | | bool byte_big_endian, |
20 | | bool bit_big_endian, |
21 | | uint8_t cnt_size) : |
22 | | m_pad_char(bit_big_endian == true ? 0x80 : 0x01), |
23 | | m_counter_size(cnt_size), |
24 | | m_block_bits(ceil_log2(block_len)), |
25 | | m_count_big_endian(byte_big_endian), |
26 | | m_count(0), |
27 | | m_buffer(block_len), |
28 | | m_position(0) |
29 | 139k | { |
30 | 139k | if(!is_power_of_2(block_len)) |
31 | 0 | throw Invalid_Argument("MDx_HashFunction block length must be a power of 2"); |
32 | 139k | if(m_block_bits < 3 || m_block_bits > 16) |
33 | 0 | throw Invalid_Argument("MDx_HashFunction block size too large or too small"); |
34 | 139k | if(m_counter_size < 8 || m_counter_size > block_len) |
35 | 0 | throw Invalid_State("MDx_HashFunction invalid counter length"); |
36 | 139k | } |
37 | | |
38 | | /* |
39 | | * Clear memory of sensitive data |
40 | | */ |
41 | | void MDx_HashFunction::clear() |
42 | 606k | { |
43 | 606k | zeroise(m_buffer); |
44 | 606k | m_count = m_position = 0; |
45 | 606k | } |
46 | | |
47 | | /* |
48 | | * Update the hash |
49 | | */ |
50 | | void MDx_HashFunction::add_data(const uint8_t input[], size_t length) |
51 | 851k | { |
52 | 851k | const size_t block_len = static_cast<size_t>(1) << m_block_bits; |
53 | 851k | |
54 | 851k | m_count += length; |
55 | 851k | |
56 | 851k | if(m_position) |
57 | 90.7k | { |
58 | 90.7k | buffer_insert(m_buffer, m_position, input, length); |
59 | 90.7k | |
60 | 90.7k | if(m_position + length >= block_len) |
61 | 49.1k | { |
62 | 49.1k | compress_n(m_buffer.data(), 1); |
63 | 49.1k | input += (block_len - m_position); |
64 | 49.1k | length -= (block_len - m_position); |
65 | 49.1k | m_position = 0; |
66 | 49.1k | } |
67 | 90.7k | } |
68 | 851k | |
69 | 851k | // Just in case the compiler can't figure out block_len is a power of 2 |
70 | 851k | const size_t full_blocks = length >> m_block_bits; |
71 | 851k | const size_t remaining = length & (block_len - 1); |
72 | 851k | |
73 | 851k | if(full_blocks > 0) |
74 | 419k | { |
75 | 419k | compress_n(input, full_blocks); |
76 | 419k | } |
77 | 851k | |
78 | 851k | buffer_insert(m_buffer, m_position, input + full_blocks * block_len, remaining); |
79 | 851k | m_position += remaining; |
80 | 851k | } |
81 | | |
82 | | /* |
83 | | * Finalize a hash |
84 | | */ |
85 | | void MDx_HashFunction::final_result(uint8_t output[]) |
86 | 427k | { |
87 | 427k | const size_t block_len = static_cast<size_t>(1) << m_block_bits; |
88 | 427k | |
89 | 427k | clear_mem(&m_buffer[m_position], block_len - m_position); |
90 | 427k | m_buffer[m_position] = m_pad_char; |
91 | 427k | |
92 | 427k | if(m_position >= block_len - m_counter_size) |
93 | 36.0k | { |
94 | 36.0k | compress_n(m_buffer.data(), 1); |
95 | 36.0k | zeroise(m_buffer); |
96 | 36.0k | } |
97 | 427k | |
98 | 427k | write_count(&m_buffer[block_len - m_counter_size]); |
99 | 427k | |
100 | 427k | compress_n(m_buffer.data(), 1); |
101 | 427k | copy_out(output); |
102 | 427k | clear(); |
103 | 427k | } |
104 | | |
105 | | /* |
106 | | * Write the count bits to the buffer |
107 | | */ |
108 | | void MDx_HashFunction::write_count(uint8_t out[]) |
109 | 427k | { |
110 | 427k | BOTAN_ASSERT_NOMSG(m_counter_size <= output_length()); |
111 | 427k | BOTAN_ASSERT_NOMSG(m_counter_size >= 8); |
112 | 427k | |
113 | 427k | const uint64_t bit_count = m_count * 8; |
114 | 427k | |
115 | 427k | if(m_count_big_endian) |
116 | 427k | store_be(bit_count, out + m_counter_size - 8); |
117 | 132 | else |
118 | 132 | store_le(bit_count, out + m_counter_size - 8); |
119 | 427k | } |
120 | | |
121 | | } |