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