1 /*
2  * Copyright 1999-2024 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 <openssl/trace.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <string.h>
14 #include <openssl/evp.h>
15 #include <openssl/kdf.h>
16 #include <openssl/core_names.h>
17 #include <openssl/proverr.h>
18 #include "internal/cryptlib.h"
19 #include "internal/numbers.h"
20 #include "crypto/evp.h"
21 #include "prov/provider_ctx.h"
22 #include "prov/providercommon.h"
23 #include "prov/implementations.h"
24 #include "prov/provider_util.h"
25 
26 static OSSL_FUNC_kdf_newctx_fn kdf_pbkdf1_new;
27 static OSSL_FUNC_kdf_freectx_fn kdf_pbkdf1_free;
28 static OSSL_FUNC_kdf_reset_fn kdf_pbkdf1_reset;
29 static OSSL_FUNC_kdf_derive_fn kdf_pbkdf1_derive;
30 static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf1_settable_ctx_params;
31 static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf1_set_ctx_params;
32 static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf1_gettable_ctx_params;
33 static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf1_get_ctx_params;
34 
35 typedef struct {
36     void *provctx;
37     PROV_DIGEST digest;
38     unsigned char *pass;
39     size_t pass_len;
40     unsigned char *salt;
41     size_t salt_len;
42     uint64_t iter;
43 } KDF_PBKDF1;
44 
45 /*
46  * PKCS5 PBKDF1 compatible key/IV generation as specified in:
47  * https://tools.ietf.org/html/rfc8018#page-10
48  */
49 
kdf_pbkdf1_do_derive(const unsigned char * pass,size_t passlen,const unsigned char * salt,size_t saltlen,uint64_t iter,const EVP_MD * md_type,unsigned char * out,size_t n)50 static int kdf_pbkdf1_do_derive(const unsigned char *pass, size_t passlen,
51                                 const unsigned char *salt, size_t saltlen,
52                                 uint64_t iter, const EVP_MD *md_type,
53                                 unsigned char *out, size_t n)
54 {
55     uint64_t i;
56     int mdsize, ret = 0;
57     unsigned char md_tmp[EVP_MAX_MD_SIZE];
58     EVP_MD_CTX *ctx = NULL;
59 
60     ctx = EVP_MD_CTX_new();
61     if (ctx == NULL) {
62         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
63         goto err;
64     }
65 
66     if (!EVP_DigestInit_ex(ctx, md_type, NULL)
67         || !EVP_DigestUpdate(ctx, pass, passlen)
68         || !EVP_DigestUpdate(ctx, salt, saltlen)
69         || !EVP_DigestFinal_ex(ctx, md_tmp, NULL))
70         goto err;
71     mdsize = EVP_MD_size(md_type);
72     if (mdsize < 0)
73         goto err;
74     if (n > (size_t)mdsize) {
75         ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);
76         goto err;
77     }
78 
79     for (i = 1; i < iter; i++) {
80         if (!EVP_DigestInit_ex(ctx, md_type, NULL))
81             goto err;
82         if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
83             goto err;
84         if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
85             goto err;
86     }
87 
88     memcpy(out, md_tmp, n);
89     ret = 1;
90 err:
91     OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
92     EVP_MD_CTX_free(ctx);
93     return ret;
94 }
95 
kdf_pbkdf1_new(void * provctx)96 static void *kdf_pbkdf1_new(void *provctx)
97 {
98     KDF_PBKDF1 *ctx;
99 
100     if (!ossl_prov_is_running())
101         return NULL;
102 
103     ctx = OPENSSL_zalloc(sizeof(*ctx));
104     if (ctx == NULL) {
105         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
106         return NULL;
107     }
108     ctx->provctx = provctx;
109     return ctx;
110 }
111 
kdf_pbkdf1_cleanup(KDF_PBKDF1 * ctx)112 static void kdf_pbkdf1_cleanup(KDF_PBKDF1 *ctx)
113 {
114     ossl_prov_digest_reset(&ctx->digest);
115     OPENSSL_free(ctx->salt);
116     OPENSSL_clear_free(ctx->pass, ctx->pass_len);
117     memset(ctx, 0, sizeof(*ctx));
118 }
119 
kdf_pbkdf1_free(void * vctx)120 static void kdf_pbkdf1_free(void *vctx)
121 {
122     KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
123 
124     if (ctx != NULL) {
125         kdf_pbkdf1_cleanup(ctx);
126         OPENSSL_free(ctx);
127     }
128 }
129 
kdf_pbkdf1_reset(void * vctx)130 static void kdf_pbkdf1_reset(void *vctx)
131 {
132     KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
133     void *provctx = ctx->provctx;
134 
135     kdf_pbkdf1_cleanup(ctx);
136     ctx->provctx = provctx;
137 }
138 
kdf_pbkdf1_set_membuf(unsigned char ** buffer,size_t * buflen,const OSSL_PARAM * p)139 static int kdf_pbkdf1_set_membuf(unsigned char **buffer, size_t *buflen,
140                              const OSSL_PARAM *p)
141 {
142     OPENSSL_clear_free(*buffer, *buflen);
143     *buffer = NULL;
144     *buflen = 0;
145 
146     if (p->data_size == 0) {
147         if ((*buffer = OPENSSL_malloc(1)) == NULL) {
148             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
149             return 0;
150         }
151     } else if (p->data != NULL) {
152         if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
153             return 0;
154     }
155     return 1;
156 }
157 
kdf_pbkdf1_derive(void * vctx,unsigned char * key,size_t keylen,const OSSL_PARAM params[])158 static int kdf_pbkdf1_derive(void *vctx, unsigned char *key, size_t keylen,
159                              const OSSL_PARAM params[])
160 {
161     KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
162     const EVP_MD *md;
163 
164     if (!ossl_prov_is_running() || !kdf_pbkdf1_set_ctx_params(ctx, params))
165         return 0;
166 
167     if (ctx->pass == NULL) {
168         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
169         return 0;
170     }
171 
172     if (ctx->salt == NULL) {
173         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
174         return 0;
175     }
176 
177     md = ossl_prov_digest_md(&ctx->digest);
178     return kdf_pbkdf1_do_derive(ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len,
179                                 ctx->iter, md, key, keylen);
180 }
181 
kdf_pbkdf1_set_ctx_params(void * vctx,const OSSL_PARAM params[])182 static int kdf_pbkdf1_set_ctx_params(void *vctx, const OSSL_PARAM params[])
183 {
184     const OSSL_PARAM *p;
185     KDF_PBKDF1 *ctx = vctx;
186     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
187 
188     if (!ossl_prov_digest_load_from_params(&ctx->digest, params, libctx))
189         return 0;
190 
191     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
192         if (!kdf_pbkdf1_set_membuf(&ctx->pass, &ctx->pass_len, p))
193             return 0;
194 
195     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
196         if (!kdf_pbkdf1_set_membuf(&ctx->salt, &ctx->salt_len,p))
197             return 0;
198 
199     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL)
200         if (!OSSL_PARAM_get_uint64(p, &ctx->iter))
201             return 0;
202     return 1;
203 }
204 
kdf_pbkdf1_settable_ctx_params(ossl_unused void * ctx,ossl_unused void * p_ctx)205 static const OSSL_PARAM *kdf_pbkdf1_settable_ctx_params(ossl_unused void *ctx,
206                                                         ossl_unused void *p_ctx)
207 {
208     static const OSSL_PARAM known_settable_ctx_params[] = {
209         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
210         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
211         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
212         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
213         OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
214         OSSL_PARAM_END
215     };
216     return known_settable_ctx_params;
217 }
218 
kdf_pbkdf1_get_ctx_params(void * vctx,OSSL_PARAM params[])219 static int kdf_pbkdf1_get_ctx_params(void *vctx, OSSL_PARAM params[])
220 {
221     OSSL_PARAM *p;
222 
223     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
224         return OSSL_PARAM_set_size_t(p, SIZE_MAX);
225     return -2;
226 }
227 
kdf_pbkdf1_gettable_ctx_params(ossl_unused void * ctx,ossl_unused void * p_ctx)228 static const OSSL_PARAM *kdf_pbkdf1_gettable_ctx_params(ossl_unused void *ctx,
229                                                         ossl_unused void *p_ctx)
230 {
231     static const OSSL_PARAM known_gettable_ctx_params[] = {
232         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
233         OSSL_PARAM_END
234     };
235     return known_gettable_ctx_params;
236 }
237 
238 const OSSL_DISPATCH ossl_kdf_pbkdf1_functions[] = {
239     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf1_new },
240     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf1_free },
241     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf1_reset },
242     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf1_derive },
243     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
244       (void(*)(void))kdf_pbkdf1_settable_ctx_params },
245     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf1_set_ctx_params },
246     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
247       (void(*)(void))kdf_pbkdf1_gettable_ctx_params },
248     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf1_get_ctx_params },
249     { 0, NULL }
250 };
251