1/*
2 * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2018-2019, Oracle and/or its affiliates.  All rights reserved.
4 *
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
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
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"
22
23EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf)
24{
25    EVP_KDF_CTX *ctx = NULL;
26
27    if (kdf == NULL)
28        return NULL;
29
30    ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX));
31    if (ctx == NULL
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);
35        if (ctx != NULL)
36            kdf->freectx(ctx->algctx);
37        OPENSSL_free(ctx);
38        ctx = NULL;
39    } else {
40        ctx->meth = kdf;
41    }
42    return ctx;
43}
44
45void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
46{
47    if (ctx == NULL)
48        return;
49    ctx->meth->freectx(ctx->algctx);
50    ctx->algctx = NULL;
51    EVP_KDF_free(ctx->meth);
52    OPENSSL_free(ctx);
53}
54
55EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src)
56{
57    EVP_KDF_CTX *dst;
58
59    if (src == NULL || src->algctx == NULL || src->meth->dupctx == NULL)
60        return NULL;
61
62    dst = OPENSSL_malloc(sizeof(*dst));
63    if (dst == NULL) {
64        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
65        return NULL;
66    }
67
68    memcpy(dst, src, sizeof(*dst));
69    if (!EVP_KDF_up_ref(dst->meth)) {
70        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
71        OPENSSL_free(dst);
72        return NULL;
73    }
74
75    dst->algctx = src->meth->dupctx(src->algctx);
76    if (dst->algctx == NULL) {
77        EVP_KDF_CTX_free(dst);
78        return NULL;
79    }
80    return dst;
81}
82
83int evp_kdf_get_number(const EVP_KDF *kdf)
84{
85    return kdf->name_id;
86}
87
88const char *EVP_KDF_get0_name(const EVP_KDF *kdf)
89{
90    return kdf->type_name;
91}
92
93const char *EVP_KDF_get0_description(const EVP_KDF *kdf)
94{
95    return kdf->description;
96}
97
98int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name)
99{
100    return kdf != NULL && evp_is_a(kdf->prov, kdf->name_id, NULL, name);
101}
102
103const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf)
104{
105    return kdf->prov;
106}
107
108const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx)
109{
110    return ctx->meth;
111}
112
113void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx)
114{
115    if (ctx == NULL)
116        return;
117
118    if (ctx->meth->reset != NULL)
119        ctx->meth->reset(ctx->algctx);
120}
121
122size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx)
123{
124    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
125    size_t s = 0;
126
127    if (ctx == NULL)
128        return 0;
129
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))
133            return s;
134    if (ctx->meth->get_params != NULL
135        && ctx->meth->get_params(params))
136            return s;
137    return 0;
138}
139
140int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen,
141                   const OSSL_PARAM params[])
142{
143    if (ctx == NULL)
144        return 0;
145
146    return ctx->meth->derive(ctx->algctx, key, keylen, params);
147}
148
149/*
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.
154 */
155int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[])
156{
157    if (kdf->get_params != NULL)
158        return kdf->get_params(params);
159    return 1;
160}
161
162int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[])
163{
164    if (ctx->meth->get_ctx_params != NULL)
165        return ctx->meth->get_ctx_params(ctx->algctx, params);
166    return 1;
167}
168
169int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[])
170{
171    if (ctx->meth->set_ctx_params != NULL)
172        return ctx->meth->set_ctx_params(ctx->algctx, params);
173    return 1;
174}
175
176int EVP_KDF_names_do_all(const EVP_KDF *kdf,
177                         void (*fn)(const char *name, void *data),
178                         void *data)
179{
180    if (kdf->prov != NULL)
181        return evp_names_do_all(kdf->prov, kdf->name_id, fn, data);
182
183    return 1;
184}
185