Crypto++ 8.4 Free C++ class library of cryptographic schemes

hmac.cpp
1 // hmac.cpp - originally written and placed in the public domain by Wei Dai
2
3 #include "pch.h"
4
5 #ifndef CRYPTOPP_IMPORTS
6
7 #include "hmac.h"
8
9 NAMESPACE_BEGIN(CryptoPP)
10
11 void HMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
12 {
13 AssertValidKeyLength(keylength);
14
15 Restart();
16
17 HashTransformation &hash = AccessHash();
18 unsigned int blockSize = hash.BlockSize();
19
20 if (!blockSize)
21 throw InvalidArgument("HMAC: can only be used with a block-based hash function");
22
23 m_buf.resize(2*AccessHash().BlockSize() + AccessHash().DigestSize());
24
25 if (keylength <= blockSize)
26 {
27 // hmac.cpp:26:9: runtime error: null pointer passed as argument 2
28 if (AccessIpad() && userKey && keylength)
29 memcpy(AccessIpad(), userKey, keylength);
30 }
31 else
32 {
33 AccessHash().CalculateDigest(AccessIpad(), userKey, keylength);
34 keylength = hash.DigestSize();
35 }
36
37 CRYPTOPP_ASSERT(keylength <= blockSize);
38 memset(AccessIpad()+keylength, 0, blockSize-keylength);
39
40 for (unsigned int i=0; i<blockSize; i++)
41 {
42 AccessOpad()[i] = AccessIpad()[i] ^ 0x5c;
43 AccessIpad()[i] ^= 0x36;
44 }
45 }
46
47 void HMAC_Base::KeyInnerHash()
48 {
49 CRYPTOPP_ASSERT(!m_innerHashKeyed);
50 HashTransformation &hash = AccessHash();
51 hash.Update(AccessIpad(), hash.BlockSize());
52 m_innerHashKeyed = true;
53 }
54
55 void HMAC_Base::Restart()
56 {
57 if (m_innerHashKeyed)
58 {
59 AccessHash().Restart();
60 m_innerHashKeyed = false;
61 }
62 }
63
64 void HMAC_Base::Update(const byte *input, size_t length)
65 {
66 if (!m_innerHashKeyed)
67 KeyInnerHash();
68 AccessHash().Update(input, length);
69 }
70
71 void HMAC_Base::TruncatedFinal(byte *mac, size_t size)
72 {
73 ThrowIfInvalidTruncatedSize(size);
74
75 HashTransformation &hash = AccessHash();
76
77 if (!m_innerHashKeyed)
78 KeyInnerHash();
79 hash.Final(AccessInnerHash());
80
81 hash.Update(AccessOpad(), hash.BlockSize());
82 hash.Update(AccessInnerHash(), hash.DigestSize());
83 hash.TruncatedFinal(mac, size);
84
85 m_innerHashKeyed = false;
86 }
87
88 NAMESPACE_END
89
90 #endif
HMAC_Base::Update
void Update(const byte *input, size_t length)
Updates a hash with additional input.
HashTransformation::TruncatedFinal
virtual void TruncatedFinal(byte *digest, size_t digestSize)=0
Computes the hash of the current message.
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1112
HashTransformation::CalculateDigest
virtual void CalculateDigest(byte *digest, const byte *input, size_t length)
Updates the hash with additional input and computes the hash of the current message.
Definition: cryptlib.h:1188
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68
HMAC_Base::TruncatedFinal
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
pch.h
Precompiled header file.
SecBlock::resize
void resize(size_type newSize)
Change size and preserve contents.
Definition: secblock.h:1088
HashTransformation::BlockSize
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:1165
HMAC_Base::Restart
void Restart()
Restart the hash.
HMAC_Base::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: hmac.h:30
HashTransformation::DigestSize
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
InvalidArgument
An invalid argument was detected.
Definition: cryptlib.h:202
CryptoPP
Crypto++ library namespace.
HMAC_Base::UncheckedSetKey
void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
HashTransformation::Restart
virtual void Restart()
Restart the hash.
Definition: cryptlib.h:1147
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:321
hmac.h
Classes for HMAC message authentication codes.
HashTransformation::Update
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
HashTransformation::Final
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:1142