// validat3.cpp - written and placed in the public domain by Wei Dai #include "pch.h" #include "validate.h" #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 #include "smartptr.h" #include "crc.h" #include "adler32.h" #include "md2.h" #include "md4.h" #include "md5.h" #include "sha.h" #include "tiger.h" #include "ripemd.h" #include "hmac.h" #include "hkdf.h" #include "ttmac.h" #include "integer.h" #include "pwdbased.h" #include "filters.h" #include "hex.h" #include "misc.h" #include "files.h" #include #include USING_NAMESPACE(CryptoPP) USING_NAMESPACE(std) struct HashTestTuple { HashTestTuple(const char *input, const char *output, unsigned int repeatTimes=1) : input((byte *)input), output((byte *)output), inputLen(strlen(input)), repeatTimes(repeatTimes) {} HashTestTuple(const char *input, unsigned int inputLen, const char *output, unsigned int repeatTimes) : input((byte *)input), output((byte *)output), inputLen(inputLen), repeatTimes(repeatTimes) {} const byte *input, *output; size_t inputLen; unsigned int repeatTimes; }; bool HashModuleTest(HashTransformation &md, const HashTestTuple *testSet, unsigned int testSetSize) { bool pass=true, fail; SecByteBlock digest(md.DigestSize()); for (unsigned int i=0; i XMACC_MD5; static const byte keys[2][XMACC_MD5::KEYLENGTH]={ {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb}, {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98}}; static const word32 counters[2]={0xccddeeff, 0x76543210}; static const char *TestVals[7]={ "", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "12345678901234567890123456789012345678901234567890123456789012345678901234567890"}; static const byte output[2][7][XMACC_MD5::DIGESTSIZE]={ {{0xcc,0xdd,0xef,0x00,0xfa,0x89,0x54,0x92,0x86,0x32,0xda,0x2a,0x3f,0x29,0xc5,0x52,0xa0,0x0d,0x05,0x13}, {0xcc,0xdd,0xef,0x01,0xae,0xdb,0x8b,0x7b,0x69,0x71,0xc7,0x91,0x71,0x48,0x9d,0x18,0xe7,0xdf,0x9d,0x5a}, {0xcc,0xdd,0xef,0x02,0x5e,0x01,0x2e,0x2e,0x4b,0xc3,0x83,0x62,0xc2,0xf4,0xe6,0x18,0x1c,0x44,0xaf,0xca}, {0xcc,0xdd,0xef,0x03,0x3e,0xa9,0xf1,0xe0,0x97,0x91,0xf8,0xe2,0xbe,0xe0,0xdf,0xf3,0x41,0x03,0xb3,0x5a}, {0xcc,0xdd,0xef,0x04,0x2e,0x6a,0x8d,0xb9,0x72,0xe3,0xce,0x9f,0xf4,0x28,0x45,0xe7,0xbc,0x80,0xa9,0xc7}, {0xcc,0xdd,0xef,0x05,0x1a,0xd5,0x40,0x78,0xfb,0x16,0x37,0xfc,0x7a,0x1d,0xce,0xb4,0x77,0x10,0xb2,0xa0}, {0xcc,0xdd,0xef,0x06,0x13,0x2f,0x11,0x47,0xd7,0x1b,0xb5,0x52,0x36,0x51,0x26,0xb0,0x96,0xd7,0x60,0x81}}, {{0x76,0x54,0x32,0x11,0xe9,0xcb,0x74,0x32,0x07,0x93,0xfe,0x01,0xdd,0x27,0xdb,0xde,0x6b,0x77,0xa4,0x56}, {0x76,0x54,0x32,0x12,0xcd,0x55,0x87,0x5c,0xc0,0x35,0x85,0x99,0x44,0x02,0xa5,0x0b,0x8c,0xe7,0x2c,0x68}, {0x76,0x54,0x32,0x13,0xac,0xfd,0x87,0x50,0xc3,0x8f,0xcd,0x58,0xaa,0xa5,0x7e,0x7a,0x25,0x63,0x26,0xd1}, {0x76,0x54,0x32,0x14,0xe3,0x30,0xf5,0xdd,0x27,0x2b,0x76,0x22,0x7f,0xaa,0x90,0x73,0x6a,0x48,0xdb,0x00}, {0x76,0x54,0x32,0x15,0xfc,0x57,0x00,0x20,0x7c,0x9d,0xf6,0x30,0x6f,0xbd,0x46,0x3e,0xfb,0x8a,0x2c,0x60}, {0x76,0x54,0x32,0x16,0xfb,0x0f,0xd3,0xdf,0x4c,0x4b,0xc3,0x05,0x9d,0x63,0x1e,0xba,0x25,0x2b,0xbe,0x35}, {0x76,0x54,0x32,0x17,0xc6,0xfe,0xe6,0x5f,0xb1,0x35,0x8a,0xf5,0x32,0x7a,0x80,0xbd,0xb8,0x72,0xee,0xae}}}; byte digest[XMACC_MD5::DIGESTSIZE]; bool pass=true, fail; cout << "\nXMACC/MD5 validation suite running...\n"; for (int k=0; k<2; k++) { XMACC_MD5 mac(keys[k], counters[k]); cout << "\nKEY: "; for (int j=0;j(derivedKey.data()), derived.size()); pass = pass && !fail; HexEncoder enc(new FileSink(cout)); cout << (fail ? "FAILED " : "passed "); enc.Put(tuple.purpose); cout << " " << tuple.iterations; cout << " " << tuple.hexPassword << " " << tuple.hexSalt << " "; enc.Put(derived, derived.size()); cout << endl; } return pass; } bool ValidatePBKDF() { bool pass = true; { // from OpenSSL PKCS#12 Program FAQ v1.77, at http://www.drh-consultancy.demon.co.uk/test.txt static const PBKDF_TestTuple testSet[] = { {1, 1, "0073006D006500670000", "0A58CF64530D823F", "8AAAE6297B6CB04642AB5B077851284EB7128F1A2A7FBCA3"}, {2, 1, "0073006D006500670000", "0A58CF64530D823F", "79993DFE048D3B76"}, {1, 1, "0073006D006500670000", "642B99AB44FB4B1F", "F3A95FEC48D7711E985CFE67908C5AB79FA3D7C5CAA5D966"}, {2, 1, "0073006D006500670000", "642B99AB44FB4B1F", "C0A38D64A79BEA1D"}, {3, 1, "0073006D006500670000", "3D83C0E4546AC140", "8D967D88F6CAA9D714800AB3D48051D63F73A312"}, {1, 1000, "007100750065006500670000", "05DEC959ACFF72F7", "ED2034E36328830FF09DF1E1A07DD357185DAC0D4F9EB3D4"}, {2, 1000, "007100750065006500670000", "05DEC959ACFF72F7", "11DEDAD7758D4860"}, {1, 1000, "007100750065006500670000", "1682C0FC5B3F7EC5", "483DD6E919D7DE2E8E648BA8F862F3FBFBDC2BCB2C02957F"}, {2, 1000, "007100750065006500670000", "1682C0FC5B3F7EC5", "9D461D1B00355C50"}, {3, 1000, "007100750065006500670000", "263216FCC2FAB31C", "5EC4C7A80DF652294C3925B6489A7AB857C83476"} }; PKCS12_PBKDF pbkdf; cout << "\nPKCS #12 PBKDF validation suite running...\n\n"; pass = TestPBKDF(pbkdf, testSet, COUNTOF(testSet)) && pass; } { // from draft-ietf-smime-password-03.txt, at http://www.imc.org/draft-ietf-smime-password static const PBKDF_TestTuple testSet[] = { {0, 5, "70617373776f7264", "1234567878563412", "D1DAA78615F287E6"}, {0, 500, "416C6C206E2D656E746974696573206D75737420636F6D6D756E69636174652077697468206F74686572206E2d656E74697469657320766961206E2D3120656E746974656568656568656573", "1234567878563412","6A8970BF68C92CAEA84A8DF28510858607126380CC47AB2D"} }; PKCS5_PBKDF2_HMAC pbkdf; cout << "\nPKCS #5 PBKDF2 validation suite running...\n\n"; pass = TestPBKDF(pbkdf, testSet, COUNTOF(testSet)) && pass; } return pass; } struct HKDF_TestTuple { const char *hexSecret, *hexSalt, *hexContext, *hexDerivedKey; size_t len; }; bool TestHKDF(KeyDerivationFunction &kdf, const HKDF_TestTuple *testSet, unsigned int testSetSize) { bool pass = true; for (unsigned int i=0; i(secret.data()), secret.size(), reinterpret_cast(salt.data()), salt.size(), reinterpret_cast(context.data()), context.size()); pass = pass && (ret == tuple.len); bool fail = !VerifyBufsEqual(derived, reinterpret_cast(derivedKey.data()), derived.size()); pass = pass && !fail; HexEncoder enc(new FileSink(cout)); cout << (fail ? "FAILED " : "passed "); cout << " " << tuple.hexSecret << " " << (tuple.hexSalt ? tuple.hexSalt : ""); cout << " " << (tuple.hexContext ? tuple.hexContext : "") << " "; enc.Put(derived, derived.size()); cout << endl; } return pass; } bool ValidateHKDF() { bool pass = true; { // SHA-1 from RFC 5869, Appendix A, https://tools.ietf.org/html/rfc5869 static const HKDF_TestTuple testSet[] = { // Test Case #4 {"0b0b0b0b0b0b0b0b0b0b0b", "000102030405060708090a0b0c", "f0f1f2f3f4f5f6f7f8f9", "085a01ea1b10f36933068b56efa5ad81 a4f14b822f5b091568a9cdd4f155fda2 c22e422478d305f3f896", 42}, // Test Case #5 {"000102030405060708090a0b0c0d0e0f 101112131415161718191a1b1c1d1e1f 202122232425262728292a2b2c2d2e2f 303132333435363738393a3b3c3d3e3f 404142434445464748494a4b4c4d4e4f", "606162636465666768696a6b6c6d6e6f 707172737475767778797a7b7c7d7e7f 808182838485868788898a8b8c8d8e8f 909192939495969798999a9b9c9d9e9f a0a1a2a3a4a5a6a7a8a9aaabacadaeaf", "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf c0c1c2c3c4c5c6c7c8c9cacbcccdcecf d0d1d2d3d4d5d6d7d8d9dadbdcdddedf e0e1e2e3e4e5e6e7e8e9eaebecedeeef f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "0bd770a74d1160f7c9f12cd5912a06eb ff6adcae899d92191fe4305673ba2ffe 8fa3f1a4e5ad79f3f334b3b202b2173c 486ea37ce3d397ed034c7f9dfeb15c5e 927336d0441f4c4300e2cff0d0900b52 d3b4", 82}, // Test Case #6 {"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", NULL, NULL, "0ac1af7002b3d761d1e55298da9d0506 b9ae52057220a306e07b6b87e8df21d0 ea00033de03984d34918", 42} }; HKDF hkdf; cout << "\nRFC 5869 HKDF(SHA-1) validation suite running...\n\n"; pass = TestHKDF(hkdf, testSet, COUNTOF(testSet)) && pass; } { // SHA-256 from RFC 5869, Appendix A, https://tools.ietf.org/html/rfc5869 static const HKDF_TestTuple testSet[] = { // Test Case #1 {"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", "000102030405060708090a0b0c", "f0f1f2f3f4f5f6f7f8f9", "3cb25f25faacd57a90434f64d0362f2a 2d2d0a90cf1a5a4c5db02d56ecc4c5bf 34007208d5b887185865", 42}, // Test Case #2 {"000102030405060708090a0b0c0d0e0f 101112131415161718191a1b1c1d1e1f 202122232425262728292a2b2c2d2e2f 303132333435363738393a3b3c3d3e3f 404142434445464748494a4b4c4d4e4f", "606162636465666768696a6b6c6d6e6f 707172737475767778797a7b7c7d7e7f 808182838485868788898a8b8c8d8e8f 909192939495969798999a9b9c9d9e9f a0a1a2a3a4a5a6a7a8a9aaabacadaeaf", "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf c0c1c2c3c4c5c6c7c8c9cacbcccdcecf d0d1d2d3d4d5d6d7d8d9dadbdcdddedf e0e1e2e3e4e5e6e7e8e9eaebecedeeef f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "b11e398dc80327a1c8e7f78c596a4934 4f012eda2d4efad8a050cc4c19afa97c 59045a99cac7827271cb41c65e590e09 da3275600c2f09b8367793a9aca3db71 cc30c58179ec3e87c14c01d5c1f3434f 1d87", 82}, // Test Case #3 {"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", NULL, NULL, "8da4e775a563c18f715f802a063c5a31 b8a11f5c5ee1879ec3454e5f3c738d2d 9d201395faa4b61a96c8", 42} }; HKDF hkdf; cout << "\nRFC 5869 HKDF(SHA-256) validation suite running...\n\n"; pass = TestHKDF(hkdf, testSet, COUNTOF(testSet)) && pass; } { // SHA-512 based on RFC 5869, https://tools.ietf.org/html/rfc5869 static const HKDF_TestTuple testSet[] = { // Test Case #0 {"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", "000102030405060708090a0b0c", "f0f1f2f3f4f5f6f7f8f9", "832390086CDA71FB47625BB5CEB168E4 C8E26A1A16ED34D9FC7FE92C14815793 38DA362CB8D9F925D7CB", 42}, // Test Case #0 {"000102030405060708090a0b0c0d0e0f 101112131415161718191a1b1c1d1e1f 202122232425262728292a2b2c2d2e2f 303132333435363738393a3b3c3d3e3f 404142434445464748494a4b4c4d4e4f", "606162636465666768696a6b6c6d6e6f 707172737475767778797a7b7c7d7e7f 808182838485868788898a8b8c8d8e8f 909192939495969798999a9b9c9d9e9f a0a1a2a3a4a5a6a7a8a9aaabacadaeaf", "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf c0c1c2c3c4c5c6c7c8c9cacbcccdcecf d0d1d2d3d4d5d6d7d8d9dadbdcdddedf e0e1e2e3e4e5e6e7e8e9eaebecedeeef f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "CE6C97192805B346E6161E821ED16567 3B84F400A2B514B2FE23D84CD189DDF1 B695B48CBD1C8388441137B3CE28F16A A64BA33BA466B24DF6CFCB021ECFF235 F6A2056CE3AF1DE44D572097A8505D9E 7A93", 82}, // Test Case #0 {"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", NULL, NULL, "F5FA02B18298A72A8C23898A8703472C 6EB179DC204C03425C970E3B164BF90F FF22D04836D0E2343BAC", 42} }; HKDF hkdf; cout << "\nRFC 5869 HKDF(SHA-512) validation suite running...\n\n"; pass = TestHKDF(hkdf, testSet, COUNTOF(testSet)) && pass; } return pass; }