/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 | | * 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 "pbkdf2.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 | 0 | ctx->lower_bound_checks = ossl_kdf_pbkdf2_default_checks; |
162 | 0 | } |
163 | | |
164 | | static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen, |
165 | | const OSSL_PARAM *p) |
166 | 0 | { |
167 | 0 | OPENSSL_clear_free(*buffer, *buflen); |
168 | 0 | *buffer = NULL; |
169 | 0 | *buflen = 0; |
170 | |
|
171 | 0 | if (p->data_size == 0) { |
172 | 0 | if ((*buffer = OPENSSL_malloc(1)) == NULL) |
173 | 0 | return 0; |
174 | 0 | } else if (p->data != NULL) { |
175 | 0 | if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen)) |
176 | 0 | return 0; |
177 | 0 | } |
178 | 0 | return 1; |
179 | 0 | } |
180 | | |
181 | | static int pbkdf2_lower_bound_check_passed(int saltlen, uint64_t iter, |
182 | | size_t keylen, int *error, |
183 | | const char **desc) |
184 | 0 | { |
185 | 0 | if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) { |
186 | 0 | *error = PROV_R_KEY_SIZE_TOO_SMALL; |
187 | 0 | if (desc != NULL) |
188 | 0 | *desc = "Key size"; |
189 | 0 | return 0; |
190 | 0 | } |
191 | 0 | if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) { |
192 | 0 | *error = PROV_R_INVALID_SALT_LENGTH; |
193 | 0 | if (desc != NULL) |
194 | 0 | *desc = "Salt size"; |
195 | 0 | return 0; |
196 | 0 | } |
197 | 0 | if (iter < KDF_PBKDF2_MIN_ITERATIONS) { |
198 | 0 | *error = PROV_R_INVALID_ITERATION_COUNT; |
199 | 0 | if (desc != NULL) |
200 | 0 | *desc = "Iteration count"; |
201 | 0 | return 0; |
202 | 0 | } |
203 | | |
204 | 0 | return 1; |
205 | 0 | } |
206 | | |
207 | | #ifdef FIPS_MODULE |
208 | | static int fips_lower_bound_check_passed(KDF_PBKDF2 *ctx, int saltlen, |
209 | | uint64_t iter, size_t keylen) |
210 | | { |
211 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
212 | | int error = 0; |
213 | | const char *desc = NULL; |
214 | | int approved = pbkdf2_lower_bound_check_passed(saltlen, iter, keylen, |
215 | | &error, &desc); |
216 | | |
217 | | if (!approved) { |
218 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, libctx, |
219 | | "PBKDF2", desc, |
220 | | ossl_fips_config_pbkdf2_lower_bound_check)) { |
221 | | ERR_raise(ERR_LIB_PROV, error); |
222 | | return 0; |
223 | | } |
224 | | } |
225 | | return 1; |
226 | | } |
227 | | #endif |
228 | | |
229 | | static int lower_bound_check_passed(KDF_PBKDF2 *ctx, int saltlen, uint64_t iter, |
230 | | size_t keylen, int lower_bound_checks) |
231 | 0 | { |
232 | | #ifdef FIPS_MODULE |
233 | | if (!fips_lower_bound_check_passed(ctx, saltlen, iter, keylen)) |
234 | | return 0; |
235 | | #else |
236 | 0 | if (lower_bound_checks) { |
237 | 0 | int error = 0; |
238 | 0 | int passed = pbkdf2_lower_bound_check_passed(saltlen, iter, keylen, |
239 | 0 | &error, NULL); |
240 | |
|
241 | 0 | if (!passed) { |
242 | 0 | ERR_raise(ERR_LIB_PROV, error); |
243 | 0 | return 0; |
244 | 0 | } |
245 | 0 | } else if (iter < 1) { |
246 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT); |
247 | 0 | return 0; |
248 | 0 | } |
249 | 0 | #endif |
250 | | |
251 | 0 | return 1; |
252 | 0 | } |
253 | | |
254 | | static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen, |
255 | | const OSSL_PARAM params[]) |
256 | 0 | { |
257 | 0 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
258 | 0 | const EVP_MD *md; |
259 | |
|
260 | 0 | if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params)) |
261 | 0 | return 0; |
262 | | |
263 | 0 | if (ctx->pass == NULL) { |
264 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
265 | 0 | return 0; |
266 | 0 | } |
267 | | |
268 | 0 | if (ctx->salt == NULL) { |
269 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT); |
270 | 0 | return 0; |
271 | 0 | } |
272 | | |
273 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
274 | 0 | return pbkdf2_derive(ctx, (char *)ctx->pass, ctx->pass_len, |
275 | 0 | ctx->salt, ctx->salt_len, ctx->iter, |
276 | 0 | md, key, keylen, ctx->lower_bound_checks); |
277 | 0 | } |
278 | | |
279 | | static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
280 | 0 | { |
281 | 0 | const OSSL_PARAM *p; |
282 | 0 | KDF_PBKDF2 *ctx = vctx; |
283 | 0 | OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); |
284 | 0 | int pkcs5; |
285 | 0 | uint64_t iter; |
286 | 0 | const EVP_MD *md; |
287 | |
|
288 | 0 | if (ossl_param_is_empty(params)) |
289 | 0 | return 1; |
290 | | |
291 | 0 | if (OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST) != NULL) { |
292 | 0 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx)) |
293 | 0 | return 0; |
294 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
295 | 0 | if (EVP_MD_xof(md)) { |
296 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
297 | 0 | return 0; |
298 | 0 | } |
299 | 0 | } |
300 | | |
301 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS5)) != NULL) { |
302 | 0 | if (!OSSL_PARAM_get_int(p, &pkcs5)) |
303 | 0 | return 0; |
304 | 0 | ctx->lower_bound_checks = pkcs5 == 0; |
305 | | #ifdef FIPS_MODULE |
306 | | ossl_FIPS_IND_set_settable(OSSL_FIPS_IND_GET(ctx), |
307 | | OSSL_FIPS_IND_SETTABLE0, |
308 | | ctx->lower_bound_checks); |
309 | | #endif |
310 | 0 | } |
311 | | |
312 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL) |
313 | 0 | if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p)) |
314 | 0 | return 0; |
315 | | |
316 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) { |
317 | 0 | if (!lower_bound_check_passed(ctx, p->data_size, UINT64_MAX, SIZE_MAX, |
318 | 0 | ctx->lower_bound_checks)) |
319 | 0 | return 0; |
320 | 0 | if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len, p)) |
321 | 0 | return 0; |
322 | 0 | } |
323 | | |
324 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) { |
325 | 0 | if (!OSSL_PARAM_get_uint64(p, &iter)) |
326 | 0 | return 0; |
327 | 0 | if (!lower_bound_check_passed(ctx, INT_MAX, iter, SIZE_MAX, |
328 | 0 | ctx->lower_bound_checks)) |
329 | 0 | return 0; |
330 | 0 | ctx->iter = iter; |
331 | 0 | } |
332 | 0 | return 1; |
333 | 0 | } |
334 | | |
335 | | static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx, |
336 | | ossl_unused void *p_ctx) |
337 | 0 | { |
338 | 0 | static const OSSL_PARAM known_settable_ctx_params[] = { |
339 | 0 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), |
340 | 0 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), |
341 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0), |
342 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0), |
343 | 0 | OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL), |
344 | 0 | OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL), |
345 | 0 | OSSL_PARAM_END |
346 | 0 | }; |
347 | 0 | return known_settable_ctx_params; |
348 | 0 | } |
349 | | |
350 | | static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
351 | 0 | { |
352 | 0 | OSSL_PARAM *p; |
353 | |
|
354 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) |
355 | 0 | if (!OSSL_PARAM_set_size_t(p, SIZE_MAX)) |
356 | 0 | return 0; |
357 | | |
358 | 0 | if (!OSSL_FIPS_IND_GET_CTX_PARAM((KDF_PBKDF2 *) vctx, params)) |
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 | static const OSSL_PARAM known_gettable_ctx_params[] = { |
367 | 0 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
368 | 0 | OSSL_FIPS_IND_GETTABLE_CTX_PARAM() |
369 | 0 | OSSL_PARAM_END |
370 | 0 | }; |
371 | 0 | return known_gettable_ctx_params; |
372 | 0 | } |
373 | | |
374 | | const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = { |
375 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new }, |
376 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pbkdf2_dup }, |
377 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free }, |
378 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset }, |
379 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf2_derive }, |
380 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
381 | | (void(*)(void))kdf_pbkdf2_settable_ctx_params }, |
382 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_set_ctx_params }, |
383 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
384 | | (void(*)(void))kdf_pbkdf2_gettable_ctx_params }, |
385 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_get_ctx_params }, |
386 | | OSSL_DISPATCH_END |
387 | | }; |
388 | | |
389 | | /* |
390 | | * This is an implementation of PKCS#5 v2.0 password based encryption key |
391 | | * derivation function PBKDF2. SHA1 version verified against test vectors |
392 | | * posted by Peter Gutmann to the PKCS-TNG mailing list. |
393 | | * |
394 | | * The constraints specified by SP800-132 have been added i.e. |
395 | | * - Check the range of the key length. |
396 | | * - Minimum iteration count of 1000. |
397 | | * - Randomly-generated portion of the salt shall be at least 128 bits. |
398 | | */ |
399 | | static int pbkdf2_derive(KDF_PBKDF2 *ctx, const char *pass, size_t passlen, |
400 | | const unsigned char *salt, int saltlen, uint64_t iter, |
401 | | const EVP_MD *digest, unsigned char *key, |
402 | | size_t keylen, int lower_bound_checks) |
403 | 0 | { |
404 | 0 | int ret = 0; |
405 | 0 | unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4]; |
406 | 0 | int cplen, k, tkeylen, mdlen; |
407 | 0 | uint64_t j; |
408 | 0 | unsigned long i = 1; |
409 | 0 | HMAC_CTX *hctx_tpl = NULL, *hctx = NULL; |
410 | |
|
411 | 0 | mdlen = EVP_MD_get_size(digest); |
412 | 0 | if (mdlen <= 0) |
413 | 0 | return 0; |
414 | | |
415 | | /* |
416 | | * This check should always be done because keylen / mdlen >= (2^32 - 1) |
417 | | * results in an overflow of the loop counter 'i'. |
418 | | */ |
419 | 0 | if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) { |
420 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
421 | 0 | return 0; |
422 | 0 | } |
423 | | |
424 | 0 | if (!lower_bound_check_passed(ctx, saltlen, iter, keylen, lower_bound_checks)) |
425 | 0 | return 0; |
426 | | |
427 | 0 | hctx_tpl = HMAC_CTX_new(); |
428 | 0 | if (hctx_tpl == NULL) |
429 | 0 | return 0; |
430 | 0 | p = key; |
431 | 0 | tkeylen = keylen; |
432 | 0 | if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL)) |
433 | 0 | goto err; |
434 | 0 | hctx = HMAC_CTX_new(); |
435 | 0 | if (hctx == NULL) |
436 | 0 | goto err; |
437 | 0 | while (tkeylen) { |
438 | 0 | if (tkeylen > mdlen) |
439 | 0 | cplen = mdlen; |
440 | 0 | else |
441 | 0 | cplen = tkeylen; |
442 | | /* |
443 | | * We are unlikely to ever use more than 256 blocks (5120 bits!) but |
444 | | * just in case... |
445 | | */ |
446 | 0 | itmp[0] = (unsigned char)((i >> 24) & 0xff); |
447 | 0 | itmp[1] = (unsigned char)((i >> 16) & 0xff); |
448 | 0 | itmp[2] = (unsigned char)((i >> 8) & 0xff); |
449 | 0 | itmp[3] = (unsigned char)(i & 0xff); |
450 | 0 | if (!HMAC_CTX_copy(hctx, hctx_tpl)) |
451 | 0 | goto err; |
452 | 0 | if (!HMAC_Update(hctx, salt, saltlen) |
453 | 0 | || !HMAC_Update(hctx, itmp, 4) |
454 | 0 | || !HMAC_Final(hctx, digtmp, NULL)) |
455 | 0 | goto err; |
456 | 0 | memcpy(p, digtmp, cplen); |
457 | 0 | for (j = 1; j < iter; j++) { |
458 | 0 | if (!HMAC_CTX_copy(hctx, hctx_tpl)) |
459 | 0 | goto err; |
460 | 0 | if (!HMAC_Update(hctx, digtmp, mdlen) |
461 | 0 | || !HMAC_Final(hctx, digtmp, NULL)) |
462 | 0 | goto err; |
463 | 0 | for (k = 0; k < cplen; k++) |
464 | 0 | p[k] ^= digtmp[k]; |
465 | 0 | } |
466 | 0 | tkeylen -= cplen; |
467 | 0 | i++; |
468 | 0 | p += cplen; |
469 | 0 | } |
470 | 0 | ret = 1; |
471 | |
|
472 | 0 | err: |
473 | 0 | HMAC_CTX_free(hctx); |
474 | 0 | HMAC_CTX_free(hctx_tpl); |
475 | 0 | return ret; |
476 | 0 | } |