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