]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/common/include/internal/core_mkdigest.h
Add {get,set}table_params() functions for provider digests
[thirdparty/openssl.git] / providers / common / include / internal / core_mkdigest.h
CommitLineData
d5e5e2ff
SL
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_CORE_MKDIGEST_H
11# define OSSL_CORE_MKDIGEST_H
12
13# include <openssl/core_numbers.h>
3d214461
RL
14# include <openssl/core_names.h>
15# include <openssl/params.h>
d5e5e2ff
SL
16
17# ifdef __cplusplus
18extern "C" {
19# endif
20
21# define OSSL_FUNC_DIGEST_ALLOC_METHODS(name, CTX_NAME) \
22static OSSL_OP_digest_newctx_fn name##_newctx; \
23static OSSL_OP_digest_freectx_fn name##_freectx; \
24static OSSL_OP_digest_dupctx_fn name##_dupctx; \
25static void *name##_newctx(void *prov_ctx) \
26{ \
27 CTX_NAME *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
28 return ctx; \
29} \
30static void name##_freectx(void *vctx) \
31{ \
32 CTX_NAME *ctx = (CTX_NAME *)vctx; \
33 OPENSSL_clear_free(ctx, sizeof(*ctx)); \
34} \
35static void *name##_dupctx(void *ctx) \
36{ \
37 CTX_NAME *in = (CTX_NAME *)ctx; \
38 CTX_NAME *ret = OPENSSL_malloc(sizeof(*ret)); \
39 *ret = *in; \
40 return ret; \
41}
42
3d214461
RL
43# define OSSL_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
44static OSSL_OP_digest_get_params_fn name##_get_params; \
ec02412b
RL
45static OSSL_OP_digest_gettable_params_fn name##_gettable_params; \
46static const OSSL_PARAM known_##name##_gettable_params[] = { \
47 {OSSL_DIGEST_PARAM_BLOCK_SIZE, OSSL_PARAM_INTEGER, \
48 NULL, sizeof(int), 0}, \
49 {OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_INTEGER, NULL, sizeof(int), 0}, \
50 {OSSL_DIGEST_PARAM_FLAGS, OSSL_PARAM_INTEGER, \
51 NULL, sizeof(unsigned long), 0}, \
52 OSSL_PARAM_END \
53}; \
54static const OSSL_PARAM *name##_gettable_params(void) \
55{ \
56 return known_##name##_gettable_params; \
57} \
3d214461
RL
58static int name##_get_params(OSSL_PARAM params[]) \
59{ \
60 OSSL_PARAM *p = NULL; \
61 \
62 p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE); \
63 if (p != NULL && !OSSL_PARAM_set_int(p, (blksize))) \
64 return 0; \
65 p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
66 if (p != NULL && !OSSL_PARAM_set_int(p, (dgstsize))) \
67 return 0; \
68 p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_FLAGS); \
69 if (p != NULL && !OSSL_PARAM_set_ulong(p, (flags))) \
70 return 0; \
71 return 1; \
72}
73
d5e5e2ff
SL
74# define OSSL_FUNC_DIGEST_SET_FINAL(name, dgstsize, fin) \
75static OSSL_OP_digest_final_fn name##_wrapfinal; \
76static int name##_wrapfinal(void *ctx, unsigned char *out, size_t *outl, size_t outsz) \
77{ \
78 if (outsz >= dgstsize && fin(out, ctx)) { \
79 *outl = dgstsize; \
80 return 1; \
81 } \
82 return 0; \
83}
84
3d214461 85# define OSSL_FUNC_DIGEST_COMMON(name, init, upd) \
d5e5e2ff
SL
86const OSSL_DISPATCH name##_functions[] = { \
87 { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
88 { OSSL_FUNC_DIGEST_INIT, (void (*)(void))init }, \
89 { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))upd }, \
90 { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))name##_wrapfinal }, \
91 { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))name##_freectx }, \
92 { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))name##_dupctx }, \
ec02412b
RL
93 { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \
94 { OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \
95 (void (*)(void))name##_gettable_params },
d5e5e2ff
SL
96
97# define OSSL_FUNC_DIGEST_CONSTRUCT_END \
98 { 0, NULL } \
99};
100
3d214461
RL
101# define OSSL_FUNC_DIGEST_CONSTRUCT_START(name, CTX, \
102 blksize, dgstsize, flags, \
103 init, upd, fin) \
104OSSL_FUNC_DIGEST_ALLOC_METHODS(name, CTX) \
105OSSL_FUNC_DIGEST_SET_FINAL(name, dgstsize, fin) \
106OSSL_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
107OSSL_FUNC_DIGEST_COMMON(name, init, upd)
108
109# define OSSL_FUNC_DIGEST_CONSTRUCT(name, CTX, blksize, dgstsize, flags, \
110 init, upd, fin) \
111OSSL_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
112 init, upd, fin) \
d5e5e2ff
SL
113OSSL_FUNC_DIGEST_CONSTRUCT_END
114
3d214461
RL
115# define OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(name, CTX, \
116 blksize, dgstsize, flags, \
ec02412b
RL
117 init, upd, fin, \
118 setparamtypes, setparams) \
3d214461
RL
119OSSL_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
120 init, upd, fin) \
ec02412b
RL
121 { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \
122 (void (*)(void))setparamtypes }, \
3d214461 123 { OSSL_FUNC_DIGEST_CTX_SET_PARAMS, (void (*)(void))setparams }, \
d5e5e2ff
SL
124OSSL_FUNC_DIGEST_CONSTRUCT_END
125
126# ifdef __cplusplus
127}
128# endif
129
130#endif /* OSSL_CORE_MKDIGEST_H */