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