/src/openssl/providers/implementations/kdfs/pbkdf2.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2018-2025 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  |  | /*  | 
11  |  |  * HMAC low level APIs are deprecated for public use, but still ok for internal  | 
12  |  |  * use.  | 
13  |  |  */  | 
14  |  | #include "internal/deprecated.h"  | 
15  |  |  | 
16  |  | #include <stdlib.h>  | 
17  |  | #include <stdarg.h>  | 
18  |  | #include <string.h>  | 
19  |  | #include <openssl/hmac.h>  | 
20  |  | #include <openssl/evp.h>  | 
21  |  | #include <openssl/kdf.h>  | 
22  |  | #include <openssl/core_names.h>  | 
23  |  | #include <openssl/proverr.h>  | 
24  |  | #include "internal/cryptlib.h"  | 
25  |  | #include "internal/numbers.h"  | 
26  |  | #include "crypto/evp.h"  | 
27  |  | #include "prov/provider_ctx.h"  | 
28  |  | #include "prov/providercommon.h"  | 
29  |  | #include "prov/implementations.h"  | 
30  |  | #include "prov/provider_util.h"  | 
31  |  | #include "prov/securitycheck.h"  | 
32  |  | #include "providers/implementations/kdfs/pbkdf2.inc"  | 
33  |  |  | 
34  |  | /* Constants specified in SP800-132 */  | 
35  | 0  | #define KDF_PBKDF2_MIN_KEY_LEN_BITS 112  | 
36  | 0  | #define KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO 0xFFFFFFFF  | 
37  | 0  | #define KDF_PBKDF2_MIN_ITERATIONS 1000  | 
38  | 0  | #define KDF_PBKDF2_MIN_SALT_LEN   (128 / 8)  | 
39  |  |  | 
40  |  | static OSSL_FUNC_kdf_newctx_fn kdf_pbkdf2_new;  | 
41  |  | static OSSL_FUNC_kdf_dupctx_fn kdf_pbkdf2_dup;  | 
42  |  | static OSSL_FUNC_kdf_freectx_fn kdf_pbkdf2_free;  | 
43  |  | static OSSL_FUNC_kdf_reset_fn kdf_pbkdf2_reset;  | 
44  |  | static OSSL_FUNC_kdf_derive_fn kdf_pbkdf2_derive;  | 
45  |  | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf2_settable_ctx_params;  | 
46  |  | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf2_set_ctx_params;  | 
47  |  | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf2_gettable_ctx_params;  | 
48  |  | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf2_get_ctx_params;  | 
49  |  |  | 
50  |  | typedef struct { | 
51  |  |     void *provctx;  | 
52  |  |     unsigned char *pass;  | 
53  |  |     size_t pass_len;  | 
54  |  |     unsigned char *salt;  | 
55  |  |     size_t salt_len;  | 
56  |  |     uint64_t iter;  | 
57  |  |     PROV_DIGEST digest;  | 
58  |  |     int lower_bound_checks;  | 
59  |  |     OSSL_FIPS_IND_DECLARE  | 
60  |  | } KDF_PBKDF2;  | 
61  |  |  | 
62  |  | static int pbkdf2_derive(KDF_PBKDF2 *ctx, const char *pass, size_t passlen,  | 
63  |  |                          const unsigned char *salt, int saltlen, uint64_t iter,  | 
64  |  |                          const EVP_MD *digest, unsigned char *key,  | 
65  |  |                          size_t keylen, int lower_bound_checks);  | 
66  |  |  | 
67  |  | static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx);  | 
68  |  |  | 
69  |  | static void *kdf_pbkdf2_new_no_init(void *provctx)  | 
70  | 0  | { | 
71  | 0  |     KDF_PBKDF2 *ctx;  | 
72  |  | 
  | 
73  | 0  |     if (!ossl_prov_is_running())  | 
74  | 0  |         return NULL;  | 
75  |  |  | 
76  | 0  |     ctx = OPENSSL_zalloc(sizeof(*ctx));  | 
77  | 0  |     if (ctx == NULL)  | 
78  | 0  |         return NULL;  | 
79  | 0  |     ctx->provctx = provctx;  | 
80  | 0  |     OSSL_FIPS_IND_INIT(ctx);  | 
81  | 0  |     return ctx;  | 
82  | 0  | }  | 
83  |  |  | 
84  |  | static void *kdf_pbkdf2_new(void *provctx)  | 
85  | 0  | { | 
86  | 0  |     KDF_PBKDF2 *ctx = kdf_pbkdf2_new_no_init(provctx);  | 
87  |  | 
  | 
88  | 0  |     if (ctx != NULL)  | 
89  | 0  |         kdf_pbkdf2_init(ctx);  | 
90  | 0  |     return ctx;  | 
91  | 0  | }  | 
92  |  |  | 
93  |  | static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx)  | 
94  | 0  | { | 
95  | 0  |     ossl_prov_digest_reset(&ctx->digest);  | 
96  |  | #ifdef OPENSSL_PEDANTIC_ZEROIZATION  | 
97  |  |     OPENSSL_clear_free(ctx->salt, ctx->salt_len);  | 
98  |  | #else  | 
99  | 0  |     OPENSSL_free(ctx->salt);  | 
100  | 0  | #endif  | 
101  | 0  |     OPENSSL_clear_free(ctx->pass, ctx->pass_len);  | 
102  | 0  |     memset(ctx, 0, sizeof(*ctx));  | 
103  | 0  | }  | 
104  |  |  | 
105  |  | static void kdf_pbkdf2_free(void *vctx)  | 
106  | 0  | { | 
107  | 0  |     KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;  | 
108  |  | 
  | 
109  | 0  |     if (ctx != NULL) { | 
110  | 0  |         kdf_pbkdf2_cleanup(ctx);  | 
111  | 0  |         OPENSSL_free(ctx);  | 
112  | 0  |     }  | 
113  | 0  | }  | 
114  |  |  | 
115  |  | static void kdf_pbkdf2_reset(void *vctx)  | 
116  | 0  | { | 
117  | 0  |     KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;  | 
118  | 0  |     void *provctx = ctx->provctx;  | 
119  |  | 
  | 
120  | 0  |     kdf_pbkdf2_cleanup(ctx);  | 
121  | 0  |     ctx->provctx = provctx;  | 
122  | 0  |     kdf_pbkdf2_init(ctx);  | 
123  | 0  | }  | 
124  |  |  | 
125  |  | static void *kdf_pbkdf2_dup(void *vctx)  | 
126  | 0  | { | 
127  | 0  |     const KDF_PBKDF2 *src = (const KDF_PBKDF2 *)vctx;  | 
128  | 0  |     KDF_PBKDF2 *dest;  | 
129  |  |  | 
130  |  |     /* We need a new PBKDF2 object but uninitialised since we're filling it */  | 
131  | 0  |     dest = kdf_pbkdf2_new_no_init(src->provctx);  | 
132  | 0  |     if (dest != NULL) { | 
133  | 0  |         if (!ossl_prov_memdup(src->salt, src->salt_len,  | 
134  | 0  |                               &dest->salt, &dest->salt_len)  | 
135  | 0  |                 || !ossl_prov_memdup(src->pass, src->pass_len,  | 
136  | 0  |                                      &dest->pass, &dest->pass_len)  | 
137  | 0  |                 || !ossl_prov_digest_copy(&dest->digest, &src->digest))  | 
138  | 0  |             goto err;  | 
139  | 0  |         dest->iter = src->iter;  | 
140  | 0  |         dest->lower_bound_checks = src->lower_bound_checks;  | 
141  | 0  |         OSSL_FIPS_IND_COPY(dest, src)  | 
142  | 0  |     }  | 
143  | 0  |     return dest;  | 
144  |  |  | 
145  | 0  |  err:  | 
146  | 0  |     kdf_pbkdf2_free(dest);  | 
147  | 0  |     return NULL;  | 
148  | 0  | }  | 
149  |  |  | 
150  |  | static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx)  | 
151  | 0  | { | 
152  | 0  |     OSSL_PARAM param;  | 
153  | 0  |     OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);  | 
154  |  | 
  | 
155  | 0  |     param = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,  | 
156  | 0  |                                              SN_sha1, 0);  | 
157  | 0  |     if (!ossl_prov_digest_load(&ctx->digest, ¶m, NULL, NULL, provctx))  | 
158  |  |         /* This is an error, but there is no way to indicate such directly */  | 
159  | 0  |         ossl_prov_digest_reset(&ctx->digest);  | 
160  | 0  |     ctx->iter = PKCS5_DEFAULT_ITER;  | 
161  |  | #ifdef FIPS_MODULE  | 
162  |  |     ctx->lower_bound_checks = 1;  | 
163  |  | #else  | 
164  | 0  |     ctx->lower_bound_checks = 0;  | 
165  | 0  | #endif  | 
166  | 0  | }  | 
167  |  |  | 
168  |  | static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen,  | 
169  |  |                              const OSSL_PARAM *p)  | 
170  | 0  | { | 
171  | 0  |     OPENSSL_clear_free(*buffer, *buflen);  | 
172  | 0  |     *buffer = NULL;  | 
173  | 0  |     *buflen = 0;  | 
174  |  | 
  | 
175  | 0  |     if (p->data_size == 0) { | 
176  | 0  |         if ((*buffer = OPENSSL_malloc(1)) == NULL)  | 
177  | 0  |             return 0;  | 
178  | 0  |     } else if (p->data != NULL) { | 
179  | 0  |         if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))  | 
180  | 0  |             return 0;  | 
181  | 0  |     }  | 
182  | 0  |     return 1;  | 
183  | 0  | }  | 
184  |  |  | 
185  |  | static int pbkdf2_lower_bound_check_passed(int saltlen, uint64_t iter,  | 
186  |  |                                            size_t keylen, int *error,  | 
187  |  |                                            const char **desc)  | 
188  | 0  | { | 
189  | 0  |     if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) { | 
190  | 0  |         *error = PROV_R_KEY_SIZE_TOO_SMALL;  | 
191  | 0  |         if (desc != NULL)  | 
192  | 0  |             *desc = "Key size";  | 
193  | 0  |         return 0;  | 
194  | 0  |     }  | 
195  | 0  |     if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) { | 
196  | 0  |         *error = PROV_R_INVALID_SALT_LENGTH;  | 
197  | 0  |         if (desc != NULL)  | 
198  | 0  |             *desc = "Salt size";  | 
199  | 0  |         return 0;  | 
200  | 0  |     }  | 
201  | 0  |     if (iter < KDF_PBKDF2_MIN_ITERATIONS) { | 
202  | 0  |         *error = PROV_R_INVALID_ITERATION_COUNT;  | 
203  | 0  |         if (desc != NULL)  | 
204  | 0  |             *desc = "Iteration count";  | 
205  | 0  |         return 0;  | 
206  | 0  |     }  | 
207  |  |  | 
208  | 0  |     return 1;  | 
209  | 0  | }  | 
210  |  |  | 
211  |  | #ifdef FIPS_MODULE  | 
212  |  | static int fips_lower_bound_check_passed(KDF_PBKDF2 *ctx, int saltlen,  | 
213  |  |                                          uint64_t iter, size_t keylen)  | 
214  |  | { | 
215  |  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
216  |  |     int error = 0;  | 
217  |  |     const char *desc = NULL;  | 
218  |  |     int approved = pbkdf2_lower_bound_check_passed(saltlen, iter, keylen,  | 
219  |  |                                                    &error, &desc);  | 
220  |  |  | 
221  |  |     if (!approved) { | 
222  |  |         if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, libctx,  | 
223  |  |                                          "PBKDF2", desc,  | 
224  |  |                                          ossl_fips_config_pbkdf2_lower_bound_check)) { | 
225  |  |             ERR_raise(ERR_LIB_PROV, error);  | 
226  |  |             return 0;  | 
227  |  |         }  | 
228  |  |     }  | 
229  |  |     return 1;  | 
230  |  | }  | 
231  |  | #endif  | 
232  |  |  | 
233  |  | static int lower_bound_check_passed(KDF_PBKDF2 *ctx, int saltlen, uint64_t iter,  | 
234  |  |                                     size_t keylen, int lower_bound_checks)  | 
235  | 0  | { | 
236  |  | #ifdef FIPS_MODULE  | 
237  |  |     if (!fips_lower_bound_check_passed(ctx, saltlen, iter, keylen))  | 
238  |  |         return 0;  | 
239  |  | #else  | 
240  | 0  |     if (lower_bound_checks) { | 
241  | 0  |         int error = 0;  | 
242  | 0  |         int passed = pbkdf2_lower_bound_check_passed(saltlen, iter, keylen,  | 
243  | 0  |                                                      &error, NULL);  | 
244  |  | 
  | 
245  | 0  |         if (!passed) { | 
246  | 0  |             ERR_raise(ERR_LIB_PROV, error);  | 
247  | 0  |             return 0;  | 
248  | 0  |         }  | 
249  | 0  |     } else if (iter < 1) { | 
250  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);  | 
251  | 0  |         return 0;  | 
252  | 0  |     }  | 
253  | 0  | #endif  | 
254  |  |  | 
255  | 0  |     return 1;  | 
256  | 0  | }  | 
257  |  |  | 
258  |  | static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen,  | 
259  |  |                              const OSSL_PARAM params[])  | 
260  | 0  | { | 
261  | 0  |     KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;  | 
262  | 0  |     const EVP_MD *md;  | 
263  |  | 
  | 
264  | 0  |     if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params))  | 
265  | 0  |         return 0;  | 
266  |  |  | 
267  | 0  |     if (ctx->pass == NULL) { | 
268  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);  | 
269  | 0  |         return 0;  | 
270  | 0  |     }  | 
271  |  |  | 
272  | 0  |     if (ctx->salt == NULL) { | 
273  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);  | 
274  | 0  |         return 0;  | 
275  | 0  |     }  | 
276  |  |  | 
277  | 0  |     md = ossl_prov_digest_md(&ctx->digest);  | 
278  | 0  |     return pbkdf2_derive(ctx, (char *)ctx->pass, ctx->pass_len,  | 
279  | 0  |                          ctx->salt, (int)ctx->salt_len, ctx->iter,  | 
280  | 0  |                          md, key, keylen, ctx->lower_bound_checks);  | 
281  | 0  | }  | 
282  |  |  | 
283  |  | static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])  | 
284  | 0  | { | 
285  | 0  |     struct pbkdf2_set_ctx_params_st p;  | 
286  | 0  |     KDF_PBKDF2 *ctx = vctx;  | 
287  | 0  |     OSSL_LIB_CTX *provctx;  | 
288  | 0  |     int pkcs5;  | 
289  | 0  |     uint64_t iter;  | 
290  | 0  |     const EVP_MD *md;  | 
291  |  | 
  | 
292  | 0  |     if (ctx == NULL || !pbkdf2_set_ctx_params_decoder(params, &p))  | 
293  | 0  |         return 0;  | 
294  |  |  | 
295  | 0  |     provctx = PROV_LIBCTX_OF(ctx->provctx);  | 
296  |  | 
  | 
297  | 0  |     if (p.digest != NULL) { | 
298  | 0  |         if (!ossl_prov_digest_load(&ctx->digest, p.digest,  | 
299  | 0  |                                    p.propq, p.engine, provctx))  | 
300  | 0  |             return 0;  | 
301  | 0  |         md = ossl_prov_digest_md(&ctx->digest);  | 
302  | 0  |         if (EVP_MD_xof(md)) { | 
303  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);  | 
304  | 0  |             return 0;  | 
305  | 0  |         }  | 
306  | 0  |     }  | 
307  |  |  | 
308  | 0  |     if (p.pkcs5 != NULL) { | 
309  | 0  |         if (!OSSL_PARAM_get_int(p.pkcs5, &pkcs5))  | 
310  | 0  |             return 0;  | 
311  | 0  |         ctx->lower_bound_checks = pkcs5 == 0;  | 
312  |  | #ifdef FIPS_MODULE  | 
313  |  |         ossl_FIPS_IND_set_settable(OSSL_FIPS_IND_GET(ctx),  | 
314  |  |                                    OSSL_FIPS_IND_SETTABLE0,  | 
315  |  |                                    ctx->lower_bound_checks);  | 
316  |  | #endif  | 
317  | 0  |     }  | 
318  |  |  | 
319  | 0  |     if (p.pw != NULL && !pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p.pw))  | 
320  | 0  |             return 0;  | 
321  |  |  | 
322  | 0  |     if (p.salt != NULL) { | 
323  | 0  |         if (!lower_bound_check_passed(ctx, (int)p.salt->data_size, UINT64_MAX, SIZE_MAX,  | 
324  | 0  |                                       ctx->lower_bound_checks))  | 
325  | 0  |             return 0;  | 
326  | 0  |         if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len, p.salt))  | 
327  | 0  |             return 0;  | 
328  | 0  |     }  | 
329  |  |  | 
330  | 0  |     if (p.iter != NULL) { | 
331  | 0  |         if (!OSSL_PARAM_get_uint64(p.iter, &iter))  | 
332  | 0  |             return 0;  | 
333  | 0  |         if (!lower_bound_check_passed(ctx, INT_MAX, iter, SIZE_MAX,  | 
334  | 0  |                                       ctx->lower_bound_checks))  | 
335  | 0  |             return 0;  | 
336  | 0  |         ctx->iter = iter;  | 
337  | 0  |     }  | 
338  | 0  |     return 1;  | 
339  | 0  | }  | 
340  |  |  | 
341  |  | static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx,  | 
342  |  |                                                         ossl_unused void *p_ctx)  | 
343  | 0  | { | 
344  | 0  |     return pbkdf2_set_ctx_params_list;  | 
345  | 0  | }  | 
346  |  |  | 
347  |  | static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[])  | 
348  | 0  | { | 
349  | 0  |     KDF_PBKDF2 *ctx = vctx;  | 
350  | 0  |     struct pbkdf2_get_ctx_params_st p;  | 
351  |  | 
  | 
352  | 0  |     if (ctx == NULL || !pbkdf2_get_ctx_params_decoder(params, &p))  | 
353  | 0  |         return 0;  | 
354  |  |  | 
355  | 0  |     if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX))  | 
356  | 0  |             return 0;  | 
357  |  |  | 
358  | 0  |     if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))  | 
359  | 0  |         return 0;  | 
360  | 0  |     return 1;  | 
361  | 0  | }  | 
362  |  |  | 
363  |  | static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx,  | 
364  |  |                                                         ossl_unused void *p_ctx)  | 
365  | 0  | { | 
366  | 0  |     return pbkdf2_get_ctx_params_list;  | 
367  | 0  | }  | 
368  |  |  | 
369  |  | const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = { | 
370  |  |     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new }, | 
371  |  |     { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pbkdf2_dup }, | 
372  |  |     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free }, | 
373  |  |     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset }, | 
374  |  |     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf2_derive }, | 
375  |  |     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | 
376  |  |       (void(*)(void))kdf_pbkdf2_settable_ctx_params },  | 
377  |  |     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_set_ctx_params }, | 
378  |  |     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | 
379  |  |       (void(*)(void))kdf_pbkdf2_gettable_ctx_params },  | 
380  |  |     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_get_ctx_params }, | 
381  |  |     OSSL_DISPATCH_END  | 
382  |  | };  | 
383  |  |  | 
384  |  | /*  | 
385  |  |  * This is an implementation of PKCS#5 v2.0 password based encryption key  | 
386  |  |  * derivation function PBKDF2. SHA1 version verified against test vectors  | 
387  |  |  * posted by Peter Gutmann to the PKCS-TNG mailing list.  | 
388  |  |  *  | 
389  |  |  * The constraints specified by SP800-132 have been added i.e.  | 
390  |  |  *  - Check the range of the key length.  | 
391  |  |  *  - Minimum iteration count of 1000.  | 
392  |  |  *  - Randomly-generated portion of the salt shall be at least 128 bits.  | 
393  |  |  */  | 
394  |  | static int pbkdf2_derive(KDF_PBKDF2 *ctx, const char *pass, size_t passlen,  | 
395  |  |                          const unsigned char *salt, int saltlen, uint64_t iter,  | 
396  |  |                          const EVP_MD *digest, unsigned char *key,  | 
397  |  |                          size_t keylen, int lower_bound_checks)  | 
398  | 0  | { | 
399  | 0  |     int ret = 0;  | 
400  | 0  |     unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];  | 
401  | 0  |     int cplen, k, tkeylen, mdlen;  | 
402  | 0  |     uint64_t j;  | 
403  | 0  |     unsigned long i = 1;  | 
404  | 0  |     HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;  | 
405  |  | 
  | 
406  | 0  |     mdlen = EVP_MD_get_size(digest);  | 
407  | 0  |     if (mdlen <= 0)  | 
408  | 0  |         return 0;  | 
409  |  |  | 
410  |  |     /*  | 
411  |  |      * This check should always be done because keylen / mdlen >= (2^32 - 1)  | 
412  |  |      * results in an overflow of the loop counter 'i'.  | 
413  |  |      */  | 
414  | 0  |     if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) { | 
415  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
416  | 0  |         return 0;  | 
417  | 0  |     }  | 
418  |  |  | 
419  | 0  |     if (!lower_bound_check_passed(ctx, saltlen, iter, keylen, lower_bound_checks))  | 
420  | 0  |         return 0;  | 
421  |  |  | 
422  | 0  |     hctx_tpl = HMAC_CTX_new();  | 
423  | 0  |     if (hctx_tpl == NULL)  | 
424  | 0  |         return 0;  | 
425  | 0  |     p = key;  | 
426  | 0  |     tkeylen = (int)keylen;  | 
427  | 0  |     if (!HMAC_Init_ex(hctx_tpl, pass, (int)passlen, digest, NULL))  | 
428  | 0  |         goto err;  | 
429  | 0  |     hctx = HMAC_CTX_new();  | 
430  | 0  |     if (hctx == NULL)  | 
431  | 0  |         goto err;  | 
432  | 0  |     while (tkeylen) { | 
433  | 0  |         if (tkeylen > mdlen)  | 
434  | 0  |             cplen = mdlen;  | 
435  | 0  |         else  | 
436  | 0  |             cplen = tkeylen;  | 
437  |  |         /*  | 
438  |  |          * We are unlikely to ever use more than 256 blocks (5120 bits!) but  | 
439  |  |          * just in case...  | 
440  |  |          */  | 
441  | 0  |         itmp[0] = (unsigned char)((i >> 24) & 0xff);  | 
442  | 0  |         itmp[1] = (unsigned char)((i >> 16) & 0xff);  | 
443  | 0  |         itmp[2] = (unsigned char)((i >> 8) & 0xff);  | 
444  | 0  |         itmp[3] = (unsigned char)(i & 0xff);  | 
445  | 0  |         if (!HMAC_CTX_copy(hctx, hctx_tpl))  | 
446  | 0  |             goto err;  | 
447  | 0  |         if (!HMAC_Update(hctx, salt, saltlen)  | 
448  | 0  |                 || !HMAC_Update(hctx, itmp, 4)  | 
449  | 0  |                 || !HMAC_Final(hctx, digtmp, NULL))  | 
450  | 0  |             goto err;  | 
451  | 0  |         memcpy(p, digtmp, cplen);  | 
452  | 0  |         for (j = 1; j < iter; j++) { | 
453  | 0  |             if (!HMAC_CTX_copy(hctx, hctx_tpl))  | 
454  | 0  |                 goto err;  | 
455  | 0  |             if (!HMAC_Update(hctx, digtmp, mdlen)  | 
456  | 0  |                     || !HMAC_Final(hctx, digtmp, NULL))  | 
457  | 0  |                 goto err;  | 
458  | 0  |             for (k = 0; k < cplen; k++)  | 
459  | 0  |                 p[k] ^= digtmp[k];  | 
460  | 0  |         }  | 
461  | 0  |         tkeylen -= cplen;  | 
462  | 0  |         i++;  | 
463  | 0  |         p += cplen;  | 
464  | 0  |     }  | 
465  | 0  |     ret = 1;  | 
466  |  | 
  | 
467  | 0  | err:  | 
468  | 0  |     HMAC_CTX_free(hctx);  | 
469  | 0  |     HMAC_CTX_free(hctx_tpl);  | 
470  | 0  |     return ret;  | 
471  | 0  | }  |