monotone

monotone Mtn Source Tree

Root/hmac.cc

1#include <string>
2
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()), session_key().size())
16{
17 chain_val.assign(hmac_length, 0x00);
18}
19
20void
21chained_hmac::set_key(netsync_session_key const & session_key)
22{
23 if (active)
24 {
25 key = Botan::SymmetricKey(reinterpret_cast<Botan::byte const *>(session_key().data()),
26session_key().size());
27 }
28}
29
30string
31chained_hmac::process(string const & str, size_t pos, size_t n)
32{
33 I(active);
34
35 I(pos < str.size());
36 if (n == string::npos)
37 n = str.size() - pos;
38
39 I(pos + n <= str.size());
40
41 Botan::Pipe p(new Botan::MAC_Filter("HMAC(SHA-160)", key,
42 constants::sha1_digest_length));
43 p.start_msg();
44 p.write(chain_val);
45 p.write(reinterpret_cast<Botan::byte const *>(str.data() + pos), n);
46 p.end_msg();
47
48 chain_val = p.read_all_as_string();
49 I(chain_val.size() == constants::sha1_digest_length);
50
51 return chain_val;
52}
53
54string
55chained_hmac::process(string_queue const & str, size_t pos, size_t n)
56{
57 I(active);
58
59 I(pos < str.size());
60 if (n == string::npos)
61 n = str.size() - pos;
62
63 I(pos + n <= str.size());
64
65 Botan::Pipe p(new Botan::MAC_Filter("HMAC(SHA-160)", key,
66 constants::sha1_digest_length));
67 p.start_msg();
68 p.write(chain_val);
69 p.write(reinterpret_cast<Botan::byte const *>(str.front_pointer(n) + pos), n);
70
71 p.end_msg();
72
73 chain_val = p.read_all_as_string();
74 I(chain_val.size() == constants::sha1_digest_length);
75
76 return chain_val;
77}
78
79// Local Variables:
80// mode: C++
81// fill-column: 76
82// c-file-style: "gnu"
83// indent-tabs-mode: nil
84// End:
85// 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