fipsprov.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <openssl/core_dispatch.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/params.h>
  12. #include <openssl/fips_names.h>
  13. #include <openssl/rand.h> /* RAND_get0_public() */
  14. #include <openssl/proverr.h>
  15. #include "internal/cryptlib.h"
  16. #include "prov/implementations.h"
  17. #include "prov/provider_ctx.h"
  18. #include "prov/providercommon.h"
  19. #include "prov/provider_util.h"
  20. #include "prov/seeding.h"
  21. #include "self_test.h"
  22. static const char FIPS_DEFAULT_PROPERTIES[] = "provider=fips,fips=yes";
  23. static const char FIPS_UNAPPROVED_PROPERTIES[] = "provider=fips,fips=no";
  24. /*
  25. * Forward declarations to ensure that interface functions are correctly
  26. * defined.
  27. */
  28. static OSSL_FUNC_provider_teardown_fn fips_teardown;
  29. static OSSL_FUNC_provider_gettable_params_fn fips_gettable_params;
  30. static OSSL_FUNC_provider_get_params_fn fips_get_params;
  31. static OSSL_FUNC_provider_query_operation_fn fips_query;
  32. #define ALGC(NAMES, FUNC, CHECK) { { NAMES, FIPS_DEFAULT_PROPERTIES, FUNC }, CHECK }
  33. #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
  34. extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
  35. int FIPS_security_check_enabled(void);
  36. /*
  37. * TODO(3.0): Should these be stored in the provider side provctx? Could they
  38. * ever be different from one init to the next? Unfortunately we can't do this
  39. * at the moment because c_put_error/c_add_error_vdata do not provide
  40. * us with the OSSL_LIB_CTX as a parameter.
  41. */
  42. static SELF_TEST_POST_PARAMS selftest_params;
  43. static int fips_security_checks = 1;
  44. static const char *fips_security_check_option = "1";
  45. /* Functions provided by the core */
  46. static OSSL_FUNC_core_gettable_params_fn *c_gettable_params;
  47. static OSSL_FUNC_core_get_params_fn *c_get_params;
  48. OSSL_FUNC_core_thread_start_fn *c_thread_start;
  49. static OSSL_FUNC_core_new_error_fn *c_new_error;
  50. static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
  51. static OSSL_FUNC_core_vset_error_fn *c_vset_error;
  52. static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
  53. static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
  54. static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
  55. static OSSL_FUNC_CRYPTO_malloc_fn *c_CRYPTO_malloc;
  56. static OSSL_FUNC_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
  57. static OSSL_FUNC_CRYPTO_free_fn *c_CRYPTO_free;
  58. static OSSL_FUNC_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
  59. static OSSL_FUNC_CRYPTO_realloc_fn *c_CRYPTO_realloc;
  60. static OSSL_FUNC_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
  61. static OSSL_FUNC_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
  62. static OSSL_FUNC_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
  63. static OSSL_FUNC_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
  64. static OSSL_FUNC_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
  65. static OSSL_FUNC_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
  66. static OSSL_FUNC_BIO_vsnprintf_fn *c_BIO_vsnprintf;
  67. static OSSL_FUNC_self_test_cb_fn *c_stcbfn = NULL;
  68. static OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
  69. typedef struct fips_global_st {
  70. const OSSL_CORE_HANDLE *handle;
  71. } FIPS_GLOBAL;
  72. static void *fips_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
  73. {
  74. FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
  75. return fgbl;
  76. }
  77. static void fips_prov_ossl_ctx_free(void *fgbl)
  78. {
  79. OPENSSL_free(fgbl);
  80. }
  81. static const OSSL_LIB_CTX_METHOD fips_prov_ossl_ctx_method = {
  82. fips_prov_ossl_ctx_new,
  83. fips_prov_ossl_ctx_free,
  84. };
  85. /* Parameters we provide to the core */
  86. static const OSSL_PARAM fips_param_types[] = {
  87. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
  88. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  89. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
  90. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
  91. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_SECURITY_CHECKS, OSSL_PARAM_INTEGER, NULL, 0),
  92. OSSL_PARAM_END
  93. };
  94. /*
  95. * Parameters to retrieve from the core provider - required for self testing.
  96. * NOTE: inside core_get_params() these will be loaded from config items
  97. * stored inside prov->parameters (except for
  98. * OSSL_PROV_PARAM_CORE_MODULE_FILENAME).
  99. * OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS is not a self test parameter.
  100. */
  101. static OSSL_PARAM core_params[] =
  102. {
  103. OSSL_PARAM_utf8_ptr(OSSL_PROV_PARAM_CORE_MODULE_FILENAME,
  104. &selftest_params.module_filename,
  105. sizeof(selftest_params.module_filename)),
  106. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_MODULE_MAC,
  107. &selftest_params.module_checksum_data,
  108. sizeof(selftest_params.module_checksum_data)),
  109. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_MAC,
  110. &selftest_params.indicator_checksum_data,
  111. sizeof(selftest_params.indicator_checksum_data)),
  112. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
  113. &selftest_params.indicator_data,
  114. sizeof(selftest_params.indicator_data)),
  115. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
  116. &selftest_params.indicator_version,
  117. sizeof(selftest_params.indicator_version)),
  118. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
  119. &selftest_params.conditional_error_check,
  120. sizeof(selftest_params.conditional_error_check)),
  121. OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
  122. &fips_security_check_option,
  123. sizeof(fips_security_check_option)),
  124. OSSL_PARAM_END
  125. };
  126. static const OSSL_PARAM *fips_gettable_params(void *provctx)
  127. {
  128. return fips_param_types;
  129. }
  130. static int fips_get_params(void *provctx, OSSL_PARAM params[])
  131. {
  132. OSSL_PARAM *p;
  133. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  134. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
  135. return 0;
  136. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
  137. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
  138. return 0;
  139. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
  140. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
  141. return 0;
  142. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
  143. if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
  144. return 0;
  145. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_SECURITY_CHECKS);
  146. if (p != NULL && !OSSL_PARAM_set_int(p, fips_security_checks))
  147. return 0;
  148. return 1;
  149. }
  150. static void set_self_test_cb(const OSSL_CORE_HANDLE *handle)
  151. {
  152. if (c_stcbfn != NULL && c_get_libctx != NULL) {
  153. c_stcbfn(c_get_libctx(handle), &selftest_params.cb,
  154. &selftest_params.cb_arg);
  155. } else {
  156. selftest_params.cb = NULL;
  157. selftest_params.cb_arg = NULL;
  158. }
  159. }
  160. static int fips_self_test(void *provctx)
  161. {
  162. set_self_test_cb(FIPS_get_core_handle(selftest_params.libctx));
  163. return SELF_TEST_post(&selftest_params, 1) ? 1 : 0;
  164. }
  165. /*
  166. * For the algorithm names, we use the following formula for our primary
  167. * names:
  168. *
  169. * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
  170. *
  171. * VERSION is only present if there are multiple versions of
  172. * an alg (MD2, MD4, MD5). It may be omitted if there is only
  173. * one version (if a subsequent version is released in the future,
  174. * we can always change the canonical name, and add the old name
  175. * as an alias).
  176. *
  177. * SUBNAME may be present where we are combining multiple
  178. * algorithms together, e.g. MD5-SHA1.
  179. *
  180. * SIZE is only present if multiple versions of an algorithm exist
  181. * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
  182. *
  183. * MODE is only present where applicable.
  184. *
  185. * We add diverse other names where applicable, such as the names that
  186. * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
  187. * we have used historically.
  188. */
  189. static const OSSL_ALGORITHM fips_digests[] = {
  190. /* Our primary name:NiST name[:our older names] */
  191. { "SHA1:SHA-1:SSL3-SHA1", FIPS_DEFAULT_PROPERTIES, ossl_sha1_functions },
  192. { "SHA2-224:SHA-224:SHA224", FIPS_DEFAULT_PROPERTIES,
  193. ossl_sha224_functions },
  194. { "SHA2-256:SHA-256:SHA256", FIPS_DEFAULT_PROPERTIES,
  195. ossl_sha256_functions },
  196. { "SHA2-384:SHA-384:SHA384", FIPS_DEFAULT_PROPERTIES,
  197. ossl_sha384_functions },
  198. { "SHA2-512:SHA-512:SHA512", FIPS_DEFAULT_PROPERTIES,
  199. ossl_sha512_functions },
  200. { "SHA2-512/224:SHA-512/224:SHA512-224", FIPS_DEFAULT_PROPERTIES,
  201. ossl_sha512_224_functions },
  202. { "SHA2-512/256:SHA-512/256:SHA512-256", FIPS_DEFAULT_PROPERTIES,
  203. ossl_sha512_256_functions },
  204. /* We agree with NIST here, so one name only */
  205. { "SHA3-224", FIPS_DEFAULT_PROPERTIES, ossl_sha3_224_functions },
  206. { "SHA3-256", FIPS_DEFAULT_PROPERTIES, ossl_sha3_256_functions },
  207. { "SHA3-384", FIPS_DEFAULT_PROPERTIES, ossl_sha3_384_functions },
  208. { "SHA3-512", FIPS_DEFAULT_PROPERTIES, ossl_sha3_512_functions },
  209. { "SHAKE-128:SHAKE128", FIPS_DEFAULT_PROPERTIES, ossl_shake_128_functions },
  210. { "SHAKE-256:SHAKE256", FIPS_DEFAULT_PROPERTIES, ossl_shake_256_functions },
  211. /*
  212. * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
  213. * KMAC128 and KMAC256.
  214. */
  215. { "KECCAK-KMAC-128:KECCAK-KMAC128", FIPS_DEFAULT_PROPERTIES,
  216. ossl_keccak_kmac_128_functions },
  217. { "KECCAK-KMAC-256:KECCAK-KMAC256", FIPS_DEFAULT_PROPERTIES,
  218. ossl_keccak_kmac_256_functions },
  219. { NULL, NULL, NULL }
  220. };
  221. static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
  222. /* Our primary name[:ASN.1 OID name][:our older names] */
  223. ALG("AES-256-ECB", ossl_aes256ecb_functions),
  224. ALG("AES-192-ECB", ossl_aes192ecb_functions),
  225. ALG("AES-128-ECB", ossl_aes128ecb_functions),
  226. ALG("AES-256-CBC:AES256", ossl_aes256cbc_functions),
  227. ALG("AES-192-CBC:AES192", ossl_aes192cbc_functions),
  228. ALG("AES-128-CBC:AES128", ossl_aes128cbc_functions),
  229. ALG("AES-256-CBC-CTS", ossl_aes256cbc_cts_functions),
  230. ALG("AES-192-CBC-CTS", ossl_aes192cbc_cts_functions),
  231. ALG("AES-128-CBC-CTS", ossl_aes128cbc_cts_functions),
  232. ALG("AES-256-OFB", ossl_aes256ofb_functions),
  233. ALG("AES-192-OFB", ossl_aes192ofb_functions),
  234. ALG("AES-128-OFB", ossl_aes128ofb_functions),
  235. ALG("AES-256-CFB", ossl_aes256cfb_functions),
  236. ALG("AES-192-CFB", ossl_aes192cfb_functions),
  237. ALG("AES-128-CFB", ossl_aes128cfb_functions),
  238. ALG("AES-256-CFB1", ossl_aes256cfb1_functions),
  239. ALG("AES-192-CFB1", ossl_aes192cfb1_functions),
  240. ALG("AES-128-CFB1", ossl_aes128cfb1_functions),
  241. ALG("AES-256-CFB8", ossl_aes256cfb8_functions),
  242. ALG("AES-192-CFB8", ossl_aes192cfb8_functions),
  243. ALG("AES-128-CFB8", ossl_aes128cfb8_functions),
  244. ALG("AES-256-CTR", ossl_aes256ctr_functions),
  245. ALG("AES-192-CTR", ossl_aes192ctr_functions),
  246. ALG("AES-128-CTR", ossl_aes128ctr_functions),
  247. ALG("AES-256-XTS", ossl_aes256xts_functions),
  248. ALG("AES-128-XTS", ossl_aes128xts_functions),
  249. ALG("AES-256-GCM:id-aes256-GCM", ossl_aes256gcm_functions),
  250. ALG("AES-192-GCM:id-aes192-GCM", ossl_aes192gcm_functions),
  251. ALG("AES-128-GCM:id-aes128-GCM", ossl_aes128gcm_functions),
  252. ALG("AES-256-CCM:id-aes256-CCM", ossl_aes256ccm_functions),
  253. ALG("AES-192-CCM:id-aes192-CCM", ossl_aes192ccm_functions),
  254. ALG("AES-128-CCM:id-aes128-CCM", ossl_aes128ccm_functions),
  255. ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", ossl_aes256wrap_functions),
  256. ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", ossl_aes192wrap_functions),
  257. ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", ossl_aes128wrap_functions),
  258. ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
  259. ossl_aes256wrappad_functions),
  260. ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
  261. ossl_aes192wrappad_functions),
  262. ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
  263. ossl_aes128wrappad_functions),
  264. ALG("AES-256-WRAP-INV:AES256-WRAP-INV", ossl_aes256wrapinv_functions),
  265. ALG("AES-192-WRAP-INV:AES192-WRAP-INV", ossl_aes192wrapinv_functions),
  266. ALG("AES-128-WRAP-INV:AES128-WRAP-INV", ossl_aes128wrapinv_functions),
  267. ALG("AES-256-WRAP-PAD-INV:AES256-WRAP-PAD-INV",
  268. ossl_aes256wrappadinv_functions),
  269. ALG("AES-192-WRAP-PAD-INV:AES192-WRAP-PAD-INV",
  270. ossl_aes192wrappadinv_functions),
  271. ALG("AES-128-WRAP-PAD-INV:AES128-WRAP-PAD-INV",
  272. ossl_aes128wrappadinv_functions),
  273. ALGC("AES-128-CBC-HMAC-SHA1", ossl_aes128cbc_hmac_sha1_functions,
  274. ossl_cipher_capable_aes_cbc_hmac_sha1),
  275. ALGC("AES-256-CBC-HMAC-SHA1", ossl_aes256cbc_hmac_sha1_functions,
  276. ossl_cipher_capable_aes_cbc_hmac_sha1),
  277. ALGC("AES-128-CBC-HMAC-SHA256", ossl_aes128cbc_hmac_sha256_functions,
  278. ossl_cipher_capable_aes_cbc_hmac_sha256),
  279. ALGC("AES-256-CBC-HMAC-SHA256", ossl_aes256cbc_hmac_sha256_functions,
  280. ossl_cipher_capable_aes_cbc_hmac_sha256),
  281. #ifndef OPENSSL_NO_DES
  282. ALG("DES-EDE3-ECB:DES-EDE3", ossl_tdes_ede3_ecb_functions),
  283. ALG("DES-EDE3-CBC:DES3", ossl_tdes_ede3_cbc_functions),
  284. #endif /* OPENSSL_NO_DES */
  285. { { NULL, NULL, NULL }, NULL }
  286. };
  287. static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
  288. static const OSSL_ALGORITHM fips_macs[] = {
  289. #ifndef OPENSSL_NO_CMAC
  290. { "CMAC", FIPS_DEFAULT_PROPERTIES, ossl_cmac_functions },
  291. #endif
  292. { "GMAC", FIPS_DEFAULT_PROPERTIES, ossl_gmac_functions },
  293. { "HMAC", FIPS_DEFAULT_PROPERTIES, ossl_hmac_functions },
  294. { "KMAC-128:KMAC128", FIPS_DEFAULT_PROPERTIES, ossl_kmac128_functions },
  295. { "KMAC-256:KMAC256", FIPS_DEFAULT_PROPERTIES, ossl_kmac256_functions },
  296. { NULL, NULL, NULL }
  297. };
  298. static const OSSL_ALGORITHM fips_kdfs[] = {
  299. { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_functions },
  300. { "SSKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_sskdf_functions },
  301. { "PBKDF2", FIPS_DEFAULT_PROPERTIES, ossl_kdf_pbkdf2_functions },
  302. { "SSHKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_sshkdf_functions },
  303. { "X963KDF:X942KDF-CONCAT", FIPS_DEFAULT_PROPERTIES,
  304. ossl_kdf_x963_kdf_functions },
  305. { "X942KDF-ASN1:X942KDF", FIPS_DEFAULT_PROPERTIES,
  306. ossl_kdf_x942_kdf_functions },
  307. { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_tls1_prf_functions },
  308. { "KBKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_kbkdf_functions },
  309. { NULL, NULL, NULL }
  310. };
  311. static const OSSL_ALGORITHM fips_rands[] = {
  312. { "CTR-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_ctr_functions },
  313. { "HASH-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_hash_functions },
  314. { "HMAC-DRBG", FIPS_DEFAULT_PROPERTIES, ossl_drbg_ossl_hmac_functions },
  315. { "TEST-RAND", FIPS_UNAPPROVED_PROPERTIES, ossl_test_rng_functions },
  316. { NULL, NULL, NULL }
  317. };
  318. static const OSSL_ALGORITHM fips_keyexch[] = {
  319. #ifndef OPENSSL_NO_DH
  320. { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, ossl_dh_keyexch_functions },
  321. #endif
  322. #ifndef OPENSSL_NO_EC
  323. { "ECDH", FIPS_DEFAULT_PROPERTIES, ossl_ecdh_keyexch_functions },
  324. { "X25519", FIPS_DEFAULT_PROPERTIES, ossl_x25519_keyexch_functions },
  325. { "X448", FIPS_DEFAULT_PROPERTIES, ossl_x448_keyexch_functions },
  326. #endif
  327. { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES,
  328. ossl_kdf_tls1_prf_keyexch_functions },
  329. { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_keyexch_functions },
  330. { NULL, NULL, NULL }
  331. };
  332. static const OSSL_ALGORITHM fips_signature[] = {
  333. #ifndef OPENSSL_NO_DSA
  334. { "DSA:dsaEncryption", FIPS_DEFAULT_PROPERTIES,
  335. ossl_dsa_signature_functions },
  336. #endif
  337. { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
  338. ossl_rsa_signature_functions },
  339. #ifndef OPENSSL_NO_EC
  340. { "ED25519", FIPS_DEFAULT_PROPERTIES, ossl_ed25519_signature_functions },
  341. { "ED448", FIPS_DEFAULT_PROPERTIES, ossl_ed448_signature_functions },
  342. { "ECDSA", FIPS_DEFAULT_PROPERTIES, ossl_ecdsa_signature_functions },
  343. #endif
  344. { "HMAC", FIPS_DEFAULT_PROPERTIES,
  345. ossl_mac_legacy_hmac_signature_functions },
  346. #ifndef OPENSSL_NO_CMAC
  347. { "CMAC", FIPS_DEFAULT_PROPERTIES,
  348. ossl_mac_legacy_cmac_signature_functions },
  349. #endif
  350. { NULL, NULL, NULL }
  351. };
  352. static const OSSL_ALGORITHM fips_asym_cipher[] = {
  353. { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
  354. ossl_rsa_asym_cipher_functions },
  355. { NULL, NULL, NULL }
  356. };
  357. static const OSSL_ALGORITHM fips_asym_kem[] = {
  358. { "RSA", FIPS_DEFAULT_PROPERTIES, ossl_rsa_asym_kem_functions },
  359. { NULL, NULL, NULL }
  360. };
  361. static const OSSL_ALGORITHM fips_keymgmt[] = {
  362. #ifndef OPENSSL_NO_DH
  363. { "DH:dhKeyAgreement", FIPS_DEFAULT_PROPERTIES, ossl_dh_keymgmt_functions,
  364. "OpenSSL PKCS#3 DH FIPS implementation" },
  365. { "DHX:X9.42 DH:dhpublicnumber", FIPS_DEFAULT_PROPERTIES,
  366. ossl_dhx_keymgmt_functions, "OpenSSL X9.42 DH FIPS implementation" },
  367. #endif
  368. #ifndef OPENSSL_NO_DSA
  369. { "DSA", FIPS_DEFAULT_PROPERTIES, ossl_dsa_keymgmt_functions,
  370. "OpenSSL DSA FIPS implementation" },
  371. #endif
  372. { "RSA:rsaEncryption", FIPS_DEFAULT_PROPERTIES,
  373. ossl_rsa_keymgmt_functions, "OpenSSL RSA FIPS implementation" },
  374. { "RSA-PSS:RSASSA-PSS", FIPS_DEFAULT_PROPERTIES,
  375. ossl_rsapss_keymgmt_functions, "OpenSSL RSA-PSS FIPS implementation" },
  376. #ifndef OPENSSL_NO_EC
  377. { "EC:id-ecPublicKey", FIPS_DEFAULT_PROPERTIES, ossl_ec_keymgmt_functions,
  378. "OpenSSL EC FIPS implementation" },
  379. { "X25519", FIPS_DEFAULT_PROPERTIES, ossl_x25519_keymgmt_functions,
  380. "OpenSSL X25519 FIPS implementation" },
  381. { "X448", FIPS_DEFAULT_PROPERTIES, ossl_x448_keymgmt_functions,
  382. "OpenSSL X448 FIPS implementation" },
  383. { "ED25519", FIPS_DEFAULT_PROPERTIES, ossl_ed25519_keymgmt_functions,
  384. "OpenSSL ED25519 FIPS implementation" },
  385. { "ED448", FIPS_DEFAULT_PROPERTIES, ossl_ed448_keymgmt_functions,
  386. "OpenSSL ED448 FIPS implementation" },
  387. #endif
  388. { "TLS1-PRF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
  389. "OpenSSL TLS1-PRF via EVP_PKEY FIPS implementation" },
  390. { "HKDF", FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
  391. "OpenSSL HKDF via EVP_PKEY FIPS implementation" },
  392. { "HMAC", FIPS_DEFAULT_PROPERTIES, ossl_mac_legacy_keymgmt_functions,
  393. "OpenSSL HMAC via EVP_PKEY FIPS implementation" },
  394. #ifndef OPENSSL_NO_CMAC
  395. { "CMAC", FIPS_DEFAULT_PROPERTIES, ossl_cossl_mac_legacy_keymgmt_functions,
  396. "OpenSSL CMAC via EVP_PKEY FIPS implementation" },
  397. #endif
  398. { NULL, NULL, NULL }
  399. };
  400. static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
  401. int *no_cache)
  402. {
  403. *no_cache = 0;
  404. if (!ossl_prov_is_running())
  405. return NULL;
  406. switch (operation_id) {
  407. case OSSL_OP_DIGEST:
  408. return fips_digests;
  409. case OSSL_OP_CIPHER:
  410. return exported_fips_ciphers;
  411. case OSSL_OP_MAC:
  412. return fips_macs;
  413. case OSSL_OP_KDF:
  414. return fips_kdfs;
  415. case OSSL_OP_RAND:
  416. return fips_rands;
  417. case OSSL_OP_KEYMGMT:
  418. return fips_keymgmt;
  419. case OSSL_OP_KEYEXCH:
  420. return fips_keyexch;
  421. case OSSL_OP_SIGNATURE:
  422. return fips_signature;
  423. case OSSL_OP_ASYM_CIPHER:
  424. return fips_asym_cipher;
  425. case OSSL_OP_KEM:
  426. return fips_asym_kem;
  427. }
  428. return NULL;
  429. }
  430. static void fips_teardown(void *provctx)
  431. {
  432. OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
  433. ossl_prov_ctx_free(provctx);
  434. }
  435. static void fips_intern_teardown(void *provctx)
  436. {
  437. /*
  438. * We know that the library context is the same as for the outer provider,
  439. * so no need to destroy it here.
  440. */
  441. ossl_prov_ctx_free(provctx);
  442. }
  443. /* Functions we provide to the core */
  444. static const OSSL_DISPATCH fips_dispatch_table[] = {
  445. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
  446. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
  447. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
  448. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
  449. { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
  450. (void (*)(void))ossl_prov_get_capabilities },
  451. { OSSL_FUNC_PROVIDER_SELF_TEST, (void (*)(void))fips_self_test },
  452. { 0, NULL }
  453. };
  454. /* Functions we provide to ourself */
  455. static const OSSL_DISPATCH intern_dispatch_table[] = {
  456. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
  457. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
  458. { 0, NULL }
  459. };
  460. int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
  461. const OSSL_DISPATCH *in,
  462. const OSSL_DISPATCH **out,
  463. void **provctx)
  464. {
  465. FIPS_GLOBAL *fgbl;
  466. OSSL_LIB_CTX *libctx = NULL;
  467. if (!ossl_prov_seeding_from_dispatch(in))
  468. return 0;
  469. for (; in->function_id != 0; in++) {
  470. switch (in->function_id) {
  471. case OSSL_FUNC_CORE_GET_LIBCTX:
  472. c_get_libctx = OSSL_FUNC_core_get_libctx(in);
  473. break;
  474. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  475. c_gettable_params = OSSL_FUNC_core_gettable_params(in);
  476. break;
  477. case OSSL_FUNC_CORE_GET_PARAMS:
  478. c_get_params = OSSL_FUNC_core_get_params(in);
  479. break;
  480. case OSSL_FUNC_CORE_THREAD_START:
  481. c_thread_start = OSSL_FUNC_core_thread_start(in);
  482. break;
  483. case OSSL_FUNC_CORE_NEW_ERROR:
  484. c_new_error = OSSL_FUNC_core_new_error(in);
  485. break;
  486. case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
  487. c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
  488. break;
  489. case OSSL_FUNC_CORE_VSET_ERROR:
  490. c_vset_error = OSSL_FUNC_core_vset_error(in);
  491. break;
  492. case OSSL_FUNC_CORE_SET_ERROR_MARK:
  493. c_set_error_mark = OSSL_FUNC_core_set_error_mark(in);
  494. break;
  495. case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
  496. c_clear_last_error_mark = OSSL_FUNC_core_clear_last_error_mark(in);
  497. break;
  498. case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
  499. c_pop_error_to_mark = OSSL_FUNC_core_pop_error_to_mark(in);
  500. break;
  501. case OSSL_FUNC_CRYPTO_MALLOC:
  502. c_CRYPTO_malloc = OSSL_FUNC_CRYPTO_malloc(in);
  503. break;
  504. case OSSL_FUNC_CRYPTO_ZALLOC:
  505. c_CRYPTO_zalloc = OSSL_FUNC_CRYPTO_zalloc(in);
  506. break;
  507. case OSSL_FUNC_CRYPTO_FREE:
  508. c_CRYPTO_free = OSSL_FUNC_CRYPTO_free(in);
  509. break;
  510. case OSSL_FUNC_CRYPTO_CLEAR_FREE:
  511. c_CRYPTO_clear_free = OSSL_FUNC_CRYPTO_clear_free(in);
  512. break;
  513. case OSSL_FUNC_CRYPTO_REALLOC:
  514. c_CRYPTO_realloc = OSSL_FUNC_CRYPTO_realloc(in);
  515. break;
  516. case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
  517. c_CRYPTO_clear_realloc = OSSL_FUNC_CRYPTO_clear_realloc(in);
  518. break;
  519. case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
  520. c_CRYPTO_secure_malloc = OSSL_FUNC_CRYPTO_secure_malloc(in);
  521. break;
  522. case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
  523. c_CRYPTO_secure_zalloc = OSSL_FUNC_CRYPTO_secure_zalloc(in);
  524. break;
  525. case OSSL_FUNC_CRYPTO_SECURE_FREE:
  526. c_CRYPTO_secure_free = OSSL_FUNC_CRYPTO_secure_free(in);
  527. break;
  528. case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
  529. c_CRYPTO_secure_clear_free = OSSL_FUNC_CRYPTO_secure_clear_free(in);
  530. break;
  531. case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
  532. c_CRYPTO_secure_allocated = OSSL_FUNC_CRYPTO_secure_allocated(in);
  533. break;
  534. case OSSL_FUNC_BIO_NEW_FILE:
  535. selftest_params.bio_new_file_cb = OSSL_FUNC_BIO_new_file(in);
  536. break;
  537. case OSSL_FUNC_BIO_NEW_MEMBUF:
  538. selftest_params.bio_new_buffer_cb = OSSL_FUNC_BIO_new_membuf(in);
  539. break;
  540. case OSSL_FUNC_BIO_READ_EX:
  541. selftest_params.bio_read_ex_cb = OSSL_FUNC_BIO_read_ex(in);
  542. break;
  543. case OSSL_FUNC_BIO_FREE:
  544. selftest_params.bio_free_cb = OSSL_FUNC_BIO_free(in);
  545. break;
  546. case OSSL_FUNC_BIO_VSNPRINTF:
  547. c_BIO_vsnprintf = OSSL_FUNC_BIO_vsnprintf(in);
  548. break;
  549. case OSSL_FUNC_SELF_TEST_CB:
  550. c_stcbfn = OSSL_FUNC_self_test_cb(in);
  551. break;
  552. default:
  553. /* Just ignore anything we don't understand */
  554. break;
  555. }
  556. }
  557. set_self_test_cb(handle);
  558. if (!c_get_params(handle, core_params)) {
  559. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  560. return 0;
  561. }
  562. /* Disable the conditional error check if is disabled in the fips config file*/
  563. if (selftest_params.conditional_error_check != NULL
  564. && strcmp(selftest_params.conditional_error_check, "0") == 0)
  565. SELF_TEST_disable_conditional_error_state();
  566. /* Disable the security check if is disabled in the fips config file*/
  567. if (fips_security_check_option != NULL
  568. && strcmp(fips_security_check_option, "0") == 0)
  569. fips_security_checks = 0;
  570. /* Create a context. */
  571. if ((*provctx = ossl_prov_ctx_new()) == NULL
  572. || (libctx = OSSL_LIB_CTX_new()) == NULL) {
  573. /*
  574. * We free libctx separately here and only here because it hasn't
  575. * been attached to *provctx. All other error paths below rely
  576. * solely on fips_teardown.
  577. */
  578. OSSL_LIB_CTX_free(libctx);
  579. goto err;
  580. }
  581. ossl_prov_ctx_set0_libctx(*provctx, libctx);
  582. ossl_prov_ctx_set0_handle(*provctx, handle);
  583. if ((fgbl = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_FIPS_PROV_INDEX,
  584. &fips_prov_ossl_ctx_method)) == NULL)
  585. goto err;
  586. fgbl->handle = handle;
  587. ossl_prov_cache_exported_algorithms(fips_ciphers, exported_fips_ciphers);
  588. selftest_params.libctx = libctx;
  589. if (!SELF_TEST_post(&selftest_params, 0)) {
  590. ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_POST_FAILURE);
  591. goto err;
  592. }
  593. *out = fips_dispatch_table;
  594. return 1;
  595. err:
  596. fips_teardown(*provctx);
  597. *provctx = NULL;
  598. return 0;
  599. }
  600. /*
  601. * The internal init function used when the FIPS module uses EVP to call
  602. * another algorithm also in the FIPS module. This is a recursive call that has
  603. * been made from within the FIPS module itself. To make this work, we populate
  604. * the provider context of this inner instance with the same library context
  605. * that was used in the EVP call that initiated this recursive call.
  606. */
  607. OSSL_provider_init_fn ossl_fips_intern_provider_init;
  608. int ossl_fips_intern_provider_init(const OSSL_CORE_HANDLE *handle,
  609. const OSSL_DISPATCH *in,
  610. const OSSL_DISPATCH **out,
  611. void **provctx)
  612. {
  613. OSSL_FUNC_core_get_libctx_fn *c_internal_get_libctx = NULL;
  614. for (; in->function_id != 0; in++) {
  615. switch (in->function_id) {
  616. case OSSL_FUNC_CORE_GET_LIBCTX:
  617. c_internal_get_libctx = OSSL_FUNC_core_get_libctx(in);
  618. break;
  619. default:
  620. break;
  621. }
  622. }
  623. if (c_internal_get_libctx == NULL)
  624. return 0;
  625. if ((*provctx = ossl_prov_ctx_new()) == NULL)
  626. return 0;
  627. /*
  628. * Using the parent library context only works because we are a built-in
  629. * internal provider. This is not something that most providers would be
  630. * able to do.
  631. */
  632. ossl_prov_ctx_set0_libctx(*provctx,
  633. (OSSL_LIB_CTX *)c_internal_get_libctx(handle));
  634. ossl_prov_ctx_set0_handle(*provctx, handle);
  635. *out = intern_dispatch_table;
  636. return 1;
  637. }
  638. void ERR_new(void)
  639. {
  640. c_new_error(NULL);
  641. }
  642. void ERR_set_debug(const char *file, int line, const char *func)
  643. {
  644. c_set_error_debug(NULL, file, line, func);
  645. }
  646. void ERR_set_error(int lib, int reason, const char *fmt, ...)
  647. {
  648. va_list args;
  649. va_start(args, fmt);
  650. c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
  651. va_end(args);
  652. }
  653. void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
  654. {
  655. c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
  656. }
  657. int ERR_set_mark(void)
  658. {
  659. return c_set_error_mark(NULL);
  660. }
  661. int ERR_clear_last_mark(void)
  662. {
  663. return c_clear_last_error_mark(NULL);
  664. }
  665. int ERR_pop_to_mark(void)
  666. {
  667. return c_pop_error_to_mark(NULL);
  668. }
  669. /*
  670. * This must take a library context, since it's called from the depths
  671. * of crypto/initthread.c code, where it's (correctly) assumed that the
  672. * passed caller argument is an OSSL_LIB_CTX pointer (since the same routine
  673. * is also called from other parts of libcrypto, which all pass around a
  674. * OSSL_LIB_CTX pointer)
  675. */
  676. const OSSL_CORE_HANDLE *FIPS_get_core_handle(OSSL_LIB_CTX *libctx)
  677. {
  678. FIPS_GLOBAL *fgbl = ossl_lib_ctx_get_data(libctx,
  679. OSSL_LIB_CTX_FIPS_PROV_INDEX,
  680. &fips_prov_ossl_ctx_method);
  681. if (fgbl == NULL)
  682. return NULL;
  683. return fgbl->handle;
  684. }
  685. void *CRYPTO_malloc(size_t num, const char *file, int line)
  686. {
  687. return c_CRYPTO_malloc(num, file, line);
  688. }
  689. void *CRYPTO_zalloc(size_t num, const char *file, int line)
  690. {
  691. return c_CRYPTO_zalloc(num, file, line);
  692. }
  693. void CRYPTO_free(void *ptr, const char *file, int line)
  694. {
  695. c_CRYPTO_free(ptr, file, line);
  696. }
  697. void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
  698. {
  699. c_CRYPTO_clear_free(ptr, num, file, line);
  700. }
  701. void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
  702. {
  703. return c_CRYPTO_realloc(addr, num, file, line);
  704. }
  705. void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
  706. const char *file, int line)
  707. {
  708. return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
  709. }
  710. void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
  711. {
  712. return c_CRYPTO_secure_malloc(num, file, line);
  713. }
  714. void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
  715. {
  716. return c_CRYPTO_secure_zalloc(num, file, line);
  717. }
  718. void CRYPTO_secure_free(void *ptr, const char *file, int line)
  719. {
  720. c_CRYPTO_secure_free(ptr, file, line);
  721. }
  722. void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
  723. {
  724. c_CRYPTO_secure_clear_free(ptr, num, file, line);
  725. }
  726. int CRYPTO_secure_allocated(const void *ptr)
  727. {
  728. return c_CRYPTO_secure_allocated(ptr);
  729. }
  730. int BIO_snprintf(char *buf, size_t n, const char *format, ...)
  731. {
  732. va_list args;
  733. int ret;
  734. va_start(args, format);
  735. ret = c_BIO_vsnprintf(buf, n, format, args);
  736. va_end(args);
  737. return ret;
  738. }
  739. int FIPS_security_check_enabled(void)
  740. {
  741. return fips_security_checks;
  742. }
  743. void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
  744. void **cbarg)
  745. {
  746. if (libctx == NULL)
  747. libctx = selftest_params.libctx;
  748. if (c_stcbfn != NULL && c_get_libctx != NULL) {
  749. /* Get the parent libctx */
  750. c_stcbfn(c_get_libctx(FIPS_get_core_handle(libctx)), cb, cbarg);
  751. } else {
  752. if (cb != NULL)
  753. *cb = NULL;
  754. if (cbarg != NULL)
  755. *cbarg = NULL;
  756. }
  757. }