Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/evp/mac_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2018-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 <string.h>
11
#include <stdarg.h>
12
#include <openssl/evp.h>
13
#include <openssl/err.h>
14
#include <openssl/core.h>
15
#include <openssl/core_names.h>
16
#include <openssl/types.h>
17
#include "internal/nelem.h"
18
#include "crypto/evp.h"
19
#include "internal/provider.h"
20
#include "evp_local.h"
21
22
EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac)
23
312k
{
24
312k
    EVP_MAC_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MAC_CTX));
25
26
312k
    if (ctx != NULL) {
27
312k
        ctx->meth = mac;
28
312k
        if ((ctx->algctx = mac->newctx(ossl_provider_ctx(mac->prov))) == NULL
29
312k
            || !EVP_MAC_up_ref(mac)) {
30
0
            mac->freectx(ctx->algctx);
31
0
            ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
32
0
            OPENSSL_free(ctx);
33
0
            ctx = NULL;
34
0
        }
35
312k
    }
36
312k
    return ctx;
37
312k
}
38
39
void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx)
40
3.78M
{
41
3.78M
    if (ctx == NULL)
42
1.21M
        return;
43
2.56M
    ctx->meth->freectx(ctx->algctx);
44
2.56M
    ctx->algctx = NULL;
45
    /* refcnt-- */
46
2.56M
    EVP_MAC_free(ctx->meth);
47
2.56M
    OPENSSL_free(ctx);
48
2.56M
}
49
50
EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src)
51
2.24M
{
52
2.24M
    EVP_MAC_CTX *dst;
53
54
2.24M
    if (src->algctx == NULL)
55
0
        return NULL;
56
57
2.24M
    dst = OPENSSL_malloc(sizeof(*dst));
58
2.24M
    if (dst == NULL)
59
0
        return NULL;
60
61
2.24M
    *dst = *src;
62
2.24M
    if (!EVP_MAC_up_ref(dst->meth)) {
63
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
64
0
        OPENSSL_free(dst);
65
0
        return NULL;
66
0
    }
67
68
2.24M
    dst->algctx = src->meth->dupctx(src->algctx);
69
2.24M
    if (dst->algctx == NULL) {
70
0
        EVP_MAC_CTX_free(dst);
71
0
        return NULL;
72
0
    }
73
74
2.24M
    return dst;
75
2.24M
}
76
77
EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx)
78
4.23k
{
79
4.23k
    return ctx->meth;
80
4.23k
}
81
82
static size_t get_size_t_ctx_param(EVP_MAC_CTX *ctx, const char *name)
83
1.75M
{
84
1.75M
    size_t sz = 0;
85
86
1.75M
    if (ctx->algctx != NULL) {
87
1.75M
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
88
89
1.75M
        params[0] = OSSL_PARAM_construct_size_t(name, &sz);
90
1.75M
        if (ctx->meth->get_ctx_params != NULL) {
91
1.75M
            if (ctx->meth->get_ctx_params(ctx->algctx, params))
92
1.75M
                return sz;
93
1.75M
        } else if (ctx->meth->get_params != NULL) {
94
422
            if (ctx->meth->get_params(params))
95
422
                return sz;
96
422
        }
97
1.75M
    }
98
    /*
99
     * If the MAC hasn't been initialized yet, or there is no size to get,
100
     * we return zero
101
     */
102
0
    return 0;
103
1.75M
}
104
105
size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx)
106
1.75M
{
107
1.75M
    return get_size_t_ctx_param(ctx, OSSL_MAC_PARAM_SIZE);
108
1.75M
}
109
110
size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx)
111
0
{
112
0
    return get_size_t_ctx_param(ctx, OSSL_MAC_PARAM_BLOCK_SIZE);
113
0
}
114
115
int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
116
    const OSSL_PARAM params[])
117
305k
{
118
305k
    if (ctx->meth->init == NULL) {
119
0
        ERR_raise(ERR_R_EVP_LIB, ERR_R_UNSUPPORTED);
120
0
        return 0;
121
0
    }
122
305k
    return ctx->meth->init(ctx->algctx, key, keylen, params);
123
305k
}
124
125
int EVP_MAC_init_SKEY(EVP_MAC_CTX *ctx, EVP_SKEY *skey, const OSSL_PARAM params[])
126
0
{
127
0
    if (ctx->meth->init_skey == NULL
128
0
        || skey->skeymgmt->prov != ctx->meth->prov
129
0
        || ctx->meth->init_skey == NULL) {
130
0
        ERR_raise(ERR_R_EVP_LIB, ERR_R_UNSUPPORTED);
131
0
        return 0;
132
0
    }
133
0
    return ctx->meth->init_skey(ctx->algctx, skey->keydata, params);
134
0
}
135
136
int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
137
2.60M
{
138
2.60M
    return ctx->meth->update(ctx->algctx, data, datalen);
139
2.60M
}
140
141
static int evp_mac_final(EVP_MAC_CTX *ctx, int xof,
142
    unsigned char *out, size_t *outl, size_t outsize)
143
1.64M
{
144
1.64M
    size_t l;
145
1.64M
    int res;
146
1.64M
    OSSL_PARAM params[2];
147
1.64M
    size_t macsize;
148
149
1.64M
    if (ctx == NULL || ctx->meth == NULL) {
150
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
151
0
        return 0;
152
0
    }
153
1.64M
    if (ctx->meth->final == NULL) {
154
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
155
0
        return 0;
156
0
    }
157
158
1.64M
    macsize = EVP_MAC_CTX_get_mac_size(ctx);
159
1.64M
    if (out == NULL) {
160
0
        if (outl == NULL) {
161
0
            ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
162
0
            return 0;
163
0
        }
164
0
        *outl = macsize;
165
0
        return 1;
166
0
    }
167
1.64M
    if (outsize < macsize) {
168
25
        ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL);
169
25
        return 0;
170
25
    }
171
1.64M
    if (xof) {
172
0
        params[0] = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_XOF, &xof);
173
0
        params[1] = OSSL_PARAM_construct_end();
174
175
0
        if (EVP_MAC_CTX_set_params(ctx, params) <= 0) {
176
0
            ERR_raise(ERR_LIB_EVP, EVP_R_SETTING_XOF_FAILED);
177
0
            return 0;
178
0
        }
179
0
    }
180
1.64M
    res = ctx->meth->final(ctx->algctx, out, &l, outsize);
181
1.64M
    if (outl != NULL)
182
1.39M
        *outl = l;
183
1.64M
    return res;
184
1.64M
}
185
186
int EVP_MAC_final(EVP_MAC_CTX *ctx,
187
    unsigned char *out, size_t *outl, size_t outsize)
188
1.64M
{
189
1.64M
    return evp_mac_final(ctx, 0, out, outl, outsize);
190
1.64M
}
191
192
int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize)
193
0
{
194
0
    return evp_mac_final(ctx, 1, out, NULL, outsize);
195
0
}
196
197
/*
198
 * The {get,set}_params functions return 1 if there is no corresponding
199
 * function in the implementation.  This is the same as if there was one,
200
 * but it didn't recognise any of the given params, i.e. nothing in the
201
 * bag of parameters was useful.
202
 */
203
int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[])
204
0
{
205
0
    if (mac->get_params != NULL)
206
0
        return mac->get_params(params);
207
0
    return 1;
208
0
}
209
210
int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[])
211
0
{
212
0
    if (ctx->meth->get_ctx_params != NULL)
213
0
        return ctx->meth->get_ctx_params(ctx->algctx, params);
214
0
    return 1;
215
0
}
216
217
int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[])
218
689k
{
219
689k
    if (ctx->meth->set_ctx_params != NULL)
220
689k
        return ctx->meth->set_ctx_params(ctx->algctx, params);
221
0
    return 1;
222
689k
}
223
224
int evp_mac_get_number(const EVP_MAC *mac)
225
0
{
226
0
    return mac->name_id;
227
0
}
228
229
const char *EVP_MAC_get0_name(const EVP_MAC *mac)
230
0
{
231
0
    return mac->type_name;
232
0
}
233
234
const char *EVP_MAC_get0_description(const EVP_MAC *mac)
235
0
{
236
0
    return mac->description;
237
0
}
238
239
int EVP_MAC_is_a(const EVP_MAC *mac, const char *name)
240
4.29k
{
241
4.29k
    return mac != NULL && evp_is_a(mac->prov, mac->name_id, NULL, name);
242
4.29k
}
243
244
int EVP_MAC_names_do_all(const EVP_MAC *mac,
245
    void (*fn)(const char *name, void *data),
246
    void *data)
247
0
{
248
0
    if (mac->prov != NULL)
249
0
        return evp_names_do_all(mac->prov, mac->name_id, fn, data);
250
251
0
    return 1;
252
0
}
253
254
unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx,
255
    const char *name, const char *propq,
256
    const char *subalg, const OSSL_PARAM *params,
257
    const void *key, size_t keylen,
258
    const unsigned char *data, size_t datalen,
259
    unsigned char *out, size_t outsize, size_t *outlen)
260
77.2k
{
261
77.2k
    EVP_MAC *mac = EVP_MAC_fetch(libctx, name, propq);
262
77.2k
    OSSL_PARAM subalg_param[3];
263
77.2k
    OSSL_PARAM *p = subalg_param;
264
77.2k
    EVP_MAC_CTX *ctx = NULL;
265
77.2k
    size_t len = 0;
266
77.2k
    unsigned char *res = NULL;
267
268
77.2k
    if (outlen != NULL)
269
16.5k
        *outlen = 0;
270
77.2k
    if (mac == NULL)
271
0
        return NULL;
272
77.2k
    if (subalg != NULL) {
273
77.2k
        const OSSL_PARAM *defined_params = EVP_MAC_settable_ctx_params(mac);
274
77.2k
        const char *param_name = OSSL_MAC_PARAM_DIGEST;
275
276
        /*
277
         * The underlying algorithm may be a cipher or a digest.
278
         * We don't know which it is, but we can ask the MAC what it
279
         * should be and bet on that.
280
         */
281
77.2k
        if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) {
282
0
            param_name = OSSL_MAC_PARAM_CIPHER;
283
0
            if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) {
284
0
                ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
285
0
                goto err;
286
0
            }
287
0
        }
288
77.2k
        *p++ = OSSL_PARAM_construct_utf8_string(param_name, (char *)subalg, 0);
289
77.2k
    }
290
291
    /*
292
     * If we passed in a property query, make sure the underlying algorithm uses it.
293
     * Compound algorithms may fetch other underlying algorithms (i.e. a MAC algorithm
294
     * may use a digest algorithm as part of its internal work), and the MAC decides which
295
     * implementation of the underlying digest to use based on the
296
     * OSSL_ALG_PARAM_PROPERTIES parameter, rather than the propq value passed in the
297
     * EVP_MAC_fetch call above.
298
     * If we don't set this parameter, then the MAC alg may use the default provider
299
     * to allocate an underlying digest, rather than the one that we would have gotten
300
     * by using the propq value here.
301
     */
302
77.2k
    if (propq != NULL)
303
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES,
304
0
            (char *)propq, 0);
305
306
77.2k
    *p = OSSL_PARAM_construct_end();
307
308
    /* Single-shot - on NULL key input, set dummy key value for EVP_MAC_Init. */
309
77.2k
    if (key == NULL && keylen == 0)
310
0
        key = data;
311
77.2k
    if ((ctx = EVP_MAC_CTX_new(mac)) != NULL
312
77.2k
        && EVP_MAC_CTX_set_params(ctx, subalg_param)
313
77.1k
        && EVP_MAC_CTX_set_params(ctx, params)
314
77.1k
        && EVP_MAC_init(ctx, key, keylen, params)
315
77.1k
        && EVP_MAC_update(ctx, data, datalen)
316
77.1k
        && EVP_MAC_final(ctx, out, &len, outsize)) {
317
77.1k
        if (out == NULL) {
318
0
            out = OPENSSL_malloc(len);
319
0
            if (out != NULL && !EVP_MAC_final(ctx, out, NULL, len)) {
320
0
                OPENSSL_free(out);
321
0
                out = NULL;
322
0
            }
323
0
        }
324
77.1k
        res = out;
325
77.1k
        if (res != NULL && outlen != NULL)
326
16.5k
            *outlen = len;
327
77.1k
    }
328
329
77.2k
err:
330
77.2k
    EVP_MAC_CTX_free(ctx);
331
77.2k
    EVP_MAC_free(mac);
332
77.2k
    return res;
333
77.2k
}