Coverage Report

Created: 2026-09-12 06:55

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