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