mac_meth.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2022-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/evp.h>
  10. #include <openssl/err.h>
  11. #include <openssl/core.h>
  12. #include <openssl/core_dispatch.h>
  13. #include "internal/provider.h"
  14. #include "internal/core.h"
  15. #include "crypto/evp.h"
  16. #include "evp_local.h"
  17. static int evp_mac_up_ref(void *vmac)
  18. {
  19. EVP_MAC *mac = vmac;
  20. int ref = 0;
  21. CRYPTO_UP_REF(&mac->refcnt, &ref);
  22. return 1;
  23. }
  24. static void evp_mac_free(void *vmac)
  25. {
  26. EVP_MAC *mac = vmac;
  27. int ref = 0;
  28. if (mac == NULL)
  29. return;
  30. CRYPTO_DOWN_REF(&mac->refcnt, &ref);
  31. if (ref > 0)
  32. return;
  33. OPENSSL_free(mac->type_name);
  34. ossl_provider_free(mac->prov);
  35. CRYPTO_FREE_REF(&mac->refcnt);
  36. OPENSSL_free(mac);
  37. }
  38. static void *evp_mac_new(void)
  39. {
  40. EVP_MAC *mac = NULL;
  41. if ((mac = OPENSSL_zalloc(sizeof(*mac))) == NULL
  42. || !CRYPTO_NEW_REF(&mac->refcnt, 1)) {
  43. evp_mac_free(mac);
  44. return NULL;
  45. }
  46. return mac;
  47. }
  48. static void *evp_mac_from_algorithm(int name_id,
  49. const OSSL_ALGORITHM *algodef,
  50. OSSL_PROVIDER *prov)
  51. {
  52. const OSSL_DISPATCH *fns = algodef->implementation;
  53. EVP_MAC *mac = NULL;
  54. int fnmaccnt = 0, fnctxcnt = 0;
  55. if ((mac = evp_mac_new()) == NULL) {
  56. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  57. return NULL;
  58. }
  59. mac->name_id = name_id;
  60. if ((mac->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
  61. evp_mac_free(mac);
  62. return NULL;
  63. }
  64. mac->description = algodef->algorithm_description;
  65. for (; fns->function_id != 0; fns++) {
  66. switch (fns->function_id) {
  67. case OSSL_FUNC_MAC_NEWCTX:
  68. if (mac->newctx != NULL)
  69. break;
  70. mac->newctx = OSSL_FUNC_mac_newctx(fns);
  71. fnctxcnt++;
  72. break;
  73. case OSSL_FUNC_MAC_DUPCTX:
  74. if (mac->dupctx != NULL)
  75. break;
  76. mac->dupctx = OSSL_FUNC_mac_dupctx(fns);
  77. break;
  78. case OSSL_FUNC_MAC_FREECTX:
  79. if (mac->freectx != NULL)
  80. break;
  81. mac->freectx = OSSL_FUNC_mac_freectx(fns);
  82. fnctxcnt++;
  83. break;
  84. case OSSL_FUNC_MAC_INIT:
  85. if (mac->init != NULL)
  86. break;
  87. mac->init = OSSL_FUNC_mac_init(fns);
  88. fnmaccnt++;
  89. break;
  90. case OSSL_FUNC_MAC_UPDATE:
  91. if (mac->update != NULL)
  92. break;
  93. mac->update = OSSL_FUNC_mac_update(fns);
  94. fnmaccnt++;
  95. break;
  96. case OSSL_FUNC_MAC_FINAL:
  97. if (mac->final != NULL)
  98. break;
  99. mac->final = OSSL_FUNC_mac_final(fns);
  100. fnmaccnt++;
  101. break;
  102. case OSSL_FUNC_MAC_GETTABLE_PARAMS:
  103. if (mac->gettable_params != NULL)
  104. break;
  105. mac->gettable_params =
  106. OSSL_FUNC_mac_gettable_params(fns);
  107. break;
  108. case OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS:
  109. if (mac->gettable_ctx_params != NULL)
  110. break;
  111. mac->gettable_ctx_params =
  112. OSSL_FUNC_mac_gettable_ctx_params(fns);
  113. break;
  114. case OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS:
  115. if (mac->settable_ctx_params != NULL)
  116. break;
  117. mac->settable_ctx_params =
  118. OSSL_FUNC_mac_settable_ctx_params(fns);
  119. break;
  120. case OSSL_FUNC_MAC_GET_PARAMS:
  121. if (mac->get_params != NULL)
  122. break;
  123. mac->get_params = OSSL_FUNC_mac_get_params(fns);
  124. break;
  125. case OSSL_FUNC_MAC_GET_CTX_PARAMS:
  126. if (mac->get_ctx_params != NULL)
  127. break;
  128. mac->get_ctx_params = OSSL_FUNC_mac_get_ctx_params(fns);
  129. break;
  130. case OSSL_FUNC_MAC_SET_CTX_PARAMS:
  131. if (mac->set_ctx_params != NULL)
  132. break;
  133. mac->set_ctx_params = OSSL_FUNC_mac_set_ctx_params(fns);
  134. break;
  135. }
  136. }
  137. if (fnmaccnt != 3
  138. || fnctxcnt != 2) {
  139. /*
  140. * In order to be a consistent set of functions we must have at least
  141. * a complete set of "mac" functions, and a complete set of context
  142. * management functions, as well as the size function.
  143. */
  144. evp_mac_free(mac);
  145. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  146. return NULL;
  147. }
  148. mac->prov = prov;
  149. if (prov != NULL)
  150. ossl_provider_up_ref(prov);
  151. return mac;
  152. }
  153. EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
  154. const char *properties)
  155. {
  156. return evp_generic_fetch(libctx, OSSL_OP_MAC, algorithm, properties,
  157. evp_mac_from_algorithm, evp_mac_up_ref,
  158. evp_mac_free);
  159. }
  160. int EVP_MAC_up_ref(EVP_MAC *mac)
  161. {
  162. return evp_mac_up_ref(mac);
  163. }
  164. void EVP_MAC_free(EVP_MAC *mac)
  165. {
  166. evp_mac_free(mac);
  167. }
  168. const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac)
  169. {
  170. return mac->prov;
  171. }
  172. const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac)
  173. {
  174. if (mac->gettable_params == NULL)
  175. return NULL;
  176. return mac->gettable_params(ossl_provider_ctx(EVP_MAC_get0_provider(mac)));
  177. }
  178. const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac)
  179. {
  180. void *alg;
  181. if (mac->gettable_ctx_params == NULL)
  182. return NULL;
  183. alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
  184. return mac->gettable_ctx_params(NULL, alg);
  185. }
  186. const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac)
  187. {
  188. void *alg;
  189. if (mac->settable_ctx_params == NULL)
  190. return NULL;
  191. alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
  192. return mac->settable_ctx_params(NULL, alg);
  193. }
  194. const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx)
  195. {
  196. void *alg;
  197. if (ctx->meth->gettable_ctx_params == NULL)
  198. return NULL;
  199. alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
  200. return ctx->meth->gettable_ctx_params(ctx->algctx, alg);
  201. }
  202. const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx)
  203. {
  204. void *alg;
  205. if (ctx->meth->settable_ctx_params == NULL)
  206. return NULL;
  207. alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
  208. return ctx->meth->settable_ctx_params(ctx->algctx, alg);
  209. }
  210. void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
  211. void (*fn)(EVP_MAC *mac, void *arg),
  212. void *arg)
  213. {
  214. evp_generic_do_all(libctx, OSSL_OP_MAC,
  215. (void (*)(void *, void *))fn, arg,
  216. evp_mac_from_algorithm, evp_mac_up_ref, evp_mac_free);
  217. }