Botan 1.10.17
hmac.cpp
Go to the documentation of this file.
1/*
2* HMAC
3* (C) 1999-2007 Jack Lloyd
4* 2007 Yves Jerschow
5*
6* Distributed under the terms of the Botan license
7*/
8
9#include <botan/hmac.h>
10#include <botan/internal/xor_buf.h>
11
12namespace Botan {
13
14/*
15* Update a HMAC Calculation
16*/
17void HMAC::add_data(const byte input[], size_t length)
18 {
19 hash->update(input, length);
20 }
21
22/*
23* Finalize a HMAC Calculation
24*/
25void HMAC::final_result(byte mac[])
26 {
27 hash->final(mac);
28 hash->update(o_key);
29 hash->update(mac, output_length());
30 hash->final(mac);
31 hash->update(i_key);
32 }
33
34/*
35* HMAC Key Schedule
36*/
37void HMAC::key_schedule(const byte key[], size_t length)
38 {
39 hash->clear();
40 std::fill(i_key.begin(), i_key.end(), 0x36);
41 std::fill(o_key.begin(), o_key.end(), 0x5C);
42
43 if(length > hash->hash_block_size())
44 {
45 SecureVector<byte> hmac_key = hash->process(key, length);
46 xor_buf(i_key, hmac_key, hmac_key.size());
47 xor_buf(o_key, hmac_key, hmac_key.size());
48 }
49 else
50 {
51 xor_buf(i_key, key, length);
52 xor_buf(o_key, key, length);
53 }
54
55 hash->update(i_key);
56 }
57
58/*
59* Clear memory of sensitive data
60*/
62 {
63 hash->clear();
64 zeroise(i_key);
65 zeroise(o_key);
66 }
67
68/*
69* Return the name of this type
70*/
71std::string HMAC::name() const
72 {
73 return "HMAC(" + hash->name() + ")";
74 }
75
76/*
77* Return a clone of this object
78*/
80 {
81 return new HMAC(hash->clone());
82 }
83
84/*
85* HMAC Constructor
86*/
87HMAC::HMAC(HashFunction* hash_in) : hash(hash_in)
88 {
89 if(hash->hash_block_size() == 0)
90 throw Invalid_Argument("HMAC cannot be used with " + hash->name());
91
92 i_key.resize(hash->hash_block_size());
93 o_key.resize(hash->hash_block_size());
94 }
95
96}
MessageAuthenticationCode * clone() const
Definition hmac.cpp:79
std::string name() const
Definition hmac.cpp:71
size_t output_length() const
Definition hmac.h:26
void clear()
Definition hmac.cpp:61
HMAC(HashFunction *hash)
Definition hmac.cpp:87
void zeroise(MemoryRegion< T > &vec)
Definition secmem.h:428
void xor_buf(byte out[], const byte in[], size_t length)
Definition xor_buf.h:21
std::invalid_argument Invalid_Argument
Definition exceptn.h:20