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