blake2_prov.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/proverr.h>
  11. #include "prov/blake2.h"
  12. #include "prov/digestcommon.h"
  13. #include "prov/implementations.h"
  14. static int ossl_blake2s256_init(void *ctx)
  15. {
  16. BLAKE2S_PARAM P;
  17. ossl_blake2s_param_init(&P);
  18. return ossl_blake2s_init((BLAKE2S_CTX *)ctx, &P);
  19. }
  20. static int ossl_blake2b512_init(void *ctx)
  21. {
  22. struct blake2b_md_data_st *mdctx = ctx;
  23. uint8_t digest_length = mdctx->params.digest_length;
  24. ossl_blake2b_param_init(&mdctx->params);
  25. if (digest_length != 0)
  26. mdctx->params.digest_length = digest_length;
  27. return ossl_blake2b_init(&mdctx->ctx, &mdctx->params);
  28. }
  29. /* ossl_blake2s256_functions */
  30. IMPLEMENT_digest_functions(blake2s256, BLAKE2S_CTX,
  31. BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH, 0,
  32. ossl_blake2s256_init, ossl_blake2s_update,
  33. ossl_blake2s_final)
  34. /* ossl_blake2b512_functions */
  35. static OSSL_FUNC_digest_init_fn blake2b512_internal_init;
  36. static OSSL_FUNC_digest_newctx_fn blake2b512_newctx;
  37. static OSSL_FUNC_digest_freectx_fn blake2b512_freectx;
  38. static OSSL_FUNC_digest_dupctx_fn blake2b512_dupctx;
  39. static OSSL_FUNC_digest_final_fn blake2b512_internal_final;
  40. static OSSL_FUNC_digest_get_params_fn blake2b512_get_params;
  41. static int blake2b512_internal_init(void *ctx, const OSSL_PARAM params[])
  42. {
  43. return ossl_prov_is_running() && ossl_blake2b_set_ctx_params(ctx, params)
  44. && ossl_blake2b512_init(ctx);
  45. }
  46. static void *blake2b512_newctx(void *prov_ctx)
  47. {
  48. struct blake2b_md_data_st *ctx;
  49. ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL;
  50. return ctx;
  51. }
  52. static void blake2b512_freectx(void *vctx)
  53. {
  54. struct blake2b_md_data_st *ctx;
  55. ctx = (struct blake2b_md_data_st *)vctx;
  56. OPENSSL_clear_free(ctx, sizeof(*ctx));
  57. }
  58. static void *blake2b512_dupctx(void *ctx)
  59. {
  60. struct blake2b_md_data_st *in, *ret;
  61. in = (struct blake2b_md_data_st *)ctx;
  62. ret = ossl_prov_is_running()? OPENSSL_malloc(sizeof(*ret)) : NULL;
  63. if (ret != NULL)
  64. *ret = *in;
  65. return ret;
  66. }
  67. static int blake2b512_internal_final(void *ctx, unsigned char *out,
  68. size_t *outl, size_t outsz)
  69. {
  70. struct blake2b_md_data_st *b_ctx;
  71. b_ctx = (struct blake2b_md_data_st *)ctx;
  72. if (!ossl_prov_is_running())
  73. return 0;
  74. *outl = b_ctx->ctx.outlen;
  75. if (outsz == 0)
  76. return 1;
  77. if (outsz < *outl) {
  78. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);
  79. return 0;
  80. }
  81. return ossl_blake2b_final(out, ctx);
  82. }
  83. static int blake2b512_get_params(OSSL_PARAM params[])
  84. {
  85. return ossl_digest_default_get_params(params, BLAKE2B_BLOCKBYTES, 64, 0);
  86. }
  87. const OSSL_DISPATCH ossl_blake2b512_functions[] = {
  88. {OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))blake2b512_newctx},
  89. {OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))ossl_blake2b_update},
  90. {OSSL_FUNC_DIGEST_FINAL, (void (*)(void))blake2b512_internal_final},
  91. {OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))blake2b512_freectx},
  92. {OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))blake2b512_dupctx},
  93. {OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))blake2b512_get_params},
  94. {OSSL_FUNC_DIGEST_GETTABLE_PARAMS,
  95. (void (*)(void))ossl_digest_default_gettable_params},
  96. {OSSL_FUNC_DIGEST_INIT, (void (*)(void))blake2b512_internal_init},
  97. {OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS,
  98. (void (*)(void))ossl_blake2b_gettable_ctx_params},
  99. {OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS,
  100. (void (*)(void))ossl_blake2b_settable_ctx_params},
  101. {OSSL_FUNC_DIGEST_GET_CTX_PARAMS,
  102. (void (*)(void))ossl_blake2b_get_ctx_params},
  103. {OSSL_FUNC_DIGEST_SET_CTX_PARAMS,
  104. (void (*)(void))ossl_blake2b_set_ctx_params},
  105. {0, NULL}
  106. };