mac_meth.c 6.3 KB

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