23 #include "../detail/openssl-helper.hpp" 25 #include <boost/lexical_cast.hpp> 31 class HmacFilter::Impl
53 BOOST_ASSERT(key !=
nullptr);
54 BOOST_ASSERT(keyLen > 0);
56 const EVP_MD* md = detail::digestAlgorithmToEvpMd(algo);
58 BOOST_THROW_EXCEPTION(
Error(
getIndex(),
"Unsupported digest algorithm " +
59 boost::lexical_cast<std::string>(algo)));
61 m_impl->key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC,
nullptr, key, static_cast<int>(keyLen));
62 if (m_impl->key ==
nullptr)
63 BOOST_THROW_EXCEPTION(
Error(
getIndex(),
"Failed to create HMAC key"));
65 if (EVP_DigestSignInit(m_impl->ctx,
nullptr, md,
nullptr, m_impl->key) != 1)
66 BOOST_THROW_EXCEPTION(
Error(
getIndex(),
"Failed to initialize HMAC context with " +
67 boost::lexical_cast<std::string>(algo) +
" digest"));
73 HmacFilter::convert(
const uint8_t* buf,
size_t size)
75 if (EVP_DigestSignUpdate(m_impl->ctx, buf, size) != 1)
76 BOOST_THROW_EXCEPTION(
Error(
getIndex(),
"Failed to accept more input"));
82 HmacFilter::finalize()
84 auto buffer = make_unique<OBuffer>(EVP_MAX_MD_SIZE);
87 if (EVP_DigestSignFinal(m_impl->ctx, buffer->data(), &hmacLen) != 1)
88 BOOST_THROW_EXCEPTION(
Error(
getIndex(),
"Failed to finalize HMAC"));
90 buffer->erase(buffer->begin() + hmacLen, buffer->end());
99 return make_unique<HmacFilter>(algo, key, keyLen);
Copyright (c) 2013-2017 Regents of the University of California.
unique_ptr< T > make_unique(Args &&...args)