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