Counter Strike : Global Offensive Source Code
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.

205 lines
5.5 KiB

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