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