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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/*
* EAX Mode Encryption
* (C) 1999-2007 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/eax.h>
#include <botan/cmac.h>
#include <botan/ctr.h>
#include <botan/parsing.h>
namespace Botan {
namespace {
/*
* EAX MAC-based PRF
*/
secure_vector<byte> eax_prf(byte tag, size_t block_size,
MessageAuthenticationCode& mac,
const byte in[], size_t length)
{
for(size_t i = 0; i != block_size - 1; ++i)
mac.update(0);
mac.update(tag);
mac.update(in, length);
return mac.final();
}
}
/*
* EAX_Mode Constructor
*/
EAX_Mode::EAX_Mode(BlockCipher* cipher, size_t tag_size) :
m_tag_size(tag_size ? tag_size : cipher->block_size()),
m_cipher(cipher),
m_ctr(new CTR_BE(m_cipher->clone())),
m_cmac(new CMAC(m_cipher->clone()))
{
if(m_tag_size < 8 || m_tag_size > m_cmac->output_length())
throw Invalid_Argument(name() + ": Bad tag size " + std::to_string(tag_size));
}
void EAX_Mode::clear()
{
m_cipher.reset();
m_ctr.reset();
m_cmac.reset();
zeroise(m_ad_mac);
zeroise(m_nonce_mac);
}
std::string EAX_Mode::name() const
{
return (m_cipher->name() + "/EAX");
}
size_t EAX_Mode::update_granularity() const
{
return 1;
}
Key_Length_Specification EAX_Mode::key_spec() const
{
return m_cipher->key_spec();
}
/*
* Set the EAX key
*/
void EAX_Mode::key_schedule(const byte key[], size_t length)
{
/*
* These could share the key schedule, which is one nice part of EAX,
* but it's much easier to ignore that here...
*/
m_ctr->set_key(key, length);
m_cmac->set_key(key, length);
m_ad_mac = eax_prf(1, block_size(), *m_cmac, nullptr, 0);
}
/*
* Set the EAX associated data
*/
void EAX_Mode::set_associated_data(const byte ad[], size_t length)
{
m_ad_mac = eax_prf(1, block_size(), *m_cmac, ad, length);
}
void EAX_Mode::start_msg(const byte nonce[], size_t nonce_len)
{
if(!valid_nonce_length(nonce_len))
throw Invalid_IV_Length(name(), nonce_len);
m_nonce_mac = eax_prf(0, block_size(), *m_cmac, nonce, nonce_len);
m_ctr->set_iv(m_nonce_mac.data(), m_nonce_mac.size());
for(size_t i = 0; i != block_size() - 1; ++i)
m_cmac->update(0);
m_cmac->update(2);
}
size_t EAX_Encryption::process(uint8_t buf[], size_t sz)
{
m_ctr->cipher(buf, buf, sz);
m_cmac->update(buf, sz);
return sz;
}
void EAX_Encryption::finish(secure_vector<byte>& buffer, size_t offset)
{
update(buffer, offset);
secure_vector<byte> data_mac = m_cmac->final();
xor_buf(data_mac, m_nonce_mac, data_mac.size());
xor_buf(data_mac, m_ad_mac, data_mac.size());
buffer += std::make_pair(data_mac.data(), tag_size());
}
size_t EAX_Decryption::process(uint8_t buf[], size_t sz)
{
m_cmac->update(buf, sz);
m_ctr->cipher(buf, buf, sz);
return sz;
}
void EAX_Decryption::finish(secure_vector<byte>& buffer, size_t offset)
{
BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
const size_t sz = buffer.size() - offset;
byte* buf = buffer.data() + offset;
BOTAN_ASSERT(sz >= tag_size(), "Have the tag as part of final input");
const size_t remaining = sz - tag_size();
if(remaining)
{
m_cmac->update(buf, remaining);
m_ctr->cipher(buf, buf, remaining);
}
const byte* included_tag = &buf[remaining];
secure_vector<byte> mac = m_cmac->final();
mac ^= m_nonce_mac;
mac ^= m_ad_mac;
if(!same_mem(mac.data(), included_tag, tag_size()))
throw Integrity_Failure("EAX tag check failed");
buffer.resize(offset + remaining);
}
}
|