Cleanup selft tests

This commit is contained in:
Jeffrey Walton 2017-08-05 05:32:43 -04:00
parent 326700f6ec
commit bf35d58ad7
No known key found for this signature in database
GPG Key ID: B36AB348921B1838

View File

@ -210,6 +210,24 @@ bool ValidateMD5()
}
#if defined(CRYPTOPP_EXTENDED_VALIDATION)
struct StaticTransformTestTuple32
{
void (*pfn32)(word32*, const word32*);
const char* name;
const size_t size;
const byte *data;
const byte *digest;
};
struct StaticTransformTestTuple64
{
void (*pfn64)(word64*, const word64*);
const char* name;
const size_t size;
const byte *data;
const byte *digest;
};
bool TestSHAStaticTransform()
{
std::cout << "\nSHA static transform suite running...\n\n";
@ -246,206 +264,107 @@ bool TestSHAStaticTransform()
CRYPTOPP_ALIGN_DATA(16)
byte result[MAX_PARAM];
// SHA1, Message 1
{
std::memcpy(result, SM1, SHA1::BLOCKSIZE);
SHA1::Transform(reinterpret_cast<word32*>(result), reinterpret_cast<const word32*>(SM1));
const byte expected[SHA1::DIGESTSIZE] = {
0xED,0x47,0x15,0x9E,0xC2,0x91,0xEC,0x57,
0xC8,0x8B,0xFA,0x30,0x54,0x5A,0x78,0xC7,
0xE3,0xA5,0xEF,0xA7
};
const byte SHA1M1[] = {
0xED,0x47,0x15,0x9E,0xC2,0x91,0xEC,0x57, 0xC8,0x8B,0xFA,0x30,0x54,0x5A,0x78,0xC7,
0xE3,0xA5,0xEF,0xA7
};
const byte SHA1M2[SHA1::DIGESTSIZE] = {
0x9B,0x15,0x48,0x3F,0xFA,0x51,0xCE,0x2D, 0x0A,0x93,0x1E,0x8A,0x68,0x3B,0x08,0x02,
0xE8,0xD6,0x70,0xEF
};
const byte SHA1M3[SHA1::DIGESTSIZE] = {
0x6D,0x65,0x87,0x41,0xB5,0x50,0x66,0x24, 0xAB,0x9B,0x08,0xD2,0xED,0x6E,0x32,0xAC,
0xDC,0xC5,0x17,0x67
};
const byte SHA256M1[SHA256::DIGESTSIZE] = {
0x14,0x16,0xA5,0x7C,0xA8,0x3B,0x5C,0x42, 0x2F,0xDD,0x54,0xCE,0xE7,0x0A,0x02,0xC2,
0xD1,0x74,0xE5,0xB6,0x0F,0x6D,0x13,0x98, 0xCC,0x26,0x7E,0xAE,0xA6,0xE7,0x0B,0xBF
};
const byte SHA256M2[SHA256::DIGESTSIZE] = {
0x7A,0x12,0x98,0x05,0x6E,0x70,0x11,0xAF, 0xFF,0x1F,0x40,0x77,0x49,0xC5,0x30,0xD7,
0x67,0xC8,0x84,0x0B,0xA3,0x6C,0x8C,0xA9, 0xF9,0xC0,0xF3,0xD7,0x79,0x8B,0xA7,0x06
};
const byte SHA256M3[SHA256::DIGESTSIZE] = {
0xCB,0x8E,0x2A,0xD0,0x39,0x89,0x15,0xE9, 0xFF,0x65,0xDA,0xA6,0x84,0x23,0x96,0xB9,
0xCD,0xB5,0x51,0xC1,0x1D,0x21,0xF1,0x9E, 0x67,0x8A,0xB0,0xD8,0x34,0x1D,0x06,0x11
};
const byte SHA512M1[SHA512::DIGESTSIZE] = {
0x30,0xF8,0xEB,0x38,0xA8,0x5D,0x3A,0xAD, 0xDB,0xBE,0xFC,0x85,0xFF,0x42,0x71,0x5D,
0x99,0xCA,0xF7,0xD6,0x86,0xBF,0xA8,0x7F, 0xBF,0x0B,0xD7,0x39,0xB0,0x15,0x6B,0x1C,
0x18,0x35,0x92,0x89,0x52,0x51,0xF6,0x29, 0x3A,0x8F,0x2E,0x5F,0x1D,0x10,0xD7,0x6F,
0x50,0x14,0x53,0x76,0x24,0x8D,0x35,0x1C, 0x3C,0xDC,0x56,0xD7,0x99,0x0D,0x8C,0x56
};
const byte SHA512M2[SHA512::DIGESTSIZE] = {
0xEE,0x51,0xE9,0x96,0x16,0x12,0x0F,0x9B, 0x68,0xE7,0x41,0xB7,0x67,0xB4,0xD0,0x41,
0x84,0x28,0xB7,0xB3,0xEB,0x41,0x4F,0x56, 0xC1,0x5B,0xCB,0xCA,0x0F,0xB4,0x05,0xF8,
0x00,0xA9,0x02,0xCF,0xFF,0x9C,0x37,0x3C, 0x54,0xC5,0x9A,0x80,0x7F,0xA3,0x17,0x9B,
0xAB,0xB6,0x83,0x1C,0x2E,0xFD,0xC9,0x79, 0x7A,0x2C,0xC0,0x52,0xE2,0xD9,0x48,0xE9
};
const byte SHA512M3[SHA512::DIGESTSIZE] = {
0x2F,0xA6,0xE7,0x5B,0xB4,0x3D,0xC5,0x22, 0x7B,0x29,0x3B,0xBC,0xDB,0xBC,0x00,0xD3,
0x4F,0x28,0xF9,0x33,0xB4,0xA8,0x8D,0x3B, 0x96,0xF0,0x91,0xC6,0xE6,0x75,0xD0,0x22,
0x8D,0x3F,0x26,0x85,0xCF,0x4E,0xC8,0x36, 0xE2,0xF4,0xC0,0xEF,0xD9,0x83,0x8D,0x10,
0x73,0x60,0x49,0x40,0xDB,0xA2,0x20,0x1F, 0x88,0x4A,0xE8,0xBB,0x85,0x92,0xF7,0xD5
};
fail = !!std::memcmp(result, expected, SHA1::DIGESTSIZE);
StaticTransformTestTuple32 tuples32[] = {
{SHA1::Transform, "SHA1::Transform", SHA1::DIGESTSIZE, SM1, SHA1M1},
{SHA1::Transform, "SHA1::Transform", SHA1::DIGESTSIZE, SM2, SHA1M2},
{SHA1::Transform, "SHA1::Transform", SHA1::DIGESTSIZE, SM3, SHA1M3},
{SHA256::Transform, "SHA256::Transform", SHA256::DIGESTSIZE, SM1, SHA256M1},
{SHA256::Transform, "SHA256::Transform", SHA256::DIGESTSIZE, SM2, SHA256M2},
{SHA256::Transform, "SHA256::Transform", SHA256::DIGESTSIZE, SM3, SHA256M3},
};
StaticTransformTestTuple64 tuples64[] = {
{SHA512::Transform, "SHA512::Transform", SHA512::DIGESTSIZE, SM1, SHA512M1},
{SHA512::Transform, "SHA512::Transform", SHA512::DIGESTSIZE, SM2, SHA512M2},
{SHA512::Transform, "SHA512::Transform", SHA512::DIGESTSIZE, SM3, SHA512M3},
};
for (unsigned int i=0; i<COUNTOF(tuples32); ++i)
{
std::memcpy(result, tuples32[i].data, tuples32[i].size);
tuples32[i].pfn32(reinterpret_cast<word32*>(result), reinterpret_cast<const word32*>(tuples32[i].data));
fail = !!std::memcmp(result, tuples32[i].digest, tuples32[i].size);
pass = !fail && pass;
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "SHA1::Transform, message 1\n";
std::cout << tuples32[i].name << ", message "<< (i%3)+1 << "\n";
if (fail)
{
std::cout << "Calculated: ";
HexEncoder encoder(new FileSink(std::cout));
encoder.Put(result, tuples32[i].size);
std::cout << "\n";
}
}
// SHA1, Message 2
for (unsigned int i=0; i<COUNTOF(tuples64); ++i)
{
std::memcpy(result, SM2, SHA1::BLOCKSIZE);
SHA1::Transform(reinterpret_cast<word32*>(result), reinterpret_cast<const word32*>(SM2));
const byte expected[SHA1::DIGESTSIZE] = {
0x9B,0x15,0x48,0x3F,0xFA,0x51,0xCE,0x2D,
0x0A,0x93,0x1E,0x8A,0x68,0x3B,0x08,0x02,
0xE8,0xD6,0x70,0xEF
};
std::memcpy(result, tuples64[i].data, tuples64[i].size);
tuples64[i].pfn64(reinterpret_cast<word64*>(result), reinterpret_cast<const word64*>(tuples64[i].data));
fail = !!std::memcmp(result, expected, SHA1::DIGESTSIZE);
fail = !!std::memcmp(result, tuples64[i].digest, tuples64[i].size);
pass = !fail && pass;
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "SHA1::Transform, message 2\n";
}
std::cout << tuples64[i].name << ", message "<< (i%3)+1 << "\n";
// SHA1, Message 3
{
std::memcpy(result, SM3, SHA1::BLOCKSIZE);
SHA1::Transform(reinterpret_cast<word32*>(result), reinterpret_cast<const word32*>(SM3));
const byte expected[SHA1::DIGESTSIZE] = {
0x6D,0x65,0x87,0x41,0xB5,0x50,0x66,0x24,
0xAB,0x9B,0x08,0xD2,0xED,0x6E,0x32,0xAC,
0xDC,0xC5,0x17,0x67
};
fail = !!std::memcmp(result, expected, SHA1::DIGESTSIZE);
pass = !fail && pass;
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "SHA1::Transform, message 3\n";
}
////////////////////////////////////////////////
// SHA256, Message 1
{
std::memcpy(result, SM1, SHA256::BLOCKSIZE);
SHA256::Transform(reinterpret_cast<word32*>(result), reinterpret_cast<const word32*>(SM1));
const byte expected[SHA256::DIGESTSIZE] = {
0x14,0x16,0xA5,0x7C,0xA8,0x3B,0x5C,0x42,
0x2F,0xDD,0x54,0xCE,0xE7,0x0A,0x02,0xC2,
0xD1,0x74,0xE5,0xB6,0x0F,0x6D,0x13,0x98,
0xCC,0x26,0x7E,0xAE,0xA6,0xE7,0x0B,0xBF
};
fail = !!std::memcmp(result, expected, SHA256::DIGESTSIZE);
pass = !fail && pass;
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "SHA256::Transform, message 1\n";
}
// SHA256, Message 2
{
std::memcpy(result, SM2, SHA256::BLOCKSIZE);
SHA256::Transform(reinterpret_cast<word32*>(result), reinterpret_cast<const word32*>(SM2));
const byte expected[SHA256::DIGESTSIZE] = {
0x7A,0x12,0x98,0x05,0x6E,0x70,0x11,0xAF,
0xFF,0x1F,0x40,0x77,0x49,0xC5,0x30,0xD7,
0x67,0xC8,0x84,0x0B,0xA3,0x6C,0x8C,0xA9,
0xF9,0xC0,0xF3,0xD7,0x79,0x8B,0xA7,0x06
};
fail = !!std::memcmp(result, expected, SHA256::DIGESTSIZE);
pass = !fail && pass;
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "SHA256::Transform, message 2\n";
}
// SHA256, Message 3
{
std::memcpy(result, SM3, SHA256::BLOCKSIZE);
SHA256::Transform(reinterpret_cast<word32*>(result), reinterpret_cast<const word32*>(SM3));
const byte expected[SHA256::DIGESTSIZE] = {
0xCB,0x8E,0x2A,0xD0,0x39,0x89,0x15,0xE9,
0xFF,0x65,0xDA,0xA6,0x84,0x23,0x96,0xB9,
0xCD,0xB5,0x51,0xC1,0x1D,0x21,0xF1,0x9E,
0x67,0x8A,0xB0,0xD8,0x34,0x1D,0x06,0x11
};
fail = !!std::memcmp(result, expected, SHA256::DIGESTSIZE);
pass = !fail && pass;
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "SHA256::Transform, message 3\n";
}
////////////////////////////////////////////////
// SHA512, Message 1
{
std::memcpy(result, SM1, SHA512::BLOCKSIZE);
SHA512::Transform(reinterpret_cast<word64*>(result), reinterpret_cast<const word64*>(SM1));
const byte expected[SHA512::DIGESTSIZE] = {
0x30,0xF8,0xEB,0x38,0xA8,0x5D,0x3A,0xAD,
0xDB,0xBE,0xFC,0x85,0xFF,0x42,0x71,0x5D,
0x99,0xCA,0xF7,0xD6,0x86,0xBF,0xA8,0x7F,
0xBF,0x0B,0xD7,0x39,0xB0,0x15,0x6B,0x1C,
0x18,0x35,0x92,0x89,0x52,0x51,0xF6,0x29,
0x3A,0x8F,0x2E,0x5F,0x1D,0x10,0xD7,0x6F,
0x50,0x14,0x53,0x76,0x24,0x8D,0x35,0x1C,
0x3C,0xDC,0x56,0xD7,0x99,0x0D,0x8C,0x56
};
fail = !!std::memcmp(result, expected, SHA512::DIGESTSIZE);
pass = !fail && pass;
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "SHA512::Transform, message 1\n";
}
// SHA512, Message 2
{
std::memcpy(result, SM2, SHA512::BLOCKSIZE);
SHA512::Transform(reinterpret_cast<word64*>(result), reinterpret_cast<const word64*>(SM2));
const byte expected[SHA512::DIGESTSIZE] = {
0xEE,0x51,0xE9,0x96,0x16,0x12,0x0F,0x9B,
0x68,0xE7,0x41,0xB7,0x67,0xB4,0xD0,0x41,
0x84,0x28,0xB7,0xB3,0xEB,0x41,0x4F,0x56,
0xC1,0x5B,0xCB,0xCA,0x0F,0xB4,0x05,0xF8,
0x00,0xA9,0x02,0xCF,0xFF,0x9C,0x37,0x3C,
0x54,0xC5,0x9A,0x80,0x7F,0xA3,0x17,0x9B,
0xAB,0xB6,0x83,0x1C,0x2E,0xFD,0xC9,0x79,
0x7A,0x2C,0xC0,0x52,0xE2,0xD9,0x48,0xE9
};
fail = !!std::memcmp(result, expected, SHA512::DIGESTSIZE);
pass = !fail && pass;
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "SHA512::Transform, message 2\n";
}
// SHA512, Message 3
{
std::memcpy(result, SM3, SHA512::BLOCKSIZE);
SHA512::Transform(reinterpret_cast<word64*>(result), reinterpret_cast<const word64*>(SM3));
const byte expected[SHA512::DIGESTSIZE] = {
0x2F,0xA6,0xE7,0x5B,0xB4,0x3D,0xC5,0x22,
0x7B,0x29,0x3B,0xBC,0xDB,0xBC,0x00,0xD3,
0x4F,0x28,0xF9,0x33,0xB4,0xA8,0x8D,0x3B,
0x96,0xF0,0x91,0xC6,0xE6,0x75,0xD0,0x22,
0x8D,0x3F,0x26,0x85,0xCF,0x4E,0xC8,0x36,
0xE2,0xF4,0xC0,0xEF,0xD9,0x83,0x8D,0x10,
0x73,0x60,0x49,0x40,0xDB,0xA2,0x20,0x1F,
0x88,0x4A,0xE8,0xBB,0x85,0x92,0xF7,0xD5
};
fail = !!std::memcmp(result, expected, SHA512::DIGESTSIZE);
pass = !fail && pass;
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "SHA512::Transform, message 3\n";
if (fail)
{
std::cout << "Calculated: ";
HexEncoder encoder(new FileSink(std::cout));
encoder.Put(result, tuples64[i].size);
std::cout << "\n";
}
}
return pass;