1 /* 2 * Copyright 2019-2021 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_dispatch.h> 14 # include <openssl/core_names.h> 15 # include <openssl/params.h> 16 # include "prov/providercommon.h" 17 18 /* Internal flags that can be queried */ 19 #define PROV_DIGEST_FLAG_XOF 0x0001 20 #define PROV_DIGEST_FLAG_ALGID_ABSENT 0x0002 21 22 # ifdef __cplusplus 23 extern "C" { 24 # endif 25 26 #define PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \ 27 static OSSL_FUNC_digest_get_params_fn name##_get_params; \ 28 static int name##_get_params(OSSL_PARAM params[]) \ 29 { \ 30 return ossl_digest_default_get_params(params, blksize, dgstsize, flags); \ 31 } 32 33 #define PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name) \ 34 { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \ 35 { OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \ 36 (void (*)(void))ossl_digest_default_gettable_params } 37 38 # define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START( \ 39 name, CTX, blksize, dgstsize, flags, upd, fin) \ 40 static OSSL_FUNC_digest_newctx_fn name##_newctx; \ 41 static OSSL_FUNC_digest_freectx_fn name##_freectx; \ 42 static OSSL_FUNC_digest_dupctx_fn name##_dupctx; \ 43 static void *name##_newctx(void *prov_ctx) \ 44 { \ 45 CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \ 46 return ctx; \ 47 } \ 48 static void name##_freectx(void *vctx) \ 49 { \ 50 CTX *ctx = (CTX *)vctx; \ 51 OPENSSL_clear_free(ctx, sizeof(*ctx)); \ 52 } \ 53 static void *name##_dupctx(void *ctx) \ 54 { \ 55 CTX *in = (CTX *)ctx; \ 56 CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) : NULL; \ 57 if (ret != NULL) \ 58 *ret = *in; \ 59 return ret; \ 60 } \ 61 static OSSL_FUNC_digest_final_fn name##_internal_final; \ 62 static int name##_internal_final(void *ctx, unsigned char *out, size_t *outl, \ 63 size_t outsz) \ 64 { \ 65 if (ossl_prov_is_running() && outsz >= dgstsize && fin(out, ctx)) { \ 66 *outl = dgstsize; \ 67 return 1; \ 68 } \ 69 return 0; \ 70 } \ 71 PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \ 72 const OSSL_DISPATCH ossl_##name##_functions[] = { \ 73 { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \ 74 { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))upd }, \ 75 { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))name##_internal_final }, \ 76 { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))name##_freectx }, \ 77 { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))name##_dupctx }, \ 78 PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name) 79 80 # define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END \ 81 { 0, NULL } \ 82 }; 83 84 # define IMPLEMENT_digest_functions( \ 85 name, CTX, blksize, dgstsize, flags, init, upd, fin) \ 86 static OSSL_FUNC_digest_init_fn name##_internal_init; \ 87 static int name##_internal_init(void *ctx, \ 88 ossl_unused const OSSL_PARAM params[]) \ 89 { \ 90 return ossl_prov_is_running() && init(ctx); \ 91 } \ 92 PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \ 93 upd, fin), \ 94 { OSSL_FUNC_DIGEST_INIT, (void (*)(void))name##_internal_init }, \ 95 PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END 96 97 # define IMPLEMENT_digest_functions_with_settable_ctx( \ 98 name, CTX, blksize, dgstsize, flags, init, upd, fin, \ 99 settable_ctx_params, set_ctx_params) \ 100 static OSSL_FUNC_digest_init_fn name##_internal_init; \ 101 static int name##_internal_init(void *ctx, const OSSL_PARAM params[]) \ 102 { \ 103 return ossl_prov_is_running() \ 104 && init(ctx) \ 105 && set_ctx_params(ctx, params); \ 106 } \ 107 PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \ 108 upd, fin), \ 109 { OSSL_FUNC_DIGEST_INIT, (void (*)(void))name##_internal_init }, \ 110 { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, (void (*)(void))settable_ctx_params }, \ 111 { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))set_ctx_params }, \ 112 PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END 113 114 115 const OSSL_PARAM *ossl_digest_default_gettable_params(void *provctx); 116 int ossl_digest_default_get_params(OSSL_PARAM params[], size_t blksz, 117 size_t paramsz, unsigned long flags); 118 119 # ifdef __cplusplus 120 } 121 # endif 122 123 #endif /* OSSL_PROVIDERS_DIGESTCOMMON_H */ 124