/src/botan/src/lib/hash/comb4p/comb4p.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Comb4P hash combiner |
3 | | * (C) 2010 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/internal/comb4p.h> |
9 | | |
10 | | #include <botan/exceptn.h> |
11 | | #include <botan/mem_ops.h> |
12 | | #include <botan/internal/fmt.h> |
13 | | #include <botan/internal/stl_util.h> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | namespace { |
18 | | |
19 | | void comb4p_round(secure_vector<uint8_t>& out, |
20 | | const secure_vector<uint8_t>& in, |
21 | | uint8_t round_no, |
22 | | HashFunction& h1, |
23 | 0 | HashFunction& h2) { |
24 | 0 | h1.update(round_no); |
25 | 0 | h2.update(round_no); |
26 | |
|
27 | 0 | h1.update(in.data(), in.size()); |
28 | 0 | h2.update(in.data(), in.size()); |
29 | |
|
30 | 0 | secure_vector<uint8_t> h_buf = h1.final(); |
31 | 0 | xor_buf(out.data(), h_buf.data(), std::min(out.size(), h_buf.size())); |
32 | |
|
33 | 0 | h_buf = h2.final(); |
34 | 0 | xor_buf(out.data(), h_buf.data(), std::min(out.size(), h_buf.size())); |
35 | 0 | } |
36 | | |
37 | | } // namespace |
38 | | |
39 | | Comb4P::Comb4P(std::unique_ptr<HashFunction> h1, std::unique_ptr<HashFunction> h2) : |
40 | 0 | m_hash1(std::move(h1)), m_hash2(std::move(h2)) { |
41 | 0 | if(m_hash1->name() == m_hash2->name()) { |
42 | 0 | throw Invalid_Argument("Comb4P: Must use two distinct hashes"); |
43 | 0 | } |
44 | | |
45 | 0 | if(m_hash1->output_length() != m_hash2->output_length()) { |
46 | 0 | throw Invalid_Argument(fmt("Comb4P: Incompatible hashes {} and {}", m_hash1->name(), m_hash2->name())); |
47 | 0 | } |
48 | | |
49 | 0 | clear(); |
50 | 0 | } |
51 | | |
52 | 0 | std::string Comb4P::name() const { |
53 | 0 | return fmt("Comb4P({},{})", m_hash1->name(), m_hash2->name()); |
54 | 0 | } |
55 | | |
56 | 0 | std::unique_ptr<HashFunction> Comb4P::new_object() const { |
57 | 0 | return std::make_unique<Comb4P>(m_hash1->new_object(), m_hash2->new_object()); |
58 | 0 | } |
59 | | |
60 | 0 | size_t Comb4P::hash_block_size() const { |
61 | 0 | if(m_hash1->hash_block_size() == m_hash2->hash_block_size()) { |
62 | 0 | return m_hash1->hash_block_size(); |
63 | 0 | } |
64 | | |
65 | | /* |
66 | | * Return LCM of the block sizes? This would probably be OK for |
67 | | * HMAC, which is the main thing relying on knowing the block size. |
68 | | */ |
69 | 0 | return 0; |
70 | 0 | } |
71 | | |
72 | 0 | void Comb4P::clear() { |
73 | 0 | m_hash1->clear(); |
74 | 0 | m_hash2->clear(); |
75 | | |
76 | | // Prep for processing next message, if any |
77 | 0 | m_hash1->update(0); |
78 | 0 | m_hash2->update(0); |
79 | 0 | } |
80 | | |
81 | 0 | std::unique_ptr<HashFunction> Comb4P::copy_state() const { |
82 | | // Can't use make_unique as this constructor is private |
83 | 0 | std::unique_ptr<Comb4P> copy(new Comb4P); |
84 | 0 | copy->m_hash1 = m_hash1->copy_state(); |
85 | 0 | copy->m_hash2 = m_hash2->copy_state(); |
86 | 0 | return copy; |
87 | 0 | } |
88 | | |
89 | 0 | void Comb4P::add_data(std::span<const uint8_t> input) { |
90 | 0 | m_hash1->update(input); |
91 | 0 | m_hash2->update(input); |
92 | 0 | } |
93 | | |
94 | 0 | void Comb4P::final_result(std::span<uint8_t> output) { |
95 | 0 | secure_vector<uint8_t> h1 = m_hash1->final(); |
96 | 0 | secure_vector<uint8_t> h2 = m_hash2->final(); |
97 | | |
98 | | // First round |
99 | 0 | xor_buf(h1.data(), h2.data(), std::min(h1.size(), h2.size())); |
100 | | |
101 | | // Second round |
102 | 0 | comb4p_round(h2, h1, 1, *m_hash1, *m_hash2); |
103 | | |
104 | | // Third round |
105 | 0 | comb4p_round(h1, h2, 2, *m_hash1, *m_hash2); |
106 | |
|
107 | 0 | BufferStuffer out(output); |
108 | 0 | copy_mem(out.next(h1.size()).data(), h1.data(), h1.size()); |
109 | 0 | copy_mem(out.next(h2.size()).data(), h2.data(), h2.size()); |
110 | | |
111 | | // Prep for processing next message, if any |
112 | 0 | m_hash1->update(0); |
113 | 0 | m_hash2->update(0); |
114 | 0 | } |
115 | | |
116 | | } // namespace Botan |