Make TestCurve25519 available in Release builds

This commit is contained in:
Jeffrey Walton 2018-12-14 12:15:58 -05:00
parent 00f777661d
commit 235c615a10
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
3 changed files with 56 additions and 54 deletions

View File

@ -21,11 +21,6 @@
#include "gzip.h"
#include "zlib.h"
//curve25519
#include "xed25519.h"
#include "donna.h"
#include "naclite.h"
#include <iostream>
#include <iomanip>
#include <sstream>
@ -428,53 +423,6 @@ bool TestCompressors()
return !fail1 && !fail2 && !fail3;
}
bool TestCurve25519()
{
std::cout << "\nTesting curve25519 Key Agreements...\n\n";
const unsigned int AGREE_COUNT = 64;
bool pass = true;
SecByteBlock priv1(32), priv2(32), pub1(32), pub2(32), share1(32), share2(32);
for (unsigned int i=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(pub1, priv1);
Donna::curve25519(pub2, priv2);
int ret1 = Donna::curve25519(share1, priv1, pub2);
int ret2 = Donna::curve25519(share2, priv2, pub1);
int ret3 = std::memcmp(share1, share2, 32);
#if defined(NO_OS_DEPENDENCE)
int ret4=0, ret5=0, ret6=0;
#else
// Bernstein's NaCl requires DefaultAutoSeededRNG.
NaCl::crypto_box_keypair(pub2, priv2);
int ret4 = Donna::curve25519(share1, priv1, pub2);
int ret5 = NaCl::crypto_scalarmult(share2, priv2, pub1);
int ret6 = std::memcmp(share1, share2, 32);
#endif
bool fail = ret1 != 0 || ret2 != 0 || ret3 != 0 || ret4 != 0 || ret5 != 0 || ret6 != 0;
pass = pass && !fail;
}
if (pass)
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " " << AGREE_COUNT << " key agreements" << std::endl;
return pass;
}
bool TestEncryptors()
{
std::cout << "\nTesting Default Encryptors and Decryptors...\n\n";

View File

@ -22,10 +22,14 @@
#include "xtr.h"
#include "hmqv.h"
#include "pubkey.h"
#include "xed25519.h"
#include "xtrcrypt.h"
#include "eccrypto.h"
// Curve25519
#include "xed25519.h"
#include "donna.h"
#include "naclite.h"
#include <iostream>
#include <iomanip>
#include <sstream>
@ -352,5 +356,55 @@ bool ValidateEC2N_Agreement()
return pass;
}
// TestCurve25519 is slighty more comprehensive than ValidateX25519
// because it cross-validates against Bernstein's NaCL library.
// TestCurve25519 called in Debug builds.
bool TestCurve25519()
{
std::cout << "\nTesting curve25519 Key Agreements...\n\n";
const unsigned int AGREE_COUNT = 64;
bool pass = true;
SecByteBlock priv1(32), priv2(32), pub1(32), pub2(32), share1(32), share2(32);
for (unsigned int i=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(pub1, priv1);
Donna::curve25519(pub2, priv2);
int ret1 = Donna::curve25519(share1, priv1, pub2);
int ret2 = Donna::curve25519(share2, priv2, pub1);
int ret3 = std::memcmp(share1, share2, 32);
#if defined(NO_OS_DEPENDENCE)
int ret4=0, ret5=0, ret6=0;
#else
// Bernstein's NaCl requires DefaultAutoSeededRNG.
NaCl::crypto_box_keypair(pub2, priv2);
int ret4 = Donna::curve25519(share1, priv1, pub2);
int ret5 = NaCl::crypto_scalarmult(share2, priv2, pub1);
int ret6 = std::memcmp(share1, share2, 32);
#endif
bool fail = ret1 != 0 || ret2 != 0 || ret3 != 0 || ret4 != 0 || ret5 != 0 || ret6 != 0;
pass = pass && !fail;
}
if (pass)
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " " << AGREE_COUNT << " key agreements" << std::endl;
return pass;
}
NAMESPACE_END // Test
NAMESPACE_END // CryptoPP

View File

@ -134,6 +134,7 @@ bool ValidateESIGN();
bool ValidateHashDRBG();
bool ValidateHmacDRBG();
bool TestCurve25519();
bool ValidateNaCl();
// If CRYPTOPP_DEBUG or CRYPTOPP_COVERAGE is in effect, then perform additional tests
@ -161,7 +162,6 @@ bool TestStringSink();
// Additional tests due to no coverage
bool TestCompressors();
bool TestEncryptors();
bool TestCurve25519();
bool TestMersenne();
bool TestSharing();
# if defined(CRYPTOPP_ALTIVEC_AVAILABLE)