]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/kdf_lib.c
55f5c50ab54d4e647974129c8398d0e5533d86ce
[thirdparty/openssl.git] / crypto / evp / kdf_lib.c
1 /*
2 * Copyright 2018-2021 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/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"
24 #include "evp_local.h"
25
26 EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf)
27 {
28 EVP_KDF_CTX *ctx = NULL;
29
30 if (kdf == NULL)
31 return NULL;
32
33 ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX));
34 if (ctx == NULL
35 || (ctx->algctx = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL
36 || !EVP_KDF_up_ref(kdf)) {
37 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
38 if (ctx != NULL)
39 kdf->freectx(ctx->algctx);
40 OPENSSL_free(ctx);
41 ctx = NULL;
42 } else {
43 ctx->meth = kdf;
44 }
45 return ctx;
46 }
47
48 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
49 {
50 if (ctx == NULL)
51 return;
52 ctx->meth->freectx(ctx->algctx);
53 ctx->algctx = NULL;
54 EVP_KDF_free(ctx->meth);
55 OPENSSL_free(ctx);
56 }
57
58 EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src)
59 {
60 EVP_KDF_CTX *dst;
61
62 if (src == NULL || src->algctx == NULL || src->meth->dupctx == NULL)
63 return NULL;
64
65 dst = OPENSSL_malloc(sizeof(*dst));
66 if (dst == NULL) {
67 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
68 return NULL;
69 }
70
71 memcpy(dst, src, sizeof(*dst));
72 if (!EVP_KDF_up_ref(dst->meth)) {
73 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
74 OPENSSL_free(dst);
75 return NULL;
76 }
77
78 dst->algctx = src->meth->dupctx(src->algctx);
79 if (dst->algctx == NULL) {
80 EVP_KDF_CTX_free(dst);
81 return NULL;
82 }
83 return dst;
84 }
85
86 int EVP_KDF_number(const EVP_KDF *kdf)
87 {
88 return kdf->name_id;
89 }
90
91 const char *EVP_KDF_name(const EVP_KDF *kdf)
92 {
93 return kdf->type_name;
94 }
95
96 const char *EVP_KDF_description(const EVP_KDF *kdf)
97 {
98 return kdf->description;
99 }
100
101 int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name)
102 {
103 return evp_is_a(kdf->prov, kdf->name_id, NULL, name);
104 }
105
106 const OSSL_PROVIDER *EVP_KDF_provider(const EVP_KDF *kdf)
107 {
108 return kdf->prov;
109 }
110
111 const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx)
112 {
113 return ctx->meth;
114 }
115
116 void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx)
117 {
118 if (ctx == NULL)
119 return;
120
121 if (ctx->meth->reset != NULL)
122 ctx->meth->reset(ctx->algctx);
123 }
124
125 size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx)
126 {
127 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
128 size_t s;
129
130 if (ctx == NULL)
131 return 0;
132
133 *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s);
134 if (ctx->meth->get_ctx_params != NULL
135 && ctx->meth->get_ctx_params(ctx->algctx, params))
136 return s;
137 if (ctx->meth->get_params != NULL
138 && ctx->meth->get_params(params))
139 return s;
140 return 0;
141 }
142
143 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen,
144 const OSSL_PARAM params[])
145 {
146 if (ctx == NULL)
147 return 0;
148
149 return ctx->meth->derive(ctx->algctx, key, keylen, params);
150 }
151
152 /*
153 * The {get,set}_params functions return 1 if there is no corresponding
154 * function in the implementation. This is the same as if there was one,
155 * but it didn't recognise any of the given params, i.e. nothing in the
156 * bag of parameters was useful.
157 */
158 int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[])
159 {
160 if (kdf->get_params != NULL)
161 return kdf->get_params(params);
162 return 1;
163 }
164
165 int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[])
166 {
167 if (ctx->meth->get_ctx_params != NULL)
168 return ctx->meth->get_ctx_params(ctx->algctx, params);
169 return 1;
170 }
171
172 int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[])
173 {
174 if (ctx->meth->set_ctx_params != NULL)
175 return ctx->meth->set_ctx_params(ctx->algctx, params);
176 return 1;
177 }
178
179 int EVP_KDF_names_do_all(const EVP_KDF *kdf,
180 void (*fn)(const char *name, void *data),
181 void *data)
182 {
183 if (kdf->prov != NULL)
184 return evp_names_do_all(kdf->prov, kdf->name_id, fn, data);
185
186 return 1;
187 }