- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# fipstest.cpp

    00001 // fipstest.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #include "pch.h"
    00004
    00005 #ifndef CRYPTOPP_IMPORTS
    00006
    00007 #define CRYPTOPP_DEFAULT_NO_DLL
    00008 #include "dll.h"
    00009
    00010 #ifdef CRYPTOPP_WIN32_AVAILABLE
    00011 #define _WIN32_WINNT 0x0400
    00012 #include <windows.h>
    00013
    00014 #if defined(_MSC_VER) && _MSC_VER >= 14
    00015 #ifdef _M_IX86
    00016 #define _CRT_DEBUGGER_HOOK _crt_debugger_hook
    00017 #else
    00018 #define _CRT_DEBUGGER_HOOK __crt_debugger_hook
    00019 #endif
    00020 extern "C" {_CRTIMP void __cdecl _CRT_DEBUGGER_HOOK(int);}
    00021 #endif
    00022 #endif
    00023
    00024 NAMESPACE_BEGIN(CryptoPP)
    00025
    00026 extern PowerUpSelfTestStatus g_powerUpSelfTestStatus;
    00027 SecByteBlock g_actualMac;
    00028 unsigned long g_macFileLocation = 0;
    00029
    00030 // use a random dummy string here, to be searched/replaced later with the real MAC
    00031 static const byte s_moduleMac[CryptoPP::HMAC<CryptoPP::SHA1>::DIGESTSIZE] = CRYPTOPP_DUMMY_DLL_MAC;
    00032 CRYPTOPP_COMPILE_ASSERT(sizeof(s_moduleMac) == CryptoPP::SHA1::DIGESTSIZE);
    00033
    00034 #ifdef CRYPTOPP_WIN32_AVAILABLE
    00035 static HMODULE s_hModule = NULL;
    00036 #endif
    00037
    00038 const byte * CRYPTOPP_API GetActualMacAndLocation(unsigned int &macSize, unsigned int &fileLocation)
    00039 {
    00040         macSize = (unsigned int)g_actualMac.size();
    00041         fileLocation = g_macFileLocation;
    00042         return g_actualMac;
    00043 }
    00044
    00045 void KnownAnswerTest(RandomNumberGenerator &rng, const char *output)
    00046 {
    00047         EqualityComparisonFilter comparison;
    00048
    00049         RandomNumberStore(rng, strlen(output)/2).TransferAllTo(comparison, "0");
    00050         StringSource(output, true, new HexDecoder(new ChannelSwitch(comparison, "1")));
    00051
    00052         comparison.ChannelMessageSeriesEnd("0");
    00053         comparison.ChannelMessageSeriesEnd("1");
    00054 }
    00055
    00056 template <class CIPHER>
    00057 void X917RNG_KnownAnswerTest(
    00058         const char *key,
    00059         const char *seed,
    00060         const char *deterministicTimeVector,
    00061         const char *output,
    00062         CIPHER *dummy = NULL)
    00063 {
    00064 #ifdef OS_RNG_AVAILABLE
    00065         std::string decodedKey, decodedSeed, decodedDeterministicTimeVector;
    00066         StringSource(key, true, new HexDecoder(new StringSink(decodedKey)));
    00067         StringSource(seed, true, new HexDecoder(new StringSink(decodedSeed)));
    00068         StringSource(deterministicTimeVector, true, new HexDecoder(new StringSink(decodedDeterministicTimeVector)));
    00069
    00070         AutoSeededX917RNG<CIPHER> rng;
    00071         rng.Reseed((const byte *)decodedKey.data(), decodedKey.size(), (const byte *)decodedSeed.data(), (const byte *)decodedDeterministicTimeVector.data());
    00072         KnownAnswerTest(rng, output);
    00073 #else
    00074         throw 0;
    00075 #endif
    00076 }
    00077
    00078 void KnownAnswerTest(StreamTransformation &encryption, StreamTransformation &decryption, const char *plaintext, const char *ciphertext)
    00079 {
    00080         EqualityComparisonFilter comparison;
    00081
    00082         StringSource(plaintext, true, new HexDecoder(new StreamTransformationFilter(encryption, new ChannelSwitch(comparison, "0"), StreamTransformationFilter::NO_PADDING)));
    00083         StringSource(ciphertext, true, new HexDecoder(new ChannelSwitch(comparison, "1")));
    00084
    00085         StringSource(ciphertext, true, new HexDecoder(new StreamTransformationFilter(decryption, new ChannelSwitch(comparison, "0"), StreamTransformationFilter::NO_PADDING)));
    00086         StringSource(plaintext, true, new HexDecoder(new ChannelSwitch(comparison, "1")));
    00087
    00088         comparison.ChannelMessageSeriesEnd("0");
    00089         comparison.ChannelMessageSeriesEnd("1");
    00090 }
    00091
    00092 template <class CIPHER>
    00093 void SymmetricEncryptionKnownAnswerTest(
    00094         const char *key,
    00095         const char *hexIV,
    00096         const char *plaintext,
    00097         const char *ecb,
    00098         const char *cbc,
    00099         const char *cfb,
    00100         const char *ofb,
    00101         const char *ctr,
    00102         CIPHER *dummy = NULL)
    00103 {
    00104         std::string decodedKey;
    00105         StringSource(key, true, new HexDecoder(new StringSink(decodedKey)));
    00106
    00107         typename CIPHER::Encryption encryption((const byte *)decodedKey.data(), decodedKey.size());
    00108         typename CIPHER::Decryption decryption((const byte *)decodedKey.data(), decodedKey.size());
    00109
    00110         SecByteBlock iv(encryption.BlockSize());
    00111         StringSource(hexIV, true, new HexDecoder(new ArraySink(iv, iv.size())));
    00112
    00113         if (ecb)
    00114                 KnownAnswerTest(ECB_Mode_ExternalCipher::Encryption(encryption).Ref(), ECB_Mode_ExternalCipher::Decryption(decryption).Ref(), plaintext, ecb);
    00115         if (cbc)
    00116                 KnownAnswerTest(CBC_Mode_ExternalCipher::Encryption(encryption, iv).Ref(), CBC_Mode_ExternalCipher::Decryption(decryption, iv).Ref(), plaintext, cbc);
    00117         if (cfb)
    00118                 KnownAnswerTest(CFB_Mode_ExternalCipher::Encryption(encryption, iv).Ref(), CFB_Mode_ExternalCipher::Decryption(encryption, iv).Ref(), plaintext, cfb);
    00119         if (ofb)
    00120                 KnownAnswerTest(OFB_Mode_ExternalCipher::Encryption(encryption, iv).Ref(), OFB_Mode_ExternalCipher::Decryption(encryption, iv).Ref(), plaintext, ofb);
    00121         if (ctr)
    00122                 KnownAnswerTest(CTR_Mode_ExternalCipher::Encryption(encryption, iv).Ref(), CTR_Mode_ExternalCipher::Decryption(encryption, iv).Ref(), plaintext, ctr);
    00123 }
    00124
    00125 void KnownAnswerTest(HashTransformation &hash, const char *message, const char *digest)
    00126 {
    00127         EqualityComparisonFilter comparison;
    00128         StringSource(digest, true, new HexDecoder(new ChannelSwitch(comparison, "1")));
    00129         StringSource(message, true, new HashFilter(hash, new ChannelSwitch(comparison, "0")));
    00130
    00131         comparison.ChannelMessageSeriesEnd("0");
    00132         comparison.ChannelMessageSeriesEnd("1");
    00133 }
    00134
    00135 template <class HASH>
    00136 void SecureHashKnownAnswerTest(const char *message, const char *digest, HASH *dummy = NULL)
    00137 {
    00138         HASH hash;
    00139         KnownAnswerTest(hash, message, digest);
    00140 }
    00141
    00142 template <class MAC>
    00143 void MAC_KnownAnswerTest(const char *key, const char *message, const char *digest, MAC *dummy = NULL)
    00144 {
    00145         std::string decodedKey;
    00146         StringSource(key, true, new HexDecoder(new StringSink(decodedKey)));
    00147
    00148         MAC mac((const byte *)decodedKey.data(), decodedKey.size());
    00149         KnownAnswerTest(mac, message, digest);
    00150 }
    00151
    00152 template <class SCHEME>
    00153 void SignatureKnownAnswerTest(const char *key, const char *message, const char *signature, SCHEME *dummy = NULL)
    00154 {
    00155 #ifdef OS_RNG_AVAILABLE
    00156         AutoSeededX917RNG<DES_EDE3> rng;
    00157 #else
    00158         RandomNumberGenerator &rng = NullRNG();
    00159 #endif
    00160
    00161         typename SCHEME::Signer signer(StringSource(key, true, new HexDecoder).Ref());
    00162         typename SCHEME::Verifier verifier(signer);
    00163
    00164         EqualityComparisonFilter comparison;
    00165
    00166         StringSource(message, true, new SignerFilter(rng, signer, new ChannelSwitch(comparison, "0")));
    00167         StringSource(signature, true, new HexDecoder(new ChannelSwitch(comparison, "1")));
    00168
    00169         comparison.ChannelMessageSeriesEnd("0");
    00170         comparison.ChannelMessageSeriesEnd("1");
    00171
    00172         VerifierFilter verifierFilter(verifier, NULL, VerifierFilter::SIGNATURE_AT_BEGIN | VerifierFilter::THROW_EXCEPTION);
    00173         StringSource(signature, true, new HexDecoder(new Redirector(verifierFilter, Redirector::DATA_ONLY)));
    00174         StringSource(message, true, new Redirector(verifierFilter));
    00175 }
    00176
    00177 void EncryptionPairwiseConsistencyTest(const PK_Encryptor &encryptor, const PK_Decryptor &decryptor)
    00178 {
    00179         try
    00180         {
    00181 #ifdef OS_RNG_AVAILABLE
    00182                 AutoSeededX917RNG<DES_EDE3> rng;
    00183 #else
    00184                 RandomNumberGenerator &rng = NullRNG();
    00185 #endif
    00186                 const char *testMessage ="test message";
    00187                 std::string ciphertext, decrypted;
    00188
    00189                 StringSource(
    00190                         testMessage,
    00191                         true,
    00192                         new PK_EncryptorFilter(
    00193                                 rng,
    00194                                 encryptor,
    00195                                 new StringSink(ciphertext)));
    00196
    00197                 if (ciphertext == testMessage)
    00198                         throw 0;
    00199
    00200                 StringSource(
    00201                         ciphertext,
    00202                         true,
    00203                         new PK_DecryptorFilter(
    00204                                 rng,
    00205                                 decryptor,
    00206                                 new StringSink(decrypted)));
    00207
    00208                 if (decrypted != testMessage)
    00209                         throw 0;
    00210         }
    00211         catch (...)
    00212         {
    00213                 throw SelfTestFailure(encryptor.AlgorithmName() + ": pairwise consistency test failed");
    00214         }
    00215 }
    00216
    00217 void SignaturePairwiseConsistencyTest(const PK_Signer &signer, const PK_Verifier &verifier)
    00218 {
    00219         try
    00220         {
    00221 #ifdef OS_RNG_AVAILABLE
    00222                 AutoSeededX917RNG<DES_EDE3> rng;
    00223 #else
    00224                 RandomNumberGenerator &rng = NullRNG();
    00225 #endif
    00226
    00227                 StringSource(
    00228                         "test message",
    00229                         true,
    00230                         new SignerFilter(
    00231                                 rng,
    00232                                 signer,
    00233                                 new VerifierFilter(verifier, NULL, VerifierFilter::THROW_EXCEPTION),
    00234                                 true));
    00235         }
    00236         catch (...)
    00237         {
    00238                 throw SelfTestFailure(signer.AlgorithmName() + ": pairwise consistency test failed");
    00239         }
    00240 }
    00241
    00242 template <class SCHEME>
    00243 void SignaturePairwiseConsistencyTest(const char *key, SCHEME *dummy = NULL)
    00244 {
    00245         typename SCHEME::Signer signer(StringSource(key, true, new HexDecoder).Ref());
    00246         typename SCHEME::Verifier verifier(signer);
    00247
    00248         SignaturePairwiseConsistencyTest(signer, verifier);
    00249 }
    00250
    00251 MessageAuthenticationCode * NewIntegrityCheckingMAC()
    00252 {
    00253         byte key[] = {0x47, 0x1E, 0x33, 0x96, 0x65, 0xB1, 0x6A, 0xED, 0x0B, 0xF8, 0x6B, 0xFD, 0x01, 0x65, 0x05, 0xCC};
    00254         return new HMAC<SHA1>(key, sizeof(key));
    00255 }
    00256
    00257 bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModuleMac, SecByteBlock *pActualMac, unsigned long *pMacFileLocation)
    00258 {
    00259         std::auto_ptr<MessageAuthenticationCode> mac(NewIntegrityCheckingMAC());
    00260         unsigned int macSize = mac->DigestSize();
    00261
    00262         SecByteBlock tempMac;
    00263         SecByteBlock &actualMac = pActualMac ? *pActualMac : tempMac;
    00264         actualMac.resize(macSize);
    00265
    00266         unsigned long tempLocation;
    00267         unsigned long &macFileLocation = pMacFileLocation ? *pMacFileLocation : tempLocation;
    00268         macFileLocation = 0;
    00269
    00270         MeterFilter verifier(new HashFilter(*mac, new ArraySink(actualMac, actualMac.size())));
    00271 //      MeterFilter verifier(new FileSink("c:\\dt.tmp"));
    00272         std::ifstream moduleStream;
    00273
    00274 #ifdef CRYPTOPP_WIN32_AVAILABLE
    00275         HMODULE h;
    00276         {
    00277         char moduleFilenameBuf[MAX_PATH] = "";
    00278         if (moduleFilename == NULL)
    00279         {
    00280                 wchar_t wideModuleFilename[MAX_PATH];
    00281                 if (GetModuleFileNameW(s_hModule, wideModuleFilename, MAX_PATH) > 0)
    00282                 {
    00283                         moduleStream.open(wideModuleFilename, std::ios::in | std::ios::binary);
    00284                         h = GetModuleHandleW(wideModuleFilename);
    00285                 }
    00286                 else
    00287                 {
    00288                         GetModuleFileNameA(s_hModule, moduleFilenameBuf, MAX_PATH);
    00289                         moduleFilename = moduleFilenameBuf;
    00290                 }
    00291         }
    00292 #endif
    00293         if (moduleFilename != NULL)
    00294         {
    00295                         moduleStream.open(moduleFilename, std::ios::in | std::ios::binary);
    00296 #ifdef CRYPTOPP_WIN32_AVAILABLE
    00297                         h = GetModuleHandleA(moduleFilename);
    00298                         moduleFilename = NULL;
    00299         }
    00300 #endif
    00301         }
    00302
    00303         if (!moduleStream)
    00304         {
    00305 #ifdef CRYPTOPP_WIN32_AVAILABLE
    00306                 OutputDebugString("Crypto++ DLL integrity check failed. Cannot open file for reading.");
    00307 #endif
    00308                 return false;
    00309         }
    00310         FileStore file(moduleStream);
    00311
    00312 #ifdef CRYPTOPP_WIN32_AVAILABLE
    00313         // try to hash from memory first
    00314         const byte *memBase = (const byte *)h;
    00315         const IMAGE_DOS_HEADER *ph = (IMAGE_DOS_HEADER *)memBase;
    00316         const IMAGE_NT_HEADERS *phnt = (IMAGE_NT_HEADERS *)(memBase + ph->e_lfanew);
    00317         const IMAGE_SECTION_HEADER *phs = IMAGE_FIRST_SECTION(phnt);
    00318         DWORD nSections = phnt->FileHeader.NumberOfSections;
    00319         size_t currentFilePos = 0;
    00320
    00321         size_t checksumPos = (byte *)&phnt->OptionalHeader.CheckSum - memBase;
    00322         size_t checksumSize = sizeof(phnt->OptionalHeader.CheckSum);
    00323         size_t certificateTableDirectoryPos = (byte *)&phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] - memBase;
    00324         size_t certificateTableDirectorySize = sizeof(phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY]);
    00325         size_t certificateTablePos = phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress;
    00326         size_t certificateTableSize = phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY].Size;
    00327
    00328         verifier.AddRangeToSkip(0, checksumPos, checksumSize);
    00329         verifier.AddRangeToSkip(0, certificateTableDirectoryPos, certificateTableDirectorySize);
    00330         verifier.AddRangeToSkip(0, certificateTablePos, certificateTableSize);
    00331
    00332         while (nSections--)
    00333         {
    00334                 switch (phs->Characteristics)
    00335                 {
    00336                 default:
    00337                         break;
    00338                 case IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ:
    00339                 case IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ:
    00340                         unsigned int sectionSize = STDMIN(phs->SizeOfRawData, phs->Misc.VirtualSize);
    00341                         const byte *sectionMemStart = memBase + phs->VirtualAddress;
    00342                         unsigned int sectionFileStart = phs->PointerToRawData;
    00343                         size_t subSectionStart = 0, nextSubSectionStart;
    00344
    00345                         do
    00346                         {
    00347                                 const byte *subSectionMemStart = sectionMemStart + subSectionStart;
    00348                                 size_t subSectionFileStart = sectionFileStart + subSectionStart;
    00349                                 size_t subSectionSize = sectionSize - subSectionStart;
    00350                                 nextSubSectionStart = 0;
    00351
    00352                                 unsigned int entriesToReadFromDisk[] = {IMAGE_DIRECTORY_ENTRY_IMPORT, IMAGE_DIRECTORY_ENTRY_IAT};
    00353                                 for (unsigned int i=0; i<sizeof(entriesToReadFromDisk)/sizeof(entriesToReadFromDisk[0]); i++)
    00354                                 {
    00355                                         const IMAGE_DATA_DIRECTORY &entry = phnt->OptionalHeader.DataDirectory[entriesToReadFromDisk[i]];
    00356                                         const byte *entryMemStart = memBase + entry.VirtualAddress;
    00357                                         if (subSectionMemStart <= entryMemStart && entryMemStart < subSectionMemStart + subSectionSize)
    00358                                         {
    00359                                                 subSectionSize = entryMemStart - subSectionMemStart;
    00360                                                 nextSubSectionStart = entryMemStart - sectionMemStart + entry.Size;
    00361                                         }
    00362                                 }
    00363
    00364 #if defined(_MSC_VER) && _MSC_VER >= 14
    00365                                 // first byte of _CRT_DEBUGGER_HOOK gets modified in memory by the debugger invisibly, so read it from file
    00366                                 if (IsDebuggerPresent())
    00367                                 {
    00368                                         if (subSectionMemStart <= (byte *)&_CRT_DEBUGGER_HOOK && (byte *)&_CRT_DEBUGGER_HOOK < subSectionMemStart + subSectionSize)
    00369                                         {
    00370                                                 subSectionSize = (byte *)&_CRT_DEBUGGER_HOOK - subSectionMemStart;
    00371                                                 nextSubSectionStart = (byte *)&_CRT_DEBUGGER_HOOK - sectionMemStart + 1;
    00372                                         }
    00373                                 }
    00374 #endif
    00375
    00376                                 if (subSectionMemStart <= expectedModuleMac && expectedModuleMac < subSectionMemStart + subSectionSize)
    00377                                 {
    00378                                         // found stored MAC
    00379                                         macFileLocation = (unsigned long)(subSectionFileStart + (expectedModuleMac - subSectionMemStart));
    00380                                         verifier.AddRangeToSkip(0, macFileLocation, macSize);
    00381                                 }
    00382
    00383                                 file.TransferTo(verifier, subSectionFileStart - currentFilePos);
    00384                                 verifier.Put(subSectionMemStart, subSectionSize);
    00385                                 file.Skip(subSectionSize);
    00386                                 currentFilePos = subSectionFileStart + subSectionSize;
    00387                                 subSectionStart = nextSubSectionStart;
    00388                         } while (nextSubSectionStart != 0);
    00389                 }
    00390                 phs++;
    00391         }
    00392 #endif
    00393         file.TransferAllTo(verifier);
    00394
    00395 #ifdef CRYPTOPP_WIN32_AVAILABLE
    00396         // if that fails (could be caused by debug breakpoints or DLL base relocation modifying image in memory),
    00397         // hash from disk instead
    00398         if (memcmp(expectedModuleMac, actualMac, macSize) != 0)
    00399         {
    00400                 OutputDebugString("In memory integrity check failed. This may be caused by debug breakpoints or DLL relocation.\n");
    00401                 moduleStream.clear();
    00402                 moduleStream.seekg(0);
    00403                 verifier.Initialize(MakeParameters(Name::OutputBuffer(), ByteArrayParameter(actualMac, (unsigned int)actualMac.size())));
    00404 //              verifier.Initialize(MakeParameters(Name::OutputFileName(), (const char *)"c:\\dt2.tmp"));
    00405                 verifier.AddRangeToSkip(0, checksumPos, checksumSize);
    00406                 verifier.AddRangeToSkip(0, certificateTableDirectoryPos, certificateTableDirectorySize);
    00407                 verifier.AddRangeToSkip(0, certificateTablePos, certificateTableSize);
    00408                 verifier.AddRangeToSkip(0, macFileLocation, macSize);
    00409                 FileStore(moduleStream).TransferAllTo(verifier);
    00410         }
    00411 #endif
    00412
    00413         if (memcmp(expectedModuleMac, actualMac, macSize) == 0)
    00414                 return true;
    00415
    00416 #ifdef CRYPTOPP_WIN32_AVAILABLE
    00417         std::string hexMac;
    00418         HexEncoder(new StringSink(hexMac)).PutMessageEnd(actualMac, actualMac.size());
    00419         OutputDebugString((("Crypto++ DLL integrity check failed. Actual MAC is: " + hexMac) + "\n").c_str());
    00420 #endif
    00421         return false;
    00422 }
    00423
    00424 void DoPowerUpSelfTest(const char *moduleFilename, const byte *expectedModuleMac)
    00425 {
    00426         g_powerUpSelfTestStatus = POWER_UP_SELF_TEST_NOT_DONE;
    00427         SetPowerUpSelfTestInProgressOnThisThread(true);
    00428
    00429         try
    00430         {
    00431                 if (FIPS_140_2_ComplianceEnabled() || expectedModuleMac != NULL)
    00432                 {
    00433                         if (!IntegrityCheckModule(moduleFilename, expectedModuleMac, &g_actualMac, &g_macFileLocation))
    00434                                 throw 0;        // throw here so we break in the debugger, this will be caught right away
    00435                 }
    00436
    00437                 // algorithm tests
    00438
    00439                 X917RNG_KnownAnswerTest<DES_EDE3>(
    00440                         "48851090B4992453E83CDA86416534E53EA2FCE1A0B3A40C",                                             // key
    00441                         "7D00BD0A79F6B0F5",                                                                                                             // seed
    00442                         "0000000000000001",                                                                                                             // time vector
    00443                         "fdc31a6dd6b43aca81dfe8a696a2f9cf661955a44124a05033b7fff71b5b0341");    // output
    00444
    00445                 SymmetricEncryptionKnownAnswerTest<DES_EDE3>(
    00446                         "385D7189A5C3D485E1370AA5D408082B5CCCCB5E19F2D90E",
    00447                         "C141B5FCCD28DC8A",
    00448                         "6E1BD7C6120947A464A6AAB293A0F89A563D8D40D3461B68",
    00449                         "64EAAD4ACBB9CEAD6C7615E7C7E4792FE587D91F20C7D2F4",
    00450                         "6235A461AFD312973E3B4F7AA7D23E34E03371F8E8C376C9",
    00451                         "E26BA806A59B0330DE40CA38E77A3E494BE2B212F6DD624B",
    00452                         "E26BA806A59B03307DE2BCC25A08BA40A8BA335F5D604C62",
    00453                         "E26BA806A59B03303C62C2EFF32D3ACDD5D5F35EBCC53371");
    00454
    00455                 SymmetricEncryptionKnownAnswerTest<SKIPJACK>(
    00456                         "1555E5531C3A169B2D65",
    00457                         "6EC9795701F49864",
    00458                         "00AFA48E9621E52E8CBDA312660184EDDB1F33D9DACDA8DA",
    00459                         "DBEC73562EFCAEB56204EB8AE9557EBF77473FBB52D17CD1",
    00460                         "0C7B0B74E21F99B8F2C8DF37879F6C044967F42A796DCA8B",
    00461                         "79FDDA9724E36CC2E023E9A5C717A8A8A7FDA465CADCBF63",
    00462                         "79FDDA9724E36CC26CACBD83C1ABC06EAF5B249BE5B1E040",
    00463                         "79FDDA9724E36CC211B0AEC607B95A96BCDA318440B82F49");
    00464
    00465                 SymmetricEncryptionKnownAnswerTest<AES>(
    00466                         "2b7e151628aed2a6abf7158809cf4f3c",
    00467                         "000102030405060708090a0b0c0d0e0f",
    00468                         "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",     // plaintext
    00469                         "3ad77bb40d7a3660a89ecaf32466ef97f5d3d58503b9699de785895a96fdbaaf43b1cd7f598ece23881b00e3ed0306887b0c785e27e8ad3f8223207104725dd4", // ecb
    00470                         "7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7",     // cbc
    00471                         "3b3fd92eb72dad20333449f8e83cfb4ac8a64537a0b3a93fcde3cdad9f1ce58b26751f67a3cbb140b1808cf187a4f4dfc04b05357c5d1c0eeac4c66f9ff7f2e6", // cfb
    00472                         "3b3fd92eb72dad20333449f8e83cfb4a7789508d16918f03f53c52dac54ed8259740051e9c5fecf64344f7a82260edcc304c6528f659c77866a510d9c1d6ae5e", // ofb
    00473                         NULL);
    00474
    00475                 SymmetricEncryptionKnownAnswerTest<AES>(
    00476                         "2b7e151628aed2a6abf7158809cf4f3c",
    00477                         "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
    00478                         "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
    00479                         NULL,
    00480                         NULL,
    00481                         NULL,
    00482                         NULL,
    00483                         "874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee"); // ctr
    00484
    00485
    00486                 SecureHashKnownAnswerTest<SHA1>(
    00487                         "abc",
    00488                         "A9993E364706816ABA3E25717850C26C9CD0D89D");
    00489
    00490                 SecureHashKnownAnswerTest<SHA224>(
    00491                         "abc",
    00492                         "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7");
    00493
    00494                 SecureHashKnownAnswerTest<SHA256>(
    00495                         "abc",
    00496                         "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
    00497
    00498                 SecureHashKnownAnswerTest<SHA384>(
    00499                         "abc",
    00500                         "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7");
    00501
    00502                 SecureHashKnownAnswerTest<SHA512>(
    00503                         "abc",
    00504                         "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
    00505
    00506                 MAC_KnownAnswerTest<HMAC<SHA1> >(
    00507                         "303132333435363738393a3b3c3d3e3f40414243",
    00508                         "Sample #2",
    00509                         "0922d3405faa3d194f82a45830737d5cc6c75d24");
    00510
    00511                 const char *keyRSA1 =
    00512                         "30820150020100300d06092a864886f70d01010105000482013a3082013602010002400a66791dc6988168de7ab77419bb7fb0"
    00513                         "c001c62710270075142942e19a8d8c51d053b3e3782a1de5dc5af4ebe99468170114a1dfe67cdc9a9af55d655620bbab0203010001"
    00514                         "02400123c5b61ba36edb1d3679904199a89ea80c09b9122e1400c09adcf7784676d01d23356a7d44d6bd8bd50e94bfc723fa"
    00515                         "87d8862b75177691c11d757692df8881022033d48445c859e52340de704bcdda065fbb4058d740bd1d67d29e9c146c11cf61"
    00516                         "0220335e8408866b0fd38dc7002d3f972c67389a65d5d8306566d5c4f2a5aa52628b0220045ec90071525325d3d46db79695e9af"
    00517                         "acc4523964360e02b119baa366316241022015eb327360c7b60d12e5e2d16bdcd97981d17fba6b70db13b20b436e24eada590220"
    00518                         "2ca6366d72781dfa24d34a9a24cbc2ae927a9958af426563ff63fb11658a461d";
    00519
    00520                 const char *keyRSA2 =
    00521                         "30820273020100300D06092A864886F70D01010105000482025D3082025902010002818100D40AF9"
    00522                         "A2B713034249E5780056D70FC7DE75D76E44565AA6A6B8ED9646F3C19F9E254D72D7DE6E49DB2264"
    00523                         "0C1D05AB9E2A5F901D8F3FE1F7AE02CEE2ECCE54A40ABAE55A004692752E70725AEEE7CDEA67628A"
    00524                         "82A9239B4AB660C2BC56D9F01E90CBAAB9BF0FC8E17173CEFC5709A29391A7DDF3E0B758691AAF30"
    00525                         "725B292F4F020111027F18C0BA087D082C45D75D3594E0767E4820818EB35612B80CEAB8C880ACA5"
    00526                         "44B6876DFFEF85A576C0D45B551AFAA1FD63209CD745DF75C5A0F0B580296EA466CD0338207E4752"
    00527                         "FF4E7DB724D8AE18CE5CF4153BB94C27869FBB50E64F02546E4B02997A0B8623E64017CC770759C6"
    00528                         "695DB649EEFD829D688D441BCC4E7348F1024100EF86DD7AF3F32CDE8A9F6564E43A559A0C9F8BAD"
    00529                         "36CC25330548B347AC158A345631FA90F7B873C36EFFAE2F7823227A3F580B5DD18304D5932751E7"
    00530                         "43E9234F024100E2A039854B55688740E32A51DF4AF88613D91A371CF8DDD95D780A89D7CF2119A9"
    00531                         "54F1AC0F3DCDB2F6959926E6D9D37D8BC07A4C634DE6F16315BD5F0DAC340102407ECEEDB9903572"
    00532                         "1B76909F174BA6698DCA72953D957B22C0A871C8531EDE3A1BB52984A719BC010D1CA57A555DB83F"
    00533                         "6DE54CBAB932AEC652F38D497A6F3F30CF024100854F30E4FF232E6DADB2CD99926855F484255AB7"
    00534                         "01FBCDCB27EC426F33A7046972AA700ADBCA008763DF87440F52F4E070531AC385B55AAC1C2AE7DD"
    00535                         "8F9278F1024100C313F4AF9E4A9DE1253C21080CE524251560C111550772FD08690F13FBE658342E"
    00536                         "BD2D41C9DCB12374E871B1839E26CAE252E1AE3DAAD5F1EE1F42B4D0EE7581";
    00537
    00538                 SignatureKnownAnswerTest<RSASS<PKCS1v15, SHA1> >(
    00539                         keyRSA1,
    00540                         "Everyone gets Friday off.",
    00541                         "0610761F95FFD1B8F29DA34212947EC2AA0E358866A722F03CC3C41487ADC604A48FF54F5C6BEDB9FB7BD59F82D6E55D8F3174BA361B2214B2D74E8825E04E81");
    00542
    00543                 SignatureKnownAnswerTest<RSASS_ISO<SHA1> >(
    00544                         keyRSA2,
    00545                         "test",
    00546                         "32F6BA41C8930DE71EE67F2627172CC539EDE04267FDE03AC295E3C50311F26C3B275D3AF513AC96"
    00547                         "8EE493BAB7DA3A754661D1A7C4A0D1A2B7EE8B313AACD8CB8BFBC5C15EFB0EF15C86A9334A1E87AD"
    00548                         "291EB961B5CA0E84930429B28780816AA94F96FC2367B71E2D2E4866FA966795B147F00600E5207E"
    00549                         "2F189C883B37477C");
    00550
    00551                 SignaturePairwiseConsistencyTest<DSA>(
    00552                         " ");
    00553
    00554                 SignaturePairwiseConsistencyTest<ECDSA<EC2N, SHA1> >(
    00555                         "302D020100301006072A8648CE3D020106052B8104000404163014020101040F0070337065E1E196980A9D00E37211");
    00556
    00557                 SignaturePairwiseConsistencyTest<ECDSA<ECP, SHA1> >(
    00558                         "3039020100301306072A8648CE3D020106082A8648CE3D030101041F301D02010104182BB8A13C8B867010BD9471D9E81FDB01ABD0538C64D6249A");
    00559
    00560                 SignaturePairwiseConsistencyTest<RSASS<PSS, SHA1> >(keyRSA1);
    00561         }
    00562         catch (...)
    00563         {
    00564                 g_powerUpSelfTestStatus = POWER_UP_SELF_TEST_FAILED;
    00565                 goto done;
    00566         }
    00567
    00568         g_powerUpSelfTestStatus = POWER_UP_SELF_TEST_PASSED;
    00569
    00570 done:
    00571         SetPowerUpSelfTestInProgressOnThisThread(false);
    00572         return;
    00573 }
    00574
    00575 #ifdef CRYPTOPP_WIN32_AVAILABLE
    00576
    00577 void DoDllPowerUpSelfTest()
    00578 {
    00579         CryptoPP::DoPowerUpSelfTest(NULL, s_moduleMac);
    00580 }
    00581
    00582 #else
    00583
    00584 void DoDllPowerUpSelfTest()
    00585 {
    00586         throw NotImplemented("DoDllPowerUpSelfTest() only available on Windows");
    00587 }
    00588
    00589 #endif  // #ifdef CRYPTOPP_WIN32_AVAILABLE
    00590
    00591 NAMESPACE_END
    00592
    00593 #ifdef CRYPTOPP_WIN32_AVAILABLE
    00594
    00595 // DllMain needs to be in the global namespace
    00596 BOOL APIENTRY DllMain(HANDLE hModule,
    00597                       DWORD  ul_reason_for_call,
    00598                       LPVOID lpReserved)
    00599 {
    00600         if (ul_reason_for_call == DLL_PROCESS_ATTACH)
    00601         {
    00602                 CryptoPP::s_hModule = (HMODULE)hModule;
    00603                 CryptoPP::DoDllPowerUpSelfTest();
    00604         }
    00605     return TRUE;
    00606 }
    00607
    00608 #endif  // #ifdef CRYPTOPP_WIN32_AVAILABLE
    00609
    00610 #endif  // #ifndef CRYPTOPP_IMPORTS