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