Merge 'master' into 'constexpr'

pull/131/merge
Jeffrey Walton 2016-09-07 01:29:18 -04:00
commit c45c854da0
1 changed files with 534 additions and 67 deletions

View File

@ -340,43 +340,74 @@ bool TestSecBlock()
{ {
cout << "\nTesting SecBlock...\n\n"; cout << "\nTesting SecBlock...\n\n";
bool result = true, temp = true; bool pass1=true, pass2=true, pass3=true, pass4=true, pass5=true, pass6=true, pass7=true, temp=false;
//********** Zeroized block **********// //********** Zeroized block **********//
// NULL ptr with a size means to create a new SecBloc with all elements zero'd {
SecByteBlock z1(NULL, 256); // NULL ptr with a size means to create a new SecBlock with all elements zero'd
temp = true; SecByteBlock z1(NULL, 256);
temp = true;
for (size_t i = 0; i < z1.size(); i++) for (size_t i = 0; i < z1.size(); i++)
temp &= (z1[i] == 0); temp &= (z1[i] == 0);
result &= temp; pass1 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
cout << "passed:"; cout << "passed:";
cout << " Zeroized byte array" << endl; cout << " Zeroized byte array" << endl;
SecBlock<word32> z2(NULL, 256); SecBlock<word32> z2(NULL, 256);
temp = true; temp = true;
for (size_t i = 0; i < z2.size(); i++) for (size_t i = 0; i < z2.size(); i++)
temp &= (z2[i] == 0); temp &= (z2[i] == 0);
result &= temp; pass1 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
cout << "passed:"; cout << "passed:";
cout << " Zeroized word32 array" << endl; cout << " Zeroized word32 array" << endl;
SecBlock<word64> z3(NULL, 256);
temp = true;
for (size_t i = 0; i < z3.size(); i++)
temp &= (z3[i] == 0);
pass1 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Zeroized word64 array" << endl;
#if defined(CRYPTOPP_WORD128_AVAILABLE)
SecBlock<word128> z4(NULL, 256);
temp = true;
for (size_t i = 0; i < z4.size(); i++)
temp &= (z4[i] == 0);
pass1 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Zeroized word128 array" << endl;
#endif
}
//********** Assign **********// //********** Assign **********//
try try
{ {
temp = true;
SecByteBlock a, b; SecByteBlock a, b;
temp = true;
a.Assign((const byte*)"a", 1); a.Assign((const byte*)"a", 1);
b.Assign((const byte*)"b", 1); b.Assign((const byte*)"b", 1);
@ -398,7 +429,7 @@ bool TestSecBlock()
temp = false; temp = false;
} }
result &= temp; pass2 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
@ -407,10 +438,10 @@ bool TestSecBlock()
try try
{ {
temp = true;
SecBlock<word32> a, b; SecBlock<word32> a, b;
word32 one[1] = {1}, two[1] = {2}; temp = true;
word32 one[1] = {1}, two[1] = {2};
a.Assign(one, 1); a.Assign(one, 1);
b.Assign(two, 1); b.Assign(two, 1);
@ -420,7 +451,6 @@ bool TestSecBlock()
temp &= (b[0] == 2); temp &= (b[0] == 2);
word32 three[2] = {1,2}, four[2] = {3,4}; word32 three[2] = {1,2}, four[2] = {3,4};
a.Assign(three, 2); a.Assign(three, 2);
b.Assign(four, 2); b.Assign(four, 2);
@ -434,19 +464,92 @@ bool TestSecBlock()
temp = false; temp = false;
} }
result &= temp; pass2 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
cout << "passed:"; cout << "passed:";
cout << " Assign word32" << endl; cout << " Assign word32" << endl;
try
{
SecBlock<word64> a, b;
temp = true;
word64 one[1] = {1}, two[1] = {2};
a.Assign(one, 1);
b.Assign(two, 1);
temp &= (a.SizeInBytes() == 8);
temp &= (b.SizeInBytes() == 8);
temp &= (a[0] == 1);
temp &= (b[0] == 2);
word64 three[2] = {1,2}, four[2] = {3,4};
a.Assign(three, 2);
b.Assign(four, 2);
temp &= (a.SizeInBytes() == 16);
temp &= (b.SizeInBytes() == 16);
temp &= (a[0] == 1 && a[1] == 2);
temp &= (b[0] == 3 && b[1] == 4);
}
catch(const Exception& /*ex*/)
{
temp = false;
}
pass2 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Assign word64" << endl;
#if defined(CRYPTOPP_WORD128_AVAILABLE)
try
{
SecBlock<word128> a, b;
temp = true;
word128 one[1] = {1}, two[1] = {2};
a.Assign(one, 1);
b.Assign(two, 1);
temp &= (a.SizeInBytes() == 16);
temp &= (b.SizeInBytes() == 16);
temp &= (a[0] == 1);
temp &= (b[0] == 2);
word128 three[2] = {1,2}, four[2] = {3,4};
a.Assign(three, 2);
b.Assign(four, 2);
temp &= (a.SizeInBytes() == 32);
temp &= (b.SizeInBytes() == 32);
temp &= (a[0] == 1 && a[1] == 2);
temp &= (b[0] == 3 && b[1] == 4);
}
catch(const Exception& /*ex*/)
{
temp = false;
}
pass2 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Assign word128" << endl;
#endif
//********** Append **********// //********** Append **********//
try try
{ {
temp = true;
SecByteBlock a, b; SecByteBlock a, b;
temp = true;
a.Assign((const byte*)"a", 1); a.Assign((const byte*)"a", 1);
b.Assign((const byte*)"b", 1); b.Assign((const byte*)"b", 1);
@ -478,7 +581,7 @@ bool TestSecBlock()
temp = false; temp = false;
} }
result &= temp; pass3 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
@ -487,10 +590,10 @@ bool TestSecBlock()
try try
{ {
temp = true;
SecBlock<word32> a, b; SecBlock<word32> a, b;
word32 one[1] = {1}, two[1] = {2}; temp = true;
const word32 one[1] = {1}, two[1] = {2};
a.Assign(one, 1); a.Assign(one, 1);
b.Assign(two, 1); b.Assign(two, 1);
@ -498,8 +601,7 @@ bool TestSecBlock()
temp &= (a.SizeInBytes() == 8); temp &= (a.SizeInBytes() == 8);
temp &= (a[0] == 1 && a[1] == 2); temp &= (a[0] == 1 && a[1] == 2);
word32 three[2] = {1,2}, four[2] = {3,4}; const word32 three[2] = {1,2}, four[2] = {3,4};
a.Assign(three, 2); a.Assign(three, 2);
b.Assign(four, 2); b.Assign(four, 2);
@ -524,19 +626,113 @@ bool TestSecBlock()
temp = false; temp = false;
} }
result &= temp; pass3 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
cout << "passed:"; cout << "passed:";
cout << " Append word32" << endl; cout << " Append word32" << endl;
//********** Concatenate **********//
try try
{ {
SecBlock<word64> a, b;
temp = true; temp = true;
const word64 one[1] = {1}, two[1] = {2};
a.Assign(one, 1);
b.Assign(two, 1);
a += b;
temp &= (a.SizeInBytes() == 16);
temp &= (a[0] == 1 && a[1] == 2);
const word64 three[2] = {1,2}, four[2] = {3,4};
a.Assign(three, 2);
b.Assign(four, 2);
a += b;
temp &= (a.SizeInBytes() == 32);
temp &= (a[0] == 1 && a[1] == 2 && a[2] == 3 && a[3] == 4);
a.Assign(one, 1);
a += a;
temp &= (a.SizeInBytes() == 16);
temp &= (a[0] == 1 && a[1] == 1);
a.Assign(three, 2);
a += a;
temp &= (a.SizeInBytes() == 32);
temp &= (a[0] == 1 && a[1] == 2 && a[2] == 1 && a[3] == 2);
}
catch(const Exception& /*ex*/)
{
temp = false;
}
pass3 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Append word64" << endl;
#if defined(CRYPTOPP_WORD128_AVAILABLE)
try
{
SecBlock<word128> a, b;
temp = true;
const word128 one[1] = {1}, two[1] = {2};
a.Assign(one, 1);
b.Assign(two, 1);
a += b;
temp &= (a.SizeInBytes() == 32);
temp &= (a[0] == 1 && a[1] == 2);
const word128 three[2] = {1,2}, four[2] = {3,4};
a.Assign(three, 2);
b.Assign(four, 2);
a += b;
temp &= (a.SizeInBytes() == 64);
temp &= (a[0] == 1 && a[1] == 2 && a[2] == 3 && a[3] == 4);
a.Assign(one, 1);
a += a;
temp &= (a.SizeInBytes() == 32);
temp &= (a[0] == 1 && a[1] == 1);
a.Assign(three, 2);
a += a;
temp &= (a.SizeInBytes() == 64);
temp &= (a[0] == 1 && a[1] == 2 && a[2] == 1 && a[3] == 2);
}
catch(const Exception& /*ex*/)
{
temp = false;
}
pass3 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Append word128" << endl;
#endif
//********** Concatenate **********//
// byte
try
{
SecByteBlock a, b, c; SecByteBlock a, b, c;
temp = true;
a.Assign((const byte*)"a", 1); a.Assign((const byte*)"a", 1);
b.Assign((const byte*)"b", 1); b.Assign((const byte*)"b", 1);
@ -560,19 +756,20 @@ bool TestSecBlock()
temp = false; temp = false;
} }
result &= temp; pass4 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
cout << "passed:"; cout << "passed:";
cout << " Concatenate byte" << endl; cout << " Concatenate byte" << endl;
// word32
try try
{ {
temp = true;
SecBlock<word32> a, b, c; SecBlock<word32> a, b, c;
word32 one[1] = {1}, two[1] = {2}; temp = true;
const word32 one[1] = {1}, two[1] = {2};
a.Assign(one, 1); a.Assign(one, 1);
b.Assign(two, 1); b.Assign(two, 1);
@ -582,8 +779,7 @@ bool TestSecBlock()
temp &= (c.SizeInBytes() == 8); temp &= (c.SizeInBytes() == 8);
temp &= (c[0] == 1 && c[1] == 2); temp &= (c[0] == 1 && c[1] == 2);
word32 three[2] = {1,2}, four[2] = {3,4}; const word32 three[2] = {1,2}, four[2] = {3,4};
a.Assign(three, 2); a.Assign(three, 2);
b.Assign(four, 2); b.Assign(four, 2);
@ -598,15 +794,93 @@ bool TestSecBlock()
temp = false; temp = false;
} }
result &= temp; pass4 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
cout << "passed:"; cout << "passed:";
cout << " Concatenate word32" << endl; cout << " Concatenate word32" << endl;
// word64
try
{
SecBlock<word64> a, b, c;
temp = true;
const word64 one[1] = {1}, two[1] = {2};
a.Assign(one, 1);
b.Assign(two, 1);
c = a + b;
temp &= (a[0] == 1);
temp &= (b[0] == 2);
temp &= (c.SizeInBytes() == 16);
temp &= (c[0] == 1 && c[1] == 2);
const word64 three[2] = {1,2}, four[2] = {3,4};
a.Assign(three, 2);
b.Assign(four, 2);
c = a + b;
temp &= (a[0] == 1 && a[1] == 2);
temp &= (b[0] == 3 && b[1] == 4);
temp &= (c.SizeInBytes() == 32);
temp &= (c[0] == 1 && c[1] == 2 && c[2] == 3 && c[3] == 4);
}
catch(const Exception& /*ex*/)
{
temp = false;
}
pass4 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Concatenate word64" << endl;
#if defined(CRYPTOPP_WORD128_AVAILABLE)
try
{
SecBlock<word128> a, b, c;
temp = true;
const word128 one[1] = {1}, two[1] = {2};
a.Assign(one, 1);
b.Assign(two, 1);
c = a + b;
temp &= (a[0] == 1);
temp &= (b[0] == 2);
temp &= (c.SizeInBytes() == 32);
temp &= (c[0] == 1 && c[1] == 2);
const word128 three[2] = {1,2}, four[2] = {3,4};
a.Assign(three, 2);
b.Assign(four, 2);
c = a + b;
temp &= (a[0] == 1 && a[1] == 2);
temp &= (b[0] == 3 && b[1] == 4);
temp &= (c.SizeInBytes() == 64);
temp &= (c[0] == 1 && c[1] == 2 && c[2] == 3 && c[3] == 4);
}
catch(const Exception& /*ex*/)
{
temp = false;
}
pass4 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Concatenate word128" << endl;
#endif
//********** Equality **********// //********** Equality **********//
// byte
try try
{ {
static const byte str1[] = "abcdefghijklmnopqrstuvwxyz"; static const byte str1[] = "abcdefghijklmnopqrstuvwxyz";
@ -637,13 +911,14 @@ bool TestSecBlock()
temp = false; temp = false;
} }
result &= temp; pass5 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
cout << "passed:"; cout << "passed:";
cout << " Equality byte" << endl; cout << " Equality byte" << endl;
// word32
try try
{ {
static const word32 str1[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; static const word32 str1[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
@ -674,18 +949,97 @@ bool TestSecBlock()
temp = false; temp = false;
} }
result &= temp; pass5 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
cout << "passed:"; cout << "passed:";
cout << " Equality word32" << endl; cout << " Equality word32" << endl;
//********** Size/Overflow **********// // word64
try
{
static const word64 str1[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
static const word64 str2[] = {97,89,83,79,73,71,67,61,59,53,47,43,41,37,31,29,23,19,17,13,11,7,5,3,2};
static const word64 str3[] = {0,1,2,3,4,5,6,7,8,9};
temp = true;
SecBlock<word64> a,b;
a.Assign(str1, COUNTOF(str1));
b.Assign(str1, COUNTOF(str1));
temp &= (a.operator==(b));
a.Assign(str3, COUNTOF(str3));
b.Assign(str3, COUNTOF(str3));
temp &= (a == b);
a.Assign(str1, COUNTOF(str1));
b.Assign(str2, COUNTOF(str2));
temp &= (a.operator!=(b));
a.Assign(str1, COUNTOF(str1));
b.Assign(str3, COUNTOF(str3));
temp &= (a != b);
}
catch(const Exception& /*ex*/)
{
temp = false;
}
pass5 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Equality word64" << endl;
#if defined(CRYPTOPP_WORD128_AVAILABLE)
// word128
try
{
static const word128 str1[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
static const word128 str2[] = {97,89,83,79,73,71,67,61,59,53,47,43,41,37,31,29,23,19,17,13,11,7,5,3,2};
static const word128 str3[] = {0,1,2,3,4,5,6,7,8,9};
temp = true;
SecBlock<word128> a,b;
a.Assign(str1, COUNTOF(str1));
b.Assign(str1, COUNTOF(str1));
temp &= (a.operator==(b));
a.Assign(str3, COUNTOF(str3));
b.Assign(str3, COUNTOF(str3));
temp &= (a == b);
a.Assign(str1, COUNTOF(str1));
b.Assign(str2, COUNTOF(str2));
temp &= (a.operator!=(b));
a.Assign(str1, COUNTOF(str1));
b.Assign(str3, COUNTOF(str3));
temp &= (a != b);
}
catch(const Exception& /*ex*/)
{
temp = false;
}
pass5 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Equality word128" << endl;
#endif
//********** Allocator Size/Overflow **********//
try try
{ {
temp = false; temp = false;
AllocatorBase<word32> A; AllocatorBase<word32> A;
const size_t max = A.max_size(); const size_t max = A.max_size();
SecBlock<word32> t(max+1); SecBlock<word32> t(max+1);
@ -699,7 +1053,7 @@ bool TestSecBlock()
temp = true; temp = true;
} }
result &= temp; pass6 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
@ -709,6 +1063,7 @@ bool TestSecBlock()
try try
{ {
temp = false; temp = false;
AllocatorBase<word64> A; AllocatorBase<word64> A;
const size_t max = A.max_size(); const size_t max = A.max_size();
SecBlock<word64> t(max+1); SecBlock<word64> t(max+1);
@ -722,15 +1077,42 @@ bool TestSecBlock()
temp = true; temp = true;
} }
result &= temp; pass6 &= temp;
if (!temp) if (!temp)
cout << "FAILED:"; cout << "FAILED:";
else else
cout << "passed:"; cout << "passed:";
cout << " Overflow word64" << endl; cout << " Overflow word64" << endl;
#if defined(CRYPTOPP_WORD128_AVAILABLE)
try
{
temp = false;
AllocatorBase<word128> A;
const size_t max = A.max_size();
SecBlock<word128> t(max+1);
}
catch(const Exception& /*ex*/)
{
temp = true;
}
catch(const std::exception& /*ex*/)
{
temp = true;
}
pass6 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " Overflow word128" << endl;
#endif
//********** FixedSizeAllocatorWithCleanup and Grow **********// //********** FixedSizeAllocatorWithCleanup and Grow **********//
// byte
try try
{ {
static const unsigned int SIZE = 8; static const unsigned int SIZE = 8;
@ -750,13 +1132,6 @@ bool TestSecBlock()
temp &= (block.size() == SIZE*4); temp &= (block.size() == SIZE*4);
for (size_t i = 0; i < block.size(); i++) for (size_t i = 0; i < block.size(); i++)
temp &= (block[i] == 0); temp &= (block[i] == 0);
result &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " FixedSizeAllocator and Grow with byte" << endl;
} }
catch(const Exception& /*ex*/) catch(const Exception& /*ex*/)
{ {
@ -767,6 +1142,14 @@ bool TestSecBlock()
temp = false; temp = false;
} }
pass7 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " FixedSizeAllocator Grow with byte" << endl;
// word32
try try
{ {
static const unsigned int SIZE = 8; static const unsigned int SIZE = 8;
@ -787,13 +1170,6 @@ bool TestSecBlock()
temp &= (block.size() == SIZE*4); temp &= (block.size() == SIZE*4);
for (size_t i = 0; i < block.size(); i++) for (size_t i = 0; i < block.size(); i++)
temp &= (block[i] == 0); temp &= (block[i] == 0);
result &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " FixedSizeAllocator and Grow with word32" << endl;
} }
catch(const Exception& /*ex*/) catch(const Exception& /*ex*/)
{ {
@ -804,7 +1180,92 @@ bool TestSecBlock()
temp = false; temp = false;
} }
return result; pass7 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " FixedSizeAllocator Grow with word32" << endl;
// word64
try
{
static const unsigned int SIZE = 8;
SecBlockWithHint<word64, SIZE> block(SIZE);
memset(block, 0xaa, block.SizeInBytes());
temp = true;
block.CleanGrow(SIZE*2);
temp &= (block.size() == SIZE*2);
for (size_t i = 0; i < block.size()/2; i++)
temp &= (block[i] == 0xaaaaaaaaaaaaaaaa);
for (size_t i = block.size()/2; i < block.size(); i++)
temp &= (block[i] == 0);
block.CleanNew(SIZE*4);
temp &= (block.size() == SIZE*4);
for (size_t i = 0; i < block.size(); i++)
temp &= (block[i] == 0);
}
catch(const Exception& /*ex*/)
{
temp = false;
}
catch(const std::exception& /*ex*/)
{
temp = false;
}
pass7 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " FixedSizeAllocator Grow with word64" << endl;
#if defined(CRYPTOPP_WORD128_AVAILABLE)
// word128
try
{
static const unsigned int SIZE = 8;
SecBlockWithHint<word128, SIZE> block(SIZE);
memset(block, 0xaa, block.SizeInBytes());
temp = true;
block.CleanGrow(SIZE*2);
temp &= (block.size() == SIZE*2);
for (size_t i = 0; i < block.size()/2; i++)
temp &= (block[i] == (((word128)W64LIT(0xaaaaaaaaaaaaaaaa) << 64U) | W64LIT(0xaaaaaaaaaaaaaaaa)));
for (size_t i = block.size()/2; i < block.size(); i++)
temp &= (block[i] == 0);
block.CleanNew(SIZE*4);
temp &= (block.size() == SIZE*4);
for (size_t i = 0; i < block.size(); i++)
temp &= (block[i] == 0);
}
catch(const Exception& /*ex*/)
{
temp = false;
}
catch(const std::exception& /*ex*/)
{
temp = false;
}
pass7 &= temp;
if (!temp)
cout << "FAILED:";
else
cout << "passed:";
cout << " FixedSizeAllocator Grow with word128" << endl;
#endif
return pass1 && pass2 && pass3 && pass4 && pass5 && pass6 && pass7;
} }
#endif #endif
@ -812,6 +1273,7 @@ bool TestSecBlock()
bool TestHuffmanCodes() bool TestHuffmanCodes()
{ {
cout << "\nTesting Huffman codes...\n\n"; cout << "\nTesting Huffman codes...\n\n";
bool pass=true;
static const size_t nCodes = 30; static const size_t nCodes = 30;
const unsigned int codeCounts[nCodes] = { const unsigned int codeCounts[nCodes] = {
@ -830,13 +1292,18 @@ bool TestHuffmanCodes()
{ {
HuffmanEncoder::GenerateCodeLengths(codeBits, maxCodeBits, codeCounts, nCodes); HuffmanEncoder::GenerateCodeLengths(codeBits, maxCodeBits, codeCounts, nCodes);
} }
catch(const Exception& ex) catch(const Exception& /*ex*/)
{ {
CRYPTOPP_UNUSED(ex); pass=false;
return false;
} }
return true; if (!pass)
cout << "FAILED:";
else
cout << "passed:";
cout << " GenerateCodeLengths" << endl;
return pass;
} }
#endif #endif