1 #include <openssl/evp.h>
2 #include <openssl/err.h>
3 #include <openssl/core.h>
4 #include <openssl/core_numbers.h>
5 #include "crypto/evp.h"
6 #include "internal/provider.h"
9 static int evp_mac_up_ref(void *vmac)
14 CRYPTO_UP_REF(&mac->refcnt, &ref, mac->lock);
18 static void evp_mac_free(void *vmac)
26 CRYPTO_DOWN_REF(&mac->refcnt, &ref, mac->lock);
29 ossl_provider_free(mac->prov);
30 CRYPTO_THREAD_lock_free(mac->lock);
34 static void *evp_mac_new(void)
38 if ((mac = OPENSSL_zalloc(sizeof(*mac))) == NULL
39 || (mac->lock = CRYPTO_THREAD_lock_new()) == NULL) {
49 static void *evp_mac_from_dispatch(int name_id,
50 const OSSL_DISPATCH *fns,
54 int fnmaccnt = 0, fnctxcnt = 0;
56 if ((mac = evp_mac_new()) == NULL) {
57 EVPerr(0, ERR_R_MALLOC_FAILURE);
60 mac->name_id = name_id;
62 for (; fns->function_id != 0; fns++) {
63 switch (fns->function_id) {
64 case OSSL_FUNC_MAC_NEWCTX:
65 if (mac->newctx != NULL)
67 mac->newctx = OSSL_get_OP_mac_newctx(fns);
70 case OSSL_FUNC_MAC_DUPCTX:
71 if (mac->dupctx != NULL)
73 mac->dupctx = OSSL_get_OP_mac_dupctx(fns);
75 case OSSL_FUNC_MAC_FREECTX:
76 if (mac->freectx != NULL)
78 mac->freectx = OSSL_get_OP_mac_freectx(fns);
81 case OSSL_FUNC_MAC_INIT:
82 if (mac->init != NULL)
84 mac->init = OSSL_get_OP_mac_init(fns);
87 case OSSL_FUNC_MAC_UPDATE:
88 if (mac->update != NULL)
90 mac->update = OSSL_get_OP_mac_update(fns);
93 case OSSL_FUNC_MAC_FINAL:
94 if (mac->final != NULL)
96 mac->final = OSSL_get_OP_mac_final(fns);
99 case OSSL_FUNC_MAC_GETTABLE_PARAMS:
100 if (mac->gettable_params != NULL)
102 mac->gettable_params =
103 OSSL_get_OP_mac_gettable_params(fns);
105 case OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS:
106 if (mac->gettable_ctx_params != NULL)
108 mac->gettable_ctx_params =
109 OSSL_get_OP_mac_gettable_ctx_params(fns);
111 case OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS:
112 if (mac->settable_ctx_params != NULL)
114 mac->settable_ctx_params =
115 OSSL_get_OP_mac_settable_ctx_params(fns);
117 case OSSL_FUNC_MAC_GET_PARAMS:
118 if (mac->get_params != NULL)
120 mac->get_params = OSSL_get_OP_mac_get_params(fns);
122 case OSSL_FUNC_MAC_GET_CTX_PARAMS:
123 if (mac->get_ctx_params != NULL)
125 mac->get_ctx_params = OSSL_get_OP_mac_get_ctx_params(fns);
127 case OSSL_FUNC_MAC_SET_CTX_PARAMS:
128 if (mac->set_ctx_params != NULL)
130 mac->set_ctx_params = OSSL_get_OP_mac_set_ctx_params(fns);
137 * In order to be a consistent set of functions we must have at least
138 * a complete set of "mac" functions, and a complete set of context
139 * management functions, as well as the size function.
142 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
147 ossl_provider_up_ref(prov);
152 EVP_MAC *EVP_MAC_fetch(OPENSSL_CTX *libctx, const char *algorithm,
153 const char *properties)
155 return evp_generic_fetch(libctx, OSSL_OP_MAC, algorithm, properties,
156 evp_mac_from_dispatch, evp_mac_up_ref,
160 int EVP_MAC_up_ref(EVP_MAC *mac)
162 return evp_mac_up_ref(mac);
165 void EVP_MAC_free(EVP_MAC *mac)
170 const OSSL_PROVIDER *EVP_MAC_provider(const EVP_MAC *mac)
175 const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac)
177 if (mac->gettable_params == NULL)
179 return mac->gettable_params();
182 const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac)
184 if (mac->gettable_ctx_params == NULL)
186 return mac->gettable_ctx_params();
189 const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac)
191 if (mac->settable_ctx_params == NULL)
193 return mac->settable_ctx_params();
196 void EVP_MAC_do_all_provided(OPENSSL_CTX *libctx,
197 void (*fn)(EVP_MAC *mac, void *arg),
200 evp_generic_do_all(libctx, OSSL_OP_MAC,
201 (void (*)(void *, void *))fn, arg,
202 evp_mac_from_dispatch, evp_mac_free);