Coverage Report

Created: 2025-08-03 07:12

/src/openssl/providers/implementations/kdfs/pbkdf2.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2018-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
11
/*
12
 * HMAC low level APIs are deprecated for public use, but still ok for internal
13
 * use.
14
 */
15
#include "internal/deprecated.h"
16
17
#include <stdlib.h>
18
#include <stdarg.h>
19
#include <string.h>
20
#include <openssl/hmac.h>
21
#include <openssl/evp.h>
22
#include <openssl/kdf.h>
23
#include <openssl/core_names.h>
24
#include <openssl/proverr.h>
25
#include "internal/cryptlib.h"
26
#include "internal/numbers.h"
27
#include "crypto/evp.h"
28
#include "prov/provider_ctx.h"
29
#include "prov/providercommon.h"
30
#include "prov/implementations.h"
31
#include "prov/provider_util.h"
32
#include "prov/securitycheck.h"
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 params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
153
0
    OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
154
155
0
    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
156
0
                                                 SN_sha1, 0);
157
0
    if (!ossl_prov_digest_load_from_params(&ctx->digest, params, 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
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
284
#ifndef pbkdf2_set_ctx_params_list
285
static const OSSL_PARAM pbkdf2_set_ctx_params_list[] = {
286
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
287
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
288
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
289
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
290
    OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
291
    OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL),
292
    OSSL_PARAM_END
293
};
294
#endif
295
296
#ifndef pbkdf2_set_ctx_params_st
297
struct pbkdf2_set_ctx_params_st {
298
    OSSL_PARAM *digest;
299
    OSSL_PARAM *engine;
300
    OSSL_PARAM *iter;
301
    OSSL_PARAM *pkcs5;
302
    OSSL_PARAM *propq;
303
    OSSL_PARAM *pw;
304
    OSSL_PARAM *salt;
305
};
306
#endif
307
308
#ifndef pbkdf2_set_ctx_params_decoder
309
static int pbkdf2_set_ctx_params_decoder
310
    (const OSSL_PARAM *p, struct pbkdf2_set_ctx_params_st *r)
311
0
{
312
0
    const char *s;
313
314
0
    memset(r, 0, sizeof(*r));
315
0
    if (p != NULL)
316
0
        for (; (s = p->key) != NULL; p++)
317
0
            switch(s[0]) {
318
0
            default:
319
0
                break;
320
0
            case 'd':
321
0
                if (ossl_likely(strcmp("igest", s + 1) == 0)) {
322
0
                    if (ossl_likely(r->digest == NULL))
323
0
                        r->digest = (OSSL_PARAM *)p;
324
0
                }
325
0
                break;
326
0
            case 'e':
327
0
                if (ossl_likely(strcmp("ngine", s + 1) == 0)) {
328
0
                    if (ossl_likely(r->engine == NULL))
329
0
                        r->engine = (OSSL_PARAM *)p;
330
0
                }
331
0
                break;
332
0
            case 'i':
333
0
                if (ossl_likely(strcmp("ter", s + 1) == 0)) {
334
0
                    if (ossl_likely(r->iter == NULL))
335
0
                        r->iter = (OSSL_PARAM *)p;
336
0
                }
337
0
                break;
338
0
            case 'p':
339
0
                switch(s[1]) {
340
0
                default:
341
0
                    break;
342
0
                case 'a':
343
0
                    if (ossl_likely(strcmp("ss", s + 2) == 0)) {
344
0
                        if (ossl_likely(r->pw == NULL))
345
0
                            r->pw = (OSSL_PARAM *)p;
346
0
                    }
347
0
                    break;
348
0
                case 'k':
349
0
                    if (ossl_likely(strcmp("cs5", s + 2) == 0)) {
350
0
                        if (ossl_likely(r->pkcs5 == NULL))
351
0
                            r->pkcs5 = (OSSL_PARAM *)p;
352
0
                    }
353
0
                    break;
354
0
                case 'r':
355
0
                    if (ossl_likely(strcmp("operties", s + 2) == 0)) {
356
0
                        if (ossl_likely(r->propq == NULL))
357
0
                            r->propq = (OSSL_PARAM *)p;
358
0
                    }
359
0
                }
360
0
                break;
361
0
            case 's':
362
0
                if (ossl_likely(strcmp("alt", s + 1) == 0)) {
363
0
                    if (ossl_likely(r->salt == NULL))
364
0
                        r->salt = (OSSL_PARAM *)p;
365
0
                }
366
0
            }
367
0
    return 1;
368
0
}
369
#endif
370
/* End of machine generated */
371
372
static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
373
0
{
374
0
    struct pbkdf2_set_ctx_params_st p;
375
0
    KDF_PBKDF2 *ctx = vctx;
376
0
    OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
377
0
    int pkcs5;
378
0
    uint64_t iter;
379
0
    const EVP_MD *md;
380
381
0
    if (ctx == NULL || !pbkdf2_set_ctx_params_decoder(params, &p))
382
0
        return 0;
383
384
0
    if (p.digest != NULL) {
385
0
        if (!ossl_prov_digest_load(&ctx->digest, p.digest,
386
0
                                   p.propq, p.engine, provctx))
387
0
            return 0;
388
0
        md = ossl_prov_digest_md(&ctx->digest);
389
0
        if (EVP_MD_xof(md)) {
390
0
            ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
391
0
            return 0;
392
0
        }
393
0
    }
394
395
0
    if (p.pkcs5 != NULL) {
396
0
        if (!OSSL_PARAM_get_int(p.pkcs5, &pkcs5))
397
0
            return 0;
398
0
        ctx->lower_bound_checks = pkcs5 == 0;
399
#ifdef FIPS_MODULE
400
        ossl_FIPS_IND_set_settable(OSSL_FIPS_IND_GET(ctx),
401
                                   OSSL_FIPS_IND_SETTABLE0,
402
                                   ctx->lower_bound_checks);
403
#endif
404
0
    }
405
406
0
    if (p.pw != NULL && !pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p.pw))
407
0
            return 0;
408
409
0
    if (p.salt != NULL) {
410
0
        if (!lower_bound_check_passed(ctx, (int)p.salt->data_size, UINT64_MAX, SIZE_MAX,
411
0
                                      ctx->lower_bound_checks))
412
0
            return 0;
413
0
        if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len, p.salt))
414
0
            return 0;
415
0
    }
416
417
0
    if (p.iter != NULL) {
418
0
        if (!OSSL_PARAM_get_uint64(p.iter, &iter))
419
0
            return 0;
420
0
        if (!lower_bound_check_passed(ctx, INT_MAX, iter, SIZE_MAX,
421
0
                                      ctx->lower_bound_checks))
422
0
            return 0;
423
0
        ctx->iter = iter;
424
0
    }
425
0
    return 1;
426
0
}
427
428
static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx,
429
                                                        ossl_unused void *p_ctx)
430
0
{
431
0
    return pbkdf2_set_ctx_params_list;
432
0
}
433
434
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
435
#ifndef pbkdf2_get_ctx_params_list
436
static const OSSL_PARAM pbkdf2_get_ctx_params_list[] = {
437
    OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
438
    OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR, NULL),
439
    OSSL_PARAM_END
440
};
441
#endif
442
443
#ifndef pbkdf2_get_ctx_params_st
444
struct pbkdf2_get_ctx_params_st {
445
    OSSL_PARAM *ind;
446
    OSSL_PARAM *size;
447
};
448
#endif
449
450
#ifndef pbkdf2_get_ctx_params_decoder
451
static int pbkdf2_get_ctx_params_decoder
452
    (const OSSL_PARAM *p, struct pbkdf2_get_ctx_params_st *r)
453
0
{
454
0
    const char *s;
455
456
0
    memset(r, 0, sizeof(*r));
457
0
    if (p != NULL)
458
0
        for (; (s = p->key) != NULL; p++)
459
0
            switch(s[0]) {
460
0
            default:
461
0
                break;
462
0
            case 'f':
463
0
                if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) {
464
0
                    if (ossl_likely(r->ind == NULL))
465
0
                        r->ind = (OSSL_PARAM *)p;
466
0
                }
467
0
                break;
468
0
            case 's':
469
0
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
470
0
                    if (ossl_likely(r->size == NULL))
471
0
                        r->size = (OSSL_PARAM *)p;
472
0
                }
473
0
            }
474
0
    return 1;
475
0
}
476
#endif
477
/* End of machine generated */
478
479
static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[])
480
0
{
481
0
    KDF_PBKDF2 *ctx = vctx;
482
0
    struct pbkdf2_get_ctx_params_st p;
483
484
0
    if (ctx == NULL || !pbkdf2_get_ctx_params_decoder(params, &p))
485
0
        return 0;
486
487
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX))
488
0
            return 0;
489
490
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))
491
0
        return 0;
492
0
    return 1;
493
0
}
494
495
static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx,
496
                                                        ossl_unused void *p_ctx)
497
0
{
498
0
    return pbkdf2_get_ctx_params_list;
499
0
}
500
501
const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = {
502
    { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new },
503
    { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pbkdf2_dup },
504
    { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free },
505
    { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset },
506
    { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf2_derive },
507
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
508
      (void(*)(void))kdf_pbkdf2_settable_ctx_params },
509
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_set_ctx_params },
510
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
511
      (void(*)(void))kdf_pbkdf2_gettable_ctx_params },
512
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_get_ctx_params },
513
    OSSL_DISPATCH_END
514
};
515
516
/*
517
 * This is an implementation of PKCS#5 v2.0 password based encryption key
518
 * derivation function PBKDF2. SHA1 version verified against test vectors
519
 * posted by Peter Gutmann to the PKCS-TNG mailing list.
520
 *
521
 * The constraints specified by SP800-132 have been added i.e.
522
 *  - Check the range of the key length.
523
 *  - Minimum iteration count of 1000.
524
 *  - Randomly-generated portion of the salt shall be at least 128 bits.
525
 */
526
static int pbkdf2_derive(KDF_PBKDF2 *ctx, const char *pass, size_t passlen,
527
                         const unsigned char *salt, int saltlen, uint64_t iter,
528
                         const EVP_MD *digest, unsigned char *key,
529
                         size_t keylen, int lower_bound_checks)
530
0
{
531
0
    int ret = 0;
532
0
    unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
533
0
    int cplen, k, tkeylen, mdlen;
534
0
    uint64_t j;
535
0
    unsigned long i = 1;
536
0
    HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
537
538
0
    mdlen = EVP_MD_get_size(digest);
539
0
    if (mdlen <= 0)
540
0
        return 0;
541
542
    /*
543
     * This check should always be done because keylen / mdlen >= (2^32 - 1)
544
     * results in an overflow of the loop counter 'i'.
545
     */
546
0
    if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) {
547
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
548
0
        return 0;
549
0
    }
550
551
0
    if (!lower_bound_check_passed(ctx, saltlen, iter, keylen, lower_bound_checks))
552
0
        return 0;
553
554
0
    hctx_tpl = HMAC_CTX_new();
555
0
    if (hctx_tpl == NULL)
556
0
        return 0;
557
0
    p = key;
558
0
    tkeylen = (int)keylen;
559
0
    if (!HMAC_Init_ex(hctx_tpl, pass, (int)passlen, digest, NULL))
560
0
        goto err;
561
0
    hctx = HMAC_CTX_new();
562
0
    if (hctx == NULL)
563
0
        goto err;
564
0
    while (tkeylen) {
565
0
        if (tkeylen > mdlen)
566
0
            cplen = mdlen;
567
0
        else
568
0
            cplen = tkeylen;
569
        /*
570
         * We are unlikely to ever use more than 256 blocks (5120 bits!) but
571
         * just in case...
572
         */
573
0
        itmp[0] = (unsigned char)((i >> 24) & 0xff);
574
0
        itmp[1] = (unsigned char)((i >> 16) & 0xff);
575
0
        itmp[2] = (unsigned char)((i >> 8) & 0xff);
576
0
        itmp[3] = (unsigned char)(i & 0xff);
577
0
        if (!HMAC_CTX_copy(hctx, hctx_tpl))
578
0
            goto err;
579
0
        if (!HMAC_Update(hctx, salt, saltlen)
580
0
                || !HMAC_Update(hctx, itmp, 4)
581
0
                || !HMAC_Final(hctx, digtmp, NULL))
582
0
            goto err;
583
0
        memcpy(p, digtmp, cplen);
584
0
        for (j = 1; j < iter; j++) {
585
0
            if (!HMAC_CTX_copy(hctx, hctx_tpl))
586
0
                goto err;
587
0
            if (!HMAC_Update(hctx, digtmp, mdlen)
588
0
                    || !HMAC_Final(hctx, digtmp, NULL))
589
0
                goto err;
590
0
            for (k = 0; k < cplen; k++)
591
0
                p[k] ^= digtmp[k];
592
0
        }
593
0
        tkeylen -= cplen;
594
0
        i++;
595
0
        p += cplen;
596
0
    }
597
0
    ret = 1;
598
599
0
err:
600
0
    HMAC_CTX_free(hctx);
601
0
    HMAC_CTX_free(hctx_tpl);
602
0
    return ret;
603
0
}