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
10 #include <openssl/core_dispatch.h>
11 #include <openssl/core_names.h>
12 #include <openssl/params.h>
13 #include <openssl/fips_names.h>
14 #include <openssl/rand.h> /* RAND_get0_public() */
15 #include "internal/cryptlib.h"
16 #include "prov/implementations.h"
17 #include "prov/provider_ctx.h"
18 #include "prov/providercommon.h"
19 #include "prov/providercommonerr.h"
20 #include "prov/provider_util.h"
21 #include "prov/seeding.h"
22 #include "self_test.h"
24 static const char FIPS_DEFAULT_PROPERTIES[] = "provider=fips,fips=yes";
25 static const char FIPS_UNAPPROVED_PROPERTIES[] = "provider=fips,fips=no";
28 * Forward declarations to ensure that interface functions are correctly
31 static OSSL_FUNC_provider_teardown_fn fips_teardown;
32 static OSSL_FUNC_provider_gettable_params_fn fips_gettable_params;
33 static OSSL_FUNC_provider_get_params_fn fips_get_params;
34 static OSSL_FUNC_provider_query_operation_fn fips_query;
36 #define ALGC(NAMES, FUNC, CHECK) { { NAMES, FIPS_DEFAULT_PROPERTIES, FUNC }, CHECK }
37 #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
39 extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
40 int FIPS_security_check_enabled(void);
43 * TODO(3.0): Should these be stored in the provider side provctx? Could they
44 * ever be different from one init to the next? Unfortunately we can't do this
45 * at the moment because c_put_error/c_add_error_vdata do not provide
46 * us with the OSSL_LIB_CTX as a parameter.
49 static SELF_TEST_POST_PARAMS selftest_params;
50 static int fips_security_checks = 1;
51 static const char *fips_security_check_option = "1";
53 /* Functions provided by the core */
54 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params;
55 static OSSL_FUNC_core_get_params_fn *c_get_params;
56 OSSL_FUNC_core_thread_start_fn *c_thread_start;
57 static OSSL_FUNC_core_new_error_fn *c_new_error;
58 static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
59 static OSSL_FUNC_core_vset_error_fn *c_vset_error;
60 static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
61 static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
62 static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
63 static OSSL_FUNC_CRYPTO_malloc_fn *c_CRYPTO_malloc;
64 static OSSL_FUNC_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
65 static OSSL_FUNC_CRYPTO_free_fn *c_CRYPTO_free;
66 static OSSL_FUNC_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
67 static OSSL_FUNC_CRYPTO_realloc_fn *c_CRYPTO_realloc;
68 static OSSL_FUNC_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
69 static OSSL_FUNC_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
70 static OSSL_FUNC_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
71 static OSSL_FUNC_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
72 static OSSL_FUNC_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
73 static OSSL_FUNC_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
74 static OSSL_FUNC_BIO_vsnprintf_fn *c_BIO_vsnprintf;
75 static OSSL_FUNC_self_test_cb_fn *c_stcbfn = NULL;
76 static OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
78 typedef struct fips_global_st {
79 const OSSL_CORE_HANDLE *handle;
82 static void *fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
84 FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
89 static void fips_prov_ossl_ctx_free(void *fgbl)
94 static const OSSL_LIB_CTX_METHOD fips_prov_ossl_ctx_method = {
95 fips_prov_ossl_ctx_new,
96 fips_prov_ossl_ctx_free,
100 /* Parameters we provide to the core */
101 static const OSSL_PARAM fips_param_types[] = {
102 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
103 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
104 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
105 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
106 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_SECURITY_CHECKS, OSSL_PARAM_INTEGER, NULL, 0),
111 * Parameters to retrieve from the core provider - required for self testing.
112 * NOTE: inside core_get_params() these will be loaded from config items
113 * stored inside prov->parameters (except for
114 * OSSL_PROV_PARAM_CORE_MODULE_FILENAME).
115 * OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS is not a self test parameter.
117 static OSSL_PARAM core_params[] =
119 OSSL_PARAM_utf8_ptr(OSSL_PROV_PARAM_CORE_MODULE_FILENAME,
120 selftest_params.module_filename,
121 sizeof(selftest_params.module_filename)),
122 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_MODULE_MAC,
123 selftest_params.module_checksum_data,
124 sizeof(selftest_params.module_checksum_data)),
125 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_MAC,
126 selftest_params.indicator_checksum_data,
127 sizeof(selftest_params.indicator_checksum_data)),
128 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
129 selftest_params.indicator_data,
130 sizeof(selftest_params.indicator_data)),
131 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
132 selftest_params.indicator_version,
133 sizeof(selftest_params.indicator_version)),
134 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
135 selftest_params.conditional_error_check,
136 sizeof(selftest_params.conditional_error_check)),
137 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
138 fips_security_check_option,
139 sizeof(fips_security_check_option)),
143 static const OSSL_PARAM *fips_gettable_params(void *provctx)
145 return fips_param_types;
148 static int fips_get_params(void *provctx, OSSL_PARAM params[])
152 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
153 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
155 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
156 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
158 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
159 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
161 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
162 if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
164 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_SECURITY_CHECKS);
165 if (p != NULL && !OSSL_PARAM_set_int(p, fips_security_checks))
170 static void set_self_test_cb(const OSSL_CORE_HANDLE *handle)
172 if (c_stcbfn != NULL && c_get_libctx != NULL) {
173 c_stcbfn(c_get_libctx(handle), &selftest_params.cb,
174 &selftest_params.cb_arg);
176 selftest_params.cb = NULL;
177 selftest_params.cb_arg = NULL;
181 static int fips_self_test(void *provctx)
183 set_self_test_cb(FIPS_get_core_handle(selftest_params.libctx));
184 return SELF_TEST_post(&selftest_params, 1) ? 1 : 0;
188 * For the algorithm names, we use the following formula for our primary
191 * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
193 * VERSION is only present if there are multiple versions of
194 * an alg (MD2, MD4, MD5). It may be omitted if there is only
195 * one version (if a subsequent version is released in the future,
196 * we can always change the canonical name, and add the old name
199 * SUBNAME may be present where we are combining multiple
200 * algorithms together, e.g. MD5-SHA1.
202 * SIZE is only present if multiple versions of an algorithm exist
203 * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
205 * MODE is only present where applicable.
207 * We add diverse other names where applicable, such as the names that
208 * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
209 * we have used historically.
211 static const OSSL_ALGORITHM fips_digests[] = {
212 /* Our primary name:NiST name[:our older names] */
213 { "SHA1:SHA-1:SSL3-SHA1", FIPS_DEFAULT_PROPERTIES, ossl_sha1_functions },
214 { "SHA2-224:SHA-224:SHA224", FIPS_DEFAULT_PROPERTIES,
215 ossl_sha224_functions },
216 { "SHA2-256:SHA-256:SHA256", FIPS_DEFAULT_PROPERTIES,
217 ossl_sha256_functions },
218 { "SHA2-384:SHA-384:SHA384", FIPS_DEFAULT_PROPERTIES,
219 ossl_sha384_functions },
220 { "SHA2-512:SHA-512:SHA512", FIPS_DEFAULT_PROPERTIES,
221 ossl_sha512_functions },
222 { "SHA2-512/224:SHA-512/224:SHA512-224", FIPS_DEFAULT_PROPERTIES,
223 ossl_sha512_224_functions },
224 { "SHA2-512/256:SHA-512/256:SHA512-256", FIPS_DEFAULT_PROPERTIES,
225 ossl_sha512_256_functions },
227 /* We agree with NIST here, so one name only */
228 { "SHA3-224", FIPS_DEFAULT_PROPERTIES, ossl_sha3_224_functions },
229 { "SHA3-256", FIPS_DEFAULT_PROPERTIES, ossl_sha3_256_functions },
230 { "SHA3-384", FIPS_DEFAULT_PROPERTIES, ossl_sha3_384_functions },
231 { "SHA3-512", FIPS_DEFAULT_PROPERTIES, ossl_sha3_512_functions },
233 { "SHAKE-128:SHAKE128", FIPS_DEFAULT_PROPERTIES, ossl_shake_128_functions },
234 { "SHAKE-256:SHAKE256", FIPS_DEFAULT_PROPERTIES, ossl_shake_256_functions },
237 * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
238 * KMAC128 and KMAC256.
240 { "KECCAK-KMAC-128:KECCAK-KMAC128", FIPS_DEFAULT_PROPERTIES,
241 ossl_keccak_kmac_128_functions },
242 { "KECCAK-KMAC-256:KECCAK-KMAC256", FIPS_DEFAULT_PROPERTIES,
243 ossl_keccak_kmac_256_functions },
247 static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
248 /* Our primary name[:ASN.1 OID name][:our older names] */
249 ALG("AES-256-ECB", ossl_aes256ecb_functions),
250 ALG("AES-192-ECB", ossl_aes192ecb_functions),
251 ALG("AES-128-ECB", ossl_aes128ecb_functions),
252 ALG("AES-256-CBC:AES256", ossl_aes256cbc_functions),
253 ALG("AES-192-CBC:AES192", ossl_aes192cbc_functions),
254 ALG("AES-128-CBC:AES128", ossl_aes128cbc_functions),
255 ALG("AES-256-CBC-CTS", ossl_aes256cbc_cts_functions),
256 ALG("AES-192-CBC-CTS", ossl_aes192cbc_cts_functions),
257 ALG("AES-128-CBC-CTS", ossl_aes128cbc_cts_functions),
258 ALG("AES-256-OFB", ossl_aes256ofb_functions),
259 ALG("AES-192-OFB", ossl_aes192ofb_functions),
260 ALG("AES-128-OFB", ossl_aes128ofb_functions),
261 ALG("AES-256-CFB", ossl_aes256cfb_functions),
262 ALG("AES-192-CFB", ossl_aes192cfb_functions),
263 ALG("AES-128-CFB", ossl_aes128cfb_functions),
264 ALG("AES-256-CFB1", ossl_aes256cfb1_functions),
265 ALG("AES-192-CFB1", ossl_aes192cfb1_functions),
266 ALG("AES-128-CFB1", ossl_aes128cfb1_functions),
267 ALG("AES-256-CFB8", ossl_aes256cfb8_functions),
268 ALG("AES-192-CFB8", ossl_aes192cfb8_functions),
269 ALG("AES-128-CFB8", ossl_aes128cfb8_functions),
270 ALG("AES-256-CTR", ossl_aes256ctr_functions),
271 ALG("AES-192-CTR", ossl_aes192ctr_functions),
272 ALG("AES-128-CTR", ossl_aes128ctr_functions),
273 ALG("AES-256-XTS", ossl_aes256xts_functions),
274 ALG("AES-128-XTS", ossl_aes128xts_functions),
275 ALG("AES-256-GCM:id-aes256-GCM", ossl_aes256gcm_functions),
276 ALG("AES-192-GCM:id-aes192-GCM", ossl_aes192gcm_functions),
277 ALG("AES-128-GCM:id-aes128-GCM", ossl_aes128gcm_functions),
278 ALG("AES-256-CCM:id-aes256-CCM", ossl_aes256ccm_functions),
279 ALG("AES-192-CCM:id-aes192-CCM", ossl_aes192ccm_functions),
280 ALG("AES-128-CCM:id-aes128-CCM", ossl_aes128ccm_functions),
281 ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", ossl_aes256wrap_functions),
282 ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", ossl_aes192wrap_functions),
283 ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", ossl_aes128wrap_functions),
284 ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
285 ossl_aes256wrappad_functions),
286 ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
287 ossl_aes192wrappad_functions),
288 ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
289 ossl_aes128wrappad_functions),
290 ALG("AES-256-WRAP-INV:AES256-WRAP-INV", ossl_aes256wrapinv_functions),
291 ALG("AES-192-WRAP-INV:AES192-WRAP-INV", ossl_aes192wrapinv_functions),
292 ALG("AES-128-WRAP-INV:AES128-WRAP-INV", ossl_aes128wrapinv_functions),
293 ALG("AES-256-WRAP-PAD-INV:AES256-WRAP-PAD-INV",
294 ossl_aes256wrappadinv_functions),
295 ALG("AES-192-WRAP-PAD-INV:AES192-WRAP-PAD-INV",
296 ossl_aes192wrappadinv_functions),
297 ALG("AES-128-WRAP-PAD-INV:AES128-WRAP-PAD-INV",
298 ossl_aes128wrappadinv_functions),
299 ALGC("AES-128-CBC-HMAC-SHA1", ossl_aes128cbc_hmac_sha1_functions,
300 ossl_cipher_capable_aes_cbc_hmac_sha1),
301 ALGC("AES-256-CBC-HMAC-SHA1", ossl_aes256cbc_hmac_sha1_functions,
302 ossl_cipher_capable_aes_cbc_hmac_sha1),
303 ALGC("AES-128-CBC-HMAC-SHA256", ossl_aes128cbc_hmac_sha256_functions,
304 ossl_cipher_capable_aes_cbc_hmac_sha256),
305 ALGC("AES-256-CBC-HMAC-SHA256", ossl_aes256cbc_hmac_sha256_functions,
306 ossl_cipher_capable_aes_cbc_hmac_sha256),
307 #ifndef OPENSSL_NO_DES
308 ALG("DES-EDE3-ECB:DES-EDE3", ossl_tdes_ede3_ecb_functions),
309 ALG("DES-EDE3-CBC:DES3", ossl_tdes_ede3_cbc_functions),
310 #endif /* OPENSSL_NO_DES */
311 { { NULL, NULL, NULL }, NULL }
313 static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
315 static const OSSL_ALGORITHM fips_macs[] = {
316 #ifndef OPENSSL_NO_CMAC
317 { "CMAC", FIPS_DEFAULT_PROPERTIES, ossl_cmac_functions },
319 { "GMAC", FIPS_DEFAULT_PROPERTIES, ossl_gmac_functions },
320 { "HMAC", FIPS_DEFAULT_PROPERTIES, ossl_hmac_functions },
321 { "KMAC-128:KMAC128", FIPS_DEFAULT_PROPERTIES, ossl_kmac128_functions },
322 { "KMAC-256:KMAC256", FIPS_DEFAULT_PROPERTIES, ossl_kmac256_functions },
326 static const OSSL_ALGORITHM fips_kdfs[] = {
327 { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_functions },
328 { "SSKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_sskdf_functions },
329 { "PBKDF2", FIPS_DEFAULT_PROPERTIES, ossl_kdf_pbkdf2_functions },
330 { "SSHKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_sshkdf_functions },
331 { "X963KDF:X942KDF-CONCAT", FIPS_DEFAULT_PROPERTIES,
332 ossl_kdf_x963_kdf_functions },
333 { "X942KDF-ASN1:X942KDF", FIPS_DEFAULT_PROPERTIES,
334 ossl_kdf_x942_kdf_functions },
335 { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_tls1_prf_functions },
336 { "KBKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_kbkdf_functions },
340 static const OSSL_ALGORITHM fips_rands[] = {
341 { "CTR-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_ctr_functions },
342 { "HASH-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_hash_functions },
343 { "HMAC-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_ossl_hmac_functions },
344 { "TEST-RAND", FIPS_UNAPPROVED_PROPERTIES, ossl_test_rng_functions },
348 static const OSSL_ALGORITHM fips_keyexch[] = {
349 #ifndef OPENSSL_NO_DH
350 { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, ossl_dh_keyexch_functions },
352 #ifndef OPENSSL_NO_EC
353 { "ECDH", FIPS_DEFAULT_PROPERTIES, ecossl_dh_keyexch_functions },
354 { "X25519", FIPS_DEFAULT_PROPERTIES, ossl_x25519_keyexch_functions },
355 { "X448", FIPS_DEFAULT_PROPERTIES, ossl_x448_keyexch_functions },
357 { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES,
358 ossl_kdf_tls1_prf_keyexch_functions },
359 { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_keyexch_functions },
363 static const OSSL_ALGORITHM fips_signature[] = {
364 #ifndef OPENSSL_NO_DSA
365 { "DSA:dsaEncryption", FIPS_DEFAULT_PROPERTIES,
366 ossl_dsa_signature_functions },
368 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
369 ossl_rsa_signature_functions },
370 #ifndef OPENSSL_NO_EC
371 { "ED25519", FIPS_DEFAULT_PROPERTIES, ossl_ed25519_signature_functions },
372 { "ED448", FIPS_DEFAULT_PROPERTIES, ossl_ed448_signature_functions },
373 { "ECDSA", FIPS_DEFAULT_PROPERTIES, ecossl_dsa_signature_functions },
375 { "HMAC", FIPS_DEFAULT_PROPERTIES,
376 ossl_mac_legacy_hmac_signature_functions },
377 #ifndef OPENSSL_NO_CMAC
378 { "CMAC", FIPS_DEFAULT_PROPERTIES,
379 ossl_mac_legacy_cmac_signature_functions },
384 static const OSSL_ALGORITHM fips_asym_cipher[] = {
385 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
386 ossl_rsa_asym_cipher_functions },
390 static const OSSL_ALGORITHM fips_asym_kem[] = {
391 { "RSA", FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_kem_functions },
395 static const OSSL_ALGORITHM fips_keymgmt[] = {
396 #ifndef OPENSSL_NO_DH
397 { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, ossl_dh_keymgmt_functions },
398 { "DHX:X9.42 DH:dhpublicnumber", FIPS_DEFAULT_PROPERTIES,
399 ossl_dhx_keymgmt_functions },
401 #ifndef OPENSSL_NO_DSA
402 { "DSA", FIPS_DEFAULT_PROPERTIES, ossl_dsa_keymgmt_functions },
404 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
405 ossl_rsa_keymgmt_functions },
406 { "RSA-PSS:RSASSA-PSS", FIPS_DEFAULT_PROPERTIES,
407 ossl_rsapss_keymgmt_functions },
408 #ifndef OPENSSL_NO_EC
409 { "EC:id-ecPublicKey", FIPS_DEFAULT_PROPERTIES, ossl_ec_keymgmt_functions },
410 { "X25519", FIPS_DEFAULT_PROPERTIES, ossl_x25519_keymgmt_functions },
411 { "X448", FIPS_DEFAULT_PROPERTIES, ossl_x448_keymgmt_functions },
412 { "ED25519", FIPS_DEFAULT_PROPERTIES, ossl_ed25519_keymgmt_functions },
413 { "ED448", FIPS_DEFAULT_PROPERTIES, ossl_ed448_keymgmt_functions },
415 { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions },
416 { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions },
417 { "HMAC", FIPS_DEFAULT_PROPERTIES, ossl_mac_legacy_keymgmt_functions },
418 #ifndef OPENSSL_NO_CMAC
419 { "CMAC", FIPS_DEFAULT_PROPERTIES,
420 ossl_cossl_mac_legacy_keymgmt_functions },
425 static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
430 if (!ossl_prov_is_running())
433 switch (operation_id) {
437 ossl_prov_cache_exported_algorithms(fips_ciphers,
438 exported_fips_ciphers);
439 return exported_fips_ciphers;
446 case OSSL_OP_KEYMGMT:
448 case OSSL_OP_KEYEXCH:
450 case OSSL_OP_SIGNATURE:
451 return fips_signature;
452 case OSSL_OP_ASYM_CIPHER:
453 return fips_asym_cipher;
455 return fips_asym_kem;
460 static void fips_teardown(void *provctx)
462 OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
463 ossl_prov_ctx_free(provctx);
466 static void fips_intern_teardown(void *provctx)
469 * We know that the library context is the same as for the outer provider,
470 * so no need to destroy it here.
472 ossl_prov_ctx_free(provctx);
475 /* Functions we provide to the core */
476 static const OSSL_DISPATCH fips_dispatch_table[] = {
477 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
478 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
479 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
480 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
481 { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
482 (void (*)(void))provider_get_capabilities },
483 { OSSL_FUNC_PROVIDER_SELF_TEST, (void (*)(void))fips_self_test },
487 /* Functions we provide to ourself */
488 static const OSSL_DISPATCH intern_dispatch_table[] = {
489 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
490 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
494 int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
495 const OSSL_DISPATCH *in,
496 const OSSL_DISPATCH **out,
500 OSSL_LIB_CTX *libctx = NULL;
502 if (!ossl_prov_seeding_from_dispatch(in))
504 for (; in->function_id != 0; in++) {
505 switch (in->function_id) {
506 case OSSL_FUNC_CORE_GET_LIBCTX:
507 c_get_libctx = OSSL_FUNC_core_get_libctx(in);
509 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
510 c_gettable_params = OSSL_FUNC_core_gettable_params(in);
512 case OSSL_FUNC_CORE_GET_PARAMS:
513 c_get_params = OSSL_FUNC_core_get_params(in);
515 case OSSL_FUNC_CORE_THREAD_START:
516 c_thread_start = OSSL_FUNC_core_thread_start(in);
518 case OSSL_FUNC_CORE_NEW_ERROR:
519 c_new_error = OSSL_FUNC_core_new_error(in);
521 case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
522 c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
524 case OSSL_FUNC_CORE_VSET_ERROR:
525 c_vset_error = OSSL_FUNC_core_vset_error(in);
527 case OSSL_FUNC_CORE_SET_ERROR_MARK:
528 c_set_error_mark = OSSL_FUNC_core_set_error_mark(in);
530 case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
531 c_clear_last_error_mark = OSSL_FUNC_core_clear_last_error_mark(in);
533 case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
534 c_pop_error_to_mark = OSSL_FUNC_core_pop_error_to_mark(in);
536 case OSSL_FUNC_CRYPTO_MALLOC:
537 c_CRYPTO_malloc = OSSL_FUNC_CRYPTO_malloc(in);
539 case OSSL_FUNC_CRYPTO_ZALLOC:
540 c_CRYPTO_zalloc = OSSL_FUNC_CRYPTO_zalloc(in);
542 case OSSL_FUNC_CRYPTO_FREE:
543 c_CRYPTO_free = OSSL_FUNC_CRYPTO_free(in);
545 case OSSL_FUNC_CRYPTO_CLEAR_FREE:
546 c_CRYPTO_clear_free = OSSL_FUNC_CRYPTO_clear_free(in);
548 case OSSL_FUNC_CRYPTO_REALLOC:
549 c_CRYPTO_realloc = OSSL_FUNC_CRYPTO_realloc(in);
551 case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
552 c_CRYPTO_clear_realloc = OSSL_FUNC_CRYPTO_clear_realloc(in);
554 case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
555 c_CRYPTO_secure_malloc = OSSL_FUNC_CRYPTO_secure_malloc(in);
557 case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
558 c_CRYPTO_secure_zalloc = OSSL_FUNC_CRYPTO_secure_zalloc(in);
560 case OSSL_FUNC_CRYPTO_SECURE_FREE:
561 c_CRYPTO_secure_free = OSSL_FUNC_CRYPTO_secure_free(in);
563 case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
564 c_CRYPTO_secure_clear_free = OSSL_FUNC_CRYPTO_secure_clear_free(in);
566 case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
567 c_CRYPTO_secure_allocated = OSSL_FUNC_CRYPTO_secure_allocated(in);
569 case OSSL_FUNC_BIO_NEW_FILE:
570 selftest_params.bio_new_file_cb = OSSL_FUNC_BIO_new_file(in);
572 case OSSL_FUNC_BIO_NEW_MEMBUF:
573 selftest_params.bio_new_buffer_cb = OSSL_FUNC_BIO_new_membuf(in);
575 case OSSL_FUNC_BIO_READ_EX:
576 selftest_params.bio_read_ex_cb = OSSL_FUNC_BIO_read_ex(in);
578 case OSSL_FUNC_BIO_FREE:
579 selftest_params.bio_free_cb = OSSL_FUNC_BIO_free(in);
581 case OSSL_FUNC_BIO_VSNPRINTF:
582 c_BIO_vsnprintf = OSSL_FUNC_BIO_vsnprintf(in);
584 case OSSL_FUNC_SELF_TEST_CB:
585 c_stcbfn = OSSL_FUNC_self_test_cb(in);
588 /* Just ignore anything we don't understand */
593 set_self_test_cb(handle);
595 if (!c_get_params(handle, core_params)) {
596 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
599 /* Disable the conditional error check if is disabled in the fips config file*/
600 if (selftest_params.conditional_error_check != NULL
601 && strcmp(selftest_params.conditional_error_check, "0") == 0)
602 SELF_TEST_disable_conditional_error_state();
604 /* Disable the security check if is disabled in the fips config file*/
605 if (fips_security_check_option != NULL
606 && strcmp(fips_security_check_option, "0") == 0)
607 fips_security_checks = 0;
609 /* Create a context. */
610 if ((*provctx = ossl_prov_ctx_new()) == NULL
611 || (libctx = OSSL_LIB_CTX_new()) == NULL) {
613 * We free libctx separately here and only here because it hasn't
614 * been attached to *provctx. All other error paths below rely
615 * solely on fips_teardown.
617 OSSL_LIB_CTX_free(libctx);
620 ossl_prov_ctx_set0_libctx(*provctx, libctx);
621 ossl_prov_ctx_set0_handle(*provctx, handle);
623 if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX,
624 &fips_prov_ossl_ctx_method)) == NULL)
627 fgbl->handle = handle;
629 selftest_params.libctx = libctx;
630 if (!SELF_TEST_post(&selftest_params, 0)) {
631 ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_POST_FAILURE);
635 /* TODO(3.0): Tests will hang if this is removed */
636 (void)RAND_get0_public(libctx);
638 *out = fips_dispatch_table;
641 fips_teardown(*provctx);
647 * The internal init function used when the FIPS module uses EVP to call
648 * another algorithm also in the FIPS module. This is a recursive call that has
649 * been made from within the FIPS module itself. To make this work, we populate
650 * the provider context of this inner instance with the same library context
651 * that was used in the EVP call that initiated this recursive call.
653 OSSL_provider_init_fn fips_intern_provider_init;
654 int fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
655 const OSSL_DISPATCH *in,
656 const OSSL_DISPATCH **out,
659 OSSL_FUNC_core_get_libctx_fn *c_internal_get_libctx = NULL;
661 for (; in->function_id != 0; in++) {
662 switch (in->function_id) {
663 case OSSL_FUNC_CORE_GET_LIBCTX:
664 c_internal_get_libctx = OSSL_FUNC_core_get_libctx(in);
671 if (c_internal_get_libctx == NULL)
674 if ((*provctx = ossl_prov_ctx_new()) == NULL)
678 * Using the parent library context only works because we are a built-in
679 * internal provider. This is not something that most providers would be
682 ossl_prov_ctx_set0_libctx(*provctx,
683 (OSSL_LIB_CTX *)c_internal_get_libctx(handle));
684 ossl_prov_ctx_set0_handle(*provctx, handle);
686 *out = intern_dispatch_table;
695 void ERR_set_debug(const char *file, int line, const char *func)
697 c_set_error_debug(NULL, file, line, func);
700 void ERR_set_error(int lib, int reason, const char *fmt, ...)
705 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
709 void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
711 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
714 int ERR_set_mark(void)
716 return c_set_error_mark(NULL);
719 int ERR_clear_last_mark(void)
721 return c_clear_last_error_mark(NULL);
724 int ERR_pop_to_mark(void)
726 return c_pop_error_to_mark(NULL);
730 * This must take a library context, since it's called from the depths
731 * of crypto/initthread.c code, where it's (correctly) assumed that the
732 * passed caller argument is an OSSL_LIB_CTX pointer (since the same routine
733 * is also called from other parts of libcrypto, which all pass around a
734 * OSSL_LIB_CTX pointer)
736 const OSSL_CORE_HANDLE *FIPS_get_core_handle(OSSL_LIB_CTX *libctx)
738 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
739 OSSL_LIB_CTX_FIPS_PROV_INDEX,
740 &fips_prov_ossl_ctx_method);
748 void *CRYPTO_malloc(size_t num, const char *file, int line)
750 return c_CRYPTO_malloc(num, file, line);
753 void *CRYPTO_zalloc(size_t num, const char *file, int line)
755 return c_CRYPTO_zalloc(num, file, line);
758 void CRYPTO_free(void *ptr, const char *file, int line)
760 c_CRYPTO_free(ptr, file, line);
763 void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
765 c_CRYPTO_clear_free(ptr, num, file, line);
768 void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
770 return c_CRYPTO_realloc(addr, num, file, line);
773 void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
774 const char *file, int line)
776 return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
779 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
781 return c_CRYPTO_secure_malloc(num, file, line);
784 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
786 return c_CRYPTO_secure_zalloc(num, file, line);
789 void CRYPTO_secure_free(void *ptr, const char *file, int line)
791 c_CRYPTO_secure_free(ptr, file, line);
794 void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
796 c_CRYPTO_secure_clear_free(ptr, num, file, line);
799 int CRYPTO_secure_allocated(const void *ptr)
801 return c_CRYPTO_secure_allocated(ptr);
804 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
809 va_start(args, format);
810 ret = c_BIO_vsnprintf(buf, n, format, args);
815 int FIPS_security_check_enabled(void)
817 return fips_security_checks;
820 void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
824 libctx = selftest_params.libctx;
826 if (c_stcbfn != NULL && c_get_libctx != NULL) {
827 /* Get the parent libctx */
828 c_stcbfn(c_get_libctx(FIPS_get_core_handle(libctx)), cb, cbarg);