Coverage Report

Created: 2025-12-31 06:58

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