blake2_prov.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2019-2023 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. #include <openssl/crypto.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/proverr.h>
  12. #include <openssl/err.h>
  13. #include "prov/blake2.h"
  14. #include "prov/digestcommon.h"
  15. #include "prov/implementations.h"
  16. #define IMPLEMENT_BLAKE_functions(variant, VARIANT, variantsize) \
  17. static const OSSL_PARAM known_blake##variant##_ctx_params[] = { \
  18. {OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0}, \
  19. OSSL_PARAM_END \
  20. }; \
  21. \
  22. const OSSL_PARAM *ossl_blake##variant##_gettable_ctx_params(ossl_unused void *ctx, \
  23. ossl_unused void *pctx) \
  24. { \
  25. return known_blake##variant##_ctx_params; \
  26. } \
  27. \
  28. const OSSL_PARAM *ossl_blake##variant##_settable_ctx_params(ossl_unused void *ctx, \
  29. ossl_unused void *pctx) \
  30. { \
  31. return known_blake##variant##_ctx_params; \
  32. } \
  33. \
  34. int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
  35. { \
  36. struct blake##variant##_md_data_st *mdctx = vctx; \
  37. OSSL_PARAM *p; \
  38. \
  39. BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
  40. \
  41. if (ctx == NULL) \
  42. return 0; \
  43. if (params == NULL) \
  44. return 1; \
  45. \
  46. p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
  47. if (p != NULL \
  48. && !OSSL_PARAM_set_uint(p, (unsigned int)mdctx->params.digest_length)) { \
  49. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); \
  50. return 0; \
  51. } \
  52. \
  53. return 1; \
  54. } \
  55. \
  56. int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
  57. { \
  58. size_t size; \
  59. struct blake##variant##_md_data_st *mdctx = vctx; \
  60. const OSSL_PARAM *p; \
  61. \
  62. BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
  63. \
  64. if (ctx == NULL) \
  65. return 0; \
  66. if (params == NULL) \
  67. return 1; \
  68. \
  69. p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE); \
  70. if (p != NULL) { \
  71. if (!OSSL_PARAM_get_size_t(p, &size)) { \
  72. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); \
  73. return 0; \
  74. } \
  75. if (size < 1 || size > BLAKE##VARIANT##_OUTBYTES) { \
  76. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
  77. return 0; \
  78. } \
  79. ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size); \
  80. } \
  81. \
  82. return 1; \
  83. } \
  84. \
  85. static int ossl_blake##variantsize##_init(void *ctx) \
  86. { \
  87. struct blake##variant##_md_data_st *mdctx = ctx; \
  88. uint8_t digest_length = mdctx->params.digest_length; \
  89. \
  90. ossl_blake##variant##_param_init(&mdctx->params); \
  91. if (digest_length != 0) \
  92. mdctx->params.digest_length = digest_length; \
  93. return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
  94. } \
  95. \
  96. static OSSL_FUNC_digest_init_fn blake##variantsize##_internal_init; \
  97. static OSSL_FUNC_digest_newctx_fn blake##variantsize##_newctx; \
  98. static OSSL_FUNC_digest_freectx_fn blake##variantsize##_freectx; \
  99. static OSSL_FUNC_digest_dupctx_fn blake##variantsize##_dupctx; \
  100. static OSSL_FUNC_digest_final_fn blake##variantsize##_internal_final; \
  101. static OSSL_FUNC_digest_get_params_fn blake##variantsize##_get_params; \
  102. \
  103. static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
  104. { \
  105. return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
  106. && ossl_blake##variantsize##_init(ctx); \
  107. } \
  108. \
  109. static void *blake##variantsize##_newctx(void *prov_ctx) \
  110. { \
  111. struct blake##variant##_md_data_st *ctx; \
  112. \
  113. ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
  114. return ctx; \
  115. } \
  116. \
  117. static void blake##variantsize##_freectx(void *vctx) \
  118. { \
  119. struct blake##variant##_md_data_st *ctx; \
  120. \
  121. ctx = (struct blake##variant##_md_data_st *)vctx; \
  122. OPENSSL_clear_free(ctx, sizeof(*ctx)); \
  123. } \
  124. \
  125. static void *blake##variantsize##_dupctx(void *ctx) \
  126. { \
  127. struct blake##variant##_md_data_st *in, *ret; \
  128. \
  129. in = (struct blake##variant##_md_data_st *)ctx; \
  130. ret = ossl_prov_is_running()? OPENSSL_malloc(sizeof(*ret)) : NULL; \
  131. if (ret != NULL) \
  132. *ret = *in; \
  133. return ret; \
  134. } \
  135. \
  136. static int blake##variantsize##_internal_final(void *ctx, unsigned char *out, \
  137. size_t *outl, size_t outsz) \
  138. { \
  139. struct blake##variant##_md_data_st *b_ctx; \
  140. \
  141. b_ctx = (struct blake##variant##_md_data_st *)ctx; \
  142. \
  143. if (!ossl_prov_is_running()) \
  144. return 0; \
  145. \
  146. *outl = b_ctx->ctx.outlen; \
  147. \
  148. if (outsz == 0) \
  149. return 1; \
  150. \
  151. if (outsz < *outl) { \
  152. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
  153. return 0; \
  154. } \
  155. \
  156. return ossl_blake##variant##_final(out, ctx); \
  157. } \
  158. \
  159. static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
  160. { \
  161. return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
  162. } \
  163. \
  164. const OSSL_DISPATCH ossl_blake##variantsize##_functions[] = { \
  165. {OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))blake##variantsize##_newctx}, \
  166. {OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))ossl_blake##variant##_update}, \
  167. {OSSL_FUNC_DIGEST_FINAL, (void (*)(void))blake##variantsize##_internal_final}, \
  168. {OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))blake##variantsize##_freectx}, \
  169. {OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))blake##variantsize##_dupctx}, \
  170. {OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))blake##variantsize##_get_params}, \
  171. {OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \
  172. (void (*)(void))ossl_digest_default_gettable_params}, \
  173. {OSSL_FUNC_DIGEST_INIT, (void (*)(void))blake##variantsize##_internal_init}, \
  174. {OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS, \
  175. (void (*)(void))ossl_blake##variant##_gettable_ctx_params}, \
  176. {OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \
  177. (void (*)(void))ossl_blake##variant##_settable_ctx_params}, \
  178. {OSSL_FUNC_DIGEST_GET_CTX_PARAMS, \
  179. (void (*)(void))ossl_blake##variant##_get_ctx_params}, \
  180. {OSSL_FUNC_DIGEST_SET_CTX_PARAMS, \
  181. (void (*)(void))ossl_blake##variant##_set_ctx_params}, \
  182. {0, NULL} \
  183. };
  184. IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
  185. IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)