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