Fabcoin Core  0.16.2
P2P Digital Currency
hmac.cpp
Go to the documentation of this file.
1 // hmac.cpp - 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 
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  memcpy(AccessIpad(), userKey, keylength);
27  else
28  {
29  AccessHash().CalculateDigest(AccessIpad(), userKey, keylength);
30  keylength = hash.DigestSize();
31  }
32 
33  CRYPTOPP_ASSERT(keylength <= blockSize);
34  memset(AccessIpad()+keylength, 0, blockSize-keylength);
35 
36  for (unsigned int i=0; i<blockSize; i++)
37  {
38  AccessOpad()[i] = AccessIpad()[i] ^ 0x5c;
39  AccessIpad()[i] ^= 0x36;
40  }
41 }
42 
44 {
47  hash.Update(AccessIpad(), hash.BlockSize());
48  m_innerHashKeyed = true;
49 }
50 
52 {
53  if (m_innerHashKeyed)
54  {
55  AccessHash().Restart();
56  m_innerHashKeyed = false;
57  }
58 }
59 
60 void HMAC_Base::Update(const byte *input, size_t length)
61 {
62  if (!m_innerHashKeyed)
63  KeyInnerHash();
64  AccessHash().Update(input, length);
65 }
66 
68 {
70 
72 
73  if (!m_innerHashKeyed)
74  KeyInnerHash();
75  hash.Final(AccessInnerHash());
76 
77  hash.Update(AccessOpad(), hash.BlockSize());
78  hash.Update(AccessInnerHash(), hash.DigestSize());
79  hash.TruncatedFinal(mac, size);
80 
81  m_innerHashKeyed = false;
82 }
83 
85 
86 #endif
An invalid argument was detected.
Definition: cryptlib.h:184
uint8_t byte
Definition: Common.h:57
virtual void TruncatedFinal(byte *digest, size_t digestSize)=0
Computes the hash of the current message.
byte * AccessOpad()
Definition: hmac.h:33
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
void ThrowIfInvalidTruncatedSize(size_t size) const
Validates a truncated digest size.
Definition: cryptlib.cpp:416
Classes for HMAC message authentication codes.
byte * AccessInnerHash()
Definition: hmac.h:34
HMAC information.
Definition: hmac.h:17
virtual void Restart()
Restart the hash.
Definition: cryptlib.h:965
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: hmac.cpp:60
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:981
void Restart()
Restart the hash.
Definition: hmac.cpp:51
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
Definition: hmac.cpp:67
bool m_innerHashKeyed
Definition: hmac.h:40
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
uint8_t const size_t const size
Definition: sha3.h:20
void * memcpy(void *a, const void *b, size_t c)
void KeyInnerHash()
Definition: hmac.cpp:43
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:930
byte * AccessIpad()
Definition: hmac.h:32
#define NAMESPACE_END
Definition: config.h:201
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:960
virtual HashTransformation & AccessHash()=0
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
Interface for retrieving values given their names.
Definition: cryptlib.h:279