Adapt the provider digests for more use of OSSL_PARAM

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:06:26 +02:00
parent 3d214461bf
commit f1d3df3e69
12 changed files with 78 additions and 54 deletions

View file

@ -9,6 +9,7 @@
#include <openssl/crypto.h>
#include <openssl/core_numbers.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/params.h>
@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -8,6 +8,8 @@
*/
#include <openssl/core_numbers.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/whrlpool.h>
#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 }
};

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)