Botan  1.11.26
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 
36 Comb4P* Comb4P::make(const Spec& spec)
37  {
38  if(spec.arg_count() == 2)
39  {
40  std::unique_ptr<HashFunction> h1(HashFunction::create(spec.arg(0)));
41  std::unique_ptr<HashFunction> h2(HashFunction::create(spec.arg(1)));
42 
43  if(h1 && h2)
44  return new Comb4P(h1.release(), h2.release());
45  }
46  return nullptr;
47  }
48 
50  m_hash1(h1), m_hash2(h2)
51  {
52  if(m_hash1->name() == m_hash2->name())
53  throw Invalid_Argument("Comb4P: Must use two distinct hashes");
54 
55  if(m_hash1->output_length() != m_hash2->output_length())
56  throw Invalid_Argument("Comb4P: Incompatible hashes " +
57  m_hash1->name() + " and " +
58  m_hash2->name());
59 
60  clear();
61  }
62 
64  {
65  if(m_hash1->hash_block_size() == m_hash2->hash_block_size())
66  return m_hash1->hash_block_size();
67 
68  /*
69  * Return LCM of the block sizes? This would probably be OK for
70  * HMAC, which is the main thing relying on knowing the block size.
71  */
72  return 0;
73  }
74 
76  {
77  m_hash1->clear();
78  m_hash2->clear();
79 
80  // Prep for processing next message, if any
81  m_hash1->update(0);
82  m_hash2->update(0);
83  }
84 
85 void Comb4P::add_data(const byte input[], size_t length)
86  {
87  m_hash1->update(input, length);
88  m_hash2->update(input, length);
89  }
90 
91 void Comb4P::final_result(byte out[])
92  {
93  secure_vector<byte> h1 = m_hash1->final();
94  secure_vector<byte> h2 = m_hash2->final();
95 
96  // First round
97  xor_buf(h1.data(), h2.data(), std::min(h1.size(), h2.size()));
98 
99  // Second round
100  comb4p_round(h2, h1, 1, *m_hash1, *m_hash2);
101 
102  // Third round
103  comb4p_round(h1, h2, 2, *m_hash1, *m_hash2);
104 
105  copy_mem(out , h1.data(), h1.size());
106  copy_mem(out + h1.size(), h2.data(), h2.size());
107 
108  // Prep for processing next message, if any
109  m_hash1->update(0);
110  m_hash2->update(0);
111  }
112 
113 }
114 
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:90
std::string arg(size_t i) const
Definition: scan_name.cpp:153
Comb4P(HashFunction *h1, HashFunction *h2)
Definition: comb4p.cpp:49
size_t arg_count() const
Definition: scan_name.h:64
static Comb4P * make(const Spec &spec)
Definition: comb4p.cpp:36
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:96
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:98
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:43
Definition: alg_id.cpp:13
void clear() override
Definition: comb4p.cpp:75
size_t hash_block_size() const override
Definition: comb4p.cpp:63
T min(T a, T b)
Definition: ct_utils.h:174
std::uint8_t byte
Definition: types.h:31