xref: /freebsd/crypto/openssl/providers/implementations/kdfs/pbkdf1.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*
2  * Copyright 1999-2021 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 
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     for (i = 1; i < iter; i++) {
75         if (!EVP_DigestInit_ex(ctx, md_type, NULL))
76             goto err;
77         if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
78             goto err;
79         if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
80             goto err;
81     }
82 
83     memcpy(out, md_tmp, n);
84     ret = 1;
85 err:
86     EVP_MD_CTX_free(ctx);
87     return ret;
88 }
89 
90 static void *kdf_pbkdf1_new(void *provctx)
91 {
92     KDF_PBKDF1 *ctx;
93 
94     if (!ossl_prov_is_running())
95         return NULL;
96 
97     ctx = OPENSSL_zalloc(sizeof(*ctx));
98     if (ctx == NULL) {
99         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
100         return NULL;
101     }
102     ctx->provctx = provctx;
103     return ctx;
104 }
105 
106 static void kdf_pbkdf1_cleanup(KDF_PBKDF1 *ctx)
107 {
108     ossl_prov_digest_reset(&ctx->digest);
109     OPENSSL_free(ctx->salt);
110     OPENSSL_clear_free(ctx->pass, ctx->pass_len);
111     memset(ctx, 0, sizeof(*ctx));
112 }
113 
114 static void kdf_pbkdf1_free(void *vctx)
115 {
116     KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
117 
118     if (ctx != NULL) {
119         kdf_pbkdf1_cleanup(ctx);
120         OPENSSL_free(ctx);
121     }
122 }
123 
124 static void kdf_pbkdf1_reset(void *vctx)
125 {
126     KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
127     void *provctx = ctx->provctx;
128 
129     kdf_pbkdf1_cleanup(ctx);
130     ctx->provctx = provctx;
131 }
132 
133 static int kdf_pbkdf1_set_membuf(unsigned char **buffer, size_t *buflen,
134                              const OSSL_PARAM *p)
135 {
136     OPENSSL_clear_free(*buffer, *buflen);
137     *buffer = NULL;
138     *buflen = 0;
139 
140     if (p->data_size == 0) {
141         if ((*buffer = OPENSSL_malloc(1)) == NULL) {
142             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
143             return 0;
144         }
145     } else if (p->data != NULL) {
146         if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
147             return 0;
148     }
149     return 1;
150 }
151 
152 static int kdf_pbkdf1_derive(void *vctx, unsigned char *key, size_t keylen,
153                              const OSSL_PARAM params[])
154 {
155     KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
156     const EVP_MD *md;
157 
158     if (!ossl_prov_is_running() || !kdf_pbkdf1_set_ctx_params(ctx, params))
159         return 0;
160 
161     if (ctx->pass == NULL) {
162         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
163         return 0;
164     }
165 
166     if (ctx->salt == NULL) {
167         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
168         return 0;
169     }
170 
171     md = ossl_prov_digest_md(&ctx->digest);
172     return kdf_pbkdf1_do_derive(ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len,
173                                 ctx->iter, md, key, keylen);
174 }
175 
176 static int kdf_pbkdf1_set_ctx_params(void *vctx, const OSSL_PARAM params[])
177 {
178     const OSSL_PARAM *p;
179     KDF_PBKDF1 *ctx = vctx;
180     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
181 
182     if (!ossl_prov_digest_load_from_params(&ctx->digest, params, libctx))
183         return 0;
184 
185     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
186         if (!kdf_pbkdf1_set_membuf(&ctx->pass, &ctx->pass_len, p))
187             return 0;
188 
189     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
190         if (!kdf_pbkdf1_set_membuf(&ctx->salt, &ctx->salt_len,p))
191             return 0;
192 
193     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL)
194         if (!OSSL_PARAM_get_uint64(p, &ctx->iter))
195             return 0;
196     return 1;
197 }
198 
199 static const OSSL_PARAM *kdf_pbkdf1_settable_ctx_params(ossl_unused void *ctx,
200                                                         ossl_unused void *p_ctx)
201 {
202     static const OSSL_PARAM known_settable_ctx_params[] = {
203         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
204         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
205         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
206         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
207         OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
208         OSSL_PARAM_END
209     };
210     return known_settable_ctx_params;
211 }
212 
213 static int kdf_pbkdf1_get_ctx_params(void *vctx, OSSL_PARAM params[])
214 {
215     OSSL_PARAM *p;
216 
217     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
218         return OSSL_PARAM_set_size_t(p, SIZE_MAX);
219     return -2;
220 }
221 
222 static const OSSL_PARAM *kdf_pbkdf1_gettable_ctx_params(ossl_unused void *ctx,
223                                                         ossl_unused void *p_ctx)
224 {
225     static const OSSL_PARAM known_gettable_ctx_params[] = {
226         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
227         OSSL_PARAM_END
228     };
229     return known_gettable_ctx_params;
230 }
231 
232 const OSSL_DISPATCH ossl_kdf_pbkdf1_functions[] = {
233     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf1_new },
234     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf1_free },
235     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf1_reset },
236     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf1_derive },
237     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
238       (void(*)(void))kdf_pbkdf1_settable_ctx_params },
239     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf1_set_ctx_params },
240     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
241       (void(*)(void))kdf_pbkdf1_gettable_ctx_params },
242     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf1_get_ctx_params },
243     { 0, NULL }
244 };
245