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