]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/mac_meth.c
342aadc996523215dec8052094d5b47471702ad0
[thirdparty/openssl.git] / crypto / evp / mac_meth.c
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"
8 #include "evp_local.h"
9
10 static int evp_mac_up_ref(void *vmac)
11 {
12 EVP_MAC *mac = vmac;
13 int ref = 0;
14
15 CRYPTO_UP_REF(&mac->refcnt, &ref, mac->lock);
16 return 1;
17 }
18
19 static void evp_mac_free(void *vmac)
20 {
21 EVP_MAC *mac = vmac;
22 int ref = 0;
23
24 if (mac == NULL)
25 return;
26
27 CRYPTO_DOWN_REF(&mac->refcnt, &ref, mac->lock);
28 if (ref > 0)
29 return;
30 OPENSSL_free(mac->type_name);
31 ossl_provider_free(mac->prov);
32 CRYPTO_THREAD_lock_free(mac->lock);
33 OPENSSL_free(mac);
34 }
35
36 static void *evp_mac_new(void)
37 {
38 EVP_MAC *mac = NULL;
39
40 if ((mac = OPENSSL_zalloc(sizeof(*mac))) == NULL
41 || (mac->lock = CRYPTO_THREAD_lock_new()) == NULL) {
42 evp_mac_free(mac);
43 return NULL;
44 }
45
46 mac->refcnt = 1;
47
48 return mac;
49 }
50
51 static void *evp_mac_from_algorithm(int name_id,
52 const OSSL_ALGORITHM *algodef,
53 OSSL_PROVIDER *prov)
54 {
55 const OSSL_DISPATCH *fns = algodef->implementation;
56 EVP_MAC *mac = NULL;
57 int fnmaccnt = 0, fnctxcnt = 0;
58
59 if ((mac = evp_mac_new()) == NULL) {
60 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
61 return NULL;
62 }
63 mac->name_id = name_id;
64 if ((mac->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
65 evp_mac_free(mac);
66 return NULL;
67 }
68 mac->description = algodef->algorithm_description;
69
70 for (; fns->function_id != 0; fns++) {
71 switch (fns->function_id) {
72 case OSSL_FUNC_MAC_NEWCTX:
73 if (mac->newctx != NULL)
74 break;
75 mac->newctx = OSSL_FUNC_mac_newctx(fns);
76 fnctxcnt++;
77 break;
78 case OSSL_FUNC_MAC_DUPCTX:
79 if (mac->dupctx != NULL)
80 break;
81 mac->dupctx = OSSL_FUNC_mac_dupctx(fns);
82 break;
83 case OSSL_FUNC_MAC_FREECTX:
84 if (mac->freectx != NULL)
85 break;
86 mac->freectx = OSSL_FUNC_mac_freectx(fns);
87 fnctxcnt++;
88 break;
89 case OSSL_FUNC_MAC_INIT:
90 if (mac->init != NULL)
91 break;
92 mac->init = OSSL_FUNC_mac_init(fns);
93 fnmaccnt++;
94 break;
95 case OSSL_FUNC_MAC_UPDATE:
96 if (mac->update != NULL)
97 break;
98 mac->update = OSSL_FUNC_mac_update(fns);
99 fnmaccnt++;
100 break;
101 case OSSL_FUNC_MAC_FINAL:
102 if (mac->final != NULL)
103 break;
104 mac->final = OSSL_FUNC_mac_final(fns);
105 fnmaccnt++;
106 break;
107 case OSSL_FUNC_MAC_GETTABLE_PARAMS:
108 if (mac->gettable_params != NULL)
109 break;
110 mac->gettable_params =
111 OSSL_FUNC_mac_gettable_params(fns);
112 break;
113 case OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS:
114 if (mac->gettable_ctx_params != NULL)
115 break;
116 mac->gettable_ctx_params =
117 OSSL_FUNC_mac_gettable_ctx_params(fns);
118 break;
119 case OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS:
120 if (mac->settable_ctx_params != NULL)
121 break;
122 mac->settable_ctx_params =
123 OSSL_FUNC_mac_settable_ctx_params(fns);
124 break;
125 case OSSL_FUNC_MAC_GET_PARAMS:
126 if (mac->get_params != NULL)
127 break;
128 mac->get_params = OSSL_FUNC_mac_get_params(fns);
129 break;
130 case OSSL_FUNC_MAC_GET_CTX_PARAMS:
131 if (mac->get_ctx_params != NULL)
132 break;
133 mac->get_ctx_params = OSSL_FUNC_mac_get_ctx_params(fns);
134 break;
135 case OSSL_FUNC_MAC_SET_CTX_PARAMS:
136 if (mac->set_ctx_params != NULL)
137 break;
138 mac->set_ctx_params = OSSL_FUNC_mac_set_ctx_params(fns);
139 break;
140 }
141 }
142 if (fnmaccnt != 3
143 || fnctxcnt != 2) {
144 /*
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.
148 */
149 evp_mac_free(mac);
150 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
151 return NULL;
152 }
153 mac->prov = prov;
154 if (prov != NULL)
155 ossl_provider_up_ref(prov);
156
157 return mac;
158 }
159
160 EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
161 const char *properties)
162 {
163 return evp_generic_fetch(libctx, OSSL_OP_MAC, algorithm, properties,
164 evp_mac_from_algorithm, evp_mac_up_ref,
165 evp_mac_free);
166 }
167
168 int EVP_MAC_up_ref(EVP_MAC *mac)
169 {
170 return evp_mac_up_ref(mac);
171 }
172
173 void EVP_MAC_free(EVP_MAC *mac)
174 {
175 evp_mac_free(mac);
176 }
177
178 const OSSL_PROVIDER *EVP_MAC_provider(const EVP_MAC *mac)
179 {
180 return mac->prov;
181 }
182
183 const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac)
184 {
185 if (mac->gettable_params == NULL)
186 return NULL;
187 return mac->gettable_params(ossl_provider_ctx(EVP_MAC_provider(mac)));
188 }
189
190 const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac)
191 {
192 void *alg;
193
194 if (mac->gettable_ctx_params == NULL)
195 return NULL;
196 alg = ossl_provider_ctx(EVP_MAC_provider(mac));
197 return mac->gettable_ctx_params(NULL, alg);
198 }
199
200 const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac)
201 {
202 void *alg;
203
204 if (mac->settable_ctx_params == NULL)
205 return NULL;
206 alg = ossl_provider_ctx(EVP_MAC_provider(mac));
207 return mac->settable_ctx_params(NULL, alg);
208 }
209
210 const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx)
211 {
212 void *alg;
213
214 if (ctx->meth->gettable_ctx_params == NULL)
215 return NULL;
216 alg = ossl_provider_ctx(EVP_MAC_provider(ctx->meth));
217 return ctx->meth->gettable_ctx_params(ctx->data, alg);
218 }
219
220 const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx)
221 {
222 void *alg;
223
224 if (ctx->meth->settable_ctx_params == NULL)
225 return NULL;
226 alg = ossl_provider_ctx(EVP_MAC_provider(ctx->meth));
227 return ctx->meth->settable_ctx_params(ctx->data, alg);
228 }
229
230 void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
231 void (*fn)(EVP_MAC *mac, void *arg),
232 void *arg)
233 {
234 evp_generic_do_all(libctx, OSSL_OP_MAC,
235 (void (*)(void *, void *))fn, arg,
236 evp_mac_from_algorithm, evp_mac_free);
237 }