digestcommon.h 7.3 KB

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