1b077aed3SPierre Pronchery /*
2*aa795734SPierre Pronchery * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery *
4b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery */
9b077aed3SPierre Pronchery
10b077aed3SPierre Pronchery #include <string.h>
11b077aed3SPierre Pronchery #include <openssl/evp.h>
12b077aed3SPierre Pronchery #include <openssl/params.h>
13b077aed3SPierre Pronchery #include <openssl/crypto.h>
14b077aed3SPierre Pronchery #include "internal/cryptlib.h"
15b077aed3SPierre Pronchery #include <openssl/fipskey.h>
16b077aed3SPierre Pronchery #include <openssl/err.h>
17b077aed3SPierre Pronchery #include <openssl/proverr.h>
18b077aed3SPierre Pronchery #include "e_os.h"
19*aa795734SPierre Pronchery #include "internal/tsan_assist.h"
20b077aed3SPierre Pronchery #include "prov/providercommon.h"
21b077aed3SPierre Pronchery
22b077aed3SPierre Pronchery /*
23b077aed3SPierre Pronchery * We're cheating here. Normally we don't allow RUN_ONCE usage inside the FIPS
24b077aed3SPierre Pronchery * module because all such initialisation should be associated with an
25b077aed3SPierre Pronchery * individual OSSL_LIB_CTX. That doesn't work with the self test though because
26b077aed3SPierre Pronchery * it should be run once regardless of the number of OSSL_LIB_CTXs we have.
27b077aed3SPierre Pronchery */
28b077aed3SPierre Pronchery #define ALLOW_RUN_ONCE_IN_FIPS
29b077aed3SPierre Pronchery #include "internal/thread_once.h"
30b077aed3SPierre Pronchery #include "self_test.h"
31b077aed3SPierre Pronchery
32b077aed3SPierre Pronchery #define FIPS_STATE_INIT 0
33b077aed3SPierre Pronchery #define FIPS_STATE_SELFTEST 1
34b077aed3SPierre Pronchery #define FIPS_STATE_RUNNING 2
35b077aed3SPierre Pronchery #define FIPS_STATE_ERROR 3
36b077aed3SPierre Pronchery
37b077aed3SPierre Pronchery /*
38b077aed3SPierre Pronchery * The number of times the module will report it is in the error state
39b077aed3SPierre Pronchery * before going quiet.
40b077aed3SPierre Pronchery */
41b077aed3SPierre Pronchery #define FIPS_ERROR_REPORTING_RATE_LIMIT 10
42b077aed3SPierre Pronchery
43b077aed3SPierre Pronchery /* The size of a temp buffer used to read in data */
44b077aed3SPierre Pronchery #define INTEGRITY_BUF_SIZE (4096)
45b077aed3SPierre Pronchery #define MAX_MD_SIZE 64
46b077aed3SPierre Pronchery #define MAC_NAME "HMAC"
47b077aed3SPierre Pronchery #define DIGEST_NAME "SHA256"
48b077aed3SPierre Pronchery
49b077aed3SPierre Pronchery static int FIPS_conditional_error_check = 1;
50b077aed3SPierre Pronchery static CRYPTO_RWLOCK *self_test_lock = NULL;
51b077aed3SPierre Pronchery static unsigned char fixed_key[32] = { FIPS_KEY_ELEMENTS };
52b077aed3SPierre Pronchery
53b077aed3SPierre Pronchery static CRYPTO_ONCE fips_self_test_init = CRYPTO_ONCE_STATIC_INIT;
DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init)54b077aed3SPierre Pronchery DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init)
55b077aed3SPierre Pronchery {
56b077aed3SPierre Pronchery /*
57b077aed3SPierre Pronchery * These locks get freed in platform specific ways that may occur after we
58b077aed3SPierre Pronchery * do mem leak checking. If we don't know how to free it for a particular
59b077aed3SPierre Pronchery * platform then we just leak it deliberately.
60b077aed3SPierre Pronchery */
61b077aed3SPierre Pronchery self_test_lock = CRYPTO_THREAD_lock_new();
62b077aed3SPierre Pronchery return self_test_lock != NULL;
63b077aed3SPierre Pronchery }
64b077aed3SPierre Pronchery
65b077aed3SPierre Pronchery /*
66b077aed3SPierre Pronchery * Declarations for the DEP entry/exit points.
67b077aed3SPierre Pronchery * Ones not required or incorrect need to be undefined or redefined respectively.
68b077aed3SPierre Pronchery */
69b077aed3SPierre Pronchery #define DEP_INITIAL_STATE FIPS_STATE_INIT
70b077aed3SPierre Pronchery #define DEP_INIT_ATTRIBUTE static
71b077aed3SPierre Pronchery #define DEP_FINI_ATTRIBUTE static
72b077aed3SPierre Pronchery
73b077aed3SPierre Pronchery static void init(void);
74b077aed3SPierre Pronchery static void cleanup(void);
75b077aed3SPierre Pronchery
76b077aed3SPierre Pronchery /*
77b077aed3SPierre Pronchery * This is the Default Entry Point (DEP) code.
78b077aed3SPierre Pronchery * See FIPS 140-2 IG 9.10
79b077aed3SPierre Pronchery */
80b077aed3SPierre Pronchery #if defined(_WIN32) || defined(__CYGWIN__)
81b077aed3SPierre Pronchery # ifdef __CYGWIN__
82b077aed3SPierre Pronchery /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
83b077aed3SPierre Pronchery # include <windows.h>
84b077aed3SPierre Pronchery /*
85b077aed3SPierre Pronchery * this has side-effect of _WIN32 getting defined, which otherwise is
86b077aed3SPierre Pronchery * mutually exclusive with __CYGWIN__...
87b077aed3SPierre Pronchery */
88b077aed3SPierre Pronchery # endif
89b077aed3SPierre Pronchery
90b077aed3SPierre Pronchery BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)91b077aed3SPierre Pronchery BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
92b077aed3SPierre Pronchery {
93b077aed3SPierre Pronchery switch (fdwReason) {
94b077aed3SPierre Pronchery case DLL_PROCESS_ATTACH:
95b077aed3SPierre Pronchery init();
96b077aed3SPierre Pronchery break;
97b077aed3SPierre Pronchery case DLL_PROCESS_DETACH:
98b077aed3SPierre Pronchery cleanup();
99b077aed3SPierre Pronchery break;
100b077aed3SPierre Pronchery default:
101b077aed3SPierre Pronchery break;
102b077aed3SPierre Pronchery }
103b077aed3SPierre Pronchery return TRUE;
104b077aed3SPierre Pronchery }
105b077aed3SPierre Pronchery
106b077aed3SPierre Pronchery #elif defined(__GNUC__) && !defined(_AIX)
107b077aed3SPierre Pronchery # undef DEP_INIT_ATTRIBUTE
108b077aed3SPierre Pronchery # undef DEP_FINI_ATTRIBUTE
109b077aed3SPierre Pronchery # define DEP_INIT_ATTRIBUTE static __attribute__((constructor))
110b077aed3SPierre Pronchery # define DEP_FINI_ATTRIBUTE static __attribute__((destructor))
111b077aed3SPierre Pronchery
112b077aed3SPierre Pronchery #elif defined(__sun)
113b077aed3SPierre Pronchery # pragma init(init)
114b077aed3SPierre Pronchery # pragma fini(cleanup)
115b077aed3SPierre Pronchery
116b077aed3SPierre Pronchery #elif defined(_AIX) && !defined(__GNUC__)
117b077aed3SPierre Pronchery void _init(void);
118b077aed3SPierre Pronchery void _cleanup(void);
119b077aed3SPierre Pronchery # pragma init(_init)
120b077aed3SPierre Pronchery # pragma fini(_cleanup)
_init(void)121b077aed3SPierre Pronchery void _init(void)
122b077aed3SPierre Pronchery {
123b077aed3SPierre Pronchery init();
124b077aed3SPierre Pronchery }
_cleanup(void)125b077aed3SPierre Pronchery void _cleanup(void)
126b077aed3SPierre Pronchery {
127b077aed3SPierre Pronchery cleanup();
128b077aed3SPierre Pronchery }
129b077aed3SPierre Pronchery
130b077aed3SPierre Pronchery #elif defined(__hpux)
131b077aed3SPierre Pronchery # pragma init "init"
132b077aed3SPierre Pronchery # pragma fini "cleanup"
133b077aed3SPierre Pronchery
134b077aed3SPierre Pronchery #elif defined(__TANDEM)
135b077aed3SPierre Pronchery /* Method automatically called by the NonStop OS when the DLL loads */
__INIT__init(void)136b077aed3SPierre Pronchery void __INIT__init(void) {
137b077aed3SPierre Pronchery init();
138b077aed3SPierre Pronchery }
139b077aed3SPierre Pronchery
140b077aed3SPierre Pronchery /* Method automatically called by the NonStop OS prior to unloading the DLL */
__TERM__cleanup(void)141b077aed3SPierre Pronchery void __TERM__cleanup(void) {
142b077aed3SPierre Pronchery cleanup();
143b077aed3SPierre Pronchery }
144b077aed3SPierre Pronchery
145b077aed3SPierre Pronchery #else
146b077aed3SPierre Pronchery /*
147b077aed3SPierre Pronchery * This build does not support any kind of DEP.
148b077aed3SPierre Pronchery * We force the self-tests to run as part of the FIPS provider initialisation
149b077aed3SPierre Pronchery * rather than being triggered by the DEP.
150b077aed3SPierre Pronchery */
151b077aed3SPierre Pronchery # undef DEP_INIT_ATTRIBUTE
152b077aed3SPierre Pronchery # undef DEP_FINI_ATTRIBUTE
153b077aed3SPierre Pronchery # undef DEP_INITIAL_STATE
154b077aed3SPierre Pronchery # define DEP_INITIAL_STATE FIPS_STATE_SELFTEST
155b077aed3SPierre Pronchery #endif
156b077aed3SPierre Pronchery
157*aa795734SPierre Pronchery static TSAN_QUALIFIER int FIPS_state = DEP_INITIAL_STATE;
158b077aed3SPierre Pronchery
159b077aed3SPierre Pronchery #if defined(DEP_INIT_ATTRIBUTE)
init(void)160b077aed3SPierre Pronchery DEP_INIT_ATTRIBUTE void init(void)
161b077aed3SPierre Pronchery {
162*aa795734SPierre Pronchery tsan_store(&FIPS_state, FIPS_STATE_SELFTEST);
163b077aed3SPierre Pronchery }
164b077aed3SPierre Pronchery #endif
165b077aed3SPierre Pronchery
166b077aed3SPierre Pronchery #if defined(DEP_FINI_ATTRIBUTE)
cleanup(void)167b077aed3SPierre Pronchery DEP_FINI_ATTRIBUTE void cleanup(void)
168b077aed3SPierre Pronchery {
169b077aed3SPierre Pronchery CRYPTO_THREAD_lock_free(self_test_lock);
170b077aed3SPierre Pronchery }
171b077aed3SPierre Pronchery #endif
172b077aed3SPierre Pronchery
173b077aed3SPierre Pronchery /*
174b077aed3SPierre Pronchery * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
175b077aed3SPierre Pronchery * the result matches the expected value.
176b077aed3SPierre Pronchery * Return 1 if verified, or 0 if it fails.
177b077aed3SPierre Pronchery */
verify_integrity(OSSL_CORE_BIO * bio,OSSL_FUNC_BIO_read_ex_fn read_ex_cb,unsigned char * expected,size_t expected_len,OSSL_LIB_CTX * libctx,OSSL_SELF_TEST * ev,const char * event_type)178b077aed3SPierre Pronchery static int verify_integrity(OSSL_CORE_BIO *bio, OSSL_FUNC_BIO_read_ex_fn read_ex_cb,
179b077aed3SPierre Pronchery unsigned char *expected, size_t expected_len,
180b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx, OSSL_SELF_TEST *ev,
181b077aed3SPierre Pronchery const char *event_type)
182b077aed3SPierre Pronchery {
183b077aed3SPierre Pronchery int ret = 0, status;
184b077aed3SPierre Pronchery unsigned char out[MAX_MD_SIZE];
185b077aed3SPierre Pronchery unsigned char buf[INTEGRITY_BUF_SIZE];
186b077aed3SPierre Pronchery size_t bytes_read = 0, out_len = 0;
187b077aed3SPierre Pronchery EVP_MAC *mac = NULL;
188b077aed3SPierre Pronchery EVP_MAC_CTX *ctx = NULL;
189b077aed3SPierre Pronchery OSSL_PARAM params[2], *p = params;
190b077aed3SPierre Pronchery
191b077aed3SPierre Pronchery OSSL_SELF_TEST_onbegin(ev, event_type, OSSL_SELF_TEST_DESC_INTEGRITY_HMAC);
192b077aed3SPierre Pronchery
193b077aed3SPierre Pronchery mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
194b077aed3SPierre Pronchery if (mac == NULL)
195b077aed3SPierre Pronchery goto err;
196b077aed3SPierre Pronchery ctx = EVP_MAC_CTX_new(mac);
197b077aed3SPierre Pronchery if (ctx == NULL)
198b077aed3SPierre Pronchery goto err;
199b077aed3SPierre Pronchery
200b077aed3SPierre Pronchery *p++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME, 0);
201b077aed3SPierre Pronchery *p = OSSL_PARAM_construct_end();
202b077aed3SPierre Pronchery
203b077aed3SPierre Pronchery if (!EVP_MAC_init(ctx, fixed_key, sizeof(fixed_key), params))
204b077aed3SPierre Pronchery goto err;
205b077aed3SPierre Pronchery
206b077aed3SPierre Pronchery while (1) {
207b077aed3SPierre Pronchery status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
208b077aed3SPierre Pronchery if (status != 1)
209b077aed3SPierre Pronchery break;
210b077aed3SPierre Pronchery if (!EVP_MAC_update(ctx, buf, bytes_read))
211b077aed3SPierre Pronchery goto err;
212b077aed3SPierre Pronchery }
213b077aed3SPierre Pronchery if (!EVP_MAC_final(ctx, out, &out_len, sizeof(out)))
214b077aed3SPierre Pronchery goto err;
215b077aed3SPierre Pronchery
216b077aed3SPierre Pronchery OSSL_SELF_TEST_oncorrupt_byte(ev, out);
217b077aed3SPierre Pronchery if (expected_len != out_len
218b077aed3SPierre Pronchery || memcmp(expected, out, out_len) != 0)
219b077aed3SPierre Pronchery goto err;
220b077aed3SPierre Pronchery ret = 1;
221b077aed3SPierre Pronchery err:
222b077aed3SPierre Pronchery OSSL_SELF_TEST_onend(ev, ret);
223b077aed3SPierre Pronchery EVP_MAC_CTX_free(ctx);
224b077aed3SPierre Pronchery EVP_MAC_free(mac);
225b077aed3SPierre Pronchery return ret;
226b077aed3SPierre Pronchery }
227b077aed3SPierre Pronchery
set_fips_state(int state)228b077aed3SPierre Pronchery static void set_fips_state(int state)
229b077aed3SPierre Pronchery {
230*aa795734SPierre Pronchery tsan_store(&FIPS_state, state);
231b077aed3SPierre Pronchery }
232b077aed3SPierre Pronchery
233b077aed3SPierre Pronchery /* This API is triggered either on loading of the FIPS module or on demand */
SELF_TEST_post(SELF_TEST_POST_PARAMS * st,int on_demand_test)234b077aed3SPierre Pronchery int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
235b077aed3SPierre Pronchery {
236b077aed3SPierre Pronchery int ok = 0;
237b077aed3SPierre Pronchery int kats_already_passed = 0;
238b077aed3SPierre Pronchery long checksum_len;
239b077aed3SPierre Pronchery OSSL_CORE_BIO *bio_module = NULL, *bio_indicator = NULL;
240b077aed3SPierre Pronchery unsigned char *module_checksum = NULL;
241b077aed3SPierre Pronchery unsigned char *indicator_checksum = NULL;
242b077aed3SPierre Pronchery int loclstate;
243b077aed3SPierre Pronchery OSSL_SELF_TEST *ev = NULL;
244b077aed3SPierre Pronchery
245b077aed3SPierre Pronchery if (!RUN_ONCE(&fips_self_test_init, do_fips_self_test_init))
246b077aed3SPierre Pronchery return 0;
247b077aed3SPierre Pronchery
248*aa795734SPierre Pronchery loclstate = tsan_load(&FIPS_state);
249b077aed3SPierre Pronchery
250b077aed3SPierre Pronchery if (loclstate == FIPS_STATE_RUNNING) {
251b077aed3SPierre Pronchery if (!on_demand_test)
252b077aed3SPierre Pronchery return 1;
253b077aed3SPierre Pronchery } else if (loclstate != FIPS_STATE_SELFTEST) {
254b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
255b077aed3SPierre Pronchery return 0;
256b077aed3SPierre Pronchery }
257b077aed3SPierre Pronchery
258b077aed3SPierre Pronchery if (!CRYPTO_THREAD_write_lock(self_test_lock))
259b077aed3SPierre Pronchery return 0;
260*aa795734SPierre Pronchery loclstate = tsan_load(&FIPS_state);
261*aa795734SPierre Pronchery if (loclstate == FIPS_STATE_RUNNING) {
262b077aed3SPierre Pronchery if (!on_demand_test) {
263b077aed3SPierre Pronchery CRYPTO_THREAD_unlock(self_test_lock);
264b077aed3SPierre Pronchery return 1;
265b077aed3SPierre Pronchery }
266b077aed3SPierre Pronchery set_fips_state(FIPS_STATE_SELFTEST);
267*aa795734SPierre Pronchery } else if (loclstate != FIPS_STATE_SELFTEST) {
268b077aed3SPierre Pronchery CRYPTO_THREAD_unlock(self_test_lock);
269b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
270b077aed3SPierre Pronchery return 0;
271b077aed3SPierre Pronchery }
272b077aed3SPierre Pronchery
273b077aed3SPierre Pronchery if (st == NULL
274b077aed3SPierre Pronchery || st->module_checksum_data == NULL) {
275b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
276b077aed3SPierre Pronchery goto end;
277b077aed3SPierre Pronchery }
278b077aed3SPierre Pronchery
279b077aed3SPierre Pronchery ev = OSSL_SELF_TEST_new(st->cb, st->cb_arg);
280b077aed3SPierre Pronchery if (ev == NULL)
281b077aed3SPierre Pronchery goto end;
282b077aed3SPierre Pronchery
283b077aed3SPierre Pronchery module_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
284b077aed3SPierre Pronchery &checksum_len);
285b077aed3SPierre Pronchery if (module_checksum == NULL) {
286b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
287b077aed3SPierre Pronchery goto end;
288b077aed3SPierre Pronchery }
289b077aed3SPierre Pronchery bio_module = (*st->bio_new_file_cb)(st->module_filename, "rb");
290b077aed3SPierre Pronchery
291b077aed3SPierre Pronchery /* Always check the integrity of the fips module */
292b077aed3SPierre Pronchery if (bio_module == NULL
293b077aed3SPierre Pronchery || !verify_integrity(bio_module, st->bio_read_ex_cb,
294b077aed3SPierre Pronchery module_checksum, checksum_len, st->libctx,
295b077aed3SPierre Pronchery ev, OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY)) {
296b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_MODULE_INTEGRITY_FAILURE);
297b077aed3SPierre Pronchery goto end;
298b077aed3SPierre Pronchery }
299b077aed3SPierre Pronchery
300b077aed3SPierre Pronchery /* This will be NULL during installation - so the self test KATS will run */
301b077aed3SPierre Pronchery if (st->indicator_data != NULL) {
302b077aed3SPierre Pronchery /*
303b077aed3SPierre Pronchery * If the kats have already passed indicator is set - then check the
304b077aed3SPierre Pronchery * integrity of the indicator.
305b077aed3SPierre Pronchery */
306b077aed3SPierre Pronchery if (st->indicator_checksum_data == NULL) {
307b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
308b077aed3SPierre Pronchery goto end;
309b077aed3SPierre Pronchery }
310b077aed3SPierre Pronchery indicator_checksum = OPENSSL_hexstr2buf(st->indicator_checksum_data,
311b077aed3SPierre Pronchery &checksum_len);
312b077aed3SPierre Pronchery if (indicator_checksum == NULL) {
313b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
314b077aed3SPierre Pronchery goto end;
315b077aed3SPierre Pronchery }
316b077aed3SPierre Pronchery
317b077aed3SPierre Pronchery bio_indicator =
318b077aed3SPierre Pronchery (*st->bio_new_buffer_cb)(st->indicator_data,
319b077aed3SPierre Pronchery strlen(st->indicator_data));
320b077aed3SPierre Pronchery if (bio_indicator == NULL
321b077aed3SPierre Pronchery || !verify_integrity(bio_indicator, st->bio_read_ex_cb,
322b077aed3SPierre Pronchery indicator_checksum, checksum_len,
323b077aed3SPierre Pronchery st->libctx, ev,
324b077aed3SPierre Pronchery OSSL_SELF_TEST_TYPE_INSTALL_INTEGRITY)) {
325b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INDICATOR_INTEGRITY_FAILURE);
326b077aed3SPierre Pronchery goto end;
327b077aed3SPierre Pronchery } else {
328b077aed3SPierre Pronchery kats_already_passed = 1;
329b077aed3SPierre Pronchery }
330b077aed3SPierre Pronchery }
331b077aed3SPierre Pronchery
332b077aed3SPierre Pronchery /*
333b077aed3SPierre Pronchery * Only runs the KAT's during installation OR on_demand().
334b077aed3SPierre Pronchery * NOTE: If the installation option 'self_test_onload' is chosen then this
335b077aed3SPierre Pronchery * path will always be run, since kats_already_passed will always be 0.
336b077aed3SPierre Pronchery */
337b077aed3SPierre Pronchery if (on_demand_test || kats_already_passed == 0) {
338b077aed3SPierre Pronchery if (!SELF_TEST_kats(ev, st->libctx)) {
339b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_KAT_FAILURE);
340b077aed3SPierre Pronchery goto end;
341b077aed3SPierre Pronchery }
342b077aed3SPierre Pronchery }
343b077aed3SPierre Pronchery ok = 1;
344b077aed3SPierre Pronchery end:
345b077aed3SPierre Pronchery OSSL_SELF_TEST_free(ev);
346b077aed3SPierre Pronchery OPENSSL_free(module_checksum);
347b077aed3SPierre Pronchery OPENSSL_free(indicator_checksum);
348b077aed3SPierre Pronchery
349b077aed3SPierre Pronchery if (st != NULL) {
350b077aed3SPierre Pronchery (*st->bio_free_cb)(bio_indicator);
351b077aed3SPierre Pronchery (*st->bio_free_cb)(bio_module);
352b077aed3SPierre Pronchery }
353b077aed3SPierre Pronchery if (ok)
354b077aed3SPierre Pronchery set_fips_state(FIPS_STATE_RUNNING);
355b077aed3SPierre Pronchery else
356b077aed3SPierre Pronchery ossl_set_error_state(OSSL_SELF_TEST_TYPE_NONE);
357b077aed3SPierre Pronchery CRYPTO_THREAD_unlock(self_test_lock);
358b077aed3SPierre Pronchery
359b077aed3SPierre Pronchery return ok;
360b077aed3SPierre Pronchery }
361b077aed3SPierre Pronchery
SELF_TEST_disable_conditional_error_state(void)362b077aed3SPierre Pronchery void SELF_TEST_disable_conditional_error_state(void)
363b077aed3SPierre Pronchery {
364b077aed3SPierre Pronchery FIPS_conditional_error_check = 0;
365b077aed3SPierre Pronchery }
366b077aed3SPierre Pronchery
ossl_set_error_state(const char * type)367b077aed3SPierre Pronchery void ossl_set_error_state(const char *type)
368b077aed3SPierre Pronchery {
369b077aed3SPierre Pronchery int cond_test = (type != NULL && strcmp(type, OSSL_SELF_TEST_TYPE_PCT) == 0);
370b077aed3SPierre Pronchery
371b077aed3SPierre Pronchery if (!cond_test || (FIPS_conditional_error_check == 1)) {
372b077aed3SPierre Pronchery set_fips_state(FIPS_STATE_ERROR);
373b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_ENTERING_ERROR_STATE);
374b077aed3SPierre Pronchery } else {
375b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_CONDITIONAL_ERROR);
376b077aed3SPierre Pronchery }
377b077aed3SPierre Pronchery }
378b077aed3SPierre Pronchery
ossl_prov_is_running(void)379b077aed3SPierre Pronchery int ossl_prov_is_running(void)
380b077aed3SPierre Pronchery {
381*aa795734SPierre Pronchery int res, loclstate;
382*aa795734SPierre Pronchery static TSAN_QUALIFIER unsigned int rate_limit = 0;
383b077aed3SPierre Pronchery
384*aa795734SPierre Pronchery loclstate = tsan_load(&FIPS_state);
385*aa795734SPierre Pronchery res = loclstate == FIPS_STATE_RUNNING || loclstate == FIPS_STATE_SELFTEST;
386*aa795734SPierre Pronchery if (loclstate == FIPS_STATE_ERROR)
387*aa795734SPierre Pronchery if (tsan_counter(&rate_limit) < FIPS_ERROR_REPORTING_RATE_LIMIT)
388b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_IN_ERROR_STATE);
389b077aed3SPierre Pronchery return res;
390b077aed3SPierre Pronchery }
391