Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/providers/implementations/kdfs/pvkkdf.c.in

raw
1 /*
2 * Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9 /* clang-format off */
10 {-
11 use OpenSSL::paramnames qw(produce_param_decoder);
12 -}
13 /* clang-format on */
14
15 #include <string.h>
16 #include <openssl/evp.h>
17 #include <openssl/core_names.h>
18 #include <openssl/proverr.h>
19 #include <openssl/err.h>
20 #include "internal/common.h"
21 #include "internal/numbers.h" /* SIZE_MAX */
22 #include "prov/provider_ctx.h"
23 #include "prov/providercommon.h"
24 #include "prov/implementations.h"
25 #include "prov/provider_util.h"
26
27 static OSSL_FUNC_kdf_newctx_fn kdf_pvk_new;
28 static OSSL_FUNC_kdf_dupctx_fn kdf_pvk_dup;
29 static OSSL_FUNC_kdf_freectx_fn kdf_pvk_free;
30 static OSSL_FUNC_kdf_reset_fn kdf_pvk_reset;
31 static OSSL_FUNC_kdf_derive_fn kdf_pvk_derive;
32 static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pvk_settable_ctx_params;
33 static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pvk_set_ctx_params;
34 static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pvk_gettable_ctx_params;
35 static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pvk_get_ctx_params;
36
37 typedef struct {
38 void *provctx;
39 unsigned char *pass;
40 size_t pass_len;
41 unsigned char *salt;
42 size_t salt_len;
43 PROV_DIGEST digest;
44 } KDF_PVK;
45
46 static void kdf_pvk_init(KDF_PVK *ctx);
47
48 static void *kdf_pvk_new(void *provctx)
49 {
50 KDF_PVK *ctx;
51
52 if (!ossl_prov_is_running())
53 return NULL;
54
55 ctx = OPENSSL_zalloc(sizeof(*ctx));
56 if (ctx == NULL)
57 return NULL;
58 ctx->provctx = provctx;
59 kdf_pvk_init(ctx);
60 return ctx;
61 }
62
63 static void kdf_pvk_cleanup(KDF_PVK *ctx)
64 {
65 ossl_prov_digest_reset(&ctx->digest);
66 OPENSSL_free(ctx->salt);
67 OPENSSL_clear_free(ctx->pass, ctx->pass_len);
68 OPENSSL_cleanse(ctx, sizeof(*ctx));
69 }
70
71 static void kdf_pvk_free(void *vctx)
72 {
73 KDF_PVK *ctx = (KDF_PVK *)vctx;
74
75 if (ctx != NULL) {
76 kdf_pvk_cleanup(ctx);
77 OPENSSL_free(ctx);
78 }
79 }
80
81 static void *kdf_pvk_dup(void *vctx)
82 {
83 const KDF_PVK *src = (const KDF_PVK *)vctx;
84 KDF_PVK *dest;
85
86 dest = kdf_pvk_new(src->provctx);
87 if (dest != NULL)
88 if (!ossl_prov_memdup(src->salt, src->salt_len,
89 &dest->salt, &dest->salt_len)
90 || !ossl_prov_memdup(src->pass, src->pass_len,
91 &dest->pass, &dest->pass_len)
92 || !ossl_prov_digest_copy(&dest->digest, &src->digest))
93 goto err;
94 return dest;
95
96 err:
97 kdf_pvk_free(dest);
98 return NULL;
99 }
100
101 static void kdf_pvk_reset(void *vctx)
102 {
103 KDF_PVK *ctx = (KDF_PVK *)vctx;
104 void *provctx = ctx->provctx;
105
106 kdf_pvk_cleanup(ctx);
107 ctx->provctx = provctx;
108 kdf_pvk_init(ctx);
109 }
110
111 static void kdf_pvk_init(KDF_PVK *ctx)
112 {
113 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
114 OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
115
116 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
117 SN_sha1, 0);
118 if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
119 /* This is an error, but there is no way to indicate such directly */
120 ossl_prov_digest_reset(&ctx->digest);
121 }
122
123 static int pvk_set_membuf(unsigned char **buffer, size_t *buflen,
124 const OSSL_PARAM *p)
125 {
126 OPENSSL_clear_free(*buffer, *buflen);
127 *buffer = NULL;
128 *buflen = 0;
129
130 if (p->data_size == 0) {
131 if ((*buffer = OPENSSL_malloc(1)) == NULL)
132 return 0;
133 } else if (p->data != NULL) {
134 if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
135 return 0;
136 }
137 return 1;
138 }
139
140 static int kdf_pvk_derive(void *vctx, unsigned char *key, size_t keylen,
141 const OSSL_PARAM params[])
142 {
143 KDF_PVK *ctx = (KDF_PVK *)vctx;
144 const EVP_MD *md;
145 EVP_MD_CTX *mctx;
146 int res;
147
148 if (!ossl_prov_is_running() || !kdf_pvk_set_ctx_params(ctx, params))
149 return 0;
150
151 if (ctx->pass == NULL) {
152 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
153 return 0;
154 }
155
156 if (ctx->salt == NULL) {
157 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
158 return 0;
159 }
160
161 md = ossl_prov_digest_md(&ctx->digest);
162 if (md == NULL) {
163 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
164 return 0;
165 }
166 res = EVP_MD_get_size(md);
167 if (res <= 0) {
168 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
169 return 0;
170 }
171 if ((size_t)res > keylen) {
172 ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);
173 return 0;
174 }
175
176 mctx = EVP_MD_CTX_new();
177 res = mctx != NULL
178 && EVP_DigestInit_ex(mctx, md, NULL)
179 && EVP_DigestUpdate(mctx, ctx->salt, ctx->salt_len)
180 && EVP_DigestUpdate(mctx, ctx->pass, ctx->pass_len)
181 && EVP_DigestFinal_ex(mctx, key, NULL);
182 EVP_MD_CTX_free(mctx);
183 return res;
184 }
185
186 /* clang-format off */
187 {- produce_param_decoder('pvk_set_ctx_params',
188 (['OSSL_KDF_PARAM_PROPERTIES', 'propq', 'utf8_string'],
189 ['OSSL_ALG_PARAM_ENGINE', 'engine', 'utf8_string', 'hidden'],
190 ['OSSL_KDF_PARAM_DIGEST', 'digest', 'utf8_string'],
191 ['OSSL_KDF_PARAM_PASSWORD', 'pass', 'octet_string'],
192 ['OSSL_KDF_PARAM_SALT', 'salt', 'octet_string'],
193 )); -}
194 /* clang-format on */
195
196 static int kdf_pvk_set_ctx_params(void *vctx, const OSSL_PARAM params[])
197 {
198 struct pvk_set_ctx_params_st p;
199 KDF_PVK *ctx = vctx;
200 OSSL_LIB_CTX *provctx;
201
202 if (ctx == NULL || !pvk_set_ctx_params_decoder(params, &p))
203 return 0;
204
205 provctx = PROV_LIBCTX_OF(ctx->provctx);
206
207 if (!ossl_prov_digest_load(&ctx->digest, p.digest, p.propq, p.engine,
208 provctx))
209 return 0;
210
211 if (p.pass != NULL && !pvk_set_membuf(&ctx->pass, &ctx->pass_len, p.pass))
212 return 0;
213
214 if (p.salt != NULL && !pvk_set_membuf(&ctx->salt, &ctx->salt_len, p.salt))
215 return 0;
216
217 return 1;
218 }
219
220 static const OSSL_PARAM *kdf_pvk_settable_ctx_params(ossl_unused void *ctx,
221 ossl_unused void *p_ctx)
222 {
223 return pvk_set_ctx_params_list;
224 }
225
226 /* clang-format off */
227 {- produce_param_decoder('pvk_get_ctx_params',
228 (['OSSL_KDF_PARAM_SIZE', 'size', 'size_t'],
229 )); -}
230 /* clang-format on */
231
232 static int kdf_pvk_get_ctx_params(void *vctx, OSSL_PARAM params[])
233 {
234 struct pvk_get_ctx_params_st p;
235 KDF_PVK *ctx = vctx;
236
237 if (ctx == NULL || !pvk_get_ctx_params_decoder(params, &p))
238 return 0;
239
240 if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX))
241 return 0;
242 return 1;
243 }
244
245 static const OSSL_PARAM *kdf_pvk_gettable_ctx_params(ossl_unused void *ctx,
246 ossl_unused void *p_ctx)
247 {
248 return pvk_get_ctx_params_list;
249 }
250
251 const OSSL_DISPATCH ossl_kdf_pvk_functions[] = {
252 { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_pvk_new },
253 { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_pvk_dup },
254 { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_pvk_free },
255 { OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_pvk_reset },
256 { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_pvk_derive },
257 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
258 (void (*)(void))kdf_pvk_settable_ctx_params },
259 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))kdf_pvk_set_ctx_params },
260 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
261 (void (*)(void))kdf_pvk_gettable_ctx_params },
262 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))kdf_pvk_get_ctx_params },
263 OSSL_DISPATCH_END
264 };