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
;
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 OPENSSL_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_library_context_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(OPENSSL_CTX
*libctx
)
84 FIPS_GLOBAL
*fgbl
= OPENSSL_zalloc(sizeof(*fgbl
));
89 static void fips_prov_ossl_ctx_free(void *fgbl
)
94 static const OPENSSL_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;
187 /* FIPS specific version of the function of the same name in provlib.c */
188 /* TODO(3.0) - Is this function needed ? */
189 const char *ossl_prov_util_nid_to_name(int nid
)
191 /* We don't have OBJ_nid2n() in FIPS_MODULE so we have an explicit list */
206 return "SHA-512/224";
208 return "SHA-512/256";
219 case NID_aes_256_ecb
:
220 return "AES-256-ECB";
221 case NID_aes_192_ecb
:
222 return "AES-192-ECB";
223 case NID_aes_128_ecb
:
224 return "AES-128-ECB";
225 case NID_aes_256_cbc
:
226 return "AES-256-CBC";
227 case NID_aes_192_cbc
:
228 return "AES-192-CBC";
229 case NID_aes_128_cbc
:
230 return "AES-128-CBC";
231 case NID_aes_256_ctr
:
232 return "AES-256-CTR";
233 case NID_aes_192_ctr
:
234 return "AES-192-CTR";
235 case NID_aes_128_ctr
:
236 return "AES-128-CTR";
237 case NID_aes_256_xts
:
238 return "AES-256-XTS";
239 case NID_aes_128_xts
:
240 return "AES-128-XTS";
241 case NID_aes_256_gcm
:
242 return "AES-256-GCM";
243 case NID_aes_192_gcm
:
244 return "AES-192-GCM";
245 case NID_aes_128_gcm
:
246 return "AES-128-GCM";
247 case NID_aes_256_ccm
:
248 return "AES-256-CCM";
249 case NID_aes_192_ccm
:
250 return "AES-192-CCM";
251 case NID_aes_128_ccm
:
252 return "AES-128-CCM";
253 case NID_id_aes256_wrap
:
254 return "AES-256-WRAP";
255 case NID_id_aes192_wrap
:
256 return "AES-192-WRAP";
257 case NID_id_aes128_wrap
:
258 return "AES-128-WRAP";
259 case NID_id_aes256_wrap_pad
:
260 return "AES-256-WRAP-PAD";
261 case NID_id_aes192_wrap_pad
:
262 return "AES-192-WRAP-PAD";
263 case NID_id_aes128_wrap_pad
:
264 return "AES-128-WRAP-PAD";
265 case NID_des_ede3_ecb
:
267 case NID_des_ede3_cbc
:
268 return "DES-EDE3-CBC";
269 case NID_aes_256_cbc_hmac_sha256
:
270 return "AES-256-CBC-HMAC-SHA256";
271 case NID_aes_128_cbc_hmac_sha256
:
272 return "AES-128-CBC-HMAC-SHA256";
273 case NID_aes_256_cbc_hmac_sha1
:
274 return "AES-256-CBC-HMAC-SHA1";
275 case NID_aes_128_cbc_hmac_sha1
:
276 return "AES-128-CBC-HMAC-SHA1";
285 * For the algorithm names, we use the following formula for our primary
288 * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
290 * VERSION is only present if there are multiple versions of
291 * an alg (MD2, MD4, MD5). It may be omitted if there is only
292 * one version (if a subsequent version is released in the future,
293 * we can always change the canonical name, and add the old name
296 * SUBNAME may be present where we are combining multiple
297 * algorithms together, e.g. MD5-SHA1.
299 * SIZE is only present if multiple versions of an algorithm exist
300 * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
302 * MODE is only present where applicable.
304 * We add diverse other names where applicable, such as the names that
305 * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
306 * we have used historically.
308 static const OSSL_ALGORITHM fips_digests
[] = {
309 /* Our primary name:NiST name[:our older names] */
310 { "SHA1:SHA-1:SSL3-SHA1", FIPS_DEFAULT_PROPERTIES
, sha1_functions
},
311 { "SHA2-224:SHA-224:SHA224", FIPS_DEFAULT_PROPERTIES
, sha224_functions
},
312 { "SHA2-256:SHA-256:SHA256", FIPS_DEFAULT_PROPERTIES
, sha256_functions
},
313 { "SHA2-384:SHA-384:SHA384", FIPS_DEFAULT_PROPERTIES
, sha384_functions
},
314 { "SHA2-512:SHA-512:SHA512", FIPS_DEFAULT_PROPERTIES
, sha512_functions
},
315 { "SHA2-512/224:SHA-512/224:SHA512-224", FIPS_DEFAULT_PROPERTIES
,
316 sha512_224_functions
},
317 { "SHA2-512/256:SHA-512/256:SHA512-256", FIPS_DEFAULT_PROPERTIES
,
318 sha512_256_functions
},
320 /* We agree with NIST here, so one name only */
321 { "SHA3-224", FIPS_DEFAULT_PROPERTIES
, sha3_224_functions
},
322 { "SHA3-256", FIPS_DEFAULT_PROPERTIES
, sha3_256_functions
},
323 { "SHA3-384", FIPS_DEFAULT_PROPERTIES
, sha3_384_functions
},
324 { "SHA3-512", FIPS_DEFAULT_PROPERTIES
, sha3_512_functions
},
326 { "SHAKE-128:SHAKE128", FIPS_DEFAULT_PROPERTIES
, shake_128_functions
},
327 { "SHAKE-256:SHAKE256", FIPS_DEFAULT_PROPERTIES
, shake_256_functions
},
330 * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
331 * KMAC128 and KMAC256.
333 { "KECCAK-KMAC-128:KECCAK-KMAC128", FIPS_DEFAULT_PROPERTIES
,
334 keccak_kmac_128_functions
},
335 { "KECCAK-KMAC-256:KECCAK-KMAC256", FIPS_DEFAULT_PROPERTIES
,
336 keccak_kmac_256_functions
},
340 static const OSSL_ALGORITHM_CAPABLE fips_ciphers
[] = {
341 /* Our primary name[:ASN.1 OID name][:our older names] */
342 ALG("AES-256-ECB", aes256ecb_functions
),
343 ALG("AES-192-ECB", aes192ecb_functions
),
344 ALG("AES-128-ECB", aes128ecb_functions
),
345 ALG("AES-256-CBC:AES256", aes256cbc_functions
),
346 ALG("AES-192-CBC:AES192", aes192cbc_functions
),
347 ALG("AES-128-CBC:AES128", aes128cbc_functions
),
348 ALG("AES-256-CBC-CTS", aes256cbc_cts_functions
),
349 ALG("AES-192-CBC-CTS", aes192cbc_cts_functions
),
350 ALG("AES-128-CBC-CTS", aes128cbc_cts_functions
),
351 ALG("AES-256-OFB", aes256ofb_functions
),
352 ALG("AES-192-OFB", aes192ofb_functions
),
353 ALG("AES-128-OFB", aes128ofb_functions
),
354 ALG("AES-256-CFB", aes256cfb_functions
),
355 ALG("AES-192-CFB", aes192cfb_functions
),
356 ALG("AES-128-CFB", aes128cfb_functions
),
357 ALG("AES-256-CFB1", aes256cfb1_functions
),
358 ALG("AES-192-CFB1", aes192cfb1_functions
),
359 ALG("AES-128-CFB1", aes128cfb1_functions
),
360 ALG("AES-256-CFB8", aes256cfb8_functions
),
361 ALG("AES-192-CFB8", aes192cfb8_functions
),
362 ALG("AES-128-CFB8", aes128cfb8_functions
),
363 ALG("AES-256-CTR", aes256ctr_functions
),
364 ALG("AES-192-CTR", aes192ctr_functions
),
365 ALG("AES-128-CTR", aes128ctr_functions
),
366 ALG("AES-256-XTS", aes256xts_functions
),
367 ALG("AES-128-XTS", aes128xts_functions
),
368 ALG("AES-256-GCM:id-aes256-GCM", aes256gcm_functions
),
369 ALG("AES-192-GCM:id-aes192-GCM", aes192gcm_functions
),
370 ALG("AES-128-GCM:id-aes128-GCM", aes128gcm_functions
),
371 ALG("AES-256-CCM:id-aes256-CCM", aes256ccm_functions
),
372 ALG("AES-192-CCM:id-aes192-CCM", aes192ccm_functions
),
373 ALG("AES-128-CCM:id-aes128-CCM", aes128ccm_functions
),
374 ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", aes256wrap_functions
),
375 ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", aes192wrap_functions
),
376 ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", aes128wrap_functions
),
377 ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
378 aes256wrappad_functions
),
379 ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
380 aes192wrappad_functions
),
381 ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
382 aes128wrappad_functions
),
383 ALGC("AES-128-CBC-HMAC-SHA1", aes128cbc_hmac_sha1_functions
,
384 cipher_capable_aes_cbc_hmac_sha1
),
385 ALGC("AES-256-CBC-HMAC-SHA1", aes256cbc_hmac_sha1_functions
,
386 cipher_capable_aes_cbc_hmac_sha1
),
387 ALGC("AES-128-CBC-HMAC-SHA256", aes128cbc_hmac_sha256_functions
,
388 cipher_capable_aes_cbc_hmac_sha256
),
389 ALGC("AES-256-CBC-HMAC-SHA256", aes256cbc_hmac_sha256_functions
,
390 cipher_capable_aes_cbc_hmac_sha256
),
391 #ifndef OPENSSL_NO_DES
392 ALG("DES-EDE3-ECB:DES-EDE3", tdes_ede3_ecb_functions
),
393 ALG("DES-EDE3-CBC:DES3", tdes_ede3_cbc_functions
),
394 #endif /* OPENSSL_NO_DES */
395 { { NULL
, NULL
, NULL
}, NULL
}
397 static OSSL_ALGORITHM exported_fips_ciphers
[OSSL_NELEM(fips_ciphers
)];
399 static const OSSL_ALGORITHM fips_macs
[] = {
400 #ifndef OPENSSL_NO_CMAC
401 { "CMAC", FIPS_DEFAULT_PROPERTIES
, cmac_functions
},
403 { "GMAC", FIPS_DEFAULT_PROPERTIES
, gmac_functions
},
404 { "HMAC", FIPS_DEFAULT_PROPERTIES
, hmac_functions
},
405 { "KMAC-128:KMAC128", FIPS_DEFAULT_PROPERTIES
, kmac128_functions
},
406 { "KMAC-256:KMAC256", FIPS_DEFAULT_PROPERTIES
, kmac256_functions
},
410 static const OSSL_ALGORITHM fips_kdfs
[] = {
411 { "HKDF", FIPS_DEFAULT_PROPERTIES
, kdf_hkdf_functions
},
412 { "SSKDF", FIPS_DEFAULT_PROPERTIES
, kdf_sskdf_functions
},
413 { "PBKDF2", FIPS_DEFAULT_PROPERTIES
, kdf_pbkdf2_functions
},
414 { "SSHKDF", FIPS_DEFAULT_PROPERTIES
, kdf_sshkdf_functions
},
415 { "X963KDF", FIPS_DEFAULT_PROPERTIES
, kdf_x963_kdf_functions
},
416 { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES
, kdf_tls1_prf_functions
},
417 { "KBKDF", FIPS_DEFAULT_PROPERTIES
, kdf_kbkdf_functions
},
421 static const OSSL_ALGORITHM fips_rands
[] = {
422 { "CTR-DRBG", FIPS_DEFAULT_PROPERTIES
, drbg_ctr_functions
},
423 { "HASH-DRBG", FIPS_DEFAULT_PROPERTIES
, drbg_hash_functions
},
424 { "HMAC-DRBG", FIPS_DEFAULT_PROPERTIES
, drbg_hmac_functions
},
425 { "TEST-RAND", FIPS_UNAPPROVED_PROPERTIES
, test_rng_functions
},
429 static const OSSL_ALGORITHM fips_keyexch
[] = {
430 #ifndef OPENSSL_NO_DH
431 { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES
, dh_keyexch_functions
},
433 #ifndef OPENSSL_NO_EC
434 { "ECDH", FIPS_DEFAULT_PROPERTIES
, ecdh_keyexch_functions
},
435 { "X25519", FIPS_DEFAULT_PROPERTIES
, x25519_keyexch_functions
},
436 { "X448", FIPS_DEFAULT_PROPERTIES
, x448_keyexch_functions
},
438 { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES
, kdf_tls1_prf_keyexch_functions
},
439 { "HKDF", FIPS_DEFAULT_PROPERTIES
, kdf_hkdf_keyexch_functions
},
443 static const OSSL_ALGORITHM fips_signature
[] = {
444 #ifndef OPENSSL_NO_DSA
445 { "DSA:dsaEncryption", FIPS_DEFAULT_PROPERTIES
, dsa_signature_functions
},
447 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES
, rsa_signature_functions
},
448 #ifndef OPENSSL_NO_EC
449 { "ED25519", FIPS_DEFAULT_PROPERTIES
, ed25519_signature_functions
},
450 { "ED448", FIPS_DEFAULT_PROPERTIES
, ed448_signature_functions
},
451 { "ECDSA", FIPS_DEFAULT_PROPERTIES
, ecdsa_signature_functions
},
453 { "HMAC", FIPS_DEFAULT_PROPERTIES
, mac_legacy_hmac_signature_functions
},
454 #ifndef OPENSSL_NO_CMAC
455 { "CMAC", FIPS_DEFAULT_PROPERTIES
, mac_legacy_cmac_signature_functions
},
460 static const OSSL_ALGORITHM fips_asym_cipher
[] = {
461 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES
, rsa_asym_cipher_functions
},
465 static const OSSL_ALGORITHM fips_keymgmt
[] = {
466 #ifndef OPENSSL_NO_DH
467 { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES
, dh_keymgmt_functions
},
468 { "DHX:X9.42 DH:dhpublicnumber", FIPS_DEFAULT_PROPERTIES
, dhx_keymgmt_functions
},
470 #ifndef OPENSSL_NO_DSA
471 { "DSA", FIPS_DEFAULT_PROPERTIES
, dsa_keymgmt_functions
},
473 { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES
, rsa_keymgmt_functions
},
474 { "RSA-PSS:RSASSA-PSS", FIPS_DEFAULT_PROPERTIES
,
475 rsapss_keymgmt_functions
},
476 #ifndef OPENSSL_NO_EC
477 { "EC:id-ecPublicKey", FIPS_DEFAULT_PROPERTIES
, ec_keymgmt_functions
},
478 { "X25519", FIPS_DEFAULT_PROPERTIES
, x25519_keymgmt_functions
},
479 { "X448", FIPS_DEFAULT_PROPERTIES
, x448_keymgmt_functions
},
480 { "ED25519", FIPS_DEFAULT_PROPERTIES
, ed25519_keymgmt_functions
},
481 { "ED448", FIPS_DEFAULT_PROPERTIES
, ed448_keymgmt_functions
},
483 { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES
, kdf_keymgmt_functions
},
484 { "HKDF", FIPS_DEFAULT_PROPERTIES
, kdf_keymgmt_functions
},
485 { "HMAC", FIPS_DEFAULT_PROPERTIES
, mac_legacy_keymgmt_functions
},
486 #ifndef OPENSSL_NO_CMAC
487 { "CMAC", FIPS_DEFAULT_PROPERTIES
, cmac_legacy_keymgmt_functions
},
492 static const OSSL_ALGORITHM
*fips_query(void *provctx
, int operation_id
,
497 if (!ossl_prov_is_running())
500 switch (operation_id
) {
504 ossl_prov_cache_exported_algorithms(fips_ciphers
, exported_fips_ciphers
);
505 return exported_fips_ciphers
;
512 case OSSL_OP_KEYMGMT
:
514 case OSSL_OP_KEYEXCH
:
516 case OSSL_OP_SIGNATURE
:
517 return fips_signature
;
518 case OSSL_OP_ASYM_CIPHER
:
519 return fips_asym_cipher
;
524 static void fips_teardown(void *provctx
)
526 OPENSSL_CTX_free(PROV_LIBRARY_CONTEXT_OF(provctx
));
527 PROV_CTX_free(provctx
);
530 static void fips_intern_teardown(void *provctx
)
533 * We know that the library context is the same as for the outer provider,
534 * so no need to destroy it here.
536 PROV_CTX_free(provctx
);
539 /* Functions we provide to the core */
540 static const OSSL_DISPATCH fips_dispatch_table
[] = {
541 { OSSL_FUNC_PROVIDER_TEARDOWN
, (void (*)(void))fips_teardown
},
542 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS
, (void (*)(void))fips_gettable_params
},
543 { OSSL_FUNC_PROVIDER_GET_PARAMS
, (void (*)(void))fips_get_params
},
544 { OSSL_FUNC_PROVIDER_QUERY_OPERATION
, (void (*)(void))fips_query
},
545 { OSSL_FUNC_PROVIDER_GET_CAPABILITIES
,
546 (void (*)(void))provider_get_capabilities
},
547 { OSSL_FUNC_PROVIDER_SELF_TEST
, (void (*)(void))fips_self_test
},
551 /* Functions we provide to ourself */
552 static const OSSL_DISPATCH intern_dispatch_table
[] = {
553 { OSSL_FUNC_PROVIDER_TEARDOWN
, (void (*)(void))fips_intern_teardown
},
554 { OSSL_FUNC_PROVIDER_QUERY_OPERATION
, (void (*)(void))fips_query
},
558 int OSSL_provider_init(const OSSL_CORE_HANDLE
*handle
,
559 const OSSL_DISPATCH
*in
,
560 const OSSL_DISPATCH
**out
,
564 OPENSSL_CTX
*libctx
= NULL
;
566 for (; in
->function_id
!= 0; in
++) {
567 switch (in
->function_id
) {
568 case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT
:
569 c_get_libctx
= OSSL_FUNC_core_get_library_context(in
);
571 case OSSL_FUNC_CORE_GETTABLE_PARAMS
:
572 c_gettable_params
= OSSL_FUNC_core_gettable_params(in
);
574 case OSSL_FUNC_CORE_GET_PARAMS
:
575 c_get_params
= OSSL_FUNC_core_get_params(in
);
577 case OSSL_FUNC_CORE_THREAD_START
:
578 c_thread_start
= OSSL_FUNC_core_thread_start(in
);
580 case OSSL_FUNC_CORE_NEW_ERROR
:
581 c_new_error
= OSSL_FUNC_core_new_error(in
);
583 case OSSL_FUNC_CORE_SET_ERROR_DEBUG
:
584 c_set_error_debug
= OSSL_FUNC_core_set_error_debug(in
);
586 case OSSL_FUNC_CORE_VSET_ERROR
:
587 c_vset_error
= OSSL_FUNC_core_vset_error(in
);
589 case OSSL_FUNC_CORE_SET_ERROR_MARK
:
590 c_set_error_mark
= OSSL_FUNC_core_set_error_mark(in
);
592 case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK
:
593 c_clear_last_error_mark
= OSSL_FUNC_core_clear_last_error_mark(in
);
595 case OSSL_FUNC_CORE_POP_ERROR_TO_MARK
:
596 c_pop_error_to_mark
= OSSL_FUNC_core_pop_error_to_mark(in
);
598 case OSSL_FUNC_CRYPTO_MALLOC
:
599 c_CRYPTO_malloc
= OSSL_FUNC_CRYPTO_malloc(in
);
601 case OSSL_FUNC_CRYPTO_ZALLOC
:
602 c_CRYPTO_zalloc
= OSSL_FUNC_CRYPTO_zalloc(in
);
604 case OSSL_FUNC_CRYPTO_FREE
:
605 c_CRYPTO_free
= OSSL_FUNC_CRYPTO_free(in
);
607 case OSSL_FUNC_CRYPTO_CLEAR_FREE
:
608 c_CRYPTO_clear_free
= OSSL_FUNC_CRYPTO_clear_free(in
);
610 case OSSL_FUNC_CRYPTO_REALLOC
:
611 c_CRYPTO_realloc
= OSSL_FUNC_CRYPTO_realloc(in
);
613 case OSSL_FUNC_CRYPTO_CLEAR_REALLOC
:
614 c_CRYPTO_clear_realloc
= OSSL_FUNC_CRYPTO_clear_realloc(in
);
616 case OSSL_FUNC_CRYPTO_SECURE_MALLOC
:
617 c_CRYPTO_secure_malloc
= OSSL_FUNC_CRYPTO_secure_malloc(in
);
619 case OSSL_FUNC_CRYPTO_SECURE_ZALLOC
:
620 c_CRYPTO_secure_zalloc
= OSSL_FUNC_CRYPTO_secure_zalloc(in
);
622 case OSSL_FUNC_CRYPTO_SECURE_FREE
:
623 c_CRYPTO_secure_free
= OSSL_FUNC_CRYPTO_secure_free(in
);
625 case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE
:
626 c_CRYPTO_secure_clear_free
= OSSL_FUNC_CRYPTO_secure_clear_free(in
);
628 case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED
:
629 c_CRYPTO_secure_allocated
= OSSL_FUNC_CRYPTO_secure_allocated(in
);
631 case OSSL_FUNC_BIO_NEW_FILE
:
632 selftest_params
.bio_new_file_cb
= OSSL_FUNC_BIO_new_file(in
);
634 case OSSL_FUNC_BIO_NEW_MEMBUF
:
635 selftest_params
.bio_new_buffer_cb
= OSSL_FUNC_BIO_new_membuf(in
);
637 case OSSL_FUNC_BIO_READ_EX
:
638 selftest_params
.bio_read_ex_cb
= OSSL_FUNC_BIO_read_ex(in
);
640 case OSSL_FUNC_BIO_FREE
:
641 selftest_params
.bio_free_cb
= OSSL_FUNC_BIO_free(in
);
643 case OSSL_FUNC_BIO_VSNPRINTF
:
644 c_BIO_vsnprintf
= OSSL_FUNC_BIO_vsnprintf(in
);
646 case OSSL_FUNC_SELF_TEST_CB
: {
647 c_stcbfn
= OSSL_FUNC_self_test_cb(in
);
651 /* Just ignore anything we don't understand */
656 set_self_test_cb(handle
);
658 if (!c_get_params(handle
, core_params
)) {
659 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_TO_GET_PARAMETER
);
662 /* Disable the conditional error check if is disabled in the fips config file*/
663 if (selftest_params
.conditional_error_check
!= NULL
664 && strcmp(selftest_params
.conditional_error_check
, "0") == 0)
665 SELF_TEST_disable_conditional_error_state();
667 /* Disable the security check if is disabled in the fips config file*/
668 if (fips_security_check_option
!= NULL
669 && strcmp(fips_security_check_option
, "0") == 0)
670 fips_security_checks
= 0;
672 /* Create a context. */
673 if ((*provctx
= PROV_CTX_new()) == NULL
674 || (libctx
= OPENSSL_CTX_new()) == NULL
) {
676 * We free libctx separately here and only here because it hasn't
677 * been attached to *provctx. All other error paths below rely
678 * solely on fips_teardown.
680 OPENSSL_CTX_free(libctx
);
683 PROV_CTX_set0_library_context(*provctx
, libctx
);
684 PROV_CTX_set0_handle(*provctx
, handle
);
686 if ((fgbl
= openssl_ctx_get_data(libctx
, OPENSSL_CTX_FIPS_PROV_INDEX
,
687 &fips_prov_ossl_ctx_method
)) == NULL
)
690 fgbl
->handle
= handle
;
692 selftest_params
.libctx
= libctx
;
693 if (!SELF_TEST_post(&selftest_params
, 0)) {
694 ERR_raise(ERR_LIB_PROV
, PROV_R_SELF_TEST_POST_FAILURE
);
698 /* TODO(3.0): Tests will hang if this is removed */
699 (void)RAND_get0_public(libctx
);
701 *out
= fips_dispatch_table
;
704 fips_teardown(*provctx
);
710 * The internal init function used when the FIPS module uses EVP to call
711 * another algorithm also in the FIPS module. This is a recursive call that has
712 * been made from within the FIPS module itself. To make this work, we populate
713 * the provider context of this inner instance with the same library context
714 * that was used in the EVP call that initiated this recursive call.
716 OSSL_provider_init_fn fips_intern_provider_init
;
717 int fips_intern_provider_init(const OSSL_CORE_HANDLE
*handle
,
718 const OSSL_DISPATCH
*in
,
719 const OSSL_DISPATCH
**out
,
722 OSSL_FUNC_core_get_library_context_fn
*c_internal_get_libctx
= NULL
;
724 for (; in
->function_id
!= 0; in
++) {
725 switch (in
->function_id
) {
726 case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT
:
727 c_internal_get_libctx
= OSSL_FUNC_core_get_library_context(in
);
734 if (c_internal_get_libctx
== NULL
)
737 if ((*provctx
= PROV_CTX_new()) == NULL
)
741 * Using the parent library context only works because we are a built-in
742 * internal provider. This is not something that most providers would be
745 PROV_CTX_set0_library_context(*provctx
,
746 (OPENSSL_CTX
*)c_internal_get_libctx(handle
));
747 PROV_CTX_set0_handle(*provctx
, handle
);
749 *out
= intern_dispatch_table
;
758 void ERR_set_debug(const char *file
, int line
, const char *func
)
760 c_set_error_debug(NULL
, file
, line
, func
);
763 void ERR_set_error(int lib
, int reason
, const char *fmt
, ...)
768 c_vset_error(NULL
, ERR_PACK(lib
, 0, reason
), fmt
, args
);
772 void ERR_vset_error(int lib
, int reason
, const char *fmt
, va_list args
)
774 c_vset_error(NULL
, ERR_PACK(lib
, 0, reason
), fmt
, args
);
777 int ERR_set_mark(void)
779 return c_set_error_mark(NULL
);
782 int ERR_clear_last_mark(void)
784 return c_clear_last_error_mark(NULL
);
787 int ERR_pop_to_mark(void)
789 return c_pop_error_to_mark(NULL
);
793 * This must take a library context, since it's called from the depths
794 * of crypto/initthread.c code, where it's (correctly) assumed that the
795 * passed caller argument is an OPENSSL_CTX pointer (since the same routine
796 * is also called from other parts of libcrypto, which all pass around a
797 * OPENSSL_CTX pointer)
799 const OSSL_CORE_HANDLE
*FIPS_get_core_handle(OPENSSL_CTX
*libctx
)
801 FIPS_GLOBAL
*fgbl
= openssl_ctx_get_data(libctx
,
802 OPENSSL_CTX_FIPS_PROV_INDEX
,
803 &fips_prov_ossl_ctx_method
);
811 void *CRYPTO_malloc(size_t num
, const char *file
, int line
)
813 return c_CRYPTO_malloc(num
, file
, line
);
816 void *CRYPTO_zalloc(size_t num
, const char *file
, int line
)
818 return c_CRYPTO_zalloc(num
, file
, line
);
821 void CRYPTO_free(void *ptr
, const char *file
, int line
)
823 c_CRYPTO_free(ptr
, file
, line
);
826 void CRYPTO_clear_free(void *ptr
, size_t num
, const char *file
, int line
)
828 c_CRYPTO_clear_free(ptr
, num
, file
, line
);
831 void *CRYPTO_realloc(void *addr
, size_t num
, const char *file
, int line
)
833 return c_CRYPTO_realloc(addr
, num
, file
, line
);
836 void *CRYPTO_clear_realloc(void *addr
, size_t old_num
, size_t num
,
837 const char *file
, int line
)
839 return c_CRYPTO_clear_realloc(addr
, old_num
, num
, file
, line
);
842 void *CRYPTO_secure_malloc(size_t num
, const char *file
, int line
)
844 return c_CRYPTO_secure_malloc(num
, file
, line
);
847 void *CRYPTO_secure_zalloc(size_t num
, const char *file
, int line
)
849 return c_CRYPTO_secure_zalloc(num
, file
, line
);
852 void CRYPTO_secure_free(void *ptr
, const char *file
, int line
)
854 c_CRYPTO_secure_free(ptr
, file
, line
);
857 void CRYPTO_secure_clear_free(void *ptr
, size_t num
, const char *file
, int line
)
859 c_CRYPTO_secure_clear_free(ptr
, num
, file
, line
);
862 int CRYPTO_secure_allocated(const void *ptr
)
864 return c_CRYPTO_secure_allocated(ptr
);
867 int BIO_snprintf(char *buf
, size_t n
, const char *format
, ...)
872 va_start(args
, format
);
873 ret
= c_BIO_vsnprintf(buf
, n
, format
, args
);
878 int FIPS_security_check_enabled(void)
880 return fips_security_checks
;