2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/crypto.h>
11 #include <openssl/kdf.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
14 #include <openssl/params.h>
15 #include "prov/implementations.h"
16 #include "prov/provider_ctx.h"
17 #include "prov/kdfexchange.h"
18 #include "prov/providercommon.h"
20 static OSSL_FUNC_keyexch_newctx_fn kdf_tls1_prf_newctx;
21 static OSSL_FUNC_keyexch_newctx_fn kdf_hkdf_newctx;
22 static OSSL_FUNC_keyexch_newctx_fn kdf_scrypt_newctx;
23 static OSSL_FUNC_keyexch_init_fn kdf_init;
24 static OSSL_FUNC_keyexch_derive_fn kdf_derive;
25 static OSSL_FUNC_keyexch_freectx_fn kdf_freectx;
26 static OSSL_FUNC_keyexch_dupctx_fn kdf_dupctx;
27 static OSSL_FUNC_keyexch_set_ctx_params_fn kdf_set_ctx_params;
28 static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
29 static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_hkdf_settable_ctx_params;
30 static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_scrypt_settable_ctx_params;
38 static void *kdf_newctx(const char *kdfname, void *provctx)
43 if (!ossl_prov_is_running())
46 kdfctx = OPENSSL_zalloc(sizeof(PROV_KDF_CTX));
50 kdfctx->provctx = provctx;
52 kdf = EVP_KDF_fetch(PROV_LIBCTX_OF(provctx), kdfname, NULL);
55 kdfctx->kdfctx = EVP_KDF_CTX_new(kdf);
58 if (kdfctx->kdfctx == NULL)
67 #define KDF_NEWCTX(funcname, kdfname) \
68 static void *kdf_##funcname##_newctx(void *provctx) \
70 return kdf_newctx(kdfname, provctx); \
73 KDF_NEWCTX(tls1_prf, "TLS1-PRF")
74 KDF_NEWCTX(hkdf, "HKDF")
75 KDF_NEWCTX(scrypt, "SCRYPT")
77 static int kdf_init(void *vpkdfctx, void *vkdf)
79 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
81 if (!ossl_prov_is_running()
84 || !kdf_data_up_ref(vkdf))
86 pkdfctx->kdfdata = vkdf;
91 static int kdf_derive(void *vpkdfctx, unsigned char *secret, size_t *secretlen,
94 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
96 if (!ossl_prov_is_running())
98 return EVP_KDF_derive(pkdfctx->kdfctx, secret, *secretlen);
101 static void kdf_freectx(void *vpkdfctx)
103 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
105 EVP_KDF_CTX_free(pkdfctx->kdfctx);
106 kdf_data_free(pkdfctx->kdfdata);
108 OPENSSL_free(pkdfctx);
111 static void *kdf_dupctx(void *vpkdfctx)
113 PROV_KDF_CTX *srcctx = (PROV_KDF_CTX *)vpkdfctx;
114 PROV_KDF_CTX *dstctx;
116 if (!ossl_prov_is_running())
119 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
125 dstctx->kdfctx = EVP_KDF_CTX_dup(srcctx->kdfctx);
126 if (dstctx->kdfctx == NULL) {
127 OPENSSL_free(dstctx);
130 if (!kdf_data_up_ref(dstctx->kdfdata)) {
131 EVP_KDF_CTX_free(dstctx->kdfctx);
132 OPENSSL_free(dstctx);
139 static int kdf_set_ctx_params(void *vpkdfctx, const OSSL_PARAM params[])
141 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
143 return EVP_KDF_CTX_set_params(pkdfctx->kdfctx, params);
146 static const OSSL_PARAM *kdf_settable_ctx_params(void *provctx,
149 EVP_KDF *kdf = EVP_KDF_fetch(PROV_LIBCTX_OF(provctx), kdfname,
151 const OSSL_PARAM *params;
156 params = EVP_KDF_settable_ctx_params(kdf);
162 #define KDF_SETTABLE_CTX_PARAMS(funcname, kdfname) \
163 static const OSSL_PARAM *kdf_##funcname##_settable_ctx_params(void *provctx) \
165 return kdf_settable_ctx_params(provctx, kdfname); \
168 KDF_SETTABLE_CTX_PARAMS(tls1_prf, "TLS1-PRF")
169 KDF_SETTABLE_CTX_PARAMS(hkdf, "HKDF")
170 KDF_SETTABLE_CTX_PARAMS(scrypt, "SCRYPT")
172 #define KDF_KEYEXCH_FUNCTIONS(funcname) \
173 const OSSL_DISPATCH ossl_kdf_##funcname##_keyexch_functions[] = { \
174 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))kdf_##funcname##_newctx }, \
175 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))kdf_init }, \
176 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))kdf_derive }, \
177 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))kdf_freectx }, \
178 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))kdf_dupctx }, \
179 { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))kdf_set_ctx_params }, \
180 { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS, \
181 (void (*)(void))kdf_##funcname##_settable_ctx_params }, \
185 KDF_KEYEXCH_FUNCTIONS(tls1_prf)
186 KDF_KEYEXCH_FUNCTIONS(hkdf)
187 KDF_KEYEXCH_FUNCTIONS(scrypt)