?? validat1.cpp
字號(hào):
// validat1.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#include "files.h"
#include "hex.h"
#include "idea.h"
#include "des.h"
#include "rc2.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 <stdlib.h>
#include <memory>
#include <iostream>
#include <iomanip>
#include "validate.h"
USING_NAMESPACE(CryptoPP)
USING_NAMESPACE(std)
bool ValidateAll()
{
bool pass=TestSettings();
pass=MD2Validate() && pass;
pass=MD5Validate() && pass;
pass=SHAValidate() && pass;
pass=HAVALValidate() && pass;
pass=TigerValidate() && pass;
pass=RIPEMDValidate() && pass;
pass=MD5MACValidate() && pass;
pass=HMACValidate() && pass;
pass=XMACCValidate() && pass;
pass=DESValidate() && pass;
pass=IDEAValidate() && pass;
pass=SAFERValidate() && pass;
pass=RC2Validate() && pass;
pass=RC5Validate() && pass;
pass=BlowfishValidate() && pass;
pass=Diamond2Validate() && pass;
pass=ThreeWayValidate() && pass;
pass=GOSTValidate() && pass;
pass=SHARKValidate() && pass;
pass=SHARK2Validate() && pass;
pass=CASTValidate() && pass;
pass=SquareValidate() && pass;
pass=SEALValidate() && pass;
pass=RC6Validate() && pass;
pass=MARSValidate() && 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=RabinValidate() && pass;
pass=RWValidate() && pass;
pass=BlumGoldwasserValidate() && pass;
pass=ECPValidate() && pass;
pass=EC2NValidate() && 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;
}
class CipherFactory
{
public:
virtual unsigned int BlockSize() const =0;
virtual unsigned int KeyLength() const =0;
virtual auto_ptr<BlockTransformation> NewEncryption(const byte *key) const =0;
virtual auto_ptr<BlockTransformation> NewDecryption(const byte *key) const =0;
};
template <class E, class D> class DefaultCipherFactory : public CipherFactory
{
public:
unsigned int BlockSize() const {return E::BLOCKSIZE;}
unsigned int KeyLength() const {return E::KEYLENGTH;}
auto_ptr<BlockTransformation> NewEncryption(const byte *key) const
{return auto_ptr<BlockTransformation>(new E(key));}
auto_ptr<BlockTransformation> NewDecryption(const byte *key) const
{return auto_ptr<BlockTransformation>(new D(key));}
};
template <class E, class D> class VariableCipherFactory : public CipherFactory
{
public:
VariableCipherFactory(unsigned int keylen, unsigned int n=0) : keylen(keylen), n(n?n:keylen) {}
unsigned int BlockSize() const {return E::BLOCKSIZE;}
unsigned int KeyLength() const {return keylen;}
auto_ptr<BlockTransformation> NewEncryption(const byte *key) const
{return auto_ptr<BlockTransformation>(new E(key, n));}
auto_ptr<BlockTransformation> NewDecryption(const byte *key) const
{return auto_ptr<BlockTransformation>(new D(key, n));}
unsigned int keylen, n;
};
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.MaxRetrieveable() && tuples--)
{
valdata.Get(key, cg.KeyLength());
valdata.Get(plain, cg.BlockSize());
valdata.Get(cipher, cg.BlockSize());
auto_ptr<BlockTransformation> trans = cg.NewEncryption(key);
trans->ProcessBlock(plain, out);
fail = memcmp(out, cipher, cg.BlockSize()) != 0;
trans = cg.NewDecryption(key);
trans->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;
}
bool DESValidate()
{
cout << "\nDES validation suite running...\n\n";
FileSource valdata("descert.dat", true, new HexDecoder);
return BlockTransformationTest(DefaultCipherFactory<DESEncryption, DESDecryption>(), valdata);
}
bool IDEAValidate()
{
cout << "\nIDEA validation suite running...\n\n";
FileSource valdata("ideaval.dat", true, new HexDecoder);
return BlockTransformationTest(DefaultCipherFactory<IDEAEncryption, IDEADecryption>(), valdata);
}
bool SAFERValidate()
{
cout << "\nSAFER validation suite running...\n\n";
FileSource valdata("saferval.dat", true, new HexDecoder);
bool pass = true;
pass = BlockTransformationTest(DefaultCipherFactory<SAFER_K64_Encryption, SAFER_K64_Decryption>(), valdata, 4) && pass;
pass = BlockTransformationTest(VariableCipherFactory<SAFER_K128_Encryption, SAFER_K128_Decryption>(16,12), valdata, 4) && pass;
pass = BlockTransformationTest(VariableCipherFactory<SAFER_SK64_Encryption, SAFER_SK64_Decryption>(8,6), valdata, 4) && pass;
pass = BlockTransformationTest(DefaultCipherFactory<SAFER_SK128_Encryption, SAFER_SK128_Decryption>(), valdata, 4) && pass;
return pass;
}
bool RC2Validate()
{
FileSource valdata("rc2val.dat", true, new HexDecoder);
HexEncoder output(new FileSink(cout));
SecByteBlock plain(RC2Encryption::BLOCKSIZE), cipher(RC2Encryption::BLOCKSIZE), out(RC2Encryption::BLOCKSIZE), outplain(RC2Encryption::BLOCKSIZE);
SecByteBlock key(128);
bool pass=true, fail;
while (valdata.MaxRetrieveable())
{
byte keyLen, effectiveLen;
valdata.Get(keyLen);
valdata.Get(effectiveLen);
valdata.Get(key, keyLen);
valdata.Get(plain, RC2Encryption::BLOCKSIZE);
valdata.Get(cipher, RC2Encryption::BLOCKSIZE);
auto_ptr<BlockTransformation> trans(new RC2Encryption(key, keyLen, effectiveLen));
trans->ProcessBlock(plain, out);
fail = memcmp(out, cipher, RC2Encryption::BLOCKSIZE) != 0;
trans = auto_ptr<BlockTransformation>(new RC2Decryption(key, keyLen, effectiveLen));
trans->ProcessBlock(out, outplain);
fail=fail || memcmp(outplain, plain, RC2Encryption::BLOCKSIZE);
pass = pass && !fail;
cout << (fail ? "FAILED " : "PASSED ");
output.Put(key, keyLen);
cout << " ";
output.Put(outplain, RC2Encryption::BLOCKSIZE);
cout << " ";
output.Put(out, RC2Encryption::BLOCKSIZE);
cout << endl;
}
return pass;
}
bool RC4Validate()
{
return true;
#if 0
unsigned char Key0[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
unsigned char Input0[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
unsigned char Output0[] = {0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96};
unsigned char Key1[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
unsigned char Input1[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char Output1[]={0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79};
unsigned char Key2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char Input2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char Output2[]={0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a};
unsigned char Key3[]={0xef,0x01,0x23,0x45};
unsigned char Input3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char Output3[]={0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61};
unsigned char Key4[]={ 0x01,0x23,0x45,0x67,0x89,0xab, 0xcd,0xef };
unsigned char Input4[] =
{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -