1/*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#ifndef OSSL_PROVIDERS_DIGESTCOMMON_H
11# define OSSL_PROVIDERS_DIGESTCOMMON_H
12
13# include <openssl/core_numbers.h>
14# include <openssl/core_names.h>
15# include <openssl/params.h>
16
17# ifdef __cplusplus
18extern "C" {
19# endif
20
21#define PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
22static OSSL_OP_digest_get_params_fn name##_get_params; \
23static int name##_get_params(OSSL_PARAM params[]) \
24{ \
25 return digest_default_get_params(params, blksize, dgstsize, flags); \
26}
27
28#define PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name) \
29{ OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \
30{ OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \
31 (void (*)(void))digest_default_gettable_params }
32
33# define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START( \
34 name, CTX, blksize, dgstsize, flags, init, upd, fin) \
35static OSSL_OP_digest_newctx_fn name##_newctx; \
36static OSSL_OP_digest_freectx_fn name##_freectx; \
37static OSSL_OP_digest_dupctx_fn name##_dupctx; \
38static void *name##_newctx(void *prov_ctx) \
39{ \
40 CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
41 return ctx; \
42} \
43static void name##_freectx(void *vctx) \
44{ \
45 CTX *ctx = (CTX *)vctx; \
46 OPENSSL_clear_free(ctx, sizeof(*ctx)); \
47} \
48static void *name##_dupctx(void *ctx) \
49{ \
50 CTX *in = (CTX *)ctx; \
51 CTX *ret = OPENSSL_malloc(sizeof(*ret)); \
52 if (ret != NULL) \
53 *ret = *in; \
54 return ret; \
55} \
56static OSSL_OP_digest_final_fn name##_internal_final; \
57static int name##_internal_final(void *ctx, unsigned char *out, size_t *outl, \
58 size_t outsz) \
59{ \
60 if (outsz >= dgstsize && fin(out, ctx)) { \
61 *outl = dgstsize; \
62 return 1; \
63 } \
64 return 0; \
65} \
66PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
67const OSSL_DISPATCH name##_functions[] = { \
68 { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
69 { OSSL_FUNC_DIGEST_INIT, (void (*)(void))init }, \
70 { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))upd }, \
71 { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))name##_internal_final }, \
72 { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))name##_freectx }, \
73 { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))name##_dupctx }, \
74 PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name)
75
76# define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END \
77 { 0, NULL } \
78};
79
80# define IMPLEMENT_digest_functions( \
81 name, CTX, blksize, dgstsize, flags, init, upd, fin) \
82PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
83 init, upd, fin), \
84PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
85
86# define IMPLEMENT_digest_functions_with_settable_ctx( \
87 name, CTX, blksize, dgstsize, flags, init, upd, fin, \
88 settable_ctx_params, set_ctx_params) \
89PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
90 init, upd, fin), \
91{ OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, (void (*)(void))settable_ctx_params }, \
92{ OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))set_ctx_params }, \
93PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
94
95
96const OSSL_PARAM *digest_default_gettable_params(void);
97int digest_default_get_params(OSSL_PARAM params[], size_t blksz, size_t paramsz,
98 unsigned long flags);
99
100# ifdef __cplusplus
101}
102# endif
103
104#endif /* OSSL_PROVIDERS_DIGESTCOMMON_H */
105