Coverage Report

Created: 2025-06-22 06:56

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