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/obj_mac.h> /* NIDs used by ossl_prov_util_nid_to_name() */
14 #include <openssl/fips_names.h>
15 #include <openssl/rand.h> /* RAND_get0_public() */
16 #include "internal/cryptlib.h"
17 #include "prov/implementations.h"
18 #include "prov/provider_ctx.h"
19 #include "prov/providercommon.h"
20 #include "prov/providercommonerr.h"
21 #include "prov/provider_util.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;
42 * TODO(3.0): Should these be stored in the provider side provctx? Could they
43 * ever be different from one init to the next? Unfortunately we can't do this
44 * at the moment because c_put_error/c_add_error_vdata do not provide
45 * us with the OPENSSL_CTX as a parameter.
48 static SELF_TEST_POST_PARAMS selftest_params;
50 /* Functions provided by the core */
51 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params;
52 static OSSL_FUNC_core_get_params_fn *c_get_params;
53 OSSL_FUNC_core_thread_start_fn *c_thread_start;
54 static OSSL_FUNC_core_new_error_fn *c_new_error;
55 static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
56 static OSSL_FUNC_core_vset_error_fn *c_vset_error;
57 static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
58 static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
59 static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
60 static OSSL_FUNC_CRYPTO_malloc_fn *c_CRYPTO_malloc;
61 static OSSL_FUNC_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
62 static OSSL_FUNC_CRYPTO_free_fn *c_CRYPTO_free;
63 static OSSL_FUNC_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
64 static OSSL_FUNC_CRYPTO_realloc_fn *c_CRYPTO_realloc;
65 static OSSL_FUNC_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
66 static OSSL_FUNC_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
67 static OSSL_FUNC_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
68 static OSSL_FUNC_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
69 static OSSL_FUNC_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
70 static OSSL_FUNC_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
71 static OSSL_FUNC_BIO_vsnprintf_fn *c_BIO_vsnprintf;
73 typedef struct fips_global_st {
74 const OSSL_CORE_HANDLE *handle;
77 static void *fips_prov_ossl_ctx_new(OPENSSL_CTX *libctx)
79 FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
84 static void fips_prov_ossl_ctx_free(void *fgbl)
89 static const OPENSSL_CTX_METHOD fips_prov_ossl_ctx_method = {
90 fips_prov_ossl_ctx_new,
91 fips_prov_ossl_ctx_free,
95 /* Parameters we provide to the core */
96 static const OSSL_PARAM fips_param_types[] = {
97 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
98 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
99 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
104 * Parameters to retrieve from the core provider - required for self testing.
105 * NOTE: inside core_get_params() these will be loaded from config items
106 * stored inside prov->parameters (except for
107 * OSSL_PROV_PARAM_CORE_MODULE_FILENAME).
109 static OSSL_PARAM core_params[] =
111 OSSL_PARAM_utf8_ptr(OSSL_PROV_PARAM_CORE_MODULE_FILENAME,
112 selftest_params.module_filename,
113 sizeof(selftest_params.module_filename)),
114 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_MODULE_MAC,
115 selftest_params.module_checksum_data,
116 sizeof(selftest_params.module_checksum_data)),
117 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_MAC,
118 selftest_params.indicator_checksum_data,
119 sizeof(selftest_params.indicator_checksum_data)),
120 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
121 selftest_params.indicator_data,
122 sizeof(selftest_params.indicator_data)),
123 OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
124 selftest_params.indicator_version,
125 sizeof(selftest_params.indicator_version)),
129 static const OSSL_PARAM *fips_gettable_params(void *provctx)
131 return fips_param_types;
134 static int fips_get_params(void *provctx, OSSL_PARAM params[])
138 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
139 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
141 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
142 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
144 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
145 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
151 /* FIPS specific version of the function of the same name in provlib.c */
152 /* TODO(3.0) - Is this function needed ? */
153 const char *ossl_prov_util_nid_to_name(int nid)
155 /* We don't have OBJ_nid2n() in FIPS_MODULE so we have an explicit list */
170 return "SHA-512/224";
172 return "SHA-512/256";
183 case NID_aes_256_ecb:
184 return "AES-256-ECB";
185 case NID_aes_192_ecb:
186 return "AES-192-ECB";
187 case NID_aes_128_ecb:
188 return "AES-128-ECB";
189 case NID_aes_256_cbc:
190 return "AES-256-CBC";
191 case NID_aes_192_cbc:
192 return "AES-192-CBC";
193 case NID_aes_128_cbc:
194 return "AES-128-CBC";
195 case NID_aes_256_ctr:
196 return "AES-256-CTR";
197 case NID_aes_192_ctr:
198 return "AES-192-CTR";
199 case NID_aes_128_ctr:
200 return "AES-128-CTR";
201 case NID_aes_256_xts:
202 return "AES-256-XTS";
203 case NID_aes_128_xts:
204 return "AES-128-XTS";
205 case NID_aes_256_gcm:
206 return "AES-256-GCM";
207 case NID_aes_192_gcm:
208 return "AES-192-GCM";
209 case NID_aes_128_gcm:
210 return "AES-128-GCM";
211 case NID_aes_256_ccm:
212 return "AES-256-CCM";
213 case NID_aes_192_ccm:
214 return "AES-192-CCM";
215 case NID_aes_128_ccm:
216 return "AES-128-CCM";
217 case NID_id_aes256_wrap:
218 return "AES-256-WRAP";
219 case NID_id_aes192_wrap:
220 return "AES-192-WRAP";
221 case NID_id_aes128_wrap:
222 return "AES-128-WRAP";
223 case NID_id_aes256_wrap_pad:
224 return "AES-256-WRAP-PAD";
225 case NID_id_aes192_wrap_pad:
226 return "AES-192-WRAP-PAD";
227 case NID_id_aes128_wrap_pad:
228 return "AES-128-WRAP-PAD";
229 case NID_des_ede3_ecb:
231 case NID_des_ede3_cbc:
232 return "DES-EDE3-CBC";
233 case NID_aes_256_cbc_hmac_sha256:
234 return "AES-256-CBC-HMAC-SHA256";
235 case NID_aes_128_cbc_hmac_sha256:
236 return "AES-128-CBC-HMAC-SHA256";
237 case NID_aes_256_cbc_hmac_sha1:
238 return "AES-256-CBC-HMAC-SHA1";
239 case NID_aes_128_cbc_hmac_sha1:
240 return "AES-128-CBC-HMAC-SHA1";
249 * For the algorithm names, we use the following formula for our primary
252 * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
254 * VERSION is only present if there are multiple versions of
255 * an alg (MD2, MD4, MD5). It may be omitted if there is only
256 * one version (if a subsequent version is released in the future,
257 * we can always change the canonical name, and add the old name
260 * SUBNAME may be present where we are combining multiple
261 * algorithms together, e.g. MD5-SHA1.
263 * SIZE is only present if multiple versions of an algorithm exist
264 * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
266 * MODE is only present where applicable.
268 * We add diverse other names where applicable, such as the names that
269 * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
270 * we have used historically.
272 static const OSSL_ALGORITHM fips_digests[] = {
273 /* Our primary name:NiST name[:our older names] */
274 { "SHA1:SHA-1", FIPS_DEFAULT_PROPERTIES, sha1_functions },
275 { "SHA2-224:SHA-224:SHA224", FIPS_DEFAULT_PROPERTIES, sha224_functions },
276 { "SHA2-256:SHA-256:SHA256", FIPS_DEFAULT_PROPERTIES, sha256_functions },
277 { "SHA2-384:SHA-384:SHA384", FIPS_DEFAULT_PROPERTIES, sha384_functions },
278 { "SHA2-512:SHA-512:SHA512", FIPS_DEFAULT_PROPERTIES, sha512_functions },
279 { "SHA2-512/224:SHA-512/224:SHA512-224", FIPS_DEFAULT_PROPERTIES,
280 sha512_224_functions },
281 { "SHA2-512/256:SHA-512/256:SHA512-256", FIPS_DEFAULT_PROPERTIES,
282 sha512_256_functions },
284 /* We agree with NIST here, so one name only */
285 { "SHA3-224", FIPS_DEFAULT_PROPERTIES, sha3_224_functions },
286 { "SHA3-256", FIPS_DEFAULT_PROPERTIES, sha3_256_functions },
287 { "SHA3-384", FIPS_DEFAULT_PROPERTIES, sha3_384_functions },
288 { "SHA3-512", FIPS_DEFAULT_PROPERTIES, sha3_512_functions },
290 { "SHAKE-128:SHAKE128", FIPS_DEFAULT_PROPERTIES, shake_128_functions },
291 { "SHAKE-256:SHAKE256", FIPS_DEFAULT_PROPERTIES, shake_256_functions },
294 * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
295 * KMAC128 and KMAC256.
297 { "KECCAK-KMAC-128:KECCAK-KMAC128", FIPS_DEFAULT_PROPERTIES,
298 keccak_kmac_128_functions },
299 { "KECCAK-KMAC-256:KECCAK-KMAC256", FIPS_DEFAULT_PROPERTIES,
300 keccak_kmac_256_functions },
304 static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
305 /* Our primary name[:ASN.1 OID name][:our older names] */
306 ALG("AES-256-ECB", aes256ecb_functions),
307 ALG("AES-192-ECB", aes192ecb_functions),
308 ALG("AES-128-ECB", aes128ecb_functions),
309 ALG("AES-256-CBC", aes256cbc_functions),
310 ALG("AES-192-CBC", aes192cbc_functions),
311 ALG("AES-128-CBC", aes128cbc_functions),
312 ALG("AES-256-CBC-CTS", aes256cbc_cts_functions),
313 ALG("AES-192-CBC-CTS", aes192cbc_cts_functions),
314 ALG("AES-128-CBC-CTS", aes128cbc_cts_functions),
315 ALG("AES-256-OFB", aes256ofb_functions),
316 ALG("AES-192-OFB", aes192ofb_functions),
317 ALG("AES-128-OFB", aes128ofb_functions),
318 ALG("AES-256-CFB", aes256cfb_functions),
319 ALG("AES-192-CFB", aes192cfb_functions),
320 ALG("AES-128-CFB", aes128cfb_functions),
321 ALG("AES-256-CFB1", aes256cfb1_functions),
322 ALG("AES-192-CFB1", aes192cfb1_functions),
323 ALG("AES-128-CFB1", aes128cfb1_functions),
324 ALG("AES-256-CFB8", aes256cfb8_functions),
325 ALG("AES-192-CFB8", aes192cfb8_functions),
326 ALG("AES-128-CFB8", aes128cfb8_functions),
327 ALG("AES-256-CTR", aes256ctr_functions),
328 ALG("AES-192-CTR", aes192ctr_functions),
329 ALG("AES-128-CTR", aes128ctr_functions),
330 ALG("AES-256-XTS", aes256xts_functions),
331 ALG("AES-128-XTS", aes128xts_functions),
332 ALG("AES-256-GCM:id-aes256-GCM", aes256gcm_functions),
333 ALG("AES-192-GCM:id-aes192-GCM", aes192gcm_functions),
334 ALG("AES-128-GCM:id-aes128-GCM", aes128gcm_functions),
335 ALG("AES-256-CCM:id-aes256-CCM", aes256ccm_functions),
336 ALG("AES-192-CCM:id-aes192-CCM", aes192ccm_functions),
337 ALG("AES-128-CCM:id-aes128-CCM", aes128ccm_functions),
338 ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", aes256wrap_functions),
339 ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", aes192wrap_functions),
340 ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", aes128wrap_functions),
341 ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
342 aes256wrappad_functions),
343 ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
344 aes192wrappad_functions),
345 ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
346 aes128wrappad_functions),
347 ALGC("AES-128-CBC-HMAC-SHA1", aes128cbc_hmac_sha1_functions,
348 cipher_capable_aes_cbc_hmac_sha1),
349 ALGC("AES-256-CBC-HMAC-SHA1", aes256cbc_hmac_sha1_functions,
350 cipher_capable_aes_cbc_hmac_sha1),
351 ALGC("AES-128-CBC-HMAC-SHA256", aes128cbc_hmac_sha256_functions,
352 cipher_capable_aes_cbc_hmac_sha256),
353 ALGC("AES-256-CBC-HMAC-SHA256", aes256cbc_hmac_sha256_functions,
354 cipher_capable_aes_cbc_hmac_sha256),
355 #ifndef OPENSSL_NO_DES
356 ALG("DES-EDE3-ECB:DES-EDE3", tdes_ede3_ecb_functions),
357 ALG("DES-EDE3-CBC:DES3", tdes_ede3_cbc_functions),
358 #endif /* OPENSSL_NO_DES */
359 { { NULL, NULL, NULL }, NULL }
361 static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
363 static const OSSL_ALGORITHM fips_macs[] = {
364 #ifndef OPENSSL_NO_CMAC
365 { "CMAC", FIPS_DEFAULT_PROPERTIES, cmac_functions },
367 { "GMAC", FIPS_DEFAULT_PROPERTIES, gmac_functions },
368 { "HMAC", FIPS_DEFAULT_PROPERTIES, hmac_functions },
369 { "KMAC-128:KMAC128", FIPS_DEFAULT_PROPERTIES, kmac128_functions },
370 { "KMAC-256:KMAC256", FIPS_DEFAULT_PROPERTIES, kmac256_functions },
374 static const OSSL_ALGORITHM fips_kdfs[] = {
375 { "HKDF", FIPS_DEFAULT_PROPERTIES, kdf_hkdf_functions },
376 { "SSKDF", FIPS_DEFAULT_PROPERTIES, kdf_sskdf_functions },
377 { "PBKDF2", FIPS_DEFAULT_PROPERTIES, kdf_pbkdf2_functions },
378 { "SSHKDF", FIPS_DEFAULT_PROPERTIES, kdf_sshkdf_functions },
379 { "X963KDF", FIPS_DEFAULT_PROPERTIES, kdf_x963_kdf_functions },
380 { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, kdf_tls1_prf_functions },
381 { "KBKDF", FIPS_DEFAULT_PROPERTIES, kdf_kbkdf_functions },
385 static const OSSL_ALGORITHM fips_rands[] = {
386 { "CTR-DRBG", FIPS_DEFAULT_PROPERTIES, drbg_ctr_functions },
387 { "HASH-DRBG", FIPS_DEFAULT_PROPERTIES, drbg_hash_functions },
388 { "HMAC-DRBG", FIPS_DEFAULT_PROPERTIES, drbg_hmac_functions },
389 { "TEST-RAND", FIPS_UNAPPROVED_PROPERTIES, test_rng_functions },
393 static const OSSL_ALGORITHM fips_keyexch[] = {
394 #ifndef OPENSSL_NO_DH
395 { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, dh_keyexch_functions },
397 #ifndef OPENSSL_NO_EC
398 { "ECDH", FIPS_DEFAULT_PROPERTIES, ecdh_keyexch_functions },
399 { "X25519", FIPS_DEFAULT_PROPERTIES, x25519_keyexch_functions },
400 { "X448", FIPS_DEFAULT_PROPERTIES, x448_keyexch_functions },
405 static const OSSL_ALGORITHM fips_signature[] = {
406 #ifndef OPENSSL_NO_DSA
407 { "DSA:dsaEncryption", FIPS_DEFAULT_PROPERTIES, dsa_signature_functions },
409 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES, rsa_signature_functions },
410 #ifndef OPENSSL_NO_EC
411 { "ED25519", FIPS_DEFAULT_PROPERTIES, ed25519_signature_functions },
412 { "ED448", FIPS_DEFAULT_PROPERTIES, ed448_signature_functions },
413 { "ECDSA", FIPS_DEFAULT_PROPERTIES, ecdsa_signature_functions },
418 static const OSSL_ALGORITHM fips_asym_cipher[] = {
419 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES, rsa_asym_cipher_functions },
423 static const OSSL_ALGORITHM fips_keymgmt[] = {
424 #ifndef OPENSSL_NO_DH
425 { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, dh_keymgmt_functions },
427 #ifndef OPENSSL_NO_DSA
428 { "DSA", FIPS_DEFAULT_PROPERTIES, dsa_keymgmt_functions },
430 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES, rsa_keymgmt_functions },
431 { "RSA-PSS:RSASSA-PSS", FIPS_DEFAULT_PROPERTIES,
432 rsapss_keymgmt_functions },
433 #ifndef OPENSSL_NO_EC
434 { "EC:id-ecPublicKey", FIPS_DEFAULT_PROPERTIES, ec_keymgmt_functions },
435 { "X25519", FIPS_DEFAULT_PROPERTIES, x25519_keymgmt_functions },
436 { "X448", FIPS_DEFAULT_PROPERTIES, x448_keymgmt_functions },
437 { "ED25519", FIPS_DEFAULT_PROPERTIES, ed25519_keymgmt_functions },
438 { "ED448", FIPS_DEFAULT_PROPERTIES, ed448_keymgmt_functions },
443 static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
447 switch (operation_id) {
451 ossl_prov_cache_exported_algorithms(fips_ciphers, exported_fips_ciphers);
452 return exported_fips_ciphers;
459 case OSSL_OP_KEYMGMT:
461 case OSSL_OP_KEYEXCH:
463 case OSSL_OP_SIGNATURE:
464 return fips_signature;
465 case OSSL_OP_ASYM_CIPHER:
466 return fips_asym_cipher;
471 static void fips_teardown(void *provctx)
473 OPENSSL_CTX_free(PROV_LIBRARY_CONTEXT_OF(provctx));
474 PROV_CTX_free(provctx);
477 static void fips_intern_teardown(void *provctx)
480 * We know that the library context is the same as for the outer provider,
481 * so no need to destroy it here.
483 PROV_CTX_free(provctx);
486 /* Functions we provide to the core */
487 static const OSSL_DISPATCH fips_dispatch_table[] = {
488 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
489 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
490 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
491 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
492 { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))provider_get_capabilities },
496 /* Functions we provide to ourself */
497 static const OSSL_DISPATCH intern_dispatch_table[] = {
498 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
499 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
504 int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
505 const OSSL_DISPATCH *in,
506 const OSSL_DISPATCH **out,
510 OPENSSL_CTX *libctx = NULL;
511 OSSL_FUNC_self_test_cb_fn *stcbfn = NULL;
512 OSSL_FUNC_core_get_library_context_fn *c_get_libctx = NULL;
514 for (; in->function_id != 0; in++) {
515 switch (in->function_id) {
516 case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
517 c_get_libctx = OSSL_FUNC_core_get_library_context(in);
519 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
520 c_gettable_params = OSSL_FUNC_core_gettable_params(in);
522 case OSSL_FUNC_CORE_GET_PARAMS:
523 c_get_params = OSSL_FUNC_core_get_params(in);
525 case OSSL_FUNC_CORE_THREAD_START:
526 c_thread_start = OSSL_FUNC_core_thread_start(in);
528 case OSSL_FUNC_CORE_NEW_ERROR:
529 c_new_error = OSSL_FUNC_core_new_error(in);
531 case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
532 c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
534 case OSSL_FUNC_CORE_VSET_ERROR:
535 c_vset_error = OSSL_FUNC_core_vset_error(in);
537 case OSSL_FUNC_CORE_SET_ERROR_MARK:
538 c_set_error_mark = OSSL_FUNC_core_set_error_mark(in);
540 case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
541 c_clear_last_error_mark = OSSL_FUNC_core_clear_last_error_mark(in);
543 case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
544 c_pop_error_to_mark = OSSL_FUNC_core_pop_error_to_mark(in);
546 case OSSL_FUNC_CRYPTO_MALLOC:
547 c_CRYPTO_malloc = OSSL_FUNC_CRYPTO_malloc(in);
549 case OSSL_FUNC_CRYPTO_ZALLOC:
550 c_CRYPTO_zalloc = OSSL_FUNC_CRYPTO_zalloc(in);
552 case OSSL_FUNC_CRYPTO_FREE:
553 c_CRYPTO_free = OSSL_FUNC_CRYPTO_free(in);
555 case OSSL_FUNC_CRYPTO_CLEAR_FREE:
556 c_CRYPTO_clear_free = OSSL_FUNC_CRYPTO_clear_free(in);
558 case OSSL_FUNC_CRYPTO_REALLOC:
559 c_CRYPTO_realloc = OSSL_FUNC_CRYPTO_realloc(in);
561 case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
562 c_CRYPTO_clear_realloc = OSSL_FUNC_CRYPTO_clear_realloc(in);
564 case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
565 c_CRYPTO_secure_malloc = OSSL_FUNC_CRYPTO_secure_malloc(in);
567 case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
568 c_CRYPTO_secure_zalloc = OSSL_FUNC_CRYPTO_secure_zalloc(in);
570 case OSSL_FUNC_CRYPTO_SECURE_FREE:
571 c_CRYPTO_secure_free = OSSL_FUNC_CRYPTO_secure_free(in);
573 case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
574 c_CRYPTO_secure_clear_free = OSSL_FUNC_CRYPTO_secure_clear_free(in);
576 case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
577 c_CRYPTO_secure_allocated = OSSL_FUNC_CRYPTO_secure_allocated(in);
579 case OSSL_FUNC_BIO_NEW_FILE:
580 selftest_params.bio_new_file_cb = OSSL_FUNC_BIO_new_file(in);
582 case OSSL_FUNC_BIO_NEW_MEMBUF:
583 selftest_params.bio_new_buffer_cb = OSSL_FUNC_BIO_new_membuf(in);
585 case OSSL_FUNC_BIO_READ_EX:
586 selftest_params.bio_read_ex_cb = OSSL_FUNC_BIO_read_ex(in);
588 case OSSL_FUNC_BIO_FREE:
589 selftest_params.bio_free_cb = OSSL_FUNC_BIO_free(in);
591 case OSSL_FUNC_BIO_VSNPRINTF:
592 c_BIO_vsnprintf = OSSL_FUNC_BIO_vsnprintf(in);
594 case OSSL_FUNC_SELF_TEST_CB: {
595 stcbfn = OSSL_FUNC_self_test_cb(in);
599 /* Just ignore anything we don't understand */
604 if (stcbfn != NULL && c_get_libctx != NULL) {
605 stcbfn(c_get_libctx(handle), &selftest_params.cb,
606 &selftest_params.cb_arg);
609 selftest_params.cb = NULL;
610 selftest_params.cb_arg = NULL;
613 if (!c_get_params(handle, core_params)) {
614 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
618 /* Create a context. */
619 if ((*provctx = PROV_CTX_new()) == NULL
620 || (libctx = OPENSSL_CTX_new()) == NULL) {
622 * We free libctx separately here and only here because it hasn't
623 * been attached to *provctx. All other error paths below rely
624 * solely on fips_teardown.
626 OPENSSL_CTX_free(libctx);
629 PROV_CTX_set0_library_context(*provctx, libctx);
630 PROV_CTX_set0_handle(*provctx, handle);
632 if ((fgbl = openssl_ctx_get_data(libctx, OPENSSL_CTX_FIPS_PROV_INDEX,
633 &fips_prov_ossl_ctx_method)) == NULL)
636 fgbl->handle = handle;
638 selftest_params.libctx = libctx;
639 if (!SELF_TEST_post(&selftest_params, 0)) {
640 ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_POST_FAILURE);
644 /* TODO(3.0): Tests will hang if this is removed */
645 (void)RAND_get0_public(libctx);
647 *out = fips_dispatch_table;
650 fips_teardown(*provctx);
656 * The internal init function used when the FIPS module uses EVP to call
657 * another algorithm also in the FIPS module. This is a recursive call that has
658 * been made from within the FIPS module itself. To make this work, we populate
659 * the provider context of this inner instance with the same library context
660 * that was used in the EVP call that initiated this recursive call.
662 OSSL_provider_init_fn fips_intern_provider_init;
663 int fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
664 const OSSL_DISPATCH *in,
665 const OSSL_DISPATCH **out,
668 OSSL_FUNC_core_get_library_context_fn *c_get_libctx = NULL;
670 for (; in->function_id != 0; in++) {
671 switch (in->function_id) {
672 case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
673 c_get_libctx = OSSL_FUNC_core_get_library_context(in);
680 if (c_get_libctx == NULL)
683 if ((*provctx = PROV_CTX_new()) == NULL)
686 * Using the parent library context only works because we are a built-in
687 * internal provider. This is not something that most providers would be
690 PROV_CTX_set0_library_context(*provctx, (OPENSSL_CTX *)c_get_libctx(handle));
691 PROV_CTX_set0_handle(*provctx, handle);
693 *out = intern_dispatch_table;
702 void ERR_set_debug(const char *file, int line, const char *func)
704 c_set_error_debug(NULL, file, line, func);
707 void ERR_set_error(int lib, int reason, const char *fmt, ...)
712 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
716 void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
718 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
721 int ERR_set_mark(void)
723 return c_set_error_mark(NULL);
726 int ERR_clear_last_mark(void)
728 return c_clear_last_error_mark(NULL);
731 int ERR_pop_to_mark(void)
733 return c_pop_error_to_mark(NULL);
737 * This must take a library context, since it's called from the depths
738 * of crypto/initthread.c code, where it's (correctly) assumed that the
739 * passed caller argument is an OPENSSL_CTX pointer (since the same routine
740 * is also called from other parts of libcrypto, which all pass around a
741 * OPENSSL_CTX pointer)
743 const OSSL_CORE_HANDLE *FIPS_get_core_handle(OPENSSL_CTX *libctx)
745 FIPS_GLOBAL *fgbl = openssl_ctx_get_data(libctx,
746 OPENSSL_CTX_FIPS_PROV_INDEX,
747 &fips_prov_ossl_ctx_method);
755 void *CRYPTO_malloc(size_t num, const char *file, int line)
757 return c_CRYPTO_malloc(num, file, line);
760 void *CRYPTO_zalloc(size_t num, const char *file, int line)
762 return c_CRYPTO_zalloc(num, file, line);
765 void CRYPTO_free(void *ptr, const char *file, int line)
767 c_CRYPTO_free(ptr, file, line);
770 void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
772 c_CRYPTO_clear_free(ptr, num, file, line);
775 void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
777 return c_CRYPTO_realloc(addr, num, file, line);
780 void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
781 const char *file, int line)
783 return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
786 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
788 return c_CRYPTO_secure_malloc(num, file, line);
791 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
793 return c_CRYPTO_secure_zalloc(num, file, line);
796 void CRYPTO_secure_free(void *ptr, const char *file, int line)
798 c_CRYPTO_secure_free(ptr, file, line);
801 void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
803 c_CRYPTO_secure_clear_free(ptr, num, file, line);
806 int CRYPTO_secure_allocated(const void *ptr)
808 return c_CRYPTO_secure_allocated(ptr);
811 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
816 va_start(args, format);
817 ret = c_BIO_vsnprintf(buf, n, format, args);