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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/*
* (C) 2009 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "tests.h"
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
#include "test_rng.h"
#include "test_pubkey.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <memory>
#include <botan/oids.h>
#include <botan/x509_key.h>
#include <botan/pkcs8.h>
#include <botan/pubkey.h>
#if defined(BOTAN_HAS_RSA)
#include <botan/rsa.h>
#endif
#if defined(BOTAN_HAS_DSA)
#include <botan/dsa.h>
#endif
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
#include <botan/dh.h>
#endif
#if defined(BOTAN_HAS_NYBERG_RUEPPEL)
#include <botan/nr.h>
#endif
#if defined(BOTAN_HAS_RW)
#include <botan/rw.h>
#endif
#if defined(BOTAN_HAS_ELGAMAL)
#include <botan/elgamal.h>
#endif
#if defined(BOTAN_HAS_ECDSA)
#include <botan/ecdsa.h>
#endif
#if defined(BOTAN_HAS_ECDH)
#include <botan/ecdh.h>
#endif
#if defined(BOTAN_HAS_GOST_34_10_2001)
#include <botan/gost_3410.h>
#endif
#if defined(BOTAN_HAS_DLIES)
#include <botan/dlies.h>
#include <botan/kdf.h>
#endif
#include <botan/filters.h>
#include <botan/numthry.h>
using namespace Botan;
namespace {
void dump_data(const std::vector<byte>& out,
const std::vector<byte>& expected)
{
Pipe pipe(new Hex_Encoder);
pipe.process_msg(out);
pipe.process_msg(expected);
std::cout << "Got: " << pipe.read_all_as_string(0) << std::endl;
std::cout << "Exp: " << pipe.read_all_as_string(1) << std::endl;
}
size_t validate_save_and_load(const Private_Key* priv_key,
RandomNumberGenerator& rng)
{
std::string name = priv_key->algo_name();
size_t fails = 0;
std::string pub_pem = X509::PEM_encode(*priv_key);
try
{
DataSource_Memory input_pub(pub_pem);
std::unique_ptr<Public_Key> restored_pub(X509::load_key(input_pub));
if(!restored_pub.get())
{
std::cout << "Could not recover " << name << " public key" << std::endl;
++fails;
}
else if(restored_pub->check_key(rng, true) == false)
{
std::cout << "Restored pubkey failed self tests " << name << std::endl;
++fails;
}
}
catch(std::exception& e)
{
std::cout << "Exception during load of " << name
<< " key: " << e.what() << std::endl;
std::cout << "PEM for pubkey was:\n" << pub_pem << std::endl;
++fails;
}
std::string priv_pem = PKCS8::PEM_encode(*priv_key);
try
{
DataSource_Memory input_priv(priv_pem);
std::unique_ptr<Private_Key> restored_priv(
PKCS8::load_key(input_priv, rng));
if(!restored_priv.get())
{
std::cout << "Could not recover " << name << " privlic key" << std::endl;
++fails;
}
else if(restored_priv->check_key(rng, true) == false)
{
std::cout << "Restored privkey failed self tests " << name << std::endl;
++fails;
}
}
catch(std::exception& e)
{
std::cout << "Exception during load of " << name
<< " key: " << e.what() << std::endl;
std::cout << "PEM for privkey was:\n" << priv_pem << std::endl;
++fails;
}
return fails;
}
byte nonzero_byte(RandomNumberGenerator& rng)
{
byte b = 0;
while(b == 0)
b = rng.next_byte();
return b;
}
}
#define PK_TEST(expr, msg) \
do { \
const bool test_result = expr; \
if(!test_result) \
{ \
std::cout << "Test " << #expr << " failed: " << msg << std::endl; \
++fails; \
} \
} while(0)
size_t validate_encryption(PK_Encryptor& e, PK_Decryptor& d,
const std::string& algo, const std::string& input,
const std::string& random, const std::string& exp)
{
std::vector<byte> message = hex_decode(input);
std::vector<byte> expected = hex_decode(exp);
Fixed_Output_RNG kat_rng(hex_decode(random));
size_t fails = 0;
const std::vector<byte> ctext = e.encrypt(message, kat_rng);
if(ctext != expected)
{
std::cout << "FAILED (encrypt): " << algo << std::endl;
dump_data(ctext, expected);
++fails;
}
std::vector<byte> decrypted = unlock(d.decrypt(ctext));
if(decrypted != message)
{
std::cout << "FAILED (decrypt): " << algo << std::endl;
dump_data(decrypted, message);
++fails;
}
if(algo.find("/Raw") == std::string::npos)
{
auto& rng = test_rng();
for(size_t i = 0; i != ctext.size(); ++i)
{
std::vector<byte> bad_ctext = ctext;
bad_ctext[i] ^= nonzero_byte(rng);
BOTAN_ASSERT(bad_ctext != ctext, "Made them different");
try
{
auto bad_ptext = unlock(d.decrypt(bad_ctext));
std::cout << algo << " failed - decrypted bad data" << std::endl;
std::cout << hex_encode(bad_ctext) << " -> " << hex_encode(bad_ptext) << std::endl;
std::cout << hex_encode(ctext) << " -> " << hex_encode(decrypted) << std::endl;
// Ignore PKCS #1 failures as they do occur occasionally (million message attack)
const bool is_pkcs1 = algo.find("/EME-PKCS1-v1_5") != std::string::npos;
if(is_pkcs1)
std::cout << "Ignoring PKCS #1 failure" << std::endl;
else
++fails;
}
catch(...) {}
}
}
return fails;
}
size_t validate_signature(PK_Verifier& v, PK_Signer& s, const std::string& algo,
const std::string& input,
RandomNumberGenerator& rng,
const std::string& exp)
{
return validate_signature(v, s, algo, input, rng, rng, exp);
}
size_t validate_signature(PK_Verifier& v, PK_Signer& s, const std::string& algo,
const std::string& input,
RandomNumberGenerator& signer_rng,
RandomNumberGenerator& test_rng,
const std::string& exp)
{
std::vector<byte> message = hex_decode(input);
std::vector<byte> expected = hex_decode(exp);
std::vector<byte> sig = s.sign_message(message, signer_rng);
size_t fails = 0;
if(sig != expected)
{
std::cout << "FAILED (sign): " << algo << std::endl;
dump_data(sig, expected);
++fails;
}
PK_TEST(v.verify_message(message, sig), "Correct signature is valid");
for(size_t i = 0; i != 5; ++i)
{
auto bad_sig = sig;
const size_t idx = (test_rng.next_byte() * 256 + test_rng.next_byte()) % sig.size();
bad_sig[idx] ^= nonzero_byte(test_rng);
PK_TEST(!v.verify_message(message, bad_sig), "Incorrect signature is invalid");
}
zero_mem(sig.data(), sig.size());
PK_TEST(!v.verify_message(message, sig), "All-zero signature is invalid");
return fails;
}
size_t validate_signature(PK_Verifier& v, PK_Signer& s, const std::string& algo,
const std::string& input,
RandomNumberGenerator& rng,
const std::string& random,
const std::string& exp)
{
Fixed_Output_RNG fixed_rng(hex_decode(random));
return validate_signature(v, s, algo, input, fixed_rng, rng, exp);
}
size_t validate_kas(PK_Key_Agreement& kas, const std::string& algo,
const std::vector<byte>& pubkey, const std::string& output,
size_t keylen)
{
std::vector<byte> expected = hex_decode(output);
std::vector<byte> got = unlock(kas.derive_key(keylen, pubkey).bits_of());
size_t fails = 0;
if(got != expected)
{
std::cout << "FAILED: " << algo << std::endl;
dump_data(got, expected);
++fails;
}
return fails;
}
size_t test_pk_keygen()
{
auto& rng = test_rng();
size_t tests = 0;
size_t fails = 0;
#define DL_KEY(TYPE, GROUP) \
{ \
TYPE key(rng, DL_Group(GROUP)); \
key.check_key(rng, true); \
++tests; \
fails += validate_save_and_load(&key, rng); \
}
#define EC_KEY(TYPE, GROUP) \
{ \
TYPE key(rng, EC_Group(OIDS::lookup(GROUP))); \
key.check_key(rng, true); \
++tests; \
fails += validate_save_and_load(&key, rng); \
}
#if defined(BOTAN_HAS_RSA)
{
RSA_PrivateKey rsa1024(rng, 1024);
rsa1024.check_key(rng, true);
++tests;
fails += validate_save_and_load(&rsa1024, rng);
RSA_PrivateKey rsa2048(rng, 2048);
rsa2048.check_key(rng, true);
++tests;
fails += validate_save_and_load(&rsa2048, rng);
}
#endif
#if defined(BOTAN_HAS_RW)
{
RW_PrivateKey rw1024(rng, 1024);
rw1024.check_key(rng, true);
++tests;
fails += validate_save_and_load(&rw1024, rng);
}
#endif
#if defined(BOTAN_HAS_DSA)
DL_KEY(DSA_PrivateKey, "dsa/jce/1024");
DL_KEY(DSA_PrivateKey, "dsa/botan/2048");
DL_KEY(DSA_PrivateKey, "dsa/botan/3072");
#endif
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
DL_KEY(DH_PrivateKey, "modp/ietf/1024");
DL_KEY(DH_PrivateKey, "modp/ietf/2048");
DL_KEY(DH_PrivateKey, "modp/ietf/4096");
DL_KEY(DH_PrivateKey, "dsa/jce/1024");
#endif
#if defined(BOTAN_HAS_NYBERG_RUEPPEL)
DL_KEY(NR_PrivateKey, "dsa/jce/1024");
DL_KEY(NR_PrivateKey, "dsa/botan/2048");
DL_KEY(NR_PrivateKey, "dsa/botan/3072");
#endif
#if defined(BOTAN_HAS_ELGAMAL)
DL_KEY(ElGamal_PrivateKey, "modp/ietf/1024");
DL_KEY(ElGamal_PrivateKey, "dsa/jce/1024");
DL_KEY(ElGamal_PrivateKey, "dsa/botan/2048");
DL_KEY(ElGamal_PrivateKey, "dsa/botan/3072");
#endif
#if defined(BOTAN_HAS_ECDSA)
EC_KEY(ECDSA_PrivateKey, "secp112r1");
EC_KEY(ECDSA_PrivateKey, "secp128r1");
EC_KEY(ECDSA_PrivateKey, "secp160r1");
EC_KEY(ECDSA_PrivateKey, "secp192r1");
EC_KEY(ECDSA_PrivateKey, "secp224r1");
EC_KEY(ECDSA_PrivateKey, "secp256r1");
EC_KEY(ECDSA_PrivateKey, "secp384r1");
EC_KEY(ECDSA_PrivateKey, "secp521r1");
#endif
#if defined(BOTAN_HAS_GOST_34_10_2001)
EC_KEY(GOST_3410_PrivateKey, "gost_256A");
EC_KEY(GOST_3410_PrivateKey, "secp112r1");
EC_KEY(GOST_3410_PrivateKey, "secp128r1");
EC_KEY(GOST_3410_PrivateKey, "secp160r1");
EC_KEY(GOST_3410_PrivateKey, "secp192r1");
EC_KEY(GOST_3410_PrivateKey, "secp224r1");
EC_KEY(GOST_3410_PrivateKey, "secp256r1");
EC_KEY(GOST_3410_PrivateKey, "secp384r1");
EC_KEY(GOST_3410_PrivateKey, "secp521r1");
#endif
test_report("PK keygen", tests, fails);
return fails;
}
#else
SKIP_TEST(pk_keygen);
#endif // BOTAN_HAS_PUBLIC_KEY_CRYPTO
|