Coverage Report

Created: 2026-05-24 07:14

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