Remove Coverity worakaround StreamState

Use std::ostringstream instead. Eventually I'd like to see the output stream passed into the function of interest. It will avoid problems on some mobile OSes that don't have standard inputs and outputs.
pull/696/head
Jeffrey Walton 2018-07-29 22:35:36 -04:00
parent bf37ccda6d
commit f290746a36
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
5 changed files with 158 additions and 174 deletions

View File

@ -13,6 +13,10 @@
#include "smartptr.h" #include "smartptr.h"
#include "stdcpp.h" #include "stdcpp.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#if CRYPTOPP_MSC_VERSION #if CRYPTOPP_MSC_VERSION
# pragma warning(disable: 4355) # pragma warning(disable: 4355)
#endif #endif
@ -32,7 +36,7 @@ const double CLOCK_TICKS_PER_SECOND = (double)CLK_TCK;
const double CLOCK_TICKS_PER_SECOND = 1000000.0; const double CLOCK_TICKS_PER_SECOND = 1000000.0;
#endif #endif
const byte defaultKey[] = "0123456789" // 168 + NULL extern const byte defaultKey[] = "0123456789" // 168 + NULL
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"00000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000"; "00000000000000000000000000000000000000000000000000000";
@ -43,67 +47,70 @@ time_t g_testBegin, g_testEnd;
void OutputResultBytes(const char *name, const char *provider, double length, double timeTaken) void OutputResultBytes(const char *name, const char *provider, double length, double timeTaken)
{ {
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639. std::ostringstream oss;
StreamState ss(std::cout);
// Coverity finding // Coverity finding
if (length < 0.000001f) length = 0.000001f; if (length < 0.000001f) length = 0.000001f;
if (timeTaken < 0.000001f) timeTaken = 0.000001f; if (timeTaken < 0.000001f) timeTaken = 0.000001f;
double mbs = length / timeTaken / (1024*1024); double mbs = length / timeTaken / (1024*1024);
std::cout << "\n<TR><TD>" << name << "<TD>" << provider; oss << "\n<TR><TD>" << name << "<TD>" << provider;
std::cout << std::setiosflags(std::ios::fixed); oss << std::setiosflags(std::ios::fixed);
std::cout << "<TD>" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << mbs; oss << "<TD>" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << mbs;
if (g_hertz > 1.0f) if (g_hertz > 1.0f)
{ {
const double cpb = timeTaken * g_hertz / length; const double cpb = timeTaken * g_hertz / length;
if (cpb < 24.0f) if (cpb < 24.0f)
std::cout << "<TD>" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << cpb; oss << "<TD>" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << cpb;
else else
std::cout << "<TD>" << std::setprecision(1) << std::setiosflags(std::ios::fixed) << cpb; oss << "<TD>" << std::setprecision(1) << std::setiosflags(std::ios::fixed) << cpb;
} }
g_logTotal += log(mbs); g_logTotal += log(mbs);
g_logCount++; g_logCount++;
std::cout << oss.str();
} }
void OutputResultKeying(double iterations, double timeTaken) void OutputResultKeying(double iterations, double timeTaken)
{ {
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639. std::ostringstream oss;
StreamState ss(std::cout);
// Coverity finding // Coverity finding
if (iterations < 0.000001f) iterations = 0.000001f; if (iterations < 0.000001f) iterations = 0.000001f;
if (timeTaken < 0.000001f) timeTaken = 0.000001f; if (timeTaken < 0.000001f) timeTaken = 0.000001f;
std::cout << "<TD>" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*1000*timeTaken/iterations); oss << "<TD>" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*1000*timeTaken/iterations);
// Coverity finding // Coverity finding
if (g_hertz > 1.0f) if (g_hertz > 1.0f)
std::cout << "<TD>" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / iterations; oss << "<TD>" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / iterations;
std::cout << oss.str();
} }
void OutputResultOperations(const char *name, const char *provider, const char *operation, bool pc, unsigned long iterations, double timeTaken) void OutputResultOperations(const char *name, const char *provider, const char *operation, bool pc, unsigned long iterations, double timeTaken)
{ {
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639. std::ostringstream oss;
StreamState ss(std::cout);
// Coverity finding // Coverity finding
if (!iterations) iterations++; if (!iterations) iterations++;
if (timeTaken < 0.000001f) timeTaken = 0.000001f; if (timeTaken < 0.000001f) timeTaken = 0.000001f;
std::cout << "\n<TR><TD>" << name << " " << operation << (pc ? " with precomputation" : ""); oss << "\n<TR><TD>" << name << " " << operation << (pc ? " with precomputation" : "");
std::cout << "<TD>" << provider; oss << "<TD>" << provider;
std::cout << "<TD>" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations); oss << "<TD>" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations);
// Coverity finding // Coverity finding
if (g_hertz > 1.0f) if (g_hertz > 1.0f)
{ {
const double t = timeTaken * g_hertz / iterations / 1000000; const double t = timeTaken * g_hertz / iterations / 1000000;
std::cout << "<TD>" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << t; oss << "<TD>" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << t;
} }
g_logTotal += log(iterations/timeTaken); g_logTotal += log(iterations/timeTaken);
g_logCount++; g_logCount++;
std::cout << oss.str();
} }
/* /*
@ -276,34 +283,40 @@ void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName = N
void AddHtmlHeader() void AddHtmlHeader()
{ {
std::ostringstream oss;
// HTML5 // HTML5
std::cout << "<!DOCTYPE HTML>"; oss << "<!DOCTYPE HTML>";
std::cout << "\n<HTML lang=\"en\">"; oss << "\n<HTML lang=\"en\">";
std::cout << "\n<HEAD>"; oss << "\n<HEAD>";
std::cout << "\n<META charset=\"UTF-8\">"; oss << "\n<META charset=\"UTF-8\">";
std::cout << "\n<TITLE>Speed Comparison of Popular Crypto Algorithms</TITLE>"; oss << "\n<TITLE>Speed Comparison of Popular Crypto Algorithms</TITLE>";
std::cout << "\n<STYLE>\n table {border-collapse: collapse;}"; oss << "\n<STYLE>\n table {border-collapse: collapse;}";
std::cout << "\n table, th, td, tr {border: 1px solid black;}\n</STYLE>"; oss << "\n table, th, td, tr {border: 1px solid black;}\n</STYLE>";
std::cout << "\n</HEAD>"; oss << "\n</HEAD>";
std::cout << "\n<BODY>"; oss << "\n<BODY>";
std::cout << "\n<H1><A href=\"http://www.cryptopp.com\">Crypto++</A> " << CRYPTOPP_VERSION / 100; oss << "\n<H1><A href=\"http://www.cryptopp.com\">Crypto++</A> " << CRYPTOPP_VERSION / 100;
std::cout << '.' << (CRYPTOPP_VERSION % 100) / 10 << '.' << CRYPTOPP_VERSION % 10 << " Benchmarks</H1>"; oss << '.' << (CRYPTOPP_VERSION % 100) / 10 << '.' << CRYPTOPP_VERSION % 10 << " Benchmarks</H1>";
std::cout << "\n<P>Here are speed benchmarks for some commonly used cryptographic algorithms.</P>"; oss << "\n<P>Here are speed benchmarks for some commonly used cryptographic algorithms.</P>";
if (g_hertz > 1.0f) if (g_hertz > 1.0f)
std::cout << "\n<P>CPU frequency of the test platform is " << g_hertz << " Hz.</P>"; oss << "\n<P>CPU frequency of the test platform is " << g_hertz << " Hz.</P>";
else else
std::cout << "\n<P>CPU frequency of the test platform was not provided.</P>" << std::endl; oss << "\n<P>CPU frequency of the test platform was not provided.</P>" << std::endl;
std::cout << oss.str();
} }
void AddHtmlFooter() void AddHtmlFooter()
{ {
std::ostringstream oss;
std::cout << "\n</BODY>"; std::cout << "\n</BODY>";
std::cout << "\n</HTML>" << std::endl; std::cout << "\n</HTML>" << std::endl;
std::cout << oss.str();
} }
void BenchmarkWithCommand(int argc, const char* const argv[]) void BenchmarkWithCommand(int argc, const char* const argv[])
@ -327,6 +340,7 @@ void Benchmark(Test::TestClass suites, double t, double hertz)
{ {
g_allocatedTime = t; g_allocatedTime = t;
g_hertz = hertz; g_hertz = hertz;
std::ostringstream oss;
AddHtmlHeader(); AddHtmlHeader();
@ -338,37 +352,36 @@ void Benchmark(Test::TestClass suites, double t, double hertz)
// Unkeyed algorithms // Unkeyed algorithms
if (suites & Test::Unkeyed) if (suites & Test::Unkeyed)
{ {
std::cout << "\n<BR>"; oss << "\n<BR>";
Benchmark1(t, hertz); Benchmark1(t, hertz);
} }
// Shared key algorithms // Shared key algorithms
if (suites & Test::SharedKey) if (suites & Test::SharedKey)
{ {
std::cout << "\n<BR>"; oss << "\n<BR>";
Benchmark2(t, hertz); Benchmark2(t, hertz);
} }
// Public key algorithms // Public key algorithms
if (suites & Test::PublicKey) if (suites & Test::PublicKey)
{ {
std::cout << "\n<BR>"; oss << "\n<BR>";
Benchmark3(t, hertz); Benchmark3(t, hertz);
} }
g_testEnd = ::time(NULLPTR); g_testEnd = ::time(NULLPTR);
{ oss << "\n<P>Throughput Geometric Average: " << std::setiosflags(std::ios::fixed);
StreamState state(std::cout); oss << std::exp(g_logTotal/(g_logCount > 0.0f ? g_logCount : 1.0f)) << std::endl;
std::cout << "\n<P>Throughput Geometric Average: " << std::setiosflags(std::ios::fixed);
std::cout << std::exp(g_logTotal/(g_logCount > 0.0f ? g_logCount : 1.0f)) << std::endl;
}
std::cout << "\n<P>Test started at " << TimeToString(g_testBegin); oss << "\n<P>Test started at " << TimeToString(g_testBegin);
std::cout << "\n<BR>Test ended at " << TimeToString(g_testEnd); oss << "\n<BR>Test ended at " << TimeToString(g_testEnd);
std::cout << std::endl; oss << std::endl;
AddHtmlFooter(); AddHtmlFooter();
std::cout << oss.str();
} }
void Benchmark1(double t, double hertz) void Benchmark1(double t, double hertz)

View File

@ -1061,6 +1061,9 @@ bool TestMersenne()
bool pass = true, fail = false; bool pass = true, fail = false;
member_ptr<RandomNumberGenerator> rng; member_ptr<RandomNumberGenerator> rng;
std::ostringstream oss;
oss << std::setiosflags(std::ios::fixed) << std::setprecision(6);
try {rng.reset(new PadlockRNG);} try {rng.reset(new PadlockRNG);}
catch (const PadlockRNG_Err &) {} catch (const PadlockRNG_Err &) {}
if (rng.get()) if (rng.get())
@ -1081,37 +1084,37 @@ bool TestMersenne()
fail = !(msr & (1 << 6U)); fail = !(msr & (1 << 6U));
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " VIA RNG is activated\n"; oss << " VIA RNG is activated\n";
// Bit 13 should be unset // Bit 13 should be unset
fail = !!(msr & (1 << 13U)); fail = !!(msr & (1 << 13U));
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " von Neumann corrector is activated\n"; oss << " von Neumann corrector is activated\n";
// Bit 14 should be unset // Bit 14 should be unset
fail = !!(msr & (1 << 14U)); fail = !!(msr & (1 << 14U));
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " String filter is deactivated\n"; oss << " String filter is deactivated\n";
// Bit 12:10 should be unset // Bit 12:10 should be unset
fail = !!(msr & (0x7 << 10U)); fail = !!(msr & (0x7 << 10U));
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " Bias voltage is unmodified\n"; oss << " Bias voltage is unmodified\n";
fail = false; fail = false;
if (t == zero || t == one) if (t == zero || t == one)
@ -1119,10 +1122,10 @@ bool TestMersenne()
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " All 0's or all 1's test\n"; oss << " All 0's or all 1's test\n";
MeterFilter meter(new Redirector(TheBitBucket())); MeterFilter meter(new Redirector(TheBitBucket()));
Deflator deflator(new Redirector(meter)); Deflator deflator(new Redirector(meter));
@ -1141,16 +1144,12 @@ bool TestMersenne()
if (mv < 0.98f) if (mv < 0.98f)
fail = true; fail = true;
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " Maurer Randomness Test returned value " << mv << "\n"; oss << " Maurer Randomness Test returned value " << mv << "\n";
fail = false; fail = false;
if (meter.GetTotalBytes() < SIZE) if (meter.GetTotalBytes() < SIZE)
@ -1158,10 +1157,10 @@ bool TestMersenne()
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n"; oss << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
try try
{ {
@ -1175,10 +1174,10 @@ bool TestMersenne()
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " discarded " << SIZE << " bytes\n"; oss << " discarded " << SIZE << " bytes\n";
try try
{ {
@ -1202,14 +1201,15 @@ bool TestMersenne()
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " GenerateWord32 and Crop\n"; oss << " GenerateWord32 and Crop\n";
} }
else else
std::cout << "Padlock RNG generator not available, skipping test.\n"; oss << "Padlock RNG generator not available, skipping test.\n";
std::cout << oss.str();
return pass; return pass;
} }
@ -1220,6 +1220,9 @@ bool TestRDRAND()
bool pass = true, fail = false; bool pass = true, fail = false;
member_ptr<RandomNumberGenerator> rng; member_ptr<RandomNumberGenerator> rng;
std::ostringstream oss;
oss << std::setiosflags(std::ios::fixed) << std::setprecision(6);
try {rng.reset(new RDRAND);} try {rng.reset(new RDRAND);}
catch (const RDRAND_Err &) {} catch (const RDRAND_Err &) {}
if (rng.get()) if (rng.get())
@ -1243,16 +1246,12 @@ bool TestRDRAND()
if (mv < 0.98f) if (mv < 0.98f)
fail = true; fail = true;
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " Maurer Randomness Test returned value " << mv << "\n"; oss << " Maurer Randomness Test returned value " << mv << "\n";
fail = false; fail = false;
if (meter.GetTotalBytes() < SIZE) if (meter.GetTotalBytes() < SIZE)
@ -1260,10 +1259,10 @@ bool TestRDRAND()
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n"; oss << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
try try
{ {
@ -1277,10 +1276,10 @@ bool TestRDRAND()
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " discarded " << SIZE << " bytes\n"; oss << " discarded " << SIZE << " bytes\n";
try try
{ {
@ -1304,14 +1303,15 @@ bool TestRDRAND()
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " GenerateWord32 and Crop\n"; oss << " GenerateWord32 and Crop\n";
} }
else else
std::cout << "RDRAND generator not available, skipping test.\n"; oss << "RDRAND generator not available, skipping test.\n";
std::cout << oss.str();
return pass; return pass;
} }
@ -1322,6 +1322,9 @@ bool TestRDSEED()
bool pass = true, fail = false; bool pass = true, fail = false;
member_ptr<RandomNumberGenerator> rng; member_ptr<RandomNumberGenerator> rng;
std::ostringstream oss;
oss << std::setiosflags(std::ios::fixed) << std::setprecision(6);
try {rng.reset(new RDSEED);} try {rng.reset(new RDSEED);}
catch (const RDSEED_Err &) {} catch (const RDSEED_Err &) {}
if (rng.get()) if (rng.get())
@ -1345,16 +1348,12 @@ bool TestRDSEED()
if (mv < 0.98f) if (mv < 0.98f)
fail = true; fail = true;
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " Maurer Randomness Test returned value " << mv << "\n"; oss << " Maurer Randomness Test returned value " << mv << "\n";
fail = false; fail = false;
if (meter.GetTotalBytes() < SIZE) if (meter.GetTotalBytes() < SIZE)
@ -1362,10 +1361,10 @@ bool TestRDSEED()
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n"; oss << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
try try
{ {
@ -1379,10 +1378,10 @@ bool TestRDSEED()
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " discarded " << SIZE << " bytes\n"; oss << " discarded " << SIZE << " bytes\n";
try try
{ {
@ -1406,14 +1405,15 @@ bool TestRDSEED()
pass &= !fail; pass &= !fail;
if (fail) if (fail)
std::cout << "FAILED:"; oss << "FAILED:";
else else
std::cout << "passed:"; oss << "passed:";
std::cout << " GenerateWord32 and Crop\n"; oss << " GenerateWord32 and Crop\n";
} }
else else
std::cout << "RDSEED generator not available, skipping test.\n"; oss << "RDSEED generator not available, skipping test.\n";
std::cout << oss.str();
return pass; return pass;
} }
#endif #endif

View File

@ -69,10 +69,9 @@ struct HashTestTuple
bool HashModuleTest(HashTransformation &md, const HashTestTuple *testSet, unsigned int testSetSize) bool HashModuleTest(HashTransformation &md, const HashTestTuple *testSet, unsigned int testSetSize)
{ {
bool pass=true, fail; bool pass=true, fail;
SecByteBlock digest(md.DigestSize()); std::ostringstream oss;
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639. SecByteBlock digest(md.DigestSize());
StreamState ss(std::cout);
for (unsigned int i=0; i<testSetSize; i++) for (unsigned int i=0; i<testSetSize; i++)
{ {
unsigned j; unsigned j;
@ -83,15 +82,16 @@ bool HashModuleTest(HashTransformation &md, const HashTestTuple *testSet, unsign
fail = !!memcmp(digest, testSet[i].output, md.DigestSize()) != 0; fail = !!memcmp(digest, testSet[i].output, md.DigestSize()) != 0;
pass = pass && !fail; pass = pass && !fail;
std::cout << (fail ? "FAILED " : "passed "); oss << (fail ? "FAILED " : "passed ");
for (j=0; j<md.DigestSize(); j++) for (j=0; j<md.DigestSize(); j++)
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)digest[j]; oss << std::setw(2) << std::setfill('0') << std::hex << (int)digest[j];
std::cout << " \"" << (char *)testSet[i].input << '\"'; oss << " \"" << (char *)testSet[i].input << '\"';
if (testSet[i].repeatTimes != 1) if (testSet[i].repeatTimes != 1)
std::cout << " repeated " << std::dec << testSet[i].repeatTimes << " times"; oss << " repeated " << std::dec << testSet[i].repeatTimes << " times";
std::cout << std::endl; oss << std::endl;
} }
std::cout << oss.str();
return pass; return pass;
} }
@ -407,21 +407,19 @@ bool ValidateMD5MAC()
{0x18,0xe3,0x49,0xa5,0x24,0x44,0xb3,0x0e,0x5e,0xba,0x5a,0xdd,0xdc,0xd9,0xf1,0x8d}, {0x18,0xe3,0x49,0xa5,0x24,0x44,0xb3,0x0e,0x5e,0xba,0x5a,0xdd,0xdc,0xd9,0xf1,0x8d},
{0xf2,0xb9,0x06,0xa5,0xb8,0x4b,0x9b,0x4b,0xbe,0x95,0xed,0x32,0x56,0x4e,0xe7,0xeb}}}; {0xf2,0xb9,0x06,0xa5,0xb8,0x4b,0x9b,0x4b,0xbe,0x95,0xed,0x32,0x56,0x4e,0xe7,0xeb}}};
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
byte digest[MD5MAC::DIGESTSIZE]; byte digest[MD5MAC::DIGESTSIZE];
bool pass=true, fail; bool pass=true, fail;
std::ostringstream oss;
std::cout << "\nMD5MAC validation suite running...\n"; oss << "\nMD5MAC validation suite running...\n";
for (int k=0; k<2; k++) for (int k=0; k<2; k++)
{ {
MD5MAC mac(keys[k]); MD5MAC mac(keys[k]);
std::cout << "\nKEY: "; oss << "\nKEY: ";
for (int j=0;j<MD5MAC::KEYLENGTH;j++) for (int j=0;j<MD5MAC::KEYLENGTH;j++)
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)keys[k][j]; oss << std::setw(2) << std::setfill('0') << std::hex << (int)keys[k][j];
std::cout << std::endl << std::endl; oss << std::endl << std::endl;
for (int i=0;i<7;i++) for (int i=0;i<7;i++)
{ {
mac.Update((byte *)TestVals[i], strlen(TestVals[i])); mac.Update((byte *)TestVals[i], strlen(TestVals[i]));
@ -429,13 +427,14 @@ bool ValidateMD5MAC()
fail = !!memcmp(digest, output[k][i], MD5MAC::DIGESTSIZE) fail = !!memcmp(digest, output[k][i], MD5MAC::DIGESTSIZE)
|| !mac.VerifyDigest(output[k][i], (byte *)TestVals[i], strlen(TestVals[i])); || !mac.VerifyDigest(output[k][i], (byte *)TestVals[i], strlen(TestVals[i]));
pass = pass && !fail; pass = pass && !fail;
std::cout << (fail ? "FAILED " : "passed "); oss << (fail ? "FAILED " : "passed ");
for (int j=0;j<MD5MAC::DIGESTSIZE;j++) for (int j=0;j<MD5MAC::DIGESTSIZE;j++)
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)digest[j]; oss << std::setw(2) << std::setfill('0') << std::hex << (int)digest[j];
std::cout << " \"" << TestVals[i] << '\"' << std::endl; oss << " \"" << TestVals[i] << '\"' << std::endl;
} }
} }
std::cout << oss.str();
return pass; return pass;
} }
#endif #endif
@ -540,13 +539,11 @@ bool ValidateTTMAC()
{0x54,0xba,0xc3,0x92,0xa8,0x86,0x80,0x6d,0x16,0x95,0x56,0xfc,0xbb,0x67,0x89,0xb5,0x4f,0xb3,0x64,0xfb}, {0x54,0xba,0xc3,0x92,0xa8,0x86,0x80,0x6d,0x16,0x95,0x56,0xfc,0xbb,0x67,0x89,0xb5,0x4f,0xb3,0x64,0xfb},
{0x0c,0xed,0x2c,0x9f,0x8f,0x0d,0x9d,0x03,0x98,0x1a,0xb5,0xc8,0x18,0x4b,0xac,0x43,0xdd,0x54,0xc4,0x84}}; {0x0c,0xed,0x2c,0x9f,0x8f,0x0d,0x9d,0x03,0x98,0x1a,0xb5,0xc8,0x18,0x4b,0xac,0x43,0xdd,0x54,0xc4,0x84}};
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
byte digest[TTMAC::DIGESTSIZE]; byte digest[TTMAC::DIGESTSIZE];
bool pass=true, fail; bool pass=true, fail;
std::ostringstream oss;
std::cout << "\nTwo-Track-MAC validation suite running...\n"; oss << "\nTwo-Track-MAC validation suite running...\n";
TTMAC mac(key, sizeof(key)); TTMAC mac(key, sizeof(key));
for (unsigned int k = 0; k<COUNTOF(TestVals); k++) for (unsigned int k = 0; k<COUNTOF(TestVals); k++)
@ -556,12 +553,13 @@ bool ValidateTTMAC()
fail = !!memcmp(digest, output[k], TTMAC::DIGESTSIZE) fail = !!memcmp(digest, output[k], TTMAC::DIGESTSIZE)
|| !mac.VerifyDigest(output[k], (byte *)TestVals[k], strlen(TestVals[k])); || !mac.VerifyDigest(output[k], (byte *)TestVals[k], strlen(TestVals[k]));
pass = pass && !fail; pass = pass && !fail;
std::cout << (fail ? "FAILED " : "passed "); oss << (fail ? "FAILED " : "passed ");
for (int j=0;j<TTMAC::DIGESTSIZE;j++) for (int j=0;j<TTMAC::DIGESTSIZE;j++)
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)digest[j]; oss << std::setw(2) << std::setfill('0') << std::hex << (int)digest[j];
std::cout << " \"" << TestVals[k] << '\"' << std::endl; oss << " \"" << TestVals[k] << '\"' << std::endl;
} }
std::cout << oss.str();
return true; return true;
} }

View File

@ -195,46 +195,46 @@ bool ValidateBBS()
bool pass = true, fail; bool pass = true, fail;
int j; int j;
static const byte output1[] = { const byte output1[] = {
0x49,0xEA,0x2C,0xFD,0xB0,0x10,0x64,0xA0,0xBB,0xB9, 0x49,0xEA,0x2C,0xFD,0xB0,0x10,0x64,0xA0,0xBB,0xB9,
0x2A,0xF1,0x01,0xDA,0xC1,0x8A,0x94,0xF7,0xB7,0xCE}; 0x2A,0xF1,0x01,0xDA,0xC1,0x8A,0x94,0xF7,0xB7,0xCE};
static const byte output2[] = { const byte output2[] = {
0x74,0x45,0x48,0xAE,0xAC,0xB7,0x0E,0xDF,0xAF,0xD7, 0x74,0x45,0x48,0xAE,0xAC,0xB7,0x0E,0xDF,0xAF,0xD7,
0xD5,0x0E,0x8E,0x29,0x83,0x75,0x6B,0x27,0x46,0xA1}; 0xD5,0x0E,0x8E,0x29,0x83,0x75,0x6B,0x27,0x46,0xA1};
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
byte buf[20]; byte buf[20];
std::ostringstream oss;
bbs.GenerateBlock(buf, 20); bbs.GenerateBlock(buf, 20);
fail = memcmp(output1, buf, 20) != 0; fail = memcmp(output1, buf, 20) != 0;
pass = pass && !fail; pass = pass && !fail;
std::cout << (fail ? "FAILED " : "passed "); oss << (fail ? "FAILED " : "passed ");
for (j=0;j<20;j++) for (j=0;j<20;j++)
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j]; oss << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j];
std::cout << std::endl; oss << std::endl;
bbs.Seek(10); bbs.Seek(10);
bbs.GenerateBlock(buf, 10); bbs.GenerateBlock(buf, 10);
fail = memcmp(output1+10, buf, 10) != 0; fail = memcmp(output1+10, buf, 10) != 0;
pass = pass && !fail; pass = pass && !fail;
std::cout << (fail ? "FAILED " : "passed "); oss << (fail ? "FAILED " : "passed ");
for (j=0;j<10;j++) for (j=0;j<10;j++)
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j]; oss << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j];
std::cout << std::endl; oss << std::endl;
bbs.Seek(1234567); bbs.Seek(1234567);
bbs.GenerateBlock(buf, 20); bbs.GenerateBlock(buf, 20);
fail = memcmp(output2, buf, 20) != 0; fail = memcmp(output2, buf, 20) != 0;
pass = pass && !fail; pass = pass && !fail;
std::cout << (fail ? "FAILED " : "passed "); oss << (fail ? "FAILED " : "passed ");
for (j=0;j<20;j++) for (j=0;j<20;j++)
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j]; oss << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j];
std::cout << std::endl; oss << std::endl;
std::cout << oss.str();
return pass; return pass;
} }

View File

@ -170,33 +170,6 @@ private:
BufferedTransformation &m_source; BufferedTransformation &m_source;
}; };
#if 1
// Coverity findings in benchmark and validation routines
class StreamState
{
public:
StreamState(std::ostream& out)
: m_out(out), m_prec(out.precision()), m_width(out.width()), m_fmt(out.flags()), m_fill(out.fill())
{
}
~StreamState()
{
m_out.fill(m_fill);
m_out.flags(m_fmt);
m_out.width(m_width);
m_out.precision(m_prec);
}
private:
std::ostream& m_out;
std::streamsize m_prec;
std::streamsize m_width;
std::ios_base::fmtflags m_fmt;
std::ostream::char_type m_fill;
};
#endif
// Safer functions on Windows for C&A, http://github.com/weidai11/cryptopp/issues/55 // Safer functions on Windows for C&A, http://github.com/weidai11/cryptopp/issues/55
inline std::string TimeToString(const time_t& t) inline std::string TimeToString(const time_t& t)
{ {