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; }
bool ValidateRC2() { cout << "\nRC2 validation suite running...\n\n"; 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.MaxRetrievable()) { byte keyLen, effectiveLen; valdata.Get(keyLen); valdata.Get(effectiveLen); valdata.Get(key, keyLen); valdata.Get(plain, RC2Encryption::BLOCKSIZE); valdata.Get(cipher, RC2Encryption::BLOCKSIZE); apbt transE(new RC2Encryption(key, keyLen, effectiveLen)); transE->ProcessBlock(plain, out); fail = memcmp(out, cipher, RC2Encryption::BLOCKSIZE) != 0; apbt transD(new RC2Decryption(key, keyLen, effectiveLen)); transD->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 ValidateRC2() { std::cout << "\nRC2 validation suite running...\n\n"; FileSource valdata("TestData/rc2val.dat", true, new HexDecoder); HexEncoder output(new FileSink(std::cout)); SecByteBlock plain(RC2Encryption::BLOCKSIZE), cipher(RC2Encryption::BLOCKSIZE), out(RC2Encryption::BLOCKSIZE), outplain(RC2Encryption::BLOCKSIZE); SecByteBlock key(128); bool pass=true, fail; while (valdata.MaxRetrievable()) { 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> transE(new RC2Encryption(key, keyLen, effectiveLen)); transE->ProcessBlock(plain, out); fail = !VerifyBufsEqual(out, cipher, RC2Encryption::BLOCKSIZE); auto_ptr<BlockTransformation> transD(new RC2Decryption(key, keyLen, effectiveLen)); transD->ProcessBlock(out, outplain); fail=fail || !VerifyBufsEqual(outplain, plain, RC2Encryption::BLOCKSIZE); pass = pass && !fail; std::cout << (fail ? "FAILED " : "passed "); output.Put(key, keyLen); std::cout << " "; output.Put(outplain, RC2Encryption::BLOCKSIZE); std::cout << " "; output.Put(out, RC2Encryption::BLOCKSIZE); std::cout << std::endl; } return pass; }