digestcommon.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2019 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_numbers.h>
  12. # include <openssl/core_names.h>
  13. # include <openssl/params.h>
  14. # ifdef __cplusplus
  15. extern "C" {
  16. # endif
  17. #define PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
  18. static OSSL_OP_digest_get_params_fn name##_get_params; \
  19. static int name##_get_params(OSSL_PARAM params[]) \
  20. { \
  21. return digest_default_get_params(params, blksize, dgstsize, flags); \
  22. }
  23. #define PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name) \
  24. { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \
  25. { OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \
  26. (void (*)(void))digest_default_gettable_params }
  27. # define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START( \
  28. name, CTX, blksize, dgstsize, flags, init, upd, fin) \
  29. static OSSL_OP_digest_newctx_fn name##_newctx; \
  30. static OSSL_OP_digest_freectx_fn name##_freectx; \
  31. static OSSL_OP_digest_dupctx_fn name##_dupctx; \
  32. static void *name##_newctx(void *prov_ctx) \
  33. { \
  34. CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
  35. return ctx; \
  36. } \
  37. static void name##_freectx(void *vctx) \
  38. { \
  39. CTX *ctx = (CTX *)vctx; \
  40. OPENSSL_clear_free(ctx, sizeof(*ctx)); \
  41. } \
  42. static void *name##_dupctx(void *ctx) \
  43. { \
  44. CTX *in = (CTX *)ctx; \
  45. CTX *ret = OPENSSL_malloc(sizeof(*ret)); \
  46. *ret = *in; \
  47. return ret; \
  48. } \
  49. static OSSL_OP_digest_final_fn name##_internal_final; \
  50. static int name##_internal_final(void *ctx, unsigned char *out, size_t *outl, \
  51. size_t outsz) \
  52. { \
  53. if (outsz >= dgstsize && fin(out, ctx)) { \
  54. *outl = dgstsize; \
  55. return 1; \
  56. } \
  57. return 0; \
  58. } \
  59. PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \
  60. const OSSL_DISPATCH name##_functions[] = { \
  61. { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
  62. { OSSL_FUNC_DIGEST_INIT, (void (*)(void))init }, \
  63. { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))upd }, \
  64. { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))name##_internal_final }, \
  65. { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))name##_freectx }, \
  66. { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))name##_dupctx }, \
  67. PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name)
  68. # define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END \
  69. { 0, NULL } \
  70. };
  71. # define IMPLEMENT_digest_functions( \
  72. name, CTX, blksize, dgstsize, flags, init, upd, fin) \
  73. PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
  74. init, upd, fin), \
  75. PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
  76. # define IMPLEMENT_digest_functions_with_settable_ctx( \
  77. name, CTX, blksize, dgstsize, flags, init, upd, fin, \
  78. settable_ctx_params, set_ctx_params) \
  79. PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
  80. init, upd, fin), \
  81. { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, (void (*)(void))settable_ctx_params }, \
  82. { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))set_ctx_params }, \
  83. PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
  84. const OSSL_PARAM *digest_default_gettable_params(void);
  85. int digest_default_get_params(OSSL_PARAM params[], size_t blksz, size_t paramsz,
  86. unsigned long flags);
  87. # ifdef __cplusplus
  88. }
  89. # endif
  90. #endif /* OSSL_PROVIDERS_DIGESTCOMMON_H */