source: trunk/src-cryptopp/hmac.h

Last change on this file was e230cb0, checked in by David Stainton <dstainton415@…>, at 2016-10-12T13:27:29Z

Add cryptopp from tag CRYPTOPP_5_6_5

  • Property mode set to 100644
File size: 2.4 KB
Line 
1// hmac.h - written and placed in the public domain by Wei Dai
2
3//! \file hmac.h
4//! \brief Classes for HMAC message authentication codes
5
6#ifndef CRYPTOPP_HMAC_H
7#define CRYPTOPP_HMAC_H
8
9#include "seckey.h"
10#include "secblock.h"
11
12NAMESPACE_BEGIN(CryptoPP)
13
14//! \class HMAC_Base
15//! \brief HMAC information
16//! \details HMAC_Base derives from VariableKeyLength and MessageAuthenticationCode
17class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
18{
19public:
20        //! \brief Construct a HMAC_Base
21        HMAC_Base() : m_innerHashKeyed(false) {}
22        void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
23
24        void Restart();
25        void Update(const byte *input, size_t length);
26        void TruncatedFinal(byte *mac, size_t size);
27        unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
28        unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
29
30protected:
31        virtual HashTransformation & AccessHash() =0;
32        byte * AccessIpad() {return m_buf;}
33        byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
34        byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
35
36private:
37        void KeyInnerHash();
38
39        SecByteBlock m_buf;
40        bool m_innerHashKeyed;
41};
42
43//! \class HMAC
44//! \brief HMAC
45//! \tparam T HashTransformation derived class
46//! \details HMAC derives from MessageAuthenticationCodeImpl. It calculates the HMAC using
47//!   <tt>HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text))</tt>.
48//! \sa <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
49template <class T>
50class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
51{
52public:
53        CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE)
54        CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE)
55
56        //! \brief Construct a HMAC
57        HMAC() {}
58        //! \brief Construct a HMAC
59        //! \param key the HMAC key
60        //! \param length the size of the HMAC key
61        HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
62                {this->SetKey(key, length);}
63
64        static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
65        std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
66
67private:
68        HashTransformation & AccessHash() {return m_hash;}
69
70        T m_hash;
71};
72
73NAMESPACE_END
74
75#endif
Note: See TracBrowser for help on using the repository browser.