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
10chained_hmac::chained_hmac(netsync_session_key const & session_key) :
11 hmac_length(constants::sha1_digest_length),
12 key(reinterpret_cast<Botan::byte const *>(session_key().data()), session_key().size())
13{
14 chain_val.assign(hmac_length, 0x00);
15}
16
17void
18chained_hmac::set_key(netsync_session_key const & session_key)
19{
20 key = Botan::SymmetricKey(reinterpret_cast<Botan::byte const *>(session_key().data()),
21 session_key().size());
22}
23
24std::string
25chained_hmac::process(std::string const & str, size_t pos, size_t n)
26{
27 I(pos < str.size());
28 if (n == std::string::npos)
29 n = str.size() - pos;
30
31 I(pos + n <= str.size());
32
33 Botan::Pipe p(new Botan::MAC_Filter("HMAC(SHA-1)", key,
34 constants::sha1_digest_length));
35 p.start_msg();
36 p.write(chain_val);
37 p.write(reinterpret_cast<Botan::byte const *>(str.data() + pos), n);
38 p.end_msg();
39
40 chain_val = p.read_all_as_string();
41 I(chain_val.size() == constants::sha1_digest_length);
42
43 return chain_val;
44}
45
46std::string
47chained_hmac::process(string_queue const & str, size_t pos, size_t n)
48{
49 I(pos < str.size());
50 if (n == std::string::npos)
51 n = str.size() - pos;
52
53 I(pos + n <= str.size());
54
55 Botan::Pipe p(new Botan::MAC_Filter("HMAC(SHA-1)", key,
56 constants::sha1_digest_length));
57 p.start_msg();
58 p.write(chain_val);
59 p.write(reinterpret_cast<Botan::byte const *>(str.front_pointer(n) + pos), n);
60
61 p.end_msg();
62
63 chain_val = p.read_all_as_string();
64 I(chain_val.size() == constants::sha1_digest_length);
65
66 return chain_val;
67}

Archive Download this file

Branches

Tags

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