⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validat1.cpp

📁 一个DES,RSA,MD5,RC4等加密算法的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// validat1.cpp - written and placed in the public domain by Wei Dai

#include "pch.h"

#include "files.h"
#include "hex.h"
#include "cbc.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 "rng.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 pass=TestSettings();
	pass=TestOS_RNG();

	pass=CRC32Validate() && pass;
	pass=Adler32Validate() && pass;
	pass=MD2Validate() && pass;
	pass=MD5Validate() && pass;
	pass=SHAValidate() && pass;
	pass=SHA2Validate() && pass;
	pass=HAVALValidate() && pass;
	pass=TigerValidate() && pass;
	pass=RIPEMDValidate() && pass;
	pass=PanamaValidate() && pass;

	pass=MD5MACValidate() && pass;
	pass=HMACValidate() && pass;
	pass=XMACCValidate() && pass;

	pass=DESValidate() && pass;
	pass=CipherModesValidate() && pass;
	pass=IDEAValidate() && pass;
	pass=SAFERValidate() && pass;
	pass=RC2Validate() && pass;
	pass=ARC4Validate() && pass;
	pass=RC5Validate() && pass;
	pass=BlowfishValidate() && pass;
	pass=Diamond2Validate() && pass;
	pass=ThreeWayValidate() && pass;
	pass=GOSTValidate() && pass;
	pass=SHARKValidate() && pass;
	pass=CASTValidate() && pass;
	pass=SquareValidate() && pass;
	pass=SKIPJACKValidate() && pass;
	pass=SEALValidate() && pass;
	pass=RC6Validate() && pass;
	pass=MARSValidate() && pass;
	pass=RijndaelValidate() && pass;
	pass=TwofishValidate() && pass;
	pass=SerpentValidate() && pass;

	pass=BBSValidate() && pass;
	pass=DHValidate() && pass;
	pass=MQVValidate() && pass;
	pass=RSAValidate() && pass;
	pass=ElGamalValidate() && pass;
	pass=NRValidate() && pass;
	pass=DSAValidate() && pass;
	pass=LUCValidate() && pass;
	pass=LUCDIFValidate() && pass;
	pass=LUCELGValidate() && pass;
	pass=XTRDHValidate() && pass;
	pass=RabinValidate() && pass;
	pass=RWValidate() && pass;
	pass=BlumGoldwasserValidate() && pass;
	pass=ECPValidate() && pass;
	pass=EC2NValidate() && pass;
	pass=ECDSAValidate() && 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;

#ifdef BLOCKING_RNG_AVAILABLE
	{
		cout << "\nTesting operating system provided blocking random number generator...\n\n";

		BlockingRng rng;
		ArraySink *sink;
		RandomNumberSource test(rng, 100000, false, new Deflator(sink=new ArraySink(NULL,0)));
		unsigned long total=0, length=0;
		time_t t;

		// check that it doesn't take too long to generate a reasonable amount of randomness
		t = time(NULL);
		test.Pump(16);
		total += 16;
		t = time(NULL) - t;
		if (t > 120)
		{
			cout << "FAILED:";
			pass = false;
		}
		else
			cout << "passed:";
		cout << "  it took " << t << " seconds to generate 16 bytes" << endl;

		if (t < 2)
		{
			// that was fast, are we really blocking?
			// first exhaust the extropy reserve
			t = time(NULL);
			while (time(NULL) - t < 2)
			{
				test.Pump(8);
				total += 8;
			}

			// 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(8);
				total += 8;
				length += 8;
			}
			if (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;
#endif

#ifdef NONBLOCKING_RNG_AVAILABLE
	{
		cout << "\nTesting operating system provided nonblocking random number generator...\n\n";

		NonblockingRng rng;
		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;
#endif

	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)
		{assert(E::KeyLength(m_keylen)==m_keylen && D::KeyLength(m_keylen)==m_keylen);}
	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)
		{assert(E::KeyLength(m_keylen)==m_keylen && D::KeyLength(m_keylen)==m_keylen);}
	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());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -