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