2018-07-30 00:34:29 +00:00
|
|
|
// validat6.cpp - originally written and placed in the public domain by Wei Dai
|
2018-07-28 18:57:12 +00:00
|
|
|
// 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"
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
#include "asn.h"
|
|
|
|
#include "oids.h"
|
|
|
|
#include "blumshub.h"
|
|
|
|
#include "eccrypto.h"
|
2018-07-28 18:57:12 +00:00
|
|
|
|
|
|
|
#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)
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool CryptoSystemValidate(PK_Decryptor &priv, PK_Encryptor &pub, bool thorough)
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
bool pass = true, fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
fail = !pub.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2) || !priv.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2);
|
|
|
|
pass = pass && !fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << (fail ? "FAILED " : "passed ");
|
|
|
|
std::cout << "cryptosystem key validation\n";
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2019-07-06 19:57:08 +00:00
|
|
|
const byte message[] = "test message";
|
2018-07-30 00:30:19 +00:00
|
|
|
const int messageLen = 12;
|
|
|
|
SecByteBlock ciphertext(priv.CiphertextLength(messageLen));
|
|
|
|
SecByteBlock plaintext(priv.MaxPlaintextLength(ciphertext.size()));
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
pub.Encrypt(GlobalRNG(), message, messageLen, ciphertext);
|
|
|
|
fail = priv.Decrypt(GlobalRNG(), ciphertext, priv.CiphertextLength(messageLen), plaintext) != DecodingResult(messageLen);
|
2023-04-15 20:45:02 +00:00
|
|
|
fail = fail || std::memcmp(message, plaintext, messageLen);
|
2018-07-30 00:30:19 +00:00
|
|
|
pass = pass && !fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << (fail ? "FAILED " : "passed ");
|
|
|
|
std::cout << "encryption and decryption\n";
|
2018-07-28 18:57:12 +00:00
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool SimpleKeyAgreementValidate(SimpleKeyAgreementDomain &d)
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
if (d.GetCryptoParameters().Validate(GlobalRNG(), 3))
|
|
|
|
std::cout << "passed simple key agreement domain parameters validation" << std::endl;
|
|
|
|
else
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "FAILED simple key agreement domain parameters invalid" << std::endl;
|
|
|
|
return false;
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
SecByteBlock priv1(d.PrivateKeyLength()), priv2(d.PrivateKeyLength());
|
|
|
|
SecByteBlock pub1(d.PublicKeyLength()), pub2(d.PublicKeyLength());
|
|
|
|
SecByteBlock val1(d.AgreedValueLength()), val2(d.AgreedValueLength());
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
d.GenerateKeyPair(GlobalRNG(), priv1, pub1);
|
|
|
|
d.GenerateKeyPair(GlobalRNG(), priv2, pub2);
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2023-04-15 20:45:02 +00:00
|
|
|
std::memset(val1.begin(), 0x10, val1.size());
|
|
|
|
std::memset(val2.begin(), 0x11, val2.size());
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
if (!(d.Agree(val1, priv1, pub2) && d.Agree(val2, priv2, pub1)))
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "FAILED simple key agreement failed" << std::endl;
|
|
|
|
return false;
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 20:45:02 +00:00
|
|
|
if (std::memcmp(val1.begin(), val2.begin(), d.AgreedValueLength()))
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "FAILED simple agreed values not equal" << std::endl;
|
|
|
|
return false;
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "passed simple key agreement" << std::endl;
|
2018-07-28 18:57:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool AuthenticatedKeyAgreementValidate(AuthenticatedKeyAgreementDomain &d)
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
if (d.GetCryptoParameters().Validate(GlobalRNG(), 3))
|
|
|
|
std::cout << "passed authenticated key agreement domain parameters validation" << std::endl;
|
|
|
|
else
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "FAILED authenticated key agreement domain parameters invalid" << std::endl;
|
|
|
|
return false;
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
SecByteBlock spriv1(d.StaticPrivateKeyLength()), spriv2(d.StaticPrivateKeyLength());
|
|
|
|
SecByteBlock epriv1(d.EphemeralPrivateKeyLength()), epriv2(d.EphemeralPrivateKeyLength());
|
|
|
|
SecByteBlock spub1(d.StaticPublicKeyLength()), spub2(d.StaticPublicKeyLength());
|
|
|
|
SecByteBlock epub1(d.EphemeralPublicKeyLength()), epub2(d.EphemeralPublicKeyLength());
|
|
|
|
SecByteBlock val1(d.AgreedValueLength()), val2(d.AgreedValueLength());
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
d.GenerateStaticKeyPair(GlobalRNG(), spriv1, spub1);
|
|
|
|
d.GenerateStaticKeyPair(GlobalRNG(), spriv2, spub2);
|
|
|
|
d.GenerateEphemeralKeyPair(GlobalRNG(), epriv1, epub1);
|
|
|
|
d.GenerateEphemeralKeyPair(GlobalRNG(), epriv2, epub2);
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2023-04-15 20:45:02 +00:00
|
|
|
std::memset(val1.begin(), 0x10, val1.size());
|
|
|
|
std::memset(val2.begin(), 0x11, val2.size());
|
2018-07-30 00:30:19 +00:00
|
|
|
|
2019-08-04 03:52:50 +00:00
|
|
|
if (d.Agree(val1, spriv1, epriv1, spub2, epub2) && d.Agree(val2, spriv2, epriv2, spub1, epub1))
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2019-08-04 03:52:50 +00:00
|
|
|
std::cout << "passed authenticated key agreement protocol execution" << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << "FAILED authenticated key agreement protocol execution" << std::endl;
|
2018-07-30 00:30:19 +00:00
|
|
|
return false;
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 20:45:02 +00:00
|
|
|
if (std::memcmp(val1.begin(), val2.begin(), d.AgreedValueLength()))
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "FAILED authenticated agreed values not equal" << std::endl;
|
|
|
|
return false;
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "passed authenticated key agreement" << std::endl;
|
|
|
|
return true;
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 03:52:50 +00:00
|
|
|
bool AuthenticatedKeyAgreementWithRolesValidate(AuthenticatedKeyAgreementDomain &initiator, AuthenticatedKeyAgreementDomain &recipient)
|
2019-08-03 20:32:36 +00:00
|
|
|
{
|
|
|
|
if (initiator.GetCryptoParameters().Validate(GlobalRNG(), 3))
|
|
|
|
std::cout << "passed authenticated key agreement domain parameters validation (initiator)" << std::endl;
|
|
|
|
else
|
|
|
|
{
|
2019-08-04 03:59:25 +00:00
|
|
|
std::cout << "FAILED authenticated key agreement domain parameters invalid (initiator)" << std::endl;
|
2019-08-03 20:32:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-04 03:52:50 +00:00
|
|
|
if (recipient.GetCryptoParameters().Validate(GlobalRNG(), 3))
|
2019-08-04 03:59:25 +00:00
|
|
|
std::cout << "passed authenticated key agreement domain parameters validation (recipient)" << std::endl;
|
2019-08-03 20:32:36 +00:00
|
|
|
else
|
|
|
|
{
|
2019-08-04 03:52:50 +00:00
|
|
|
std::cout << "FAILED authenticated key agreement domain parameters invalid (recipient)" << std::endl;
|
2019-08-03 20:32:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-04 03:52:50 +00:00
|
|
|
if (initiator.StaticPrivateKeyLength() != recipient.StaticPrivateKeyLength() ||
|
|
|
|
initiator.EphemeralPrivateKeyLength() != recipient.EphemeralPrivateKeyLength() ||
|
|
|
|
initiator.StaticPublicKeyLength() != recipient.StaticPublicKeyLength() ||
|
|
|
|
initiator.EphemeralPublicKeyLength() != recipient.EphemeralPublicKeyLength() ||
|
|
|
|
initiator.AgreedValueLength() != recipient.AgreedValueLength())
|
2019-08-03 20:32:36 +00:00
|
|
|
{
|
2019-08-03 21:01:02 +00:00
|
|
|
std::cout << "FAILED authenticated key agreement domain parameter consistency" << std::endl;
|
|
|
|
return false;
|
2019-08-03 20:32:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-03 21:01:02 +00:00
|
|
|
std::cout << "passed authenticated key agreement domain parameter consistency" << std::endl;
|
2019-08-03 20:32:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 03:52:50 +00:00
|
|
|
SecByteBlock spriv1(initiator.StaticPrivateKeyLength()), spriv2(recipient.StaticPrivateKeyLength());
|
|
|
|
SecByteBlock epriv1(initiator.EphemeralPrivateKeyLength()), epriv2(recipient.EphemeralPrivateKeyLength());
|
|
|
|
SecByteBlock spub1(initiator.StaticPublicKeyLength()), spub2(recipient.StaticPublicKeyLength());
|
|
|
|
SecByteBlock epub1(initiator.EphemeralPublicKeyLength()), epub2(recipient.EphemeralPublicKeyLength());
|
|
|
|
SecByteBlock val1(initiator.AgreedValueLength()), val2(recipient.AgreedValueLength());
|
2019-08-03 20:32:36 +00:00
|
|
|
|
|
|
|
initiator.GenerateStaticKeyPair(GlobalRNG(), spriv1, spub1);
|
2019-08-04 03:52:50 +00:00
|
|
|
recipient.GenerateStaticKeyPair(GlobalRNG(), spriv2, spub2);
|
2019-08-03 20:32:36 +00:00
|
|
|
initiator.GenerateEphemeralKeyPair(GlobalRNG(), epriv1, epub1);
|
2019-08-04 03:52:50 +00:00
|
|
|
recipient.GenerateEphemeralKeyPair(GlobalRNG(), epriv2, epub2);
|
2019-08-03 20:32:36 +00:00
|
|
|
|
2023-04-15 20:45:02 +00:00
|
|
|
std::memset(val1.begin(), 0x10, val1.size());
|
|
|
|
std::memset(val2.begin(), 0x11, val2.size());
|
2019-08-03 20:32:36 +00:00
|
|
|
|
2019-08-04 03:52:50 +00:00
|
|
|
if (initiator.Agree(val1, spriv1, epriv1, spub2, epub2) && recipient.Agree(val2, spriv2, epriv2, spub1, epub1))
|
|
|
|
{
|
|
|
|
std::cout << "passed authenticated key agreement protocol execution" << std::endl;
|
|
|
|
}
|
|
|
|
else
|
2019-08-03 20:32:36 +00:00
|
|
|
{
|
2019-08-04 03:52:50 +00:00
|
|
|
std::cout << "FAILED authenticated key agreement protocol execution" << std::endl;
|
2019-08-03 20:32:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-15 20:45:02 +00:00
|
|
|
if (std::memcmp(val1.begin(), val2.begin(), initiator.AgreedValueLength()))
|
2019-08-03 20:32:36 +00:00
|
|
|
{
|
|
|
|
std::cout << "FAILED authenticated agreed values not equal" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-05 07:51:58 +00:00
|
|
|
std::cout << "passed authenticated key agreement shared secret" << std::endl;
|
2019-08-03 20:32:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool SignatureValidate(PK_Signer &priv, PK_Verifier &pub, bool thorough)
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
bool pass = true, fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
fail = !pub.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2) || !priv.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2);
|
|
|
|
pass = pass && !fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << (fail ? "FAILED " : "passed ");
|
|
|
|
std::cout << "signature key validation\n";
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-12-24 22:17:32 +00:00
|
|
|
const byte message[] = "test message";
|
2018-07-30 00:30:19 +00:00
|
|
|
const int messageLen = 12;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
SecByteBlock signature(priv.MaxSignatureLength());
|
|
|
|
size_t signatureLength = priv.SignMessage(GlobalRNG(), message, messageLen, signature);
|
|
|
|
fail = !pub.VerifyMessage(message, messageLen, signature, signatureLength);
|
|
|
|
pass = pass && !fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << (fail ? "FAILED " : "passed ");
|
|
|
|
std::cout << "signature and verification\n";
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
++signature[0];
|
|
|
|
fail = pub.VerifyMessage(message, messageLen, signature, signatureLength);
|
|
|
|
pass = pass && !fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << (fail ? "FAILED " : "passed ");
|
|
|
|
std::cout << "checking invalid signature" << std::endl;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
if (priv.MaxRecoverableLength() > 0)
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
signatureLength = priv.SignMessageWithRecovery(GlobalRNG(), message, messageLen, NULLPTR, 0, signature);
|
|
|
|
SecByteBlock recovered(priv.MaxRecoverableLengthFromSignatureLength(signatureLength));
|
|
|
|
DecodingResult result = pub.RecoverMessage(recovered, NULLPTR, 0, signature, signatureLength);
|
2023-04-15 20:45:02 +00:00
|
|
|
fail = !(result.isValidCoding && result.messageLength == messageLen && std::memcmp(recovered, message, messageLen) == 0);
|
2018-07-30 00:30:19 +00:00
|
|
|
pass = pass && !fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << (fail ? "FAILED " : "passed ");
|
|
|
|
std::cout << "signature and verification with recovery" << std::endl;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
++signature[0];
|
|
|
|
result = pub.RecoverMessage(recovered, NULLPTR, 0, signature, signatureLength);
|
|
|
|
fail = result.isValidCoding;
|
|
|
|
pass = pass && !fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << (fail ? "FAILED " : "passed ");
|
|
|
|
std::cout << "recovery with invalid signature" << std::endl;
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool ValidateBBS()
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "\nBlumBlumShub validation suite running...\n\n";
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
Integer p("212004934506826557583707108431463840565872545889679278744389317666981496005411448865750399674653351");
|
|
|
|
Integer q("100677295735404212434355574418077394581488455772477016953458064183204108039226017738610663984508231");
|
|
|
|
Integer seed("63239752671357255800299643604761065219897634268887145610573595874544114193025997412441121667211431");
|
|
|
|
BlumBlumShub bbs(p, q, seed);
|
|
|
|
bool pass = true, fail;
|
|
|
|
int j;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 02:35:36 +00:00
|
|
|
const byte output1[] = {
|
2018-07-30 00:30:19 +00:00
|
|
|
0x49,0xEA,0x2C,0xFD,0xB0,0x10,0x64,0xA0,0xBB,0xB9,
|
|
|
|
0x2A,0xF1,0x01,0xDA,0xC1,0x8A,0x94,0xF7,0xB7,0xCE};
|
2018-07-30 02:35:36 +00:00
|
|
|
const byte output2[] = {
|
2018-07-30 00:30:19 +00:00
|
|
|
0x74,0x45,0x48,0xAE,0xAC,0xB7,0x0E,0xDF,0xAF,0xD7,
|
|
|
|
0xD5,0x0E,0x8E,0x29,0x83,0x75,0x6B,0x27,0x46,0xA1};
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
byte buf[20];
|
2018-07-30 02:35:36 +00:00
|
|
|
std::ostringstream oss;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bbs.GenerateBlock(buf, 20);
|
2023-04-15 20:45:02 +00:00
|
|
|
fail = std::memcmp(output1, buf, 20) != 0;
|
2018-07-30 00:30:19 +00:00
|
|
|
pass = pass && !fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 02:35:36 +00:00
|
|
|
oss << (fail ? "FAILED " : "passed ");
|
2018-07-30 00:30:19 +00:00
|
|
|
for (j=0;j<20;j++)
|
2018-07-30 02:35:36 +00:00
|
|
|
oss << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j];
|
|
|
|
oss << std::endl;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bbs.Seek(10);
|
|
|
|
bbs.GenerateBlock(buf, 10);
|
2023-04-15 20:45:02 +00:00
|
|
|
fail = std::memcmp(output1+10, buf, 10) != 0;
|
2018-07-30 00:30:19 +00:00
|
|
|
pass = pass && !fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 02:35:36 +00:00
|
|
|
oss << (fail ? "FAILED " : "passed ");
|
2018-07-30 00:30:19 +00:00
|
|
|
for (j=0;j<10;j++)
|
2018-07-30 02:35:36 +00:00
|
|
|
oss << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j];
|
|
|
|
oss << std::endl;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bbs.Seek(1234567);
|
|
|
|
bbs.GenerateBlock(buf, 20);
|
2023-04-15 20:45:02 +00:00
|
|
|
fail = std::memcmp(output2, buf, 20) != 0;
|
2018-07-30 00:30:19 +00:00
|
|
|
pass = pass && !fail;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 02:35:36 +00:00
|
|
|
oss << (fail ? "FAILED " : "passed ");
|
2018-07-30 00:30:19 +00:00
|
|
|
for (j=0;j<20;j++)
|
2018-07-30 02:35:36 +00:00
|
|
|
oss << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j];
|
|
|
|
oss << std::endl;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 02:35:36 +00:00
|
|
|
std::cout << oss.str();
|
2018-07-28 18:57:12 +00:00
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool ValidateECP()
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 19:58:44 +00:00
|
|
|
// Remove word recommend. Some ECP curves may not be recommended depending
|
|
|
|
// on whom you ask. ECP is more descriptive item in this case.
|
|
|
|
std::cout << "\nTesting SEC 2, NIST and Brainpool ECP curves...\n\n";
|
2018-07-30 00:30:19 +00:00
|
|
|
bool pass = true; OID oid;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
while (!(oid = DL_GroupParameters_EC<ECP>::GetNextRecommendedParametersOID(oid)).GetValues().empty())
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
DL_GroupParameters_EC<ECP> params(oid);
|
2020-12-30 20:36:26 +00:00
|
|
|
pass = params.Validate(GlobalRNG(), 2);
|
2020-12-30 11:38:38 +00:00
|
|
|
|
|
|
|
// Test addition of identity element
|
|
|
|
DL_GroupParameters_EC<ECP>::Element e1;
|
|
|
|
e1 = params.GetCurve().Add(e1, e1);
|
2020-12-30 20:36:26 +00:00
|
|
|
pass = params.IsIdentity(e1) && pass;
|
2020-12-30 11:38:38 +00:00
|
|
|
|
|
|
|
// Test doubling of identity element
|
|
|
|
DL_GroupParameters_EC<ECP>::Element e2;
|
|
|
|
e2 = params.GetCurve().Double(e2);
|
2020-12-30 20:36:26 +00:00
|
|
|
pass = params.IsIdentity(e2) && pass;
|
2020-12-30 11:38:38 +00:00
|
|
|
|
|
|
|
// Test multiplication of identity element
|
|
|
|
DL_GroupParameters_EC<ECP>::Element e3;
|
|
|
|
Integer two = Integer::Two();
|
|
|
|
e3 = params.GetCurve().Multiply(two, e3);
|
2020-12-30 20:36:26 +00:00
|
|
|
pass = params.IsIdentity(e3) && pass;
|
2020-12-30 11:38:38 +00:00
|
|
|
|
2020-12-30 20:36:26 +00:00
|
|
|
std::cout << (pass ? "passed" : "FAILED") << " " << std::dec << params.GetCurve().GetField().MaxElementBitLength() << " bits\n";
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "\nECP validation suite running...\n\n";
|
2019-07-02 14:47:46 +00:00
|
|
|
return ValidateECP_Agreement() && ValidateECP_Encrypt() && ValidateECP_NULLDigest_Encrypt() && ValidateECP_Sign() && pass;
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool ValidateEC2N()
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 19:58:44 +00:00
|
|
|
// Remove word recommend. Binary curves may not be recommended depending
|
|
|
|
// on whom you ask. EC2N is more descriptive item in this case.
|
|
|
|
std::cout << "\nTesting SEC 2 EC2N curves...\n\n";
|
2018-07-30 00:30:19 +00:00
|
|
|
bool pass = true; OID oid;
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
#if 1 // TODO: turn this back on when I make EC2N faster for pentanomial basis
|
|
|
|
while (!(oid = DL_GroupParameters_EC<EC2N>::GetNextRecommendedParametersOID(oid)).GetValues().empty())
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
DL_GroupParameters_EC<EC2N> params(oid);
|
2020-12-30 20:36:26 +00:00
|
|
|
pass = params.Validate(GlobalRNG(), 2);
|
2020-12-30 11:38:38 +00:00
|
|
|
|
|
|
|
// Test addition of identity element
|
|
|
|
DL_GroupParameters_EC<EC2N>::Element e1;
|
|
|
|
e1 = params.GetCurve().Add(e1, e1);
|
2020-12-30 20:36:26 +00:00
|
|
|
pass = params.IsIdentity(e1) && pass;
|
2020-12-30 11:38:38 +00:00
|
|
|
|
|
|
|
// Test doubling of identity element
|
|
|
|
DL_GroupParameters_EC<EC2N>::Element e2;
|
|
|
|
e2 = params.GetCurve().Double(e2);
|
2020-12-30 20:36:26 +00:00
|
|
|
pass = params.IsIdentity(e2) && pass;
|
2020-12-30 11:38:38 +00:00
|
|
|
|
|
|
|
// Test multiplication of identity element
|
|
|
|
DL_GroupParameters_EC<EC2N>::Element e3;
|
|
|
|
Integer two = Integer::Two();
|
|
|
|
e3 = params.GetCurve().Multiply(two, e3);
|
2020-12-30 20:36:26 +00:00
|
|
|
pass = params.IsIdentity(e3) && pass;
|
2020-12-30 11:38:38 +00:00
|
|
|
|
2020-12-30 20:36:26 +00:00
|
|
|
std::cout << (pass ? "passed" : "FAILED") << " " << params.GetCurve().GetField().MaxElementBitLength() << " bits\n";
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
2018-07-30 00:30:19 +00:00
|
|
|
#endif
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "\nEC2N validation suite running...\n\n";
|
|
|
|
return ValidateEC2N_Agreement() && ValidateEC2N_Encrypt() && ValidateEC2N_Sign() && pass;
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool ValidateRSA()
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "\nRSA validation suite running...\n\n";
|
|
|
|
return ValidateRSA_Encrypt() && ValidateRSA_Sign();
|
|
|
|
}
|
2018-07-28 18:57:12 +00:00
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool ValidateLUC()
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "\nLUC validation suite running...\n\n";
|
|
|
|
return ValidateLUC_Encrypt() && ValidateLUC_Sign();
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool ValidateLUC_DL()
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
// Prologue printed in each function
|
|
|
|
return ValidateLUC_DL_Encrypt() && ValidateLUC_DL_Sign();
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:30:19 +00:00
|
|
|
bool ValidateRabin()
|
2018-07-28 18:57:12 +00:00
|
|
|
{
|
2018-07-30 00:30:19 +00:00
|
|
|
std::cout << "\nRabin validation suite running...\n\n";
|
|
|
|
return ValidateRabin_Encrypt() && ValidateRabin_Sign();
|
2018-07-28 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END // Test
|
|
|
|
NAMESPACE_END // CryptoPP
|