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