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