summaryrefslogtreecommitdiff
path: root/eax.cpp
blob: 496762a91815bbe63ee9f600537e10e604afd1cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// eax.cpp - originally written and placed in the public domain by Wei Dai

#include "pch.h"
#include "eax.h"

NAMESPACE_BEGIN(CryptoPP)

void EAX_Base::SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params)
{
	AccessMAC().SetKey(userKey, keylength, params);
	m_buffer.New(2*AccessMAC().TagSize());
}

void EAX_Base::Resync(const byte *iv, size_t len)
{
	MessageAuthenticationCode &mac = AccessMAC();
	unsigned int blockSize = mac.TagSize();

	std::memset(m_buffer, 0, blockSize);
	mac.Update(m_buffer, blockSize);
	mac.CalculateDigest(m_buffer+blockSize, iv, len);

	m_buffer[blockSize-1] = 1;
	mac.Update(m_buffer, blockSize);

	m_ctr.SetCipherWithIV(AccessMAC().AccessCipher(), m_buffer+blockSize, blockSize);
}

size_t EAX_Base::AuthenticateBlocks(const byte *data, size_t len)
{
	AccessMAC().Update(data, len);
	return 0;
}

void EAX_Base::AuthenticateLastHeaderBlock()
{
	CRYPTOPP_ASSERT(m_bufferedDataLength == 0);
	MessageAuthenticationCode &mac = AccessMAC();
	const unsigned int blockSize = mac.TagSize();

	mac.Final(m_buffer);
	xorbuf(m_buffer+blockSize, m_buffer, blockSize);

	std::memset(m_buffer, 0, blockSize);
	m_buffer[blockSize-1] = 2;
	mac.Update(m_buffer, blockSize);
}

void EAX_Base::AuthenticateLastFooterBlock(byte *tag, size_t macSize)
{
	CRYPTOPP_ASSERT(m_bufferedDataLength == 0);
	MessageAuthenticationCode &mac = AccessMAC();
	unsigned int blockSize = mac.TagSize();

	mac.TruncatedFinal(m_buffer, macSize);
	xorbuf(tag, m_buffer, m_buffer+blockSize, macSize);
}

NAMESPACE_END