// validat0.cpp - originally written and placed in the public domain by Wei Dai and Jeffrey Walton
// Routines in this source file are only tested in Debug builds
#include "pch.h"
#include "secblock.h"
#include "integer.h"
#include "nbtheory.h"
#include "zdeflate.h"
#include "filters.h"
#include "stdcpp.h"
#include "default.h"
#include "zinflate.h"
#include "channels.h"
#include "files.h"
#include "gf2n.h"
#include "gzip.h"
#include "zlib.h"
#include "ida.h"
#include "hex.h"
#include "asn.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include "validate.h"
// 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)
#if defined(CRYPTOPP_EXTENDED_VALIDATION)
// Issue 64: "PolynomialMod2::operator<<=", http://github.com/weidai11/cryptopp/issues/64
bool TestPolynomialMod2()
{
bool pass1 = true, pass2 = true, pass3 = true;
std::cout << "\nTesting PolynomialMod2 bit operations...\n\n";
static const unsigned int start = 0;
static const unsigned int stop = 4 * WORD_BITS + 1;
for (unsigned int i = start; i < stop; i++)
{
PolynomialMod2 p(1);
p <<= i;
Integer n(Integer::One());
n <<= i;
std::ostringstream oss1;
oss1 << p;
std::string str1, str2;
// str1 needs the commas removed used for grouping
str1 = oss1.str();
str1.erase(std::remove(str1.begin(), str1.end(), ','), str1.end());
// str1 needs the trailing 'b' removed
str1.erase(str1.end() - 1);
// str2 is fine as-is
str2 = IntToString(n, 2);
pass1 &= (str1 == str2);
}
for (unsigned int i = start; i < stop; i++)
{
const word w((word)SIZE_MAX);
PolynomialMod2 p(w);
p <<= i;
Integer n(Integer::POSITIVE, static_cast<lword>(w));
n <<= i;
std::ostringstream oss1;
oss1 << p;
std::string str1, str2;
// str1 needs the commas removed used for grouping
str1 = oss1.str();
str1.erase(std::remove(str1.begin(), str1.end(), ','), str1.end());
// str1 needs the trailing 'b' removed
str1.erase(str1.end() - 1);
// str2 is fine as-is
str2 = IntToString(n, 2);
pass2 &= (str1 == str2);
}
RandomNumberGenerator& prng = GlobalRNG();
for (unsigned int i = start; i < stop; i++)
{
word w; // Cast to lword due to Visual Studio
prng.GenerateBlock((byte*)&w, sizeof(w));
PolynomialMod2 p(w);
p <<= i;
Integer n(Integer::POSITIVE, static_cast<lword>(w));
n <<= i;
std::ostringstream oss1;
oss1 << p;
std::string str1, str2;
// str1 needs the commas removed used for grouping
str1 = oss1.str();
str1.erase(std::remove(str1.begin(), str1.end(), ','), str1.end());
// str1 needs the trailing 'b' removed
str1.erase(str1.end() - 1);
// str2 is fine as-is
str2 = IntToString(n, 2);
if (str1 != str2)
{
std::cout << " Oops..." << "\n";
std::cout << " random: " << std::hex << n << std::dec << "\n";
std::cout << " str1: " << str1 << "\n";
std::cout << " str2: " << str2 << "\n";
}
pass3 &= (str1 == str2);
}
std::cout << (!pass1 ? "FAILED" : "passed") << ": " << "1 shifted over range [" << std::dec << start << "," << stop << "]" << "\n";
std::cout << (!pass2 ? "FAILED" : "passed") << ": " << "0x" << std::hex << word(SIZE_MAX) << std::dec << " shifted over range [" << start << "," << stop << "]" << "\n";
std::cout << (!pass3 ? "FAILED" : "passed") << ": " << "random values shifted over range [" << std::dec << start << "," << stop << "]" << "\n";
return pass1 && pass2 && pass3;
}
#endif
#if defined(CRYPTOPP_EXTENDED_VALIDATION)
bool TestCompressors()
{
std::cout << "\nTesting Compressors and Decompressors...\n\n";
bool fail1 = false, fail2 = false, fail3 = false;
try
{
// Gzip uses Adler32 checksums. We expect a failure to to happen on occasion.
// If we see more than 2 failures in a run of 128, then we need to investigate.
unsigned int truncatedCount=0;
for (unsigned int i = 0; i<128; ++i)
{
std::string src, dest, rec;
unsigned int len = GlobalRNG().GenerateWord32(4, 0xfff);
RandomNumberSource(GlobalRNG(), len, true, new StringSink(src));
StringSource(src, true, new Gzip(new StringSink(dest)));
StringSource(dest, true, new Gunzip(new StringSink(rec)));
if (src != rec)
throw Exception(Exception::OTHER_ERROR, "Gzip failed to decompress stream");
// Tamper
try {
StringSource(dest.substr(0, len - 2), true, new Gunzip(new StringSink(rec)));
if (truncatedCount++ >= 2)
{
std::cout << "FAILED: Gzip failed to detect a truncated stream\n";
fail1 = true;
}
}
catch (const Exception&) {}
}
}
catch (const Exception& ex)
{
std::cout << "FAILED: " << ex.what() << "\n";
fail1 = true;
}
// **************************************************************
// Gzip Filename, Filetime and Comment
try
{
std::string filename = "test.txt";
std::string comment = "This is a test";
word32 filetime = GlobalRNG().GenerateWord32(4, 0xffffff);
AlgorithmParameters params = MakeParameters(Name::FileTime(), (int)filetime)
(Name::FileName(), ConstByteArrayParameter(filename.c_str(), false))
(Name::Comment(), ConstByteArrayParameter(comment.c_str(), false));
std::string src, dest;
unsigned int len = GlobalRNG().GenerateWord32(4, 0xfff);
RandomNumberSource(GlobalRNG(), len, true, new StringSink(src));
Gunzip unzip(new StringSink(dest));
StringSource(src, true, new Gzip(params, new Redirector(unzip)));
if (filename != unzip.GetFilename())
throw Exception(Exception::OTHER_ERROR, "Failed to retrieve filename");
if (filetime != unzip.GetFiletime())
throw Exception(Exception::OTHER_ERROR, "Failed to retrieve filetime");
if (comment != unzip.GetComment())
throw Exception(Exception::OTHER_ERROR, "Failed to retrieve comment");
std::cout << "passed: filenames, filetimes and comments\n";
}
catch (const Exception& ex)
{
std::cout << "FAILED: " << ex.what() << "\n";
}
// Unzip random data. See if we can induce a crash
for (unsigned int i = 0; i<128; i++)
{
SecByteBlock src;
unsigned int len = GlobalRNG().GenerateWord32(4, 0xfff);
RandomNumberSource(GlobalRNG(), len, true, new ArraySink(src, src.size()));
try {
ArraySource(src.data(), src.size(), true, new Gunzip(new Redirector(TheBitBucket())));
}
catch (const Exception&) {}
}
// Unzip random data. See if we can induce a crash
for (unsigned int i = 0; i<128; i++)
{
SecByteBlock src;
unsigned int len = GlobalRNG().GenerateWord32(4, 0xfff);
src.resize(len);
RandomNumberSource(GlobalRNG(), len, true, new ArraySink(src, src.size()));
src[0] = (byte)0x1f; // magic header
src[1] = (byte)0x8b;
src[2] = 0x00; // extra flags
src[3] = sr
zhangshu7573256
- 粉丝: 0
- 资源: 10
最新资源
- 基于opensees 平台建立的单柱墩模型 考虑了滑移粘接的捏缩效应 内容包括有 1.墩柱模型建模全过程及源代码 2.钢筋混凝土之间的粘接滑移 3.基于位移控制的滞回分析代码
- 车用驱动电机原理与控制基础-P144公式(6-54)
- 群智能算法优化bp:将思维进化算法结合两层bp,对数据进行预测回归,对多层bp神经网络有兴趣的朋友可以借鉴,有意咨询,非诚勿扰 思维进化优化算法(Memetic Evolutionary Algor
- 纸箱封装包装机sw22可编辑全套技术资料100%好用.zip
- 小清新教学通用模板.pptx
- stm32 永磁同步电机pcb,原理图 利用stm32f4xx制作的pmsm 控制器电路原理图,pcb,还有pmsm simulink模型 以及simulink模型代码自动生成来设计电机控制算法资料
- JavaEE-图书管理系统源码+数据库+文档说明
- 机器视觉,OpenCV,Qt,工业相机采集,图像采集,图像处理,卡尺工具,找线,找圆,颜色检测,模板匹配,形状匹配,海康工业相机采集+基于形状的模板匹配界面,提前说明,形状匹配算法封装成dll直接调用
- 模拟IC设计,buck型dcdc设计,smic.18工艺,aot自适应导通模式,输出0.6v,最大负载电流1.2A,纹波30mv附近,可实现pwm和pfm的切,可以直接导入到cadence仿真查看,比
- MATLAB代码:面向削峰填谷的电动汽车多目标优化调度策略 关键词:电动汽车 削峰填谷 多目标 充放电优化 参考文档:自己整理的说明文档,公式、约束、数据齐全,可联系我查看 仿真平台:MATLAB Y
- 整车控制器VCU模型,控制策略,说明书,接口定义文档
- PyTorch入门案例-手写数字图像去噪
- 中宝磨牛设备sw18可编辑全套技术资料100%好用.zip
- 重负载平移机sw15可编辑全套技术资料100%好用.zip
- 汇川easy523+HMI. 电子凸轮双轴绕线 绕线的例程 主轴周期360度 一层为来回一圈,自动计算圈数,绕线完成后输出完成信号,可与其他取料机对接,进行自动放转子,自动取绕线完成产品A1431
- 永磁同步电机模型预测电流控制Simulink仿真
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈