Crypto++  5.6.3
Free C++ class library of cryptographic schemes
fipstest.cpp
1 // fipstest.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "config.h"
5 
6 #ifndef CRYPTOPP_IMPORTS
7 
8 #define CRYPTOPP_DEFAULT_NO_DLL
9 #include "dll.h"
10 #include "cryptlib.h"
11 #include "filters.h"
12 #include "smartptr.h"
13 #include "misc.h"
14 
15 // Simply disable CRYPTOPP_WIN32_AVAILABLE for Windows Phone and Windows Store apps
16 #ifdef CRYPTOPP_WIN32_AVAILABLE
17 # if defined(WINAPI_FAMILY)
18 # if !(WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP))
19 # undef CRYPTOPP_WIN32_AVAILABLE
20 # endif
21 # endif
22 #endif
23 
24 #ifdef CRYPTOPP_WIN32_AVAILABLE
25 #ifndef _WIN32_WINNT
26 #define _WIN32_WINNT 0x0400
27 #endif
28 
29 #include <windows.h>
30 
31 #if defined(_MSC_VER) && _MSC_VER >= 1400
32 #ifdef _M_IX86
33 #define _CRT_DEBUGGER_HOOK _crt_debugger_hook
34 #else
35 #define _CRT_DEBUGGER_HOOK __crt_debugger_hook
36 #endif
37 extern "C" {_CRTIMP void __cdecl _CRT_DEBUGGER_HOOK(int);}
38 #endif
39 #endif
40 
41 #include <iostream>
42 
43 #if CRYPTOPP_MSC_VERSION
44 # pragma warning(disable: 4100)
45 #endif
46 
47 NAMESPACE_BEGIN(CryptoPP)
48 
49 extern PowerUpSelfTestStatus g_powerUpSelfTestStatus;
50 SecByteBlock g_actualMac;
51 unsigned long g_macFileLocation = 0;
52 
53 // use a random dummy string here, to be searched/replaced later with the real MAC
54 static const byte s_moduleMac[CryptoPP::HMAC<CryptoPP::SHA1>::DIGESTSIZE] = CRYPTOPP_DUMMY_DLL_MAC;
55 CRYPTOPP_COMPILE_ASSERT(sizeof(s_moduleMac) == CryptoPP::SHA1::DIGESTSIZE);
56 
57 #ifdef CRYPTOPP_WIN32_AVAILABLE
58 static HMODULE s_hModule = NULL;
59 #endif
60 
61 const byte * CRYPTOPP_API GetActualMacAndLocation(unsigned int &macSize, unsigned int &fileLocation)
62 {
63  macSize = (unsigned int)g_actualMac.size();
64  fileLocation = g_macFileLocation;
65  return g_actualMac;
66 }
67 
68 void KnownAnswerTest(RandomNumberGenerator &rng, const char *output)
69 {
70  EqualityComparisonFilter comparison;
71 
72  RandomNumberStore(rng, strlen(output)/2).TransferAllTo(comparison, "0");
73  StringSource(output, true, new HexDecoder(new ChannelSwitch(comparison, "1")));
74 
75  comparison.ChannelMessageSeriesEnd("0");
76  comparison.ChannelMessageSeriesEnd("1");
77 }
78 
79 template <class CIPHER>
80 void X917RNG_KnownAnswerTest(
81  const char *key,
82  const char *seed,
83  const char *deterministicTimeVector,
84  const char *output,
85  CIPHER *dummy = NULL)
86 {
87  CRYPTOPP_UNUSED(dummy);
88 #ifdef OS_RNG_AVAILABLE
89  std::string decodedKey, decodedSeed, decodedDeterministicTimeVector;
90  StringSource(key, true, new HexDecoder(new StringSink(decodedKey)));
91  StringSource(seed, true, new HexDecoder(new StringSink(decodedSeed)));
92  StringSource(deterministicTimeVector, true, new HexDecoder(new StringSink(decodedDeterministicTimeVector)));
93 
94  AutoSeededX917RNG<CIPHER> rng(false, false);
95  rng.Reseed((const byte *)decodedKey.data(), decodedKey.size(), (const byte *)decodedSeed.data(), (const byte *)decodedDeterministicTimeVector.data());
96  KnownAnswerTest(rng, output);
97 #else
98  throw 0;
99 #endif
100 }
101 
102 void KnownAnswerTest(StreamTransformation &encryption, StreamTransformation &decryption, const char *plaintext, const char *ciphertext)
103 {
104  EqualityComparisonFilter comparison;
105 
106  StringSource(plaintext, true, new HexDecoder(new StreamTransformationFilter(encryption, new ChannelSwitch(comparison, "0"), StreamTransformationFilter::NO_PADDING)));
107  StringSource(ciphertext, true, new HexDecoder(new ChannelSwitch(comparison, "1")));
108 
109  StringSource(ciphertext, true, new HexDecoder(new StreamTransformationFilter(decryption, new ChannelSwitch(comparison, "0"), StreamTransformationFilter::NO_PADDING)));
110  StringSource(plaintext, true, new HexDecoder(new ChannelSwitch(comparison, "1")));
111 
112  comparison.ChannelMessageSeriesEnd("0");
113  comparison.ChannelMessageSeriesEnd("1");
114 }
115 
116 template <class CIPHER>
117 void SymmetricEncryptionKnownAnswerTest(
118  const char *key,
119  const char *hexIV,
120  const char *plaintext,
121  const char *ecb,
122  const char *cbc,
123  const char *cfb,
124  const char *ofb,
125  const char *ctr,
126  CIPHER *dummy = NULL)
127 {
128  CRYPTOPP_UNUSED(dummy);
129  std::string decodedKey;
130  StringSource(key, true, new HexDecoder(new StringSink(decodedKey)));
131 
132  typename CIPHER::Encryption encryption((const byte *)decodedKey.data(), decodedKey.size());
133  typename CIPHER::Decryption decryption((const byte *)decodedKey.data(), decodedKey.size());
134 
135  SecByteBlock iv(encryption.BlockSize());
136  StringSource(hexIV, true, new HexDecoder(new ArraySink(iv, iv.size())));
137 
138  if (ecb)
139  KnownAnswerTest(ECB_Mode_ExternalCipher::Encryption(encryption).Ref(), ECB_Mode_ExternalCipher::Decryption(decryption).Ref(), plaintext, ecb);
140  if (cbc)
141  KnownAnswerTest(CBC_Mode_ExternalCipher::Encryption(encryption, iv).Ref(), CBC_Mode_ExternalCipher::Decryption(decryption, iv).Ref(), plaintext, cbc);
142  if (cfb)
143  KnownAnswerTest(CFB_Mode_ExternalCipher::Encryption(encryption, iv).Ref(), CFB_Mode_ExternalCipher::Decryption(encryption, iv).Ref(), plaintext, cfb);
144  if (ofb)
145  KnownAnswerTest(OFB_Mode_ExternalCipher::Encryption(encryption, iv).Ref(), OFB_Mode_ExternalCipher::Decryption(encryption, iv).Ref(), plaintext, ofb);
146  if (ctr)
147  KnownAnswerTest(CTR_Mode_ExternalCipher::Encryption(encryption, iv).Ref(), CTR_Mode_ExternalCipher::Decryption(encryption, iv).Ref(), plaintext, ctr);
148 }
149 
150 void KnownAnswerTest(HashTransformation &hash, const char *message, const char *digest)
151 {
152  EqualityComparisonFilter comparison;
153  StringSource(digest, true, new HexDecoder(new ChannelSwitch(comparison, "1")));
154  StringSource(message, true, new HashFilter(hash, new ChannelSwitch(comparison, "0")));
155 
156  comparison.ChannelMessageSeriesEnd("0");
157  comparison.ChannelMessageSeriesEnd("1");
158 }
159 
160 template <class HASH>
161 void SecureHashKnownAnswerTest(const char *message, const char *digest, HASH *dummy = NULL)
162 {
163  CRYPTOPP_UNUSED(dummy);
164  HASH hash;
165  KnownAnswerTest(hash, message, digest);
166 }
167 
168 template <class MAC>
169 void MAC_KnownAnswerTest(const char *key, const char *message, const char *digest, MAC *dummy = NULL)
170 {
171  CRYPTOPP_UNUSED(dummy);
172  std::string decodedKey;
173  StringSource(key, true, new HexDecoder(new StringSink(decodedKey)));
174 
175  MAC mac((const byte *)decodedKey.data(), decodedKey.size());
176  KnownAnswerTest(mac, message, digest);
177 }
178 
179 template <class SCHEME>
180 void SignatureKnownAnswerTest(const char *key, const char *message, const char *signature, SCHEME *dummy = NULL)
181 {
182  typename SCHEME::Signer signer(StringSource(key, true, new HexDecoder).Ref());
183  typename SCHEME::Verifier verifier(signer);
184 
185  CRYPTOPP_UNUSED(dummy);
186  RandomPool rng;
187  EqualityComparisonFilter comparison;
188 
189  StringSource(message, true, new SignerFilter(rng, signer, new ChannelSwitch(comparison, "0")));
190  StringSource(signature, true, new HexDecoder(new ChannelSwitch(comparison, "1")));
191 
192  comparison.ChannelMessageSeriesEnd("0");
193  comparison.ChannelMessageSeriesEnd("1");
194 
196  StringSource(signature, true, new HexDecoder(new Redirector(verifierFilter, Redirector::DATA_ONLY)));
197  StringSource(message, true, new Redirector(verifierFilter));
198 }
199 
200 void EncryptionPairwiseConsistencyTest(const PK_Encryptor &encryptor, const PK_Decryptor &decryptor)
201 {
202  try
203  {
204  RandomPool rng;
205  const char *testMessage ="test message";
206  std::string ciphertext, decrypted;
207 
208  StringSource(
209  testMessage,
210  true,
211  new PK_EncryptorFilter(
212  rng,
213  encryptor,
214  new StringSink(ciphertext)));
215 
216  if (ciphertext == testMessage)
217  throw 0;
218 
219  StringSource(
220  ciphertext,
221  true,
222  new PK_DecryptorFilter(
223  rng,
224  decryptor,
225  new StringSink(decrypted)));
226 
227  if (decrypted != testMessage)
228  throw 0;
229  }
230  catch (...)
231  {
232  throw SelfTestFailure(encryptor.AlgorithmName() + ": pairwise consistency test failed");
233  }
234 }
235 
236 void SignaturePairwiseConsistencyTest(const PK_Signer &signer, const PK_Verifier &verifier)
237 {
238  try
239  {
240  RandomPool rng;
241 
242  StringSource(
243  "test message",
244  true,
245  new SignerFilter(
246  rng,
247  signer,
248  new VerifierFilter(verifier, NULL, VerifierFilter::THROW_EXCEPTION),
249  true));
250  }
251  catch (...)
252  {
253  throw SelfTestFailure(signer.AlgorithmName() + ": pairwise consistency test failed");
254  }
255 }
256 
257 template <class SCHEME>
258 void SignaturePairwiseConsistencyTest(const char *key, SCHEME *dummy = NULL)
259 {
260  typename SCHEME::Signer signer(StringSource(key, true, new HexDecoder).Ref());
261  typename SCHEME::Verifier verifier(signer);
262 
263  CRYPTOPP_UNUSED(dummy);
264  SignaturePairwiseConsistencyTest(signer, verifier);
265 }
266 
268 {
269  byte key[] = {0x47, 0x1E, 0x33, 0x96, 0x65, 0xB1, 0x6A, 0xED, 0x0B, 0xF8, 0x6B, 0xFD, 0x01, 0x65, 0x05, 0xCC};
270  return new HMAC<SHA1>(key, sizeof(key));
271 }
272 
273 bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModuleMac, SecByteBlock *pActualMac, unsigned long *pMacFileLocation)
274 {
276  unsigned int macSize = mac->DigestSize();
277 
278  SecByteBlock tempMac;
279  SecByteBlock &actualMac = pActualMac ? *pActualMac : tempMac;
280  actualMac.resize(macSize);
281 
282  unsigned long tempLocation = 0;
283  unsigned long &macFileLocation = pMacFileLocation ? *pMacFileLocation : tempLocation;
284  macFileLocation = 0;
285 
286  MeterFilter verifier(new HashFilter(*mac, new ArraySink(actualMac, actualMac.size())));
287 // MeterFilter verifier(new FileSink("c:\\dt.tmp"));
288  std::ifstream moduleStream;
289 
290 #ifdef CRYPTOPP_WIN32_AVAILABLE
291  HMODULE h = NULL;
292  {
293  char moduleFilenameBuf[MAX_PATH] = "";
294  if (moduleFilename == NULL)
295  {
296 #if (_MSC_VER >= 1400 && !defined(_STLPORT_VERSION)) // ifstream doesn't support wide filename on other compilers
297  wchar_t wideModuleFilename[MAX_PATH];
298  if (GetModuleFileNameW(s_hModule, wideModuleFilename, MAX_PATH) > 0)
299  {
300  moduleStream.open(wideModuleFilename, std::ios::in | std::ios::binary);
301  h = GetModuleHandleW(wideModuleFilename);
302  }
303  else
304 #endif
305  {
306  GetModuleFileNameA(s_hModule, moduleFilenameBuf, MAX_PATH);
307  moduleFilename = moduleFilenameBuf;
308  }
309  }
310 #endif
311  if (moduleFilename != NULL)
312  {
313  moduleStream.open(moduleFilename, std::ios::in | std::ios::binary);
314 #ifdef CRYPTOPP_WIN32_AVAILABLE
315  h = GetModuleHandleA(moduleFilename);
316  moduleFilename = NULL;
317  }
318 #endif
319  }
320 
321  if (!moduleStream)
322  {
323 #ifdef CRYPTOPP_WIN32_AVAILABLE
324  OutputDebugString("Crypto++ DLL integrity check failed. Cannot open file for reading.");
325 #endif
326  return false;
327  }
328  FileStore file(moduleStream);
329 
330 #ifdef CRYPTOPP_WIN32_AVAILABLE
331  // try to hash from memory first
332  const byte *memBase = (const byte *)h;
333  const IMAGE_DOS_HEADER *ph = (IMAGE_DOS_HEADER *)memBase;
334  const IMAGE_NT_HEADERS *phnt = (IMAGE_NT_HEADERS *)(memBase + ph->e_lfanew);
335  const IMAGE_SECTION_HEADER *phs = IMAGE_FIRST_SECTION(phnt);
336  DWORD nSections = phnt->FileHeader.NumberOfSections;
337  size_t currentFilePos = 0;
338 
339  size_t checksumPos = (byte *)&phnt->OptionalHeader.CheckSum - memBase;
340  size_t checksumSize = sizeof(phnt->OptionalHeader.CheckSum);
341  size_t certificateTableDirectoryPos = (byte *)&phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] - memBase;
342  size_t certificateTableDirectorySize = sizeof(phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY]);
343  size_t certificateTablePos = phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress;
344  size_t certificateTableSize = phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY].Size;
345 
346  verifier.AddRangeToSkip(0, checksumPos, checksumSize);
347  verifier.AddRangeToSkip(0, certificateTableDirectoryPos, certificateTableDirectorySize);
348  verifier.AddRangeToSkip(0, certificateTablePos, certificateTableSize);
349 
350  while (nSections--)
351  {
352  switch (phs->Characteristics)
353  {
354  default:
355  break;
356  case IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ:
357  case IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ:
358  unsigned int sectionSize = STDMIN(phs->SizeOfRawData, phs->Misc.VirtualSize);
359  const byte *sectionMemStart = memBase + phs->VirtualAddress;
360  unsigned int sectionFileStart = phs->PointerToRawData;
361  size_t subSectionStart = 0, nextSubSectionStart;
362 
363  do
364  {
365  const byte *subSectionMemStart = sectionMemStart + subSectionStart;
366  size_t subSectionFileStart = sectionFileStart + subSectionStart;
367  size_t subSectionSize = sectionSize - subSectionStart;
368  nextSubSectionStart = 0;
369 
370  unsigned int entriesToReadFromDisk[] = {IMAGE_DIRECTORY_ENTRY_IMPORT, IMAGE_DIRECTORY_ENTRY_IAT};
371  for (unsigned int i=0; i<sizeof(entriesToReadFromDisk)/sizeof(entriesToReadFromDisk[0]); i++)
372  {
373  const IMAGE_DATA_DIRECTORY &entry = phnt->OptionalHeader.DataDirectory[entriesToReadFromDisk[i]];
374  const byte *entryMemStart = memBase + entry.VirtualAddress;
375  if (subSectionMemStart <= entryMemStart && entryMemStart < subSectionMemStart + subSectionSize)
376  {
377  subSectionSize = entryMemStart - subSectionMemStart;
378  nextSubSectionStart = entryMemStart - sectionMemStart + entry.Size;
379  }
380  }
381 
382 #if defined(_MSC_VER) && _MSC_VER >= 1400
383  // first byte of _CRT_DEBUGGER_HOOK gets modified in memory by the debugger invisibly, so read it from file
384  if (IsDebuggerPresent())
385  {
386  if (subSectionMemStart <= (byte *)&_CRT_DEBUGGER_HOOK && (byte *)&_CRT_DEBUGGER_HOOK < subSectionMemStart + subSectionSize)
387  {
388  subSectionSize = (byte *)&_CRT_DEBUGGER_HOOK - subSectionMemStart;
389  nextSubSectionStart = (byte *)&_CRT_DEBUGGER_HOOK - sectionMemStart + 1;
390  }
391  }
392 #endif
393 
394  if (subSectionMemStart <= expectedModuleMac && expectedModuleMac < subSectionMemStart + subSectionSize)
395  {
396  // found stored MAC
397  macFileLocation = (unsigned long)(subSectionFileStart + (expectedModuleMac - subSectionMemStart));
398  verifier.AddRangeToSkip(0, macFileLocation, macSize);
399  }
400 
401  file.TransferTo(verifier, subSectionFileStart - currentFilePos);
402  verifier.Put(subSectionMemStart, subSectionSize);
403  file.Skip(subSectionSize);
404  currentFilePos = subSectionFileStart + subSectionSize;
405  subSectionStart = nextSubSectionStart;
406  } while (nextSubSectionStart != 0);
407  }
408  phs++;
409  }
410 #endif
411  file.TransferAllTo(verifier);
412 
413 #ifdef CRYPTOPP_WIN32_AVAILABLE
414  // if that fails (could be caused by debug breakpoints or DLL base relocation modifying image in memory),
415  // hash from disk instead
416  if (!VerifyBufsEqual(expectedModuleMac, actualMac, macSize))
417  {
418  OutputDebugString("In memory integrity check failed. This may be caused by debug breakpoints or DLL relocation.\n");
419  moduleStream.clear();
420  moduleStream.seekg(0);
421  verifier.Initialize(MakeParameters(Name::OutputBuffer(), ByteArrayParameter(actualMac, (unsigned int)actualMac.size())));
422 // verifier.Initialize(MakeParameters(Name::OutputFileName(), (const char *)"c:\\dt2.tmp"));
423  verifier.AddRangeToSkip(0, checksumPos, checksumSize);
424  verifier.AddRangeToSkip(0, certificateTableDirectoryPos, certificateTableDirectorySize);
425  verifier.AddRangeToSkip(0, certificateTablePos, certificateTableSize);
426  verifier.AddRangeToSkip(0, macFileLocation, macSize);
427  FileStore(moduleStream).TransferAllTo(verifier);
428  }
429 #endif
430 
431  if (VerifyBufsEqual(expectedModuleMac, actualMac, macSize))
432  return true;
433 
434 #ifdef CRYPTOPP_WIN32_AVAILABLE
435  std::string hexMac;
436  HexEncoder(new StringSink(hexMac)).PutMessageEnd(actualMac, actualMac.size());
437  OutputDebugString((("Crypto++ DLL integrity check failed. Actual MAC is: " + hexMac) + "\n").c_str());
438 #endif
439  return false;
440 }
441 
442 void DoPowerUpSelfTest(const char *moduleFilename, const byte *expectedModuleMac)
443 {
444  g_powerUpSelfTestStatus = POWER_UP_SELF_TEST_NOT_DONE;
445  SetPowerUpSelfTestInProgressOnThisThread(true);
446 
447  try
448  {
449  if (FIPS_140_2_ComplianceEnabled() || expectedModuleMac != NULL)
450  {
451  if (!IntegrityCheckModule(moduleFilename, expectedModuleMac, &g_actualMac, &g_macFileLocation))
452  throw 0; // throw here so we break in the debugger, this will be caught right away
453  }
454 
455  // algorithm tests
456 
457  X917RNG_KnownAnswerTest<AES>(
458  "2b7e151628aed2a6abf7158809cf4f3c", // key
459  "000102030405060708090a0b0c0d0e0f", // seed
460  "00000000000000000000000000000001", // time vector
461  "D176EDD27493B0395F4D10546232B0693DC7061C03C3A554F09CECF6F6B46D945A"); // output
462 
463  SymmetricEncryptionKnownAnswerTest<DES_EDE3>(
464  "385D7189A5C3D485E1370AA5D408082B5CCCCB5E19F2D90E",
465  "C141B5FCCD28DC8A",
466  "6E1BD7C6120947A464A6AAB293A0F89A563D8D40D3461B68",
467  "64EAAD4ACBB9CEAD6C7615E7C7E4792FE587D91F20C7D2F4",
468  "6235A461AFD312973E3B4F7AA7D23E34E03371F8E8C376C9",
469  "E26BA806A59B0330DE40CA38E77A3E494BE2B212F6DD624B",
470  "E26BA806A59B03307DE2BCC25A08BA40A8BA335F5D604C62",
471  "E26BA806A59B03303C62C2EFF32D3ACDD5D5F35EBCC53371");
472 
473  SymmetricEncryptionKnownAnswerTest<SKIPJACK>(
474  "1555E5531C3A169B2D65",
475  "6EC9795701F49864",
476  "00AFA48E9621E52E8CBDA312660184EDDB1F33D9DACDA8DA",
477  "DBEC73562EFCAEB56204EB8AE9557EBF77473FBB52D17CD1",
478  "0C7B0B74E21F99B8F2C8DF37879F6C044967F42A796DCA8B",
479  "79FDDA9724E36CC2E023E9A5C717A8A8A7FDA465CADCBF63",
480  "79FDDA9724E36CC26CACBD83C1ABC06EAF5B249BE5B1E040",
481  "79FDDA9724E36CC211B0AEC607B95A96BCDA318440B82F49");
482 
483  SymmetricEncryptionKnownAnswerTest<AES>(
484  "2b7e151628aed2a6abf7158809cf4f3c",
485  "000102030405060708090a0b0c0d0e0f",
486  "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", // plaintext
487  "3ad77bb40d7a3660a89ecaf32466ef97f5d3d58503b9699de785895a96fdbaaf43b1cd7f598ece23881b00e3ed0306887b0c785e27e8ad3f8223207104725dd4", // ecb
488  "7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7", // cbc
489  "3b3fd92eb72dad20333449f8e83cfb4ac8a64537a0b3a93fcde3cdad9f1ce58b26751f67a3cbb140b1808cf187a4f4dfc04b05357c5d1c0eeac4c66f9ff7f2e6", // cfb
490  "3b3fd92eb72dad20333449f8e83cfb4a7789508d16918f03f53c52dac54ed8259740051e9c5fecf64344f7a82260edcc304c6528f659c77866a510d9c1d6ae5e", // ofb
491  NULL);
492 
493  SymmetricEncryptionKnownAnswerTest<AES>(
494  "2b7e151628aed2a6abf7158809cf4f3c",
495  "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
496  "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
497  NULL,
498  NULL,
499  NULL,
500  NULL,
501  "874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee"); // ctr
502 
503 
504  SecureHashKnownAnswerTest<SHA1>(
505  "abc",
506  "A9993E364706816ABA3E25717850C26C9CD0D89D");
507 
508  SecureHashKnownAnswerTest<SHA224>(
509  "abc",
510  "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7");
511 
512  SecureHashKnownAnswerTest<SHA256>(
513  "abc",
514  "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
515 
516  SecureHashKnownAnswerTest<SHA384>(
517  "abc",
518  "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7");
519 
520  SecureHashKnownAnswerTest<SHA512>(
521  "abc",
522  "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
523 
524  MAC_KnownAnswerTest<HMAC<SHA1> >(
525  "303132333435363738393a3b3c3d3e3f40414243",
526  "Sample #2",
527  "0922d3405faa3d194f82a45830737d5cc6c75d24");
528 
529  const char *keyRSA1 =
530  "30820150020100300d06092a864886f70d01010105000482013a3082013602010002400a66791dc6988168de7ab77419bb7fb0"
531  "c001c62710270075142942e19a8d8c51d053b3e3782a1de5dc5af4ebe99468170114a1dfe67cdc9a9af55d655620bbab0203010001"
532  "02400123c5b61ba36edb1d3679904199a89ea80c09b9122e1400c09adcf7784676d01d23356a7d44d6bd8bd50e94bfc723fa"
533  "87d8862b75177691c11d757692df8881022033d48445c859e52340de704bcdda065fbb4058d740bd1d67d29e9c146c11cf61"
534  "0220335e8408866b0fd38dc7002d3f972c67389a65d5d8306566d5c4f2a5aa52628b0220045ec90071525325d3d46db79695e9af"
535  "acc4523964360e02b119baa366316241022015eb327360c7b60d12e5e2d16bdcd97981d17fba6b70db13b20b436e24eada590220"
536  "2ca6366d72781dfa24d34a9a24cbc2ae927a9958af426563ff63fb11658a461d";
537 
538  const char *keyRSA2 =
539  "30820273020100300D06092A864886F70D01010105000482025D3082025902010002818100D40AF9"
540  "A2B713034249E5780056D70FC7DE75D76E44565AA6A6B8ED9646F3C19F9E254D72D7DE6E49DB2264"
541  "0C1D05AB9E2A5F901D8F3FE1F7AE02CEE2ECCE54A40ABAE55A004692752E70725AEEE7CDEA67628A"
542  "82A9239B4AB660C2BC56D9F01E90CBAAB9BF0FC8E17173CEFC5709A29391A7DDF3E0B758691AAF30"
543  "725B292F4F020111027F18C0BA087D082C45D75D3594E0767E4820818EB35612B80CEAB8C880ACA5"
544  "44B6876DFFEF85A576C0D45B551AFAA1FD63209CD745DF75C5A0F0B580296EA466CD0338207E4752"
545  "FF4E7DB724D8AE18CE5CF4153BB94C27869FBB50E64F02546E4B02997A0B8623E64017CC770759C6"
546  "695DB649EEFD829D688D441BCC4E7348F1024100EF86DD7AF3F32CDE8A9F6564E43A559A0C9F8BAD"
547  "36CC25330548B347AC158A345631FA90F7B873C36EFFAE2F7823227A3F580B5DD18304D5932751E7"
548  "43E9234F024100E2A039854B55688740E32A51DF4AF88613D91A371CF8DDD95D780A89D7CF2119A9"
549  "54F1AC0F3DCDB2F6959926E6D9D37D8BC07A4C634DE6F16315BD5F0DAC340102407ECEEDB9903572"
550  "1B76909F174BA6698DCA72953D957B22C0A871C8531EDE3A1BB52984A719BC010D1CA57A555DB83F"
551  "6DE54CBAB932AEC652F38D497A6F3F30CF024100854F30E4FF232E6DADB2CD99926855F484255AB7"
552  "01FBCDCB27EC426F33A7046972AA700ADBCA008763DF87440F52F4E070531AC385B55AAC1C2AE7DD"
553  "8F9278F1024100C313F4AF9E4A9DE1253C21080CE524251560C111550772FD08690F13FBE658342E"
554  "BD2D41C9DCB12374E871B1839E26CAE252E1AE3DAAD5F1EE1F42B4D0EE7581";
555 
556  SignatureKnownAnswerTest<RSASS<PKCS1v15, SHA1> >(
557  keyRSA1,
558  "Everyone gets Friday off.",
559  "0610761F95FFD1B8F29DA34212947EC2AA0E358866A722F03CC3C41487ADC604A48FF54F5C6BEDB9FB7BD59F82D6E55D8F3174BA361B2214B2D74E8825E04E81");
560 
561  SignatureKnownAnswerTest<RSASS_ISO<SHA1> >(
562  keyRSA2,
563  "test",
564  "32F6BA41C8930DE71EE67F2627172CC539EDE04267FDE03AC295E3C50311F26C3B275D3AF513AC96"
565  "8EE493BAB7DA3A754661D1A7C4A0D1A2B7EE8B313AACD8CB8BFBC5C15EFB0EF15C86A9334A1E87AD"
566  "291EB961B5CA0E84930429B28780816AA94F96FC2367B71E2D2E4866FA966795B147F00600E5207E"
567  "2F189C883B37477C");
568 
569  SignaturePairwiseConsistencyTest<DSA>(
570  "3082014A0201003082012B06072A8648CE3804013082011E02818100F468699A6F6EBCC0120D3B34C8E007F125EC7D81F763B8D0F33869AE3BD6B9F2ECCC7DF34DF84C0307449E9B85D30D57194BCCEB310F48141914DD13A077AAF9B624A6CBE666BBA1D7EBEA95B5BA6F54417FD5D4E4220C601E071D316A24EA814E8B0122DBF47EE8AEEFD319EBB01DD95683F10DBB4FEB023F8262A07EAEB7FD02150082AD4E034DA6EEACDFDAE68C36F2BAD614F9E53B02818071AAF73361A26081529F7D84078ADAFCA48E031DB54AD57FB1A833ADBD8672328AABAA0C756247998D7A5B10DACA359D231332CE8120B483A784FE07D46EEBFF0D7D374A10691F78653E6DC29E27CCB1B174923960DFE5B959B919B2C3816C19251832AFD8E35D810E598F82877ABF7D40A041565168BD7F0E21E3FE2A8D8C1C0416021426EBA66E846E755169F84A1DA981D86502405DDF");
571 
572  SignaturePairwiseConsistencyTest<ECDSA<EC2N, SHA1> >(
573  "302D020100301006072A8648CE3D020106052B8104000404163014020101040F0070337065E1E196980A9D00E37211");
574 
575  SignaturePairwiseConsistencyTest<ECDSA<ECP, SHA1> >(
576  "3039020100301306072A8648CE3D020106082A8648CE3D030101041F301D02010104182BB8A13C8B867010BD9471D9E81FDB01ABD0538C64D6249A");
577 
578  SignaturePairwiseConsistencyTest<RSASS<PSS, SHA1> >(keyRSA1);
579  }
580  catch (...)
581  {
582  g_powerUpSelfTestStatus = POWER_UP_SELF_TEST_FAILED;
583  goto done;
584  }
585 
586  g_powerUpSelfTestStatus = POWER_UP_SELF_TEST_PASSED;
587 
588 done:
589  SetPowerUpSelfTestInProgressOnThisThread(false);
590  return;
591 }
592 
593 #ifdef CRYPTOPP_WIN32_AVAILABLE
594 
596 {
597  CryptoPP::DoPowerUpSelfTest(NULL, s_moduleMac);
598 }
599 
600 #else
601 
603 {
604  throw NotImplemented("DoDllPowerUpSelfTest() only available on Windows");
605 }
606 
607 #endif // #ifdef CRYPTOPP_WIN32_AVAILABLE
608 
609 NAMESPACE_END
610 
611 #ifdef CRYPTOPP_WIN32_AVAILABLE
612 
613 // DllMain needs to be in the global namespace
614 BOOL APIENTRY DllMain(HANDLE hModule,
615  DWORD dwReason,
616  LPVOID /*lpReserved*/)
617 {
618  if (dwReason == DLL_PROCESS_ATTACH)
619  {
620  CryptoPP::s_hModule = (HMODULE)hModule;
622  }
623  return TRUE;
624 }
625 
626 #endif // #ifdef CRYPTOPP_WIN32_AVAILABLE
627 
628 #endif // #ifndef CRYPTOPP_IMPORTS
OFB block cipher mode of operation.
Definition: modes.h:302
The self tests were executed via DoPowerUpSelfTest() or DoDllPowerUpSelfTest(), and the result was su...
Definition: fips140.h:47
Interface for message authentication codes.
Definition: cryptlib.h:1107
Randomness Pool.
Definition: randpool.h:25
Filter wrapper for PK_Verifier.
Definition: filters.h:694
Utility functions for the Crypto++ library.
void resize(size_type newSize)
Change size and preserve contents.
Definition: secblock.h:708
Converts given data to base 16.
Definition: hex.h:16
Interface for public-key signers.
Definition: cryptlib.h:2534
Interface for public-key encryptors.
Definition: cryptlib.h:2342
Pass data only.
Definition: filters.h:763
Decode base 16 data back to bytes.
Definition: hex.h:36
Abstract base classes that provide a uniform interface to this library.
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:527
Indicates the filter should throw a HashVerificationFailed if a failure is encountered.
Definition: filters.h:720
The self tests were executed via DoPowerUpSelfTest() or DoDllPowerUpSelfTest(), but the result was fa...
Definition: fips140.h:44
Classes for automatic resource management.
Filter wrapper for PK_Signer.
Definition: filters.h:668
Library configuration file.
Interface for random number generators.
Definition: cryptlib.h:1186
SecBlock typedef.
Definition: secblock.h:734
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:104
Route input to different and/or multiple channels based on channel ID.
Definition: channels.h:92
bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModuleMac, SecByteBlock *pActualMac=NULL, unsigned long *pMacFileLocation=NULL)
Verifies the MAC on the module.
Definition: fipstest.cpp:273
Pointer that overloads operator ->
Definition: smartptr.h:39
bool FIPS_140_2_ComplianceEnabled()
Determines whether the library provides FIPS validated cryptography.
Definition: fips140.cpp:29
Copy input to a memory buffer.
Definition: filters.h:1000
lword TransferTo(BufferedTransformation &target, lword transferMax=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL)
move transferMax bytes of the buffered output to target as input
Definition: cryptlib.h:1656
Interface for public-key decryptors.
Definition: cryptlib.h:2378
A method was called which was not implemented.
Definition: cryptlib.h:203
Filter wrapper for HashTransformation.
Definition: filters.h:508
RNG-based implementation of Source interface.
Definition: filters.h:1081
lword Skip(lword skipMax=ULONG_MAX)
Discard skipMax bytes from the output buffer.
Definition: files.cpp:166
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:554
Exception thrown when a crypto algorithm is used after a self test fails.
Definition: fips140.h:23
bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
Marks the end of a series of messages on a channel.
Definition: mqueue.cpp:135
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:106
SHA-1
Definition: sha.h:21
virtual std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: cryptlib.h:518
Filter wrapper for PK_Decryptor.
Definition: filters.h:927
MessageAuthenticationCode * NewIntegrityCheckingMAC()
Class object that calculates the MAC on the module.
Definition: fipstest.cpp:267
void DoDllPowerUpSelfTest()
Performs the power-up self test on the DLL.
Definition: fipstest.cpp:595
Automatically Seeded X9.17 RNG.
Definition: osrng.h:178
Interface for the data processing portion of stream ciphers.
Definition: cryptlib.h:816
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:431
Redirect input to another BufferedTransformation without owning it.
Definition: filters.h:755
Implementation of BufferedTransformation's attachment interface.
file-based implementation of Store interface
Definition: files.h:15
The self tests have not been performed.
Definition: fips140.h:41
const char * OutputBuffer()
ByteArrayParameter.
Definition: argnames.h:56
Filter wrapper for PK_Encryptor.
Definition: filters.h:912
Filter wrapper for StreamTransformation.
Definition: filters.h:476
#define CRYPTOPP_DUMMY_DLL_MAC
The placeholder used prior to embedding the actual MAC in the module.
Definition: fips140.h:109
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
void TransferAllTo(BufferedTransformation &target, const std::string &channel=DEFAULT_CHANNEL)
Transfer all bytes from this object to another BufferedTransformation.
Definition: cryptlib.h:1757
Interface for public-key signature verifiers.
Definition: cryptlib.h:2601
HMAC.
Definition: hmac.h:50
void DoPowerUpSelfTest(const char *moduleFilename, const byte *expectedModuleMac)
Performs the power-up self test.
Definition: fipstest.cpp:442
PowerUpSelfTestStatus
Status of the power-up self test.
Definition: fips140.h:38
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:922
bool VerifyBufsEqual(const byte *buf1, const byte *buf2, size_t count)
Performs a near constant-time comparison of two equally sized buffers.
Definition: misc.cpp:96
Measure how many bytes and messages pass through the filter.
Definition: filters.h:220
size_t PutMessageEnd(const byte *inString, size_t length, int propagation=-1, bool blocking=true)
Input multiple bytes for processing and signal the end of a message.
Definition: cryptlib.h:1449
A filter that checks messages on two channels for equality.
Definition: mqueue.h:71
No padding added to a block.
Definition: filters.h:461
Crypto++ library namespace.
Indicates the signature is at the beginning of the message (i.e., concatenation of signature+message)...
Definition: filters.h:712
Functions and definitions required for building the FIPS-140 DLL on Windows.