Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.6 KiB

  1. #ifndef CRYPTOPP_DLL_ONLY
  2. # define CRYPTOPP_DEFAULT_NO_DLL
  3. #endif
  4. #include "dll.h"
  5. #include "cryptlib.h"
  6. #include "filters.h"
  7. USING_NAMESPACE(CryptoPP)
  8. USING_NAMESPACE(std)
  9. void FIPS140_SampleApplication()
  10. {
  11. if (!FIPS_140_2_ComplianceEnabled())
  12. {
  13. cerr << "FIPS 140-2 compliance was turned off at compile time.\n";
  14. abort();
  15. }
  16. // check self test status
  17. if (GetPowerUpSelfTestStatus() != POWER_UP_SELF_TEST_PASSED)
  18. {
  19. cerr << "Automatic power-up self test failed.\n";
  20. abort();
  21. }
  22. cout << "0. Automatic power-up self test passed.\n";
  23. // simulate a power-up self test error
  24. SimulatePowerUpSelfTestFailure();
  25. try
  26. {
  27. // trying to use a crypto algorithm after power-up self test error will result in an exception
  28. AES::Encryption aes;
  29. // should not be here
  30. cerr << "Use of AES failed to cause an exception after power-up self test error.\n";
  31. abort();
  32. }
  33. catch (SelfTestFailure &e)
  34. {
  35. cout << "1. Caught expected exception when simulating self test failure. Exception message follows: ";
  36. cout << e.what() << endl;
  37. }
  38. // clear the self test error state and redo power-up self test
  39. DoDllPowerUpSelfTest();
  40. if (GetPowerUpSelfTestStatus() != POWER_UP_SELF_TEST_PASSED)
  41. {
  42. cerr << "Re-do power-up self test failed.\n";
  43. abort();
  44. }
  45. cout << "2. Re-do power-up self test passed.\n";
  46. // encrypt and decrypt
  47. const byte key[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
  48. const byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
  49. const byte plaintext[] = { // "Now is the time for all " without tailing 0
  50. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  51. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  52. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};
  53. byte ciphertext[24];
  54. byte decrypted[24];
  55. CFB_FIPS_Mode<DES_EDE3>::Encryption encryption_DES_EDE3_CFB;
  56. encryption_DES_EDE3_CFB.SetKeyWithIV(key, sizeof(key), iv);
  57. encryption_DES_EDE3_CFB.ProcessString(ciphertext, plaintext, 24);
  58. CFB_FIPS_Mode<DES_EDE3>::Decryption decryption_DES_EDE3_CFB;
  59. decryption_DES_EDE3_CFB.SetKeyWithIV(key, sizeof(key), iv);
  60. decryption_DES_EDE3_CFB.ProcessString(decrypted, ciphertext, 24);
  61. if (memcmp(plaintext, decrypted, 24) != 0)
  62. {
  63. cerr << "DES-EDE3-CFB Encryption/decryption failed.\n";
  64. abort();
  65. }
  66. cout << "3. DES-EDE3-CFB Encryption/decryption succeeded.\n";
  67. // hash
  68. const byte message[] = {'a', 'b', 'c'};
  69. const byte expectedDigest[] = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};
  70. byte digest[20];
  71. SHA1 sha;
  72. sha.Update(message, 3);
  73. sha.Final(digest);
  74. if (memcmp(digest, expectedDigest, 20) != 0)
  75. {
  76. cerr << "SHA-1 hash failed.\n";
  77. abort();
  78. }
  79. cout << "4. SHA-1 hash succeeded.\n";
  80. // create auto-seeded X9.17 RNG object, if available
  81. #ifdef OS_RNG_AVAILABLE
  82. AutoSeededX917RNG<AES> rng;
  83. #else
  84. // this is used to allow this function to compile on platforms that don't have auto-seeded RNGs
  85. RandomNumberGenerator &rng(NullRNG());
  86. #endif
  87. // generate DSA key
  88. DSA::PrivateKey dsaPrivateKey;
  89. dsaPrivateKey.GenerateRandomWithKeySize(rng, 1024);
  90. DSA::PublicKey dsaPublicKey;
  91. dsaPublicKey.AssignFrom(dsaPrivateKey);
  92. if (!dsaPrivateKey.Validate(rng, 3) || !dsaPublicKey.Validate(rng, 3))
  93. {
  94. cerr << "DSA key generation failed.\n";
  95. abort();
  96. }
  97. cout << "5. DSA key generation succeeded.\n";
  98. // encode DSA key
  99. std::string encodedDsaPublicKey, encodedDsaPrivateKey;
  100. dsaPublicKey.DEREncode(StringSink(encodedDsaPublicKey).Ref());
  101. dsaPrivateKey.DEREncode(StringSink(encodedDsaPrivateKey).Ref());
  102. // decode DSA key
  103. DSA::PrivateKey decodedDsaPrivateKey;
  104. decodedDsaPrivateKey.BERDecode(StringStore(encodedDsaPrivateKey).Ref());
  105. DSA::PublicKey decodedDsaPublicKey;
  106. decodedDsaPublicKey.BERDecode(StringStore(encodedDsaPublicKey).Ref());
  107. if (!decodedDsaPrivateKey.Validate(rng, 3) || !decodedDsaPublicKey.Validate(rng, 3))
  108. {
  109. cerr << "DSA key encode/decode failed.\n";
  110. abort();
  111. }
  112. cout << "6. DSA key encode/decode succeeded.\n";
  113. // sign and verify
  114. byte signature[40];
  115. DSA::Signer signer(dsaPrivateKey);
  116. assert(signer.SignatureLength() == 40);
  117. signer.SignMessage(rng, message, 3, signature);
  118. DSA::Verifier verifier(dsaPublicKey);
  119. if (!verifier.VerifyMessage(message, 3, signature, sizeof(signature)))
  120. {
  121. cerr << "DSA signature and verification failed.\n";
  122. abort();
  123. }
  124. cout << "7. DSA signature and verification succeeded.\n";
  125. // try to verify an invalid signature
  126. signature[0] ^= 1;
  127. if (verifier.VerifyMessage(message, 3, signature, sizeof(signature)))
  128. {
  129. cerr << "DSA signature verification failed to detect bad signature.\n";
  130. abort();
  131. }
  132. cout << "8. DSA signature verification successfully detected bad signature.\n";
  133. // try to use an invalid key length
  134. try
  135. {
  136. ECB_Mode<DES_EDE3>::Encryption encryption_DES_EDE3_ECB;
  137. encryption_DES_EDE3_ECB.SetKey(key, 5);
  138. // should not be here
  139. cerr << "DES-EDE3 implementation did not detect use of invalid key length.\n";
  140. abort();
  141. }
  142. catch (InvalidArgument &e)
  143. {
  144. cout << "9. Caught expected exception when using invalid key length. Exception message follows: ";
  145. cout << e.what() << endl;
  146. }
  147. cout << "\nFIPS 140-2 Sample Application completed normally.\n";
  148. }
  149. #ifdef CRYPTOPP_IMPORTS
  150. static PNew s_pNew = NULL;
  151. static PDelete s_pDelete = NULL;
  152. extern "C" __declspec(dllexport) void __cdecl SetNewAndDeleteFromCryptoPP(PNew pNew, PDelete pDelete, PSetNewHandler pSetNewHandler)
  153. {
  154. s_pNew = pNew;
  155. s_pDelete = pDelete;
  156. }
  157. void * __cdecl operator new (size_t size)
  158. {
  159. return s_pNew(size);
  160. }
  161. void __cdecl operator delete (void * p)
  162. {
  163. s_pDelete(p);
  164. }
  165. #endif
  166. #ifdef CRYPTOPP_DLL_ONLY
  167. int __cdecl main()
  168. {
  169. FIPS140_SampleApplication();
  170. return 0;
  171. }
  172. #endif