Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/providers/fips/self_test.c

raw
1 /*
2 * Copyright 2019-2026 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <string.h>
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>
18 #include <openssl/rand.h>
19 #include "internal/e_os.h"
20 #include "internal/fips.h"
21 #include "internal/threads_common.h"
22 #include "internal/tsan_assist.h"
23 #include "prov/providercommon.h"
24 #include "crypto/rand.h"
25
26 /*
27 * We're cheating here. Normally we don't allow RUN_ONCE usage inside the FIPS
28 * module because all such initialisation should be associated with an
29 * individual OSSL_LIB_CTX. That doesn't work with the self test though because
30 * it should be run once regardless of the number of OSSL_LIB_CTXs we have.
31 */
32 #define ALLOW_RUN_ONCE_IN_FIPS
33 #include "internal/thread_once.h"
34 #include "self_test.h"
35
36 #define FIPS_STATE_INIT 0
37 #define FIPS_STATE_SELFTEST 1
38 #define FIPS_STATE_RUNNING 2
39 #define FIPS_STATE_ERROR 3
40
41 /*
42 * The number of times the module will report it is in the error state
43 * before going quiet.
44 */
45 #define FIPS_ERROR_REPORTING_RATE_LIMIT 10
46
47 /* The size of a temp buffer used to read in data */
48 #define INTEGRITY_BUF_SIZE (4096)
49 #define MAX_MD_SIZE 64
50 #define MAC_NAME "HMAC"
51 #define DIGEST_NAME "SHA256"
52
53 static int FIPS_conditional_error_check = 1;
54 static CRYPTO_RWLOCK *self_test_lock = NULL;
55
56 static CRYPTO_ONCE fips_self_test_init = CRYPTO_ONCE_STATIC_INIT;
57 #if !defined(OPENSSL_NO_FIPS_POST)
58 static unsigned char fixed_key[32] = { FIPS_KEY_ELEMENTS };
59 #endif
60
61 DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init)
62 {
63 /*
64 * These locks get freed in platform specific ways that may occur after we
65 * do mem leak checking. If we don't know how to free it for a particular
66 * platform then we just leak it deliberately.
67 */
68 self_test_lock = CRYPTO_THREAD_lock_new();
69 return self_test_lock != NULL;
70 }
71
72 /*
73 * Declarations for the DEP entry/exit points.
74 * Ones not required or incorrect need to be undefined or redefined respectively.
75 */
76 #define DEP_INITIAL_STATE FIPS_STATE_INIT
77 #define DEP_INIT_ATTRIBUTE static
78 #define DEP_FINI_ATTRIBUTE static
79
80 static void init(void);
81 static void cleanup(void);
82
83 /*
84 * This is the Default Entry Point (DEP) code.
85 * See FIPS 140-2 IG 9.10
86 */
87 #if defined(_WIN32) || defined(__CYGWIN__)
88 #ifdef __CYGWIN__
89 /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
90 #include <windows.h>
91 /*
92 * this has side-effect of _WIN32 getting defined, which otherwise is
93 * mutually exclusive with __CYGWIN__...
94 */
95 #endif
96
97 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
98 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
99 {
100 switch (fdwReason) {
101 case DLL_PROCESS_ATTACH:
102 init();
103 break;
104 case DLL_PROCESS_DETACH:
105 #ifndef __CYGWIN__
106 cleanup();
107 #endif
108 break;
109 default:
110 break;
111 }
112 return TRUE;
113 }
114
115 #elif defined(__GNUC__) && !defined(_AIX)
116 #undef DEP_INIT_ATTRIBUTE
117 #undef DEP_FINI_ATTRIBUTE
118 #define DEP_INIT_ATTRIBUTE static __attribute__((constructor))
119 #define DEP_FINI_ATTRIBUTE static __attribute__((destructor))
120
121 #elif defined(__sun)
122 #pragma init(init)
123 #pragma fini(cleanup)
124
125 #elif defined(_AIX) && !defined(__GNUC__)
126 void _init(void);
127 void _cleanup(void);
128 #pragma init(_init)
129 #pragma fini(_cleanup)
130 void _init(void)
131 {
132 init();
133 }
134 void _cleanup(void)
135 {
136 cleanup();
137 }
138
139 #elif defined(__hpux)
140 #pragma init "init"
141 #pragma fini "cleanup"
142
143 #elif defined(__TANDEM)
144 /* Method automatically called by the NonStop OS when the DLL loads */
145 void __INIT__init(void)
146 {
147 init();
148 }
149
150 /* Method automatically called by the NonStop OS prior to unloading the DLL */
151 void __TERM__cleanup(void)
152 {
153 cleanup();
154 }
155
156 #else
157 /*
158 * This build does not support any kind of DEP.
159 * We force the self-tests to run as part of the FIPS provider initialisation
160 * rather than being triggered by the DEP.
161 */
162 #undef DEP_INIT_ATTRIBUTE
163 #undef DEP_FINI_ATTRIBUTE
164 #undef DEP_INITIAL_STATE
165 #define DEP_INITIAL_STATE FIPS_STATE_SELFTEST
166 #endif
167
168 static TSAN_QUALIFIER int FIPS_state = DEP_INITIAL_STATE;
169
170 #if defined(DEP_INIT_ATTRIBUTE)
171 DEP_INIT_ATTRIBUTE void init(void)
172 {
173 tsan_store(&FIPS_state, FIPS_STATE_SELFTEST);
174 }
175 #endif
176
177 #if defined(DEP_FINI_ATTRIBUTE)
178 DEP_FINI_ATTRIBUTE void cleanup(void)
179 {
180 CRYPTO_THREAD_lock_free(self_test_lock);
181 CRYPTO_THREAD_clean_local();
182 }
183 #endif
184
185 #if !defined(OPENSSL_NO_FIPS_POST)
186 /*
187 * We need an explicit HMAC-SHA-256 KAT even though it is also
188 * checked as part of the KDF KATs. Refer IG 10.3.
189 */
190 static const unsigned char hmac_kat_pt[] = {
191 0xdd, 0x0c, 0x30, 0x33, 0x35, 0xf9, 0xe4, 0x2e,
192 0xc2, 0xef, 0xcc, 0xbf, 0x07, 0x95, 0xee, 0xa2
193 };
194 static const unsigned char hmac_kat_key[] = {
195 0xf4, 0x55, 0x66, 0x50, 0xac, 0x31, 0xd3, 0x54,
196 0x61, 0x61, 0x0b, 0xac, 0x4e, 0xd8, 0x1b, 0x1a,
197 0x18, 0x1b, 0x2d, 0x8a, 0x43, 0xea, 0x28, 0x54,
198 0xcb, 0xae, 0x22, 0xca, 0x74, 0x56, 0x08, 0x13
199 };
200 static const unsigned char hmac_kat_digest[] = {
201 0xf5, 0xf5, 0xe5, 0xf2, 0x66, 0x49, 0xe2, 0x40,
202 0xfc, 0x9e, 0x85, 0x7f, 0x2b, 0x9a, 0xbe, 0x28,
203 0x20, 0x12, 0x00, 0x92, 0x82, 0x21, 0x3e, 0x51,
204 0x44, 0x5d, 0xe3, 0x31, 0x04, 0x01, 0x72, 0x6b
205 };
206
207 static int integrity_self_test(OSSL_SELF_TEST *ev, OSSL_LIB_CTX *libctx)
208 {
209 int ok = 0;
210 unsigned char out[EVP_MAX_MD_SIZE];
211 size_t out_len = 0;
212
213 OSSL_PARAM params[2];
214 EVP_MAC *mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
215 EVP_MAC_CTX *ctx = EVP_MAC_CTX_new(mac);
216
217 OSSL_SELF_TEST_onbegin(ev, OSSL_SELF_TEST_TYPE_KAT_INTEGRITY,
218 OSSL_SELF_TEST_DESC_INTEGRITY_HMAC);
219
220 params[0] = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME, 0);
221 params[1] = OSSL_PARAM_construct_end();
222
223 if (ctx == NULL
224 || mac == NULL
225 || !EVP_MAC_init(ctx, hmac_kat_key, sizeof(hmac_kat_key), params)
226 || !EVP_MAC_update(ctx, hmac_kat_pt, sizeof(hmac_kat_pt))
227 || !EVP_MAC_final(ctx, out, &out_len, MAX_MD_SIZE))
228 goto err;
229
230 /* Optional corruption */
231 OSSL_SELF_TEST_oncorrupt_byte(ev, out);
232
233 if (out_len != sizeof(hmac_kat_digest)
234 || memcmp(out, hmac_kat_digest, out_len) != 0)
235 goto err;
236 ok = 1;
237 err:
238 OSSL_SELF_TEST_onend(ev, ok);
239 EVP_MAC_free(mac);
240 EVP_MAC_CTX_free(ctx);
241 return ok;
242 }
243
244 /*
245 * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
246 * the result matches the expected value.
247 * Return 1 if verified, or 0 if it fails.
248 */
249 static int verify_integrity(OSSL_CORE_BIO *bio, OSSL_FUNC_BIO_read_ex_fn read_ex_cb,
250 unsigned char *expected, size_t expected_len,
251 OSSL_LIB_CTX *libctx, OSSL_SELF_TEST *ev,
252 const char *event_type)
253 {
254 int ret = 0, status;
255 unsigned char out[MAX_MD_SIZE];
256 unsigned char buf[INTEGRITY_BUF_SIZE];
257 size_t bytes_read = 0, out_len = 0;
258 EVP_MAC *mac = NULL;
259 EVP_MAC_CTX *ctx = NULL;
260 OSSL_PARAM params[2], *p = params;
261
262 if (!integrity_self_test(ev, libctx))
263 goto err;
264
265 OSSL_SELF_TEST_onbegin(ev, event_type, OSSL_SELF_TEST_DESC_INTEGRITY_HMAC);
266
267 mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
268 if (mac == NULL)
269 goto err;
270 ctx = EVP_MAC_CTX_new(mac);
271 if (ctx == NULL)
272 goto err;
273
274 *p++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME, 0);
275 *p = OSSL_PARAM_construct_end();
276
277 if (!EVP_MAC_init(ctx, fixed_key, sizeof(fixed_key), params))
278 goto err;
279
280 while (1) {
281 status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
282 if (status != 1)
283 break;
284 if (!EVP_MAC_update(ctx, buf, bytes_read))
285 goto err;
286 }
287 if (!EVP_MAC_final(ctx, out, &out_len, sizeof(out)))
288 goto err;
289
290 OSSL_SELF_TEST_oncorrupt_byte(ev, out);
291 if (expected_len != out_len
292 || memcmp(expected, out, out_len) != 0)
293 goto err;
294 ret = 1;
295 err:
296 OSSL_SELF_TEST_onend(ev, ret);
297 EVP_MAC_CTX_free(ctx);
298 EVP_MAC_free(mac);
299 #ifdef OPENSSL_PEDANTIC_ZEROIZATION
300 OPENSSL_cleanse(out, sizeof(out));
301 #endif
302 return ret;
303 }
304 #endif /* OPENSSL_NO_FIPS_POST */
305
306 static void set_fips_state(int state)
307 {
308 tsan_store(&FIPS_state, state);
309 }
310
311 /* Return 1 if the FIPS self tests are running and 0 otherwise */
312 int ossl_fips_self_testing(void)
313 {
314 return tsan_load(&FIPS_state) == FIPS_STATE_SELFTEST;
315 }
316
317 /* This API is triggered either on loading of the FIPS module or on demand */
318 int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
319 {
320 int loclstate;
321 #if !defined(OPENSSL_NO_FIPS_POST)
322 int ok = 0;
323 long checksum_len;
324 OSSL_CORE_BIO *bio_module = NULL;
325 unsigned char *module_checksum = NULL;
326 OSSL_SELF_TEST *ev = NULL;
327 EVP_RAND *testrand = NULL;
328 EVP_RAND_CTX *rng;
329 #endif
330
331 if (!RUN_ONCE(&fips_self_test_init, do_fips_self_test_init))
332 return 0;
333
334 loclstate = tsan_load(&FIPS_state);
335
336 if (loclstate == FIPS_STATE_RUNNING) {
337 if (!on_demand_test)
338 return 1;
339 } else if (loclstate != FIPS_STATE_SELFTEST) {
340 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
341 return 0;
342 }
343
344 if (!CRYPTO_THREAD_write_lock(self_test_lock))
345 return 0;
346
347 #if !defined(OPENSSL_NO_FIPS_POST)
348 loclstate = tsan_load(&FIPS_state);
349 if (loclstate == FIPS_STATE_RUNNING) {
350 if (!on_demand_test) {
351 CRYPTO_THREAD_unlock(self_test_lock);
352 return 1;
353 }
354 set_fips_state(FIPS_STATE_SELFTEST);
355 } else if (loclstate != FIPS_STATE_SELFTEST) {
356 CRYPTO_THREAD_unlock(self_test_lock);
357 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
358 return 0;
359 }
360
361 if (st == NULL
362 || st->module_checksum_data == NULL) {
363 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
364 goto end;
365 }
366
367 ev = OSSL_SELF_TEST_new(st->cb, st->cb_arg);
368 if (ev == NULL)
369 goto end;
370
371 module_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
372 &checksum_len);
373 if (module_checksum == NULL) {
374 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
375 goto end;
376 }
377 bio_module = (*st->bio_new_file_cb)(st->module_filename, "rb");
378
379 /* Always check the integrity of the fips module */
380 if (bio_module == NULL
381 || !verify_integrity(bio_module, st->bio_read_ex_cb,
382 module_checksum, checksum_len, st->libctx,
383 ev, OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY)) {
384 ERR_raise(ERR_LIB_PROV, PROV_R_MODULE_INTEGRITY_FAILURE);
385 goto end;
386 }
387
388 if (!SELF_TEST_kats(ev, st->libctx)) {
389 ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_KAT_FAILURE);
390 goto end;
391 }
392
393 /* Verify that the RNG has been restored properly */
394 rng = ossl_rand_get0_private_noncreating(st->libctx);
395 if (rng != NULL)
396 if ((testrand = EVP_RAND_fetch(st->libctx, "TEST-RAND", NULL)) == NULL
397 || strcmp(EVP_RAND_get0_name(EVP_RAND_CTX_get0_rand(rng)),
398 EVP_RAND_get0_name(testrand))
399 == 0) {
400 ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_KAT_FAILURE);
401 goto end;
402 }
403
404 ok = 1;
405 end:
406 EVP_RAND_free(testrand);
407 OSSL_SELF_TEST_free(ev);
408 OPENSSL_free(module_checksum);
409
410 if (st != NULL)
411 (*st->bio_free_cb)(bio_module);
412
413 if (ok)
414 set_fips_state(FIPS_STATE_RUNNING);
415 else
416 ossl_set_error_state(OSSL_SELF_TEST_TYPE_NONE);
417 CRYPTO_THREAD_unlock(self_test_lock);
418
419 return ok;
420 #else
421 set_fips_state(FIPS_STATE_RUNNING);
422 CRYPTO_THREAD_unlock(self_test_lock);
423 return 1;
424 #endif /* !defined(OPENSSL_NO_FIPS_POST) */
425 }
426
427 void SELF_TEST_disable_conditional_error_state(void)
428 {
429 FIPS_conditional_error_check = 0;
430 }
431
432 void ossl_set_error_state(const char *type)
433 {
434 int cond_test = 0;
435 int import_pct = 0;
436
437 if (type != NULL) {
438 cond_test = strcmp(type, OSSL_SELF_TEST_TYPE_PCT) == 0;
439 import_pct = strcmp(type, OSSL_SELF_TEST_TYPE_PCT_IMPORT) == 0;
440 }
441
442 if (import_pct) {
443 /* Failure to import is transient to avoid a DoS attack */
444 ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_IMPORT_PCT_ERROR);
445 } else if (!cond_test || (FIPS_conditional_error_check == 1)) {
446 set_fips_state(FIPS_STATE_ERROR);
447 ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_ENTERING_ERROR_STATE);
448 } else {
449 ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_CONDITIONAL_ERROR);
450 }
451 }
452
453 int ossl_prov_is_running(void)
454 {
455 int res, loclstate;
456 static TSAN_QUALIFIER unsigned int rate_limit = 0;
457
458 loclstate = tsan_load(&FIPS_state);
459 res = loclstate == FIPS_STATE_RUNNING || loclstate == FIPS_STATE_SELFTEST;
460 if (loclstate == FIPS_STATE_ERROR)
461 if (tsan_counter(&rate_limit) < FIPS_ERROR_REPORTING_RATE_LIMIT)
462 ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_IN_ERROR_STATE);
463 return res;
464 }