Crypto++  5.6.3
Free C++ class library of cryptographic schemes
eax.cpp
1 // eax.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "eax.h"
5 
6 NAMESPACE_BEGIN(CryptoPP)
7 
8 void EAX_Base::SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params)
9 {
10  AccessMAC().SetKey(userKey, keylength, params);
11  m_buffer.New(2*AccessMAC().TagSize());
12 }
13 
14 void EAX_Base::Resync(const byte *iv, size_t len)
15 {
16  MessageAuthenticationCode &mac = AccessMAC();
17  unsigned int blockSize = mac.TagSize();
18 
19  memset(m_buffer, 0, blockSize);
20  mac.Update(m_buffer, blockSize);
21  mac.CalculateDigest(m_buffer+blockSize, iv, len);
22 
23  m_buffer[blockSize-1] = 1;
24  mac.Update(m_buffer, blockSize);
25 
26  m_ctr.SetCipherWithIV(AccessMAC().AccessCipher(), m_buffer+blockSize, blockSize);
27 }
28 
29 size_t EAX_Base::AuthenticateBlocks(const byte *data, size_t len)
30 {
31  AccessMAC().Update(data, len);
32  return 0;
33 }
34 
35 void EAX_Base::AuthenticateLastHeaderBlock()
36 {
37  assert(m_bufferedDataLength == 0);
38  MessageAuthenticationCode &mac = AccessMAC();
39  unsigned int blockSize = mac.TagSize();
40 
41  mac.Final(m_buffer);
42  xorbuf(m_buffer+blockSize, m_buffer, blockSize);
43 
44  memset(m_buffer, 0, blockSize);
45  m_buffer[blockSize-1] = 2;
46  mac.Update(m_buffer, blockSize);
47 }
48 
49 void EAX_Base::AuthenticateLastFooterBlock(byte *tag, size_t macSize)
50 {
51  assert(m_bufferedDataLength == 0);
52  MessageAuthenticationCode &mac = AccessMAC();
53  unsigned int blockSize = mac.TagSize();
54 
55  mac.TruncatedFinal(m_buffer, macSize);
56  xorbuf(tag, m_buffer, m_buffer+blockSize, macSize);
57 }
58 
59 NAMESPACE_END
Interface for message authentication codes.
Definition: cryptlib.h:1107
virtual void TruncatedFinal(byte *digest, size_t digestSize)=0
Computes the hash of the current message.
unsigned int TagSize() const
Provides the tag size of the hash.
Definition: cryptlib.h:969
EAX block cipher base implementation.
Definition: eax.h:18
void SetKey(const byte *userKey, size_t keylength, const NameValuePairs &params)
Sets or reset the key of this object.
Definition: authenc.cpp:48
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:997
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Definition: misc.cpp:28
EAX block cipher mode of operation.
Crypto++ library namespace.
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:954
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: cmac.cpp:58
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:277