2 * Copyright 2019-2022 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/core_dispatch.h>
12 #include <openssl/core_names.h>
13 #include <openssl/params.h>
14 #include <openssl/fips_names.h>
15 #include <openssl/rand.h> /* RAND_get0_public() */
16 #include <openssl/proverr.h>
17 #include "internal/cryptlib.h"
18 #include "prov/implementations.h"
19 #include "prov/names.h"
20 #include "prov/provider_ctx.h"
21 #include "prov/providercommon.h"
22 #include "prov/provider_util.h"
23 #include "prov/seeding.h"
24 #include "internal/nelem.h"
25 #include "self_test.h"
26 #include "crypto/context.h"
27 #include "internal/core.h"
29 static const char FIPS_DEFAULT_PROPERTIES[] = "provider=fips,fips=yes";
30 static const char FIPS_UNAPPROVED_PROPERTIES[] = "provider=fips,fips=no";
33 * Forward declarations to ensure that interface functions are correctly
36 static OSSL_FUNC_provider_teardown_fn fips_teardown;
37 static OSSL_FUNC_provider_gettable_params_fn fips_gettable_params;
38 static OSSL_FUNC_provider_get_params_fn fips_get_params;
39 static OSSL_FUNC_provider_query_operation_fn fips_query;
41 #define ALGC(NAMES, FUNC, CHECK) \
42 { { NAMES, FIPS_DEFAULT_PROPERTIES, FUNC }, CHECK }
43 #define UNAPPROVED_ALGC(NAMES, FUNC, CHECK) \
44 { { NAMES, FIPS_UNAPPROVED_PROPERTIES, FUNC }, CHECK }
45 #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
46 #define UNAPPROVED_ALG(NAMES, FUNC) UNAPPROVED_ALGC(NAMES, FUNC, NULL)
48 extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
49 int FIPS_security_check_enabled(OSSL_LIB_CTX *libctx);
52 * Should these function pointers be stored in the provider side provctx? Could
53 * they ever be different from one init to the next? We assume not for now.
56 /* Functions provided by the core */
57 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params;
58 static OSSL_FUNC_core_get_params_fn *c_get_params;
59 OSSL_FUNC_core_thread_start_fn *c_thread_start;
60 static OSSL_FUNC_core_new_error_fn *c_new_error;
61 static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
62 static OSSL_FUNC_core_vset_error_fn *c_vset_error;
63 static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
64 static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
65 static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
66 static OSSL_FUNC_CRYPTO_malloc_fn *c_CRYPTO_malloc;
67 static OSSL_FUNC_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
68 static OSSL_FUNC_CRYPTO_free_fn *c_CRYPTO_free;
69 static OSSL_FUNC_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
70 static OSSL_FUNC_CRYPTO_realloc_fn *c_CRYPTO_realloc;
71 static OSSL_FUNC_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
72 static OSSL_FUNC_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
73 static OSSL_FUNC_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
74 static OSSL_FUNC_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
75 static OSSL_FUNC_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
76 static OSSL_FUNC_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
77 static OSSL_FUNC_BIO_vsnprintf_fn *c_BIO_vsnprintf;
78 static OSSL_FUNC_self_test_cb_fn *c_stcbfn = NULL;
79 static OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
81 typedef struct fips_global_st {
82 const OSSL_CORE_HANDLE *handle;
83 SELF_TEST_POST_PARAMS selftest_params;
84 int fips_security_checks;
85 const char *fips_security_check_option;
88 void *ossl_fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
90 FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
94 fgbl->fips_security_checks = 1;
95 fgbl->fips_security_check_option = "1";
100 void ossl_fips_prov_ossl_ctx_free(void *fgbl)
105 /* Parameters we provide to the core */
106 static const OSSL_PARAM fips_param_types[] = {
107 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
108 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
109 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
110 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
111 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_SECURITY_CHECKS, OSSL_PARAM_INTEGER, NULL, 0),
115 static int fips_get_params_from_core(FIPS_GLOBAL *fgbl)
118 * Parameters to retrieve from the core provider - required for self testing.
119 * NOTE: inside core_get_params() these will be loaded from config items
120 * stored inside prov->parameters (except for
121 * OSSL_PROV_PARAM_CORE_MODULE_FILENAME).
122 * OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS is not a self test parameter.
124 OSSL_PARAM core_params[8], *p = core_params;
126 *p++ = OSSL_PARAM_construct_utf8_ptr(
127 OSSL_PROV_PARAM_CORE_MODULE_FILENAME,
128 (char **)&fgbl->selftest_params.module_filename,
129 sizeof(fgbl->selftest_params.module_filename));
130 *p++ = OSSL_PARAM_construct_utf8_ptr(
131 OSSL_PROV_FIPS_PARAM_MODULE_MAC,
132 (char **)&fgbl->selftest_params.module_checksum_data,
133 sizeof(fgbl->selftest_params.module_checksum_data));
134 *p++ = OSSL_PARAM_construct_utf8_ptr(
135 OSSL_PROV_FIPS_PARAM_INSTALL_MAC,
136 (char **)&fgbl->selftest_params.indicator_checksum_data,
137 sizeof(fgbl->selftest_params.indicator_checksum_data));
138 *p++ = OSSL_PARAM_construct_utf8_ptr(
139 OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
140 (char **)&fgbl->selftest_params.indicator_data,
141 sizeof(fgbl->selftest_params.indicator_data));
142 *p++ = OSSL_PARAM_construct_utf8_ptr(
143 OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
144 (char **)&fgbl->selftest_params.indicator_version,
145 sizeof(fgbl->selftest_params.indicator_version));
146 *p++ = OSSL_PARAM_construct_utf8_ptr(
147 OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
148 (char **)&fgbl->selftest_params.conditional_error_check,
149 sizeof(fgbl->selftest_params.conditional_error_check));
150 *p++ = OSSL_PARAM_construct_utf8_ptr(
151 OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
152 (char **)&fgbl->fips_security_check_option,
153 sizeof(fgbl->fips_security_check_option));
154 *p = OSSL_PARAM_construct_end();
156 if (!c_get_params(fgbl->handle, core_params)) {
157 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
164 static const OSSL_PARAM *fips_gettable_params(void *provctx)
166 return fips_param_types;
169 static int fips_get_params(void *provctx, OSSL_PARAM params[])
172 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(ossl_prov_ctx_get0_libctx(provctx),
173 OSSL_LIB_CTX_FIPS_PROV_INDEX);
175 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
176 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
178 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
179 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
181 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
182 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
184 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
185 if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
187 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_SECURITY_CHECKS);
188 if (p != NULL && !OSSL_PARAM_set_int(p, fgbl->fips_security_checks))
193 static void set_self_test_cb(FIPS_GLOBAL *fgbl)
195 const OSSL_CORE_HANDLE *handle =
196 FIPS_get_core_handle(fgbl->selftest_params.libctx);
198 if (c_stcbfn != NULL && c_get_libctx != NULL) {
199 c_stcbfn(c_get_libctx(handle), &fgbl->selftest_params.cb,
200 &fgbl->selftest_params.cb_arg);
202 fgbl->selftest_params.cb = NULL;
203 fgbl->selftest_params.cb_arg = NULL;
207 static int fips_self_test(void *provctx)
209 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(ossl_prov_ctx_get0_libctx(provctx),
210 OSSL_LIB_CTX_FIPS_PROV_INDEX);
212 set_self_test_cb(fgbl);
213 return SELF_TEST_post(&fgbl->selftest_params, 1) ? 1 : 0;
217 * For the algorithm names, we use the following formula for our primary
220 * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
222 * VERSION is only present if there are multiple versions of
223 * an alg (MD2, MD4, MD5). It may be omitted if there is only
224 * one version (if a subsequent version is released in the future,
225 * we can always change the canonical name, and add the old name
228 * SUBNAME may be present where we are combining multiple
229 * algorithms together, e.g. MD5-SHA1.
231 * SIZE is only present if multiple versions of an algorithm exist
232 * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
234 * MODE is only present where applicable.
236 * We add diverse other names where applicable, such as the names that
237 * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
238 * we have used historically.
240 static const OSSL_ALGORITHM fips_digests[] = {
241 /* Our primary name:NiST name[:our older names] */
242 { PROV_NAMES_SHA1, FIPS_DEFAULT_PROPERTIES, ossl_sha1_functions },
243 { PROV_NAMES_SHA2_224, FIPS_DEFAULT_PROPERTIES, ossl_sha224_functions },
244 { PROV_NAMES_SHA2_256, FIPS_DEFAULT_PROPERTIES, ossl_sha256_functions },
245 { PROV_NAMES_SHA2_384, FIPS_DEFAULT_PROPERTIES, ossl_sha384_functions },
246 { PROV_NAMES_SHA2_512, FIPS_DEFAULT_PROPERTIES, ossl_sha512_functions },
247 { PROV_NAMES_SHA2_512_224, FIPS_DEFAULT_PROPERTIES,
248 ossl_sha512_224_functions },
249 { PROV_NAMES_SHA2_512_256, FIPS_DEFAULT_PROPERTIES,
250 ossl_sha512_256_functions },
252 /* We agree with NIST here, so one name only */
253 { PROV_NAMES_SHA3_224, FIPS_DEFAULT_PROPERTIES, ossl_sha3_224_functions },
254 { PROV_NAMES_SHA3_256, FIPS_DEFAULT_PROPERTIES, ossl_sha3_256_functions },
255 { PROV_NAMES_SHA3_384, FIPS_DEFAULT_PROPERTIES, ossl_sha3_384_functions },
256 { PROV_NAMES_SHA3_512, FIPS_DEFAULT_PROPERTIES, ossl_sha3_512_functions },
258 { PROV_NAMES_SHAKE_128, FIPS_DEFAULT_PROPERTIES, ossl_shake_128_functions },
259 { PROV_NAMES_SHAKE_256, FIPS_DEFAULT_PROPERTIES, ossl_shake_256_functions },
262 * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
263 * KMAC128 and KMAC256.
265 { PROV_NAMES_KECCAK_KMAC_128, FIPS_DEFAULT_PROPERTIES,
266 ossl_keccak_kmac_128_functions },
267 { PROV_NAMES_KECCAK_KMAC_256, FIPS_DEFAULT_PROPERTIES,
268 ossl_keccak_kmac_256_functions },
272 static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
273 /* Our primary name[:ASN.1 OID name][:our older names] */
274 ALG(PROV_NAMES_AES_256_ECB, ossl_aes256ecb_functions),
275 ALG(PROV_NAMES_AES_192_ECB, ossl_aes192ecb_functions),
276 ALG(PROV_NAMES_AES_128_ECB, ossl_aes128ecb_functions),
277 ALG(PROV_NAMES_AES_256_CBC, ossl_aes256cbc_functions),
278 ALG(PROV_NAMES_AES_192_CBC, ossl_aes192cbc_functions),
279 ALG(PROV_NAMES_AES_128_CBC, ossl_aes128cbc_functions),
280 ALG(PROV_NAMES_AES_256_CBC_CTS, ossl_aes256cbc_cts_functions),
281 ALG(PROV_NAMES_AES_192_CBC_CTS, ossl_aes192cbc_cts_functions),
282 ALG(PROV_NAMES_AES_128_CBC_CTS, ossl_aes128cbc_cts_functions),
283 ALG(PROV_NAMES_AES_256_OFB, ossl_aes256ofb_functions),
284 ALG(PROV_NAMES_AES_192_OFB, ossl_aes192ofb_functions),
285 ALG(PROV_NAMES_AES_128_OFB, ossl_aes128ofb_functions),
286 ALG(PROV_NAMES_AES_256_CFB, ossl_aes256cfb_functions),
287 ALG(PROV_NAMES_AES_192_CFB, ossl_aes192cfb_functions),
288 ALG(PROV_NAMES_AES_128_CFB, ossl_aes128cfb_functions),
289 ALG(PROV_NAMES_AES_256_CFB1, ossl_aes256cfb1_functions),
290 ALG(PROV_NAMES_AES_192_CFB1, ossl_aes192cfb1_functions),
291 ALG(PROV_NAMES_AES_128_CFB1, ossl_aes128cfb1_functions),
292 ALG(PROV_NAMES_AES_256_CFB8, ossl_aes256cfb8_functions),
293 ALG(PROV_NAMES_AES_192_CFB8, ossl_aes192cfb8_functions),
294 ALG(PROV_NAMES_AES_128_CFB8, ossl_aes128cfb8_functions),
295 ALG(PROV_NAMES_AES_256_CTR, ossl_aes256ctr_functions),
296 ALG(PROV_NAMES_AES_192_CTR, ossl_aes192ctr_functions),
297 ALG(PROV_NAMES_AES_128_CTR, ossl_aes128ctr_functions),
298 ALG(PROV_NAMES_AES_256_XTS, ossl_aes256xts_functions),
299 ALG(PROV_NAMES_AES_128_XTS, ossl_aes128xts_functions),
300 #ifndef OPENSSL_NO_SIV
301 ALG(PROV_NAMES_AES_128_GCM_SIV, ossl_aes128gcm_siv_functions),
302 ALG(PROV_NAMES_AES_192_GCM_SIV, ossl_aes192gcm_siv_functions),
303 ALG(PROV_NAMES_AES_256_GCM_SIV, ossl_aes256gcm_siv_functions),
304 #endif /* OPENSSL_NO_SIV */
305 ALG(PROV_NAMES_AES_256_GCM, ossl_aes256gcm_functions),
306 ALG(PROV_NAMES_AES_192_GCM, ossl_aes192gcm_functions),
307 ALG(PROV_NAMES_AES_128_GCM, ossl_aes128gcm_functions),
308 ALG(PROV_NAMES_AES_256_CCM, ossl_aes256ccm_functions),
309 ALG(PROV_NAMES_AES_192_CCM, ossl_aes192ccm_functions),
310 ALG(PROV_NAMES_AES_128_CCM, ossl_aes128ccm_functions),
311 ALG(PROV_NAMES_AES_256_WRAP, ossl_aes256wrap_functions),
312 ALG(PROV_NAMES_AES_192_WRAP, ossl_aes192wrap_functions),
313 ALG(PROV_NAMES_AES_128_WRAP, ossl_aes128wrap_functions),
314 ALG(PROV_NAMES_AES_256_WRAP_PAD, ossl_aes256wrappad_functions),
315 ALG(PROV_NAMES_AES_192_WRAP_PAD, ossl_aes192wrappad_functions),
316 ALG(PROV_NAMES_AES_128_WRAP_PAD, ossl_aes128wrappad_functions),
317 ALG(PROV_NAMES_AES_256_WRAP_INV, ossl_aes256wrapinv_functions),
318 ALG(PROV_NAMES_AES_192_WRAP_INV, ossl_aes192wrapinv_functions),
319 ALG(PROV_NAMES_AES_128_WRAP_INV, ossl_aes128wrapinv_functions),
320 ALG(PROV_NAMES_AES_256_WRAP_PAD_INV, ossl_aes256wrappadinv_functions),
321 ALG(PROV_NAMES_AES_192_WRAP_PAD_INV, ossl_aes192wrappadinv_functions),
322 ALG(PROV_NAMES_AES_128_WRAP_PAD_INV, ossl_aes128wrappadinv_functions),
323 ALGC(PROV_NAMES_AES_128_CBC_HMAC_SHA1, ossl_aes128cbc_hmac_sha1_functions,
324 ossl_cipher_capable_aes_cbc_hmac_sha1),
325 ALGC(PROV_NAMES_AES_256_CBC_HMAC_SHA1, ossl_aes256cbc_hmac_sha1_functions,
326 ossl_cipher_capable_aes_cbc_hmac_sha1),
327 ALGC(PROV_NAMES_AES_128_CBC_HMAC_SHA256, ossl_aes128cbc_hmac_sha256_functions,
328 ossl_cipher_capable_aes_cbc_hmac_sha256),
329 ALGC(PROV_NAMES_AES_256_CBC_HMAC_SHA256, ossl_aes256cbc_hmac_sha256_functions,
330 ossl_cipher_capable_aes_cbc_hmac_sha256),
331 #ifndef OPENSSL_NO_DES
332 UNAPPROVED_ALG(PROV_NAMES_DES_EDE3_ECB, ossl_tdes_ede3_ecb_functions),
333 UNAPPROVED_ALG(PROV_NAMES_DES_EDE3_CBC, ossl_tdes_ede3_cbc_functions),
334 #endif /* OPENSSL_NO_DES */
335 { { NULL, NULL, NULL }, NULL }
337 static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
339 static const OSSL_ALGORITHM fips_macs[] = {
340 #ifndef OPENSSL_NO_CMAC
341 { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES, ossl_cmac_functions },
343 { PROV_NAMES_GMAC, FIPS_DEFAULT_PROPERTIES, ossl_gmac_functions },
344 { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES, ossl_hmac_functions },
345 { PROV_NAMES_KMAC_128, FIPS_DEFAULT_PROPERTIES, ossl_kmac128_functions },
346 { PROV_NAMES_KMAC_256, FIPS_DEFAULT_PROPERTIES, ossl_kmac256_functions },
350 static const OSSL_ALGORITHM fips_kdfs[] = {
351 { PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_functions },
352 { PROV_NAMES_TLS1_3_KDF, FIPS_DEFAULT_PROPERTIES,
353 ossl_kdf_tls1_3_kdf_functions },
354 { PROV_NAMES_SSKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_sskdf_functions },
355 { PROV_NAMES_PBKDF2, FIPS_DEFAULT_PROPERTIES, ossl_kdf_pbkdf2_functions },
356 { PROV_NAMES_SSHKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_sshkdf_functions },
357 { PROV_NAMES_X963KDF, FIPS_DEFAULT_PROPERTIES,
358 ossl_kdf_x963_kdf_functions },
359 { PROV_NAMES_X942KDF_ASN1, FIPS_DEFAULT_PROPERTIES,
360 ossl_kdf_x942_kdf_functions },
361 { PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES,
362 ossl_kdf_tls1_prf_functions },
363 { PROV_NAMES_KBKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_kbkdf_functions },
367 static const OSSL_ALGORITHM fips_rands[] = {
368 { PROV_NAMES_CTR_DRBG, FIPS_DEFAULT_PROPERTIES, ossl_drbg_ctr_functions },
369 { PROV_NAMES_HASH_DRBG, FIPS_DEFAULT_PROPERTIES, ossl_drbg_hash_functions },
370 { PROV_NAMES_HMAC_DRBG, FIPS_DEFAULT_PROPERTIES, ossl_drbg_ossl_hmac_functions },
371 { PROV_NAMES_TEST_RAND, FIPS_UNAPPROVED_PROPERTIES, ossl_test_rng_functions },
375 static const OSSL_ALGORITHM fips_keyexch[] = {
376 #ifndef OPENSSL_NO_DH
377 { PROV_NAMES_DH, FIPS_DEFAULT_PROPERTIES, ossl_dh_keyexch_functions },
379 #ifndef OPENSSL_NO_EC
380 { PROV_NAMES_ECDH, FIPS_DEFAULT_PROPERTIES, ossl_ecdh_keyexch_functions },
381 { PROV_NAMES_X25519, FIPS_DEFAULT_PROPERTIES, ossl_x25519_keyexch_functions },
382 { PROV_NAMES_X448, FIPS_DEFAULT_PROPERTIES, ossl_x448_keyexch_functions },
384 { PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES,
385 ossl_kdf_tls1_prf_keyexch_functions },
386 { PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_keyexch_functions },
390 static const OSSL_ALGORITHM fips_signature[] = {
391 #ifndef OPENSSL_NO_DSA
392 { PROV_NAMES_DSA, FIPS_DEFAULT_PROPERTIES, ossl_dsa_signature_functions },
394 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_signature_functions },
395 #ifndef OPENSSL_NO_EC
396 { PROV_NAMES_ED25519, FIPS_DEFAULT_PROPERTIES, ossl_ed25519_signature_functions },
397 { PROV_NAMES_ED448, FIPS_DEFAULT_PROPERTIES, ossl_ed448_signature_functions },
398 { PROV_NAMES_ECDSA, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_signature_functions },
400 { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES,
401 ossl_mac_legacy_hmac_signature_functions },
402 #ifndef OPENSSL_NO_CMAC
403 { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES,
404 ossl_mac_legacy_cmac_signature_functions },
409 static const OSSL_ALGORITHM fips_asym_cipher[] = {
410 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_cipher_functions },
414 static const OSSL_ALGORITHM fips_asym_kem[] = {
415 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_kem_functions },
419 static const OSSL_ALGORITHM fips_keymgmt[] = {
420 #ifndef OPENSSL_NO_DH
421 { PROV_NAMES_DH, FIPS_DEFAULT_PROPERTIES, ossl_dh_keymgmt_functions,
423 { PROV_NAMES_DHX, FIPS_DEFAULT_PROPERTIES, ossl_dhx_keymgmt_functions,
426 #ifndef OPENSSL_NO_DSA
427 { PROV_NAMES_DSA, FIPS_DEFAULT_PROPERTIES, ossl_dsa_keymgmt_functions,
430 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_keymgmt_functions,
432 { PROV_NAMES_RSA_PSS, FIPS_DEFAULT_PROPERTIES,
433 ossl_rsapss_keymgmt_functions, PROV_DESCS_RSA_PSS },
434 #ifndef OPENSSL_NO_EC
435 { PROV_NAMES_EC, FIPS_DEFAULT_PROPERTIES, ossl_ec_keymgmt_functions,
437 { PROV_NAMES_X25519, FIPS_DEFAULT_PROPERTIES, ossl_x25519_keymgmt_functions,
439 { PROV_NAMES_X448, FIPS_DEFAULT_PROPERTIES, ossl_x448_keymgmt_functions,
441 { PROV_NAMES_ED25519, FIPS_DEFAULT_PROPERTIES, ossl_ed25519_keymgmt_functions,
442 PROV_DESCS_ED25519 },
443 { PROV_NAMES_ED448, FIPS_DEFAULT_PROPERTIES, ossl_ed448_keymgmt_functions,
446 { PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
447 PROV_DESCS_TLS1_PRF_SIGN },
448 { PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
449 PROV_DESCS_HKDF_SIGN },
450 { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES, ossl_mac_legacy_keymgmt_functions,
451 PROV_DESCS_HMAC_SIGN },
452 #ifndef OPENSSL_NO_CMAC
453 { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES,
454 ossl_cmac_legacy_keymgmt_functions, PROV_DESCS_CMAC_SIGN },
459 static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
464 if (!ossl_prov_is_running())
467 switch (operation_id) {
471 return exported_fips_ciphers;
478 case OSSL_OP_KEYMGMT:
480 case OSSL_OP_KEYEXCH:
482 case OSSL_OP_SIGNATURE:
483 return fips_signature;
484 case OSSL_OP_ASYM_CIPHER:
485 return fips_asym_cipher;
487 return fips_asym_kem;
492 static void fips_teardown(void *provctx)
494 OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
495 ossl_prov_ctx_free(provctx);
498 static void fips_intern_teardown(void *provctx)
501 * We know that the library context is the same as for the outer provider,
502 * so no need to destroy it here.
504 ossl_prov_ctx_free(provctx);
507 /* Functions we provide to the core */
508 static const OSSL_DISPATCH fips_dispatch_table[] = {
509 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
510 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
511 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
512 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
513 { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
514 (void (*)(void))ossl_prov_get_capabilities },
515 { OSSL_FUNC_PROVIDER_SELF_TEST, (void (*)(void))fips_self_test },
519 /* Functions we provide to ourself */
520 static const OSSL_DISPATCH intern_dispatch_table[] = {
521 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
522 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
527 * On VMS, the provider init function name is expected to be uppercase,
528 * see the pragmas in <openssl/core.h>. Let's do the same with this
529 * internal name. This is how symbol names are treated by default
530 * by the compiler if nothing else is said, but since this is part
531 * of libfips, and we build our libraries with mixed case symbol names,
532 * we must switch back to this default explicitly here.
536 # pragma names uppercase,truncated
538 OSSL_provider_init_fn OSSL_provider_init_int;
540 # pragma names restore
542 int OSSL_provider_init_int(const OSSL_CORE_HANDLE *handle,
543 const OSSL_DISPATCH *in,
544 const OSSL_DISPATCH **out,
548 OSSL_LIB_CTX *libctx = NULL;
549 SELF_TEST_POST_PARAMS selftest_params;
551 memset(&selftest_params, 0, sizeof(selftest_params));
553 if (!ossl_prov_seeding_from_dispatch(in))
555 for (; in->function_id != 0; in++) {
557 * We do not support the scenario of an application linked against
558 * multiple versions of libcrypto (e.g. one static and one dynamic), but
559 * sharing a single fips.so. We do a simple sanity check here.
561 #define set_func(c, f) if (c == NULL) c = f; else if (c != f) return 0;
562 switch (in->function_id) {
563 case OSSL_FUNC_CORE_GET_LIBCTX:
564 set_func(c_get_libctx, OSSL_FUNC_core_get_libctx(in));
566 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
567 set_func(c_gettable_params, OSSL_FUNC_core_gettable_params(in));
569 case OSSL_FUNC_CORE_GET_PARAMS:
570 set_func(c_get_params, OSSL_FUNC_core_get_params(in));
572 case OSSL_FUNC_CORE_THREAD_START:
573 set_func(c_thread_start, OSSL_FUNC_core_thread_start(in));
575 case OSSL_FUNC_CORE_NEW_ERROR:
576 set_func(c_new_error, OSSL_FUNC_core_new_error(in));
578 case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
579 set_func(c_set_error_debug, OSSL_FUNC_core_set_error_debug(in));
581 case OSSL_FUNC_CORE_VSET_ERROR:
582 set_func(c_vset_error, OSSL_FUNC_core_vset_error(in));
584 case OSSL_FUNC_CORE_SET_ERROR_MARK:
585 set_func(c_set_error_mark, OSSL_FUNC_core_set_error_mark(in));
587 case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
588 set_func(c_clear_last_error_mark,
589 OSSL_FUNC_core_clear_last_error_mark(in));
591 case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
592 set_func(c_pop_error_to_mark, OSSL_FUNC_core_pop_error_to_mark(in));
594 case OSSL_FUNC_CRYPTO_MALLOC:
595 set_func(c_CRYPTO_malloc, OSSL_FUNC_CRYPTO_malloc(in));
597 case OSSL_FUNC_CRYPTO_ZALLOC:
598 set_func(c_CRYPTO_zalloc, OSSL_FUNC_CRYPTO_zalloc(in));
600 case OSSL_FUNC_CRYPTO_FREE:
601 set_func(c_CRYPTO_free, OSSL_FUNC_CRYPTO_free(in));
603 case OSSL_FUNC_CRYPTO_CLEAR_FREE:
604 set_func(c_CRYPTO_clear_free, OSSL_FUNC_CRYPTO_clear_free(in));
606 case OSSL_FUNC_CRYPTO_REALLOC:
607 set_func(c_CRYPTO_realloc, OSSL_FUNC_CRYPTO_realloc(in));
609 case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
610 set_func(c_CRYPTO_clear_realloc,
611 OSSL_FUNC_CRYPTO_clear_realloc(in));
613 case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
614 set_func(c_CRYPTO_secure_malloc,
615 OSSL_FUNC_CRYPTO_secure_malloc(in));
617 case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
618 set_func(c_CRYPTO_secure_zalloc,
619 OSSL_FUNC_CRYPTO_secure_zalloc(in));
621 case OSSL_FUNC_CRYPTO_SECURE_FREE:
622 set_func(c_CRYPTO_secure_free,
623 OSSL_FUNC_CRYPTO_secure_free(in));
625 case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
626 set_func(c_CRYPTO_secure_clear_free,
627 OSSL_FUNC_CRYPTO_secure_clear_free(in));
629 case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
630 set_func(c_CRYPTO_secure_allocated,
631 OSSL_FUNC_CRYPTO_secure_allocated(in));
633 case OSSL_FUNC_BIO_NEW_FILE:
634 set_func(selftest_params.bio_new_file_cb,
635 OSSL_FUNC_BIO_new_file(in));
637 case OSSL_FUNC_BIO_NEW_MEMBUF:
638 set_func(selftest_params.bio_new_buffer_cb,
639 OSSL_FUNC_BIO_new_membuf(in));
641 case OSSL_FUNC_BIO_READ_EX:
642 set_func(selftest_params.bio_read_ex_cb,
643 OSSL_FUNC_BIO_read_ex(in));
645 case OSSL_FUNC_BIO_FREE:
646 set_func(selftest_params.bio_free_cb, OSSL_FUNC_BIO_free(in));
648 case OSSL_FUNC_BIO_VSNPRINTF:
649 set_func(c_BIO_vsnprintf, OSSL_FUNC_BIO_vsnprintf(in));
651 case OSSL_FUNC_SELF_TEST_CB:
652 set_func(c_stcbfn, OSSL_FUNC_self_test_cb(in));
655 /* Just ignore anything we don't understand */
660 /* Create a context. */
661 if ((*provctx = ossl_prov_ctx_new()) == NULL
662 || (libctx = OSSL_LIB_CTX_new()) == NULL) {
664 * We free libctx separately here and only here because it hasn't
665 * been attached to *provctx. All other error paths below rely
666 * solely on fips_teardown.
668 OSSL_LIB_CTX_free(libctx);
672 if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX)) == NULL)
675 fgbl->handle = handle;
678 * We did initial set up of selftest_params in a local copy, because we
679 * could not create fgbl until c_CRYPTO_zalloc was defined in the loop
682 fgbl->selftest_params = selftest_params;
684 fgbl->selftest_params.libctx = libctx;
686 set_self_test_cb(fgbl);
688 if (!fips_get_params_from_core(fgbl)) {
689 /* Error already raised */
693 * Disable the conditional error check if it's disabled in the fips config
696 if (fgbl->selftest_params.conditional_error_check != NULL
697 && strcmp(fgbl->selftest_params.conditional_error_check, "0") == 0)
698 SELF_TEST_disable_conditional_error_state();
700 /* Disable the security check if it's disabled in the fips config file. */
701 if (fgbl->fips_security_check_option != NULL
702 && strcmp(fgbl->fips_security_check_option, "0") == 0)
703 fgbl->fips_security_checks = 0;
705 ossl_prov_cache_exported_algorithms(fips_ciphers, exported_fips_ciphers);
707 if (!SELF_TEST_post(&fgbl->selftest_params, 0)) {
708 ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_POST_FAILURE);
712 ossl_prov_ctx_set0_libctx(*provctx, libctx);
713 ossl_prov_ctx_set0_handle(*provctx, handle);
715 *out = fips_dispatch_table;
718 fips_teardown(*provctx);
719 OSSL_LIB_CTX_free(libctx);
725 * The internal init function used when the FIPS module uses EVP to call
726 * another algorithm also in the FIPS module. This is a recursive call that has
727 * been made from within the FIPS module itself. To make this work, we populate
728 * the provider context of this inner instance with the same library context
729 * that was used in the EVP call that initiated this recursive call.
731 OSSL_provider_init_fn ossl_fips_intern_provider_init;
732 int ossl_fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
733 const OSSL_DISPATCH *in,
734 const OSSL_DISPATCH **out,
737 OSSL_FUNC_core_get_libctx_fn *c_internal_get_libctx = NULL;
739 for (; in->function_id != 0; in++) {
740 switch (in->function_id) {
741 case OSSL_FUNC_CORE_GET_LIBCTX:
742 c_internal_get_libctx = OSSL_FUNC_core_get_libctx(in);
749 if (c_internal_get_libctx == NULL)
752 if ((*provctx = ossl_prov_ctx_new()) == NULL)
756 * Using the parent library context only works because we are a built-in
757 * internal provider. This is not something that most providers would be
760 ossl_prov_ctx_set0_libctx(*provctx,
761 (OSSL_LIB_CTX *)c_internal_get_libctx(handle));
762 ossl_prov_ctx_set0_handle(*provctx, handle);
764 *out = intern_dispatch_table;
773 void ERR_set_debug(const char *file, int line, const char *func)
775 c_set_error_debug(NULL, file, line, func);
778 void ERR_set_error(int lib, int reason, const char *fmt, ...)
783 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
787 void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
789 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
792 int ERR_set_mark(void)
794 return c_set_error_mark(NULL);
797 int ERR_clear_last_mark(void)
799 return c_clear_last_error_mark(NULL);
802 int ERR_pop_to_mark(void)
804 return c_pop_error_to_mark(NULL);
808 * This must take a library context, since it's called from the depths
809 * of crypto/initthread.c code, where it's (correctly) assumed that the
810 * passed caller argument is an OSSL_LIB_CTX pointer (since the same routine
811 * is also called from other parts of libcrypto, which all pass around a
812 * OSSL_LIB_CTX pointer)
814 const OSSL_CORE_HANDLE *FIPS_get_core_handle(OSSL_LIB_CTX *libctx)
816 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
817 OSSL_LIB_CTX_FIPS_PROV_INDEX);
825 void *CRYPTO_malloc(size_t num, const char *file, int line)
827 return c_CRYPTO_malloc(num, file, line);
830 void *CRYPTO_zalloc(size_t num, const char *file, int line)
832 return c_CRYPTO_zalloc(num, file, line);
835 void CRYPTO_free(void *ptr, const char *file, int line)
837 c_CRYPTO_free(ptr, file, line);
840 void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
842 c_CRYPTO_clear_free(ptr, num, file, line);
845 void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
847 return c_CRYPTO_realloc(addr, num, file, line);
850 void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
851 const char *file, int line)
853 return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
856 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
858 return c_CRYPTO_secure_malloc(num, file, line);
861 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
863 return c_CRYPTO_secure_zalloc(num, file, line);
866 void CRYPTO_secure_free(void *ptr, const char *file, int line)
868 c_CRYPTO_secure_free(ptr, file, line);
871 void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
873 c_CRYPTO_secure_clear_free(ptr, num, file, line);
876 int CRYPTO_secure_allocated(const void *ptr)
878 return c_CRYPTO_secure_allocated(ptr);
881 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
886 va_start(args, format);
887 ret = c_BIO_vsnprintf(buf, n, format, args);
892 int FIPS_security_check_enabled(OSSL_LIB_CTX *libctx)
894 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
895 OSSL_LIB_CTX_FIPS_PROV_INDEX);
897 return fgbl->fips_security_checks;
900 void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
903 assert(libctx != NULL);
905 if (c_stcbfn != NULL && c_get_libctx != NULL) {
906 /* Get the parent libctx */
907 c_stcbfn(c_get_libctx(FIPS_get_core_handle(libctx)), cb, cbarg);