- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# validat3.cpp

    00001 // validat3.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #include "pch.h"
    00004 #include "validate.h"
    00005
    00006 #include "smartptr.h"
    00007 #include "crc.h"
    00008 #include "adler32.h"
    00009 #include "md2.h"
    00010 #include "md4.h"
    00011 #include "md5.h"
    00012 #include "sha.h"
    00013 #include "tiger.h"
    00014 #include "ripemd.h"
    00015 #include "haval.h"
    00016
    00017 #include "xormac.h"
    00018 #include "md5mac.h"
    00019 #include "hmac.h"
    00020 #include "ttmac.h"
    00021
    00022 #include "integer.h"
    00023 #include "pwdbased.h"
    00024 #include "filters.h"
    00025 #include "hex.h"
    00026 #include "files.h"
    00027
    00028 #include <iostream>
    00029 #include <iomanip>
    00030
    00031 USING_NAMESPACE(CryptoPP)
    00032 USING_NAMESPACE(std)
    00033
    00034 struct HashTestTuple
    00035 {
    00036         HashTestTuple(const char *input, const char *output, unsigned int repeatTimes=1)
    00037                 : input((byte *)input), output((byte *)output), inputLen(strlen(input)), repeatTimes(repeatTimes) {}
    00038
    00039         HashTestTuple(const char *input, unsigned int inputLen, const char *output, unsigned int repeatTimes)
    00040                 : input((byte *)input), output((byte *)output), inputLen(inputLen), repeatTimes(repeatTimes) {}
    00041
    00042         const byte *input, *output;
    00043         size_t inputLen;
    00044         unsigned int repeatTimes;
    00045 };
    00046
    00047 bool HashModuleTest(HashTransformation &md, const HashTestTuple *testSet, unsigned int testSetSize)
    00048 {
    00049         bool pass=true, fail;
    00050         SecByteBlock digest(md.DigestSize());
    00051
    00052         for (unsigned int i=0; i<testSetSize; i++)
    00053         {
    00054                 unsigned j;
    00055
    00056                 for (j=0; j<testSet[i].repeatTimes; j++)
    00057                         md.Update(testSet[i].input, testSet[i].inputLen);
    00058                 md.Final(digest);
    00059                 fail = memcmp(digest, testSet[i].output, md.DigestSize()) != 0;
    00060                 pass = pass && !fail;
    00061
    00062                 cout << (fail ? "FAILED   " : "passed   ");
    00063                 for (j=0; j<md.DigestSize(); j++)
    00064                         cout << setw(2) << setfill('0') << hex << (int)digest[j];
    00065                 cout << "   \"" << (char *)testSet[i].input << '\"';
    00066                 if (testSet[i].repeatTimes != 1)
    00067                         cout << " repeated " << dec << testSet[i].repeatTimes << " times";
    00068                 cout  << endl;
    00069         }
    00070
    00071         return pass;
    00072 }
    00073
    00074 bool ValidateCRC32()
    00075 {
    00076         HashTestTuple testSet[] =
    00077         {
    00078                 HashTestTuple("", "\x00\x00\x00\x00"),
    00079                 HashTestTuple("a", "\x43\xbe\xb7\xe8"),
    00080                 HashTestTuple("abc", "\xc2\x41\x24\x35"),
    00081                 HashTestTuple("message digest", "\x7f\x9d\x15\x20"),
    00082                 HashTestTuple("abcdefghijklmnopqrstuvwxyz", "\xbd\x50\x27\x4c"),
    00083                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "\xd2\xe6\xc2\x1f"),
    00084                 HashTestTuple("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "\x72\x4a\xa9\x7c"),
    00085                 HashTestTuple("123456789", "\x26\x39\xf4\xcb")
    00086         };
    00087
    00088         CRC32 crc;
    00089
    00090         cout << "\nCRC-32 validation suite running...\n\n";
    00091         return HashModuleTest(crc, testSet, sizeof(testSet)/sizeof(testSet[0]));
    00092 }
    00093
    00094 bool ValidateAdler32()
    00095 {
    00096         HashTestTuple testSet[] =
    00097         {
    00098                 HashTestTuple("", "\x00\x00\x00\x01"),
    00099                 HashTestTuple("a", "\x00\x62\x00\x62"),
    00100                 HashTestTuple("abc", "\x02\x4d\x01\x27"),
    00101                 HashTestTuple("message digest", "\x29\x75\x05\x86"),
    00102                 HashTestTuple("abcdefghijklmnopqrstuvwxyz", "\x90\x86\x0b\x20"),
    00103                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "\x8a\xdb\x15\x0c"),
    00104                 HashTestTuple("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "\x15\xd8\x70\xf9", 15625)
    00105         };
    00106
    00107         Adler32 md;
    00108
    00109         cout << "\nAdler-32 validation suite running...\n\n";
    00110         return HashModuleTest(md, testSet, sizeof(testSet)/sizeof(testSet[0]));
    00111 }
    00112
    00113 bool ValidateMD2()
    00114 {
    00115         HashTestTuple testSet[] =
    00116         {
    00117                 HashTestTuple("", "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69\x27\x73"),
    00118                 HashTestTuple("a", "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0\xb5\xd1"),
    00119                 HashTestTuple("abc", "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde\xd6\xbb"),
    00120                 HashTestTuple("message digest", "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe\x06\xb0"),
    00121                 HashTestTuple("abcdefghijklmnopqrstuvwxyz", "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47\x94\x0b"),
    00122                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03\x38\xcd"),
    00123                 HashTestTuple("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3\xef\xd8")
    00124         };
    00125
    00126         MD2 md2;
    00127
    00128         cout << "\nMD2 validation suite running...\n\n";
    00129         return HashModuleTest(md2, testSet, sizeof(testSet)/sizeof(testSet[0]));
    00130 }
    00131
    00132 bool ValidateMD4()
    00133 {
    00134         HashTestTuple testSet[] =
    00135         {
    00136                 HashTestTuple("", "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89\xc0"),
    00137                 HashTestTuple("a", "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb\x24"),
    00138                 HashTestTuple("abc", "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72\x9d"),
    00139                 HashTestTuple("message digest", "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01\x4b"),
    00140                 HashTestTuple("abcdefghijklmnopqrstuvwxyz", "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d\xa9"),
    00141                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0\xe4"),
    00142                 HashTestTuple("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05\x36")
    00143         };
    00144
    00145         MD4 md4;
    00146
    00147         cout << "\nMD4 validation suite running...\n\n";
    00148         return HashModuleTest(md4, testSet, sizeof(testSet)/sizeof(testSet[0]));
    00149 }
    00150
    00151 bool ValidateMD5()
    00152 {
    00153         HashTestTuple testSet[] =
    00154         {
    00155                 HashTestTuple("", "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e"),
    00156                 HashTestTuple("a", "\x0c\xc1\x75\xb9\xc0\xf1\xb6\xa8\x31\xc3\x99\xe2\x69\x77\x26\x61"),
    00157                 HashTestTuple("abc", "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72"),
    00158                 HashTestTuple("message digest", "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0"),
    00159                 HashTestTuple("abcdefghijklmnopqrstuvwxyz", "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1\x3b"),
    00160                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d\x9f"),
    00161                 HashTestTuple("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6\x7a")
    00162         };
    00163
    00164         MD5 md5;
    00165
    00166         cout << "\nMD5 validation suite running...\n\n";
    00167         return HashModuleTest(md5, testSet, sizeof(testSet)/sizeof(testSet[0]));
    00168 }
    00169
    00170 bool ValidateSHA()
    00171 {
    00172         HashTestTuple testSet[] =
    00173         {
    00174                 HashTestTuple("abc", "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D"),
    00175                 HashTestTuple("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1"),
    00176                 HashTestTuple("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F", 15625)
    00177         };
    00178
    00179         SHA sha;
    00180
    00181         cout << "\nSHA validation suite running...\n\n";
    00182         return HashModuleTest(sha, testSet, sizeof(testSet)/sizeof(testSet[0]));
    00183 }
    00184
    00185 bool ValidateSHA2()
    00186 {
    00187         HashTestTuple testSet256[] =
    00188         {
    00189                 HashTestTuple("abc", "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad"),
    00190                 HashTestTuple("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1"),
    00191         };
    00192
    00193         bool pass = true;
    00194
    00195         cout << "\nSHA-256 validation suite running...\n\n";
    00196         SHA256 sha256;
    00197         pass = HashModuleTest(sha256, testSet256, sizeof(testSet256)/sizeof(testSet256[0])) && pass;
    00198
    00199         cout << "\nSHA-384 validation suite running...\n\n";
    00200
    00201 #ifdef WORD64_AVAILABLE
    00202         HashTestTuple testSet384[] =
    00203         {
    00204                 HashTestTuple("abc", "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34\xc8\x25\xa7"),
    00205                 HashTestTuple("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91\x74\x60\x39"),
    00206         };
    00207
    00208         SHA384 sha384;
    00209         pass = HashModuleTest(sha384, testSet384, sizeof(testSet384)/sizeof(testSet384[0])) && pass;
    00210 #else
    00211         cout << "word64 not available, skipping SHA-384 validation." << endl;
    00212 #endif
    00213
    00214         cout << "\nSHA-512 validation suite running...\n\n";
    00215
    00216 #ifdef WORD64_AVAILABLE
    00217         HashTestTuple testSet512[] =
    00218         {
    00219                 HashTestTuple("abc", "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5\x4c\xa4\x9f"),
    00220                 HashTestTuple("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b\x87\x4b\xe9\x09"),
    00221         };
    00222
    00223         SHA512 sha512;
    00224         pass = HashModuleTest(sha512, testSet512, sizeof(testSet512)/sizeof(testSet512[0])) && pass;
    00225 #else
    00226         cout << "word64 not available, skipping SHA-512 validation." << endl;
    00227 #endif
    00228
    00229         return pass;
    00230 }
    00231
    00232 bool ValidateTiger()
    00233 {
    00234         cout << "\nTiger validation suite running...\n\n";
    00235
    00236 #ifdef WORD64_AVAILABLE
    00237         HashTestTuple testSet[] =
    00238         {
    00239                 HashTestTuple("", "\x32\x93\xac\x63\x0c\x13\xf0\x24\x5f\x92\xbb\xb1\x76\x6e\x16\x16\x7a\x4e\x58\x49\x2d\xde\x73\xf3"),
    00240                 HashTestTuple("abc", "\x2a\xab\x14\x84\xe8\xc1\x58\xf2\xbf\xb8\xc5\xff\x41\xb5\x7a\x52\x51\x29\x13\x1c\x95\x7b\x5f\x93"),
    00241                 HashTestTuple("Tiger", "\xdd\x00\x23\x07\x99\xf5\x00\x9f\xec\x6d\xeb\xc8\x38\xbb\x6a\x27\xdf\x2b\x9d\x6f\x11\x0c\x79\x37"),
    00242                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-", "\xf7\x1c\x85\x83\x90\x2a\xfb\x87\x9e\xdf\xe6\x10\xf8\x2c\x0d\x47\x86\xa3\xa5\x34\x50\x44\x86\xb5"),
    00243                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZ=abcdefghijklmnopqrstuvwxyz+0123456789", "\x48\xce\xeb\x63\x08\xb8\x7d\x46\xe9\x5d\x65\x61\x12\xcd\xf1\x8d\x97\x91\x5f\x97\x65\x65\x89\x57"),
    00244                 HashTestTuple("Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham", "\x8a\x86\x68\x29\x04\x0a\x41\x0c\x72\x9a\xd2\x3f\x5a\xda\x71\x16\x03\xb3\xcd\xd3\x57\xe4\xc1\x5e"),
    00245                 HashTestTuple("Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge.", "\xce\x55\xa6\xaf\xd5\x91\xf5\xeb\xac\x54\x7f\xf8\x4f\x89\x22\x7f\x93\x31\xda\xb0\xb6\x11\xc8\x89"),
    00246                 HashTestTuple("Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge, 1996.", "\x63\x1a\xbd\xd1\x03\xeb\x9a\x3d\x24\x5b\x6d\xfd\x4d\x77\xb2\x57\xfc\x74\x39\x50\x1d\x15\x68\xdd"),
    00247                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-", "\xc5\x40\x34\xe5\xb4\x3e\xb8\x00\x58\x48\xa7\xe0\xae\x6a\xac\x76\xe4\xff\x59\x0a\xe7\x15\xfd\x25")
    00248         };
    00249
    00250         Tiger tiger;
    00251
    00252         return HashModuleTest(tiger, testSet, sizeof(testSet)/sizeof(testSet[0]));
    00253 #else
    00254         cout << "word64 not available, skipping Tiger validation." << endl;
    00255         return true;
    00256 #endif
    00257 }
    00258
    00259 bool ValidateRIPEMD()
    00260 {
    00261         HashTestTuple testSet128[] =
    00262         {
    00263                 HashTestTuple("", "\xcd\xf2\x62\x13\xa1\x50\xdc\x3e\xcb\x61\x0f\x18\xf6\xb3\x8b\x46"),
    00264                 HashTestTuple("a", "\x86\xbe\x7a\xfa\x33\x9d\x0f\xc7\xcf\xc7\x85\xe7\x2f\x57\x8d\x33"),
    00265                 HashTestTuple("abc", "\xc1\x4a\x12\x19\x9c\x66\xe4\xba\x84\x63\x6b\x0f\x69\x14\x4c\x77"),
    00266                 HashTestTuple("message digest", "\x9e\x32\x7b\x3d\x6e\x52\x30\x62\xaf\xc1\x13\x2d\x7d\xf9\xd1\xb8"),
    00267                 HashTestTuple("abcdefghijklmnopqrstuvwxyz", "\xfd\x2a\xa6\x07\xf7\x1d\xc8\xf5\x10\x71\x49\x22\xb3\x71\x83\x4e"),
    00268                 HashTestTuple("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "\xa1\xaa\x06\x89\xd0\xfa\xfa\x2d\xdc\x22\xe8\x8b\x49\x13\x3a\x06"),
    00269                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "\xd1\xe9\x59\xeb\x17\x9c\x91\x1f\xae\xa4\x62\x4c\x60\xc5\xc7\x02"),
    00270                 HashTestTuple("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "\x3f\x45\xef\x19\x47\x32\xc2\xdb\xb2\xc4\xa2\xc7\x69\x79\x5f\xa3"),
    00271                 HashTestTuple("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "\x4a\x7f\x57\x23\xf9\x54\xeb\xa1\x21\x6c\x9d\x8f\x63\x20\x43\x1f", 15625)
    00272         };
    00273
    00274         HashTestTuple testSet160[] =
    00275         {
    00276                 HashTestTuple("", "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28\x08\x97\x7e\xe8\xf5\x48\xb2\x25\x8d\x31"),
    00277                 HashTestTuple("a", "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae\x34\x7b\xe6\xf4\xdc\x83\x5a\x46\x7f\xfe"),
    00278                 HashTestTuple("abc", "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6\xb0\x87\xf1\x5a\x0b\xfc"),
    00279                 HashTestTuple("message digest", "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8\x5f\xfa\x21\x59\x5f\x36"),
    00280                 HashTestTuple("abcdefghijklmnopqrstuvwxyz", "\xf7\x1c\x27\x10\x9c\x69\x2c\x1b\x56\xbb\xdc\xeb\x5b\x9d\x28\x65\xb3\x70\x8d\xbc"),
    00281                 HashTestTuple("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc\xf4\x9a\xda\x62\xeb\x2b"),
    00282                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "\xb0\xe2\x0b\x6e\x31\x16\x64\x02\x86\xed\x3a\x87\xa5\x71\x30\x79\xb2\x1f\x51\x89"),
    00283                 HashTestTuple("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab\x82\xbf\x63\x32\x6b\xfb"),
    00284                 HashTestTuple("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "\x52\x78\x32\x43\xc1\x69\x7b\xdb\xe1\x6d\x37\xf9\x7f\x68\xf0\x83\x25\xdc\x15\x28", 15625)
    00285         };
    00286
    00287         HashTestTuple testSet256[] =
    00288         {
    00289                 HashTestTuple("", "\x02\xba\x4c\x4e\x5f\x8e\xcd\x18\x77\xfc\x52\xd6\x4d\x30\xe3\x7a\x2d\x97\x74\xfb\x1e\x5d\x02\x63\x80\xae\x01\x68\xe3\xc5\x52\x2d"),
    00290                 HashTestTuple("a", "\xf9\x33\x3e\x45\xd8\x57\xf5\xd9\x0a\x91\xba\xb7\x0a\x1e\xba\x0c\xfb\x1b\xe4\xb0\x78\x3c\x9a\xcf\xcd\x88\x3a\x91\x34\x69\x29\x25"),
    00291                 HashTestTuple("abc", "\xaf\xbd\x6e\x22\x8b\x9d\x8c\xbb\xce\xf5\xca\x2d\x03\xe6\xdb\xa1\x0a\xc0\xbc\x7d\xcb\xe4\x68\x0e\x1e\x42\xd2\xe9\x75\x45\x9b\x65"),
    00292                 HashTestTuple("message digest", "\x87\xe9\x71\x75\x9a\x1c\xe4\x7a\x51\x4d\x5c\x91\x4c\x39\x2c\x90\x18\xc7\xc4\x6b\xc1\x44\x65\x55\x4a\xfc\xdf\x54\xa5\x07\x0c\x0e"),
    00293                 HashTestTuple("abcdefghijklmnopqrstuvwxyz", "\x64\x9d\x30\x34\x75\x1e\xa2\x16\x77\x6b\xf9\xa1\x8a\xcc\x81\xbc\x78\x96\x11\x8a\x51\x97\x96\x87\x82\xdd\x1f\xd9\x7d\x8d\x51\x33"),
    00294                 HashTestTuple("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "\x38\x43\x04\x55\x83\xaa\xc6\xc8\xc8\xd9\x12\x85\x73\xe7\xa9\x80\x9a\xfb\x2a\x0f\x34\xcc\xc3\x6e\xa9\xe7\x2f\x16\xf6\x36\x8e\x3f"),
    00295                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "\x57\x40\xa4\x08\xac\x16\xb7\x20\xb8\x44\x24\xae\x93\x1c\xbb\x1f\xe3\x63\xd1\xd0\xbf\x40\x17\xf1\xa8\x9f\x7e\xa6\xde\x77\xa0\xb8"),
    00296                 HashTestTuple("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "\x06\xfd\xcc\x7a\x40\x95\x48\xaa\xf9\x13\x68\xc0\x6a\x62\x75\xb5\x53\xe3\xf0\x99\xbf\x0e\xa4\xed\xfd\x67\x78\xdf\x89\xa8\x90\xdd"),
    00297                 HashTestTuple("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "\xac\x95\x37\x44\xe1\x0e\x31\x51\x4c\x15\x0d\x4d\x8d\x7b\x67\x73\x42\xe3\x33\x99\x78\x82\x96\xe4\x3a\xe4\x85\x0c\xe4\xf9\x79\x78", 15625)
    00298         };
    00299
    00300         HashTestTuple testSet320[] =
    00301         {
    00302                 HashTestTuple("", "\x22\xd6\x5d\x56\x61\x53\x6c\xdc\x75\xc1\xfd\xf5\xc6\xde\x7b\x41\xb9\xf2\x73\x25\xeb\xc6\x1e\x85\x57\x17\x7d\x70\x5a\x0e\xc8\x80\x15\x1c\x3a\x32\xa0\x08\x99\xb8"),
    00303                 HashTestTuple("a", "\xce\x78\x85\x06\x38\xf9\x26\x58\xa5\xa5\x85\x09\x75\x79\x92\x6d\xda\x66\x7a\x57\x16\x56\x2c\xfc\xf6\xfb\xe7\x7f\x63\x54\x2f\x99\xb0\x47\x05\xd6\x97\x0d\xff\x5d"),
    00304                 HashTestTuple("abc", "\xde\x4c\x01\xb3\x05\x4f\x89\x30\xa7\x9d\x09\xae\x73\x8e\x92\x30\x1e\x5a\x17\x08\x5b\xef\xfd\xc1\xb8\xd1\x16\x71\x3e\x74\xf8\x2f\xa9\x42\xd6\x4c\xdb\xc4\x68\x2d"),
    00305                 HashTestTuple("message digest", "\x3a\x8e\x28\x50\x2e\xd4\x5d\x42\x2f\x68\x84\x4f\x9d\xd3\x16\xe7\xb9\x85\x33\xfa\x3f\x2a\x91\xd2\x9f\x84\xd4\x25\xc8\x8d\x6b\x4e\xff\x72\x7d\xf6\x6a\x7c\x01\x97"),
    00306                 HashTestTuple("abcdefghijklmnopqrstuvwxyz", "\xca\xbd\xb1\x81\x0b\x92\x47\x0a\x20\x93\xaa\x6b\xce\x05\x95\x2c\x28\x34\x8c\xf4\x3f\xf6\x08\x41\x97\x51\x66\xbb\x40\xed\x23\x40\x04\xb8\x82\x44\x63\xe6\xb0\x09"),
    00307                 HashTestTuple("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "\xd0\x34\xa7\x95\x0c\xf7\x22\x02\x1b\xa4\xb8\x4d\xf7\x69\xa5\xde\x20\x60\xe2\x59\xdf\x4c\x9b\xb4\xa4\x26\x8c\x0e\x93\x5b\xbc\x74\x70\xa9\x69\xc9\xd0\x72\xa1\xac"),
    00308                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "\xed\x54\x49\x40\xc8\x6d\x67\xf2\x50\xd2\x32\xc3\x0b\x7b\x3e\x57\x70\xe0\xc6\x0c\x8c\xb9\xa4\xca\xfe\x3b\x11\x38\x8a\xf9\x92\x0e\x1b\x99\x23\x0b\x84\x3c\x86\xa4"),
    00309                 HashTestTuple("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "\x55\x78\x88\xaf\x5f\x6d\x8e\xd6\x2a\xb6\x69\x45\xc6\xd2\xa0\xa4\x7e\xcd\x53\x41\xe9\x15\xeb\x8f\xea\x1d\x05\x24\x95\x5f\x82\x5d\xc7\x17\xe4\xa0\x08\xab\x2d\x42"),
    00310                 HashTestTuple("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "\xbd\xee\x37\xf4\x37\x1e\x20\x64\x6b\x8b\x0d\x86\x2d\xda\x16\x29\x2a\xe3\x6f\x40\x96\x5e\x8c\x85\x09\xe6\x3d\x1d\xbd\xde\xcc\x50\x3e\x2b\x63\xeb\x92\x45\xbb\x66", 15625)
    00311         };
    00312
    00313         bool pass = true;
    00314
    00315         cout << "\nRIPEMD-128 validation suite running...\n\n";
    00316         RIPEMD128 md128;
    00317         pass = HashModuleTest(md128, testSet128, sizeof(testSet128)/sizeof(testSet128[0])) && pass;
    00318
    00319         cout << "\nRIPEMD-160 validation suite running...\n\n";
    00320         RIPEMD160 md160;
    00321         pass = HashModuleTest(md160, testSet160, sizeof(testSet160)/sizeof(testSet160[0])) && pass;
    00322
    00323         cout << "\nRIPEMD-256 validation suite running...\n\n";
    00324         RIPEMD256 md256;
    00325         pass = HashModuleTest(md256, testSet256, sizeof(testSet256)/sizeof(testSet256[0])) && pass;
    00326
    00327         cout << "\nRIPEMD-320 validation suite running...\n\n";
    00328         RIPEMD320 md320;
    00329         pass = HashModuleTest(md320, testSet320, sizeof(testSet320)/sizeof(testSet320[0])) && pass;
    00330
    00331         return pass;
    00332 }
    00333
    00334 bool ValidateHAVAL()
    00335 {
    00336         HashTestTuple testSet[] =
    00337         {
    00338                 HashTestTuple("", "\xC6\x8F\x39\x91\x3F\x90\x1F\x3D\xDF\x44\xC7\x07\x35\x7A\x7D\x70"),
    00339                 HashTestTuple("a", "\x4D\xA0\x8F\x51\x4A\x72\x75\xDB\xC4\xCE\xCE\x4A\x34\x73\x85\x98\x39\x83\xA8\x30"),
    00340                 HashTestTuple("HAVAL", "\x0C\x13\x96\xD7\x77\x26\x89\xC4\x67\x73\xF3\xDA\xAC\xA4\xEF\xA9\x82\xAD\xBF\xB2\xF1\x46\x7E\xEA"),
    00341                 HashTestTuple("0123456789", "\xBE\xBD\x78\x16\xF0\x9B\xAE\xEC\xF8\x90\x3B\x1B\x9B\xC6\x72\xD9\xFA\x42\x8E\x46\x2B\xA6\x99\xF8\x14\x84\x15\x29"),
    00342                 HashTestTuple("abcdefghijklmnopqrstuvwxyz", "\xC9\xC7\xD8\xAF\xA1\x59\xFD\x9E\x96\x5C\xB8\x3F\xF5\xEE\x6F\x58\xAE\xDA\x35\x2C\x0E\xFF\x00\x55\x48\x15\x3A\x61\x55\x1C\x38\xEE"),
    00343                 HashTestTuple("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "\xB4\x5C\xB6\xE6\x2F\x2B\x13\x20\xE4\xF8\xF1\xB0\xB2\x73\xD4\x5A\xDD\x47\xC3\x21\xFD\x23\x99\x9D\xCF\x40\x3A\xC3\x76\x36\xD9\x63")
    00344         };
    00345
    00346         bool pass=true;
    00347
    00348         cout << "\nHAVAL validation suite running...\n\n";
    00349         {
    00350                 HAVAL3 md(16);
    00351                 pass = HashModuleTest(md, testSet+0, 1) && pass;
    00352         }
    00353         {
    00354                 HAVAL3 md(20);
    00355                 pass = HashModuleTest(md, testSet+1, 1) && pass;
    00356         }
    00357         {
    00358                 HAVAL4 md(24);
    00359                 pass = HashModuleTest(md, testSet+2, 1) && pass;
    00360         }
    00361         {
    00362                 HAVAL4 md(28);
    00363                 pass = HashModuleTest(md, testSet+3, 1) && pass;
    00364         }
    00365         {
    00366                 HAVAL5 md(32);
    00367                 pass = HashModuleTest(md, testSet+4, 1) && pass;
    00368         }
    00369         {
    00370                 HAVAL5 md(32);
    00371                 pass = HashModuleTest(md, testSet+5, 1) && pass;
    00372         }
    00373
    00374         return pass;
    00375 }
    00376
    00377 bool ValidatePanama()
    00378 {
    00379         cout << "\nPanama validation suite running...\n";
    00380
    00381         return RunTestDataFile("TestVectors/panama.txt");
    00382 }
    00383
    00384 bool ValidateWhirlpool()
    00385 {
    00386         cout << "\nWhirlpool Hash Function validation suite running...\n";
    00387
    00388 #ifdef WORD64_AVAILABLE
    00389         return RunTestDataFile("TestVectors/whrlpool.txt");
    00390 #else
    00391         cout << "word64 not available, skipping Whirlpool validation." << endl;
    00392         return true;
    00393 #endif
    00394         }
    00395
    00396 bool ValidateMD5MAC()
    00397 {
    00398         const byte keys[2][MD5MAC::KEYLENGTH]={
    00399                 {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
    00400                 {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10}};
    00401
    00402         const char *TestVals[7]={
    00403                 "",
    00404                 "a",
    00405                 "abc",
    00406                 "message digest",
    00407                 "abcdefghijklmnopqrstuvwxyz",
    00408                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
    00409                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"};
    00410
    00411         const byte output[2][7][MD5MAC::DIGESTSIZE]={
    00412                 {{0x1f,0x1e,0xf2,0x37,0x5c,0xc0,0xe0,0x84,0x4f,0x98,0xe7,0xe8,0x11,0xa3,0x4d,0xa8},
    00413                 {0x7a,0x76,0xee,0x64,0xca,0x71,0xef,0x23,0x7e,0x26,0x29,0xed,0x94,0x52,0x73,0x65},
    00414                 {0xe8,0x01,0x3c,0x11,0xf7,0x20,0x9d,0x13,0x28,0xc0,0xca,0xa0,0x4f,0xd0,0x12,0xa6},
    00415                 {0xc8,0x95,0x53,0x4f,0x22,0xa1,0x74,0xbc,0x3e,0x6a,0x25,0xa2,0xb2,0xef,0xd6,0x30},
    00416                 {0x91,0x72,0x86,0x7e,0xb6,0x00,0x17,0x88,0x4c,0x6f,0xa8,0xcc,0x88,0xeb,0xe7,0xc9},
    00417                 {0x3b,0xd0,0xe1,0x1d,0x5e,0x09,0x4c,0xb7,0x1e,0x35,0x44,0xac,0xa9,0xb8,0xbf,0xa2},
    00418                 {0x93,0x37,0x16,0x64,0x44,0xcc,0x95,0x35,0xb7,0xd5,0xb8,0x0f,0x91,0xe5,0x29,0xcb}},
    00419                 {{0x2f,0x6e,0x73,0x13,0xbf,0xbb,0xbf,0xcc,0x3a,0x2d,0xde,0x26,0x8b,0x59,0xcc,0x4d},
    00420                 {0x69,0xf6,0xca,0xff,0x40,0x25,0x36,0xd1,0x7a,0xe1,0x38,0x03,0x2c,0x0c,0x5f,0xfd},
    00421                 {0x56,0xd3,0x2b,0x6c,0x34,0x76,0x65,0xd9,0x74,0xd6,0xf7,0x5c,0x3f,0xc6,0xf0,0x40},
    00422                 {0xb8,0x02,0xb2,0x15,0x4e,0x59,0x8b,0x6f,0x87,0x60,0x56,0xc7,0x85,0x46,0x2c,0x0b},
    00423                 {0x5a,0xde,0xf4,0xbf,0xf8,0x04,0xbe,0x08,0x58,0x7e,0x94,0x41,0xcf,0x6d,0xbd,0x57},
    00424                 {0x18,0xe3,0x49,0xa5,0x24,0x44,0xb3,0x0e,0x5e,0xba,0x5a,0xdd,0xdc,0xd9,0xf1,0x8d},
    00425                 {0xf2,0xb9,0x06,0xa5,0xb8,0x4b,0x9b,0x4b,0xbe,0x95,0xed,0x32,0x56,0x4e,0xe7,0xeb}}};
    00426
    00427         byte digest[MD5MAC::DIGESTSIZE];
    00428         bool pass=true, fail;
    00429
    00430         cout << "\nMD5MAC validation suite running...\n";
    00431
    00432         for (int k=0; k<2; k++)
    00433         {
    00434                 MD5MAC mac(keys[k]);
    00435                 cout << "\nKEY: ";
    00436                 for (int j=0;j<MD5MAC::KEYLENGTH;j++)
    00437                         cout << setw(2) << setfill('0') << hex << (int)keys[k][j];
    00438                 cout << endl << endl;
    00439                 for (int i=0;i<7;i++)
    00440                 {
    00441                         mac.Update((byte *)TestVals[i], strlen(TestVals[i]));
    00442                         mac.Final(digest);
    00443                         fail = memcmp(digest, output[k][i], MD5MAC::DIGESTSIZE)
    00444                                  || !mac.VerifyDigest(output[k][i], (byte *)TestVals[i], strlen(TestVals[i]));
    00445                         pass = pass && !fail;
    00446                         cout << (fail ? "FAILED   " : "passed   ");
    00447                         for (int j=0;j<MD5MAC::DIGESTSIZE;j++)
    00448                                 cout << setw(2) << setfill('0') << hex << (int)digest[j];
    00449                         cout << "   \"" << TestVals[i] << '\"' << endl;
    00450                 }
    00451         }
    00452
    00453         return pass;
    00454 }
    00455
    00456 bool ValidateHMAC()
    00457 {
    00458         typedef HMAC<MD5> HMAC_MD5;
    00459
    00460         const char* keys[]=
    00461         {
    00462                 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
    00463                 "Jefe",
    00464                 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
    00465                 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
    00466                         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
    00467                         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
    00468                         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
    00469                         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
    00470         };
    00471
    00472         HashTestTuple testSet[] =
    00473         {
    00474                 HashTestTuple("Hi There", "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d"),
    00475                 HashTestTuple("what do ya want for nothing?", "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38"),
    00476                 HashTestTuple("\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD",
    00477                         "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6"),
    00478                 HashTestTuple("Test Using Larger Than Block-Size Key - Hash Key First", "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd")
    00479         };
    00480
    00481         bool pass=true;
    00482
    00483         cout << "\nHMAC/MD5 validation suite running...\n";
    00484
    00485         for (int k=0; k<4; k++)
    00486         {
    00487                 HMAC_MD5 mac((byte *)keys[k], strlen(keys[k]));
    00488                 cout << "\nKEY: ";
    00489                 for (int j=0; keys[k][j] != 0; j++)
    00490                         cout << setw(2) << setfill('0') << hex << (int)(byte)keys[k][j];
    00491                 cout << endl;
    00492
    00493                 pass = HashModuleTest(mac, testSet+k, 1) && pass;
    00494         }
    00495
    00496         return pass;
    00497 }
    00498
    00499 bool ValidateXMACC()
    00500 {
    00501         typedef XMACC<MD5> XMACC_MD5;
    00502
    00503         const byte keys[2][XMACC_MD5::KEYLENGTH]={
    00504                 {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb},
    00505                 {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98}};
    00506
    00507         const word32 counters[2]={0xccddeeff, 0x76543210};
    00508
    00509         const char *TestVals[7]={
    00510                 "",
    00511                 "a",
    00512                 "abc",
    00513                 "message digest",
    00514                 "abcdefghijklmnopqrstuvwxyz",
    00515                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
    00516                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"};
    00517
    00518         const byte output[2][7][XMACC_MD5::DIGESTSIZE]={
    00519                 {{0xcc,0xdd,0xef,0x00,0xfa,0x89,0x54,0x92,0x86,0x32,0xda,0x2a,0x3f,0x29,0xc5,0x52,0xa0,0x0d,0x05,0x13},
    00520                 {0xcc,0xdd,0xef,0x01,0xae,0xdb,0x8b,0x7b,0x69,0x71,0xc7,0x91,0x71,0x48,0x9d,0x18,0xe7,0xdf,0x9d,0x5a},
    00521                 {0xcc,0xdd,0xef,0x02,0x5e,0x01,0x2e,0x2e,0x4b,0xc3,0x83,0x62,0xc2,0xf4,0xe6,0x18,0x1c,0x44,0xaf,0xca},
    00522                 {0xcc,0xdd,0xef,0x03,0x3e,0xa9,0xf1,0xe0,0x97,0x91,0xf8,0xe2,0xbe,0xe0,0xdf,0xf3,0x41,0x03,0xb3,0x5a},
    00523                 {0xcc,0xdd,0xef,0x04,0x2e,0x6a,0x8d,0xb9,0x72,0xe3,0xce,0x9f,0xf4,0x28,0x45,0xe7,0xbc,0x80,0xa9,0xc7},
    00524                 {0xcc,0xdd,0xef,0x05,0x1a,0xd5,0x40,0x78,0xfb,0x16,0x37,0xfc,0x7a,0x1d,0xce,0xb4,0x77,0x10,0xb2,0xa0},
    00525                 {0xcc,0xdd,0xef,0x06,0x13,0x2f,0x11,0x47,0xd7,0x1b,0xb5,0x52,0x36,0x51,0x26,0xb0,0x96,0xd7,0x60,0x81}},
    00526                 {{0x76,0x54,0x32,0x11,0xe9,0xcb,0x74,0x32,0x07,0x93,0xfe,0x01,0xdd,0x27,0xdb,0xde,0x6b,0x77,0xa4,0x56},
    00527                 {0x76,0x54,0x32,0x12,0xcd,0x55,0x87,0x5c,0xc0,0x35,0x85,0x99,0x44,0x02,0xa5,0x0b,0x8c,0xe7,0x2c,0x68},
    00528                 {0x76,0x54,0x32,0x13,0xac,0xfd,0x87,0x50,0xc3,0x8f,0xcd,0x58,0xaa,0xa5,0x7e,0x7a,0x25,0x63,0x26,0xd1},
    00529                 {0x76,0x54,0x32,0x14,0xe3,0x30,0xf5,0xdd,0x27,0x2b,0x76,0x22,0x7f,0xaa,0x90,0x73,0x6a,0x48,0xdb,0x00},
    00530                 {0x76,0x54,0x32,0x15,0xfc,0x57,0x00,0x20,0x7c,0x9d,0xf6,0x30,0x6f,0xbd,0x46,0x3e,0xfb,0x8a,0x2c,0x60},
    00531                 {0x76,0x54,0x32,0x16,0xfb,0x0f,0xd3,0xdf,0x4c,0x4b,0xc3,0x05,0x9d,0x63,0x1e,0xba,0x25,0x2b,0xbe,0x35},
    00532                 {0x76,0x54,0x32,0x17,0xc6,0xfe,0xe6,0x5f,0xb1,0x35,0x8a,0xf5,0x32,0x7a,0x80,0xbd,0xb8,0x72,0xee,0xae}}};
    00533
    00534         byte digest[XMACC_MD5::DIGESTSIZE];
    00535         bool pass=true, fail;
    00536
    00537         cout << "\nXMACC/MD5 validation suite running...\n";
    00538
    00539         for (int k=0; k<2; k++)
    00540         {
    00541                 XMACC_MD5 mac(keys[k], counters[k]);
    00542                 cout << "\nKEY: ";
    00543                 for (int j=0;j<XMACC_MD5::KEYLENGTH;j++)
    00544                         cout << setw(2) << setfill('0') << hex << (int)keys[k][j];
    00545                 cout << "    COUNTER: 0x" << hex << counters[k] << endl << endl;
    00546                 for (int i=0;i<7;i++)
    00547                 {
    00548                         mac.Update((byte *)TestVals[i], strlen(TestVals[i]));
    00549                         mac.Final(digest);
    00550                         fail = memcmp(digest, output[k][i], XMACC_MD5::DIGESTSIZE)
    00551                                  || !mac.VerifyDigest(output[k][i], (byte *)TestVals[i], strlen(TestVals[i]));
    00552                         pass = pass && !fail;
    00553                         cout << (fail ? "FAILED   " : "passed   ");
    00554                         for (int j=0;j<XMACC_MD5::DIGESTSIZE;j++)
    00555                                 cout << setw(2) << setfill('0') << hex << (int)digest[j];
    00556                         cout << "   \"" << TestVals[i] << '\"' << endl;
    00557                 }
    00558         }
    00559
    00560         return pass;
    00561 }
    00562
    00563 bool ValidateTTMAC()
    00564 {
    00565         const byte key[TTMAC::KEYLENGTH]={
    00566                 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,
    00567                 0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x01,0x23,0x45,0x67};
    00568
    00569         const char *TestVals[8]={
    00570                 "",
    00571                 "a",
    00572                 "abc",
    00573                 "message digest",
    00574                 "abcdefghijklmnopqrstuvwxyz",
    00575                 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
    00576                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
    00577                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"};
    00578
    00579         const byte output[8][TTMAC::DIGESTSIZE]={
    00580                 {0x2d,0xec,0x8e,0xd4,0xa0,0xfd,0x71,0x2e,0xd9,0xfb,0xf2,0xab,0x46,0x6e,0xc2,0xdf,0x21,0x21,0x5e,0x4a},
    00581                 {0x58,0x93,0xe3,0xe6,0xe3,0x06,0x70,0x4d,0xd7,0x7a,0xd6,0xe6,0xed,0x43,0x2c,0xde,0x32,0x1a,0x77,0x56},
    00582                 {0x70,0xbf,0xd1,0x02,0x97,0x97,0xa5,0xc1,0x6d,0xa5,0xb5,0x57,0xa1,0xf0,0xb2,0x77,0x9b,0x78,0x49,0x7e},
    00583                 {0x82,0x89,0xf4,0xf1,0x9f,0xfe,0x4f,0x2a,0xf7,0x37,0xde,0x4b,0xd7,0x1c,0x82,0x9d,0x93,0xa9,0x72,0xfa},
    00584                 {0x21,0x86,0xca,0x09,0xc5,0x53,0x31,0x98,0xb7,0x37,0x1f,0x24,0x52,0x73,0x50,0x4c,0xa9,0x2b,0xae,0x60},
    00585                 {0x8a,0x7b,0xf7,0x7a,0xef,0x62,0xa2,0x57,0x84,0x97,0xa2,0x7c,0x0d,0x65,0x18,0xa4,0x29,0xe7,0xc1,0x4d},
    00586                 {0x54,0xba,0xc3,0x92,0xa8,0x86,0x80,0x6d,0x16,0x95,0x56,0xfc,0xbb,0x67,0x89,0xb5,0x4f,0xb3,0x64,0xfb},
    00587                 {0x0c,0xed,0x2c,0x9f,0x8f,0x0d,0x9d,0x03,0x98,0x1a,0xb5,0xc8,0x18,0x4b,0xac,0x43,0xdd,0x54,0xc4,0x84}};
    00588
    00589         byte digest[TTMAC::DIGESTSIZE];
    00590         bool pass=true, fail;
    00591
    00592         cout << "\nTwo-Track-MAC validation suite running...\n";
    00593
    00594         TTMAC mac(key, sizeof(key));
    00595         for (int k=0; k<sizeof(TestVals)/sizeof(TestVals[0]); k++)
    00596         {
    00597                 mac.Update((byte *)TestVals[k], strlen(TestVals[k]));
    00598                 mac.Final(digest);
    00599                 fail = memcmp(digest, output[k], TTMAC::DIGESTSIZE)
    00600                         || !mac.VerifyDigest(output[k], (byte *)TestVals[k], strlen(TestVals[k]));
    00601                 pass = pass && !fail;
    00602                 cout << (fail ? "FAILED   " : "passed   ");
    00603                 for (int j=0;j<TTMAC::DIGESTSIZE;j++)
    00604                         cout << setw(2) << setfill('0') << hex << (int)digest[j];
    00605                 cout << "   \"" << TestVals[k] << '\"' << endl;
    00606         }
    00607
    00608         return true;
    00609 }
    00610
    00611 struct PBKDF_TestTuple
    00612 {
    00613         byte purpose;
    00614         unsigned int iterations;
    00615         const char *hexPassword, *hexSalt, *hexDerivedKey;
    00616 };
    00617
    00618 bool TestPBKDF(PasswordBasedKeyDerivationFunction &pbkdf, const PBKDF_TestTuple *testSet, unsigned int testSetSize)
    00619 {
    00620         bool pass = true;
    00621
    00622         for (unsigned int i=0; i<testSetSize; i++)
    00623         {
    00624                 const PBKDF_TestTuple &tuple = testSet[i];
    00625
    00626                 string password, salt, derivedKey;
    00627                 StringSource(tuple.hexPassword, true, new HexDecoder(new StringSink(password)));
    00628                 StringSource(tuple.hexSalt, true, new HexDecoder(new StringSink(salt)));
    00629                 StringSource(tuple.hexDerivedKey, true, new HexDecoder(new StringSink(derivedKey)));
    00630
    00631                 SecByteBlock derived(derivedKey.size());
    00632                 pbkdf.DeriveKey(derived, derived.size(), tuple.purpose, (byte *)password.data(), password.size(), (byte *)salt.data(), salt.size(), tuple.iterations);
    00633                 bool fail = memcmp(derived, derivedKey.data(), derived.size()) != 0;
    00634                 pass = pass && !fail;
    00635
    00636                 HexEncoder enc(new FileSink(cout));
    00637                 cout << (fail ? "FAILED   " : "passed   ");
    00638                 enc.Put(tuple.purpose);
    00639                 cout << " " << tuple.iterations;
    00640                 cout << " " << tuple.hexPassword << " " << tuple.hexSalt << " ";
    00641                 enc.Put(derived, derived.size());
    00642                 cout << endl;
    00643         }
    00644
    00645         return pass;
    00646 }
    00647
    00648 bool ValidatePBKDF()
    00649 {
    00650         bool pass = true;
    00651
    00652         {
    00653         // from OpenSSL PKCS#12 Program FAQ v1.77, at http://www.drh-consultancy.demon.co.uk/test.txt
    00654         PBKDF_TestTuple testSet[] =
    00655         {
    00656                 {1, 1, "0073006D006500670000", "0A58CF64530D823F", "8AAAE6297B6CB04642AB5B077851284EB7128F1A2A7FBCA3"},
    00657                 {2, 1, "0073006D006500670000", "0A58CF64530D823F", "79993DFE048D3B76"},
    00658                 {1, 1, "0073006D006500670000", "642B99AB44FB4B1F", "F3A95FEC48D7711E985CFE67908C5AB79FA3D7C5CAA5D966"},
    00659                 {2, 1, "0073006D006500670000", "642B99AB44FB4B1F", "C0A38D64A79BEA1D"},
    00660                 {3, 1, "0073006D006500670000", "3D83C0E4546AC140", "8D967D88F6CAA9D714800AB3D48051D63F73A312"},
    00661                 {1, 1000, "007100750065006500670000", "05DEC959ACFF72F7", "ED2034E36328830FF09DF1E1A07DD357185DAC0D4F9EB3D4"},
    00662                 {2, 1000, "007100750065006500670000", "05DEC959ACFF72F7", "11DEDAD7758D4860"},
    00663                 {1, 1000, "007100750065006500670000", "1682C0FC5B3F7EC5", "483DD6E919D7DE2E8E648BA8F862F3FBFBDC2BCB2C02957F"},
    00664                 {2, 1000, "007100750065006500670000", "1682C0FC5B3F7EC5", "9D461D1B00355C50"},
    00665                 {3, 1000, "007100750065006500670000", "263216FCC2FAB31C", "5EC4C7A80DF652294C3925B6489A7AB857C83476"}
    00666         };
    00667
    00668         PKCS12_PBKDF<SHA1> pbkdf;
    00669
    00670         cout << "\nPKCS #12 PBKDF validation suite running...\n\n";
    00671         pass = TestPBKDF(pbkdf, testSet, sizeof(testSet)/sizeof(testSet[0])) && pass;
    00672         }
    00673
    00674         {
    00675         // from draft-ietf-smime-password-03.txt, at http://www.imc.org/draft-ietf-smime-password
    00676         PBKDF_TestTuple testSet[] =
    00677         {
    00678                 {0, 5, "70617373776f7264", "1234567878563412", "D1DAA78615F287E6"},
    00679                 {0, 500, "416C6C206E2D656E746974696573206D75737420636F6D6D756E69636174652077697468206F74686572206E2d656E74697469657320766961206E2D3120656E746974656568656568656573", "1234567878563412","6A8970BF68C92CAEA84A8DF28510858607126380CC47AB2D"}
    00680         };
    00681
    00682         PKCS5_PBKDF2_HMAC<SHA1> pbkdf;
    00683
    00684         cout << "\nPKCS #5 PBKDF2 validation suite running...\n\n";
    00685         pass = TestPBKDF(pbkdf, testSet, sizeof(testSet)/sizeof(testSet[0])) && pass;
    00686         }
    00687
    00688         return pass;
    00689 }