Adapt diverse EVP_MD functions to use get_params and set_params interfaces

Also clean up EVP_MD_CTX_ctrl(), which did use these interfaces, but
development since allows for more elegant code.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9391)
This commit is contained in:
Richard Levitte 2019-07-16 06:03:12 +02:00
parent 2893111fc6
commit 6a3b7c6887
4 changed files with 82 additions and 59 deletions

View file

@ -526,55 +526,66 @@ int EVP_Digest(const void *data, size_t count,
int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
{
if (ctx->digest != NULL && ctx->digest->set_params != NULL)
return ctx->digest->set_params(ctx->provctx, params);
if (ctx->digest != NULL && ctx->digest->ctx_set_params != NULL)
return ctx->digest->ctx_set_params(ctx->provctx, params);
return 0;
}
int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
{
if (ctx->digest != NULL && ctx->digest->get_params != NULL)
return ctx->digest->get_params(ctx->provctx, params);
return ctx->digest->ctx_get_params(ctx->provctx, params);
return 0;
}
/* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
{
if (ctx->digest != NULL) {
if (ctx->digest->prov != NULL) {
OSSL_PARAM params[2];
size_t i, n = 0;
int ret = EVP_CTRL_RET_UNSUPPORTED;
int set_params = 1;
size_t sz;
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
switch (cmd) {
case EVP_MD_CTRL_XOF_LEN:
if (ctx->digest->set_params == NULL)
break;
i = (size_t)p1;
params[n++] =
OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &i);
params[n++] = OSSL_PARAM_construct_end();
return ctx->digest->set_params(ctx->provctx, params);
case EVP_MD_CTRL_MICALG:
if (ctx->digest->get_params == NULL)
break;
params[n++] =
OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
p2, p1 ? p1 : 9999);
params[n++] = OSSL_PARAM_construct_end();
return ctx->digest->get_params(ctx->provctx, params);
}
return 0;
}
/* legacy code */
if (ctx->digest->md_ctrl != NULL) {
int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
if (ret <= 0)
return 0;
return 1;
}
if (ctx == NULL || ctx->digest == NULL) {
ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
return 0;
}
return 0;
if (ctx->digest->prov == NULL)
goto legacy;
switch (cmd) {
case EVP_MD_CTRL_XOF_LEN:
sz = (size_t)p1;
params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
break;
case EVP_MD_CTRL_MICALG:
set_params = 0;
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
p2, p1 ? p1 : 9999);
break;
default:
return EVP_CTRL_RET_UNSUPPORTED;
}
if (set_params)
ret = evp_do_md_ctx_setparams(ctx->digest, ctx->provctx, params);
else
ret = evp_do_md_ctx_getparams(ctx->digest, ctx->provctx, params);
return ret;
/* TODO(3.0): Remove legacy code below */
legacy:
if (ctx->digest->md_ctrl == NULL) {
ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
return 0;
}
ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
if (ret <= 0)
return 0;
return ret;
}
static void *evp_md_from_dispatch(const char *name, const OSSL_DISPATCH *fns,
@ -632,27 +643,22 @@ static void *evp_md_from_dispatch(const char *name, const OSSL_DISPATCH *fns,
if (md->dupctx == NULL)
md->dupctx = OSSL_get_OP_digest_dupctx(fns);
break;
case OSSL_FUNC_DIGEST_SIZE:
if (md->size == NULL)
md->size = OSSL_get_OP_digest_size(fns);
break;
case OSSL_FUNC_DIGEST_BLOCK_SIZE:
if (md->dblock_size == NULL)
md->dblock_size = OSSL_get_OP_digest_block_size(fns);
break;
case OSSL_FUNC_DIGEST_SET_PARAMS:
if (md->set_params == NULL)
md->set_params = OSSL_get_OP_digest_set_params(fns);
break;
case OSSL_FUNC_DIGEST_GET_PARAMS:
if (md->get_params == NULL)
md->get_params = OSSL_get_OP_digest_get_params(fns);
break;
case OSSL_FUNC_DIGEST_CTX_SET_PARAMS:
if (md->ctx_set_params == NULL)
md->ctx_set_params = OSSL_get_OP_digest_ctx_set_params(fns);
break;
case OSSL_FUNC_DIGEST_CTX_GET_PARAMS:
if (md->ctx_get_params == NULL)
md->ctx_get_params = OSSL_get_OP_digest_ctx_get_params(fns);
break;
}
}
if ((fncnt != 0 && fncnt != 5)
|| (fncnt == 0 && md->digest == NULL)
|| md->size == NULL) {
|| (fncnt == 0 && md->digest == NULL)) {
/*
* In order to be a consistent set of functions we either need the
* whole set of init/update/final etc functions or none of them.
@ -660,6 +666,7 @@ static void *evp_md_from_dispatch(const char *name, const OSSL_DISPATCH *fns,
* generate digests.
*/
EVP_MD_meth_free(md);
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
return NULL;
}
md->prov = prov;

View file

@ -475,15 +475,18 @@ const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md)
int EVP_MD_block_size(const EVP_MD *md)
{
int ok, v = md->block_size;
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
if (md == NULL) {
EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
return -1;
}
if (md->prov != NULL && md->dblock_size != NULL)
return (int)md->dblock_size();
params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_BLOCK_SIZE, &v);
ok = evp_do_md_getparams(md, params);
return md->block_size;
return ok != 0 ? v : -1;
}
int EVP_MD_type(const EVP_MD *md)
@ -498,20 +501,30 @@ int EVP_MD_pkey_type(const EVP_MD *md)
int EVP_MD_size(const EVP_MD *md)
{
if (!md) {
int ok, v = md->md_size;
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
if (md == NULL) {
EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
return -1;
}
if (md->prov != NULL && md->size != NULL)
return (int)md->size();
params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_SIZE, &v);
ok = evp_do_md_getparams(md, params);
return md->md_size;
return ok != 0 ? v : -1;
}
unsigned long EVP_MD_flags(const EVP_MD *md)
{
return md->flags;
int ok;
unsigned long v = md->flags;
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
params[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &v);
ok = evp_do_md_getparams(md, params);
return ok != 0 ? v : 0;
}
EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)

View file

@ -170,6 +170,11 @@ int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
OSSL_PARAM params[]);
int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
OSSL_PARAM params[]);
int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
OSSL_PARAM params[]);
int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
OSSL_PARAM params[]);
OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);

View file

@ -73,9 +73,7 @@ PARAM_FUNCTIONS(EVP_CIPHER,
evp_do_ciph_ctx_getparams, ctx_get_params,
evp_do_ciph_ctx_setparams, ctx_set_params)
#if 0
PARAM_FUNCTIONS(EVP_MD,
evp_do_md_getparams, get_params,
evp_do_md_ctx_getparams, ctx_get_params,
evp_do_md_ctx_setparams, ctx_set_params)
#endif