2015-03-02 00:06:47 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-03-02 00:06:47 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-03-02 00:06:47 +11:00
|
|
|
*
|
|
|
|
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hmac.hpp"
|
|
|
|
|
2016-04-05 11:06:01 +10:00
|
|
|
#include "../debug.hpp"
|
2015-03-02 00:06:47 +11:00
|
|
|
|
|
|
|
#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");
|
|
|
|
|
2015-03-02 00:35:58 +11:00
|
|
|
// 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) {
|
2015-03-02 00:35:58 +11:00
|
|
|
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"
|
|
|
|
|
2016-10-11 23:47:57 +11:00
|
|
|
namespace util::hash {
|
2015-03-06 02:02:27 +11:00
|
|
|
template class HMAC<MD5>;
|
|
|
|
template class HMAC<SHA1>;
|
2016-10-11 23:47:57 +11:00
|
|
|
}
|