defltfips_test.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <string.h>
  2. #include <openssl/evp.h>
  3. #include <openssl/provider.h>
  4. #include "testutil.h"
  5. static int is_fips;
  6. static int test_is_fips_enabled(void)
  7. {
  8. int is_fips_enabled, is_fips_loaded;
  9. EVP_MD *sha256 = NULL;
  10. /*
  11. * Check we're in FIPS mode when we're supposed to be. We do this early to
  12. * confirm that EVP_default_properties_is_fips_enabled() works even before
  13. * other function calls have auto-loaded the config file.
  14. */
  15. is_fips_enabled = EVP_default_properties_is_fips_enabled(NULL);
  16. is_fips_loaded = OSSL_PROVIDER_available(NULL, "fips");
  17. /*
  18. * Check we're in an expected state. EVP_default_properties_is_fips_enabled
  19. * can return true even if the FIPS provider isn't loaded - it is only based
  20. * on the default properties. However we only set those properties if also
  21. * loading the FIPS provider.
  22. */
  23. if (!TEST_int_eq(is_fips, is_fips_enabled)
  24. || !TEST_int_eq(is_fips, is_fips_loaded))
  25. return 0;
  26. /*
  27. * Fetching an algorithm shouldn't change the state and should come from
  28. * expected provider.
  29. */
  30. sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL);
  31. if (!TEST_ptr(sha256))
  32. return 0;
  33. if (is_fips
  34. && !TEST_str_eq(OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(sha256)),
  35. "fips")) {
  36. EVP_MD_free(sha256);
  37. return 0;
  38. }
  39. EVP_MD_free(sha256);
  40. /* State should still be consistent */
  41. is_fips_enabled = EVP_default_properties_is_fips_enabled(NULL);
  42. if (!TEST_int_eq(is_fips, is_fips_enabled))
  43. return 0;
  44. return 1;
  45. }
  46. int setup_tests(void)
  47. {
  48. size_t argc;
  49. if (!test_skip_common_options()) {
  50. TEST_error("Error parsing test options\n");
  51. return 0;
  52. }
  53. argc = test_get_argument_count();
  54. switch(argc) {
  55. case 0:
  56. is_fips = 0;
  57. break;
  58. case 1:
  59. if (strcmp(test_get_argument(0), "fips") == 0) {
  60. is_fips = 1;
  61. break;
  62. }
  63. /* fall through */
  64. default:
  65. TEST_error("Invalid argument\n");
  66. return 0;
  67. }
  68. /* Must be the first test before any other libcrypto calls are made */
  69. ADD_TEST(test_is_fips_enabled);
  70. return 1;
  71. }