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
10 #include <openssl/evp.h>
11 #include <openssl/err.h>
12 #include <openssl/core.h>
13 #include <openssl/core_dispatch.h>
14 #include "internal/provider.h"
15 #include "internal/core.h"
16 #include "crypto/evp.h"
17 #include "evp_local.h"
18
evp_mac_up_ref(void * vmac)19 static int evp_mac_up_ref(void *vmac)
20 {
21 EVP_MAC *mac = vmac;
22 int ref = 0;
23
24 CRYPTO_UP_REF(&mac->refcnt, &ref);
25 return 1;
26 }
27
evp_mac_free(void * vmac)28 static void evp_mac_free(void *vmac)
29 {
30 EVP_MAC *mac = vmac;
31 int ref = 0;
32
33 if (mac == NULL)
34 return;
35
36 CRYPTO_DOWN_REF(&mac->refcnt, &ref);
37 if (ref > 0)
38 return;
39 OPENSSL_free(mac->type_name);
40 ossl_provider_free(mac->prov);
41 CRYPTO_FREE_REF(&mac->refcnt);
42 OPENSSL_free(mac);
43 }
44
evp_mac_new(void)45 static void *evp_mac_new(void)
46 {
47 EVP_MAC *mac = NULL;
48
49 if ((mac = OPENSSL_zalloc(sizeof(*mac))) == NULL
50 || !CRYPTO_NEW_REF(&mac->refcnt, 1)) {
51 evp_mac_free(mac);
52 return NULL;
53 }
54 return mac;
55 }
56
evp_mac_from_algorithm(int name_id,const OSSL_ALGORITHM * algodef,OSSL_PROVIDER * prov)57 static void *evp_mac_from_algorithm(int name_id,
58 const OSSL_ALGORITHM *algodef,
59 OSSL_PROVIDER *prov)
60 {
61 const OSSL_DISPATCH *fns = algodef->implementation;
62 EVP_MAC *mac = NULL;
63 int fnmaccnt = 0, fnctxcnt = 0, mac_init_found = 0;
64
65 if ((mac = evp_mac_new()) == NULL) {
66 ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
67 goto err;
68 }
69 mac->name_id = name_id;
70
71 if ((mac->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
72 goto err;
73
74 mac->description = algodef->algorithm_description;
75
76 for (; fns->function_id != 0; fns++) {
77 switch (fns->function_id) {
78 case OSSL_FUNC_MAC_NEWCTX:
79 if (mac->newctx != NULL)
80 break;
81 mac->newctx = OSSL_FUNC_mac_newctx(fns);
82 fnctxcnt++;
83 break;
84 case OSSL_FUNC_MAC_DUPCTX:
85 if (mac->dupctx != NULL)
86 break;
87 mac->dupctx = OSSL_FUNC_mac_dupctx(fns);
88 break;
89 case OSSL_FUNC_MAC_FREECTX:
90 if (mac->freectx != NULL)
91 break;
92 mac->freectx = OSSL_FUNC_mac_freectx(fns);
93 fnctxcnt++;
94 break;
95 case OSSL_FUNC_MAC_INIT:
96 if (mac->init != NULL)
97 break;
98 mac->init = OSSL_FUNC_mac_init(fns);
99 mac_init_found = 1;
100 break;
101 case OSSL_FUNC_MAC_UPDATE:
102 if (mac->update != NULL)
103 break;
104 mac->update = OSSL_FUNC_mac_update(fns);
105 fnmaccnt++;
106 break;
107 case OSSL_FUNC_MAC_FINAL:
108 if (mac->final != NULL)
109 break;
110 mac->final = OSSL_FUNC_mac_final(fns);
111 fnmaccnt++;
112 break;
113 case OSSL_FUNC_MAC_GETTABLE_PARAMS:
114 if (mac->gettable_params != NULL)
115 break;
116 mac->gettable_params =
117 OSSL_FUNC_mac_gettable_params(fns);
118 break;
119 case OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS:
120 if (mac->gettable_ctx_params != NULL)
121 break;
122 mac->gettable_ctx_params =
123 OSSL_FUNC_mac_gettable_ctx_params(fns);
124 break;
125 case OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS:
126 if (mac->settable_ctx_params != NULL)
127 break;
128 mac->settable_ctx_params =
129 OSSL_FUNC_mac_settable_ctx_params(fns);
130 break;
131 case OSSL_FUNC_MAC_GET_PARAMS:
132 if (mac->get_params != NULL)
133 break;
134 mac->get_params = OSSL_FUNC_mac_get_params(fns);
135 break;
136 case OSSL_FUNC_MAC_GET_CTX_PARAMS:
137 if (mac->get_ctx_params != NULL)
138 break;
139 mac->get_ctx_params = OSSL_FUNC_mac_get_ctx_params(fns);
140 break;
141 case OSSL_FUNC_MAC_SET_CTX_PARAMS:
142 if (mac->set_ctx_params != NULL)
143 break;
144 mac->set_ctx_params = OSSL_FUNC_mac_set_ctx_params(fns);
145 break;
146 case OSSL_FUNC_MAC_INIT_SKEY:
147 if (mac->init_skey != NULL)
148 break;
149 mac->init_skey = OSSL_FUNC_mac_init_skey(fns);
150 mac_init_found = 1;
151 break;
152 }
153 }
154 fnmaccnt += mac_init_found;
155 if (fnmaccnt != 3
156 || fnctxcnt != 2) {
157 /*
158 * In order to be a consistent set of functions we must have at least
159 * a complete set of "mac" functions, and a complete set of context
160 * management functions, as well as the size function.
161 */
162 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
163 goto err;
164 }
165
166 if (prov != NULL && !ossl_provider_up_ref(prov))
167 goto err;
168
169 mac->prov = prov;
170
171 return mac;
172
173 err:
174 evp_mac_free(mac);
175 return NULL;
176 }
177
EVP_MAC_fetch(OSSL_LIB_CTX * libctx,const char * algorithm,const char * properties)178 EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
179 const char *properties)
180 {
181 return evp_generic_fetch(libctx, OSSL_OP_MAC, algorithm, properties,
182 evp_mac_from_algorithm, evp_mac_up_ref,
183 evp_mac_free);
184 }
185
EVP_MAC_up_ref(EVP_MAC * mac)186 int EVP_MAC_up_ref(EVP_MAC *mac)
187 {
188 return evp_mac_up_ref(mac);
189 }
190
EVP_MAC_free(EVP_MAC * mac)191 void EVP_MAC_free(EVP_MAC *mac)
192 {
193 evp_mac_free(mac);
194 }
195
EVP_MAC_get0_provider(const EVP_MAC * mac)196 const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac)
197 {
198 return mac->prov;
199 }
200
EVP_MAC_gettable_params(const EVP_MAC * mac)201 const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac)
202 {
203 if (mac->gettable_params == NULL)
204 return NULL;
205 return mac->gettable_params(ossl_provider_ctx(EVP_MAC_get0_provider(mac)));
206 }
207
EVP_MAC_gettable_ctx_params(const EVP_MAC * mac)208 const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac)
209 {
210 void *alg;
211
212 if (mac->gettable_ctx_params == NULL)
213 return NULL;
214 alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
215 return mac->gettable_ctx_params(NULL, alg);
216 }
217
EVP_MAC_settable_ctx_params(const EVP_MAC * mac)218 const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac)
219 {
220 void *alg;
221
222 if (mac->settable_ctx_params == NULL)
223 return NULL;
224 alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
225 return mac->settable_ctx_params(NULL, alg);
226 }
227
EVP_MAC_CTX_gettable_params(EVP_MAC_CTX * ctx)228 const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx)
229 {
230 void *alg;
231
232 if (ctx->meth->gettable_ctx_params == NULL)
233 return NULL;
234 alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
235 return ctx->meth->gettable_ctx_params(ctx->algctx, alg);
236 }
237
EVP_MAC_CTX_settable_params(EVP_MAC_CTX * ctx)238 const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx)
239 {
240 void *alg;
241
242 if (ctx->meth->settable_ctx_params == NULL)
243 return NULL;
244 alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
245 return ctx->meth->settable_ctx_params(ctx->algctx, alg);
246 }
247
EVP_MAC_do_all_provided(OSSL_LIB_CTX * libctx,void (* fn)(EVP_MAC * mac,void * arg),void * arg)248 void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
249 void (*fn)(EVP_MAC *mac, void *arg),
250 void *arg)
251 {
252 evp_generic_do_all(libctx, OSSL_OP_MAC,
253 (void (*)(void *, void *))fn, arg,
254 evp_mac_from_algorithm, evp_mac_up_ref, evp_mac_free);
255 }
256
evp_mac_fetch_from_prov(OSSL_PROVIDER * prov,const char * algorithm,const char * properties)257 EVP_MAC *evp_mac_fetch_from_prov(OSSL_PROVIDER *prov,
258 const char *algorithm,
259 const char *properties)
260 {
261 return evp_generic_fetch_from_prov(prov, OSSL_OP_MAC,
262 algorithm, properties,
263 evp_mac_from_algorithm,
264 evp_mac_up_ref,
265 evp_mac_free);
266 }
267