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"
19 static OSSL_FUNC_keyexch_newctx_fn kdf_newctx;
20 static OSSL_FUNC_keyexch_init_fn kdf_init;
21 static OSSL_FUNC_keyexch_derive_fn kdf_derive;
22 static OSSL_FUNC_keyexch_freectx_fn kdf_freectx;
23 static OSSL_FUNC_keyexch_dupctx_fn kdf_dupctx;
24 static OSSL_FUNC_keyexch_set_ctx_params_fn kdf_set_ctx_params;
25 static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_settable_ctx_params;
33 static void *kdf_newctx(void *provctx)
35 PROV_KDF_CTX *kdfctx = OPENSSL_zalloc(sizeof(PROV_KDF_CTX));
41 kdfctx->provctx = provctx;
43 kdf = EVP_KDF_fetch(PROV_LIBRARY_CONTEXT_OF(provctx), "TLS1-PRF", NULL);
46 kdfctx->kdfctx = EVP_KDF_new_ctx(kdf);
49 if (kdfctx->kdfctx == NULL)
58 static int kdf_init(void *vpkdfctx, void *vkdf)
60 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
62 if (pkdfctx == NULL || vkdf == NULL || !kdf_data_up_ref(vkdf))
64 pkdfctx->kdfdata = vkdf;
69 static int kdf_derive(void *vpkdfctx, unsigned char *secret, size_t *secretlen,
72 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
74 return EVP_KDF_derive(pkdfctx->kdfctx, secret, *secretlen);
77 static void kdf_freectx(void *vpkdfctx)
79 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
81 EVP_KDF_CTX_free(pkdfctx->kdfctx);
82 kdf_data_free(pkdfctx->kdfdata);
84 OPENSSL_free(pkdfctx);
87 static void *kdf_dupctx(void *vpkdfctx)
89 PROV_KDF_CTX *srcctx = (PROV_KDF_CTX *)vpkdfctx;
92 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
98 dstctx->kdfctx = EVP_KDF_dup_ctx(srcctx->kdfctx);
99 if (dstctx->kdfctx == NULL) {
100 OPENSSL_free(dstctx);
103 if (!kdf_data_up_ref(dstctx->kdfdata)) {
104 EVP_KDF_CTX_free(dstctx->kdfctx);
105 OPENSSL_free(dstctx);
112 static int kdf_set_ctx_params(void *vpkdfctx, const OSSL_PARAM params[])
114 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
116 return EVP_KDF_set_ctx_params(pkdfctx->kdfctx, params);
120 static const OSSL_PARAM *kdf_settable_ctx_params(void)
123 * TODO(3.0): FIXME FIXME!! These settable_ctx_params functions should
124 * should have a provctx argument so we can get hold of the libctx.
126 EVP_KDF *kdf = EVP_KDF_fetch(NULL, "TLS1-PRF", NULL);
127 const OSSL_PARAM *params;
132 params = EVP_KDF_settable_ctx_params(kdf);
138 const OSSL_DISPATCH kdf_keyexch_functions[] = {
139 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))kdf_newctx },
140 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))kdf_init },
141 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))kdf_derive },
142 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))kdf_freectx },
143 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))kdf_dupctx },
144 { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))kdf_set_ctx_params },
145 { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS,
146 (void (*)(void))kdf_settable_ctx_params },