Add EVP_KDF-X942 to the fips module
[openssl.git] / providers / defltprov.c
1 /*
2  * Copyright 2019-2020 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
10 #include <string.h>
11 #include <stdio.h>
12 #include <openssl/opensslconf.h>
13 #include <openssl/core.h>
14 #include <openssl/core_dispatch.h>
15 #include <openssl/core_names.h>
16 #include <openssl/params.h>
17 #include "prov/bio.h"
18 #include "prov/provider_ctx.h"
19 #include "prov/providercommon.h"
20 #include "prov/implementations.h"
21 #include "prov/provider_util.h"
22 #include "prov/seeding.h"
23 #include "internal/nelem.h"
24
25 /*
26  * Forward declarations to ensure that interface functions are correctly
27  * defined.
28  */
29 static OSSL_FUNC_provider_gettable_params_fn deflt_gettable_params;
30 static OSSL_FUNC_provider_get_params_fn deflt_get_params;
31 static OSSL_FUNC_provider_query_operation_fn deflt_query;
32
33 #define ALGC(NAMES, FUNC, CHECK) { { NAMES, "provider=default", FUNC }, CHECK }
34 #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
35
36 /* Functions provided by the core */
37 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
38 static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
39
40 /* Parameters we provide to the core */
41 static const OSSL_PARAM deflt_param_types[] = {
42     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
43     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
44     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
45     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
46     OSSL_PARAM_END
47 };
48
49 static const OSSL_PARAM *deflt_gettable_params(void *provctx)
50 {
51     return deflt_param_types;
52 }
53
54 static int deflt_get_params(void *provctx, OSSL_PARAM params[])
55 {
56     OSSL_PARAM *p;
57
58     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
59     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Default Provider"))
60         return 0;
61     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
62     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
63         return 0;
64     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
65     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
66         return 0;
67     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
68     if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
69         return 0;
70     return 1;
71 }
72
73 /*
74  * For the algorithm names, we use the following formula for our primary
75  * names:
76  *
77  *     ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
78  *
79  *     VERSION is only present if there are multiple versions of
80  *     an alg (MD2, MD4, MD5).  It may be omitted if there is only
81  *     one version (if a subsequent version is released in the future,
82  *     we can always change the canonical name, and add the old name
83  *     as an alias).
84  *
85  *     SUBNAME may be present where we are combining multiple
86  *     algorithms together, e.g. MD5-SHA1.
87  *
88  *     SIZE is only present if multiple versions of an algorithm exist
89  *     with different sizes (e.g. AES-128-CBC, AES-256-CBC)
90  *
91  *     MODE is only present where applicable.
92  *
93  * We add diverse other names where applicable, such as the names that
94  * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
95  * we have used historically.
96  *
97  * Algorithm names are case insensitive, but we use all caps in our "canonical"
98  * names for consistency.
99  */
100 static const OSSL_ALGORITHM deflt_digests[] = {
101     /* Our primary name:NIST name[:our older names] */
102     { "SHA1:SHA-1:SSL3-SHA1", "provider=default", ossl_sha1_functions },
103     { "SHA2-224:SHA-224:SHA224", "provider=default", ossl_sha224_functions },
104     { "SHA2-256:SHA-256:SHA256", "provider=default", ossl_sha256_functions },
105     { "SHA2-384:SHA-384:SHA384", "provider=default", ossl_sha384_functions },
106     { "SHA2-512:SHA-512:SHA512", "provider=default", ossl_sha512_functions },
107     { "SHA2-512/224:SHA-512/224:SHA512-224", "provider=default",
108       ossl_sha512_224_functions },
109     { "SHA2-512/256:SHA-512/256:SHA512-256", "provider=default",
110       ossl_sha512_256_functions },
111
112     /* We agree with NIST here, so one name only */
113     { "SHA3-224", "provider=default", ossl_sha3_224_functions },
114     { "SHA3-256", "provider=default", ossl_sha3_256_functions },
115     { "SHA3-384", "provider=default", ossl_sha3_384_functions },
116     { "SHA3-512", "provider=default", ossl_sha3_512_functions },
117
118     /*
119      * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for
120      * the KMAC-128 and KMAC-256.
121      */
122     { "KECCAK-KMAC-128:KECCAK-KMAC128", "provider=default",
123       ossl_keccak_kmac_128_functions },
124     { "KECCAK-KMAC-256:KECCAK-KMAC256", "provider=default",
125       ossl_keccak_kmac_256_functions },
126
127     /* Our primary name:NIST name */
128     { "SHAKE-128:SHAKE128", "provider=default", ossl_shake_128_functions },
129     { "SHAKE-256:SHAKE256", "provider=default", ossl_shake_256_functions },
130
131 #ifndef OPENSSL_NO_BLAKE2
132     /*
133      * https://blake2.net/ doesn't specify size variants,
134      * but mentions that Bouncy Castle uses the names
135      * BLAKE2b-160, BLAKE2b-256, BLAKE2b-384, and BLAKE2b-512
136      * If we assume that "2b" and "2s" are versions, that pattern
137      * fits with ours.  We also add our historical names.
138      */
139     { "BLAKE2S-256:BLAKE2s256", "provider=default", ossl_blake2s256_functions },
140     { "BLAKE2B-512:BLAKE2b512", "provider=default", ossl_blake2b512_functions },
141 #endif /* OPENSSL_NO_BLAKE2 */
142
143 #ifndef OPENSSL_NO_SM3
144     { "SM3", "provider=default", ossl_sm3_functions },
145 #endif /* OPENSSL_NO_SM3 */
146
147 #ifndef OPENSSL_NO_MD5
148     { "MD5:SSL3-MD5", "provider=default", ossl_md5_functions },
149     { "MD5-SHA1", "provider=default", ossl_md5_sha1_functions },
150 #endif /* OPENSSL_NO_MD5 */
151
152     { NULL, NULL, NULL }
153 };
154
155 static const OSSL_ALGORITHM_CAPABLE deflt_ciphers[] = {
156     ALG("NULL", ossl_null_functions),
157     ALG("AES-256-ECB", ossl_aes256ecb_functions),
158     ALG("AES-192-ECB", ossl_aes192ecb_functions),
159     ALG("AES-128-ECB", ossl_aes128ecb_functions),
160     ALG("AES-256-CBC:AES256", ossl_aes256cbc_functions),
161     ALG("AES-192-CBC:AES192", ossl_aes192cbc_functions),
162     ALG("AES-128-CBC:AES128", ossl_aes128cbc_functions),
163     ALG("AES-128-CBC-CTS", ossl_aes128cbc_cts_functions),
164     ALG("AES-192-CBC-CTS", ossl_aes192cbc_cts_functions),
165     ALG("AES-256-CBC-CTS", ossl_aes256cbc_cts_functions),
166     ALG("AES-256-OFB", ossl_aes256ofb_functions),
167     ALG("AES-192-OFB", ossl_aes192ofb_functions),
168     ALG("AES-128-OFB", ossl_aes128ofb_functions),
169     ALG("AES-256-CFB", ossl_aes256cfb_functions),
170     ALG("AES-192-CFB", ossl_aes192cfb_functions),
171     ALG("AES-128-CFB", ossl_aes128cfb_functions),
172     ALG("AES-256-CFB1", ossl_aes256cfb1_functions),
173     ALG("AES-192-CFB1", ossl_aes192cfb1_functions),
174     ALG("AES-128-CFB1", ossl_aes128cfb1_functions),
175     ALG("AES-256-CFB8", ossl_aes256cfb8_functions),
176     ALG("AES-192-CFB8", ossl_aes192cfb8_functions),
177     ALG("AES-128-CFB8", ossl_aes128cfb8_functions),
178     ALG("AES-256-CTR", ossl_aes256ctr_functions),
179     ALG("AES-192-CTR", ossl_aes192ctr_functions),
180     ALG("AES-128-CTR", ossl_aes128ctr_functions),
181     ALG("AES-256-XTS", ossl_aes256xts_functions),
182     ALG("AES-128-XTS", ossl_aes128xts_functions),
183 #ifndef OPENSSL_NO_OCB
184     ALG("AES-256-OCB", ossl_aes256ocb_functions),
185     ALG("AES-192-OCB", ossl_aes192ocb_functions),
186     ALG("AES-128-OCB", ossl_aes128ocb_functions),
187 #endif /* OPENSSL_NO_OCB */
188 #ifndef OPENSSL_NO_SIV
189     ALG("AES-128-SIV", ossl_aes128siv_functions),
190     ALG("AES-192-SIV", ossl_aes192siv_functions),
191     ALG("AES-256-SIV", ossl_aes256siv_functions),
192 #endif /* OPENSSL_NO_SIV */
193     ALG("AES-256-GCM:id-aes256-GCM", ossl_aes256gcm_functions),
194     ALG("AES-192-GCM:id-aes192-GCM", ossl_aes192gcm_functions),
195     ALG("AES-128-GCM:id-aes128-GCM", ossl_aes128gcm_functions),
196     ALG("AES-256-CCM:id-aes256-CCM", ossl_aes256ccm_functions),
197     ALG("AES-192-CCM:id-aes192-CCM", ossl_aes192ccm_functions),
198     ALG("AES-128-CCM:id-aes128-CCM", ossl_aes128ccm_functions),
199     ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", ossl_aes256wrap_functions),
200     ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", ossl_aes192wrap_functions),
201     ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", ossl_aes128wrap_functions),
202     ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
203         ossl_aes256wrappad_functions),
204     ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
205         ossl_aes192wrappad_functions),
206     ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
207         ossl_aes128wrappad_functions),
208     ALG("AES-256-WRAP-INV:AES256-WRAP-INV", ossl_aes256wrapinv_functions),
209     ALG("AES-192-WRAP-INV:AES192-WRAP-INV", ossl_aes192wrapinv_functions),
210     ALG("AES-128-WRAP-INV:AES128-WRAP-INV", ossl_aes128wrapinv_functions),
211     ALG("AES-256-WRAP-PAD-INV:AES256-WRAP-PAD-INV",
212         ossl_aes256wrappadinv_functions),
213     ALG("AES-192-WRAP-PAD-INV:AES192-WRAP-PAD-INV",
214         ossl_aes192wrappadinv_functions),
215     ALG("AES-128-WRAP-PAD-INV:AES128-WRAP-PAD-INV",
216         ossl_aes128wrappadinv_functions),
217     ALGC("AES-128-CBC-HMAC-SHA1", ossl_aes128cbc_hmac_sha1_functions,
218          ossl_cipher_capable_aes_cbc_hmac_sha1),
219     ALGC("AES-256-CBC-HMAC-SHA1", ossl_aes256cbc_hmac_sha1_functions,
220          ossl_cipher_capable_aes_cbc_hmac_sha1),
221     ALGC("AES-128-CBC-HMAC-SHA256", ossl_aes128cbc_hmac_sha256_functions,
222         ossl_cipher_capable_aes_cbc_hmac_sha256),
223     ALGC("AES-256-CBC-HMAC-SHA256", ossl_aes256cbc_hmac_sha256_functions,
224          ossl_cipher_capable_aes_cbc_hmac_sha256),
225 #ifndef OPENSSL_NO_ARIA
226     ALG("ARIA-256-GCM", ossl_aria256gcm_functions),
227     ALG("ARIA-192-GCM", ossl_aria192gcm_functions),
228     ALG("ARIA-128-GCM", ossl_aria128gcm_functions),
229     ALG("ARIA-256-CCM", ossl_aria256ccm_functions),
230     ALG("ARIA-192-CCM", ossl_aria192ccm_functions),
231     ALG("ARIA-128-CCM", ossl_aria128ccm_functions),
232     ALG("ARIA-256-ECB", ossl_aria256ecb_functions),
233     ALG("ARIA-192-ECB", ossl_aria192ecb_functions),
234     ALG("ARIA-128-ECB", ossl_aria128ecb_functions),
235     ALG("ARIA-256-CBC:ARIA256", ossl_aria256cbc_functions),
236     ALG("ARIA-192-CBC:ARIA192", ossl_aria192cbc_functions),
237     ALG("ARIA-128-CBC:ARIA128", ossl_aria128cbc_functions),
238     ALG("ARIA-256-OFB", ossl_aria256ofb_functions),
239     ALG("ARIA-192-OFB", ossl_aria192ofb_functions),
240     ALG("ARIA-128-OFB", ossl_aria128ofb_functions),
241     ALG("ARIA-256-CFB", ossl_aria256cfb_functions),
242     ALG("ARIA-192-CFB", ossl_aria192cfb_functions),
243     ALG("ARIA-128-CFB", ossl_aria128cfb_functions),
244     ALG("ARIA-256-CFB1", ossl_aria256cfb1_functions),
245     ALG("ARIA-192-CFB1", ossl_aria192cfb1_functions),
246     ALG("ARIA-128-CFB1", ossl_aria128cfb1_functions),
247     ALG("ARIA-256-CFB8", ossl_aria256cfb8_functions),
248     ALG("ARIA-192-CFB8", ossl_aria192cfb8_functions),
249     ALG("ARIA-128-CFB8", ossl_aria128cfb8_functions),
250     ALG("ARIA-256-CTR", ossl_aria256ctr_functions),
251     ALG("ARIA-192-CTR", ossl_aria192ctr_functions),
252     ALG("ARIA-128-CTR", ossl_aria128ctr_functions),
253 #endif /* OPENSSL_NO_ARIA */
254 #ifndef OPENSSL_NO_CAMELLIA
255     ALG("CAMELLIA-256-ECB", ossl_camellia256ecb_functions),
256     ALG("CAMELLIA-192-ECB", ossl_camellia192ecb_functions),
257     ALG("CAMELLIA-128-ECB", ossl_camellia128ecb_functions),
258     ALG("CAMELLIA-256-CBC:CAMELLIA256", ossl_camellia256cbc_functions),
259     ALG("CAMELLIA-192-CBC:CAMELLIA192", ossl_camellia192cbc_functions),
260     ALG("CAMELLIA-128-CBC:CAMELLIA128", ossl_camellia128cbc_functions),
261     ALG("CAMELLIA-256-OFB", ossl_camellia256ofb_functions),
262     ALG("CAMELLIA-192-OFB", ossl_camellia192ofb_functions),
263     ALG("CAMELLIA-128-OFB", ossl_camellia128ofb_functions),
264     ALG("CAMELLIA-256-CFB", ossl_camellia256cfb_functions),
265     ALG("CAMELLIA-192-CFB", ossl_camellia192cfb_functions),
266     ALG("CAMELLIA-128-CFB", ossl_camellia128cfb_functions),
267     ALG("CAMELLIA-256-CFB1", ossl_camellia256cfb1_functions),
268     ALG("CAMELLIA-192-CFB1", ossl_camellia192cfb1_functions),
269     ALG("CAMELLIA-128-CFB1", ossl_camellia128cfb1_functions),
270     ALG("CAMELLIA-256-CFB8", ossl_camellia256cfb8_functions),
271     ALG("CAMELLIA-192-CFB8", ossl_camellia192cfb8_functions),
272     ALG("CAMELLIA-128-CFB8", ossl_camellia128cfb8_functions),
273     ALG("CAMELLIA-256-CTR", ossl_camellia256ctr_functions),
274     ALG("CAMELLIA-192-CTR", ossl_camellia192ctr_functions),
275     ALG("CAMELLIA-128-CTR", ossl_camellia128ctr_functions),
276 #endif /* OPENSSL_NO_CAMELLIA */
277 #ifndef OPENSSL_NO_DES
278     ALG("DES-EDE3-ECB:DES-EDE3", ossl_tdes_ede3_ecb_functions),
279     ALG("DES-EDE3-CBC:DES3", ossl_tdes_ede3_cbc_functions),
280     ALG("DES-EDE3-OFB", ossl_tdes_ede3_ofb_functions),
281     ALG("DES-EDE3-CFB", ossl_tdes_ede3_cfb_functions),
282     ALG("DES-EDE3-CFB8", ossl_tdes_ede3_cfb8_functions),
283     ALG("DES-EDE3-CFB1", ossl_tdes_ede3_cfb1_functions),
284     ALG("DES3-WRAP:id-smime-alg-CMS3DESwrap", ossl_tdes_wrap_cbc_functions),
285     ALG("DES-EDE-ECB:DES-EDE", ossl_tdes_ede2_ecb_functions),
286     ALG("DES-EDE-CBC", ossl_tdes_ede2_cbc_functions),
287     ALG("DES-EDE-OFB", ossl_tdes_ede2_ofb_functions),
288     ALG("DES-EDE-CFB", ossl_tdes_ede2_cfb_functions),
289 #endif /* OPENSSL_NO_DES */
290 #ifndef OPENSSL_NO_SM4
291     ALG("SM4-ECB", ossl_sm4128ecb_functions),
292     ALG("SM4-CBC:SM4", ossl_sm4128cbc_functions),
293     ALG("SM4-CTR", ossl_sm4128ctr_functions),
294     ALG("SM4-OFB:SM4-OFB128", ossl_sm4128ofb128_functions),
295     ALG("SM4-CFB:SM4-CFB128", ossl_sm4128cfb128_functions),
296 #endif /* OPENSSL_NO_SM4 */
297 #ifndef OPENSSL_NO_CHACHA
298     ALG("ChaCha20", ossl_chacha20_functions),
299 # ifndef OPENSSL_NO_POLY1305
300     ALG("ChaCha20-Poly1305", ossl_chacha20_ossl_poly1305_functions),
301 # endif /* OPENSSL_NO_POLY1305 */
302 #endif /* OPENSSL_NO_CHACHA */
303     { { NULL, NULL, NULL }, NULL }
304 };
305 static OSSL_ALGORITHM exported_ciphers[OSSL_NELEM(deflt_ciphers)];
306
307 static const OSSL_ALGORITHM deflt_macs[] = {
308 #ifndef OPENSSL_NO_BLAKE2
309     { "BLAKE2BMAC", "provider=default", ossl_blake2bmac_functions },
310     { "BLAKE2SMAC", "provider=default", ossl_blake2smac_functions },
311 #endif
312 #ifndef OPENSSL_NO_CMAC
313     { "CMAC", "provider=default", ossl_cmac_functions },
314 #endif
315     { "GMAC", "provider=default", ossl_gmac_functions },
316     { "HMAC", "provider=default", ossl_hmac_functions },
317     { "KMAC-128:KMAC128", "provider=default", ossl_kmac128_functions },
318     { "KMAC-256:KMAC256", "provider=default", ossl_kmac256_functions },
319 #ifndef OPENSSL_NO_SIPHASH
320     { "SIPHASH", "provider=default", ossl_siphash_functions },
321 #endif
322 #ifndef OPENSSL_NO_POLY1305
323     { "POLY1305", "provider=default", ossl_poly1305_functions },
324 #endif
325     { NULL, NULL, NULL }
326 };
327
328 static const OSSL_ALGORITHM deflt_kdfs[] = {
329     { "HKDF", "provider=default", ossl_kdf_hkdf_functions },
330     { "SSKDF", "provider=default", ossl_kdf_sskdf_functions },
331     { "PBKDF2", "provider=default", ossl_kdf_pbkdf2_functions },
332     { "PKCS12KDF", "provider=default", ossl_kdf_pkcs12_functions },
333     { "SSHKDF", "provider=default", ossl_kdf_sshkdf_functions },
334     { "X963KDF:X942KDF-CONCAT", "provider=default", ossl_kdf_x963_kdf_functions },
335     { "TLS1-PRF", "provider=default", ossl_kdf_tls1_prf_functions },
336     { "KBKDF", "provider=default", ossl_kdf_kbkdf_functions },
337     { "X942KDF-ASN1:X942KDF", "provider=default", ossl_kdf_x942_kdf_functions },
338 #ifndef OPENSSL_NO_SCRYPT
339     { "SCRYPT:id-scrypt", "provider=default", ossl_kdf_scrypt_functions },
340 #endif
341     { "KRB5KDF", "provider=default", ossl_kdf_krb5kdf_functions },
342     { NULL, NULL, NULL }
343 };
344
345 static const OSSL_ALGORITHM deflt_keyexch[] = {
346 #ifndef OPENSSL_NO_DH
347     { "DH:dhKeyAgreement", "provider=default", ossl_dh_keyexch_functions },
348 #endif
349 #ifndef OPENSSL_NO_EC
350     { "ECDH", "provider=default", ecossl_dh_keyexch_functions },
351     { "X25519", "provider=default", ossl_x25519_keyexch_functions },
352     { "X448", "provider=default", ossl_x448_keyexch_functions },
353 #endif
354     { "TLS1-PRF", "provider=default", ossl_kdf_tls1_prf_keyexch_functions },
355     { "HKDF", "provider=default", ossl_kdf_hkdf_keyexch_functions },
356     { "SCRYPT:id-scrypt", "provider=default",
357       ossl_kdf_scrypt_keyexch_functions },
358     { NULL, NULL, NULL }
359 };
360
361 static const OSSL_ALGORITHM deflt_rands[] = {
362     { "CTR-DRBG", "provider=default", ossl_drbg_ctr_functions },
363     { "HASH-DRBG", "provider=default", ossl_drbg_hash_functions },
364     { "HMAC-DRBG", "provider=default", ossl_drbg_ossl_hmac_functions },
365     { "TEST-RAND", "provider=default", ossl_test_rng_functions },
366     { NULL, NULL, NULL }
367 };
368
369 static const OSSL_ALGORITHM deflt_signature[] = {
370 #ifndef OPENSSL_NO_DSA
371     { "DSA:dsaEncryption", "provider=default", ossl_dsa_signature_functions },
372 #endif
373     { "RSA:rsaEncryption", "provider=default", ossl_rsa_signature_functions },
374 #ifndef OPENSSL_NO_EC
375     { "ED25519", "provider=default", ossl_ed25519_signature_functions },
376     { "ED448", "provider=default", ossl_ed448_signature_functions },
377     { "ECDSA", "provider=default", ecossl_dsa_signature_functions },
378 # ifndef OPENSSL_NO_SM2
379     { "SM2", "provider=default", sm2_signature_functions },
380 # endif
381 #endif
382     { "HMAC", "provider=default", ossl_mac_legacy_hmac_signature_functions },
383     { "SIPHASH", "provider=default",
384       ossl_mac_legacy_siphash_signature_functions },
385 #ifndef OPENSSL_NO_POLY1305
386     { "POLY1305", "provider=default",
387       ossl_mac_legacy_poly1305_signature_functions },
388 #endif
389 #ifndef OPENSSL_NO_CMAC
390     { "CMAC", "provider=default", ossl_mac_legacy_cmac_signature_functions },
391 #endif
392     { NULL, NULL, NULL }
393 };
394
395 static const OSSL_ALGORITHM deflt_asym_cipher[] = {
396     { "RSA:rsaEncryption", "provider=default", ossl_rsa_asym_cipher_functions },
397 #ifndef OPENSSL_NO_SM2
398     { "SM2", "provider=default", sm2_asym_cipher_functions },
399 #endif
400     { NULL, NULL, NULL }
401 };
402
403 static const OSSL_ALGORITHM deflt_asym_kem[] = {
404     { "RSA", "provider=default", ossl_rsa_asym_kem_functions },
405     { NULL, NULL, NULL }
406 };
407
408 static const OSSL_ALGORITHM deflt_keymgmt[] = {
409 #ifndef OPENSSL_NO_DH
410     { "DH:dhKeyAgreement", "provider=default", ossl_dh_keymgmt_functions },
411     { "DHX:X9.42 DH:dhpublicnumber", "provider=default",
412       ossl_dhx_keymgmt_functions },
413 #endif
414 #ifndef OPENSSL_NO_DSA
415     { "DSA:dsaEncryption", "provider=default", ossl_dsa_keymgmt_functions },
416 #endif
417     { "RSA:rsaEncryption", "provider=default", ossl_rsa_keymgmt_functions },
418     { "RSA-PSS:RSASSA-PSS", "provider=default", ossl_rsapss_keymgmt_functions },
419 #ifndef OPENSSL_NO_EC
420     { "EC:id-ecPublicKey", "provider=default", ossl_ec_keymgmt_functions },
421     { "X25519", "provider=default", ossl_x25519_keymgmt_functions },
422     { "X448", "provider=default", ossl_x448_keymgmt_functions },
423     { "ED25519", "provider=default", ossl_ed25519_keymgmt_functions },
424     { "ED448", "provider=default", ossl_ed448_keymgmt_functions },
425 #endif
426     { "TLS1-PRF", "provider=default", ossl_kdf_keymgmt_functions },
427     { "HKDF", "provider=default", ossl_kdf_keymgmt_functions },
428     { "SCRYPT:id-scrypt", "provider=default", ossl_kdf_keymgmt_functions },
429     { "HMAC", "provider=default", ossl_mac_legacy_keymgmt_functions },
430     { "SIPHASH", "provider=default", ossl_mac_legacy_keymgmt_functions },
431 #ifndef OPENSSL_NO_POLY1305
432     { "POLY1305", "provider=default", ossl_mac_legacy_keymgmt_functions },
433 #endif
434 #ifndef OPENSSL_NO_CMAC
435     { "CMAC", "provider=default", ossl_cossl_mac_legacy_keymgmt_functions },
436 #endif
437 #ifndef OPENSSL_NO_SM2
438     { "SM2", "provider=default", sm2_keymgmt_functions },
439 #endif
440     { NULL, NULL, NULL }
441 };
442
443 static const OSSL_ALGORITHM deflt_encoder[] = {
444 #define ENCODER_PROVIDER "default"
445 #include "encoders.inc"
446     { NULL, NULL, NULL }
447 #undef ENCODER_PROVIDER
448 };
449
450 static const OSSL_ALGORITHM deflt_decoder[] = {
451 #define DECODER_PROVIDER "default"
452 #include "decoders.inc"
453     { NULL, NULL, NULL }
454 #undef DECODER_PROVIDER
455 };
456
457 static const OSSL_ALGORITHM deflt_store[] = {
458 #define STORE(name, _fips, func_table)                           \
459     { name, "provider=default,fips=" _fips, (func_table) },
460
461 #include "stores.inc"
462     { NULL, NULL, NULL }
463 #undef STORE
464 };
465
466 static const OSSL_ALGORITHM *deflt_query(void *provctx, int operation_id,
467                                          int *no_cache)
468 {
469     *no_cache = 0;
470     switch (operation_id) {
471     case OSSL_OP_DIGEST:
472         return deflt_digests;
473     case OSSL_OP_CIPHER:
474         ossl_prov_cache_exported_algorithms(deflt_ciphers, exported_ciphers);
475         return exported_ciphers;
476     case OSSL_OP_MAC:
477         return deflt_macs;
478     case OSSL_OP_KDF:
479         return deflt_kdfs;
480     case OSSL_OP_RAND:
481         return deflt_rands;
482     case OSSL_OP_KEYMGMT:
483         return deflt_keymgmt;
484     case OSSL_OP_KEYEXCH:
485         return deflt_keyexch;
486     case OSSL_OP_SIGNATURE:
487         return deflt_signature;
488     case OSSL_OP_ASYM_CIPHER:
489         return deflt_asym_cipher;
490     case OSSL_OP_KEM:
491         return deflt_asym_kem;
492     case OSSL_OP_ENCODER:
493         return deflt_encoder;
494     case OSSL_OP_DECODER:
495         return deflt_decoder;
496     case OSSL_OP_STORE:
497         return deflt_store;
498     }
499     return NULL;
500 }
501
502
503 static void deflt_teardown(void *provctx)
504 {
505     BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx));
506     ossl_prov_ctx_free(provctx);
507 }
508
509 /* Functions we provide to the core */
510 static const OSSL_DISPATCH deflt_dispatch_table[] = {
511     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))deflt_teardown },
512     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))deflt_gettable_params },
513     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))deflt_get_params },
514     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))deflt_query },
515     { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))provider_get_capabilities },
516     { 0, NULL }
517 };
518
519 OSSL_provider_init_fn ossl_default_provider_init;
520
521 int ossl_default_provider_init(const OSSL_CORE_HANDLE *handle,
522                                const OSSL_DISPATCH *in,
523                                const OSSL_DISPATCH **out,
524                                void **provctx)
525 {
526     OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
527     BIO_METHOD *corebiometh;
528
529     if (!ossl_prov_bio_from_dispatch(in)
530             || !ossl_prov_seeding_from_dispatch(in))
531         return 0;
532     for (; in->function_id != 0; in++) {
533         switch (in->function_id) {
534         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
535             c_gettable_params = OSSL_FUNC_core_gettable_params(in);
536             break;
537         case OSSL_FUNC_CORE_GET_PARAMS:
538             c_get_params = OSSL_FUNC_core_get_params(in);
539             break;
540         case OSSL_FUNC_CORE_GET_LIBCTX:
541             c_get_libctx = OSSL_FUNC_core_get_libctx(in);
542             break;
543         default:
544             /* Just ignore anything we don't understand */
545             break;
546         }
547     }
548
549     if (c_get_libctx == NULL)
550         return 0;
551
552     /*
553      * We want to make sure that all calls from this provider that requires
554      * a library context use the same context as the one used to call our
555      * functions.  We do that by passing it along in the provider context.
556      *
557      * This only works for built-in providers.  Most providers should
558      * create their own library context.
559      */
560     if ((*provctx = ossl_prov_ctx_new()) == NULL
561             || (corebiometh = bio_prov_init_bio_method()) == NULL) {
562         ossl_prov_ctx_free(*provctx);
563         *provctx = NULL;
564         return 0;
565     }
566     ossl_prov_ctx_set0_libctx(*provctx,
567                                        (OSSL_LIB_CTX *)c_get_libctx(handle));
568     ossl_prov_ctx_set0_handle(*provctx, handle);
569     ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh);
570
571     *out = deflt_dispatch_table;
572
573     return 1;
574 }