monotone

monotone Mtn Source Tree

Root/hmac.cc

1
2#include "base.hh"
3#include "botan/botan.h"
4
5#include "sanity.hh"
6#include "hmac.hh"
7#include "vocab.hh"
8#include "constants.hh"
9
10using std::string;
11
12chained_hmac::chained_hmac(netsync_session_key const & session_key, bool active) :
13 hmac_length(constants::sha1_digest_length),
14 active(active),
15 key(reinterpret_cast<Botan::byte const *>(session_key().data()),
16 session_key().size()),
17 engine(new Botan::MAC_Filter("HMAC(SHA-160)", key,
18 constants::sha1_digest_length))
19{
20 chain_val.assign(hmac_length, 0x00);
21}
22
23void
24chained_hmac::set_key(netsync_session_key const & session_key)
25{
26 if (active)
27 {
28 key = Botan::SymmetricKey(reinterpret_cast<Botan::byte const *>(session_key().data()),
29session_key().size());
30 engine.reset();
31 engine.append(new Botan::MAC_Filter("HMAC(SHA-160)", key,
32 constants::sha1_digest_length));
33 }
34}
35
36string
37chained_hmac::process(string const & str, size_t pos, size_t n)
38{
39 I(active);
40
41 I(pos < str.size());
42 if (n == string::npos)
43 n = str.size() - pos;
44
45 I(pos + n <= str.size());
46
47 engine.start_msg();
48 engine.write(chain_val);
49 engine.write(reinterpret_cast<Botan::byte const *>(str.data() + pos), n);
50 engine.end_msg();
51
52 chain_val = engine.read_all_as_string(Botan::Pipe::LAST_MESSAGE);
53 I(chain_val.size() == constants::sha1_digest_length);
54
55 return chain_val;
56}
57
58string
59chained_hmac::process(string_queue const & str, size_t pos, size_t n)
60{
61 I(active);
62
63 I(pos < str.size());
64 if (n == string::npos)
65 n = str.size() - pos;
66
67 I(pos + n <= str.size());
68
69 engine.start_msg();
70 engine.write(chain_val);
71 engine.write(reinterpret_cast<Botan::byte const *>(str.front_pointer(n)
72 + pos), n);
73 engine.end_msg();
74
75 chain_val = engine.read_all_as_string(Botan::Pipe::LAST_MESSAGE);
76 I(chain_val.size() == constants::sha1_digest_length);
77
78 return chain_val;
79}
80
81// Local Variables:
82// mode: C++
83// fill-column: 76
84// c-file-style: "gnu"
85// indent-tabs-mode: nil
86// End:
87// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

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