monotone

monotone Mtn Source Tree

Root/cryptopp/hmac.h

1// hmac.h - written and placed in the public domain by Wei Dai
2
3#ifndef CRYPTOPP_HMAC_H
4#define CRYPTOPP_HMAC_H
5
6#include "seckey.h"
7#include "secblock.h"
8
9NAMESPACE_BEGIN(CryptoPP)
10
11template <class T>
12class HMAC_Base : public VariableKeyLength<16, 0, UINT_MAX>, public MessageAuthenticationCode
13{
14public:
15static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
16
17// put enums here for Metrowerks 4
18enum {DIGESTSIZE=T::DIGESTSIZE, BLOCKSIZE=T::BLOCKSIZE};
19
20HMAC_Base() : m_innerHashKeyed(false) {}
21void UncheckedSetKey(const byte *userKey, unsigned int keylength);
22
23void Restart();
24void Update(const byte *input, unsigned int length);
25void TruncatedFinal(byte *mac, unsigned int size);
26unsigned int DigestSize() const {return DIGESTSIZE;}
27
28private:
29void KeyInnerHash();
30
31enum {IPAD=0x36, OPAD=0x5c};
32
33FixedSizeSecBlock<byte, BLOCKSIZE> k_ipad, k_opad;
34FixedSizeSecBlock<byte, DIGESTSIZE> m_innerHash;
35T m_hash;
36bool m_innerHashKeyed;
37};
38
39//! <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
40/*! HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text)) */
41template <class T>
42class HMAC : public MessageAuthenticationCodeTemplate<HMAC_Base<T> >
43{
44public:
45HMAC() {}
46HMAC(const byte *key, unsigned int length=HMAC_Base<T>::DEFAULT_KEYLENGTH)
47{SetKey(key, length);}
48};
49
50template <class T>
51void HMAC_Base<T>::UncheckedSetKey(const byte *userKey, unsigned int keylength)
52{
53AssertValidKeyLength(keylength);
54
55Restart();
56
57if (keylength <= T::BLOCKSIZE)
58memcpy(k_ipad, userKey, keylength);
59else
60{
61m_hash.CalculateDigest(k_ipad, userKey, keylength);
62keylength = T::DIGESTSIZE;
63}
64
65assert(keylength <= T::BLOCKSIZE);
66memset(k_ipad+keylength, 0, T::BLOCKSIZE-keylength);
67
68for (unsigned int i=0; i<T::BLOCKSIZE; i++)
69{
70k_opad[i] = k_ipad[i] ^ OPAD;
71k_ipad[i] ^= IPAD;
72}
73}
74
75template <class T>
76void HMAC_Base<T>::KeyInnerHash()
77{
78assert(!m_innerHashKeyed);
79m_hash.Update(k_ipad, T::BLOCKSIZE);
80m_innerHashKeyed = true;
81}
82
83template <class T>
84void HMAC_Base<T>::Restart()
85{
86if (m_innerHashKeyed)
87{
88m_hash.Restart();
89m_innerHashKeyed = false;
90}
91}
92
93template <class T>
94void HMAC_Base<T>::Update(const byte *input, unsigned int length)
95{
96if (!m_innerHashKeyed)
97KeyInnerHash();
98m_hash.Update(input, length);
99}
100
101template <class T>
102void HMAC_Base<T>::TruncatedFinal(byte *mac, unsigned int size)
103{
104ThrowIfInvalidTruncatedSize(size);
105
106if (!m_innerHashKeyed)
107KeyInnerHash();
108m_hash.Final(m_innerHash);
109
110m_hash.Update(k_opad, T::BLOCKSIZE);
111m_hash.Update(m_innerHash, DIGESTSIZE);
112m_hash.TruncatedFinal(mac, size);
113
114m_innerHashKeyed = false;
115}
116
117NAMESPACE_END
118
119#endif

Archive Download this file

Branches

Tags

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