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
|
/*************************************************
* HMAC Source File *
* (C) 1999-2008 The Botan Project *
*************************************************/
#include <botan/hmac.h>
#include <botan/lookup.h>
#include <botan/bit_ops.h>
namespace Botan {
/*************************************************
* Update a HMAC Calculation *
*************************************************/
void HMAC::add_data(const byte input[], u32bit length)
{
hash->update(input, length);
}
/*************************************************
* Finalize a HMAC Calculation *
*************************************************/
void HMAC::final_result(byte mac[])
{
hash->final(mac);
hash->update(o_key);
hash->update(mac, OUTPUT_LENGTH);
hash->final(mac);
hash->update(i_key);
}
/*************************************************
* HMAC Key Schedule *
*************************************************/
void HMAC::key(const byte key[], u32bit length)
{
hash->clear();
std::fill(i_key.begin(), i_key.end(), 0x36);
std::fill(o_key.begin(), o_key.end(), 0x5C);
if(length > hash->HASH_BLOCK_SIZE)
{
SecureVector<byte> hmac_key = hash->process(key, length);
xor_buf(i_key, hmac_key, hmac_key.size());
xor_buf(o_key, hmac_key, hmac_key.size());
}
else
{
xor_buf(i_key, key, length);
xor_buf(o_key, key, length);
}
hash->update(i_key);
}
/*************************************************
* Clear memory of sensitive data *
*************************************************/
void HMAC::clear() throw()
{
hash->clear();
i_key.clear();
o_key.clear();
}
/*************************************************
* Return the name of this type *
*************************************************/
std::string HMAC::name() const
{
return "HMAC(" + hash->name() + ")";
}
/*************************************************
* Return a clone of this object *
*************************************************/
MessageAuthenticationCode* HMAC::clone() const
{
return new HMAC(hash->name());
}
/*************************************************
* HMAC Constructor *
*************************************************/
HMAC::HMAC(const std::string& hash_name) :
MessageAuthenticationCode(output_length_of(hash_name),
1, 2*block_size_of(hash_name)),
hash(get_hash(hash_name))
{
if(hash->HASH_BLOCK_SIZE == 0)
throw Invalid_Argument("HMAC cannot be used with " + hash->name());
i_key.create(hash->HASH_BLOCK_SIZE);
o_key.create(hash->HASH_BLOCK_SIZE);
}
}
|