summaryrefslogtreecommitdiff
path: root/validat7.cpp
blob: 62f5b1d0803694880d8a657b7903098724b27b24 (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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// validat7.cpp - originally written and placed in the public domain by Wei Dai
//                CryptoPP::Test namespace added by JW in February 2017.
//                Source files split in July 2018 to expedite compiles.

#include "pch.h"

#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1

#include "cryptlib.h"
#include "cpu.h"
#include "validate.h"

#include "asn.h"
#include "oids.h"

#include "sha.h"
#include "sha3.h"

#include "dh.h"
#include "luc.h"
#include "mqv.h"
#include "xtr.h"
#include "hmqv.h"
#include "pubkey.h"
#include "xtrcrypt.h"
#include "eccrypto.h"

// Curve25519
#include "xed25519.h"
#include "donna.h"
#include "naclite.h"

#include <iostream>
#include <iomanip>
#include <sstream>

// Aggressive stack checking with VS2005 SP1 and above.
#if (_MSC_FULL_VER >= 140050727)
# pragma strict_gs_check (on)
#endif

#if CRYPTOPP_MSC_VERSION
# pragma warning(disable: 4505 4355)
#endif

NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)

ANONYMOUS_NAMESPACE_BEGIN

inline bool operator==(const x25519& lhs, const x25519& rhs)
{
	// This is a hack because the KeyAgreement classes do not make it easy to access the PrivateKey
	ByteQueue q1, q2;
	lhs.DEREncodePrivateKey(q1);
	rhs.DEREncodePrivateKey(q2);

	return q1 == q2;
}

inline bool operator!=(const x25519& lhs, const x25519& rhs)
{
	return !operator==(lhs, rhs);
}

ANONYMOUS_NAMESPACE_END

bool ValidateDH()
{
	std::cout << "\nDH validation suite running...\n\n";

	FileSource f(DataDir("TestData/dh1024.dat").c_str(), true, new HexDecoder);
	DH dh(f);
	return SimpleKeyAgreementValidate(dh);
}

bool ValidateX25519()
{
	std::cout << "\nx25519 validation suite running...\n\n";

	FileSource f(DataDir("TestData/x25519.dat").c_str(), true, new HexDecoder);
	x25519 dh(f);
	return SimpleKeyAgreementValidate(dh);
}

bool ValidateMQV()
{
	std::cout << "\nMQV validation suite running...\n\n";

	FileSource f(DataDir("TestData/mqv1024.dat").c_str(), true, new HexDecoder);
	MQV mqv(f);
	return AuthenticatedKeyAgreementValidate(mqv);
}

bool ValidateHMQV()
{
	std::cout << "\nHMQV validation suite running...\n\n";
	bool success = true, fail;

	FileSource f256(DataDir("TestData/hmqv256.dat").c_str(), true, new HexDecoder);
	FileSource f384(DataDir("TestData/hmqv384.dat").c_str(), true, new HexDecoder);
	FileSource f512(DataDir("TestData/hmqv512.dat").c_str(), true, new HexDecoder);

	/////////////////////////

	std::cout << "HMQV with NIST P-256 and SHA-256:" << std::endl;

	ECHMQV256 hmqvB256(false);
	hmqvB256.AccessGroupParameters().BERDecode(f256);
	const OID oid = ASN1::secp256r1();
	ECHMQV< ECP >::Domain hmqvA256(oid, true /*client*/);

	fail = !AuthenticatedKeyAgreementWithRolesValidate(hmqvA256, hmqvB256);
	success = !fail && success;
	if (fail == false)
		std::cout << "passed    authenticated key agreement" << std::endl;
	else
		std::cout << "FAILED    authenticated key agreement" << std::endl;

	/////////////////////////

	std::cout << "HMQV with NIST P-384 and SHA-384:" << std::endl;

	ECHMQV384 hmqvB384(false);
	hmqvB384.AccessGroupParameters().BERDecode(f384);
	const OID oid384 = ASN1::secp384r1();
	ECHMQV384 hmqvA384(oid384, true /*client*/);

	fail = !AuthenticatedKeyAgreementWithRolesValidate(hmqvA384, hmqvB384);
	success = !fail && success;
	if (fail == false)
		std::cout << "passed    authenticated key agreement" << std::endl;
	else
		std::cout << "FAILED    authenticated key agreement" << std::endl;

	/////////////////////////

	std::cout << "HMQV with NIST P-521 and SHA-512:" << std::endl;

	ECHMQV512 hmqvB521(false);
	hmqvB521.AccessGroupParameters().BERDecode(f512);
	const OID oid521 = ASN1::secp521r1();
	ECHMQV512 hmqvA521(oid521, true /*client*/);

	fail = !AuthenticatedKeyAgreementWithRolesValidate(hmqvA521, hmqvB521);
	success = !fail && success;
	if (fail == false)
		std::cout << "passed    authenticated key agreement" << std::endl;
	else
		std::cout << "FAILED    authenticated key agreement" << std::endl;

	return success;
}

bool ValidateFHMQV()
{
	std::cout << "\nFHMQV validation suite running...\n\n";
	bool success = true, fail;

	FileSource f256(DataDir("TestData/fhmqv256.dat").c_str(), true, new HexDecoder);
	FileSource f384(DataDir("TestData/fhmqv384.dat").c_str(), true, new HexDecoder);
	FileSource f512(DataDir("TestData/fhmqv512.dat").c_str(), true, new HexDecoder);

	/////////////////////////

	std::cout << "FHMQV with NIST P-256 and SHA-256:" << std::endl;

	ECFHMQV256 fhmqvB256(false);
	fhmqvB256.AccessGroupParameters().BERDecode(f256);
	const OID oid = ASN1::secp256r1();
	ECFHMQV< ECP >::Domain fhmqvA256(oid, true /*client*/);

	fail = !AuthenticatedKeyAgreementWithRolesValidate(fhmqvA256, fhmqvB256);
	success = !fail && success;
	if (fail == false)
		std::cout << "passed    authenticated key agreement" << std::endl;
	else
		std::cout << "FAILED    authenticated key agreement" << std::endl;

	/////////////////////////

	std::cout << "FHMQV with NIST P-384 and SHA-384:" << std::endl;

	ECHMQV384 fhmqvB384(false);
	fhmqvB384.AccessGroupParameters().BERDecode(f384);
	const OID oid384 = ASN1::secp384r1();
	ECHMQV384 fhmqvA384(oid384, true /*client*/);

	fail = !AuthenticatedKeyAgreementWithRolesValidate(fhmqvA384, fhmqvB384);
	success = !fail && success;
	if (fail == false)
		std::cout << "passed    authenticated key agreement" << std::endl;
	else
		std::cout << "FAILED    authenticated key agreement" << std::endl;

	/////////////////////////

	std::cout << "FHMQV with NIST P-521 and SHA-512:" << std::endl;

	ECHMQV512 fhmqvB521(false);
	fhmqvB521.AccessGroupParameters().BERDecode(f512);
	const OID oid521 = ASN1::secp521r1();
	ECHMQV512 fhmqvA521(oid521, true /*client*/);

	fail = !AuthenticatedKeyAgreementWithRolesValidate(fhmqvA521, fhmqvB521);
	success = !fail && success;
	if (fail == false)
		std::cout << "passed    authenticated key agreement" << std::endl;
	else
		std::cout << "FAILED    authenticated key agreement" << std::endl;

	return success;
}

bool ValidateLUC_DH()
{
	std::cout << "\nLUC-DH validation suite running...\n\n";

	FileSource f(DataDir("TestData/lucd512.dat").c_str(), true, new HexDecoder);
	LUC_DH dh(f);
	return SimpleKeyAgreementValidate(dh);
}

bool ValidateXTR_DH()
{
	std::cout << "\nXTR-DH validation suite running...\n\n";

	FileSource f(DataDir("TestData/xtrdh171.dat").c_str(), true, new HexDecoder);
	XTR_DH dh(f);
	return SimpleKeyAgreementValidate(dh);
}

bool ValidateECP_Agreement()
{
	ECDH<ECP>::Domain ecdhc(ASN1::secp192r1());
	ECMQV<ECP>::Domain ecmqvc(ASN1::secp192r1());
	bool pass = SimpleKeyAgreementValidate(ecdhc);
	pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;

	std::cout << "Turning on point compression..." << std::endl;
	ecdhc.AccessGroupParameters().SetPointCompression(true);
	ecmqvc.AccessGroupParameters().SetPointCompression(true);
	pass = SimpleKeyAgreementValidate(ecdhc) && pass;
	pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;

	return pass;
}

bool ValidateEC2N_Agreement()
{
	ECDH<EC2N>::Domain ecdhc(ASN1::sect193r1());
	ECMQV<EC2N>::Domain ecmqvc(ASN1::sect193r1());
	bool pass = SimpleKeyAgreementValidate(ecdhc);
	pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;

	std::cout << "Turning on point compression..." << std::endl;
	ecdhc.AccessGroupParameters().SetPointCompression(true);
	ecmqvc.AccessGroupParameters().SetPointCompression(true);
	pass = SimpleKeyAgreementValidate(ecdhc) && pass;
	pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;

	return pass;
}

// TestX25519 is slightly more comprehensive than ValidateX25519
// because it cross-validates against Bernstein's NaCL library.
// TestX25519 called in Debug builds.
bool TestX25519()
{
	std::cout << "\nTesting curve25519 Key Agreements...\n\n";
	const unsigned int AGREE_COUNT = 64;
	bool pass = true, fail;

	size_t i = 0;
	unsigned int failed = 0;

	SecByteBlock priv1(32), priv2(32), pub1(32), pub2(32), share1(32), share2(32);
	for (i=0, failed=0; i<AGREE_COUNT; ++i)
	{
		GlobalRNG().GenerateBlock(priv1, priv1.size());
		GlobalRNG().GenerateBlock(priv2, priv2.size());

		priv1[0] &= 248; priv1[31] &= 127; priv1[31] |= 64;
		priv2[0] &= 248; priv2[31] &= 127; priv2[31] |= 64;

		// Andrew Moon's curve25519-donna
		Donna::curve25519_mult(pub1, priv1);
		Donna::curve25519_mult(pub2, priv2);

		int ret1 = Donna::curve25519_mult(share1, priv1, pub2);
		int ret2 = Donna::curve25519_mult(share2, priv2, pub1);
		int ret3 = std::memcmp(share1, share2, 32);

#if defined(CRYPTOPP_DISABLE_NACL)
		int ret4=0, ret5=0, ret6=0;
#else
		// Bernstein's NaCl requires DefaultAutoSeededRNG.
		NaCl::crypto_box_keypair(pub2, priv2);

		int ret4 = Donna::curve25519_mult(share1, priv1, pub2);
		int ret5 = NaCl::crypto_scalarmult(share2, priv2, pub1);
		int ret6 = std::memcmp(share1, share2, 32);
#endif

		fail = ret1 != 0 || ret2 != 0 || ret3 != 0 || ret4 != 0 || ret5 != 0 || ret6 != 0;
		if (fail) failed++;
	}

	failed ? fail = true : fail = false;
	pass = pass && !fail;

	std::cout << (fail ? "FAILED" : "passed") << "  ";
	std::cout << AGREE_COUNT << " key agreements" << std::endl;

	try {

		FileSource f1(DataDir("TestData/x25519.dat").c_str(), true, new HexDecoder);
		FileSource f2(DataDir("TestData/x25519v0.dat").c_str(), true, new HexDecoder);
		FileSource f3(DataDir("TestData/x25519v1.dat").c_str(), true, new HexDecoder);

		x25519 x1(f1);
		x25519 x2(f2);
		x25519 x3(f3);

		FileSource f4(DataDir("TestData/x25519.dat").c_str(), true, new HexDecoder);
		FileSource f5(DataDir("TestData/x25519v0.dat").c_str(), true, new HexDecoder);
		FileSource f6(DataDir("TestData/x25519v1.dat").c_str(), true, new HexDecoder);

		x1.Load(f4);
		x2.Load(f5);
		x3.Load(f6);

		// No throw is success
		fail = false;
	}
	catch (const BERDecodeErr&) {
		fail = true;
	}

	pass = pass && !fail;

	std::cout << (fail ? "FAILED" : "passed") << "  ";
	std::cout << "x25519 load and save\n";

#if defined(CRYPTOPP_EXTENDED_VALIDATION)
	{
		x25519 x1(GlobalRNG()), x2;

		ByteQueue q;
		x1.DEREncode(q);
		x2.BERDecode(q);

		fail = (x1 != x2);
		pass = pass && !fail;

		std::cout << (fail ? "FAILED" : "passed") << "  ";
		std::cout << "x25519 encoding and decoding\n";
	}
	{
		SecByteBlock b1(32);
		GlobalRNG().GenerateBlock(b1, b1.size());
		b1[0] &= 248; b1[31] &= 127; b1[31] |= 64;

		try {
			fail = false;
			x25519 x1(b1);
			if (!x1.Validate(GlobalRNG(), 3))
				throw Exception(Exception::OTHER_ERROR, "x25519::Validate");
		} catch(const Exception&) {
			fail = true;
		}

		pass = pass && !fail;

		std::cout << (fail ? "FAILED" : "passed") << "  ";
		std::cout << "x25519 private key byte buffer construction\n";

		std::reverse(b1.begin(), b1.begin()+b1.size());
		Integer x(b1.begin(), b1.size());

		try {
			fail = false;
			x25519 x1(x);
			if (!x1.Validate(GlobalRNG(), 3))
				throw Exception(Exception::OTHER_ERROR, "x25519::Validate");
		} catch(const Exception&) {
			fail = true;
		}

		pass = pass && !fail;

		std::cout << (fail ? "FAILED" : "passed") << "  ";
		std::cout << "x25519 private key Integer construction\n";
	}

	{
		SecByteBlock b1(32), b2(32);
		GlobalRNG().GenerateBlock(b1, b1.size());
		b1[0] &= 248; b1[31] &= 127; b1[31] |= 64;
		Donna::curve25519_mult(b2, b1);

		try {
			fail = false;
			x25519 x1(b2, b1);
			if (!x1.Validate(GlobalRNG(), 3))
				throw Exception(Exception::OTHER_ERROR, "x25519::Validate");
		} catch(const Exception&) {
			fail = true;
		}

		pass = pass && !fail;

		std::cout << (fail ? "FAILED" : "passed") << "  ";
		std::cout << "x25519 public key byte buffer construction\n";

		SecByteBlock b3(b1), b4(b2);
		std::reverse(b3.begin(), b3+b3.size());
		std::reverse(b4.begin(), b4+b4.size());

		Integer x(b3, b3.size());
		Integer y(b4, b4.size());

		try {
			fail = false;
			x25519 x1(y, x);
			if (!x1.Validate(GlobalRNG(), 3))
				throw Exception(Exception::OTHER_ERROR, "x25519::Validate");
		} catch(const Exception&) {
			fail = true;
		}

		pass = pass && !fail;

		std::cout << (fail ? "FAILED" : "passed");
		std::cout << "  " << "x25519 public key Integer construction\n";

		try {
			fail = false;
			x25519 x1(b2, b1);
			x25519 x2(y, x);
			if (x1 != x2)
				throw Exception(Exception::OTHER_ERROR, "x25519::Validate");
		} catch(const Exception&) {
			fail = true;
		}

		pass = pass && !fail;

		std::cout << (fail ? "FAILED" : "passed") << "  ";
		std::cout << "x25519 byte buffer and Integer consistency\n";
	}
#endif

	return pass;
}

// TestEd25519 is slightly more comprehensive than ValidateEd25519
// because it cross-validates against Bernstein's NaCL library.
// TestEd25519 called in Debug builds.
bool TestEd25519()
{
	std::cout << "\nTesting ed25519 Signatures...\n\n";
	bool pass = true, fail;

	size_t i = 0;
	unsigned int failed = 0;

#ifndef CRYPTOPP_DISABLE_NACL
	const unsigned int SIGN_COUNT = 64, MSG_SIZE=128;
	const unsigned int NACL_EXTRA=NaCl::crypto_sign_BYTES;

	// Test key conversion
	byte seed[32], sk1[64], sk2[64], pk1[32], pk2[32];
	for (i = 0, failed = 0; i<SIGN_COUNT; ++i)
	{
		GlobalRNG().GenerateBlock(seed, 32);
		std::memcpy(sk1, seed, 32);
		std::memcpy(sk2, seed, 32);

		int ret1 = NaCl::crypto_sign_sk2pk(pk1, sk1);
		int ret2 = Donna::ed25519_publickey(pk2, sk2);
		int ret3 = std::memcmp(pk1, pk2, 32);

		fail = ret1 != 0 || ret2 != 0 || ret3 != 0;
		if (fail) failed++;
	}

	failed ? fail = true : fail = false;
	pass = pass && !fail;

	std::cout << (fail ? "FAILED" : "passed") << "  ";
	std::cout << SIGN_COUNT << " public keys" << std::endl;

	// Test signature generation
	for (i = 0, failed = 0; i<SIGN_COUNT; ++i)
	{
		// Fresh keypair
		(void)NaCl::crypto_sign_keypair(pk1, sk1);
		std::memcpy(sk2, sk1, 32);
		std::memcpy(pk2, pk1, 32);

		// Message and signatures
		byte msg[MSG_SIZE], sig1[MSG_SIZE+NACL_EXTRA], sig2[64];
		GlobalRNG().GenerateBlock(msg, MSG_SIZE);
		size_t len = GlobalRNG().GenerateWord32(0, MSG_SIZE);

		// Spike the signatures
		sig1[1] = 1; sig2[2] = 2;
		word64 smlen = sizeof(sig1);

		int ret1 = NaCl::crypto_sign(sig1, &smlen, msg, len, sk1);
		int ret2 = Donna::ed25519_sign(msg, len, sk2, pk2, sig2);
		int ret3 = std::memcmp(sig1, sig2, 64);

		fail = ret1 != 0 || ret2 != 0 || ret3 != 0;
		if (fail) failed++;
	}

	failed ? fail = true : fail = false;
	pass = pass && !fail;

	std::cout << (fail ? "FAILED" : "passed") << "  ";
	std::cout << SIGN_COUNT << " signatures" << std::endl;

	// Test signature verification
	for (i = 0, failed = 0; i<SIGN_COUNT; ++i)
	{
		// Fresh keypair
		(void)NaCl::crypto_sign_keypair(pk1, sk1);
		std::memcpy(sk2, sk1, 32);
		std::memcpy(pk2, pk1, 32);

		// Message and signatures
		byte msg1[MSG_SIZE+NACL_EXTRA], msg2[MSG_SIZE];
		byte sig1[MSG_SIZE+NACL_EXTRA], sig2[64];
		GlobalRNG().GenerateBlock(msg1, MSG_SIZE);
		size_t len = GlobalRNG().GenerateWord32(0, MSG_SIZE);
		std::memcpy(msg2, msg1, len);

		// Spike the signatures
		sig1[1] = 1; sig2[2] = 2;

		word64 smlen = sizeof(sig1);
		int ret1 = NaCl::crypto_sign(sig1, &smlen, msg1, len, sk1);
		int ret2 = Donna::ed25519_sign(msg2, len, sk2, pk2, sig2);
		int ret3 = std::memcmp(sig1, sig2, 64);

		bool tamper = !!GlobalRNG().GenerateBit();
		if (tamper)
		{
			sig1[1] ^= 1;
			sig2[1] ^= 1;
		}

		// Verify the other's signature using the other's key
		word64 mlen = len+NACL_EXTRA;
		int ret4 = NaCl::crypto_sign_open(msg1, &mlen, sig1, smlen, pk2);
		int ret5 = Donna::ed25519_sign_open(msg2, len, pk1, sig2);

		fail = ret1 != 0 || ret2 != 0 || ret3 != 0 || ((ret4 != 0) ^ tamper) || ((ret5 != 0) ^ tamper);
		if (fail) failed++;
	}

	failed ? fail = true : fail = false;
	pass = pass && !fail;

	std::cout << (fail ? "FAILED" : "passed") << "  ";
	std::cout << SIGN_COUNT << " verifications" << std::endl;

	// Test signature verification using streams
	for (i = 0, failed = 0; i<SIGN_COUNT; ++i)
	{
		// Fresh keypair
		(void)NaCl::crypto_sign_keypair(pk1, sk1);
		std::memcpy(sk2, sk1, 32);
		std::memcpy(pk2, pk1, 32);

		// Message and signatures
		byte msg1[MSG_SIZE+NACL_EXTRA], msg2[MSG_SIZE];
		byte sig1[MSG_SIZE+NACL_EXTRA], sig2[64];
		GlobalRNG().GenerateBlock(msg1, MSG_SIZE);
		size_t len = GlobalRNG().GenerateWord32(0, MSG_SIZE);
		std::memcpy(msg2, msg1, len);

		// Spike the signatures
		sig1[1] = 1; sig2[2] = 2;

		// Create a stream
		std::string str2((const char*)msg2, len);
		std::istringstream iss(str2);

		word64 smlen = sizeof(sig1);
		int ret1 = NaCl::crypto_sign(sig1, &smlen, msg1, len, sk1);
		int ret2 = Donna::ed25519_sign(iss, sk2, pk2, sig2);
		int ret3 = std::memcmp(sig1, sig2, 64);

		bool tamper = !!GlobalRNG().GenerateBit();
		if (tamper)
		{
			sig1[1] ^= 1;
			sig2[1] ^= 1;
		}

		// Reset stream
		iss.clear();
		iss.seekg(0);

		// Verify the other's signature using the other's key
		word64 mlen = len+NACL_EXTRA;
		int ret4 = NaCl::crypto_sign_open(msg1, &mlen, sig1, smlen, pk2);
		int ret5 = Donna::ed25519_sign_open(iss, pk1, sig2);

		fail = ret1 != 0 || ret2 != 0 || ret3 != 0 || ((ret4 != 0) ^ tamper) || ((ret5 != 0) ^ tamper);
		if (fail) failed++;
	}

	failed ? fail = true : fail = false;
	pass = pass && !fail;

	std::cout << (fail ? "FAILED" : "passed") << "  ";
	std::cout << SIGN_COUNT << " streams" << std::endl;
#endif

	// RFC 8032 test vector
	try
	{
		// RFC 8032 Ed25519 test vector 3, p. 23
		byte sk[] = {
			0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
			0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
		};
		byte pk[] = {
			0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
			0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
		};

		const byte exp[] = {
			0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
			0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
			0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
			0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
		};

		const byte msg[2] = {0xaf, 0x82}; byte sig[64];

		// Test the filter framework
		ed25519Signer signer(pk, sk);
		StringSource(msg, sizeof(msg), true, new SignerFilter(NullRNG(), signer, new ArraySink(sig, sizeof(sig))));

		if (std::memcmp(exp, sig, 64) != 0)
			throw Exception(Exception::OTHER_ERROR, "TestEd25519: SignerFilter");

		ed25519Verifier verifier(pk);
		int flags = SignatureVerificationFilter::THROW_EXCEPTION | SignatureVerificationFilter::SIGNATURE_AT_END;
		std::string msg_sig = std::string((char*)msg, sizeof(msg)) + std::string((char*)sig, sizeof(sig));
		StringSource(msg_sig, true, new SignatureVerificationFilter(verifier, NULLPTR, flags));

		// No throw is success
		fail = false;
	}
	catch(const Exception&)
	{
		fail = true;
	}

	pass = pass && !fail;

	std::cout << (fail ? "FAILED" : "passed") << "  ";
	std::cout << "RFC 8032 test vectors" << std::endl;

	// Test key loads
	try {
		FileSource f1(DataDir("TestData/ed25519.dat").c_str(), true, new HexDecoder);
		FileSource f2(DataDir("TestData/ed25519v0.dat").c_str(), true, new HexDecoder);
		FileSource f3(DataDir("TestData/ed25519v1.dat").c_str(), true, new HexDecoder);

		ed25519::Signer s1(f1);
		ed25519::Signer s2(f2);
		ed25519::Signer s3(f3);

		FileSource f4(DataDir("TestData/ed25519.dat").c_str(), true, new HexDecoder);
		FileSource f5(DataDir("TestData/ed25519v0.dat").c_str(), true, new HexDecoder);
		FileSource f6(DataDir("TestData/ed25519v1.dat").c_str(), true, new HexDecoder);

		s1.AccessKey().Load(f4);
		s2.AccessKey().Load(f5);
		s3.AccessKey().Load(f6);

		// No throw is success
		fail = false;
	}
	catch (const BERDecodeErr&) {
		fail = true;
	}

	pass = pass && !fail;

	std::cout << (fail ? "FAILED" : "passed") << "  ";
	std::cout << "RFC 5208 and 5958 key loads" << std::endl;

	return pass;
}

NAMESPACE_END  // Test
NAMESPACE_END  // CryptoPP