2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/evp.h>
12 #include <openssl/params.h>
13 #include <openssl/crypto.h>
14 #include "internal/cryptlib.h"
15 #include <openssl/fipskey.h>
16 #include <openssl/err.h>
17 #include <openssl/proverr.h>
19 #include "prov/providercommon.h"
22 * We're cheating here. Normally we don't allow RUN_ONCE usage inside the FIPS
23 * module because all such initialisation should be associated with an
24 * individual OSSL_LIB_CTX. That doesn't work with the self test though because
25 * it should be run once regardless of the number of OSSL_LIB_CTXs we have.
27 #define ALLOW_RUN_ONCE_IN_FIPS
28 #include "internal/thread_once.h"
29 #include "self_test.h"
31 #define FIPS_STATE_INIT 0
32 #define FIPS_STATE_SELFTEST 1
33 #define FIPS_STATE_RUNNING 2
34 #define FIPS_STATE_ERROR 3
37 * The number of times the module will report it is in the error state
40 #define FIPS_ERROR_REPORTING_RATE_LIMIT 10
42 /* The size of a temp buffer used to read in data */
43 #define INTEGRITY_BUF_SIZE (4096)
44 #define MAX_MD_SIZE 64
45 #define MAC_NAME "HMAC"
46 #define DIGEST_NAME "SHA256"
48 static int FIPS_conditional_error_check = 1;
49 static CRYPTO_RWLOCK *self_test_lock = NULL;
50 static CRYPTO_RWLOCK *fips_state_lock = NULL;
51 static unsigned char fixed_key[32] = { FIPS_KEY_ELEMENTS };
53 static CRYPTO_ONCE fips_self_test_init = CRYPTO_ONCE_STATIC_INIT;
54 DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init)
57 * These locks get freed in platform specific ways that may occur after we
58 * do mem leak checking. If we don't know how to free it for a particular
59 * platform then we just leak it deliberately.
61 self_test_lock = CRYPTO_THREAD_lock_new();
62 fips_state_lock = CRYPTO_THREAD_lock_new();
63 return self_test_lock != NULL;
67 * Declarations for the DEP entry/exit points.
68 * Ones not required or incorrect need to be undefined or redefined respectively.
70 #define DEP_INITIAL_STATE FIPS_STATE_INIT
71 #define DEP_INIT_ATTRIBUTE static
72 #define DEP_FINI_ATTRIBUTE static
74 #if !defined(__GNUC__)
75 static void init(void);
76 static void cleanup(void);
80 * This is the Default Entry Point (DEP) code.
81 * See FIPS 140-2 IG 9.10
83 #if defined(_WIN32) || defined(__CYGWIN__)
85 /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
88 * this has side-effect of _WIN32 getting defined, which otherwise is
89 * mutually exclusive with __CYGWIN__...
93 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
94 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
97 case DLL_PROCESS_ATTACH:
100 case DLL_PROCESS_DETACH:
108 #elif defined(__sun) || defined(_AIX)
110 # pragma fini(cleanup)
112 #elif defined(__hpux)
114 # pragma fini "cleanup"
116 #elif defined(__GNUC__)
117 # undef DEP_INIT_ATTRIBUTE
118 # undef DEP_FINI_ATTRIBUTE
119 # define DEP_INIT_ATTRIBUTE static __attribute__((constructor))
120 # define DEP_FINI_ATTRIBUTE static __attribute__((destructor))
122 #elif defined(__TANDEM)
123 /* Method automatically called by the NonStop OS when the DLL loads */
124 void __INIT__init(void) {
128 /* Method automatically called by the NonStop OS prior to unloading the DLL */
129 void __TERM__cleanup(void) {
135 * This build does not support any kind of DEP.
136 * We force the self-tests to run as part of the FIPS provider initialisation
137 * rather than being triggered by the DEP.
139 # undef DEP_INIT_ATTRIBUTE
140 # undef DEP_FINI_ATTRIBUTE
141 # undef DEP_INITIAL_STATE
142 # define DEP_INITIAL_STATE FIPS_STATE_SELFTEST
145 static int FIPS_state = DEP_INITIAL_STATE;
147 #if defined(DEP_INIT_ATTRIBUTE)
148 DEP_INIT_ATTRIBUTE void init(void)
150 FIPS_state = FIPS_STATE_SELFTEST;
154 #if defined(DEP_FINI_ATTRIBUTE)
155 DEP_FINI_ATTRIBUTE void cleanup(void)
157 CRYPTO_THREAD_lock_free(self_test_lock);
158 CRYPTO_THREAD_lock_free(fips_state_lock);
163 * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
164 * the result matches the expected value.
165 * Return 1 if verified, or 0 if it fails.
167 static int verify_integrity(OSSL_CORE_BIO *bio, OSSL_FUNC_BIO_read_ex_fn read_ex_cb,
168 unsigned char *expected, size_t expected_len,
169 OSSL_LIB_CTX *libctx, OSSL_SELF_TEST *ev,
170 const char *event_type)
173 unsigned char out[MAX_MD_SIZE];
174 unsigned char buf[INTEGRITY_BUF_SIZE];
175 size_t bytes_read = 0, out_len = 0;
177 EVP_MAC_CTX *ctx = NULL;
178 OSSL_PARAM params[2], *p = params;
180 OSSL_SELF_TEST_onbegin(ev, event_type, OSSL_SELF_TEST_DESC_INTEGRITY_HMAC);
182 mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
185 ctx = EVP_MAC_CTX_new(mac);
189 *p++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME, 0);
190 *p = OSSL_PARAM_construct_end();
192 if (!EVP_MAC_init(ctx, fixed_key, sizeof(fixed_key), params))
196 status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
199 if (!EVP_MAC_update(ctx, buf, bytes_read))
202 if (!EVP_MAC_final(ctx, out, &out_len, sizeof(out)))
205 OSSL_SELF_TEST_oncorrupt_byte(ev, out);
206 if (expected_len != out_len
207 || memcmp(expected, out, out_len) != 0)
211 OSSL_SELF_TEST_onend(ev, ret);
212 EVP_MAC_CTX_free(ctx);
217 static void set_fips_state(int state)
219 if (ossl_assert(CRYPTO_THREAD_write_lock(fips_state_lock) != 0)) {
221 CRYPTO_THREAD_unlock(fips_state_lock);
225 /* This API is triggered either on loading of the FIPS module or on demand */
226 int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
229 int kats_already_passed = 0;
231 OSSL_CORE_BIO *bio_module = NULL, *bio_indicator = NULL;
232 unsigned char *module_checksum = NULL;
233 unsigned char *indicator_checksum = NULL;
235 OSSL_SELF_TEST *ev = NULL;
237 if (!RUN_ONCE(&fips_self_test_init, do_fips_self_test_init))
240 if (!CRYPTO_THREAD_read_lock(fips_state_lock))
242 loclstate = FIPS_state;
243 CRYPTO_THREAD_unlock(fips_state_lock);
245 if (loclstate == FIPS_STATE_RUNNING) {
248 } else if (loclstate != FIPS_STATE_SELFTEST) {
249 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
253 if (!CRYPTO_THREAD_write_lock(self_test_lock))
255 if (!CRYPTO_THREAD_read_lock(fips_state_lock)) {
256 CRYPTO_THREAD_unlock(self_test_lock);
259 if (FIPS_state == FIPS_STATE_RUNNING) {
260 CRYPTO_THREAD_unlock(fips_state_lock);
261 if (!on_demand_test) {
262 CRYPTO_THREAD_unlock(self_test_lock);
265 set_fips_state(FIPS_STATE_SELFTEST);
266 } else if (FIPS_state != FIPS_STATE_SELFTEST) {
267 CRYPTO_THREAD_unlock(fips_state_lock);
268 CRYPTO_THREAD_unlock(self_test_lock);
269 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
272 CRYPTO_THREAD_unlock(fips_state_lock);
276 || st->module_checksum_data == NULL) {
277 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
281 ev = OSSL_SELF_TEST_new(st->cb, st->cb_arg);
285 module_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
287 if (module_checksum == NULL) {
288 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
291 bio_module = (*st->bio_new_file_cb)(st->module_filename, "rb");
293 /* Always check the integrity of the fips module */
294 if (bio_module == NULL
295 || !verify_integrity(bio_module, st->bio_read_ex_cb,
296 module_checksum, checksum_len, st->libctx,
297 ev, OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY)) {
298 ERR_raise(ERR_LIB_PROV, PROV_R_MODULE_INTEGRITY_FAILURE);
302 /* This will be NULL during installation - so the self test KATS will run */
303 if (st->indicator_data != NULL) {
305 * If the kats have already passed indicator is set - then check the
306 * integrity of the indicator.
308 if (st->indicator_checksum_data == NULL) {
309 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
312 indicator_checksum = OPENSSL_hexstr2buf(st->indicator_checksum_data,
314 if (indicator_checksum == NULL) {
315 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
320 (*st->bio_new_buffer_cb)(st->indicator_data,
321 strlen(st->indicator_data));
322 if (bio_indicator == NULL
323 || !verify_integrity(bio_indicator, st->bio_read_ex_cb,
324 indicator_checksum, checksum_len,
326 OSSL_SELF_TEST_TYPE_INSTALL_INTEGRITY)) {
327 ERR_raise(ERR_LIB_PROV, PROV_R_INDICATOR_INTEGRITY_FAILURE);
330 kats_already_passed = 1;
335 * Only runs the KAT's during installation OR on_demand().
336 * NOTE: If the installation option 'self_test_onload' is chosen then this
337 * path will always be run, since kats_already_passed will always be 0.
339 if (on_demand_test || kats_already_passed == 0) {
340 if (!SELF_TEST_kats(ev, st->libctx)) {
341 ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_KAT_FAILURE);
347 OSSL_SELF_TEST_free(ev);
348 OPENSSL_free(module_checksum);
349 OPENSSL_free(indicator_checksum);
352 (*st->bio_free_cb)(bio_indicator);
353 (*st->bio_free_cb)(bio_module);
356 set_fips_state(FIPS_STATE_RUNNING);
358 ossl_set_error_state(OSSL_SELF_TEST_TYPE_NONE);
359 CRYPTO_THREAD_unlock(self_test_lock);
364 void SELF_TEST_disable_conditional_error_state(void)
366 FIPS_conditional_error_check = 0;
369 void ossl_set_error_state(const char *type)
371 int cond_test = (type != NULL && strcmp(type, OSSL_SELF_TEST_TYPE_PCT) == 0);
373 if (!cond_test || (FIPS_conditional_error_check == 1)) {
374 set_fips_state(FIPS_STATE_ERROR);
375 ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_ENTERING_ERROR_STATE);
377 ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_CONDITIONAL_ERROR);
381 int ossl_prov_is_running(void)
384 static unsigned int rate_limit = 0;
386 if (!CRYPTO_THREAD_read_lock(fips_state_lock))
388 res = FIPS_state == FIPS_STATE_RUNNING
389 || FIPS_state == FIPS_STATE_SELFTEST;
390 if (FIPS_state == FIPS_STATE_ERROR) {
391 CRYPTO_THREAD_unlock(fips_state_lock);
392 if (!CRYPTO_THREAD_write_lock(fips_state_lock))
394 if (rate_limit++ < FIPS_ERROR_REPORTING_RATE_LIMIT)
395 ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_IN_ERROR_STATE);
397 CRYPTO_THREAD_unlock(fips_state_lock);