hmac-filter.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
23 #include "ndn-cxx/security/impl/openssl-helper.hpp"
24 
25 #include <boost/lexical_cast.hpp>
26 
27 namespace ndn {
28 namespace security {
29 namespace transform {
30 
31 class HmacFilter::Impl
32 {
33 public:
34  Impl()
35  : key(nullptr)
36  {
37  }
38 
39  ~Impl()
40  {
41  EVP_PKEY_free(key);
42  }
43 
44 public:
45  detail::EvpMdCtx ctx;
46  EVP_PKEY* key;
47 };
48 
49 
50 HmacFilter::HmacFilter(DigestAlgorithm algo, const uint8_t* key, size_t keyLen)
51  : m_impl(make_unique<Impl>())
52 {
53  BOOST_ASSERT(key != nullptr);
54  BOOST_ASSERT(keyLen > 0);
55 
56  const EVP_MD* md = detail::digestAlgorithmToEvpMd(algo);
57  if (md == nullptr)
58  NDN_THROW(Error(getIndex(), "Unsupported digest algorithm " + boost::lexical_cast<std::string>(algo)));
59 
60  m_impl->key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, nullptr, key, static_cast<int>(keyLen));
61  if (m_impl->key == nullptr)
62  NDN_THROW(Error(getIndex(), "Failed to create HMAC key"));
63 
64  if (EVP_DigestSignInit(m_impl->ctx, nullptr, md, nullptr, m_impl->key) != 1)
65  NDN_THROW(Error(getIndex(), "Failed to initialize HMAC context with " +
66  boost::lexical_cast<std::string>(algo) + " digest"));
67 }
68 
69 HmacFilter::~HmacFilter() = default;
70 
71 size_t
72 HmacFilter::convert(const uint8_t* buf, size_t size)
73 {
74  if (EVP_DigestSignUpdate(m_impl->ctx, buf, size) != 1)
75  NDN_THROW(Error(getIndex(), "Failed to accept more input"));
76 
77  return size;
78 }
79 
80 void
81 HmacFilter::finalize()
82 {
83  auto buffer = make_unique<OBuffer>(EVP_MAX_MD_SIZE);
84  size_t hmacLen = 0;
85 
86  if (EVP_DigestSignFinal(m_impl->ctx, buffer->data(), &hmacLen) != 1)
87  NDN_THROW(Error(getIndex(), "Failed to finalize HMAC"));
88 
89  buffer->erase(buffer->begin() + hmacLen, buffer->end());
90  setOutputBuffer(std::move(buffer));
91 
93 }
94 
95 unique_ptr<Transform>
96 hmacFilter(DigestAlgorithm algo, const uint8_t* key, size_t keyLen)
97 {
98  return make_unique<HmacFilter>(algo, key, keyLen);
99 }
100 
101 } // namespace transform
102 } // namespace security
103 } // namespace ndn
Definition: data.cpp:26
unique_ptr< Transform > hmacFilter(DigestAlgorithm algo, const uint8_t *key, size_t keyLen)
Definition: hmac-filter.cpp:96
void flushAllOutput()
Read the all the content from output buffer and write it into next module.
#define NDN_THROW(e)
Definition: exception.hpp:61
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
Base class of transformation error.
HmacFilter(DigestAlgorithm algo, const uint8_t *key, size_t keyLen)
Create a module to generate HMAC using digest algorithm algo and key key.
Definition: hmac-filter.cpp:50
size_t getIndex() const
Get the module index.