📄 validat1.cpp
字号:
// validat1.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#include "files.h"
#include "hex.h"
#include "base64.h"
#include "modes.h"
#include "cbcmac.h"
#include "dmac.h"
#include "idea.h"
#include "des.h"
#include "rc2.h"
#include "arc4.h"
#include "rc5.h"
#include "blowfish.h"
#include "diamond.h"
#include "wake.h"
#include "3way.h"
#include "safer.h"
#include "gost.h"
#include "shark.h"
#include "cast.h"
#include "square.h"
#include "seal.h"
#include "rc6.h"
#include "mars.h"
#include "rijndael.h"
#include "twofish.h"
#include "serpent.h"
#include "skipjack.h"
#include "osrng.h"
#include "zdeflate.h"
#include <stdlib.h>
#include <time.h>
#include <memory>
#include <iostream>
#include <iomanip>
#include "validate.h"
USING_NAMESPACE(CryptoPP)
USING_NAMESPACE(std)
bool ValidateAll(bool thorough)
{
bool pass=TestSettings();
pass=TestOS_RNG() && pass;
pass=ValidateCRC32() && pass;
pass=ValidateAdler32() && pass;
pass=ValidateMD2() && pass;
pass=ValidateMD5() && pass;
pass=ValidateSHA() && pass;
pass=ValidateSHA2() && pass;
pass=ValidateHAVAL() && pass;
pass=ValidateTiger() && pass;
pass=ValidateRIPEMD() && pass;
pass=ValidatePanama() && pass;
pass=ValidateMD5MAC() && pass;
pass=ValidateHMAC() && pass;
pass=ValidateXMACC() && pass;
pass=ValidatePBKDF() && pass;
pass=ValidateDES() && pass;
pass=ValidateCipherModes() && pass;
pass=ValidateIDEA() && pass;
pass=ValidateSAFER() && pass;
pass=ValidateRC2() && pass;
pass=ValidateARC4() && pass;
pass=ValidateRC5() && pass;
pass=ValidateBlowfish() && pass;
pass=ValidateDiamond2() && pass;
pass=ValidateThreeWay() && pass;
pass=ValidateGOST() && pass;
pass=ValidateSHARK() && pass;
pass=ValidateCAST() && pass;
pass=ValidateSquare() && pass;
pass=ValidateSKIPJACK() && pass;
pass=ValidateSEAL() && pass;
pass=ValidateRC6() && pass;
pass=ValidateMARS() && pass;
pass=ValidateRijndael() && pass;
pass=ValidateTwofish() && pass;
pass=ValidateSerpent() && pass;
pass=ValidateBBS() && pass;
pass=ValidateDH() && pass;
pass=ValidateMQV() && pass;
pass=ValidateRSA() && pass;
pass=ValidateElGamal() && pass;
pass=ValidateDLIES() && pass;
pass=ValidateNR() && pass;
pass=ValidateDSA(thorough) && pass;
pass=ValidateLUC() && pass;
pass=ValidateLUC_DH() && pass;
pass=ValidateLUC_DL() && pass;
pass=ValidateXTR_DH() && pass;
pass=ValidateRabin() && pass;
pass=ValidateRW() && pass;
// pass=ValidateBlumGoldwasser() && pass;
pass=ValidateECP() && pass;
pass=ValidateEC2N() && pass;
pass=ValidateECDSA() && pass;
pass=ValidateESIGN() && pass;
if (pass)
cout << "\nAll tests passed!\n";
else
cout << "\nOops! Not all tests passed.\n";
return pass;
}
bool TestSettings()
{
bool pass = true;
cout << "\nTesting Settings...\n\n";
if (*(word32 *)"\x01\x02\x03\x04" == 0x04030201L)
{
#ifdef IS_LITTLE_ENDIAN
cout << "passed: ";
#else
cout << "FAILED: ";
pass = false;
#endif
cout << "Your machine is little endian.\n";
}
else if (*(word32 *)"\x01\x02\x03\x04" == 0x01020304L)
{
#ifndef IS_LITTLE_ENDIAN
cout << "passed: ";
#else
cout << "FAILED: ";
pass = false;
#endif
cout << "Your machine is big endian.\n";
}
else
{
cout << "FAILED: Your machine is neither big endian nor little endian.\n";
pass = false;
}
if (sizeof(byte) == 1)
cout << "passed: ";
else
{
cout << "FAILED: ";
pass = false;
}
cout << "sizeof(byte) == " << sizeof(byte) << endl;
if (sizeof(word16) == 2)
cout << "passed: ";
else
{
cout << "FAILED: ";
pass = false;
}
cout << "sizeof(word16) == " << sizeof(word16) << endl;
if (sizeof(word32) == 4)
cout << "passed: ";
else
{
cout << "FAILED: ";
pass = false;
}
cout << "sizeof(word32) == " << sizeof(word32) << endl;
#ifdef WORD64_AVAILABLE
if (sizeof(word64) == 8)
cout << "passed: ";
else
{
cout << "FAILED: ";
pass = false;
}
cout << "sizeof(word64) == " << sizeof(word64) << endl;
#else
if (sizeof(dword) >= 8)
{
cout << "FAILED: sizeof(dword) >= 8, but WORD64_AVAILABLE not defined" << endl;
pass = false;
}
else
cout << "passed: word64 not available" << endl;
#endif
if (sizeof(dword) == 2*sizeof(word))
cout << "passed: ";
else
{
cout << "FAILED: ";
pass = false;
}
cout << "sizeof(word) == " << sizeof(word) << ", sizeof(dword) == " << sizeof(dword) << endl;
dword test = (dword(1)<<WORD_BITS) + 2;
if (HIGH_WORD(test) == 1 && LOW_WORD(test) == 2)
cout << "passed: ";
else
{
cout << "FAILED: ";
pass = false;
}
cout << "HIGH_WORD() and LOW_WORD() macros\n";
if (!pass)
{
cout << "Some critical setting in config.h is in error. Please fix it and recompile." << endl;
abort();
}
return pass;
}
bool TestOS_RNG()
{
bool pass = true;
member_ptr<RandomNumberGenerator> rng;
#ifdef BLOCKING_RNG_AVAILABLE
try {rng.reset(new BlockingRng);}
catch (OS_RNG_Err &) {}
#endif
if (rng.get())
{
cout << "\nTesting operating system provided blocking random number generator...\n\n";
ArraySink *sink;
RandomNumberSource test(*rng, UINT_MAX, false, new Deflator(sink=new ArraySink(NULL,0)));
unsigned long total=0, length=0;
time_t t = time(NULL), t1 = 0;
// check that it doesn't take too long to generate a reasonable amount of randomness
while (total < 16 && (t1 < 10 || total*8 > (unsigned long)t1))
{
test.Pump(1);
total += 1;
t1 = time(NULL) - t;
}
if (total < 16)
{
cout << "FAILED:";
pass = false;
}
else
cout << "passed:";
cout << " it took " << t1 << " seconds to generate " << total << " bytes" << endl;
if (t1 < 2)
{
// that was fast, are we really blocking?
// first exhaust the extropy reserve
t = time(NULL);
while (time(NULL) - t < 2)
{
test.Pump(1);
total += 1;
}
// if it generates too many bytes in a certain amount of time,
// something's probably wrong
t = time(NULL);
while (time(NULL) - t < 2)
{
test.Pump(1);
total += 1;
length += 1;
}
// turn off this test because it fails on several systems, including Darwin
// they don't block, or gather entropy too fast?
if (false) // (length > 1024)
{
cout << "FAILED:";
pass = false;
}
else
cout << "passed:";
cout << " it generated " << length << " bytes in " << time(NULL) - t << " seconds" << endl;
}
test.AttachedTransformation()->MessageEnd();
if (sink->TotalPutLength() < total)
{
cout << "FAILED:";
pass = false;
}
else
cout << "passed:";
cout << " " << total << " generated bytes compressed to " << sink->TotalPutLength() << " bytes by DEFLATE" << endl;
}
else
cout << "\nNo operating system provided blocking random number generator, skipping test." << endl;
rng.reset(NULL);
#ifdef NONBLOCKING_RNG_AVAILABLE
try {rng.reset(new NonblockingRng);}
catch (OS_RNG_Err &) {}
#endif
if (rng.get())
{
cout << "\nTesting operating system provided nonblocking random number generator...\n\n";
ArraySink *sink;
RandomNumberSource test(*rng, 100000, true, new Deflator(sink=new ArraySink(NULL, 0)));
if (sink->TotalPutLength() < 100000)
{
cout << "FAILED:";
pass = false;
}
else
cout << "passed:";
cout << " 100000 generated bytes compressed to " << sink->TotalPutLength() << " bytes by DEFLATE" << endl;
}
else
cout << "\nNo operating system provided nonblocking random number generator, skipping test." << endl;
return pass;
}
// VC50 workaround
typedef auto_ptr<BlockTransformation> apbt;
class CipherFactory
{
public:
virtual unsigned int BlockSize() const =0;
virtual unsigned int KeyLength() const =0;
virtual apbt NewEncryption(const byte *key) const =0;
virtual apbt NewDecryption(const byte *key) const =0;
};
template <class E, class D> class FixedRoundsCipherFactory : public CipherFactory
{
public:
FixedRoundsCipherFactory(unsigned int keylen=0) : m_keylen(keylen?keylen:E::DEFAULT_KEYLENGTH) {}
unsigned int BlockSize() const {return E::BLOCKSIZE;}
unsigned int KeyLength() const {return m_keylen;}
apbt NewEncryption(const byte *key) const
{return apbt(new E(key, m_keylen));}
apbt NewDecryption(const byte *key) const
{return apbt(new D(key, m_keylen));}
unsigned int m_keylen;
};
template <class E, class D> class VariableRoundsCipherFactory : public CipherFactory
{
public:
VariableRoundsCipherFactory(unsigned int keylen=0, unsigned int rounds=0)
: m_keylen(keylen ? keylen : E::DEFAULT_KEYLENGTH), m_rounds(rounds ? rounds : E::DEFAULT_ROUNDS) {}
unsigned int BlockSize() const {return E::BLOCKSIZE;}
unsigned int KeyLength() const {return m_keylen;}
apbt NewEncryption(const byte *key) const
{return apbt(new E(key, m_keylen, m_rounds));}
apbt NewDecryption(const byte *key) const
{return apbt(new D(key, m_keylen, m_rounds));}
unsigned int m_keylen, m_rounds;
};
bool BlockTransformationTest(const CipherFactory &cg, BufferedTransformation &valdata, unsigned int tuples = 0xffff)
{
HexEncoder output(new FileSink(cout));
SecByteBlock plain(cg.BlockSize()), cipher(cg.BlockSize()), out(cg.BlockSize()), outplain(cg.BlockSize());
SecByteBlock key(cg.KeyLength());
bool pass=true, fail;
while (valdata.MaxRetrievable() && tuples--)
{
valdata.Get(key, cg.KeyLength());
valdata.Get(plain, cg.BlockSize());
valdata.Get(cipher, cg.BlockSize());
apbt transE = cg.NewEncryption(key);
transE->ProcessBlock(plain, out);
fail = memcmp(out, cipher, cg.BlockSize()) != 0;
apbt transD = cg.NewDecryption(key);
transD->ProcessBlock(out, outplain);
fail=fail || memcmp(outplain, plain, cg.BlockSize());
pass = pass && !fail;
cout << (fail ? "FAILED " : "passed ");
output.Put(key, cg.KeyLength());
cout << " ";
output.Put(outplain, cg.BlockSize());
cout << " ";
output.Put(out, cg.BlockSize());
cout << endl;
}
return pass;
}
class FilterTester : public Unflushable<Sink>
{
public:
FilterTester(const byte *validOutput, unsigned int outputLen)
: validOutput(validOutput), outputLen(outputLen), counter(0), fail(false) {}
void PutByte(byte inByte)
{
if (counter >= outputLen || validOutput[counter] != inByte)
{
fail = true;
assert(false);
}
counter++;
}
unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking)
{
while (length--)
FilterTester::PutByte(*inString++);
if (messageEnd)
if (counter != outputLen)
{
fail = true;
assert(false);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -