defltprov.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * Copyright 2019-2023 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 <string.h>
  10. #include <stdio.h>
  11. #include <openssl/opensslconf.h>
  12. #include <openssl/core.h>
  13. #include <openssl/core_dispatch.h>
  14. #include <openssl/core_names.h>
  15. #include <openssl/params.h>
  16. #include "prov/bio.h"
  17. #include "prov/provider_ctx.h"
  18. #include "prov/providercommon.h"
  19. #include "prov/implementations.h"
  20. #include "prov/names.h"
  21. #include "prov/provider_util.h"
  22. #include "prov/seeding.h"
  23. #include "internal/nelem.h"
  24. /*
  25. * Forward declarations to ensure that interface functions are correctly
  26. * defined.
  27. */
  28. static OSSL_FUNC_provider_gettable_params_fn deflt_gettable_params;
  29. static OSSL_FUNC_provider_get_params_fn deflt_get_params;
  30. static OSSL_FUNC_provider_query_operation_fn deflt_query;
  31. #define ALGC(NAMES, FUNC, CHECK) { { NAMES, "provider=default", FUNC }, CHECK }
  32. #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
  33. /* Functions provided by the core */
  34. static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
  35. static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
  36. /* Parameters we provide to the core */
  37. static const OSSL_PARAM deflt_param_types[] = {
  38. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
  39. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  40. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
  41. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
  42. OSSL_PARAM_END
  43. };
  44. static const OSSL_PARAM *deflt_gettable_params(void *provctx)
  45. {
  46. return deflt_param_types;
  47. }
  48. static int deflt_get_params(void *provctx, OSSL_PARAM params[])
  49. {
  50. OSSL_PARAM *p;
  51. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  52. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Default Provider"))
  53. return 0;
  54. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
  55. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
  56. return 0;
  57. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
  58. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
  59. return 0;
  60. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
  61. if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
  62. return 0;
  63. return 1;
  64. }
  65. /*
  66. * For the algorithm names, we use the following formula for our primary
  67. * names:
  68. *
  69. * ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
  70. *
  71. * VERSION is only present if there are multiple versions of
  72. * an alg (MD2, MD4, MD5). It may be omitted if there is only
  73. * one version (if a subsequent version is released in the future,
  74. * we can always change the canonical name, and add the old name
  75. * as an alias).
  76. *
  77. * SUBNAME may be present where we are combining multiple
  78. * algorithms together, e.g. MD5-SHA1.
  79. *
  80. * SIZE is only present if multiple versions of an algorithm exist
  81. * with different sizes (e.g. AES-128-CBC, AES-256-CBC)
  82. *
  83. * MODE is only present where applicable.
  84. *
  85. * We add diverse other names where applicable, such as the names that
  86. * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
  87. * we have used historically.
  88. *
  89. * Algorithm names are case insensitive, but we use all caps in our "canonical"
  90. * names for consistency.
  91. */
  92. static const OSSL_ALGORITHM deflt_digests[] = {
  93. /* Our primary name:NIST name[:our older names] */
  94. { PROV_NAMES_SHA1, "provider=default", ossl_sha1_functions },
  95. { PROV_NAMES_SHA2_224, "provider=default", ossl_sha224_functions },
  96. { PROV_NAMES_SHA2_256, "provider=default", ossl_sha256_functions },
  97. { PROV_NAMES_SHA2_256_192, "provider=default", ossl_sha256_192_functions },
  98. { PROV_NAMES_SHA2_384, "provider=default", ossl_sha384_functions },
  99. { PROV_NAMES_SHA2_512, "provider=default", ossl_sha512_functions },
  100. { PROV_NAMES_SHA2_512_224, "provider=default", ossl_sha512_224_functions },
  101. { PROV_NAMES_SHA2_512_256, "provider=default", ossl_sha512_256_functions },
  102. /* We agree with NIST here, so one name only */
  103. { PROV_NAMES_SHA3_224, "provider=default", ossl_sha3_224_functions },
  104. { PROV_NAMES_SHA3_256, "provider=default", ossl_sha3_256_functions },
  105. { PROV_NAMES_SHA3_384, "provider=default", ossl_sha3_384_functions },
  106. { PROV_NAMES_SHA3_512, "provider=default", ossl_sha3_512_functions },
  107. { PROV_NAMES_KECCAK_224, "provider=default", ossl_keccak_224_functions },
  108. { PROV_NAMES_KECCAK_256, "provider=default", ossl_keccak_256_functions },
  109. { PROV_NAMES_KECCAK_384, "provider=default", ossl_keccak_384_functions },
  110. { PROV_NAMES_KECCAK_512, "provider=default", ossl_keccak_512_functions },
  111. /*
  112. * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
  113. * the KMAC-128 and KMAC-256.
  114. */
  115. { PROV_NAMES_KECCAK_KMAC_128, "provider=default",
  116. ossl_keccak_kmac_128_functions },
  117. { PROV_NAMES_KECCAK_KMAC_256, "provider=default",
  118. ossl_keccak_kmac_256_functions },
  119. /* Our primary name:NIST name */
  120. { PROV_NAMES_SHAKE_128, "provider=default", ossl_shake_128_functions },
  121. { PROV_NAMES_SHAKE_256, "provider=default", ossl_shake_256_functions },
  122. #ifndef OPENSSL_NO_BLAKE2
  123. /*
  124. * https://blake2.net/ doesn't specify size variants,
  125. * but mentions that Bouncy Castle uses the names
  126. * BLAKE2b-160, BLAKE2b-256, BLAKE2b-384, and BLAKE2b-512
  127. * If we assume that "2b" and "2s" are versions, that pattern
  128. * fits with ours. We also add our historical names.
  129. */
  130. { PROV_NAMES_BLAKE2S_256, "provider=default", ossl_blake2s256_functions },
  131. { PROV_NAMES_BLAKE2B_512, "provider=default", ossl_blake2b512_functions },
  132. #endif /* OPENSSL_NO_BLAKE2 */
  133. #ifndef OPENSSL_NO_SM3
  134. { PROV_NAMES_SM3, "provider=default", ossl_sm3_functions },
  135. #endif /* OPENSSL_NO_SM3 */
  136. #ifndef OPENSSL_NO_MD5
  137. { PROV_NAMES_MD5, "provider=default", ossl_md5_functions },
  138. { PROV_NAMES_MD5_SHA1, "provider=default", ossl_md5_sha1_functions },
  139. #endif /* OPENSSL_NO_MD5 */
  140. #ifndef OPENSSL_NO_RMD160
  141. { PROV_NAMES_RIPEMD_160, "provider=default", ossl_ripemd160_functions },
  142. #endif /* OPENSSL_NO_RMD160 */
  143. { PROV_NAMES_NULL, "provider=default", ossl_nullmd_functions },
  144. { NULL, NULL, NULL }
  145. };
  146. static const OSSL_ALGORITHM_CAPABLE deflt_ciphers[] = {
  147. ALG(PROV_NAMES_NULL, ossl_null_functions),
  148. ALG(PROV_NAMES_AES_256_ECB, ossl_aes256ecb_functions),
  149. ALG(PROV_NAMES_AES_192_ECB, ossl_aes192ecb_functions),
  150. ALG(PROV_NAMES_AES_128_ECB, ossl_aes128ecb_functions),
  151. ALG(PROV_NAMES_AES_256_CBC, ossl_aes256cbc_functions),
  152. ALG(PROV_NAMES_AES_192_CBC, ossl_aes192cbc_functions),
  153. ALG(PROV_NAMES_AES_128_CBC, ossl_aes128cbc_functions),
  154. ALG(PROV_NAMES_AES_128_CBC_CTS, ossl_aes128cbc_cts_functions),
  155. ALG(PROV_NAMES_AES_192_CBC_CTS, ossl_aes192cbc_cts_functions),
  156. ALG(PROV_NAMES_AES_256_CBC_CTS, ossl_aes256cbc_cts_functions),
  157. ALG(PROV_NAMES_AES_256_OFB, ossl_aes256ofb_functions),
  158. ALG(PROV_NAMES_AES_192_OFB, ossl_aes192ofb_functions),
  159. ALG(PROV_NAMES_AES_128_OFB, ossl_aes128ofb_functions),
  160. ALG(PROV_NAMES_AES_256_CFB, ossl_aes256cfb_functions),
  161. ALG(PROV_NAMES_AES_192_CFB, ossl_aes192cfb_functions),
  162. ALG(PROV_NAMES_AES_128_CFB, ossl_aes128cfb_functions),
  163. ALG(PROV_NAMES_AES_256_CFB1, ossl_aes256cfb1_functions),
  164. ALG(PROV_NAMES_AES_192_CFB1, ossl_aes192cfb1_functions),
  165. ALG(PROV_NAMES_AES_128_CFB1, ossl_aes128cfb1_functions),
  166. ALG(PROV_NAMES_AES_256_CFB8, ossl_aes256cfb8_functions),
  167. ALG(PROV_NAMES_AES_192_CFB8, ossl_aes192cfb8_functions),
  168. ALG(PROV_NAMES_AES_128_CFB8, ossl_aes128cfb8_functions),
  169. ALG(PROV_NAMES_AES_256_CTR, ossl_aes256ctr_functions),
  170. ALG(PROV_NAMES_AES_192_CTR, ossl_aes192ctr_functions),
  171. ALG(PROV_NAMES_AES_128_CTR, ossl_aes128ctr_functions),
  172. ALG(PROV_NAMES_AES_256_XTS, ossl_aes256xts_functions),
  173. ALG(PROV_NAMES_AES_128_XTS, ossl_aes128xts_functions),
  174. #ifndef OPENSSL_NO_OCB
  175. ALG(PROV_NAMES_AES_256_OCB, ossl_aes256ocb_functions),
  176. ALG(PROV_NAMES_AES_192_OCB, ossl_aes192ocb_functions),
  177. ALG(PROV_NAMES_AES_128_OCB, ossl_aes128ocb_functions),
  178. #endif /* OPENSSL_NO_OCB */
  179. #ifndef OPENSSL_NO_SIV
  180. ALG(PROV_NAMES_AES_128_SIV, ossl_aes128siv_functions),
  181. ALG(PROV_NAMES_AES_192_SIV, ossl_aes192siv_functions),
  182. ALG(PROV_NAMES_AES_256_SIV, ossl_aes256siv_functions),
  183. ALG(PROV_NAMES_AES_128_GCM_SIV, ossl_aes128gcm_siv_functions),
  184. ALG(PROV_NAMES_AES_192_GCM_SIV, ossl_aes192gcm_siv_functions),
  185. ALG(PROV_NAMES_AES_256_GCM_SIV, ossl_aes256gcm_siv_functions),
  186. #endif /* OPENSSL_NO_SIV */
  187. ALG(PROV_NAMES_AES_256_GCM, ossl_aes256gcm_functions),
  188. ALG(PROV_NAMES_AES_192_GCM, ossl_aes192gcm_functions),
  189. ALG(PROV_NAMES_AES_128_GCM, ossl_aes128gcm_functions),
  190. ALG(PROV_NAMES_AES_256_CCM, ossl_aes256ccm_functions),
  191. ALG(PROV_NAMES_AES_192_CCM, ossl_aes192ccm_functions),
  192. ALG(PROV_NAMES_AES_128_CCM, ossl_aes128ccm_functions),
  193. ALG(PROV_NAMES_AES_256_WRAP, ossl_aes256wrap_functions),
  194. ALG(PROV_NAMES_AES_192_WRAP, ossl_aes192wrap_functions),
  195. ALG(PROV_NAMES_AES_128_WRAP, ossl_aes128wrap_functions),
  196. ALG(PROV_NAMES_AES_256_WRAP_PAD, ossl_aes256wrappad_functions),
  197. ALG(PROV_NAMES_AES_192_WRAP_PAD, ossl_aes192wrappad_functions),
  198. ALG(PROV_NAMES_AES_128_WRAP_PAD, ossl_aes128wrappad_functions),
  199. ALG(PROV_NAMES_AES_256_WRAP_INV, ossl_aes256wrapinv_functions),
  200. ALG(PROV_NAMES_AES_192_WRAP_INV, ossl_aes192wrapinv_functions),
  201. ALG(PROV_NAMES_AES_128_WRAP_INV, ossl_aes128wrapinv_functions),
  202. ALG(PROV_NAMES_AES_256_WRAP_PAD_INV, ossl_aes256wrappadinv_functions),
  203. ALG(PROV_NAMES_AES_192_WRAP_PAD_INV, ossl_aes192wrappadinv_functions),
  204. ALG(PROV_NAMES_AES_128_WRAP_PAD_INV, ossl_aes128wrappadinv_functions),
  205. ALGC(PROV_NAMES_AES_128_CBC_HMAC_SHA1, ossl_aes128cbc_hmac_sha1_functions,
  206. ossl_cipher_capable_aes_cbc_hmac_sha1),
  207. ALGC(PROV_NAMES_AES_256_CBC_HMAC_SHA1, ossl_aes256cbc_hmac_sha1_functions,
  208. ossl_cipher_capable_aes_cbc_hmac_sha1),
  209. ALGC(PROV_NAMES_AES_128_CBC_HMAC_SHA256, ossl_aes128cbc_hmac_sha256_functions,
  210. ossl_cipher_capable_aes_cbc_hmac_sha256),
  211. ALGC(PROV_NAMES_AES_256_CBC_HMAC_SHA256, ossl_aes256cbc_hmac_sha256_functions,
  212. ossl_cipher_capable_aes_cbc_hmac_sha256),
  213. #ifndef OPENSSL_NO_ARIA
  214. ALG(PROV_NAMES_ARIA_256_GCM, ossl_aria256gcm_functions),
  215. ALG(PROV_NAMES_ARIA_192_GCM, ossl_aria192gcm_functions),
  216. ALG(PROV_NAMES_ARIA_128_GCM, ossl_aria128gcm_functions),
  217. ALG(PROV_NAMES_ARIA_256_CCM, ossl_aria256ccm_functions),
  218. ALG(PROV_NAMES_ARIA_192_CCM, ossl_aria192ccm_functions),
  219. ALG(PROV_NAMES_ARIA_128_CCM, ossl_aria128ccm_functions),
  220. ALG(PROV_NAMES_ARIA_256_ECB, ossl_aria256ecb_functions),
  221. ALG(PROV_NAMES_ARIA_192_ECB, ossl_aria192ecb_functions),
  222. ALG(PROV_NAMES_ARIA_128_ECB, ossl_aria128ecb_functions),
  223. ALG(PROV_NAMES_ARIA_256_CBC, ossl_aria256cbc_functions),
  224. ALG(PROV_NAMES_ARIA_192_CBC, ossl_aria192cbc_functions),
  225. ALG(PROV_NAMES_ARIA_128_CBC, ossl_aria128cbc_functions),
  226. ALG(PROV_NAMES_ARIA_256_OFB, ossl_aria256ofb_functions),
  227. ALG(PROV_NAMES_ARIA_192_OFB, ossl_aria192ofb_functions),
  228. ALG(PROV_NAMES_ARIA_128_OFB, ossl_aria128ofb_functions),
  229. ALG(PROV_NAMES_ARIA_256_CFB, ossl_aria256cfb_functions),
  230. ALG(PROV_NAMES_ARIA_192_CFB, ossl_aria192cfb_functions),
  231. ALG(PROV_NAMES_ARIA_128_CFB, ossl_aria128cfb_functions),
  232. ALG(PROV_NAMES_ARIA_256_CFB1, ossl_aria256cfb1_functions),
  233. ALG(PROV_NAMES_ARIA_192_CFB1, ossl_aria192cfb1_functions),
  234. ALG(PROV_NAMES_ARIA_128_CFB1, ossl_aria128cfb1_functions),
  235. ALG(PROV_NAMES_ARIA_256_CFB8, ossl_aria256cfb8_functions),
  236. ALG(PROV_NAMES_ARIA_192_CFB8, ossl_aria192cfb8_functions),
  237. ALG(PROV_NAMES_ARIA_128_CFB8, ossl_aria128cfb8_functions),
  238. ALG(PROV_NAMES_ARIA_256_CTR, ossl_aria256ctr_functions),
  239. ALG(PROV_NAMES_ARIA_192_CTR, ossl_aria192ctr_functions),
  240. ALG(PROV_NAMES_ARIA_128_CTR, ossl_aria128ctr_functions),
  241. #endif /* OPENSSL_NO_ARIA */
  242. #ifndef OPENSSL_NO_CAMELLIA
  243. ALG(PROV_NAMES_CAMELLIA_256_ECB, ossl_camellia256ecb_functions),
  244. ALG(PROV_NAMES_CAMELLIA_192_ECB, ossl_camellia192ecb_functions),
  245. ALG(PROV_NAMES_CAMELLIA_128_ECB, ossl_camellia128ecb_functions),
  246. ALG(PROV_NAMES_CAMELLIA_256_CBC, ossl_camellia256cbc_functions),
  247. ALG(PROV_NAMES_CAMELLIA_192_CBC, ossl_camellia192cbc_functions),
  248. ALG(PROV_NAMES_CAMELLIA_128_CBC, ossl_camellia128cbc_functions),
  249. ALG(PROV_NAMES_CAMELLIA_128_CBC_CTS, ossl_camellia128cbc_cts_functions),
  250. ALG(PROV_NAMES_CAMELLIA_192_CBC_CTS, ossl_camellia192cbc_cts_functions),
  251. ALG(PROV_NAMES_CAMELLIA_256_CBC_CTS, ossl_camellia256cbc_cts_functions),
  252. ALG(PROV_NAMES_CAMELLIA_256_OFB, ossl_camellia256ofb_functions),
  253. ALG(PROV_NAMES_CAMELLIA_192_OFB, ossl_camellia192ofb_functions),
  254. ALG(PROV_NAMES_CAMELLIA_128_OFB, ossl_camellia128ofb_functions),
  255. ALG(PROV_NAMES_CAMELLIA_256_CFB, ossl_camellia256cfb_functions),
  256. ALG(PROV_NAMES_CAMELLIA_192_CFB, ossl_camellia192cfb_functions),
  257. ALG(PROV_NAMES_CAMELLIA_128_CFB, ossl_camellia128cfb_functions),
  258. ALG(PROV_NAMES_CAMELLIA_256_CFB1, ossl_camellia256cfb1_functions),
  259. ALG(PROV_NAMES_CAMELLIA_192_CFB1, ossl_camellia192cfb1_functions),
  260. ALG(PROV_NAMES_CAMELLIA_128_CFB1, ossl_camellia128cfb1_functions),
  261. ALG(PROV_NAMES_CAMELLIA_256_CFB8, ossl_camellia256cfb8_functions),
  262. ALG(PROV_NAMES_CAMELLIA_192_CFB8, ossl_camellia192cfb8_functions),
  263. ALG(PROV_NAMES_CAMELLIA_128_CFB8, ossl_camellia128cfb8_functions),
  264. ALG(PROV_NAMES_CAMELLIA_256_CTR, ossl_camellia256ctr_functions),
  265. ALG(PROV_NAMES_CAMELLIA_192_CTR, ossl_camellia192ctr_functions),
  266. ALG(PROV_NAMES_CAMELLIA_128_CTR, ossl_camellia128ctr_functions),
  267. #endif /* OPENSSL_NO_CAMELLIA */
  268. #ifndef OPENSSL_NO_DES
  269. ALG(PROV_NAMES_DES_EDE3_ECB, ossl_tdes_ede3_ecb_functions),
  270. ALG(PROV_NAMES_DES_EDE3_CBC, ossl_tdes_ede3_cbc_functions),
  271. ALG(PROV_NAMES_DES_EDE3_OFB, ossl_tdes_ede3_ofb_functions),
  272. ALG(PROV_NAMES_DES_EDE3_CFB, ossl_tdes_ede3_cfb_functions),
  273. ALG(PROV_NAMES_DES_EDE3_CFB8, ossl_tdes_ede3_cfb8_functions),
  274. ALG(PROV_NAMES_DES_EDE3_CFB1, ossl_tdes_ede3_cfb1_functions),
  275. ALG(PROV_NAMES_DES3_WRAP, ossl_tdes_wrap_cbc_functions),
  276. ALG(PROV_NAMES_DES_EDE_ECB, ossl_tdes_ede2_ecb_functions),
  277. ALG(PROV_NAMES_DES_EDE_CBC, ossl_tdes_ede2_cbc_functions),
  278. ALG(PROV_NAMES_DES_EDE_OFB, ossl_tdes_ede2_ofb_functions),
  279. ALG(PROV_NAMES_DES_EDE_CFB, ossl_tdes_ede2_cfb_functions),
  280. #endif /* OPENSSL_NO_DES */
  281. #ifndef OPENSSL_NO_SM4
  282. ALG(PROV_NAMES_SM4_GCM, ossl_sm4128gcm_functions),
  283. ALG(PROV_NAMES_SM4_CCM, ossl_sm4128ccm_functions),
  284. ALG(PROV_NAMES_SM4_ECB, ossl_sm4128ecb_functions),
  285. ALG(PROV_NAMES_SM4_CBC, ossl_sm4128cbc_functions),
  286. ALG(PROV_NAMES_SM4_CTR, ossl_sm4128ctr_functions),
  287. ALG(PROV_NAMES_SM4_OFB, ossl_sm4128ofb128_functions),
  288. ALG(PROV_NAMES_SM4_CFB, ossl_sm4128cfb128_functions),
  289. ALG(PROV_NAMES_SM4_XTS, ossl_sm4128xts_functions),
  290. #endif /* OPENSSL_NO_SM4 */
  291. #ifndef OPENSSL_NO_CHACHA
  292. ALG(PROV_NAMES_ChaCha20, ossl_chacha20_functions),
  293. # ifndef OPENSSL_NO_POLY1305
  294. ALG(PROV_NAMES_ChaCha20_Poly1305, ossl_chacha20_ossl_poly1305_functions),
  295. # endif /* OPENSSL_NO_POLY1305 */
  296. #endif /* OPENSSL_NO_CHACHA */
  297. { { NULL, NULL, NULL }, NULL }
  298. };
  299. static OSSL_ALGORITHM exported_ciphers[OSSL_NELEM(deflt_ciphers)];
  300. static const OSSL_ALGORITHM deflt_macs[] = {
  301. #ifndef OPENSSL_NO_BLAKE2
  302. { PROV_NAMES_BLAKE2BMAC, "provider=default", ossl_blake2bmac_functions },
  303. { PROV_NAMES_BLAKE2SMAC, "provider=default", ossl_blake2smac_functions },
  304. #endif
  305. #ifndef OPENSSL_NO_CMAC
  306. { PROV_NAMES_CMAC, "provider=default", ossl_cmac_functions },
  307. #endif
  308. { PROV_NAMES_GMAC, "provider=default", ossl_gmac_functions },
  309. { PROV_NAMES_HMAC, "provider=default", ossl_hmac_functions },
  310. { PROV_NAMES_KMAC_128, "provider=default", ossl_kmac128_functions },
  311. { PROV_NAMES_KMAC_256, "provider=default", ossl_kmac256_functions },
  312. #ifndef OPENSSL_NO_SIPHASH
  313. { PROV_NAMES_SIPHASH, "provider=default", ossl_siphash_functions },
  314. #endif
  315. #ifndef OPENSSL_NO_POLY1305
  316. { PROV_NAMES_POLY1305, "provider=default", ossl_poly1305_functions },
  317. #endif
  318. { NULL, NULL, NULL }
  319. };
  320. static const OSSL_ALGORITHM deflt_kdfs[] = {
  321. { PROV_NAMES_HKDF, "provider=default", ossl_kdf_hkdf_functions },
  322. { PROV_NAMES_TLS1_3_KDF, "provider=default",
  323. ossl_kdf_tls1_3_kdf_functions },
  324. { PROV_NAMES_SSKDF, "provider=default", ossl_kdf_sskdf_functions },
  325. { PROV_NAMES_PBKDF2, "provider=default", ossl_kdf_pbkdf2_functions },
  326. { PROV_NAMES_PKCS12KDF, "provider=default", ossl_kdf_pkcs12_functions },
  327. { PROV_NAMES_SSHKDF, "provider=default", ossl_kdf_sshkdf_functions },
  328. { PROV_NAMES_X963KDF, "provider=default", ossl_kdf_x963_kdf_functions },
  329. { PROV_NAMES_TLS1_PRF, "provider=default", ossl_kdf_tls1_prf_functions },
  330. { PROV_NAMES_KBKDF, "provider=default", ossl_kdf_kbkdf_functions },
  331. { PROV_NAMES_X942KDF_ASN1, "provider=default", ossl_kdf_x942_kdf_functions },
  332. #ifndef OPENSSL_NO_SCRYPT
  333. { PROV_NAMES_SCRYPT, "provider=default", ossl_kdf_scrypt_functions },
  334. #endif
  335. { PROV_NAMES_KRB5KDF, "provider=default", ossl_kdf_krb5kdf_functions },
  336. { PROV_NAMES_HMAC_DRBG_KDF, "provider=default",
  337. ossl_kdf_hmac_drbg_functions },
  338. #ifndef OPENSSL_NO_ARGON2
  339. { PROV_NAMES_ARGON2I, "provider=default", ossl_kdf_argon2i_functions },
  340. { PROV_NAMES_ARGON2D, "provider=default", ossl_kdf_argon2d_functions },
  341. { PROV_NAMES_ARGON2ID, "provider=default", ossl_kdf_argon2id_functions },
  342. #endif
  343. { NULL, NULL, NULL }
  344. };
  345. static const OSSL_ALGORITHM deflt_keyexch[] = {
  346. #ifndef OPENSSL_NO_DH
  347. { PROV_NAMES_DH, "provider=default", ossl_dh_keyexch_functions },
  348. #endif
  349. #ifndef OPENSSL_NO_EC
  350. { PROV_NAMES_ECDH, "provider=default", ossl_ecdh_keyexch_functions },
  351. # ifndef OPENSSL_NO_ECX
  352. { PROV_NAMES_X25519, "provider=default", ossl_x25519_keyexch_functions },
  353. { PROV_NAMES_X448, "provider=default", ossl_x448_keyexch_functions },
  354. # endif
  355. #endif
  356. { PROV_NAMES_TLS1_PRF, "provider=default", ossl_kdf_tls1_prf_keyexch_functions },
  357. { PROV_NAMES_HKDF, "provider=default", ossl_kdf_hkdf_keyexch_functions },
  358. { PROV_NAMES_SCRYPT, "provider=default",
  359. ossl_kdf_scrypt_keyexch_functions },
  360. { NULL, NULL, NULL }
  361. };
  362. static const OSSL_ALGORITHM deflt_rands[] = {
  363. { PROV_NAMES_CTR_DRBG, "provider=default", ossl_drbg_ctr_functions },
  364. { PROV_NAMES_HASH_DRBG, "provider=default", ossl_drbg_hash_functions },
  365. { PROV_NAMES_HMAC_DRBG, "provider=default", ossl_drbg_ossl_hmac_functions },
  366. { PROV_NAMES_SEED_SRC, "provider=default", ossl_seed_src_functions },
  367. { PROV_NAMES_TEST_RAND, "provider=default", ossl_test_rng_functions },
  368. { NULL, NULL, NULL }
  369. };
  370. static const OSSL_ALGORITHM deflt_signature[] = {
  371. #ifndef OPENSSL_NO_DSA
  372. { PROV_NAMES_DSA, "provider=default", ossl_dsa_signature_functions },
  373. #endif
  374. { PROV_NAMES_RSA, "provider=default", ossl_rsa_signature_functions },
  375. #ifndef OPENSSL_NO_EC
  376. # ifndef OPENSSL_NO_ECX
  377. { PROV_NAMES_ED25519, "provider=default", ossl_ed25519_signature_functions },
  378. { PROV_NAMES_ED448, "provider=default", ossl_ed448_signature_functions },
  379. # endif
  380. { PROV_NAMES_ECDSA, "provider=default", ossl_ecdsa_signature_functions },
  381. # ifndef OPENSSL_NO_SM2
  382. { PROV_NAMES_SM2, "provider=default", ossl_sm2_signature_functions },
  383. # endif
  384. #endif
  385. { PROV_NAMES_HMAC, "provider=default", ossl_mac_legacy_hmac_signature_functions },
  386. { PROV_NAMES_SIPHASH, "provider=default",
  387. ossl_mac_legacy_siphash_signature_functions },
  388. #ifndef OPENSSL_NO_POLY1305
  389. { PROV_NAMES_POLY1305, "provider=default",
  390. ossl_mac_legacy_poly1305_signature_functions },
  391. #endif
  392. #ifndef OPENSSL_NO_CMAC
  393. { PROV_NAMES_CMAC, "provider=default", ossl_mac_legacy_cmac_signature_functions },
  394. #endif
  395. { NULL, NULL, NULL }
  396. };
  397. static const OSSL_ALGORITHM deflt_asym_cipher[] = {
  398. { PROV_NAMES_RSA, "provider=default", ossl_rsa_asym_cipher_functions },
  399. #ifndef OPENSSL_NO_SM2
  400. { PROV_NAMES_SM2, "provider=default", ossl_sm2_asym_cipher_functions },
  401. #endif
  402. { NULL, NULL, NULL }
  403. };
  404. static const OSSL_ALGORITHM deflt_asym_kem[] = {
  405. { PROV_NAMES_RSA, "provider=default", ossl_rsa_asym_kem_functions },
  406. #ifndef OPENSSL_NO_EC
  407. # ifndef OPENSSL_NO_ECX
  408. { PROV_NAMES_X25519, "provider=default", ossl_ecx_asym_kem_functions },
  409. { PROV_NAMES_X448, "provider=default", ossl_ecx_asym_kem_functions },
  410. # endif
  411. { PROV_NAMES_EC, "provider=default", ossl_ec_asym_kem_functions },
  412. #endif
  413. { NULL, NULL, NULL }
  414. };
  415. static const OSSL_ALGORITHM deflt_keymgmt[] = {
  416. #ifndef OPENSSL_NO_DH
  417. { PROV_NAMES_DH, "provider=default", ossl_dh_keymgmt_functions,
  418. PROV_DESCS_DH },
  419. { PROV_NAMES_DHX, "provider=default", ossl_dhx_keymgmt_functions,
  420. PROV_DESCS_DHX },
  421. #endif
  422. #ifndef OPENSSL_NO_DSA
  423. { PROV_NAMES_DSA, "provider=default", ossl_dsa_keymgmt_functions,
  424. PROV_DESCS_DSA},
  425. #endif
  426. { PROV_NAMES_RSA, "provider=default", ossl_rsa_keymgmt_functions,
  427. PROV_DESCS_RSA },
  428. { PROV_NAMES_RSA_PSS, "provider=default", ossl_rsapss_keymgmt_functions,
  429. PROV_DESCS_RSA_PSS },
  430. #ifndef OPENSSL_NO_EC
  431. { PROV_NAMES_EC, "provider=default", ossl_ec_keymgmt_functions,
  432. PROV_DESCS_EC },
  433. # ifndef OPENSSL_NO_ECX
  434. { PROV_NAMES_X25519, "provider=default", ossl_x25519_keymgmt_functions,
  435. PROV_DESCS_X25519 },
  436. { PROV_NAMES_X448, "provider=default", ossl_x448_keymgmt_functions,
  437. PROV_DESCS_X448 },
  438. { PROV_NAMES_ED25519, "provider=default", ossl_ed25519_keymgmt_functions,
  439. PROV_DESCS_ED25519 },
  440. { PROV_NAMES_ED448, "provider=default", ossl_ed448_keymgmt_functions,
  441. PROV_DESCS_ED448 },
  442. # endif
  443. #endif
  444. { PROV_NAMES_TLS1_PRF, "provider=default", ossl_kdf_keymgmt_functions,
  445. PROV_DESCS_TLS1_PRF_SIGN },
  446. { PROV_NAMES_HKDF, "provider=default", ossl_kdf_keymgmt_functions,
  447. PROV_DESCS_HKDF_SIGN },
  448. { PROV_NAMES_SCRYPT, "provider=default", ossl_kdf_keymgmt_functions,
  449. PROV_DESCS_SCRYPT_SIGN },
  450. { PROV_NAMES_HMAC, "provider=default", ossl_mac_legacy_keymgmt_functions,
  451. PROV_DESCS_HMAC_SIGN },
  452. { PROV_NAMES_SIPHASH, "provider=default", ossl_mac_legacy_keymgmt_functions,
  453. PROV_DESCS_SIPHASH_SIGN },
  454. #ifndef OPENSSL_NO_POLY1305
  455. { PROV_NAMES_POLY1305, "provider=default", ossl_mac_legacy_keymgmt_functions,
  456. PROV_DESCS_POLY1305_SIGN },
  457. #endif
  458. #ifndef OPENSSL_NO_CMAC
  459. { PROV_NAMES_CMAC, "provider=default", ossl_cmac_legacy_keymgmt_functions,
  460. PROV_DESCS_CMAC_SIGN },
  461. #endif
  462. #ifndef OPENSSL_NO_SM2
  463. { PROV_NAMES_SM2, "provider=default", ossl_sm2_keymgmt_functions,
  464. PROV_DESCS_SM2 },
  465. #endif
  466. { NULL, NULL, NULL }
  467. };
  468. static const OSSL_ALGORITHM deflt_encoder[] = {
  469. #define ENCODER_PROVIDER "default"
  470. #include "encoders.inc"
  471. { NULL, NULL, NULL }
  472. #undef ENCODER_PROVIDER
  473. };
  474. static const OSSL_ALGORITHM deflt_decoder[] = {
  475. #define DECODER_PROVIDER "default"
  476. #include "decoders.inc"
  477. { NULL, NULL, NULL }
  478. #undef DECODER_PROVIDER
  479. };
  480. static const OSSL_ALGORITHM deflt_store[] = {
  481. #define STORE(name, _fips, func_table) \
  482. { name, "provider=default,fips=" _fips, (func_table) },
  483. #include "stores.inc"
  484. { NULL, NULL, NULL }
  485. #undef STORE
  486. };
  487. static const OSSL_ALGORITHM *deflt_query(void *provctx, int operation_id,
  488. int *no_cache)
  489. {
  490. *no_cache = 0;
  491. switch (operation_id) {
  492. case OSSL_OP_DIGEST:
  493. return deflt_digests;
  494. case OSSL_OP_CIPHER:
  495. return exported_ciphers;
  496. case OSSL_OP_MAC:
  497. return deflt_macs;
  498. case OSSL_OP_KDF:
  499. return deflt_kdfs;
  500. case OSSL_OP_RAND:
  501. return deflt_rands;
  502. case OSSL_OP_KEYMGMT:
  503. return deflt_keymgmt;
  504. case OSSL_OP_KEYEXCH:
  505. return deflt_keyexch;
  506. case OSSL_OP_SIGNATURE:
  507. return deflt_signature;
  508. case OSSL_OP_ASYM_CIPHER:
  509. return deflt_asym_cipher;
  510. case OSSL_OP_KEM:
  511. return deflt_asym_kem;
  512. case OSSL_OP_ENCODER:
  513. return deflt_encoder;
  514. case OSSL_OP_DECODER:
  515. return deflt_decoder;
  516. case OSSL_OP_STORE:
  517. return deflt_store;
  518. }
  519. return NULL;
  520. }
  521. static void deflt_teardown(void *provctx)
  522. {
  523. BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx));
  524. ossl_prov_ctx_free(provctx);
  525. }
  526. /* Functions we provide to the core */
  527. static const OSSL_DISPATCH deflt_dispatch_table[] = {
  528. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))deflt_teardown },
  529. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))deflt_gettable_params },
  530. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))deflt_get_params },
  531. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))deflt_query },
  532. { OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
  533. (void (*)(void))ossl_prov_get_capabilities },
  534. OSSL_DISPATCH_END
  535. };
  536. OSSL_provider_init_fn ossl_default_provider_init;
  537. int ossl_default_provider_init(const OSSL_CORE_HANDLE *handle,
  538. const OSSL_DISPATCH *in,
  539. const OSSL_DISPATCH **out,
  540. void **provctx)
  541. {
  542. OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
  543. BIO_METHOD *corebiometh;
  544. if (!ossl_prov_bio_from_dispatch(in)
  545. || !ossl_prov_seeding_from_dispatch(in))
  546. return 0;
  547. for (; in->function_id != 0; in++) {
  548. switch (in->function_id) {
  549. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  550. c_gettable_params = OSSL_FUNC_core_gettable_params(in);
  551. break;
  552. case OSSL_FUNC_CORE_GET_PARAMS:
  553. c_get_params = OSSL_FUNC_core_get_params(in);
  554. break;
  555. case OSSL_FUNC_CORE_GET_LIBCTX:
  556. c_get_libctx = OSSL_FUNC_core_get_libctx(in);
  557. break;
  558. default:
  559. /* Just ignore anything we don't understand */
  560. break;
  561. }
  562. }
  563. if (c_get_libctx == NULL)
  564. return 0;
  565. /*
  566. * We want to make sure that all calls from this provider that requires
  567. * a library context use the same context as the one used to call our
  568. * functions. We do that by passing it along in the provider context.
  569. *
  570. * This only works for built-in providers. Most providers should
  571. * create their own library context.
  572. */
  573. if ((*provctx = ossl_prov_ctx_new()) == NULL
  574. || (corebiometh = ossl_bio_prov_init_bio_method()) == NULL) {
  575. ossl_prov_ctx_free(*provctx);
  576. *provctx = NULL;
  577. return 0;
  578. }
  579. ossl_prov_ctx_set0_libctx(*provctx,
  580. (OSSL_LIB_CTX *)c_get_libctx(handle));
  581. ossl_prov_ctx_set0_handle(*provctx, handle);
  582. ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh);
  583. *out = deflt_dispatch_table;
  584. ossl_prov_cache_exported_algorithms(deflt_ciphers, exported_ciphers);
  585. return 1;
  586. }