2 * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2018-2019, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
13 #include "internal/cryptlib.h"
14 #include <openssl/evp.h>
15 #include <openssl/kdf.h>
16 #include <openssl/core.h>
17 #include <openssl/core_names.h>
18 #include "crypto/evp.h"
19 #include "internal/numbers.h"
20 #include "internal/provider.h"
21 #include "evp_local.h"
23 EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf)
25 EVP_KDF_CTX *ctx = NULL;
30 ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX));
32 || (ctx->algctx = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL
33 || !EVP_KDF_up_ref(kdf)) {
34 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
36 kdf->freectx(ctx->algctx);
45 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
49 ctx->meth->freectx(ctx->algctx);
51 EVP_KDF_free(ctx->meth);
55 EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src)
59 if (src == NULL || src->algctx == NULL || src->meth->dupctx == NULL)
62 dst = OPENSSL_malloc(sizeof(*dst));
64 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
68 memcpy(dst, src, sizeof(*dst));
69 if (!EVP_KDF_up_ref(dst->meth)) {
70 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
75 dst->algctx = src->meth->dupctx(src->algctx);
76 if (dst->algctx == NULL) {
77 EVP_KDF_CTX_free(dst);
83 int evp_kdf_get_number(const EVP_KDF *kdf)
88 const char *EVP_KDF_get0_name(const EVP_KDF *kdf)
90 return kdf->type_name;
93 const char *EVP_KDF_get0_description(const EVP_KDF *kdf)
95 return kdf->description;
98 int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name)
100 return evp_is_a(kdf->prov, kdf->name_id, NULL, name);
103 const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf)
108 const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx)
113 void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx)
118 if (ctx->meth->reset != NULL)
119 ctx->meth->reset(ctx->algctx);
122 size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx)
124 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
130 *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s);
131 if (ctx->meth->get_ctx_params != NULL
132 && ctx->meth->get_ctx_params(ctx->algctx, params))
134 if (ctx->meth->get_params != NULL
135 && ctx->meth->get_params(params))
140 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen,
141 const OSSL_PARAM params[])
146 return ctx->meth->derive(ctx->algctx, key, keylen, params);
150 * The {get,set}_params functions return 1 if there is no corresponding
151 * function in the implementation. This is the same as if there was one,
152 * but it didn't recognise any of the given params, i.e. nothing in the
153 * bag of parameters was useful.
155 int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[])
157 if (kdf->get_params != NULL)
158 return kdf->get_params(params);
162 int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[])
164 if (ctx->meth->get_ctx_params != NULL)
165 return ctx->meth->get_ctx_params(ctx->algctx, params);
169 int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[])
171 if (ctx->meth->set_ctx_params != NULL)
172 return ctx->meth->set_ctx_params(ctx->algctx, params);
176 int EVP_KDF_names_do_all(const EVP_KDF *kdf,
177 void (*fn)(const char *name, void *data),
180 if (kdf->prov != NULL)
181 return evp_names_do_all(kdf->prov, kdf->name_id, fn, data);