-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_cipher.cpp
353 lines (276 loc) · 9.51 KB
/
test_cipher.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <catch2/catch.hpp>
#include "generator.hpp"
#include "mbedcrypto/cipher.hpp"
#include "mbedcrypto/rnd_generator.hpp"
#include "mbedcrypto/tcodec.hpp"
#include "src/conversions.hpp"
#include "mbedtls/cipher.h"
#include <iostream>
///////////////////////////////////////////////////////////////////////////////
namespace {
using namespace mbedcrypto;
///////////////////////////////////////////////////////////////////////////////
class sub_finder
{
const char* pstr = nullptr;
public:
explicit sub_finder(const char* name) : pstr(name) {}
sub_finder() = delete;
~sub_finder() = default;
bool contains(const char* part) const {
if (part == nullptr || pstr == nullptr)
throw std::logic_error("invalid arguments");
return strstr(pstr, part) != nullptr;
}
}; // sub_finder
padding_t
padding_of(cipher_bm bm) {
switch (bm) {
case cipher_bm::cbc:
return padding_t::pkcs7;
case cipher_bm::ecb:
case cipher_bm::cfb:
case cipher_bm::ctr:
case cipher_bm::stream:
case cipher_bm::gcm:
case cipher_bm::ccm:
return padding_t::none;
default:
return padding_t::none;
}
}
size_t
chunk_size_of(cipher_t ct) {
switch (cipher::block_mode(ct)) {
case cipher_bm::ecb:
return cipher::block_size(ct);
case cipher_bm::cbc:
case cipher_bm::cfb:
case cipher_bm::ctr:
case cipher_bm::stream:
case cipher_bm::gcm:
case cipher_bm::ccm:
return 160; // custom value, could be any value > 0
default:
throw std::logic_error(
std::string("invalid cipher type: ") + to_string(ct));
return 0;
}
}
buffer_t
make_input(cipher_bm bm, size_t bs, rnd_generator& drbg) {
switch (bm) {
case cipher_bm::ecb:
return drbg.make(100 * bs);
case cipher_bm::cbc:
case cipher_bm::cfb:
case cipher_bm::ctr:
case cipher_bm::stream:
case cipher_bm::gcm:
case cipher_bm::ccm:
return drbg.make(3241);
default: // not supported types
return buffer_t();
}
}
buffer_t
chunker_impl(size_t chunk_size, const buffer_t& input, cipher& cip) {
cip.start();
size_t isize = input.size();
size_t osize = isize + cip.block_size() + 32;
buffer_t output(osize, '\0');
size_t i_index = 0;
size_t o_index = 0;
// blocks
size_t chunks = isize / chunk_size;
for (size_t i = 0; i < chunks; ++i) {
o_index += cip.update(input, i_index, chunk_size, output, o_index);
i_index += chunk_size;
}
// last block
size_t residue = isize % chunk_size;
if (residue)
o_index += cip.update(input, i_index, residue, output, o_index);
// finalize
o_index += cip.finish(output, o_index);
output.resize(o_index);
return output;
}
std::string
AdditionalData() {
return "some additional data!\n"
"may be transferred in plain text if you like.";
}
///////////////////////////////////////////////////////////////////////////////
struct cipher_tester {
cipher_t ctype = cipher_t::none;
padding_t padding_mode = padding_t::none;
cipher_bm block_mode = cipher_bm::none;
size_t block_size = 0;
size_t key_size = 0;
size_t iv_size = 0;
size_t chunk_size = 0;
buffer_t iv;
buffer_t key;
buffer_t input;
cipher cipenc;
cipher cipdec;
public:
explicit cipher_tester(cipher_t ct) : ctype(ct), cipenc(ct), cipdec(ct) {}
bool setup(rnd_generator& drbg) {
// properties
block_mode = cipher::block_mode(ctype);
block_size = cipher::block_size(ctype);
key_size = cipher::key_bitlen(ctype) / 8; // bits to bytes
iv_size = cipher::iv_size(ctype);
chunk_size = chunk_size_of(ctype);
padding_mode = padding_of(block_mode);
// input parameters
iv = drbg.make(iv_size);
key = drbg.make(key_size);
input = make_input(block_mode, block_size, drbg);
if (input.empty()) // not supported yet
return false;
// children
cipenc.padding(padding_mode)
.iv(iv)
.key(key, cipher::encrypt_mode);
cipdec.padding(padding_mode)
.iv(iv)
.key(key, cipher::decrypt_mode);
return true;
}
void one_shot() {
auto encr = cipher::encrypt(ctype, padding_mode, iv, key, input);
auto decr = cipher::decrypt(ctype, padding_mode, iv, key, encr);
INFO(to_string(ctype));
REQUIRE((decr == input));
#if defined(QT_CORE_LIB)
auto qencr = cipher::encrypt<QByteArray>(ctype, padding_mode, iv, key, input);
auto qdecr = cipher::decrypt<QByteArray>(ctype, padding_mode, iv, key, qencr);
INFO(to_string(ctype));
REQUIRE((qencr == encr));
REQUIRE((qdecr == decr));
#endif // QT_CORE_LIB
}
void one_shot_prepend() {
auto encr = cipher::pencrypt(ctype, padding_mode, iv, key, input);
auto decr = cipher::pdecrypt(ctype, padding_mode, key, encr);
INFO(to_string(ctype));
REQUIRE((decr == input));
#if defined(QT_CORE_LIB)
auto qencr = cipher::pencrypt<QByteArray>(ctype, padding_mode, iv, key, input);
auto qdecr = cipher::pdecrypt<QByteArray>(ctype, padding_mode, key, encr);
INFO(to_string(ctype));
REQUIRE((qencr == encr));
REQUIRE((qdecr == decr));
#endif // QT_CORE_LIB
}
void by_object() {
cipenc.start();
auto encr = cipenc.update(input);
encr.append(cipenc.finish());
cipdec.start();
auto decr = cipdec.update(encr);
decr.append(cipdec.finish());
REQUIRE((decr == input));
}
void by_object_chunked() {
auto encr = chunker_impl(chunk_size, input, cipenc);
auto decr = chunker_impl(chunk_size, encr, cipdec);
REQUIRE((decr == input));
}
void aead_one_shot() {
auto encr =
cipher::encrypt_aead(ctype, iv, key, AdditionalData(), input);
auto decr =
cipher::decrypt_aead(ctype, iv, key, AdditionalData(), encr);
INFO(to_string(ctype));
REQUIRE(std::get<0>(decr)); // result status
REQUIRE((std::get<1>(decr) == input));
}
void gcm_check() {
cipenc.start();
cipenc.gcm_additional_data(AdditionalData());
auto encr = cipenc.update(input);
encr.append(cipenc.finish());
auto tag = cipenc.gcm_encryption_tag(16);
REQUIRE(tag.size() == 16);
cipdec.start();
cipdec.gcm_additional_data(AdditionalData());
auto decr = cipdec.update(encr);
decr.append(cipdec.finish());
REQUIRE((decr == input));
REQUIRE(cipdec.gcm_check_decryption_tag(tag));
}
}; // struct cipher_tester
///////////////////////////////////////////////////////////////////////////////
} // namespace anon
///////////////////////////////////////////////////////////////////////////////
TEST_CASE("test block mode", "[cipher][types]") {
using namespace mbedcrypto;
SECTION("block modes") {
const auto ciphers = installed_ciphers();
for (const auto t : ciphers) {
if (!supports(t))
continue;
sub_finder f(to_string(t));
if (f.contains("ECB")) {
REQUIRE(cipher::block_mode(t) == cipher_bm::ecb);
} else if (f.contains("CBC")) {
REQUIRE(cipher::block_mode(t) == cipher_bm::cbc);
} else if (f.contains("CFB")) {
REQUIRE(cipher::block_mode(t) == cipher_bm::cfb);
} else if (f.contains("CTR")) {
REQUIRE(cipher::block_mode(t) == cipher_bm::ctr);
} else if (f.contains("GCM")) {
REQUIRE(cipher::block_mode(t) == cipher_bm::gcm);
} else if (f.contains("CCM")) {
REQUIRE(cipher::block_mode(t) == cipher_bm::ccm);
} else if (t == cipher_t::arc4_128) {
REQUIRE(cipher::block_mode(t) == cipher_bm::stream);
}
}
}
}
TEST_CASE("test ciphers against mbedtls", "[cipher]") {
using namespace mbedcrypto;
rnd_generator drbg;
const auto types = installed_ciphers();
for (auto ctype : types) {
try {
cipher_tester tester(ctype);
if (!tester.setup(drbg)) {
std::cerr << "not supported yet: " << to_string(ctype)
<< std::endl;
continue;
}
// ccm only works in aead mode
if (tester.block_mode != cipher_bm::ccm) {
// single shot calls
tester.one_shot();
// single shot with prepending iv
tester.one_shot_prepend();
// cipher object
// single start()/update()/finish()
tester.by_object();
// by many chunked updates
tester.by_object_chunked();
}
// aead tests
if (tester.block_mode == cipher_bm::gcm ||
tester.block_mode == cipher_bm::ccm) {
tester.aead_one_shot();
}
// gcm special checks
if (tester.block_mode == cipher_bm::gcm) {
tester.gcm_check();
}
} catch (mbedcrypto::exception& cerr) {
std::cerr << "error(" << to_string(ctype) << ") :" << cerr.what()
<< std::endl;
REQUIRE_FALSE("exception failure");
}
}
}
///////////////////////////////////////////////////////////////////////////////