monotone

monotone Mtn Source Tree

Root/src/hmac.cc

1// Copyright (C) 2005 Matt Johnston <matt@ucc.asn.au>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#include "base.hh"
11#include <botan/botan.h>
12
13#include "sanity.hh"
14#include "hmac.hh"
15#include "vocab.hh"
16#include "constants.hh"
17
18using std::string;
19
20chained_hmac::chained_hmac(netsync_session_key const & session_key, bool active) :
21 hmac_length(constants::sha1_digest_length),
22 active(active),
23 key(reinterpret_cast<Botan::byte const *>(session_key().data()),
24 session_key().size()),
25 engine(new Botan::MAC_Filter("HMAC(SHA-160)", key,
26 constants::sha1_digest_length))
27{
28 chain_val.assign(hmac_length, 0x00);
29}
30
31void
32chained_hmac::set_key(netsync_session_key const & session_key)
33{
34 if (active)
35 {
36 key = Botan::SymmetricKey(reinterpret_cast<Botan::byte const *>(session_key().data()),
37 session_key().size());
38 engine.reset();
39 engine.append(new Botan::MAC_Filter("HMAC(SHA-160)", key,
40 constants::sha1_digest_length));
41 }
42}
43
44string
45chained_hmac::process(string const & str, size_t pos, size_t n)
46{
47 I(active);
48
49 I(pos < str.size());
50 if (n == string::npos)
51 n = str.size() - pos;
52
53 I(pos + n <= str.size());
54
55 engine.start_msg();
56 engine.write(chain_val);
57 engine.write(reinterpret_cast<Botan::byte const *>(str.data() + pos), n);
58 engine.end_msg();
59
60 chain_val = engine.read_all_as_string(Botan::Pipe::LAST_MESSAGE);
61 I(chain_val.size() == constants::sha1_digest_length);
62
63 return chain_val;
64}
65
66string
67chained_hmac::process(string_queue const & str, size_t pos, size_t n)
68{
69 I(active);
70
71 I(pos < str.size());
72 if (n == string::npos)
73 n = str.size() - pos;
74
75 I(pos + n <= str.size());
76
77 engine.start_msg();
78 engine.write(chain_val);
79 engine.write(reinterpret_cast<Botan::byte const *>(str.front_pointer(n)
80 + pos), n);
81 engine.end_msg();
82
83 chain_val = engine.read_all_as_string(Botan::Pipe::LAST_MESSAGE);
84 I(chain_val.size() == constants::sha1_digest_length);
85
86 return chain_val;
87}
88
89// Local Variables:
90// mode: C++
91// fill-column: 76
92// c-file-style: "gnu"
93// indent-tabs-mode: nil
94// End:
95// 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