Botan  1.11.34
Crypto and TLS for C++11
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/comb4p.h>
9 #include <botan/exceptn.h>
10 
11 namespace Botan {
12 
13 namespace {
14 
15 void comb4p_round(secure_vector<byte>& out,
16  const secure_vector<byte>& in,
17  byte round_no,
18  HashFunction& h1,
19  HashFunction& h2)
20  {
21  h1.update(round_no);
22  h2.update(round_no);
23 
24  h1.update(in.data(), in.size());
25  h2.update(in.data(), in.size());
26 
27  secure_vector<byte> h_buf = h1.final();
28  xor_buf(out.data(), h_buf.data(), std::min(out.size(), h_buf.size()));
29 
30  h_buf = h2.final();
31  xor_buf(out.data(), h_buf.data(), std::min(out.size(), h_buf.size()));
32  }
33 
34 }
35 
37  m_hash1(h1), m_hash2(h2)
38  {
39  if(m_hash1->name() == m_hash2->name())
40  throw Invalid_Argument("Comb4P: Must use two distinct hashes");
41 
42  if(m_hash1->output_length() != m_hash2->output_length())
43  throw Invalid_Argument("Comb4P: Incompatible hashes " +
44  m_hash1->name() + " and " +
45  m_hash2->name());
46 
47  clear();
48  }
49 
51  {
52  if(m_hash1->hash_block_size() == m_hash2->hash_block_size())
53  return m_hash1->hash_block_size();
54 
55  /*
56  * Return LCM of the block sizes? This would probably be OK for
57  * HMAC, which is the main thing relying on knowing the block size.
58  */
59  return 0;
60  }
61 
63  {
64  m_hash1->clear();
65  m_hash2->clear();
66 
67  // Prep for processing next message, if any
68  m_hash1->update(0);
69  m_hash2->update(0);
70  }
71 
72 void Comb4P::add_data(const byte input[], size_t length)
73  {
74  m_hash1->update(input, length);
75  m_hash2->update(input, length);
76  }
77 
78 void Comb4P::final_result(byte out[])
79  {
80  secure_vector<byte> h1 = m_hash1->final();
81  secure_vector<byte> h2 = m_hash2->final();
82 
83  // First round
84  xor_buf(h1.data(), h2.data(), std::min(h1.size(), h2.size()));
85 
86  // Second round
87  comb4p_round(h2, h1, 1, *m_hash1, *m_hash2);
88 
89  // Third round
90  comb4p_round(h1, h2, 2, *m_hash1, *m_hash2);
91 
92  copy_mem(out , h1.data(), h1.size());
93  copy_mem(out + h1.size(), h2.data(), h2.size());
94 
95  // Prep for processing next message, if any
96  m_hash1->update(0);
97  m_hash2->update(0);
98  }
99 
100 }
101 
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:115
Comb4P(HashFunction *h1, HashFunction *h2)
Definition: comb4p.cpp:36
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:113
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
Definition: alg_id.cpp:13
void clear() override
Definition: comb4p.cpp:62
size_t hash_block_size() const override
Definition: comb4p.cpp:50
T min(T a, T b)
Definition: ct_utils.h:180
std::uint8_t byte
Definition: types.h:31