Coverage Report

Created: 2025-12-04 06:33

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