diff --git a/validat1.cpp b/validat1.cpp index fbe097ce..afda0d94 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -340,43 +340,74 @@ bool TestSecBlock() { 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 **********// - // NULL ptr with a size means to create a new SecBloc with all elements zero'd - SecByteBlock z1(NULL, 256); - temp = true; + { + // NULL ptr with a size means to create a new SecBlock with all elements zero'd + SecByteBlock z1(NULL, 256); + temp = true; - for (size_t i = 0; i < z1.size(); i++) - temp &= (z1[i] == 0); + for (size_t i = 0; i < z1.size(); i++) + temp &= (z1[i] == 0); - result &= temp; - if (!temp) - cout << "FAILED:"; - else - cout << "passed:"; - cout << " Zeroized byte array" << endl; + pass1 &= temp; + if (!temp) + cout << "FAILED:"; + else + cout << "passed:"; + cout << " Zeroized byte array" << endl; - SecBlock z2(NULL, 256); - temp = true; + SecBlock z2(NULL, 256); + temp = true; - for (size_t i = 0; i < z2.size(); i++) - temp &= (z2[i] == 0); + for (size_t i = 0; i < z2.size(); i++) + temp &= (z2[i] == 0); - result &= temp; - if (!temp) - cout << "FAILED:"; - else - cout << "passed:"; - cout << " Zeroized word32 array" << endl; + pass1 &= temp; + if (!temp) + cout << "FAILED:"; + else + cout << "passed:"; + cout << " Zeroized word32 array" << endl; + + SecBlock 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 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 **********// try { - temp = true; SecByteBlock a, b; + temp = true; + a.Assign((const byte*)"a", 1); b.Assign((const byte*)"b", 1); @@ -398,7 +429,7 @@ bool TestSecBlock() temp = false; } - result &= temp; + pass2 &= temp; if (!temp) cout << "FAILED:"; else @@ -407,10 +438,10 @@ bool TestSecBlock() try { - temp = true; SecBlock a, b; - word32 one[1] = {1}, two[1] = {2}; + temp = true; + word32 one[1] = {1}, two[1] = {2}; a.Assign(one, 1); b.Assign(two, 1); @@ -420,7 +451,6 @@ bool TestSecBlock() temp &= (b[0] == 2); word32 three[2] = {1,2}, four[2] = {3,4}; - a.Assign(three, 2); b.Assign(four, 2); @@ -434,19 +464,92 @@ bool TestSecBlock() temp = false; } - result &= temp; + pass2 &= temp; if (!temp) cout << "FAILED:"; else cout << "passed:"; cout << " Assign word32" << endl; + try + { + SecBlock 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 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 **********// try { - temp = true; SecByteBlock a, b; + temp = true; + a.Assign((const byte*)"a", 1); b.Assign((const byte*)"b", 1); @@ -478,7 +581,7 @@ bool TestSecBlock() temp = false; } - result &= temp; + pass3 &= temp; if (!temp) cout << "FAILED:"; else @@ -487,10 +590,10 @@ bool TestSecBlock() try { - temp = true; SecBlock a, b; - word32 one[1] = {1}, two[1] = {2}; + temp = true; + const word32 one[1] = {1}, two[1] = {2}; a.Assign(one, 1); b.Assign(two, 1); @@ -498,8 +601,7 @@ bool TestSecBlock() temp &= (a.SizeInBytes() == 8); 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); b.Assign(four, 2); @@ -524,19 +626,113 @@ bool TestSecBlock() temp = false; } - result &= temp; + pass3 &= temp; if (!temp) cout << "FAILED:"; else cout << "passed:"; cout << " Append word32" << endl; - //********** Concatenate **********// - try { + SecBlock a, b; 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 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; + temp = true; + a.Assign((const byte*)"a", 1); b.Assign((const byte*)"b", 1); @@ -560,19 +756,20 @@ bool TestSecBlock() temp = false; } - result &= temp; + pass4 &= temp; if (!temp) cout << "FAILED:"; else cout << "passed:"; cout << " Concatenate byte" << endl; + // word32 try { - temp = true; SecBlock a, b, c; - word32 one[1] = {1}, two[1] = {2}; + temp = true; + const word32 one[1] = {1}, two[1] = {2}; a.Assign(one, 1); b.Assign(two, 1); @@ -582,8 +779,7 @@ bool TestSecBlock() temp &= (c.SizeInBytes() == 8); 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); b.Assign(four, 2); @@ -598,15 +794,93 @@ bool TestSecBlock() temp = false; } - result &= temp; + pass4 &= temp; if (!temp) cout << "FAILED:"; else cout << "passed:"; cout << " Concatenate word32" << endl; + // word64 + try + { + SecBlock 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 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 **********// + // byte try { static const byte str1[] = "abcdefghijklmnopqrstuvwxyz"; @@ -637,13 +911,14 @@ bool TestSecBlock() temp = false; } - result &= temp; + pass5 &= temp; if (!temp) cout << "FAILED:"; else cout << "passed:"; cout << " Equality byte" << endl; + // word32 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}; @@ -674,18 +949,97 @@ bool TestSecBlock() temp = false; } - result &= temp; + pass5 &= temp; if (!temp) cout << "FAILED:"; else cout << "passed:"; 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 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 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 { temp = false; + AllocatorBase A; const size_t max = A.max_size(); SecBlock t(max+1); @@ -699,7 +1053,7 @@ bool TestSecBlock() temp = true; } - result &= temp; + pass6 &= temp; if (!temp) cout << "FAILED:"; else @@ -709,6 +1063,7 @@ bool TestSecBlock() try { temp = false; + AllocatorBase A; const size_t max = A.max_size(); SecBlock t(max+1); @@ -722,15 +1077,42 @@ bool TestSecBlock() temp = true; } - result &= temp; + pass6 &= temp; if (!temp) cout << "FAILED:"; else cout << "passed:"; cout << " Overflow word64" << endl; +#if defined(CRYPTOPP_WORD128_AVAILABLE) + try + { + temp = false; + + AllocatorBase A; + const size_t max = A.max_size(); + SecBlock 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 **********// + // byte try { static const unsigned int SIZE = 8; @@ -750,13 +1132,6 @@ bool TestSecBlock() temp &= (block.size() == SIZE*4); for (size_t i = 0; i < block.size(); i++) temp &= (block[i] == 0); - - result &= temp; - if (!temp) - cout << "FAILED:"; - else - cout << "passed:"; - cout << " FixedSizeAllocator and Grow with byte" << endl; } catch(const Exception& /*ex*/) { @@ -767,6 +1142,14 @@ bool TestSecBlock() temp = false; } + pass7 &= temp; + if (!temp) + cout << "FAILED:"; + else + cout << "passed:"; + cout << " FixedSizeAllocator Grow with byte" << endl; + + // word32 try { static const unsigned int SIZE = 8; @@ -787,13 +1170,6 @@ bool TestSecBlock() temp &= (block.size() == SIZE*4); for (size_t i = 0; i < block.size(); i++) temp &= (block[i] == 0); - - result &= temp; - if (!temp) - cout << "FAILED:"; - else - cout << "passed:"; - cout << " FixedSizeAllocator and Grow with word32" << endl; } catch(const Exception& /*ex*/) { @@ -804,7 +1180,92 @@ bool TestSecBlock() 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 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 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 @@ -812,6 +1273,7 @@ bool TestSecBlock() bool TestHuffmanCodes() { cout << "\nTesting Huffman codes...\n\n"; + bool pass=true; static const size_t nCodes = 30; const unsigned int codeCounts[nCodes] = { @@ -830,13 +1292,18 @@ bool TestHuffmanCodes() { HuffmanEncoder::GenerateCodeLengths(codeBits, maxCodeBits, codeCounts, nCodes); } - catch(const Exception& ex) + catch(const Exception& /*ex*/) { - CRYPTOPP_UNUSED(ex); - return false; + pass=false; } - return true; + if (!pass) + cout << "FAILED:"; + else + cout << "passed:"; + cout << " GenerateCodeLengths" << endl; + + return pass; } #endif