2 * Copyright 2019-2020 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 <openssl/fipskey.h>
15 #include <openssl/err.h>
17 #include "prov/providercommonerr.h"
18 #include "prov/providercommon.h"
21 * We're cheating here. Normally we don't allow RUN_ONCE usage inside the FIPS
22 * module because all such initialisation should be associated with an
23 * individual OPENSSL_CTX. That doesn't work with the self test though because
24 * it should be run once regardless of the number of OPENSSL_CTXs we have.
26 #define ALLOW_RUN_ONCE_IN_FIPS
27 #include <internal/thread_once.h>
28 #include "self_test.h"
30 #define FIPS_STATE_INIT 0
31 #define FIPS_STATE_SELFTEST 1
32 #define FIPS_STATE_RUNNING 2
33 #define FIPS_STATE_ERROR 3
36 * The number of times the module will report it is in the error state
39 #define FIPS_ERROR_REPORTING_RATE_LIMIT 10
41 /* The size of a temp buffer used to read in data */
42 #define INTEGRITY_BUF_SIZE (4096)
43 #define MAX_MD_SIZE 64
44 #define MAC_NAME "HMAC"
45 #define DIGEST_NAME "SHA256"
47 static int FIPS_conditional_error_check
= 1;
48 static int FIPS_state
= FIPS_STATE_INIT
;
49 static CRYPTO_RWLOCK
*self_test_lock
= NULL
;
50 static unsigned char fixed_key
[32] = { FIPS_KEY_ELEMENTS
};
52 static CRYPTO_ONCE fips_self_test_init
= CRYPTO_ONCE_STATIC_INIT
;
53 DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init
)
56 * This lock gets freed in platform specific ways that may occur after we
57 * do mem leak checking. If we don't know how to free it for a particular
58 * platform then we just leak it deliberately. So we temporarily disable the
59 * mem leak checking while we allocate this.
61 self_test_lock
= CRYPTO_THREAD_lock_new();
62 return self_test_lock
!= NULL
;
65 #define DEP_DECLARE() \
70 * This is the Default Entry Point (DEP) code. Every platform must have a DEP.
71 * See FIPS 140-2 IG 9.10
73 * If we're run on a platform where we don't know how to define the DEP then
74 * the self-tests will never get triggered (FIPS_state never moves to
75 * FIPS_STATE_SELFTEST). This will be detected as an error when SELF_TEST_post()
76 * is called from OSSL_provider_init(), and so the fips module will be unusable
79 #if defined(_WIN32) || defined(__CYGWIN__)
81 /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
84 * this has side-effect of _WIN32 getting defined, which otherwise is
85 * mutually exclusive with __CYGWIN__...
90 # define DEP_INIT_ATTRIBUTE
91 # define DEP_FINI_ATTRIBUTE
92 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
);
93 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
96 case DLL_PROCESS_ATTACH
:
99 case DLL_PROCESS_DETACH
:
107 #elif defined(__sun) || defined(_AIX)
109 DEP_DECLARE() /* must be declared before pragma */
110 # define DEP_INIT_ATTRIBUTE
111 # define DEP_FINI_ATTRIBUTE
113 # pragma fini(cleanup)
115 #elif defined(__hpux)
118 # define DEP_INIT_ATTRIBUTE
119 # define DEP_FINI_ATTRIBUTE
121 # pragma fini "cleanup"
123 #elif defined(__GNUC__)
124 # define DEP_INIT_ATTRIBUTE static __attribute__((constructor))
125 # define DEP_FINI_ATTRIBUTE static __attribute__((destructor))
128 #if defined(DEP_INIT_ATTRIBUTE) && defined(DEP_FINI_ATTRIBUTE)
129 DEP_INIT_ATTRIBUTE
void init(void)
131 FIPS_state
= FIPS_STATE_SELFTEST
;
134 DEP_FINI_ATTRIBUTE
void cleanup(void)
136 CRYPTO_THREAD_lock_free(self_test_lock
);
141 * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
142 * the result matches the expected value.
143 * Return 1 if verified, or 0 if it fails.
145 static int verify_integrity(OSSL_CORE_BIO
*bio
, OSSL_FUNC_BIO_read_ex_fn read_ex_cb
,
146 unsigned char *expected
, size_t expected_len
,
147 OPENSSL_CTX
*libctx
, OSSL_SELF_TEST
*ev
,
148 const char *event_type
)
151 unsigned char out
[MAX_MD_SIZE
];
152 unsigned char buf
[INTEGRITY_BUF_SIZE
];
153 size_t bytes_read
= 0, out_len
= 0;
155 EVP_MAC_CTX
*ctx
= NULL
;
156 OSSL_PARAM params
[3], *p
= params
;
158 OSSL_SELF_TEST_onbegin(ev
, event_type
, OSSL_SELF_TEST_DESC_INTEGRITY_HMAC
);
160 mac
= EVP_MAC_fetch(libctx
, MAC_NAME
, NULL
);
161 ctx
= EVP_MAC_CTX_new(mac
);
162 if (mac
== NULL
|| ctx
== NULL
)
165 *p
++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME
,
166 strlen(DIGEST_NAME
) + 1);
167 *p
++ = OSSL_PARAM_construct_octet_string("key", fixed_key
,
169 *p
= OSSL_PARAM_construct_end();
171 if (EVP_MAC_CTX_set_params(ctx
, params
) <= 0
172 || !EVP_MAC_init(ctx
))
176 status
= read_ex_cb(bio
, buf
, sizeof(buf
), &bytes_read
);
179 if (!EVP_MAC_update(ctx
, buf
, bytes_read
))
182 if (!EVP_MAC_final(ctx
, out
, &out_len
, sizeof(out
)))
185 OSSL_SELF_TEST_oncorrupt_byte(ev
, out
);
186 if (expected_len
!= out_len
187 || memcmp(expected
, out
, out_len
) != 0)
191 OSSL_SELF_TEST_onend(ev
, ret
);
192 EVP_MAC_CTX_free(ctx
);
197 /* This API is triggered either on loading of the FIPS module or on demand */
198 int SELF_TEST_post(SELF_TEST_POST_PARAMS
*st
, int on_demand_test
)
201 int kats_already_passed
= 0;
203 OSSL_CORE_BIO
*bio_module
= NULL
, *bio_indicator
= NULL
;
204 unsigned char *module_checksum
= NULL
;
205 unsigned char *indicator_checksum
= NULL
;
207 OSSL_SELF_TEST
*ev
= NULL
;
209 if (!RUN_ONCE(&fips_self_test_init
, do_fips_self_test_init
))
212 CRYPTO_THREAD_read_lock(self_test_lock
);
213 loclstate
= FIPS_state
;
214 CRYPTO_THREAD_unlock(self_test_lock
);
216 if (loclstate
== FIPS_STATE_RUNNING
) {
219 } else if (loclstate
!= FIPS_STATE_SELFTEST
) {
220 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_STATE
);
224 CRYPTO_THREAD_write_lock(self_test_lock
);
225 if (FIPS_state
== FIPS_STATE_RUNNING
) {
226 if (!on_demand_test
) {
227 CRYPTO_THREAD_unlock(self_test_lock
);
230 FIPS_state
= FIPS_STATE_SELFTEST
;
231 } else if (FIPS_state
!= FIPS_STATE_SELFTEST
) {
232 CRYPTO_THREAD_unlock(self_test_lock
);
233 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_STATE
);
237 || st
->module_checksum_data
== NULL
) {
238 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_CONFIG_DATA
);
242 ev
= OSSL_SELF_TEST_new(st
->cb
, st
->cb_arg
);
246 module_checksum
= OPENSSL_hexstr2buf(st
->module_checksum_data
,
248 if (module_checksum
== NULL
) {
249 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_CONFIG_DATA
);
252 bio_module
= (*st
->bio_new_file_cb
)(st
->module_filename
, "rb");
254 /* Always check the integrity of the fips module */
255 if (bio_module
== NULL
256 || !verify_integrity(bio_module
, st
->bio_read_ex_cb
,
257 module_checksum
, checksum_len
, st
->libctx
,
258 ev
, OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY
)) {
259 ERR_raise(ERR_LIB_PROV
, PROV_R_MODULE_INTEGRITY_FAILURE
);
263 /* This will be NULL during installation - so the self test KATS will run */
264 if (st
->indicator_data
!= NULL
) {
266 * If the kats have already passed indicator is set - then check the
267 * integrity of the indicator.
269 if (st
->indicator_checksum_data
== NULL
) {
270 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_CONFIG_DATA
);
273 indicator_checksum
= OPENSSL_hexstr2buf(st
->indicator_checksum_data
,
275 if (indicator_checksum
== NULL
) {
276 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_CONFIG_DATA
);
281 (*st
->bio_new_buffer_cb
)(st
->indicator_data
,
282 strlen(st
->indicator_data
));
283 if (bio_indicator
== NULL
284 || !verify_integrity(bio_indicator
, st
->bio_read_ex_cb
,
285 indicator_checksum
, checksum_len
,
287 OSSL_SELF_TEST_TYPE_INSTALL_INTEGRITY
)) {
288 ERR_raise(ERR_LIB_PROV
, PROV_R_INDICATOR_INTEGRITY_FAILURE
);
291 kats_already_passed
= 1;
295 /* Only runs the KAT's during installation OR on_demand() */
296 if (on_demand_test
|| kats_already_passed
== 0) {
297 if (!SELF_TEST_kats(ev
, st
->libctx
)) {
298 ERR_raise(ERR_LIB_PROV
, PROV_R_SELF_TEST_KAT_FAILURE
);
304 OSSL_SELF_TEST_free(ev
);
305 OPENSSL_free(module_checksum
);
306 OPENSSL_free(indicator_checksum
);
309 (*st
->bio_free_cb
)(bio_indicator
);
310 (*st
->bio_free_cb
)(bio_module
);
313 FIPS_state
= FIPS_STATE_RUNNING
;
315 ossl_set_error_state(OSSL_SELF_TEST_TYPE_NONE
);
316 CRYPTO_THREAD_unlock(self_test_lock
);
321 void SELF_TEST_disable_conditional_error_state(void)
323 FIPS_conditional_error_check
= 0;
326 void ossl_set_error_state(const char *type
)
328 int cond_test
= (type
!= NULL
&& strcmp(type
, OSSL_SELF_TEST_TYPE_PCT
) == 0);
330 if (!cond_test
|| (FIPS_conditional_error_check
== 1)) {
331 FIPS_state
= FIPS_STATE_ERROR
;
332 ERR_raise(ERR_LIB_PROV
, PROV_R_FIPS_MODULE_ENTERING_ERROR_STATE
);
334 ERR_raise(ERR_LIB_PROV
, PROV_R_FIPS_MODULE_CONDITIONAL_ERROR
);
338 int ossl_prov_is_running(void)
340 const int res
= FIPS_state
== FIPS_STATE_RUNNING
341 || FIPS_state
== FIPS_STATE_SELFTEST
;
342 static unsigned int rate_limit
= 0;
346 } else if (FIPS_state
== FIPS_STATE_ERROR
) {
347 if (rate_limit
++ < FIPS_ERROR_REPORTING_RATE_LIMIT
)
348 ERR_raise(ERR_LIB_PROV
, PROV_R_FIPS_MODULE_IN_ERROR_STATE
);