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