fips140.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // fips140.h - originally written and placed in the public domain by Wei Dai
  2. /// \file fips140.h
  3. /// \brief Classes and functions for the FIPS 140-2 validated library
  4. /// \details The FIPS validated library is only available on Windows as a DLL. Once compiled,
  5. /// the library is always in FIPS mode contingent upon successful execution of
  6. /// DoPowerUpSelfTest() or DoDllPowerUpSelfTest().
  7. /// \sa <A HREF="http://cryptopp.com/wiki/Visual_Studio">Visual Studio</A> and
  8. /// <A HREF="http://cryptopp.com/wiki/config.h">config.h</A> on the Crypto++ wiki.
  9. #ifndef CRYPTOPP_FIPS140_H
  10. #define CRYPTOPP_FIPS140_H
  11. #include "cryptlib.h"
  12. #include "secblock.h"
  13. NAMESPACE_BEGIN(CryptoPP)
  14. /// Exception thrown when a crypto algorithm is used after a self test fails
  15. /// \details The self tests for an algorithm are performed by Algorithm class
  16. /// when CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2 is defined.
  17. class CRYPTOPP_DLL SelfTestFailure : public Exception
  18. {
  19. public:
  20. explicit SelfTestFailure(const std::string &s) : Exception(OTHER_ERROR, s) {}
  21. };
  22. /// \brief Determines whether the library provides FIPS validated cryptography
  23. /// \return true if FIPS 140-2 validated features were enabled at compile time.
  24. /// \details true if FIPS 140-2 validated features were enabled at compile time,
  25. /// false otherwise.
  26. /// \note FIPS mode is enabled at compile time. A program or other module cannot
  27. /// arbitrarily enter or exit the mode.
  28. CRYPTOPP_DLL bool CRYPTOPP_API FIPS_140_2_ComplianceEnabled();
  29. /// \brief Status of the power-up self test
  30. enum PowerUpSelfTestStatus {
  31. /// \brief The self tests have not been performed.
  32. POWER_UP_SELF_TEST_NOT_DONE,
  33. /// \brief The self tests were executed via DoPowerUpSelfTest() or
  34. /// DoDllPowerUpSelfTest(), but the result was failure.
  35. POWER_UP_SELF_TEST_FAILED,
  36. /// \brief The self tests were executed via DoPowerUpSelfTest() or
  37. /// DoDllPowerUpSelfTest(), and the result was success.
  38. POWER_UP_SELF_TEST_PASSED
  39. };
  40. /// \brief Performs the power-up self test
  41. /// \param moduleFilename the fully qualified name of the module
  42. /// \param expectedModuleMac the expected MAC of the components protected by the integrity check
  43. /// \details Performs the power-up self test, and sets the self test status to
  44. /// POWER_UP_SELF_TEST_PASSED or POWER_UP_SELF_TEST_FAILED.
  45. /// \details The self tests for an algorithm are performed by the Algorithm class
  46. /// when CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2 is defined.
  47. CRYPTOPP_DLL void CRYPTOPP_API DoPowerUpSelfTest(const char *moduleFilename, const byte *expectedModuleMac);
  48. /// \brief Performs the power-up self test on the DLL
  49. /// \details Performs the power-up self test using the filename of this DLL and the
  50. /// embedded module MAC, and sets the self test status to POWER_UP_SELF_TEST_PASSED or
  51. /// POWER_UP_SELF_TEST_FAILED.
  52. /// \details The self tests for an algorithm are performed by the Algorithm class
  53. /// when CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2 is defined.
  54. CRYPTOPP_DLL void CRYPTOPP_API DoDllPowerUpSelfTest();
  55. /// \brief Sets the power-up self test status to POWER_UP_SELF_TEST_FAILED
  56. /// \details Sets the power-up self test status to POWER_UP_SELF_TEST_FAILED to simulate failure.
  57. CRYPTOPP_DLL void CRYPTOPP_API SimulatePowerUpSelfTestFailure();
  58. /// \brief Provides the current power-up self test status
  59. /// \return the current power-up self test status
  60. CRYPTOPP_DLL PowerUpSelfTestStatus CRYPTOPP_API GetPowerUpSelfTestStatus();
  61. #ifndef CRYPTOPP_DOXYGEN_PROCESSING
  62. typedef PowerUpSelfTestStatus (CRYPTOPP_API * PGetPowerUpSelfTestStatus)();
  63. #endif
  64. /// \brief Class object that calculates the MAC on the module
  65. /// \return the MAC for the module
  66. CRYPTOPP_DLL MessageAuthenticationCode * CRYPTOPP_API NewIntegrityCheckingMAC();
  67. /// \brief Verifies the MAC on the module
  68. /// \param moduleFilename the fully qualified name of the module
  69. /// \param expectedModuleMac the expected MAC of the components protected by the integrity check
  70. /// \param pActualMac the actual MAC of the components calculated by the integrity check
  71. /// \param pMacFileLocation the offset of the MAC in the PE/PE+ module
  72. /// \return true if the MAC is valid, false otherwise
  73. CRYPTOPP_DLL bool CRYPTOPP_API IntegrityCheckModule(const char *moduleFilename, const byte *expectedModuleMac, SecByteBlock *pActualMac = NULLPTR, unsigned long *pMacFileLocation = NULLPTR);
  74. #ifndef CRYPTOPP_DOXYGEN_PROCESSING
  75. // this is used by Algorithm constructor to allow Algorithm objects to be constructed for the self test
  76. bool PowerUpSelfTestInProgressOnThisThread();
  77. void SetPowerUpSelfTestInProgressOnThisThread(bool inProgress);
  78. void SignaturePairwiseConsistencyTest(const PK_Signer &signer, const PK_Verifier &verifier);
  79. void EncryptionPairwiseConsistencyTest(const PK_Encryptor &encryptor, const PK_Decryptor &decryptor);
  80. void SignaturePairwiseConsistencyTest_FIPS_140_Only(const PK_Signer &signer, const PK_Verifier &verifier);
  81. void EncryptionPairwiseConsistencyTest_FIPS_140_Only(const PK_Encryptor &encryptor, const PK_Decryptor &decryptor);
  82. #endif
  83. /// \brief The placeholder used prior to embedding the actual MAC in the module.
  84. /// \details After the DLL is built but before it is MAC'd, the string CRYPTOPP_DUMMY_DLL_MAC
  85. /// is used as a placeholder for the actual MAC. A post-build step is performed which calculates
  86. /// the MAC of the DLL and embeds it in the module. The actual MAC is written by the
  87. /// <tt>cryptest.exe</tt> program using the <tt>mac_dll</tt> subcommand.
  88. #define CRYPTOPP_DUMMY_DLL_MAC "MAC_51f34b8db820ae8"
  89. NAMESPACE_END
  90. #endif