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.

88 lines
2.4 KiB

  1. // fips140.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "fips140.h"
  5. #include "misc.h"
  6. #include "trdlocal.h" // needs to be included last for cygwin
  7. NAMESPACE_BEGIN(CryptoPP)
  8. // Define this to 1 to turn on FIPS 140-2 compliance features, including additional tests during
  9. // startup, random number generation, and key generation. These tests may affect performance.
  10. #ifndef CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2
  11. #define CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2 0
  12. #endif
  13. #if (CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2 && !defined(THREADS_AVAILABLE))
  14. #error FIPS 140-2 compliance requires the availability of thread local storage.
  15. #endif
  16. #if (CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2 && !defined(OS_RNG_AVAILABLE))
  17. #error FIPS 140-2 compliance requires the availability of OS provided RNG.
  18. #endif
  19. PowerUpSelfTestStatus g_powerUpSelfTestStatus = POWER_UP_SELF_TEST_NOT_DONE;
  20. bool FIPS_140_2_ComplianceEnabled()
  21. {
  22. return CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2;
  23. }
  24. void SimulatePowerUpSelfTestFailure()
  25. {
  26. g_powerUpSelfTestStatus = POWER_UP_SELF_TEST_FAILED;
  27. }
  28. PowerUpSelfTestStatus CRYPTOPP_API GetPowerUpSelfTestStatus()
  29. {
  30. return g_powerUpSelfTestStatus;
  31. }
  32. #if CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2
  33. ThreadLocalStorage & AccessPowerUpSelfTestInProgress()
  34. {
  35. static ThreadLocalStorage selfTestInProgress;
  36. return selfTestInProgress;
  37. }
  38. #endif
  39. bool PowerUpSelfTestInProgressOnThisThread()
  40. {
  41. #if CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2
  42. return AccessPowerUpSelfTestInProgress().GetValue() != NULL;
  43. #else
  44. assert(false); // should not be called
  45. return false;
  46. #endif
  47. }
  48. void SetPowerUpSelfTestInProgressOnThisThread(bool inProgress)
  49. {
  50. CRYPTOPP_UNUSED(inProgress);
  51. #if CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2
  52. AccessPowerUpSelfTestInProgress().SetValue((void *)inProgress);
  53. #endif
  54. }
  55. void EncryptionPairwiseConsistencyTest_FIPS_140_Only(const PK_Encryptor &encryptor, const PK_Decryptor &decryptor)
  56. {
  57. CRYPTOPP_UNUSED(encryptor), CRYPTOPP_UNUSED(decryptor);
  58. #if CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2
  59. EncryptionPairwiseConsistencyTest(encryptor, decryptor);
  60. #endif
  61. }
  62. void SignaturePairwiseConsistencyTest_FIPS_140_Only(const PK_Signer &signer, const PK_Verifier &verifier)
  63. {
  64. CRYPTOPP_UNUSED(signer), CRYPTOPP_UNUSED(verifier);
  65. #if CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2
  66. SignaturePairwiseConsistencyTest(signer, verifier);
  67. #endif
  68. }
  69. NAMESPACE_END
  70. #endif