X-Git-Url: https://git.openssl.org/gitweb/?a=blobdiff_plain;f=providers%2Fimplementations%2Fkdfs%2Fpbkdf2.c;h=ce27fe9b393e15fe6f8f5ec6fca8b838102479db;hb=3469b388164775546022635d6695cae17104faa6;hp=e6956fe1552adc324133dec49603d303c019b1a8;hpb=0577959ceab4ca2a72a662ed12067da83cdbb3c7;p=openssl.git diff --git a/providers/implementations/kdfs/pbkdf2.c b/providers/implementations/kdfs/pbkdf2.c index e6956fe155..ce27fe9b39 100644 --- a/providers/implementations/kdfs/pbkdf2.c +++ b/providers/implementations/kdfs/pbkdf2.c @@ -1,5 +1,5 @@ /* - * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -20,11 +20,12 @@ #include #include #include +#include #include "internal/cryptlib.h" #include "internal/numbers.h" #include "crypto/evp.h" #include "prov/provider_ctx.h" -#include "prov/providercommonerr.h" +#include "prov/providercommon.h" #include "prov/implementations.h" #include "prov/provider_util.h" #include "pbkdf2.h" @@ -41,6 +42,8 @@ static OSSL_FUNC_kdf_reset_fn kdf_pbkdf2_reset; static OSSL_FUNC_kdf_derive_fn kdf_pbkdf2_derive; static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf2_settable_ctx_params; static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf2_set_ctx_params; +static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf2_gettable_ctx_params; +static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf2_get_ctx_params; static int pbkdf2_derive(const char *pass, size_t passlen, const unsigned char *salt, int saltlen, uint64_t iter, @@ -64,6 +67,9 @@ static void *kdf_pbkdf2_new(void *provctx) { KDF_PBKDF2 *ctx; + if (!ossl_prov_is_running()) + return NULL; + ctx = OPENSSL_zalloc(sizeof(*ctx)); if (ctx == NULL) { ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); @@ -105,7 +111,7 @@ static void kdf_pbkdf2_reset(void *vctx) static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx) { OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; - OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx); + OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, SN_sha1, 0); @@ -133,11 +139,14 @@ static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen, return 1; } -static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, - size_t keylen) +static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen, + const OSSL_PARAM params[]) { KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; - const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); + const EVP_MD *md; + + if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params)) + return 0; if (ctx->pass == NULL) { ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); @@ -149,6 +158,7 @@ static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, return 0; } + md = ossl_prov_digest_md(&ctx->digest); return pbkdf2_derive((char *)ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len, ctx->iter, md, key, keylen, ctx->lower_bound_checks); @@ -158,7 +168,7 @@ static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[]) { const OSSL_PARAM *p; KDF_PBKDF2 *ctx = vctx; - OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx); + OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); int pkcs5; uint64_t iter, min_iter; @@ -198,7 +208,8 @@ static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[]) return 1; } -static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(void) +static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx, + ossl_unused void *p_ctx) { static const OSSL_PARAM known_settable_ctx_params[] = { OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), @@ -221,7 +232,8 @@ static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[]) return -2; } -static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(void) +static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx, + ossl_unused void *p_ctx) { static const OSSL_PARAM known_gettable_ctx_params[] = { OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), @@ -230,7 +242,7 @@ static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(void) return known_gettable_ctx_params; } -const OSSL_DISPATCH kdf_pbkdf2_functions[] = { +const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = { { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new }, { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free }, { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset }, @@ -275,13 +287,13 @@ static int pbkdf2_derive(const char *pass, size_t passlen, * results in an overflow of the loop counter 'i'. */ if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) { - ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LEN); + ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); return 0; } if (lower_bound_checks) { if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) { - ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LEN); + ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL); return 0; } if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) {