From 696ff7bc3e0d4f15aa54d539000b934ee294c940 Mon Sep 17 00:00:00 2001 From: tb Date: Fri, 29 Dec 2023 06:08:01 +0000 Subject: [PATCH] Move the middle part of evp_lib.c to evp_digest.c These are ~200 lines of EVP_MD API that separated two parts of the file dedicated to EVP_CIPHER thingies. --- lib/libcrypto/evp/evp_digest.c | 205 ++++++++++++++++++++++++++++++++- lib/libcrypto/evp/evp_lib.c | 205 +-------------------------------- 2 files changed, 205 insertions(+), 205 deletions(-) diff --git a/lib/libcrypto/evp/evp_digest.c b/lib/libcrypto/evp/evp_digest.c index deb282e4ecc..583c454845d 100644 --- a/lib/libcrypto/evp/evp_digest.c +++ b/lib/libcrypto/evp/evp_digest.c @@ -1,4 +1,4 @@ -/* $OpenBSD: evp_digest.c,v 1.1 2023/12/29 05:57:24 tb Exp $ */ +/* $OpenBSD: evp_digest.c,v 1.2 2023/12/29 06:08:01 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -367,3 +367,206 @@ EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr) } return ret; } + +int +EVP_MD_block_size(const EVP_MD *md) +{ + return md->block_size; +} + +int +EVP_MD_type(const EVP_MD *md) +{ + return md->type; +} + +int +EVP_MD_pkey_type(const EVP_MD *md) +{ + return md->pkey_type; +} + +int +EVP_MD_size(const EVP_MD *md) +{ + if (!md) { + EVPerror(EVP_R_MESSAGE_DIGEST_IS_NULL); + return -1; + } + return md->md_size; +} + +unsigned long +EVP_MD_flags(const EVP_MD *md) +{ + return md->flags; +} + +EVP_MD * +EVP_MD_meth_new(int md_type, int pkey_type) +{ + EVP_MD *md; + + if ((md = calloc(1, sizeof(*md))) == NULL) + return NULL; + + md->type = md_type; + md->pkey_type = pkey_type; + + return md; +} + +EVP_MD * +EVP_MD_meth_dup(const EVP_MD *md) +{ + EVP_MD *to; + + if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) == NULL) + return NULL; + + memcpy(to, md, sizeof(*to)); + + return to; +} + +void +EVP_MD_meth_free(EVP_MD *md) +{ + freezero(md, sizeof(*md)); +} + +int +EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize) +{ + md->block_size = blocksize; + return 1; +} + +int +EVP_MD_meth_set_result_size(EVP_MD *md, int result_size) +{ + md->md_size = result_size; + return 1; +} + +int +EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize) +{ + md->ctx_size = datasize; + return 1; +} + +int +EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags) +{ + md->flags = flags; + return 1; +} + +int +EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx)) +{ + md->init = init; + return 1; +} + +int +EVP_MD_meth_set_update(EVP_MD *md, + int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count)) +{ + md->update = update; + return 1; +} + +int +EVP_MD_meth_set_final(EVP_MD *md, + int (*final)(EVP_MD_CTX *ctx, unsigned char *md)) +{ + md->final = final; + return 1; +} + +int +EVP_MD_meth_set_copy(EVP_MD *md, + int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from)) +{ + md->copy = copy; + return 1; +} + +int +EVP_MD_meth_set_cleanup(EVP_MD *md, + int (*cleanup)(EVP_MD_CTX *ctx)) +{ + md->cleanup = cleanup; + return 1; +} + +int +EVP_MD_meth_set_ctrl(EVP_MD *md, + int (*ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)) +{ + md->md_ctrl = ctrl; + return 1; +} + +const EVP_MD * +EVP_MD_CTX_md(const EVP_MD_CTX *ctx) +{ + if (!ctx) + return NULL; + return ctx->digest; +} + +void * +EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx) +{ + return ctx->md_data; +} + +EVP_PKEY_CTX * +EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx) +{ + return ctx->pctx; +} + +void +EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx) +{ + if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) { + EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); + } else { + EVP_PKEY_CTX_free(ctx->pctx); + } + + ctx->pctx = pctx; + + if (pctx != NULL) { + /* + * For unclear reasons it was decided that the caller keeps + * ownership of pctx. So a flag was invented to make sure we + * don't free it in EVP_MD_CTX_cleanup(). We also need to + * unset it in EVP_MD_CTX_copy_ex(). Fortunately, the flag + * isn't public... + */ + EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); + } +} + +void +EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) +{ + ctx->flags |= flags; +} + +void +EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags) +{ + ctx->flags &= ~flags; +} + +int +EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags) +{ + return (ctx->flags & flags); +} diff --git a/lib/libcrypto/evp/evp_lib.c b/lib/libcrypto/evp/evp_lib.c index 622d40dbdff..32881294420 100644 --- a/lib/libcrypto/evp/evp_lib.c +++ b/lib/libcrypto/evp/evp_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: evp_lib.c,v 1.30 2023/12/15 13:28:30 tb Exp $ */ +/* $OpenBSD: evp_lib.c,v 1.31 2023/12/29 06:08:01 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -343,209 +343,6 @@ EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx, const unsigned char *iv, size_t len) return 1; } -int -EVP_MD_block_size(const EVP_MD *md) -{ - return md->block_size; -} - -int -EVP_MD_type(const EVP_MD *md) -{ - return md->type; -} - -int -EVP_MD_pkey_type(const EVP_MD *md) -{ - return md->pkey_type; -} - -int -EVP_MD_size(const EVP_MD *md) -{ - if (!md) { - EVPerror(EVP_R_MESSAGE_DIGEST_IS_NULL); - return -1; - } - return md->md_size; -} - -unsigned long -EVP_MD_flags(const EVP_MD *md) -{ - return md->flags; -} - -EVP_MD * -EVP_MD_meth_new(int md_type, int pkey_type) -{ - EVP_MD *md; - - if ((md = calloc(1, sizeof(*md))) == NULL) - return NULL; - - md->type = md_type; - md->pkey_type = pkey_type; - - return md; -} - -EVP_MD * -EVP_MD_meth_dup(const EVP_MD *md) -{ - EVP_MD *to; - - if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) == NULL) - return NULL; - - memcpy(to, md, sizeof(*to)); - - return to; -} - -void -EVP_MD_meth_free(EVP_MD *md) -{ - freezero(md, sizeof(*md)); -} - -int -EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize) -{ - md->block_size = blocksize; - return 1; -} - -int -EVP_MD_meth_set_result_size(EVP_MD *md, int result_size) -{ - md->md_size = result_size; - return 1; -} - -int -EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize) -{ - md->ctx_size = datasize; - return 1; -} - -int -EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags) -{ - md->flags = flags; - return 1; -} - -int -EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx)) -{ - md->init = init; - return 1; -} - -int -EVP_MD_meth_set_update(EVP_MD *md, - int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count)) -{ - md->update = update; - return 1; -} - -int -EVP_MD_meth_set_final(EVP_MD *md, - int (*final)(EVP_MD_CTX *ctx, unsigned char *md)) -{ - md->final = final; - return 1; -} - -int -EVP_MD_meth_set_copy(EVP_MD *md, - int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from)) -{ - md->copy = copy; - return 1; -} - -int -EVP_MD_meth_set_cleanup(EVP_MD *md, - int (*cleanup)(EVP_MD_CTX *ctx)) -{ - md->cleanup = cleanup; - return 1; -} - -int -EVP_MD_meth_set_ctrl(EVP_MD *md, - int (*ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)) -{ - md->md_ctrl = ctrl; - return 1; -} - -const EVP_MD * -EVP_MD_CTX_md(const EVP_MD_CTX *ctx) -{ - if (!ctx) - return NULL; - return ctx->digest; -} - -void * -EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx) -{ - return ctx->md_data; -} - -EVP_PKEY_CTX * -EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx) -{ - return ctx->pctx; -} - -void -EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx) -{ - if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) { - EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); - } else { - EVP_PKEY_CTX_free(ctx->pctx); - } - - ctx->pctx = pctx; - - if (pctx != NULL) { - /* - * For unclear reasons it was decided that the caller keeps - * ownership of pctx. So a flag was invented to make sure we - * don't free it in EVP_MD_CTX_cleanup(). We also need to - * unset it in EVP_MD_CTX_copy_ex(). Fortunately, the flag - * isn't public... - */ - EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); - } -} - -void -EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) -{ - ctx->flags |= flags; -} - -void -EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags) -{ - ctx->flags &= ~flags; -} - -int -EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags) -{ - return (ctx->flags & flags); -} - void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags) { -- 2.20.1