xref: /freebsd/crypto/openssl/providers/implementations/include/prov/digestcommon.h (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery #ifndef OSSL_PROVIDERS_DIGESTCOMMON_H
11*b077aed3SPierre Pronchery # define OSSL_PROVIDERS_DIGESTCOMMON_H
12*b077aed3SPierre Pronchery 
13*b077aed3SPierre Pronchery # include <openssl/core_dispatch.h>
14*b077aed3SPierre Pronchery # include <openssl/core_names.h>
15*b077aed3SPierre Pronchery # include <openssl/params.h>
16*b077aed3SPierre Pronchery # include "prov/providercommon.h"
17*b077aed3SPierre Pronchery 
18*b077aed3SPierre Pronchery /* Internal flags that can be queried */
19*b077aed3SPierre Pronchery #define PROV_DIGEST_FLAG_XOF             0x0001
20*b077aed3SPierre Pronchery #define PROV_DIGEST_FLAG_ALGID_ABSENT    0x0002
21*b077aed3SPierre Pronchery 
22*b077aed3SPierre Pronchery # ifdef __cplusplus
23*b077aed3SPierre Pronchery extern "C" {
24*b077aed3SPierre Pronchery # endif
25*b077aed3SPierre Pronchery 
26*b077aed3SPierre Pronchery #define PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags)             \
27*b077aed3SPierre Pronchery static OSSL_FUNC_digest_get_params_fn name##_get_params;                       \
28*b077aed3SPierre Pronchery static int name##_get_params(OSSL_PARAM params[])                              \
29*b077aed3SPierre Pronchery {                                                                              \
30*b077aed3SPierre Pronchery     return ossl_digest_default_get_params(params, blksize, dgstsize, flags);   \
31*b077aed3SPierre Pronchery }
32*b077aed3SPierre Pronchery 
33*b077aed3SPierre Pronchery #define PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name)                             \
34*b077aed3SPierre Pronchery { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params },            \
35*b077aed3SPierre Pronchery { OSSL_FUNC_DIGEST_GETTABLE_PARAMS,                                            \
36*b077aed3SPierre Pronchery   (void (*)(void))ossl_digest_default_gettable_params }
37*b077aed3SPierre Pronchery 
38*b077aed3SPierre Pronchery # define PROV_FUNC_DIGEST_FINAL(name, dgstsize, fin)                           \
39*b077aed3SPierre Pronchery static OSSL_FUNC_digest_final_fn name##_internal_final;                        \
40*b077aed3SPierre Pronchery static int name##_internal_final(void *ctx, unsigned char *out, size_t *outl,  \
41*b077aed3SPierre Pronchery                                  size_t outsz)                                 \
42*b077aed3SPierre Pronchery {                                                                              \
43*b077aed3SPierre Pronchery     if (ossl_prov_is_running() && outsz >= dgstsize && fin(out, ctx)) {        \
44*b077aed3SPierre Pronchery         *outl = dgstsize;                                                      \
45*b077aed3SPierre Pronchery         return 1;                                                              \
46*b077aed3SPierre Pronchery     }                                                                          \
47*b077aed3SPierre Pronchery     return 0;                                                                  \
48*b077aed3SPierre Pronchery }
49*b077aed3SPierre Pronchery 
50*b077aed3SPierre Pronchery # define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(                            \
51*b077aed3SPierre Pronchery     name, CTX, blksize, dgstsize, flags, upd, fin)                             \
52*b077aed3SPierre Pronchery static OSSL_FUNC_digest_newctx_fn name##_newctx;                               \
53*b077aed3SPierre Pronchery static OSSL_FUNC_digest_freectx_fn name##_freectx;                             \
54*b077aed3SPierre Pronchery static OSSL_FUNC_digest_dupctx_fn name##_dupctx;                               \
55*b077aed3SPierre Pronchery static void *name##_newctx(void *prov_ctx)                                     \
56*b077aed3SPierre Pronchery {                                                                              \
57*b077aed3SPierre Pronchery     CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL;   \
58*b077aed3SPierre Pronchery     return ctx;                                                                \
59*b077aed3SPierre Pronchery }                                                                              \
60*b077aed3SPierre Pronchery static void name##_freectx(void *vctx)                                         \
61*b077aed3SPierre Pronchery {                                                                              \
62*b077aed3SPierre Pronchery     CTX *ctx = (CTX *)vctx;                                                    \
63*b077aed3SPierre Pronchery     OPENSSL_clear_free(ctx,  sizeof(*ctx));                                    \
64*b077aed3SPierre Pronchery }                                                                              \
65*b077aed3SPierre Pronchery static void *name##_dupctx(void *ctx)                                          \
66*b077aed3SPierre Pronchery {                                                                              \
67*b077aed3SPierre Pronchery     CTX *in = (CTX *)ctx;                                                      \
68*b077aed3SPierre Pronchery     CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) : NULL;   \
69*b077aed3SPierre Pronchery     if (ret != NULL)                                                           \
70*b077aed3SPierre Pronchery         *ret = *in;                                                            \
71*b077aed3SPierre Pronchery     return ret;                                                                \
72*b077aed3SPierre Pronchery }                                                                              \
73*b077aed3SPierre Pronchery PROV_FUNC_DIGEST_FINAL(name, dgstsize, fin)                                    \
74*b077aed3SPierre Pronchery PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags)                     \
75*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_##name##_functions[] = {                              \
76*b077aed3SPierre Pronchery     { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx },                \
77*b077aed3SPierre Pronchery     { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))upd },                          \
78*b077aed3SPierre Pronchery     { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))name##_internal_final },         \
79*b077aed3SPierre Pronchery     { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))name##_freectx },              \
80*b077aed3SPierre Pronchery     { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))name##_dupctx },                \
81*b077aed3SPierre Pronchery     PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name)
82*b077aed3SPierre Pronchery 
83*b077aed3SPierre Pronchery # define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END                               \
84*b077aed3SPierre Pronchery     { 0, NULL }                                                                \
85*b077aed3SPierre Pronchery };
86*b077aed3SPierre Pronchery 
87*b077aed3SPierre Pronchery # define IMPLEMENT_digest_functions(                                           \
88*b077aed3SPierre Pronchery     name, CTX, blksize, dgstsize, flags, init, upd, fin)                       \
89*b077aed3SPierre Pronchery static OSSL_FUNC_digest_init_fn name##_internal_init;                          \
90*b077aed3SPierre Pronchery static int name##_internal_init(void *ctx,                                     \
91*b077aed3SPierre Pronchery                                 ossl_unused const OSSL_PARAM params[])         \
92*b077aed3SPierre Pronchery {                                                                              \
93*b077aed3SPierre Pronchery     return ossl_prov_is_running() && init(ctx);                                \
94*b077aed3SPierre Pronchery }                                                                              \
95*b077aed3SPierre Pronchery PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
96*b077aed3SPierre Pronchery                                           upd, fin),                           \
97*b077aed3SPierre Pronchery     { OSSL_FUNC_DIGEST_INIT, (void (*)(void))name##_internal_init },           \
98*b077aed3SPierre Pronchery PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
99*b077aed3SPierre Pronchery 
100*b077aed3SPierre Pronchery # define IMPLEMENT_digest_functions_with_settable_ctx(                         \
101*b077aed3SPierre Pronchery     name, CTX, blksize, dgstsize, flags, init, upd, fin,                       \
102*b077aed3SPierre Pronchery     settable_ctx_params, set_ctx_params)                                       \
103*b077aed3SPierre Pronchery static OSSL_FUNC_digest_init_fn name##_internal_init;                          \
104*b077aed3SPierre Pronchery static int name##_internal_init(void *ctx, const OSSL_PARAM params[])          \
105*b077aed3SPierre Pronchery {                                                                              \
106*b077aed3SPierre Pronchery     return ossl_prov_is_running()                                              \
107*b077aed3SPierre Pronchery            && init(ctx)                                                        \
108*b077aed3SPierre Pronchery            && set_ctx_params(ctx, params);                                     \
109*b077aed3SPierre Pronchery }                                                                              \
110*b077aed3SPierre Pronchery PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
111*b077aed3SPierre Pronchery                                           upd, fin),                           \
112*b077aed3SPierre Pronchery     { OSSL_FUNC_DIGEST_INIT, (void (*)(void))name##_internal_init },           \
113*b077aed3SPierre Pronchery     { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, (void (*)(void))settable_ctx_params }, \
114*b077aed3SPierre Pronchery     { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))set_ctx_params },       \
115*b077aed3SPierre Pronchery PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
116*b077aed3SPierre Pronchery 
117*b077aed3SPierre Pronchery 
118*b077aed3SPierre Pronchery const OSSL_PARAM *ossl_digest_default_gettable_params(void *provctx);
119*b077aed3SPierre Pronchery int ossl_digest_default_get_params(OSSL_PARAM params[], size_t blksz,
120*b077aed3SPierre Pronchery                                    size_t paramsz, unsigned long flags);
121*b077aed3SPierre Pronchery 
122*b077aed3SPierre Pronchery # ifdef __cplusplus
123*b077aed3SPierre Pronchery }
124*b077aed3SPierre Pronchery # endif
125*b077aed3SPierre Pronchery 
126*b077aed3SPierre Pronchery #endif /* OSSL_PROVIDERS_DIGESTCOMMON_H */
127