2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
16 * We're cheating here. Normally we don't allow RUN_ONCE usage inside the FIPS
17 * module because all such initialisation should be associated with an
18 * individual OPENSSL_CTX. That doesn't work with the self test though because
19 * it should be run once regardless of the number of OPENSSL_CTXs we have.
21 #define ALLOW_RUN_ONCE_IN_FIPS
22 #include <internal/thread_once.h>
25 #define FIPS_STATE_INIT 0
26 #define FIPS_STATE_SELFTEST 1
27 #define FIPS_STATE_RUNNING 2
28 #define FIPS_STATE_ERROR 3
30 /* The size of a temp buffer used to read in data */
31 #define INTEGRITY_BUF_SIZE (4096)
32 #define MAX_MD_SIZE 64
33 #define MAC_NAME "HMAC"
34 #define DIGEST_NAME "SHA256"
36 static int FIPS_state = FIPS_STATE_INIT;
37 static CRYPTO_RWLOCK *self_test_lock = NULL;
38 static unsigned char fixed_key[32] = { 0 };
40 static CRYPTO_ONCE fips_self_test_init = CRYPTO_ONCE_STATIC_INIT;
41 DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init)
44 * This lock gets freed in platform specific ways that may occur after we
45 * do mem leak checking. If we don't know how to free it for a particular
46 * platform then we just leak it deliberately. So we temporarily disable the
47 * mem leak checking while we allocate this.
49 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
50 self_test_lock = CRYPTO_THREAD_lock_new();
51 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
53 return self_test_lock != NULL;
57 * This is the Default Entry Point (DEP) code. Every platform must have a DEP.
58 * See FIPS 140-2 IG 9.10
60 * If we're run on a platform where we don't know how to define the DEP then
61 * the self-tests will never get triggered (FIPS_state never moves to
62 * FIPS_STATE_SELFTEST). This will be detected as an error when SELF_TEST_post()
63 * is called from OSSL_provider_init(), and so the fips module will be unusable
66 #if defined(_WIN32) || defined(__CYGWIN__)
68 /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
71 * this has side-effect of _WIN32 getting defined, which otherwise is
72 * mutually exclusive with __CYGWIN__...
76 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
77 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
80 case DLL_PROCESS_ATTACH:
81 FIPS_state = FIPS_STATE_SELFTEST;
83 case DLL_PROCESS_DETACH:
84 CRYPTO_THREAD_lock_free(self_test_lock);
91 #elif defined(__GNUC__)
93 static __attribute__((constructor)) void init(void)
95 FIPS_state = FIPS_STATE_SELFTEST;
99 static __attribute__((destructor)) void cleanup(void)
101 CRYPTO_THREAD_lock_free(self_test_lock);
107 * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
108 * the result matches the expected value.
109 * Return 1 if verified, or 0 if it fails.
111 static int verify_integrity(BIO *bio, OSSL_BIO_read_ex_fn read_ex_cb,
112 unsigned char *expected, size_t expected_len,
116 unsigned char out[MAX_MD_SIZE];
117 unsigned char buf[INTEGRITY_BUF_SIZE];
118 size_t bytes_read = 0, out_len = 0;
120 EVP_MAC_CTX *ctx = NULL;
121 OSSL_PARAM params[3], *p = params;
123 mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
124 ctx = EVP_MAC_CTX_new(mac);
125 if (mac == NULL || ctx == NULL)
128 *p++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME,
129 strlen(DIGEST_NAME) + 1);
130 *p++ = OSSL_PARAM_construct_octet_string("key", fixed_key,
132 *p = OSSL_PARAM_construct_end();
134 if (EVP_MAC_CTX_set_params(ctx, params) <= 0
135 || !EVP_MAC_init(ctx))
139 status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
142 if (!EVP_MAC_update(ctx, buf, bytes_read))
145 if (!EVP_MAC_final(ctx, out, &out_len, sizeof(out)))
148 if (expected_len != out_len
149 || memcmp(expected, out, out_len) != 0)
153 EVP_MAC_CTX_free(ctx);
158 /* This API is triggered either on loading of the FIPS module or on demand */
159 int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
162 int kats_already_passed = 0;
164 BIO *bio_module = NULL, *bio_indicator = NULL;
165 unsigned char *module_checksum = NULL;
166 unsigned char *indicator_checksum = NULL;
169 if (!RUN_ONCE(&fips_self_test_init, do_fips_self_test_init))
172 CRYPTO_THREAD_read_lock(self_test_lock);
173 loclstate = FIPS_state;
174 CRYPTO_THREAD_unlock(self_test_lock);
176 if (loclstate == FIPS_STATE_RUNNING) {
179 } else if (loclstate != FIPS_STATE_SELFTEST) {
183 CRYPTO_THREAD_write_lock(self_test_lock);
184 if (FIPS_state == FIPS_STATE_RUNNING) {
185 if (!on_demand_test) {
186 CRYPTO_THREAD_unlock(self_test_lock);
189 FIPS_state = FIPS_STATE_SELFTEST;
190 } else if (FIPS_state != FIPS_STATE_SELFTEST) {
191 CRYPTO_THREAD_unlock(self_test_lock);
195 || st->module_checksum_data == NULL)
198 module_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
200 if (module_checksum == NULL)
202 bio_module = (*st->bio_new_file_cb)(st->module_filename, "rb");
204 /* Always check the integrity of the fips module */
205 if (bio_module == NULL
206 || !verify_integrity(bio_module, st->bio_read_ex_cb,
207 module_checksum, checksum_len, st->libctx))
210 /* This will be NULL during installation - so the self test KATS will run */
211 if (st->indicator_data != NULL) {
213 * If the kats have already passed indicator is set - then check the
214 * integrity of the indicator.
216 if (st->indicator_checksum_data == NULL)
218 indicator_checksum = OPENSSL_hexstr2buf(st->indicator_checksum_data,
220 if (indicator_checksum == NULL)
224 (*st->bio_new_buffer_cb)(st->indicator_data,
225 strlen(st->indicator_data));
226 if (bio_indicator == NULL
227 || !verify_integrity(bio_indicator, st->bio_read_ex_cb,
228 indicator_checksum, checksum_len,
232 kats_already_passed = 1;
235 /* Only runs the KAT's during installation OR on_demand() */
236 if (on_demand_test || kats_already_passed == 0) {
237 /*TODO (3.0) Add self test KATS */
241 OPENSSL_free(module_checksum);
242 OPENSSL_free(indicator_checksum);
245 (*st->bio_free_cb)(bio_indicator);
246 (*st->bio_free_cb)(bio_module);
248 FIPS_state = ok ? FIPS_STATE_RUNNING : FIPS_STATE_ERROR;
249 CRYPTO_THREAD_unlock(self_test_lock);