From f1d3df3e69601f6f9f8070705543794f9b6696e2 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Tue, 16 Jul 2019 06:06:26 +0200 Subject: [PATCH] Adapt the provider digests for more use of OSSL_PARAM Reviewed-by: Matt Caswell (Merged from https://github.com/openssl/openssl/pull/9391) --- providers/common/digests/sha2_prov.c | 10 +++- providers/common/digests/sha3_prov.c | 64 +++++++++++++---------- providers/default/digests/blake2_prov.c | 4 +- providers/default/digests/md5_prov.c | 2 +- providers/default/digests/md5_sha1_prov.c | 4 +- providers/default/digests/null_prov.c | 34 +++++++----- providers/default/digests/sm3_prov.c | 2 +- providers/legacy/digests/md2_prov.c | 2 +- providers/legacy/digests/md4_prov.c | 2 +- providers/legacy/digests/mdc2_prov.c | 4 +- providers/legacy/digests/ripemd_prov.c | 2 +- providers/legacy/digests/wp_prov.c | 2 +- 12 files changed, 78 insertions(+), 54 deletions(-) diff --git a/providers/common/digests/sha2_prov.c b/providers/common/digests/sha2_prov.c index 0e49ae679c..e823c27039 100644 --- a/providers/common/digests/sha2_prov.c +++ b/providers/common/digests/sha2_prov.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -17,7 +18,7 @@ #include "internal/provider_algs.h" #include "internal/sha.h" -static OSSL_OP_digest_set_params_fn sha1_set_params; +static OSSL_OP_digest_ctx_set_params_fn sha1_set_params; /* Special set_params method for SSL3 */ static int sha1_set_params(void *vctx, const OSSL_PARAM params[]) @@ -36,30 +37,37 @@ static int sha1_set_params(void *vctx, const OSSL_PARAM params[]) OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(sha1, SHA_CTX, SHA_CBLOCK, SHA_DIGEST_LENGTH, + EVP_MD_FLAG_DIGALGID_ABSENT, SHA1_Init, SHA1_Update, SHA1_Final, sha1_set_params) OSSL_FUNC_DIGEST_CONSTRUCT(sha224, SHA256_CTX, SHA256_CBLOCK, SHA224_DIGEST_LENGTH, + EVP_MD_FLAG_DIGALGID_ABSENT, SHA224_Init, SHA224_Update, SHA224_Final) OSSL_FUNC_DIGEST_CONSTRUCT(sha256, SHA256_CTX, SHA256_CBLOCK, SHA256_DIGEST_LENGTH, + EVP_MD_FLAG_DIGALGID_ABSENT, SHA256_Init, SHA256_Update, SHA256_Final) OSSL_FUNC_DIGEST_CONSTRUCT(sha384, SHA512_CTX, SHA512_CBLOCK, SHA384_DIGEST_LENGTH, + EVP_MD_FLAG_DIGALGID_ABSENT, SHA384_Init, SHA384_Update, SHA384_Final) OSSL_FUNC_DIGEST_CONSTRUCT(sha512, SHA512_CTX, SHA512_CBLOCK, SHA512_DIGEST_LENGTH, + EVP_MD_FLAG_DIGALGID_ABSENT, SHA512_Init, SHA512_Update, SHA512_Final) OSSL_FUNC_DIGEST_CONSTRUCT(sha512_224, SHA512_CTX, SHA512_CBLOCK, SHA224_DIGEST_LENGTH, + EVP_MD_FLAG_DIGALGID_ABSENT, sha512_224_init, SHA512_Update, SHA512_Final) OSSL_FUNC_DIGEST_CONSTRUCT(sha512_256, SHA512_CTX, SHA512_CBLOCK, SHA256_DIGEST_LENGTH, + EVP_MD_FLAG_DIGALGID_ABSENT, sha512_256_init, SHA512_Update, SHA512_Final) diff --git a/providers/common/digests/sha3_prov.c b/providers/common/digests/sha3_prov.c index 15cd670a0e..63a57dbcb3 100644 --- a/providers/common/digests/sha3_prov.c +++ b/providers/common/digests/sha3_prov.c @@ -26,7 +26,7 @@ static OSSL_OP_digest_update_fn keccak_update; static OSSL_OP_digest_final_fn keccak_final; static OSSL_OP_digest_freectx_fn keccak_freectx; static OSSL_OP_digest_dupctx_fn keccak_dupctx; -static OSSL_OP_digest_set_params_fn shake_set_params; +static OSSL_OP_digest_ctx_set_params_fn shake_set_params; static sha3_absorb_fn generic_sha3_absorb; static sha3_final_fn generic_sha3_final; @@ -202,27 +202,33 @@ static void *uname##_newctx(void *provctx) \ return ctx; \ } -#define OSSL_FUNC_SHA3_DIGEST(name, bitlen, dgstsize, stparams) \ -static OSSL_OP_digest_size_fn name##_size; \ -static OSSL_OP_digest_block_size_fn name##_block_size; \ -static size_t name##_block_size(void) \ -{ \ - return SHA3_BLOCKSIZE(bitlen); \ -} \ -static size_t name##_size(void) \ -{ \ - return dgstsize; \ -} \ -const OSSL_DISPATCH name##_functions[] = { \ - { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \ - { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \ - { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \ - { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \ - { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \ - { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \ - { OSSL_FUNC_DIGEST_SIZE, (void (*)(void))name##_size }, \ - { OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))name##_block_size }, \ - { OSSL_FUNC_DIGEST_SET_PARAMS, (void (*)(void))stparams },\ +#define OSSL_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags, \ + stparams) \ +static OSSL_OP_digest_get_params_fn name##_get_params; \ +static int name##_get_params(OSSL_PARAM params[]) \ +{ \ + OSSL_PARAM *p = NULL; \ + \ + p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE); \ + if (p != NULL && !OSSL_PARAM_set_int(p, (blksize))) \ + return 0; \ + p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \ + if (p != NULL && !OSSL_PARAM_set_int(p, (dgstsize))) \ + return 0; \ + p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_FLAGS); \ + if (p != NULL && !OSSL_PARAM_set_ulong(p, (flags))) \ + return 0; \ + return 1; \ +} \ +const OSSL_DISPATCH name##_functions[] = { \ + { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \ + { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \ + { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \ + { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \ + { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \ + { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \ + { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \ + { OSSL_FUNC_DIGEST_CTX_SET_PARAMS, (void (*)(void))stparams }, \ OSSL_FUNC_DIGEST_CONSTRUCT_END static void keccak_freectx(void *vctx) @@ -257,16 +263,20 @@ static int shake_set_params(void *vctx, const OSSL_PARAM params[]) #define SHA3(bitlen) \ SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \ - OSSL_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, SHA3_MDSIZE(bitlen), NULL) + OSSL_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \ + SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ + EVP_MD_FLAG_DIGALGID_ABSENT, NULL) #define SHAKE(bitlen) \ SHA3_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, '\x1f') \ - OSSL_FUNC_SHA3_DIGEST(shake_##bitlen, bitlen, SHA3_MDSIZE(bitlen), \ - shake_set_params) + OSSL_FUNC_SHA3_DIGEST(shake_##bitlen, bitlen, \ + SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ + EVP_MD_FLAG_XOF, shake_set_params) #define KMAC(bitlen) \ KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \ - OSSL_FUNC_SHA3_DIGEST(keccak_kmac_##bitlen, bitlen, KMAC_MDSIZE(bitlen), \ - shake_set_params) + OSSL_FUNC_SHA3_DIGEST(keccak_kmac_##bitlen, bitlen, \ + SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen), \ + EVP_MD_FLAG_XOF, shake_set_params) SHA3(224) SHA3(256) diff --git a/providers/default/digests/blake2_prov.c b/providers/default/digests/blake2_prov.c index 18a8d60b0c..87c090c092 100644 --- a/providers/default/digests/blake2_prov.c +++ b/providers/default/digests/blake2_prov.c @@ -32,9 +32,9 @@ int blake2b512_init(void *ctx) } OSSL_FUNC_DIGEST_CONSTRUCT(blake2s256, BLAKE2S_CTX, - BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH, + BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH, 0, blake2s256_init, blake2s_update, blake2s_final) OSSL_FUNC_DIGEST_CONSTRUCT(blake2b512, BLAKE2B_CTX, - BLAKE2B_BLOCKBYTES, BLAKE2B_DIGEST_LENGTH, + BLAKE2B_BLOCKBYTES, BLAKE2B_DIGEST_LENGTH, 0, blake2b512_init, blake2b_update, blake2b_final) diff --git a/providers/default/digests/md5_prov.c b/providers/default/digests/md5_prov.c index 81636f670c..39b8d88613 100644 --- a/providers/default/digests/md5_prov.c +++ b/providers/default/digests/md5_prov.c @@ -13,5 +13,5 @@ #include "internal/provider_algs.h" OSSL_FUNC_DIGEST_CONSTRUCT(md5, MD5_CTX, - MD5_CBLOCK, MD5_DIGEST_LENGTH, + MD5_CBLOCK, MD5_DIGEST_LENGTH, 0, MD5_Init, MD5_Update, MD5_Final) diff --git a/providers/default/digests/md5_sha1_prov.c b/providers/default/digests/md5_sha1_prov.c index 490134e7eb..2ed36d03b3 100644 --- a/providers/default/digests/md5_sha1_prov.c +++ b/providers/default/digests/md5_sha1_prov.c @@ -17,7 +17,7 @@ #include "internal/md5_sha1.h" #include "internal/provider_algs.h" -static OSSL_OP_digest_set_params_fn md5_sha1_set_params; +static OSSL_OP_digest_ctx_set_params_fn md5_sha1_set_params; /* Special set_params method for SSL3 */ static int md5_sha1_set_params(void *vctx, const OSSL_PARAM params[]) @@ -35,6 +35,6 @@ static int md5_sha1_set_params(void *vctx, const OSSL_PARAM params[]) } OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(md5_sha1, MD5_SHA1_CTX, - MD5_SHA1_CBLOCK, MD5_SHA1_DIGEST_LENGTH, + MD5_SHA1_CBLOCK, MD5_SHA1_DIGEST_LENGTH, 0, md5_sha1_init, md5_sha1_update, md5_sha1_final, md5_sha1_set_params) diff --git a/providers/default/digests/null_prov.c b/providers/default/digests/null_prov.c index d7644f4cbf..893737b5ae 100644 --- a/providers/default/digests/null_prov.c +++ b/providers/default/digests/null_prov.c @@ -8,6 +8,8 @@ */ #include +#include +#include #include #include "internal/provider_algs.h" @@ -19,18 +21,7 @@ static OSSL_OP_digest_final_fn nullmd_final; static OSSL_OP_digest_newctx_fn nullmd_newctx; static OSSL_OP_digest_freectx_fn nullmd_freectx; static OSSL_OP_digest_dupctx_fn nullmd_dupctx; -static OSSL_OP_digest_size_fn nullmd_size; -static OSSL_OP_digest_block_size_fn nullmd_block_size; - -static size_t nullmd_block_size(void) -{ - return 0; -} - -static size_t nullmd_size(void) -{ - return 0; -} +static OSSL_OP_digest_get_params_fn nullmd_get_params; static int nullmd_init(void *vctx) { @@ -62,6 +53,22 @@ static void *nullmd_dupctx(void *ctx) return &nullmd_dummy; } +static int nullmd_get_params(OSSL_PARAM params[]) +{ + OSSL_PARAM *p = NULL; + + p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE); + if (p != NULL && !OSSL_PARAM_set_int(p, 0)) + return 0; + p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); + if (p != NULL && !OSSL_PARAM_set_int(p, 0)) + return 0; + p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_FLAGS); + if (p != NULL && !OSSL_PARAM_set_ulong(p, 0)) + return 0; + return 1; +} + const OSSL_DISPATCH nullmd_functions[] = { { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))nullmd_newctx }, { OSSL_FUNC_DIGEST_INIT, (void (*)(void))nullmd_init }, @@ -69,7 +76,6 @@ const OSSL_DISPATCH nullmd_functions[] = { { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))nullmd_final }, { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))nullmd_freectx }, { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))nullmd_dupctx }, - { OSSL_FUNC_DIGEST_SIZE, (void (*)(void))nullmd_size }, - { OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))nullmd_block_size }, + { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))nullmd_get_params }, { 0, NULL } }; diff --git a/providers/default/digests/sm3_prov.c b/providers/default/digests/sm3_prov.c index bd039a8863..60789a469e 100644 --- a/providers/default/digests/sm3_prov.c +++ b/providers/default/digests/sm3_prov.c @@ -13,5 +13,5 @@ #include "internal/provider_algs.h" OSSL_FUNC_DIGEST_CONSTRUCT(sm3, SM3_CTX, - SM3_CBLOCK, SM3_DIGEST_LENGTH, + SM3_CBLOCK, SM3_DIGEST_LENGTH, 0, sm3_init, sm3_update, sm3_final) diff --git a/providers/legacy/digests/md2_prov.c b/providers/legacy/digests/md2_prov.c index edd4b78bbd..833aab7685 100644 --- a/providers/legacy/digests/md2_prov.c +++ b/providers/legacy/digests/md2_prov.c @@ -14,5 +14,5 @@ #include "internal/provider_algs.h" OSSL_FUNC_DIGEST_CONSTRUCT(md2, MD2_CTX, - MD2_BLOCK, MD2_DIGEST_LENGTH, + MD2_BLOCK, MD2_DIGEST_LENGTH, 0, MD2_Init, MD2_Update, MD2_Final) diff --git a/providers/legacy/digests/md4_prov.c b/providers/legacy/digests/md4_prov.c index 86937f7b43..9d336f9919 100644 --- a/providers/legacy/digests/md4_prov.c +++ b/providers/legacy/digests/md4_prov.c @@ -14,5 +14,5 @@ #include "internal/provider_algs.h" OSSL_FUNC_DIGEST_CONSTRUCT(md4, MD4_CTX, - MD4_CBLOCK, MD4_DIGEST_LENGTH, + MD4_CBLOCK, MD4_DIGEST_LENGTH, 0, MD4_Init, MD4_Update, MD4_Final) diff --git a/providers/legacy/digests/mdc2_prov.c b/providers/legacy/digests/mdc2_prov.c index 31e95cb391..f77e3992b0 100644 --- a/providers/legacy/digests/mdc2_prov.c +++ b/providers/legacy/digests/mdc2_prov.c @@ -15,7 +15,7 @@ #include "internal/core_mkdigest.h" #include "internal/provider_algs.h" -static OSSL_OP_digest_set_params_fn mdc2_set_params; +static OSSL_OP_digest_ctx_set_params_fn mdc2_set_params; static int mdc2_set_params(void *vctx, const OSSL_PARAM params[]) { @@ -32,6 +32,6 @@ static int mdc2_set_params(void *vctx, const OSSL_PARAM params[]) } OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(mdc2, MDC2_CTX, - MDC2_BLOCK, MDC2_DIGEST_LENGTH, + MDC2_BLOCK, MDC2_DIGEST_LENGTH, 0, MDC2_Init, MDC2_Update, MDC2_Final, mdc2_set_params) diff --git a/providers/legacy/digests/ripemd_prov.c b/providers/legacy/digests/ripemd_prov.c index 124351257b..2b192bf917 100644 --- a/providers/legacy/digests/ripemd_prov.c +++ b/providers/legacy/digests/ripemd_prov.c @@ -14,5 +14,5 @@ #include "internal/provider_algs.h" OSSL_FUNC_DIGEST_CONSTRUCT(ripemd160, RIPEMD160_CTX, - RIPEMD160_CBLOCK, RIPEMD160_DIGEST_LENGTH, + RIPEMD160_CBLOCK, RIPEMD160_DIGEST_LENGTH, 0, RIPEMD160_Init, RIPEMD160_Update, RIPEMD160_Final) diff --git a/providers/legacy/digests/wp_prov.c b/providers/legacy/digests/wp_prov.c index ece67e0941..297d367667 100644 --- a/providers/legacy/digests/wp_prov.c +++ b/providers/legacy/digests/wp_prov.c @@ -14,5 +14,5 @@ #include "internal/provider_algs.h" OSSL_FUNC_DIGEST_CONSTRUCT(wp, WHIRLPOOL_CTX, - WHIRLPOOL_BBLOCK / 8, WHIRLPOOL_DIGEST_LENGTH, + WHIRLPOOL_BBLOCK / 8, WHIRLPOOL_DIGEST_LENGTH, 0, WHIRLPOOL_Init, WHIRLPOOL_Update, WHIRLPOOL_Final) -- 2.25.1