monotone

monotone Mtn Source Tree

Root/botan/hmac.cpp

1/*************************************************
2* HMAC Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/hmac.h>
7#include <botan/lookup.h>
8#include <botan/bit_ops.h>
9
10namespace Botan {
11
12/*************************************************
13* Update a HMAC Calculation *
14*************************************************/
15void HMAC::add_data(const byte input[], u32bit length)
16 {
17 hash->update(input, length);
18 }
19
20/*************************************************
21* Finalize a HMAC Calculation *
22*************************************************/
23void HMAC::final_result(byte mac[])
24 {
25 hash->final(mac);
26 hash->update(o_key);
27 hash->update(mac, OUTPUT_LENGTH);
28 hash->final(mac);
29 hash->update(i_key);
30 }
31
32/*************************************************
33* HMAC Key Schedule *
34*************************************************/
35void HMAC::key(const byte key[], u32bit length)
36 {
37 hash->clear();
38 std::fill(i_key.begin(), i_key.end(), 0x36);
39 std::fill(o_key.begin(), o_key.end(), 0x5C);
40
41 SecureVector<byte> hmac_key(key, length);
42 if(hmac_key.size() > hash->HASH_BLOCK_SIZE)
43 hmac_key = hash->process(hmac_key);
44
45 xor_buf(i_key, hmac_key, hmac_key.size());
46 xor_buf(o_key, hmac_key, hmac_key.size());
47 hash->update(i_key);
48 }
49
50/*************************************************
51* Clear memory of sensitive data *
52*************************************************/
53void HMAC::clear() throw()
54 {
55 hash->clear();
56 i_key.clear();
57 o_key.clear();
58 }
59
60/*************************************************
61* Return the name of this type *
62*************************************************/
63std::string HMAC::name() const
64 {
65 return "HMAC(" + hash->name() + ")";
66 }
67
68/*************************************************
69* Return a clone of this object *
70*************************************************/
71MessageAuthenticationCode* HMAC::clone() const
72 {
73 return new HMAC(hash->name());
74 }
75
76/*************************************************
77* HMAC Constructor *
78*************************************************/
79HMAC::HMAC(const std::string& hash_name) :
80 MessageAuthenticationCode(output_length_of(hash_name), 1, 64),
81 hash(get_hash(hash_name))
82 {
83 if(hash->HASH_BLOCK_SIZE == 0)
84 throw Invalid_Argument("HMAC cannot be used with " + hash->name());
85 i_key.create(hash->HASH_BLOCK_SIZE);
86 o_key.create(hash->HASH_BLOCK_SIZE);
87 }
88
89}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status