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