libcruft-util/hash/hmac.cpp

128 lines
3.2 KiB
C++
Raw Normal View History

2015-03-02 00:06:47 +11:00
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "hmac.hpp"
#include "debug.hpp"
#include <algorithm>
using util::hash::HMAC;
//-----------------------------------------------------------------------------
static const uint8_t IFILL = 0x36;
static const uint8_t OFILL = 0x5C;
//-----------------------------------------------------------------------------
2015-03-02 01:21:52 +11:00
template <class T>
HMAC<T>::HMAC (const uint8_t *restrict key, size_t len)
2015-03-02 00:06:47 +11:00
{
CHECK (key);
static_assert (sizeof (m_ikey) == sizeof (m_okey), "key padding must match");
// If the key is larger than the blocklength, use the hash of the key
2015-03-02 01:21:52 +11:00
if (len > T::BLOCK_SIZE) {
m_hash.update (key, len);
m_hash.finish ();
auto d = m_hash.digest ();
m_hash.reset ();
std::copy (d.begin (), d.end (), m_ikey.begin ());
len = d.size ();
// Use the key directly
} else {
std::copy (key, key + len, m_ikey.begin ());
}
2015-03-02 00:06:47 +11:00
std::fill (m_ikey.begin () + len,
m_ikey.end (),
0);
m_okey = m_ikey;
std::transform (m_ikey.begin (),
m_ikey.end (),
m_ikey.begin (),
[] (auto v) { return v ^ IFILL; });
std::transform (m_okey.begin (),
m_okey.end (),
m_okey.begin (),
[] (auto v) { return v ^ OFILL; });
m_hash.update (m_ikey.data (), m_ikey.size ());
}
//-----------------------------------------------------------------------------
2015-03-02 01:21:52 +11:00
template <class T>
2015-03-02 00:06:47 +11:00
void
2015-03-02 01:21:52 +11:00
HMAC<T>::update (const void *restrict data, size_t len)
2015-03-02 00:06:47 +11:00
{
m_hash.update ((const uint8_t*)data, len);
}
//-----------------------------------------------------------------------------
2015-03-02 01:21:52 +11:00
template <class T>
2015-03-02 00:06:47 +11:00
void
2015-03-02 01:21:52 +11:00
HMAC<T>::finish (void)
2015-03-02 00:06:47 +11:00
{
m_hash.finish ();
auto d = m_hash.digest ();
m_hash.reset ();
m_hash.update (m_okey.data (), m_okey.size ());
m_hash.update (d.data (), d.size ());
m_hash.finish ();
}
//-----------------------------------------------------------------------------
2015-03-02 01:21:52 +11:00
template <class T>
2015-03-02 00:06:47 +11:00
void
2015-03-02 01:21:52 +11:00
HMAC<T>::reset (void)
2015-03-02 00:06:47 +11:00
{
m_hash.reset ();
m_hash.update (m_ikey.data (), m_ikey.size ());
}
//-----------------------------------------------------------------------------
2015-03-02 01:21:52 +11:00
template <class T>
typename HMAC<T>::digest_t
HMAC<T>::digest (void)
2015-03-02 00:06:47 +11:00
{
return m_hash.digest ();
}
2015-03-02 01:21:52 +11:00
//-----------------------------------------------------------------------------
#include "md5.hpp"
#include "sha1.hpp"
namespace util { namespace hash {
template class HMAC<MD5>;
template class HMAC<SHA1>;
} }