mac_meth.c 6.9 KB

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