2 * Copyright 2018-2019 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/engine.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/kdf.h>
18 #include <openssl/core.h>
19 #include <openssl/core_names.h>
20 #include "crypto/asn1.h"
21 #include "crypto/evp.h"
22 #include "internal/numbers.h"
23 #include "internal/provider.h"
26 EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf)
28 EVP_KDF_CTX *ctx = NULL;
33 ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX));
35 || (ctx->data = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL
36 || !EVP_KDF_up_ref(kdf)) {
37 EVPerr(EVP_F_EVP_KDF_CTX_NEW, ERR_R_MALLOC_FAILURE);
39 kdf->freectx(ctx->data);
48 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
51 ctx->meth->freectx(ctx->data);
53 EVP_KDF_free(ctx->meth);
58 EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src)
62 if (src == NULL || src->data == NULL || src->meth->dupctx == NULL)
65 dst = OPENSSL_malloc(sizeof(*dst));
67 EVPerr(EVP_F_EVP_KDF_CTX_DUP, ERR_R_MALLOC_FAILURE);
71 memcpy(dst, src, sizeof(*dst));
72 if (!EVP_KDF_up_ref(dst->meth)) {
73 EVPerr(EVP_F_EVP_KDF_CTX_DUP, ERR_R_MALLOC_FAILURE);
78 dst->data = src->meth->dupctx(src->data);
79 if (dst->data == NULL) {
80 EVP_KDF_CTX_free(dst);
86 const char *EVP_KDF_name(const EVP_KDF *kdf)
88 return evp_first_name(kdf->prov, kdf->name_id);
91 const OSSL_PROVIDER *EVP_KDF_provider(const EVP_KDF *kdf)
96 const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx)
101 void EVP_KDF_reset(EVP_KDF_CTX *ctx)
106 if (ctx->meth->reset != NULL)
107 ctx->meth->reset(ctx->data);
110 size_t EVP_KDF_size(EVP_KDF_CTX *ctx)
112 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
118 *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s);
119 if (ctx->meth->get_ctx_params != NULL
120 && ctx->meth->get_ctx_params(ctx->data, params))
122 if (ctx->meth->get_params != NULL
123 && ctx->meth->get_params(params))
128 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen)
133 return ctx->meth->derive(ctx->data, key, keylen);
137 * The {get,set}_params functions return 1 if there is no corresponding
138 * function in the implementation. This is the same as if there was one,
139 * but it didn't recognise any of the given params, i.e. nothing in the
140 * bag of parameters was useful.
142 int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[])
144 if (kdf->get_params != NULL)
145 return kdf->get_params(params);
149 int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[])
151 if (ctx->meth->get_ctx_params != NULL)
152 return ctx->meth->get_ctx_params(ctx->data, params);
156 int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[])
158 if (ctx->meth->set_ctx_params != NULL)
159 return ctx->meth->set_ctx_params(ctx->data, params);