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
|
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "tests.h"
#if defined(BOTAN_HAS_AEAD_MODES)
#include <botan/hex.h>
#include <botan/aead.h>
#include <iostream>
#include <fstream>
#include <memory>
using namespace Botan;
namespace {
size_t aead_test(const std::string& algo,
const std::string& input,
const std::string& expected,
const std::string& nonce_hex,
const std::string& ad_hex,
const std::string& key_hex)
{
const auto nonce = hex_decode_locked(nonce_hex);
const auto ad = hex_decode_locked(ad_hex);
const auto key = hex_decode_locked(key_hex);
std::unique_ptr<Cipher_Mode> enc(get_aead(algo, ENCRYPTION));
std::unique_ptr<Cipher_Mode> dec(get_aead(algo, DECRYPTION));
if(!enc || !dec)
throw std::runtime_error("Unknown AEAD '" + algo + "'");
enc->set_key(key);
dec->set_key(key);
if(auto aead_enc = dynamic_cast<AEAD_Mode*>(enc.get()))
aead_enc->set_associated_data_vec(ad);
if(auto aead_dec = dynamic_cast<AEAD_Mode*>(dec.get()))
aead_dec->set_associated_data_vec(ad);
size_t fail = 0;
const auto pt = hex_decode_locked(input);
const auto expected_ct = hex_decode_locked(expected);
auto vec = pt;
enc->start(nonce);
// should first update if possible
enc->finish(vec);
if(vec != expected_ct)
{
std::cout << algo << " got ct " << hex_encode(vec) << " expected " << expected << std::endl;
++fail;
}
vec = expected_ct;
dec->start(nonce);
dec->finish(vec);
if(vec != pt)
{
std::cout << algo << " got pt " << hex_encode(vec) << " expected " << input << std::endl;
++fail;
}
if(enc->authenticated())
{
vec = expected_ct;
vec[0] ^= 1;
dec->start(nonce);
try
{
dec->finish(vec);
std::cout << algo << " accepted message with modified message" << std::endl;
++fail;
}
catch(...) {}
if(nonce.size())
{
auto bad_nonce = nonce;
bad_nonce[0] ^= 1;
vec = expected_ct;
dec->start(bad_nonce);
try
{
dec->finish(vec);
std::cout << algo << " accepted message with modified nonce" << std::endl;
++fail;
}
catch(...) {}
}
if(auto aead_dec = dynamic_cast<AEAD_Mode*>(dec.get()))
{
auto bad_ad = ad;
if(ad.size())
bad_ad[0] ^= 1;
else
bad_ad.push_back(0);
aead_dec->set_associated_data_vec(bad_ad);
vec = expected_ct;
dec->start(nonce);
try
{
dec->finish(vec);
std::cout << algo << " accepted message with modified AD" << std::endl;
++fail;
}
catch(...) {}
}
}
return fail;
}
}
size_t test_aead()
{
auto test = [](const std::string& input)
{
std::ifstream vec(input);
return run_tests_bb(vec, "AEAD", "Out", true,
[](std::map<std::string, std::string> m)
{
return aead_test(m["AEAD"], m["In"], m["Out"],
m["Nonce"], m["AD"], m["Key"]);
});
};
return run_tests_in_dir(TEST_DATA_DIR "/aead", test);
}
#else
SKIP_TEST(aead);
#endif // BOTAN_HAS_AEAD_MODES
|