1 #include <openssl/evp.h>
2 #include <openssl/err.h>
3 #include <openssl/core.h>
4 #include <openssl/core_dispatch.h>
5 #include "internal/provider.h"
6 #include "internal/core.h"
7 #include "crypto/evp.h"
10 static int evp_mac_up_ref(void *vmac)
15 CRYPTO_UP_REF(&mac->refcnt, &ref, mac->lock);
19 static void evp_mac_free(void *vmac)
27 CRYPTO_DOWN_REF(&mac->refcnt, &ref, mac->lock);
30 OPENSSL_free(mac->type_name);
31 ossl_provider_free(mac->prov);
32 CRYPTO_THREAD_lock_free(mac->lock);
36 static void *evp_mac_new(void)
40 if ((mac = OPENSSL_zalloc(sizeof(*mac))) == NULL
41 || (mac->lock = CRYPTO_THREAD_lock_new()) == NULL) {
51 static void *evp_mac_from_algorithm(int name_id,
52 const OSSL_ALGORITHM *algodef,
55 const OSSL_DISPATCH *fns = algodef->implementation;
57 int fnmaccnt = 0, fnctxcnt = 0;
59 if ((mac = evp_mac_new()) == NULL) {
60 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
63 mac->name_id = name_id;
64 if ((mac->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
68 mac->description = algodef->algorithm_description;
70 for (; fns->function_id != 0; fns++) {
71 switch (fns->function_id) {
72 case OSSL_FUNC_MAC_NEWCTX:
73 if (mac->newctx != NULL)
75 mac->newctx = OSSL_FUNC_mac_newctx(fns);
78 case OSSL_FUNC_MAC_DUPCTX:
79 if (mac->dupctx != NULL)
81 mac->dupctx = OSSL_FUNC_mac_dupctx(fns);
83 case OSSL_FUNC_MAC_FREECTX:
84 if (mac->freectx != NULL)
86 mac->freectx = OSSL_FUNC_mac_freectx(fns);
89 case OSSL_FUNC_MAC_INIT:
90 if (mac->init != NULL)
92 mac->init = OSSL_FUNC_mac_init(fns);
95 case OSSL_FUNC_MAC_UPDATE:
96 if (mac->update != NULL)
98 mac->update = OSSL_FUNC_mac_update(fns);
101 case OSSL_FUNC_MAC_FINAL:
102 if (mac->final != NULL)
104 mac->final = OSSL_FUNC_mac_final(fns);
107 case OSSL_FUNC_MAC_GETTABLE_PARAMS:
108 if (mac->gettable_params != NULL)
110 mac->gettable_params =
111 OSSL_FUNC_mac_gettable_params(fns);
113 case OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS:
114 if (mac->gettable_ctx_params != NULL)
116 mac->gettable_ctx_params =
117 OSSL_FUNC_mac_gettable_ctx_params(fns);
119 case OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS:
120 if (mac->settable_ctx_params != NULL)
122 mac->settable_ctx_params =
123 OSSL_FUNC_mac_settable_ctx_params(fns);
125 case OSSL_FUNC_MAC_GET_PARAMS:
126 if (mac->get_params != NULL)
128 mac->get_params = OSSL_FUNC_mac_get_params(fns);
130 case OSSL_FUNC_MAC_GET_CTX_PARAMS:
131 if (mac->get_ctx_params != NULL)
133 mac->get_ctx_params = OSSL_FUNC_mac_get_ctx_params(fns);
135 case OSSL_FUNC_MAC_SET_CTX_PARAMS:
136 if (mac->set_ctx_params != NULL)
138 mac->set_ctx_params = OSSL_FUNC_mac_set_ctx_params(fns);
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.
150 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
155 ossl_provider_up_ref(prov);
160 EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
161 const char *properties)
163 return evp_generic_fetch(libctx, OSSL_OP_MAC, algorithm, properties,
164 evp_mac_from_algorithm, evp_mac_up_ref,
168 int EVP_MAC_up_ref(EVP_MAC *mac)
170 return evp_mac_up_ref(mac);
173 void EVP_MAC_free(EVP_MAC *mac)
178 const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac)
183 const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac)
185 if (mac->gettable_params == NULL)
187 return mac->gettable_params(ossl_provider_ctx(EVP_MAC_get0_provider(mac)));
190 const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac)
194 if (mac->gettable_ctx_params == NULL)
196 alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
197 return mac->gettable_ctx_params(NULL, alg);
200 const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac)
204 if (mac->settable_ctx_params == NULL)
206 alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
207 return mac->settable_ctx_params(NULL, alg);
210 const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx)
214 if (ctx->meth->gettable_ctx_params == NULL)
216 alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
217 return ctx->meth->gettable_ctx_params(ctx->algctx, alg);
220 const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx)
224 if (ctx->meth->settable_ctx_params == NULL)
226 alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
227 return ctx->meth->settable_ctx_params(ctx->algctx, alg);
230 void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
231 void (*fn)(EVP_MAC *mac, void *arg),
234 evp_generic_do_all(libctx, OSSL_OP_MAC,
235 (void (*)(void *, void *))fn, arg,
236 evp_mac_from_algorithm, evp_mac_up_ref, evp_mac_free);