cleanup provider digests

Added some missing #ifdef NO_XXX around some of the digest functions.
Renamed core_mkdigest.h to digestcommon.h
Added ERR_raise() to set/get params for digest.
Moved common code for get_params/gettable_params into digest_common.c
Renamed #defines in digestcommon.
Removed null_prov.c (It should not be needed)

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9625)
This commit is contained in:
Shane Lontis 2019-08-20 09:07:12 +10:00
parent 3bfe9005e5
commit c85d5e0257
21 changed files with 312 additions and 383 deletions

View file

@ -12,9 +12,8 @@ provider-digest - The digest library E<lt>-E<gt> provider functions
#include <openssl/core_names.h>
/*
* None of these are actual functions, but are displayed like this for
* the function signatures for functions that are offered as function
* pointers in OSSL_DISPATCH arrays.
* Digests support the following function signatures in OSSL_DISPATCH arrays.
* (The function signatures are not actual functions).
*/
/* Context management */
@ -90,7 +89,7 @@ macros in L<openssl-core_numbers.h(7)>, as follows:
OP_digest_settable_ctx_params OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS
A digest algorithm implementation may not implement all of these functions.
In order to be useable all or none of OP_digest_newctx, OP_digest_freectx,
In order to be usable all or none of OP_digest_newctx, OP_digest_freectx,
OP_digest_init, OP_digest_update and OP_digest_final should be implemented.
All other functions are optional.
@ -100,7 +99,7 @@ OP_digest_newctx() should create and return a pointer to a provider side
structure for holding context information during a digest operation.
A pointer to this context will be passed back in a number of the other digest
operation function calls.
The paramater B<provctx> is the provider context generated during provider
The parameter B<provctx> is the provider context generated during provider
initialisation (see L<provider(3)>).
OP_digest_freectx() is passed a pointer to the provider side digest context in
@ -113,7 +112,7 @@ B<dctx> parameter and return the duplicate copy.
=head2 Digest Generation Functions
OP_digest_init() initialises a digest operation given a newly created
provider side digest context in the B<dctx> paramter.
provider side digest context in the B<dctx> parameter.
OP_digest_update() is called to supply data to be digested as part of a
previously initialised digest operation.
@ -160,7 +159,7 @@ OP_digest_get_ctx_params(), and OP_digest_set_ctx_params() can handle,
respectively.
Parameters currently recognised by built-in digests with this function
are as follows. Not all parametes are relevant to, or are understood
are as follows. Not all parameters are relevant to, or are understood
by all digests:
=over 4
@ -224,7 +223,7 @@ and stores them in B<params>.
See L<OSSL_PARAM(3)> for further details on the parameters structure.
Parameters currently recognised by built-in digests are as follows. Not all
parametes are relevant to, or are understood by all digests:
parameters are relevant to, or are understood by all digests:
=over 4

View file

@ -1,5 +1,5 @@
SOURCE[../../../libcrypto]=\
sha2_prov.c sha3_prov.c
$COMMON=sha2_prov.c sha3_prov.c digest_common.c
SOURCE[../../fips]=\
sha2_prov.c sha3_prov.c
SOURCE[../../../libcrypto]=$COMMON
SOURCE[../../fips]=$COMMON
SOURCE[../../legacy]= digest_common.c

View file

@ -0,0 +1,47 @@
/*
* Copyright 2019 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
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "openssl/err.h"
#include "internal/digestcommon.h"
#include "internal/providercommonerr.h"
int digest_default_get_params(OSSL_PARAM params[], int blksz, int paramsz,
unsigned long flags)
{
OSSL_PARAM *p = NULL;
p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE);
if (p != NULL && !OSSL_PARAM_set_int(p, blksz)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE);
if (p != NULL && !OSSL_PARAM_set_int(p, paramsz)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_FLAGS);
if (p != NULL && !OSSL_PARAM_set_ulong(p, flags)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
return 1;
}
static const OSSL_PARAM digest_default_known_gettable_params[] = {
{ OSSL_DIGEST_PARAM_BLOCK_SIZE, OSSL_PARAM_INTEGER, NULL, sizeof(int), 0},
{ OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_INTEGER, NULL, sizeof(int), 0},
{ OSSL_DIGEST_PARAM_FLAGS, OSSL_PARAM_INTEGER, NULL,
sizeof(unsigned long), 0},
OSSL_PARAM_END
};
const OSSL_PARAM *digest_default_gettable_params(void)
{
return digest_default_known_gettable_params;
}

View file

@ -14,25 +14,24 @@
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/core_names.h>
#include "internal/core_mkdigest.h"
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
#include "internal/sha.h"
static OSSL_OP_digest_set_ctx_params_fn sha1_set_params;
static OSSL_OP_digest_settable_ctx_params_fn sha1_settable_params;
static OSSL_OP_digest_set_ctx_params_fn sha1_set_ctx_params;
static OSSL_OP_digest_settable_ctx_params_fn sha1_settable_ctx_params;
static const OSSL_PARAM known_sha1_ctx_params[] = {
static const OSSL_PARAM known_sha1_settable_ctx_params[] = {
{OSSL_DIGEST_PARAM_SSL3_MS, OSSL_PARAM_OCTET_STRING, NULL, 0, 0},
OSSL_PARAM_END
};
static const OSSL_PARAM *sha1_settable_params(void)
static const OSSL_PARAM *sha1_settable_ctx_params(void)
{
return known_sha1_ctx_params;
return known_sha1_settable_ctx_params;
}
/* Special set_params method for SSL3 */
static int sha1_set_params(void *vctx, const OSSL_PARAM params[])
static int sha1_set_ctx_params(void *vctx, const OSSL_PARAM params[])
{
const OSSL_PARAM *p;
SHA_CTX *ctx = (SHA_CTX *)vctx;
@ -46,38 +45,44 @@ static int sha1_set_params(void *vctx, const OSSL_PARAM params[])
return 0;
}
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_settable_params, sha1_set_params)
/* sha1_functions */
IMPLEMENT_digest_functions_with_settable_ctx(
sha1, SHA_CTX, SHA_CBLOCK, SHA_DIGEST_LENGTH, EVP_MD_FLAG_DIGALGID_ABSENT,
SHA1_Init, SHA1_Update, SHA1_Final,
sha1_settable_ctx_params, sha1_set_ctx_params)
OSSL_FUNC_DIGEST_CONSTRUCT(sha224, SHA256_CTX,
/* sha224_functions */
IMPLEMENT_digest_functions(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_functions */
IMPLEMENT_digest_functions(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,
/* sha384_functions */
IMPLEMENT_digest_functions(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_functions */
IMPLEMENT_digest_functions(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_224_functions */
IMPLEMENT_digest_functions(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_256_functions */
IMPLEMENT_digest_functions(sha512_256, SHA512_CTX,
SHA512_CBLOCK, SHA256_DIGEST_LENGTH,
EVP_MD_FLAG_DIGALGID_ABSENT,
sha512_256_init, SHA512_Update, SHA512_Final)

View file

@ -7,14 +7,16 @@
* https://www.openssl.org/source/license.html
*/
#include <openssl/core_names.h>
#include <string.h>
#include <openssl/core_names.h>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/err.h>
#include "internal/sha3.h"
#include "internal/core_mkdigest.h"
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
#include "internal/providercommonerr.h"
/*
* Forward declaration of any unique methods implemented here. This is not strictly
@ -168,88 +170,64 @@ static PROV_SHA3_METHOD shake_s390x_md =
s390x_shake_final
};
# define SHA3_SET_MD(uname, typ) \
if (S390_SHA3_CAPABLE(uname)) { \
ctx->pad = S390X_##uname; \
ctx->meth = typ##_s390x_md; \
} else { \
ctx->meth = sha3_generic_md; \
# define SHA3_SET_MD(uname, typ) \
if (S390_SHA3_CAPABLE(uname)) { \
ctx->pad = S390X_##uname; \
ctx->meth = typ##_s390x_md; \
} else { \
ctx->meth = sha3_generic_md; \
}
#else
# define SHA3_SET_MD(uname, typ) ctx->meth = sha3_generic_md;
#endif /* S390_SHA3 */
#define SHA3_newctx(typ, uname, name, bitlen, pad) \
static OSSL_OP_digest_newctx_fn name##_newctx; \
static void *name##_newctx(void *provctx) \
{ \
KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
\
if (ctx == NULL) \
return NULL; \
sha3_init(ctx, pad, bitlen); \
SHA3_SET_MD(uname, typ) \
return ctx; \
#define SHA3_newctx(typ, uname, name, bitlen, pad) \
static OSSL_OP_digest_newctx_fn name##_newctx; \
static void *name##_newctx(void *provctx) \
{ \
KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
\
if (ctx == NULL) \
return NULL; \
sha3_init(ctx, pad, bitlen); \
SHA3_SET_MD(uname, typ) \
return ctx; \
}
#define KMAC_newctx(uname, bitlen, pad) \
static OSSL_OP_digest_newctx_fn uname##_newctx; \
static void *uname##_newctx(void *provctx) \
{ \
KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
\
if (ctx == NULL) \
return NULL; \
keccak_kmac_init(ctx, pad, bitlen); \
ctx->meth = sha3_generic_md; \
return ctx; \
#define KMAC_newctx(uname, bitlen, pad) \
static OSSL_OP_digest_newctx_fn uname##_newctx; \
static void *uname##_newctx(void *provctx) \
{ \
KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
\
if (ctx == NULL) \
return NULL; \
keccak_kmac_init(ctx, pad, bitlen); \
ctx->meth = sha3_generic_md; \
return ctx; \
}
#define OSSL_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags, \
stparamtypes, stparams) \
static OSSL_OP_digest_get_params_fn name##_get_params; \
static OSSL_OP_digest_gettable_params_fn name##_gettable_params; \
static const OSSL_PARAM known_##name##_gettable_params[] = { \
{OSSL_DIGEST_PARAM_BLOCK_SIZE, OSSL_PARAM_INTEGER, \
NULL, sizeof(int), 0}, \
{OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_INTEGER, NULL, sizeof(int), 0}, \
{OSSL_DIGEST_PARAM_FLAGS, OSSL_PARAM_INTEGER, \
NULL, sizeof(unsigned long), 0}, \
OSSL_PARAM_END \
}; \
static const OSSL_PARAM *name##_gettable_params(void) \
{ \
return known_##name##_gettable_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_GETTABLE_PARAMS, \
(void (*)(void))name##_gettable_params }, \
{ OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))stparams }, \
{ OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \
(void (*)(void))stparamtypes }, \
OSSL_FUNC_DIGEST_CONSTRUCT_END
#define PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags) \
PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
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 }, \
PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name)
#define PROV_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags) \
PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \
PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
#define PROV_FUNC_SHAKE_DIGEST(name, bitlen, blksize, dgstsize, flags) \
PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \
{ OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))shake_set_ctx_params }, \
{ OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \
(void (*)(void))shake_settable_ctx_params }, \
PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
static void keccak_freectx(void *vctx)
{
@ -271,7 +249,6 @@ static const OSSL_PARAM known_shake_settable_ctx_params[] = {
{OSSL_DIGEST_PARAM_SSL3_MS, OSSL_PARAM_OCTET_STRING, NULL, 0, 0},
OSSL_PARAM_END
};
static const OSSL_PARAM *shake_settable_ctx_params(void)
{
return known_shake_settable_ctx_params;
@ -284,37 +261,45 @@ static int shake_set_ctx_params(void *vctx, const OSSL_PARAM params[])
if (ctx != NULL && params != NULL) {
p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_XOFLEN);
if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size))
if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
return 1;
}
return 0; /* Null Parameter */
}
#define SHA3(bitlen) \
SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \
OSSL_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \
SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
EVP_MD_FLAG_DIGALGID_ABSENT, NULL, NULL)
#define IMPLEMENT_SHA3_functions(bitlen) \
SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \
PROV_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \
SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
EVP_MD_FLAG_DIGALGID_ABSENT)
#define SHAKE(bitlen) \
SHA3_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, '\x1f') \
OSSL_FUNC_SHA3_DIGEST(shake_##bitlen, bitlen, \
SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
EVP_MD_FLAG_XOF, \
shake_settable_ctx_params, shake_set_ctx_params)
#define KMAC(bitlen) \
KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \
OSSL_FUNC_SHA3_DIGEST(keccak_kmac_##bitlen, bitlen, \
SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen), \
EVP_MD_FLAG_XOF, \
shake_settable_ctx_params, shake_set_ctx_params)
#define IMPLEMENT_SHAKE_functions(bitlen) \
SHA3_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, '\x1f') \
PROV_FUNC_SHAKE_DIGEST(shake_##bitlen, bitlen, \
SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
EVP_MD_FLAG_XOF)
#define IMPLEMENT_KMAC_functions(bitlen) \
KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \
PROV_FUNC_SHAKE_DIGEST(keccak_kmac_##bitlen, bitlen, \
SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen), \
EVP_MD_FLAG_XOF)
SHA3(224)
SHA3(256)
SHA3(384)
SHA3(512)
SHAKE(128)
SHAKE(256)
KMAC(128)
KMAC(256)
/* sha3_224_functions */
IMPLEMENT_SHA3_functions(224)
/* sha3_256_functions */
IMPLEMENT_SHA3_functions(256)
/* sha3_384_functions */
IMPLEMENT_SHA3_functions(384)
/* sha3_512_functions */
IMPLEMENT_SHA3_functions(512)
/* shake_128_functions */
IMPLEMENT_SHAKE_functions(128)
/* shake_256_functions */
IMPLEMENT_SHAKE_functions(256)
/* keccak_kmac_128_functions */
IMPLEMENT_KMAC_functions(128)
/* keccak_kmac_256_functions */
IMPLEMENT_KMAC_functions(256)

View file

@ -1,130 +0,0 @@
/*
* Copyright 2019 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
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#ifndef OSSL_CORE_MKDIGEST_H
# define OSSL_CORE_MKDIGEST_H
# include <openssl/core_numbers.h>
# include <openssl/core_names.h>
# include <openssl/params.h>
# ifdef __cplusplus
extern "C" {
# endif
# define OSSL_FUNC_DIGEST_ALLOC_METHODS(name, CTX_NAME) \
static OSSL_OP_digest_newctx_fn name##_newctx; \
static OSSL_OP_digest_freectx_fn name##_freectx; \
static OSSL_OP_digest_dupctx_fn name##_dupctx; \
static void *name##_newctx(void *prov_ctx) \
{ \
CTX_NAME *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
return ctx; \
} \
static void name##_freectx(void *vctx) \
{ \
CTX_NAME *ctx = (CTX_NAME *)vctx; \
OPENSSL_clear_free(ctx, sizeof(*ctx)); \
} \
static void *name##_dupctx(void *ctx) \
{ \
CTX_NAME *in = (CTX_NAME *)ctx; \
CTX_NAME *ret = OPENSSL_malloc(sizeof(*ret)); \
*ret = *in; \
return ret; \
}
# define OSSL_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
static OSSL_OP_digest_get_params_fn name##_get_params; \
static OSSL_OP_digest_gettable_params_fn name##_gettable_params; \
static const OSSL_PARAM known_##name##_gettable_params[] = { \
{OSSL_DIGEST_PARAM_BLOCK_SIZE, OSSL_PARAM_INTEGER, \
NULL, sizeof(int), 0}, \
{OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_INTEGER, NULL, sizeof(int), 0}, \
{OSSL_DIGEST_PARAM_FLAGS, OSSL_PARAM_INTEGER, \
NULL, sizeof(unsigned long), 0}, \
OSSL_PARAM_END \
}; \
static const OSSL_PARAM *name##_gettable_params(void) \
{ \
return known_##name##_gettable_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; \
}
# define OSSL_FUNC_DIGEST_SET_FINAL(name, dgstsize, fin) \
static OSSL_OP_digest_final_fn name##_wrapfinal; \
static int name##_wrapfinal(void *ctx, unsigned char *out, size_t *outl, size_t outsz) \
{ \
if (outsz >= dgstsize && fin(out, ctx)) { \
*outl = dgstsize; \
return 1; \
} \
return 0; \
}
# define OSSL_FUNC_DIGEST_COMMON(name, init, upd) \
const OSSL_DISPATCH name##_functions[] = { \
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void))init }, \
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))upd }, \
{ OSSL_FUNC_DIGEST_FINAL, (void (*)(void))name##_wrapfinal }, \
{ OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))name##_freectx }, \
{ OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))name##_dupctx }, \
{ OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \
{ OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \
(void (*)(void))name##_gettable_params },
# define OSSL_FUNC_DIGEST_CONSTRUCT_END \
{ 0, NULL } \
};
# define OSSL_FUNC_DIGEST_CONSTRUCT_START(name, CTX, \
blksize, dgstsize, flags, \
init, upd, fin) \
OSSL_FUNC_DIGEST_ALLOC_METHODS(name, CTX) \
OSSL_FUNC_DIGEST_SET_FINAL(name, dgstsize, fin) \
OSSL_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
OSSL_FUNC_DIGEST_COMMON(name, init, upd)
# define OSSL_FUNC_DIGEST_CONSTRUCT(name, CTX, blksize, dgstsize, flags, \
init, upd, fin) \
OSSL_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
init, upd, fin) \
OSSL_FUNC_DIGEST_CONSTRUCT_END
# define OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(name, CTX, \
blksize, dgstsize, flags, \
init, upd, fin, \
setparamtypes, setparams) \
OSSL_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
init, upd, fin) \
{ OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \
(void (*)(void))setparamtypes }, \
{ OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))setparams }, \
OSSL_FUNC_DIGEST_CONSTRUCT_END
# ifdef __cplusplus
}
# endif
#endif /* OSSL_CORE_MKDIGEST_H */

View file

@ -0,0 +1,103 @@
/*
* Copyright 2019 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
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#ifndef OSSL_DIGESTCOMMON_H
# define OSSL_DIGESTCOMMON_H
# include <openssl/core_numbers.h>
# include <openssl/core_names.h>
# include <openssl/params.h>
# ifdef __cplusplus
extern "C" {
# endif
#define PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
static OSSL_OP_digest_get_params_fn name##_get_params; \
static int name##_get_params(OSSL_PARAM params[]) \
{ \
return digest_default_get_params(params, blksize, dgstsize, flags); \
}
#define PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name) \
{ OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \
{ OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \
(void (*)(void))digest_default_gettable_params }
# define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START( \
name, CTX, blksize, dgstsize, flags, init, upd, fin) \
static OSSL_OP_digest_newctx_fn name##_newctx; \
static OSSL_OP_digest_freectx_fn name##_freectx; \
static OSSL_OP_digest_dupctx_fn name##_dupctx; \
static void *name##_newctx(void *prov_ctx) \
{ \
CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
return ctx; \
} \
static void name##_freectx(void *vctx) \
{ \
CTX *ctx = (CTX *)vctx; \
OPENSSL_clear_free(ctx, sizeof(*ctx)); \
} \
static void *name##_dupctx(void *ctx) \
{ \
CTX *in = (CTX *)ctx; \
CTX *ret = OPENSSL_malloc(sizeof(*ret)); \
*ret = *in; \
return ret; \
} \
static OSSL_OP_digest_final_fn name##_internal_final; \
static int name##_internal_final(void *ctx, unsigned char *out, size_t *outl, \
size_t outsz) \
{ \
if (outsz >= dgstsize && fin(out, ctx)) { \
*outl = dgstsize; \
return 1; \
} \
return 0; \
} \
PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
const OSSL_DISPATCH name##_functions[] = { \
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void))init }, \
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))upd }, \
{ OSSL_FUNC_DIGEST_FINAL, (void (*)(void))name##_internal_final }, \
{ OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))name##_freectx }, \
{ OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))name##_dupctx }, \
PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name)
# define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END \
{ 0, NULL } \
};
# define IMPLEMENT_digest_functions( \
name, CTX, blksize, dgstsize, flags, init, upd, fin) \
PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
init, upd, fin), \
PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
# define IMPLEMENT_digest_functions_with_settable_ctx( \
name, CTX, blksize, dgstsize, flags, init, upd, fin, \
settable_ctx_params, set_ctx_params) \
PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
init, upd, fin), \
{ OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, (void (*)(void))settable_ctx_params }, \
{ OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))set_ctx_params }, \
PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
const OSSL_PARAM *digest_default_gettable_params(void);
int digest_default_get_params(OSSL_PARAM params[], int blksz, int paramsz,
unsigned long flags);
# ifdef __cplusplus
}
# endif
#endif /* OSSL_DIGESTCOMMON_H */

View file

@ -28,7 +28,6 @@ extern const OSSL_DISPATCH blake2b512_functions[];
extern const OSSL_DISPATCH md5_functions[];
extern const OSSL_DISPATCH md5_sha1_functions[];
extern const OSSL_DISPATCH sm3_functions[];
extern const OSSL_DISPATCH nullmd_functions[];
extern const OSSL_DISPATCH md2_functions[];
extern const OSSL_DISPATCH md4_functions[];
extern const OSSL_DISPATCH mdc2_functions[];

View file

@ -89,8 +89,6 @@ static const OSSL_ALGORITHM deflt_digests[] = {
{ "MD5-SHA1", "default=yes", md5_sha1_functions },
#endif /* OPENSSL_NO_MD5 */
/*{ "UNDEF", "default=yes", nullmd_functions }, */
{ NULL, NULL, NULL }
};

View file

@ -9,7 +9,7 @@
#include <openssl/crypto.h>
#include "internal/blake2.h"
#include "internal/core_mkdigest.h"
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
OSSL_OP_digest_init_fn blake2s256_init;
@ -31,10 +31,12 @@ int blake2b512_init(void *ctx)
return blake2b_init((BLAKE2B_CTX *)ctx, &P);
}
OSSL_FUNC_DIGEST_CONSTRUCT(blake2s256, BLAKE2S_CTX,
/* blake2s256_functions */
IMPLEMENT_digest_functions(blake2s256, BLAKE2S_CTX,
BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH, 0,
blake2s256_init, blake2s_update, blake2s_final)
OSSL_FUNC_DIGEST_CONSTRUCT(blake2b512, BLAKE2B_CTX,
/* blake2b512_functions */
IMPLEMENT_digest_functions(blake2b512, BLAKE2B_CTX,
BLAKE2B_BLOCKBYTES, BLAKE2B_DIGEST_LENGTH, 0,
blake2b512_init, blake2b_update, blake2b_final)

View file

@ -18,7 +18,6 @@
#include <string.h>
#include <openssl/crypto.h>
#include "blake2_impl.h"
#include "internal/blake2.h"
static const uint64_t blake2b_IV[8] =

View file

@ -1,5 +1,3 @@
SOURCE[../../../libcrypto]=\
null_prov.c
IF[{- !$disabled{blake2} -}]
SOURCE[../../../libcrypto]=\

View file

@ -9,9 +9,10 @@
#include <openssl/crypto.h>
#include <openssl/md5.h>
#include "internal/core_mkdigest.h"
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
OSSL_FUNC_DIGEST_CONSTRUCT(md5, MD5_CTX,
/* md5_functions */
IMPLEMENT_digest_functions(md5, MD5_CTX,
MD5_CBLOCK, MD5_DIGEST_LENGTH, 0,
MD5_Init, MD5_Update, MD5_Final)

View file

@ -7,14 +7,13 @@
* https://www.openssl.org/source/license.html
*/
#include <string.h>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/core_names.h>
#include "internal/core_mkdigest.h"
#include "internal/md5_sha1.h"
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
static OSSL_OP_digest_set_ctx_params_fn md5_sha1_set_ctx_params;
@ -45,8 +44,8 @@ static int md5_sha1_set_ctx_params(void *vctx, const OSSL_PARAM params[])
return 0;
}
OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(md5_sha1, MD5_SHA1_CTX,
MD5_SHA1_CBLOCK, MD5_SHA1_DIGEST_LENGTH, 0,
md5_sha1_init, md5_sha1_update, md5_sha1_final,
md5_sha1_settable_ctx_params,
md5_sha1_set_ctx_params)
/* md5_sha1_functions */
IMPLEMENT_digest_functions_with_settable_ctx(
md5_sha1, MD5_SHA1_CTX, MD5_SHA1_CBLOCK, MD5_SHA1_DIGEST_LENGTH, 0,
md5_sha1_init, md5_sha1_update, md5_sha1_final,
md5_sha1_settable_ctx_params, md5_sha1_set_ctx_params)

View file

@ -1,81 +0,0 @@
/*
* Copyright 2019 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
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <openssl/core_numbers.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/whrlpool.h>
#include "internal/provider_algs.h"
static int nullmd_dummy = 1;
static OSSL_OP_digest_init_fn nullmd_init;
static OSSL_OP_digest_update_fn nullmd_update;
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_get_params_fn nullmd_get_params;
static int nullmd_init(void *vctx)
{
return 1;
}
static int nullmd_update(void *vctx, const unsigned char *inp, size_t bytes)
{
return 1;
}
static int nullmd_final(void *ctx, unsigned char *out, size_t *outl, size_t outsz)
{
*outl = 0;
return 1;
}
static void *nullmd_newctx(void *prov_ctx)
{
return &nullmd_dummy;
}
static void nullmd_freectx(void *vctx)
{
}
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 },
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))nullmd_update },
{ 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_GET_PARAMS, (void (*)(void))nullmd_get_params },
{ 0, NULL }
};

View file

@ -9,9 +9,10 @@
#include <openssl/crypto.h>
#include "internal/sm3.h"
#include "internal/core_mkdigest.h"
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
OSSL_FUNC_DIGEST_CONSTRUCT(sm3, SM3_CTX,
/* sm3_functions */
IMPLEMENT_digest_functions(sm3, SM3_CTX,
SM3_CBLOCK, SM3_DIGEST_LENGTH, 0,
sm3_init, sm3_update, sm3_final)

View file

@ -9,10 +9,10 @@
#include <openssl/crypto.h>
#include <openssl/md2.h>
#include "internal/core_mkdigest.h"
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
OSSL_FUNC_DIGEST_CONSTRUCT(md2, MD2_CTX,
/* md2_functions */
IMPLEMENT_digest_functions(md2, MD2_CTX,
MD2_BLOCK, MD2_DIGEST_LENGTH, 0,
MD2_Init, MD2_Update, MD2_Final)

View file

@ -9,10 +9,10 @@
#include <openssl/crypto.h>
#include <openssl/md4.h>
#include "internal/core_mkdigest.h"
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
OSSL_FUNC_DIGEST_CONSTRUCT(md4, MD4_CTX,
/* md4_functions */
IMPLEMENT_digest_functions(md4, MD4_CTX,
MD4_CBLOCK, MD4_DIGEST_LENGTH, 0,
MD4_Init, MD4_Update, MD4_Final)

View file

@ -11,9 +11,10 @@
#include <openssl/params.h>
#include <openssl/mdc2.h>
#include <openssl/core_names.h>
#include "internal/core_mkdigest.h"
#include <openssl/err.h>
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
#include "internal/providercommonerr.h"
static OSSL_OP_digest_set_ctx_params_fn mdc2_set_ctx_params;
static OSSL_OP_digest_settable_ctx_params_fn mdc2_settable_ctx_params;
@ -35,14 +36,17 @@ static int mdc2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
if (ctx != NULL && params != NULL) {
p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_PAD_TYPE);
if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->pad_type))
if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->pad_type)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
return 1;
}
return 0; /* Null Parameter */
}
OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(mdc2, MDC2_CTX,
MDC2_BLOCK, MDC2_DIGEST_LENGTH, 0,
MDC2_Init, MDC2_Update, MDC2_Final,
mdc2_settable_ctx_params, mdc2_set_ctx_params)
/* mdc2_functions */
IMPLEMENT_digest_functions_with_settable_ctx(
mdc2, MDC2_CTX, MDC2_BLOCK, MDC2_DIGEST_LENGTH, 0,
MDC2_Init, MDC2_Update, MDC2_Final,
mdc2_settable_ctx_params, mdc2_set_ctx_params)

View file

@ -9,10 +9,10 @@
#include <openssl/crypto.h>
#include <openssl/ripemd.h>
#include "internal/core_mkdigest.h"
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
OSSL_FUNC_DIGEST_CONSTRUCT(ripemd160, RIPEMD160_CTX,
/* ripemd160_functions */
IMPLEMENT_digest_functions(ripemd160, RIPEMD160_CTX,
RIPEMD160_CBLOCK, RIPEMD160_DIGEST_LENGTH, 0,
RIPEMD160_Init, RIPEMD160_Update, RIPEMD160_Final)

View file

@ -9,10 +9,10 @@
#include <openssl/crypto.h>
#include <openssl/whrlpool.h>
#include "internal/core_mkdigest.h"
#include "internal/digestcommon.h"
#include "internal/provider_algs.h"
OSSL_FUNC_DIGEST_CONSTRUCT(wp, WHIRLPOOL_CTX,
/* wp_functions */
IMPLEMENT_digest_functions(wp, WHIRLPOOL_CTX,
WHIRLPOOL_BBLOCK / 8, WHIRLPOOL_DIGEST_LENGTH, 0,
WHIRLPOOL_Init, WHIRLPOOL_Update, WHIRLPOOL_Final)