monotone

monotone Mtn Source Tree

Root/botan/hmac.cpp

1/*************************************************
2* HMAC Source File *
3* (C) 1999-2007 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 if(length > hash->HASH_BLOCK_SIZE)
42 {
43 SecureVector<byte> hmac_key = hash->process(key, length);
44 xor_buf(i_key, hmac_key, hmac_key.size());
45 xor_buf(o_key, hmac_key, hmac_key.size());
46 }
47 else
48 {
49 xor_buf(i_key, key, length);
50 xor_buf(o_key, key, length);
51 }
52
53 hash->update(i_key);
54 }
55
56/*************************************************
57* Clear memory of sensitive data *
58*************************************************/
59void HMAC::clear() throw()
60 {
61 hash->clear();
62 i_key.clear();
63 o_key.clear();
64 }
65
66/*************************************************
67* Return the name of this type *
68*************************************************/
69std::string HMAC::name() const
70 {
71 return "HMAC(" + hash->name() + ")";
72 }
73
74/*************************************************
75* Return a clone of this object *
76*************************************************/
77MessageAuthenticationCode* HMAC::clone() const
78 {
79 return new HMAC(hash->name());
80 }
81
82/*************************************************
83* HMAC Constructor *
84*************************************************/
85HMAC::HMAC(const std::string& hash_name) :
86 MessageAuthenticationCode(output_length_of(hash_name),
87 1, 2*block_size_of(hash_name)),
88 hash(get_hash(hash_name))
89 {
90 if(hash->HASH_BLOCK_SIZE == 0)
91 throw Invalid_Argument("HMAC cannot be used with " + hash->name());
92 i_key.create(hash->HASH_BLOCK_SIZE);
93 o_key.create(hash->HASH_BLOCK_SIZE);
94 }
95
96}

Archive Download this file

Branches

Tags

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