2 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
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
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"
19 static int evp_mac_up_ref(void *vmac
)
24 CRYPTO_UP_REF(&mac
->refcnt
, &ref
);
28 static void evp_mac_free(void *vmac
)
36 CRYPTO_DOWN_REF(&mac
->refcnt
, &ref
);
39 OPENSSL_free(mac
->type_name
);
40 ossl_provider_free(mac
->prov
);
41 CRYPTO_FREE_REF(&mac
->refcnt
);
45 static void *evp_mac_new(void)
49 if ((mac
= OPENSSL_zalloc(sizeof(*mac
))) == NULL
50 || !CRYPTO_NEW_REF(&mac
->refcnt
, 1)) {
57 static void *evp_mac_from_algorithm(int name_id
,
58 const OSSL_ALGORITHM
*algodef
,
61 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
63 int fnmaccnt
= 0, fnctxcnt
= 0;
65 if ((mac
= evp_mac_new()) == NULL
) {
66 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
69 mac
->name_id
= name_id
;
70 if ((mac
->type_name
= ossl_algorithm_get1_first_name(algodef
)) == NULL
) {
74 mac
->description
= algodef
->algorithm_description
;
76 for (; fns
->function_id
!= 0; fns
++) {
77 switch (fns
->function_id
) {
78 case OSSL_FUNC_MAC_NEWCTX
:
79 if (mac
->newctx
!= NULL
)
81 mac
->newctx
= OSSL_FUNC_mac_newctx(fns
);
84 case OSSL_FUNC_MAC_DUPCTX
:
85 if (mac
->dupctx
!= NULL
)
87 mac
->dupctx
= OSSL_FUNC_mac_dupctx(fns
);
89 case OSSL_FUNC_MAC_FREECTX
:
90 if (mac
->freectx
!= NULL
)
92 mac
->freectx
= OSSL_FUNC_mac_freectx(fns
);
95 case OSSL_FUNC_MAC_INIT
:
96 if (mac
->init
!= NULL
)
98 mac
->init
= OSSL_FUNC_mac_init(fns
);
101 case OSSL_FUNC_MAC_UPDATE
:
102 if (mac
->update
!= NULL
)
104 mac
->update
= OSSL_FUNC_mac_update(fns
);
107 case OSSL_FUNC_MAC_FINAL
:
108 if (mac
->final
!= NULL
)
110 mac
->final
= OSSL_FUNC_mac_final(fns
);
113 case OSSL_FUNC_MAC_GETTABLE_PARAMS
:
114 if (mac
->gettable_params
!= NULL
)
116 mac
->gettable_params
=
117 OSSL_FUNC_mac_gettable_params(fns
);
119 case OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS
:
120 if (mac
->gettable_ctx_params
!= NULL
)
122 mac
->gettable_ctx_params
=
123 OSSL_FUNC_mac_gettable_ctx_params(fns
);
125 case OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS
:
126 if (mac
->settable_ctx_params
!= NULL
)
128 mac
->settable_ctx_params
=
129 OSSL_FUNC_mac_settable_ctx_params(fns
);
131 case OSSL_FUNC_MAC_GET_PARAMS
:
132 if (mac
->get_params
!= NULL
)
134 mac
->get_params
= OSSL_FUNC_mac_get_params(fns
);
136 case OSSL_FUNC_MAC_GET_CTX_PARAMS
:
137 if (mac
->get_ctx_params
!= NULL
)
139 mac
->get_ctx_params
= OSSL_FUNC_mac_get_ctx_params(fns
);
141 case OSSL_FUNC_MAC_SET_CTX_PARAMS
:
142 if (mac
->set_ctx_params
!= NULL
)
144 mac
->set_ctx_params
= OSSL_FUNC_mac_set_ctx_params(fns
);
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.
156 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
161 ossl_provider_up_ref(prov
);
166 EVP_MAC
*EVP_MAC_fetch(OSSL_LIB_CTX
*libctx
, const char *algorithm
,
167 const char *properties
)
169 return evp_generic_fetch(libctx
, OSSL_OP_MAC
, algorithm
, properties
,
170 evp_mac_from_algorithm
, evp_mac_up_ref
,
174 int EVP_MAC_up_ref(EVP_MAC
*mac
)
176 return evp_mac_up_ref(mac
);
179 void EVP_MAC_free(EVP_MAC
*mac
)
184 const OSSL_PROVIDER
*EVP_MAC_get0_provider(const EVP_MAC
*mac
)
189 const OSSL_PARAM
*EVP_MAC_gettable_params(const EVP_MAC
*mac
)
191 if (mac
->gettable_params
== NULL
)
193 return mac
->gettable_params(ossl_provider_ctx(EVP_MAC_get0_provider(mac
)));
196 const OSSL_PARAM
*EVP_MAC_gettable_ctx_params(const EVP_MAC
*mac
)
200 if (mac
->gettable_ctx_params
== NULL
)
202 alg
= ossl_provider_ctx(EVP_MAC_get0_provider(mac
));
203 return mac
->gettable_ctx_params(NULL
, alg
);
206 const OSSL_PARAM
*EVP_MAC_settable_ctx_params(const EVP_MAC
*mac
)
210 if (mac
->settable_ctx_params
== NULL
)
212 alg
= ossl_provider_ctx(EVP_MAC_get0_provider(mac
));
213 return mac
->settable_ctx_params(NULL
, alg
);
216 const OSSL_PARAM
*EVP_MAC_CTX_gettable_params(EVP_MAC_CTX
*ctx
)
220 if (ctx
->meth
->gettable_ctx_params
== NULL
)
222 alg
= ossl_provider_ctx(EVP_MAC_get0_provider(ctx
->meth
));
223 return ctx
->meth
->gettable_ctx_params(ctx
->algctx
, alg
);
226 const OSSL_PARAM
*EVP_MAC_CTX_settable_params(EVP_MAC_CTX
*ctx
)
230 if (ctx
->meth
->settable_ctx_params
== NULL
)
232 alg
= ossl_provider_ctx(EVP_MAC_get0_provider(ctx
->meth
));
233 return ctx
->meth
->settable_ctx_params(ctx
->algctx
, alg
);
236 void EVP_MAC_do_all_provided(OSSL_LIB_CTX
*libctx
,
237 void (*fn
)(EVP_MAC
*mac
, void *arg
),
240 evp_generic_do_all(libctx
, OSSL_OP_MAC
,
241 (void (*)(void *, void *))fn
, arg
,
242 evp_mac_from_algorithm
, evp_mac_up_ref
, evp_mac_free
);