mac_meth.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright 2022-2025 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, mac_init_found = 0;
  55. if ((mac = evp_mac_new()) == NULL) {
  56. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  57. goto err;
  58. }
  59. mac->name_id = name_id;
  60. if ((mac->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
  61. goto err;
  62. mac->description = algodef->algorithm_description;
  63. for (; fns->function_id != 0; fns++) {
  64. switch (fns->function_id) {
  65. case OSSL_FUNC_MAC_NEWCTX:
  66. if (mac->newctx != NULL)
  67. break;
  68. mac->newctx = OSSL_FUNC_mac_newctx(fns);
  69. fnctxcnt++;
  70. break;
  71. case OSSL_FUNC_MAC_DUPCTX:
  72. if (mac->dupctx != NULL)
  73. break;
  74. mac->dupctx = OSSL_FUNC_mac_dupctx(fns);
  75. break;
  76. case OSSL_FUNC_MAC_FREECTX:
  77. if (mac->freectx != NULL)
  78. break;
  79. mac->freectx = OSSL_FUNC_mac_freectx(fns);
  80. fnctxcnt++;
  81. break;
  82. case OSSL_FUNC_MAC_INIT:
  83. if (mac->init != NULL)
  84. break;
  85. mac->init = OSSL_FUNC_mac_init(fns);
  86. mac_init_found = 1;
  87. break;
  88. case OSSL_FUNC_MAC_UPDATE:
  89. if (mac->update != NULL)
  90. break;
  91. mac->update = OSSL_FUNC_mac_update(fns);
  92. fnmaccnt++;
  93. break;
  94. case OSSL_FUNC_MAC_FINAL:
  95. if (mac->final != NULL)
  96. break;
  97. mac->final = OSSL_FUNC_mac_final(fns);
  98. fnmaccnt++;
  99. break;
  100. case OSSL_FUNC_MAC_GETTABLE_PARAMS:
  101. if (mac->gettable_params != NULL)
  102. break;
  103. mac->gettable_params =
  104. OSSL_FUNC_mac_gettable_params(fns);
  105. break;
  106. case OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS:
  107. if (mac->gettable_ctx_params != NULL)
  108. break;
  109. mac->gettable_ctx_params =
  110. OSSL_FUNC_mac_gettable_ctx_params(fns);
  111. break;
  112. case OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS:
  113. if (mac->settable_ctx_params != NULL)
  114. break;
  115. mac->settable_ctx_params =
  116. OSSL_FUNC_mac_settable_ctx_params(fns);
  117. break;
  118. case OSSL_FUNC_MAC_GET_PARAMS:
  119. if (mac->get_params != NULL)
  120. break;
  121. mac->get_params = OSSL_FUNC_mac_get_params(fns);
  122. break;
  123. case OSSL_FUNC_MAC_GET_CTX_PARAMS:
  124. if (mac->get_ctx_params != NULL)
  125. break;
  126. mac->get_ctx_params = OSSL_FUNC_mac_get_ctx_params(fns);
  127. break;
  128. case OSSL_FUNC_MAC_SET_CTX_PARAMS:
  129. if (mac->set_ctx_params != NULL)
  130. break;
  131. mac->set_ctx_params = OSSL_FUNC_mac_set_ctx_params(fns);
  132. break;
  133. case OSSL_FUNC_MAC_INIT_SKEY:
  134. if (mac->init_skey != NULL)
  135. break;
  136. mac->init_skey = OSSL_FUNC_mac_init_skey(fns);
  137. mac_init_found = 1;
  138. break;
  139. }
  140. }
  141. fnmaccnt += mac_init_found;
  142. if (fnmaccnt != 3
  143. || fnctxcnt != 2) {
  144. /*
  145. * In order to be a consistent set of functions we must have at least
  146. * a complete set of "mac" functions, and a complete set of context
  147. * management functions, as well as the size function.
  148. */
  149. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  150. goto err;
  151. }
  152. if (prov != NULL && !ossl_provider_up_ref(prov))
  153. goto err;
  154. mac->prov = prov;
  155. return mac;
  156. err:
  157. evp_mac_free(mac);
  158. return NULL;
  159. }
  160. EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
  161. const char *properties)
  162. {
  163. return evp_generic_fetch(libctx, OSSL_OP_MAC, algorithm, properties,
  164. evp_mac_from_algorithm, evp_mac_up_ref,
  165. evp_mac_free);
  166. }
  167. int EVP_MAC_up_ref(EVP_MAC *mac)
  168. {
  169. return evp_mac_up_ref(mac);
  170. }
  171. void EVP_MAC_free(EVP_MAC *mac)
  172. {
  173. evp_mac_free(mac);
  174. }
  175. const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac)
  176. {
  177. return mac->prov;
  178. }
  179. const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac)
  180. {
  181. if (mac->gettable_params == NULL)
  182. return NULL;
  183. return mac->gettable_params(ossl_provider_ctx(EVP_MAC_get0_provider(mac)));
  184. }
  185. const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac)
  186. {
  187. void *alg;
  188. if (mac->gettable_ctx_params == NULL)
  189. return NULL;
  190. alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
  191. return mac->gettable_ctx_params(NULL, alg);
  192. }
  193. const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac)
  194. {
  195. void *alg;
  196. if (mac->settable_ctx_params == NULL)
  197. return NULL;
  198. alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
  199. return mac->settable_ctx_params(NULL, alg);
  200. }
  201. const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx)
  202. {
  203. void *alg;
  204. if (ctx->meth->gettable_ctx_params == NULL)
  205. return NULL;
  206. alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
  207. return ctx->meth->gettable_ctx_params(ctx->algctx, alg);
  208. }
  209. const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx)
  210. {
  211. void *alg;
  212. if (ctx->meth->settable_ctx_params == NULL)
  213. return NULL;
  214. alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
  215. return ctx->meth->settable_ctx_params(ctx->algctx, alg);
  216. }
  217. void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
  218. void (*fn)(EVP_MAC *mac, void *arg),
  219. void *arg)
  220. {
  221. evp_generic_do_all(libctx, OSSL_OP_MAC,
  222. (void (*)(void *, void *))fn, arg,
  223. evp_mac_from_algorithm, evp_mac_up_ref, evp_mac_free);
  224. }
  225. EVP_MAC *evp_mac_fetch_from_prov(OSSL_PROVIDER *prov,
  226. const char *algorithm,
  227. const char *properties)
  228. {
  229. return evp_generic_fetch_from_prov(prov, OSSL_OP_MAC,
  230. algorithm, properties,
  231. evp_mac_from_algorithm,
  232. evp_mac_up_ref,
  233. evp_mac_free);
  234. }