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_UNAPPROVED_PROPERTIES,
397 ossl_ed25519_signature_functions },
398 { PROV_NAMES_ED448, FIPS_UNAPPROVED_PROPERTIES, ossl_ed448_signature_functions },
399 { PROV_NAMES_ECDSA, FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_signature_functions },
401 { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES,
402 ossl_mac_legacy_hmac_signature_functions },
403 #ifndef OPENSSL_NO_CMAC
404 { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES,
405 ossl_mac_legacy_cmac_signature_functions },
410 static const OSSL_ALGORITHM fips_asym_cipher[] = {
411 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_cipher_functions },
415 static const OSSL_ALGORITHM fips_asym_kem[] = {
416 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_kem_functions },
420 static const OSSL_ALGORITHM fips_keymgmt[] = {
421 #ifndef OPENSSL_NO_DH
422 { PROV_NAMES_DH, FIPS_DEFAULT_PROPERTIES, ossl_dh_keymgmt_functions,
424 { PROV_NAMES_DHX, FIPS_DEFAULT_PROPERTIES, ossl_dhx_keymgmt_functions,
427 #ifndef OPENSSL_NO_DSA
428 { PROV_NAMES_DSA, FIPS_DEFAULT_PROPERTIES, ossl_dsa_keymgmt_functions,
431 { PROV_NAMES_RSA, FIPS_DEFAULT_PROPERTIES, ossl_rsa_keymgmt_functions,
433 { PROV_NAMES_RSA_PSS, FIPS_DEFAULT_PROPERTIES,
434 ossl_rsapss_keymgmt_functions, PROV_DESCS_RSA_PSS },
435 #ifndef OPENSSL_NO_EC
436 { PROV_NAMES_EC, FIPS_DEFAULT_PROPERTIES, ossl_ec_keymgmt_functions,
438 { PROV_NAMES_X25519, FIPS_DEFAULT_PROPERTIES, ossl_x25519_keymgmt_functions,
440 { PROV_NAMES_X448, FIPS_DEFAULT_PROPERTIES, ossl_x448_keymgmt_functions,
442 { PROV_NAMES_ED25519, FIPS_UNAPPROVED_PROPERTIES, ossl_ed25519_keymgmt_functions,
443 PROV_DESCS_ED25519 },
444 { PROV_NAMES_ED448, FIPS_UNAPPROVED_PROPERTIES, ossl_ed448_keymgmt_functions,
447 { PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
448 PROV_DESCS_TLS1_PRF_SIGN },
449 { PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
450 PROV_DESCS_HKDF_SIGN },
451 { PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES, ossl_mac_legacy_keymgmt_functions,
452 PROV_DESCS_HMAC_SIGN },
453 #ifndef OPENSSL_NO_CMAC
454 { PROV_NAMES_CMAC, FIPS_DEFAULT_PROPERTIES,
455 ossl_cmac_legacy_keymgmt_functions, PROV_DESCS_CMAC_SIGN },
460 static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
465 if (!ossl_prov_is_running())
468 switch (operation_id) {
472 return exported_fips_ciphers;
479 case OSSL_OP_KEYMGMT:
481 case OSSL_OP_KEYEXCH:
483 case OSSL_OP_SIGNATURE:
484 return fips_signature;
485 case OSSL_OP_ASYM_CIPHER:
486 return fips_asym_cipher;
488 return fips_asym_kem;
493 static void fips_teardown(void *provctx)
495 OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
496 ossl_prov_ctx_free(provctx);
499 static void fips_intern_teardown(void *provctx)
502 * We know that the library context is the same as for the outer provider,
503 * so no need to destroy it here.
505 ossl_prov_ctx_free(provctx);
508 /* Functions we provide to the core */
509 static const OSSL_DISPATCH fips_dispatch_table[] = {
510 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
511 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
512 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
513 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
514 { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
515 (void (*)(void))ossl_prov_get_capabilities },
516 { OSSL_FUNC_PROVIDER_SELF_TEST, (void (*)(void))fips_self_test },
520 /* Functions we provide to ourself */
521 static const OSSL_DISPATCH intern_dispatch_table[] = {
522 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
523 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
528 * On VMS, the provider init function name is expected to be uppercase,
529 * see the pragmas in <openssl/core.h>. Let's do the same with this
530 * internal name. This is how symbol names are treated by default
531 * by the compiler if nothing else is said, but since this is part
532 * of libfips, and we build our libraries with mixed case symbol names,
533 * we must switch back to this default explicitly here.
537 # pragma names uppercase,truncated
539 OSSL_provider_init_fn OSSL_provider_init_int;
541 # pragma names restore
543 int OSSL_provider_init_int(const OSSL_CORE_HANDLE *handle,
544 const OSSL_DISPATCH *in,
545 const OSSL_DISPATCH **out,
549 OSSL_LIB_CTX *libctx = NULL;
550 SELF_TEST_POST_PARAMS selftest_params;
552 memset(&selftest_params, 0, sizeof(selftest_params));
554 if (!ossl_prov_seeding_from_dispatch(in))
556 for (; in->function_id != 0; in++) {
558 * We do not support the scenario of an application linked against
559 * multiple versions of libcrypto (e.g. one static and one dynamic), but
560 * sharing a single fips.so. We do a simple sanity check here.
562 #define set_func(c, f) if (c == NULL) c = f; else if (c != f) return 0;
563 switch (in->function_id) {
564 case OSSL_FUNC_CORE_GET_LIBCTX:
565 set_func(c_get_libctx, OSSL_FUNC_core_get_libctx(in));
567 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
568 set_func(c_gettable_params, OSSL_FUNC_core_gettable_params(in));
570 case OSSL_FUNC_CORE_GET_PARAMS:
571 set_func(c_get_params, OSSL_FUNC_core_get_params(in));
573 case OSSL_FUNC_CORE_THREAD_START:
574 set_func(c_thread_start, OSSL_FUNC_core_thread_start(in));
576 case OSSL_FUNC_CORE_NEW_ERROR:
577 set_func(c_new_error, OSSL_FUNC_core_new_error(in));
579 case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
580 set_func(c_set_error_debug, OSSL_FUNC_core_set_error_debug(in));
582 case OSSL_FUNC_CORE_VSET_ERROR:
583 set_func(c_vset_error, OSSL_FUNC_core_vset_error(in));
585 case OSSL_FUNC_CORE_SET_ERROR_MARK:
586 set_func(c_set_error_mark, OSSL_FUNC_core_set_error_mark(in));
588 case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
589 set_func(c_clear_last_error_mark,
590 OSSL_FUNC_core_clear_last_error_mark(in));
592 case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
593 set_func(c_pop_error_to_mark, OSSL_FUNC_core_pop_error_to_mark(in));
595 case OSSL_FUNC_CRYPTO_MALLOC:
596 set_func(c_CRYPTO_malloc, OSSL_FUNC_CRYPTO_malloc(in));
598 case OSSL_FUNC_CRYPTO_ZALLOC:
599 set_func(c_CRYPTO_zalloc, OSSL_FUNC_CRYPTO_zalloc(in));
601 case OSSL_FUNC_CRYPTO_FREE:
602 set_func(c_CRYPTO_free, OSSL_FUNC_CRYPTO_free(in));
604 case OSSL_FUNC_CRYPTO_CLEAR_FREE:
605 set_func(c_CRYPTO_clear_free, OSSL_FUNC_CRYPTO_clear_free(in));
607 case OSSL_FUNC_CRYPTO_REALLOC:
608 set_func(c_CRYPTO_realloc, OSSL_FUNC_CRYPTO_realloc(in));
610 case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
611 set_func(c_CRYPTO_clear_realloc,
612 OSSL_FUNC_CRYPTO_clear_realloc(in));
614 case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
615 set_func(c_CRYPTO_secure_malloc,
616 OSSL_FUNC_CRYPTO_secure_malloc(in));
618 case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
619 set_func(c_CRYPTO_secure_zalloc,
620 OSSL_FUNC_CRYPTO_secure_zalloc(in));
622 case OSSL_FUNC_CRYPTO_SECURE_FREE:
623 set_func(c_CRYPTO_secure_free,
624 OSSL_FUNC_CRYPTO_secure_free(in));
626 case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
627 set_func(c_CRYPTO_secure_clear_free,
628 OSSL_FUNC_CRYPTO_secure_clear_free(in));
630 case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
631 set_func(c_CRYPTO_secure_allocated,
632 OSSL_FUNC_CRYPTO_secure_allocated(in));
634 case OSSL_FUNC_BIO_NEW_FILE:
635 set_func(selftest_params.bio_new_file_cb,
636 OSSL_FUNC_BIO_new_file(in));
638 case OSSL_FUNC_BIO_NEW_MEMBUF:
639 set_func(selftest_params.bio_new_buffer_cb,
640 OSSL_FUNC_BIO_new_membuf(in));
642 case OSSL_FUNC_BIO_READ_EX:
643 set_func(selftest_params.bio_read_ex_cb,
644 OSSL_FUNC_BIO_read_ex(in));
646 case OSSL_FUNC_BIO_FREE:
647 set_func(selftest_params.bio_free_cb, OSSL_FUNC_BIO_free(in));
649 case OSSL_FUNC_BIO_VSNPRINTF:
650 set_func(c_BIO_vsnprintf, OSSL_FUNC_BIO_vsnprintf(in));
652 case OSSL_FUNC_SELF_TEST_CB:
653 set_func(c_stcbfn, OSSL_FUNC_self_test_cb(in));
656 /* Just ignore anything we don't understand */
661 /* Create a context. */
662 if ((*provctx = ossl_prov_ctx_new()) == NULL
663 || (libctx = OSSL_LIB_CTX_new()) == NULL) {
665 * We free libctx separately here and only here because it hasn't
666 * been attached to *provctx. All other error paths below rely
667 * solely on fips_teardown.
669 OSSL_LIB_CTX_free(libctx);
673 if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX)) == NULL)
676 fgbl->handle = handle;
679 * We did initial set up of selftest_params in a local copy, because we
680 * could not create fgbl until c_CRYPTO_zalloc was defined in the loop
683 fgbl->selftest_params = selftest_params;
685 fgbl->selftest_params.libctx = libctx;
687 set_self_test_cb(fgbl);
689 if (!fips_get_params_from_core(fgbl)) {
690 /* Error already raised */
694 * Disable the conditional error check if it's disabled in the fips config
697 if (fgbl->selftest_params.conditional_error_check != NULL
698 && strcmp(fgbl->selftest_params.conditional_error_check, "0") == 0)
699 SELF_TEST_disable_conditional_error_state();
701 /* Disable the security check if it's disabled in the fips config file. */
702 if (fgbl->fips_security_check_option != NULL
703 && strcmp(fgbl->fips_security_check_option, "0") == 0)
704 fgbl->fips_security_checks = 0;
706 ossl_prov_cache_exported_algorithms(fips_ciphers, exported_fips_ciphers);
708 if (!SELF_TEST_post(&fgbl->selftest_params, 0)) {
709 ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_POST_FAILURE);
713 ossl_prov_ctx_set0_libctx(*provctx, libctx);
714 ossl_prov_ctx_set0_handle(*provctx, handle);
716 *out = fips_dispatch_table;
719 fips_teardown(*provctx);
720 OSSL_LIB_CTX_free(libctx);
726 * The internal init function used when the FIPS module uses EVP to call
727 * another algorithm also in the FIPS module. This is a recursive call that has
728 * been made from within the FIPS module itself. To make this work, we populate
729 * the provider context of this inner instance with the same library context
730 * that was used in the EVP call that initiated this recursive call.
732 OSSL_provider_init_fn ossl_fips_intern_provider_init;
733 int ossl_fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
734 const OSSL_DISPATCH *in,
735 const OSSL_DISPATCH **out,
738 OSSL_FUNC_core_get_libctx_fn *c_internal_get_libctx = NULL;
740 for (; in->function_id != 0; in++) {
741 switch (in->function_id) {
742 case OSSL_FUNC_CORE_GET_LIBCTX:
743 c_internal_get_libctx = OSSL_FUNC_core_get_libctx(in);
750 if (c_internal_get_libctx == NULL)
753 if ((*provctx = ossl_prov_ctx_new()) == NULL)
757 * Using the parent library context only works because we are a built-in
758 * internal provider. This is not something that most providers would be
761 ossl_prov_ctx_set0_libctx(*provctx,
762 (OSSL_LIB_CTX *)c_internal_get_libctx(handle));
763 ossl_prov_ctx_set0_handle(*provctx, handle);
765 *out = intern_dispatch_table;
774 void ERR_set_debug(const char *file, int line, const char *func)
776 c_set_error_debug(NULL, file, line, func);
779 void ERR_set_error(int lib, int reason, const char *fmt, ...)
784 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
788 void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
790 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
793 int ERR_set_mark(void)
795 return c_set_error_mark(NULL);
798 int ERR_clear_last_mark(void)
800 return c_clear_last_error_mark(NULL);
803 int ERR_pop_to_mark(void)
805 return c_pop_error_to_mark(NULL);
809 * This must take a library context, since it's called from the depths
810 * of crypto/initthread.c code, where it's (correctly) assumed that the
811 * passed caller argument is an OSSL_LIB_CTX pointer (since the same routine
812 * is also called from other parts of libcrypto, which all pass around a
813 * OSSL_LIB_CTX pointer)
815 const OSSL_CORE_HANDLE *FIPS_get_core_handle(OSSL_LIB_CTX *libctx)
817 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
818 OSSL_LIB_CTX_FIPS_PROV_INDEX);
826 void *CRYPTO_malloc(size_t num, const char *file, int line)
828 return c_CRYPTO_malloc(num, file, line);
831 void *CRYPTO_zalloc(size_t num, const char *file, int line)
833 return c_CRYPTO_zalloc(num, file, line);
836 void CRYPTO_free(void *ptr, const char *file, int line)
838 c_CRYPTO_free(ptr, file, line);
841 void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
843 c_CRYPTO_clear_free(ptr, num, file, line);
846 void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
848 return c_CRYPTO_realloc(addr, num, file, line);
851 void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
852 const char *file, int line)
854 return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
857 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
859 return c_CRYPTO_secure_malloc(num, file, line);
862 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
864 return c_CRYPTO_secure_zalloc(num, file, line);
867 void CRYPTO_secure_free(void *ptr, const char *file, int line)
869 c_CRYPTO_secure_free(ptr, file, line);
872 void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
874 c_CRYPTO_secure_clear_free(ptr, num, file, line);
877 int CRYPTO_secure_allocated(const void *ptr)
879 return c_CRYPTO_secure_allocated(ptr);
882 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
887 va_start(args, format);
888 ret = c_BIO_vsnprintf(buf, n, format, args);
893 int FIPS_security_check_enabled(OSSL_LIB_CTX *libctx)
895 FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
896 OSSL_LIB_CTX_FIPS_PROV_INDEX);
898 return fgbl->fips_security_checks;
901 void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
904 assert(libctx != NULL);
906 if (c_stcbfn != NULL && c_get_libctx != NULL) {
907 /* Get the parent libctx */
908 c_stcbfn(c_get_libctx(FIPS_get_core_handle(libctx)), cb, cbarg);