[KDF] Add KBKDF implementation for counter-mode HMAC
[openssl.git] / providers / default / defltprov.c
1 /*
2  * Copyright 2019 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_numbers.h>
15 #include <openssl/core_names.h>
16 #include <openssl/params.h>
17 #include "internal/provider_algs.h"
18
19 /* Functions provided by the core */
20 static OSSL_core_gettable_params_fn *c_gettable_params = NULL;
21 static OSSL_core_get_params_fn *c_get_params = NULL;
22
23 /* Parameters we provide to the core */
24 static const OSSL_PARAM deflt_param_types[] = {
25     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
26     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
27     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
28     OSSL_PARAM_END
29 };
30
31 static const OSSL_PARAM *deflt_gettable_params(const OSSL_PROVIDER *prov)
32 {
33     return deflt_param_types;
34 }
35
36 static int deflt_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
37 {
38     OSSL_PARAM *p;
39
40     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
41     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Default Provider"))
42         return 0;
43     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
44     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
45         return 0;
46     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
47     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
48         return 0;
49
50     return 1;
51 }
52
53 static const OSSL_ALGORITHM deflt_digests[] = {
54     { "SHA1", "default=yes", sha1_functions },
55
56     { "SHA224", "default=yes", sha224_functions },
57     { "SHA256", "default=yes", sha256_functions },
58     { "SHA384", "default=yes", sha384_functions },
59     { "SHA512", "default=yes", sha512_functions },
60     { "SHA512-224", "default=yes", sha512_224_functions },
61     { "SHA512-256", "default=yes", sha512_256_functions },
62
63     { "SHA3-224", "default=yes", sha3_224_functions },
64     { "SHA3-256", "default=yes", sha3_256_functions },
65     { "SHA3-384", "default=yes", sha3_384_functions },
66     { "SHA3-512", "default=yes", sha3_512_functions },
67
68     /*
69      * KECCAK_KMAC128 and KECCAK_KMAC256 as hashes are mostly useful for
70      * the KMAC128 and KMAC256.
71      */
72     { "KECCAK_KMAC128", "default=yes", keccak_kmac_128_functions },
73     { "KECCAK_KMAC256", "default=yes", keccak_kmac_256_functions },
74
75     { "SHAKE128", "default=yes", shake_128_functions },
76     { "SHAKE256", "default=yes", shake_256_functions },
77
78 #ifndef OPENSSL_NO_BLAKE2
79     { "BLAKE2s256", "default=yes", blake2s256_functions },
80     { "BLAKE2b512", "default=yes", blake2b512_functions },
81 #endif /* OPENSSL_NO_BLAKE2 */
82
83 #ifndef OPENSSL_NO_SM3
84     { "SM3", "default=yes", sm3_functions },
85 #endif /* OPENSSL_NO_SM3 */
86
87 #ifndef OPENSSL_NO_MD5
88     { "MD5", "default=yes", md5_functions },
89     { "MD5-SHA1", "default=yes", md5_sha1_functions },
90 #endif /* OPENSSL_NO_MD5 */
91
92     { NULL, NULL, NULL }
93 };
94
95 static const OSSL_ALGORITHM deflt_ciphers[] = {
96     { "AES-256-ECB", "default=yes", aes256ecb_functions },
97     { "AES-192-ECB", "default=yes", aes192ecb_functions },
98     { "AES-128-ECB", "default=yes", aes128ecb_functions },
99     { "AES-256-CBC", "default=yes", aes256cbc_functions },
100     { "AES-192-CBC", "default=yes", aes192cbc_functions },
101     { "AES-128-CBC", "default=yes", aes128cbc_functions },
102     { "AES-256-OFB", "default=yes", aes256ofb_functions },
103     { "AES-192-OFB", "default=yes", aes192ofb_functions },
104     { "AES-128-OFB", "default=yes", aes128ofb_functions },
105     { "AES-256-CFB", "default=yes", aes256cfb_functions },
106     { "AES-192-CFB", "default=yes", aes192cfb_functions },
107     { "AES-128-CFB", "default=yes", aes128cfb_functions },
108     { "AES-256-CFB1", "default=yes", aes256cfb1_functions },
109     { "AES-192-CFB1", "default=yes", aes192cfb1_functions },
110     { "AES-128-CFB1", "default=yes", aes128cfb1_functions },
111     { "AES-256-CFB8", "default=yes", aes256cfb8_functions },
112     { "AES-192-CFB8", "default=yes", aes192cfb8_functions },
113     { "AES-128-CFB8", "default=yes", aes128cfb8_functions },
114     { "AES-256-CTR", "default=yes", aes256ctr_functions },
115     { "AES-192-CTR", "default=yes", aes192ctr_functions },
116     { "AES-128-CTR", "default=yes", aes128ctr_functions },
117     { "AES-256-XTS", "default=yes", aes256xts_functions },
118     { "AES-128-XTS", "default=yes", aes128xts_functions },
119 #ifndef OPENSSL_NO_OCB
120     { "AES-256-OCB", "default=yes", aes256ocb_functions },
121     { "AES-192-OCB", "default=yes", aes192ocb_functions },
122     { "AES-128-OCB", "default=yes", aes128ocb_functions },
123 #endif /* OPENSSL_NO_OCB */
124 /* TODO(3.0) Add aliases when they are supported */
125     { "id-aes256-GCM", "default=yes", aes256gcm_functions },
126     { "id-aes192-GCM", "default=yes", aes192gcm_functions },
127     { "id-aes128-GCM", "default=yes", aes128gcm_functions },
128     { "id-aes256-CCM", "default=yes", aes256ccm_functions },
129     { "id-aes192-CCM", "default=yes", aes192ccm_functions },
130     { "id-aes128-CCM", "default=yes", aes128ccm_functions },
131     { "id-aes256-wrap", "default=yes", aes256wrap_functions },
132     { "id-aes192-wrap", "default=yes", aes192wrap_functions },
133     { "id-aes128-wrap", "default=yes", aes128wrap_functions },
134     { "id-aes256-wrap-pad", "default=yes", aes256wrappad_functions },
135     { "id-aes192-wrap-pad", "default=yes", aes192wrappad_functions },
136     { "id-aes128-wrap-pad", "default=yes", aes128wrappad_functions },
137 #ifndef OPENSSL_NO_ARIA
138     { "ARIA-256-GCM", "default=yes", aria256gcm_functions },
139     { "ARIA-192-GCM", "default=yes", aria192gcm_functions },
140     { "ARIA-128-GCM", "default=yes", aria128gcm_functions },
141     { "ARIA-256-CCM", "default=yes", aria256ccm_functions },
142     { "ARIA-192-CCM", "default=yes", aria192ccm_functions },
143     { "ARIA-128-CCM", "default=yes", aria128ccm_functions },
144     { "ARIA-256-ECB", "default=yes", aria256ecb_functions },
145     { "ARIA-192-ECB", "default=yes", aria192ecb_functions },
146     { "ARIA-128-ECB", "default=yes", aria128ecb_functions },
147     { "ARIA-256-CBC", "default=yes", aria256cbc_functions },
148     { "ARIA-192-CBC", "default=yes", aria192cbc_functions },
149     { "ARIA-128-CBC", "default=yes", aria128cbc_functions },
150     { "ARIA-256-OFB", "default=yes", aria256ofb_functions },
151     { "ARIA-192-OFB", "default=yes", aria192ofb_functions },
152     { "ARIA-128-OFB", "default=yes", aria128ofb_functions },
153     { "ARIA-256-CFB", "default=yes", aria256cfb_functions },
154     { "ARIA-192-CFB", "default=yes", aria192cfb_functions },
155     { "ARIA-128-CFB", "default=yes", aria128cfb_functions },
156     { "ARIA-256-CFB1", "default=yes", aria256cfb1_functions },
157     { "ARIA-192-CFB1", "default=yes", aria192cfb1_functions },
158     { "ARIA-128-CFB1", "default=yes", aria128cfb1_functions },
159     { "ARIA-256-CFB8", "default=yes", aria256cfb8_functions },
160     { "ARIA-192-CFB8", "default=yes", aria192cfb8_functions },
161     { "ARIA-128-CFB8", "default=yes", aria128cfb8_functions },
162     { "ARIA-256-CTR", "default=yes", aria256ctr_functions },
163     { "ARIA-192-CTR", "default=yes", aria192ctr_functions },
164     { "ARIA-128-CTR", "default=yes", aria128ctr_functions },
165 #endif /* OPENSSL_NO_ARIA */
166 #ifndef OPENSSL_NO_CAMELLIA
167     { "CAMELLIA-256-ECB", "default=yes", camellia256ecb_functions },
168     { "CAMELLIA-192-ECB", "default=yes", camellia192ecb_functions },
169     { "CAMELLIA-128-ECB", "default=yes", camellia128ecb_functions },
170     { "CAMELLIA-256-CBC", "default=yes", camellia256cbc_functions },
171     { "CAMELLIA-192-CBC", "default=yes", camellia192cbc_functions },
172     { "CAMELLIA-128-CBC", "default=yes", camellia128cbc_functions },
173     { "CAMELLIA-256-OFB", "default=yes", camellia256ofb_functions },
174     { "CAMELLIA-192-OFB", "default=yes", camellia192ofb_functions },
175     { "CAMELLIA-128-OFB", "default=yes", camellia128ofb_functions },
176     { "CAMELLIA-256-CFB", "default=yes", camellia256cfb_functions },
177     { "CAMELLIA-192-CFB", "default=yes", camellia192cfb_functions },
178     { "CAMELLIA-128-CFB", "default=yes", camellia128cfb_functions },
179     { "CAMELLIA-256-CFB1", "default=yes", camellia256cfb1_functions },
180     { "CAMELLIA-192-CFB1", "default=yes", camellia192cfb1_functions },
181     { "CAMELLIA-128-CFB1", "default=yes", camellia128cfb1_functions },
182     { "CAMELLIA-256-CFB8", "default=yes", camellia256cfb8_functions },
183     { "CAMELLIA-192-CFB8", "default=yes", camellia192cfb8_functions },
184     { "CAMELLIA-128-CFB8", "default=yes", camellia128cfb8_functions },
185     { "CAMELLIA-256-CTR", "default=yes", camellia256ctr_functions },
186     { "CAMELLIA-192-CTR", "default=yes", camellia192ctr_functions },
187     { "CAMELLIA-128-CTR", "default=yes", camellia128ctr_functions },
188 #endif /* OPENSSL_NO_CAMELLIA */
189 #ifndef OPENSSL_NO_DES
190     { "DES-EDE3", "default=yes", tdes_ede3_ecb_functions },
191     { "DES-EDE3-CBC", "default=yes", tdes_ede3_cbc_functions },
192     { "DES-EDE3-OFB", "default=yes", tdes_ede3_ofb_functions },
193     { "DES-EDE3-CFB", "default=yes", tdes_ede3_cfb_functions },
194     { "DES-EDE3-CFB8", "default=yes", tdes_ede3_cfb8_functions },
195     { "DES-EDE3-CFB1", "default=yes", tdes_ede3_cfb1_functions },
196     { "DES-EDE", "default=yes", tdes_ede2_ecb_functions },
197     { "DES-EDE-CBC", "default=yes", tdes_ede2_cbc_functions },
198     { "DES-EDE-OFB", "default=yes", tdes_ede2_ofb_functions },
199     { "DES-EDE-CFB", "default=yes", tdes_ede2_cfb_functions },
200     { "DESX-CBC", "default=yes", tdes_desx_cbc_functions },
201     { "id-smime-alg-CMS3DESwrap", "default=yes", tdes_wrap_cbc_functions },
202     { "DES-ECB", "default=yes", des_ecb_functions },
203     { "DES-CBC", "default=yes", des_cbc_functions },
204     { "DES-OFB", "default=yes", des_ofb64_functions },
205     { "DES-CFB", "default=yes", des_cfb64_functions },
206     { "DES-CFB1", "default=yes", des_cfb1_functions },
207     { "DES-CFB8", "default=yes", des_cfb8_functions },
208 #endif /* OPENSSL_NO_DES */
209 #ifndef OPENSSL_NO_BF
210     { "BF-ECB", "default=yes", blowfish128ecb_functions },
211     { "BF-CBC", "default=yes", blowfish128cbc_functions },
212     { "BF-OFB", "default=yes", blowfish64ofb64_functions },
213     { "BF-CFB", "default=yes", blowfish64cfb64_functions },
214 #endif /* OPENSSL_NO_BF */
215 #ifndef OPENSSL_NO_IDEA
216     { "IDEA-ECB", "default=yes", idea128ecb_functions },
217     { "IDEA-CBC", "default=yes", idea128cbc_functions },
218     { "IDEA-OFB", "default=yes", idea128ofb64_functions },
219     { "IDEA-CFB", "default=yes", idea128cfb64_functions },
220 #endif /* OPENSSL_NO_IDEA */
221 #ifndef OPENSSL_NO_CAST
222     { "CAST5-ECB", "default=yes", cast5128ecb_functions },
223     { "CAST5-CBC", "default=yes", cast5128cbc_functions },
224     { "CAST5-OFB", "default=yes", cast564ofb64_functions },
225     { "CAST5-CFB", "default=yes", cast564cfb64_functions },
226 #endif /* OPENSSL_NO_CAST */
227 #ifndef OPENSSL_NO_SEED
228     { "SEED-ECB", "default=yes", seed128ecb_functions },
229     { "SEED-CBC", "default=yes", seed128cbc_functions },
230     { "SEED-OFB", "default=yes", seed128ofb128_functions },
231     { "SEED-CFB", "default=yes", seed128cfb128_functions },
232 #endif /* OPENSSL_NO_SEED */
233 #ifndef OPENSSL_NO_SM4
234     { "SM4-ECB", "default=yes", sm4128ecb_functions },
235     { "SM4-CBC", "default=yes", sm4128cbc_functions },
236     { "SM4-CTR", "default=yes", sm4128ctr_functions },
237     { "SM4-OFB", "default=yes", sm4128ofb128_functions },
238     { "SM4-CFB", "default=yes", sm4128cfb128_functions },
239 #endif /* OPENSSL_NO_SM4 */
240 #ifndef OPENSSL_NO_RC4
241     { "RC4", "default=yes", rc4128_functions },
242     { "RC4-40", "default=yes", rc440_functions },
243 #endif /* OPENSSL_NO_RC4 */
244     { NULL, NULL, NULL }
245 };
246
247 static const OSSL_ALGORITHM deflt_macs[] = {
248 #ifndef OPENSSL_NO_BLAKE2
249     { "BLAKE2BMAC", "default=yes", blake2bmac_functions },
250     { "BLAKE2SMAC", "default=yes", blake2smac_functions },
251 #endif
252 #ifndef OPENSSL_NO_CMAC
253     { "CMAC", "default=yes", cmac_functions },
254 #endif
255     { "GMAC", "default=yes", gmac_functions },
256     { "HMAC", "default=yes", hmac_functions },
257     { "KMAC128", "default=yes", kmac128_functions },
258     { "KMAC256", "default=yes", kmac256_functions },
259 #ifndef OPENSSL_NO_SIPHASH
260     { "SipHash", "default=yes", siphash_functions },
261 #endif
262 #ifndef OPENSSL_NO_POLY1305
263     { "Poly1305", "default=yes", poly1305_functions },
264 #endif
265     { NULL, NULL, NULL }
266 };
267
268 static const OSSL_ALGORITHM deflt_kdfs[] = {
269     { OSSL_KDF_NAME_HKDF, "default=yes", kdf_hkdf_functions },
270     { OSSL_KDF_NAME_SSKDF, "default=yes", kdf_sskdf_functions },
271     { OSSL_KDF_NAME_PBKDF2, "default=yes", kdf_pbkdf2_functions },
272     { OSSL_KDF_NAME_SSHKDF, "default=yes", kdf_sshkdf_functions },
273     { OSSL_KDF_NAME_X963KDF, "default=yes", kdf_x963_kdf_functions },
274     { OSSL_KDF_NAME_TLS1_PRF, "default=yes", kdf_tls1_prf_functions },
275     { OSSL_KDF_NAME_KBKDF, "default=yes", kdf_kbkdf_functions },
276 #ifndef OPENSSL_NO_CMS
277     { OSSL_KDF_NAME_X942KDF, "default=yes", kdf_x942_kdf_functions },
278 #endif
279 #ifndef OPENSSL_NO_SCRYPT
280     { OSSL_KDF_NAME_SCRYPT, "default=yes", kdf_scrypt_functions },
281 #endif
282    { NULL, NULL, NULL }
283 };
284
285 static const OSSL_ALGORITHM deflt_keyexch[] = {
286 #ifndef OPENSSL_NO_DH
287     { "dhKeyAgreement", "default=yes", dh_keyexch_functions },
288 #endif
289     { NULL, NULL, NULL }
290 };
291
292 static const OSSL_ALGORITHM deflt_signature[] = {
293 #ifndef OPENSSL_NO_DSA
294     { "DSA", "default=yes", dsa_signature_functions },
295 #endif
296     { NULL, NULL, NULL }
297 };
298
299
300 static const OSSL_ALGORITHM deflt_keymgmt[] = {
301 #ifndef OPENSSL_NO_DH
302     { "dhKeyAgreement", "default=yes", dh_keymgmt_functions },
303 #endif
304 #ifndef OPENSSL_NO_DSA
305     { "DSA", "default=yes", dsa_keymgmt_functions },
306 #endif
307     { NULL, NULL, NULL }
308 };
309
310 static const OSSL_ALGORITHM *deflt_query(OSSL_PROVIDER *prov,
311                                          int operation_id,
312                                          int *no_cache)
313 {
314     *no_cache = 0;
315     switch (operation_id) {
316     case OSSL_OP_DIGEST:
317         return deflt_digests;
318     case OSSL_OP_CIPHER:
319         return deflt_ciphers;
320     case OSSL_OP_MAC:
321         return deflt_macs;
322     case OSSL_OP_KDF:
323         return deflt_kdfs;
324     case OSSL_OP_KEYMGMT:
325         return deflt_keymgmt;
326     case OSSL_OP_KEYEXCH:
327         return deflt_keyexch;
328     case OSSL_OP_SIGNATURE:
329         return deflt_signature;
330     }
331     return NULL;
332 }
333
334 /* Functions we provide to the core */
335 static const OSSL_DISPATCH deflt_dispatch_table[] = {
336     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))deflt_gettable_params },
337     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))deflt_get_params },
338     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))deflt_query },
339     { 0, NULL }
340 };
341
342 OSSL_provider_init_fn ossl_default_provider_init;
343
344 int ossl_default_provider_init(const OSSL_PROVIDER *provider,
345                                const OSSL_DISPATCH *in,
346                                const OSSL_DISPATCH **out,
347                                void **provctx)
348 {
349     OSSL_core_get_library_context_fn *c_get_libctx = NULL;
350
351     for (; in->function_id != 0; in++) {
352         switch (in->function_id) {
353         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
354             c_gettable_params = OSSL_get_core_gettable_params(in);
355             break;
356         case OSSL_FUNC_CORE_GET_PARAMS:
357             c_get_params = OSSL_get_core_get_params(in);
358             break;
359         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
360             c_get_libctx = OSSL_get_core_get_library_context(in);
361             break;
362         default:
363             /* Just ignore anything we don't understand */
364             break;
365         }
366     }
367
368     if (c_get_libctx == NULL)
369         return 0;
370
371     *out = deflt_dispatch_table;
372
373     /*
374      * We want to make sure that all calls from this provider that requires
375      * a library context use the same context as the one used to call our
376      * functions.  We do that by passing it along as the provider context.
377      */
378     *provctx = c_get_libctx(provider);
379     return 1;
380 }