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_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2018-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 <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
10.1k
{
24
10.1k
    EVP_MAC_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MAC_CTX));
25
26
10.1k
    if (ctx == NULL
27
10.1k
        || (ctx->algctx = mac->newctx(ossl_provider_ctx(mac->prov))) == NULL
28
10.1k
        || !EVP_MAC_up_ref(mac)) {
29
0
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
30
0
        if (ctx != NULL)
31
0
            mac->freectx(ctx->algctx);
32
0
        OPENSSL_free(ctx);
33
0
        ctx = NULL;
34
10.1k
    } else {
35
10.1k
        ctx->meth = mac;
36
10.1k
    }
37
10.1k
    return ctx;
38
10.1k
}
39
40
void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx)
41
2.56M
{
42
2.56M
    if (ctx == NULL)
43
874k
        return;
44
1.68M
    ctx->meth->freectx(ctx->algctx);
45
1.68M
    ctx->algctx = NULL;
46
    /* refcnt-- */
47
1.68M
    EVP_MAC_free(ctx->meth);
48
1.68M
    OPENSSL_free(ctx);
49
1.68M
}
50
51
EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src)
52
1.41M
{
53
1.41M
    EVP_MAC_CTX *dst;
54
55
1.41M
    if (src->algctx == NULL)
56
0
        return NULL;
57
58
1.41M
    dst = OPENSSL_malloc(sizeof(*dst));
59
1.41M
    if (dst == NULL) {
60
0
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
61
0
        return NULL;
62
0
    }
63
64
1.41M
    *dst = *src;
65
1.41M
    if (!EVP_MAC_up_ref(dst->meth)) {
66
0
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
67
0
        OPENSSL_free(dst);
68
0
        return NULL;
69
0
    }
70
71
1.41M
    dst->algctx = src->meth->dupctx(src->algctx);
72
1.41M
    if (dst->algctx == NULL) {
73
0
        EVP_MAC_CTX_free(dst);
74
0
        return NULL;
75
0
    }
76
77
1.41M
    return dst;
78
1.41M
}
79
80
EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx)
81
1.52k
{
82
1.52k
    return ctx->meth;
83
1.52k
}
84
85
static size_t get_size_t_ctx_param(EVP_MAC_CTX *ctx, const char *name)
86
1.23M
{
87
1.23M
    size_t sz = 0;
88
89
1.23M
    if (ctx->algctx != NULL) {
90
1.23M
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
91
92
1.23M
        params[0] = OSSL_PARAM_construct_size_t(name, &sz);
93
1.23M
        if (ctx->meth->get_ctx_params != NULL) {
94
1.23M
            if (ctx->meth->get_ctx_params(ctx->algctx, params))
95
1.23M
                return sz;
96
1.23M
        } else if (ctx->meth->get_params != NULL) {
97
280
            if (ctx->meth->get_params(params))
98
280
                return sz;
99
280
        }
100
1.23M
    }
101
    /*
102
     * If the MAC hasn't been initialized yet, or there is no size to get,
103
     * we return zero
104
     */
105
0
    return 0;
106
1.23M
}
107
108
size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx)
109
1.23M
{
110
1.23M
    return get_size_t_ctx_param(ctx, OSSL_MAC_PARAM_SIZE);
111
1.23M
}
112
113
size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx)
114
0
{
115
0
    return get_size_t_ctx_param(ctx, OSSL_MAC_PARAM_BLOCK_SIZE);
116
0
}
117
118
int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
119
    const OSSL_PARAM params[])
120
123k
{
121
123k
    return ctx->meth->init(ctx->algctx, key, keylen, params);
122
123k
}
123
124
int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
125
1.70M
{
126
1.70M
    return ctx->meth->update(ctx->algctx, data, datalen);
127
1.70M
}
128
129
static int evp_mac_final(EVP_MAC_CTX *ctx, int xof,
130
    unsigned char *out, size_t *outl, size_t outsize)
131
1.14M
{
132
1.14M
    size_t l;
133
1.14M
    int res;
134
1.14M
    OSSL_PARAM params[2];
135
1.14M
    size_t macsize;
136
137
1.14M
    if (ctx == NULL || ctx->meth == NULL) {
138
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
139
0
        return 0;
140
0
    }
141
1.14M
    if (ctx->meth->final == NULL) {
142
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
143
0
        return 0;
144
0
    }
145
146
1.14M
    macsize = EVP_MAC_CTX_get_mac_size(ctx);
147
1.14M
    if (out == NULL) {
148
0
        if (outl == NULL) {
149
0
            ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
150
0
            return 0;
151
0
        }
152
0
        *outl = macsize;
153
0
        return 1;
154
0
    }
155
1.14M
    if (outsize < macsize) {
156
24
        ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL);
157
24
        return 0;
158
24
    }
159
1.14M
    if (xof) {
160
0
        params[0] = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_XOF, &xof);
161
0
        params[1] = OSSL_PARAM_construct_end();
162
163
0
        if (EVP_MAC_CTX_set_params(ctx, params) <= 0) {
164
0
            ERR_raise(ERR_LIB_EVP, EVP_R_SETTING_XOF_FAILED);
165
0
            return 0;
166
0
        }
167
0
    }
168
1.14M
    res = ctx->meth->final(ctx->algctx, out, &l, outsize);
169
1.14M
    if (outl != NULL)
170
951k
        *outl = l;
171
1.14M
    return res;
172
1.14M
}
173
174
int EVP_MAC_final(EVP_MAC_CTX *ctx,
175
    unsigned char *out, size_t *outl, size_t outsize)
176
1.14M
{
177
1.14M
    return evp_mac_final(ctx, 0, out, outl, outsize);
178
1.14M
}
179
180
int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize)
181
0
{
182
0
    return evp_mac_final(ctx, 1, out, NULL, outsize);
183
0
}
184
185
/*
186
 * The {get,set}_params functions return 1 if there is no corresponding
187
 * function in the implementation.  This is the same as if there was one,
188
 * but it didn't recognise any of the given params, i.e. nothing in the
189
 * bag of parameters was useful.
190
 */
191
int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[])
192
0
{
193
0
    if (mac->get_params != NULL)
194
0
        return mac->get_params(params);
195
0
    return 1;
196
0
}
197
198
int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[])
199
0
{
200
0
    if (ctx->meth->get_ctx_params != NULL)
201
0
        return ctx->meth->get_ctx_params(ctx->algctx, params);
202
0
    return 1;
203
0
}
204
205
int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[])
206
580k
{
207
580k
    if (ctx->meth->set_ctx_params != NULL)
208
580k
        return ctx->meth->set_ctx_params(ctx->algctx, params);
209
0
    return 1;
210
580k
}
211
212
int evp_mac_get_number(const EVP_MAC *mac)
213
0
{
214
0
    return mac->name_id;
215
0
}
216
217
const char *EVP_MAC_get0_name(const EVP_MAC *mac)
218
0
{
219
0
    return mac->type_name;
220
0
}
221
222
const char *EVP_MAC_get0_description(const EVP_MAC *mac)
223
0
{
224
0
    return mac->description;
225
0
}
226
227
int EVP_MAC_is_a(const EVP_MAC *mac, const char *name)
228
1.56k
{
229
1.56k
    return mac != NULL && evp_is_a(mac->prov, mac->name_id, NULL, name);
230
1.56k
}
231
232
int EVP_MAC_names_do_all(const EVP_MAC *mac,
233
    void (*fn)(const char *name, void *data),
234
    void *data)
235
0
{
236
0
    if (mac->prov != NULL)
237
0
        return evp_names_do_all(mac->prov, mac->name_id, fn, data);
238
239
0
    return 1;
240
0
}
241
242
unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx,
243
    const char *name, const char *propq,
244
    const char *subalg, const OSSL_PARAM *params,
245
    const void *key, size_t keylen,
246
    const unsigned char *data, size_t datalen,
247
    unsigned char *out, size_t outsize, size_t *outlen)
248
122k
{
249
122k
    EVP_MAC *mac = EVP_MAC_fetch(libctx, name, propq);
250
122k
    OSSL_PARAM subalg_param[] = { OSSL_PARAM_END, OSSL_PARAM_END };
251
122k
    EVP_MAC_CTX *ctx = NULL;
252
122k
    size_t len = 0;
253
122k
    unsigned char *res = NULL;
254
255
122k
    if (outlen != NULL)
256
22.9k
        *outlen = 0;
257
122k
    if (mac == NULL)
258
0
        return NULL;
259
122k
    if (subalg != NULL) {
260
122k
        const OSSL_PARAM *defined_params = EVP_MAC_settable_ctx_params(mac);
261
122k
        const char *param_name = OSSL_MAC_PARAM_DIGEST;
262
263
        /*
264
         * The underlying algorithm may be a cipher or a digest.
265
         * We don't know which it is, but we can ask the MAC what it
266
         * should be and bet on that.
267
         */
268
122k
        if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) {
269
0
            param_name = OSSL_MAC_PARAM_CIPHER;
270
0
            if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) {
271
0
                ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
272
0
                goto err;
273
0
            }
274
0
        }
275
122k
        subalg_param[0] = OSSL_PARAM_construct_utf8_string(param_name, (char *)subalg, 0);
276
122k
    }
277
    /* Single-shot - on NULL key input, set dummy key value for EVP_MAC_Init. */
278
122k
    if (key == NULL && keylen == 0)
279
0
        key = data;
280
122k
    if ((ctx = EVP_MAC_CTX_new(mac)) != NULL
281
122k
        && EVP_MAC_CTX_set_params(ctx, subalg_param)
282
122k
        && EVP_MAC_CTX_set_params(ctx, params)
283
122k
        && EVP_MAC_init(ctx, key, keylen, params)
284
122k
        && EVP_MAC_update(ctx, data, datalen)
285
122k
        && EVP_MAC_final(ctx, out, &len, outsize)) {
286
122k
        if (out == NULL) {
287
0
            out = OPENSSL_malloc(len);
288
0
            if (out != NULL && !EVP_MAC_final(ctx, out, NULL, len)) {
289
0
                OPENSSL_free(out);
290
0
                out = NULL;
291
0
            }
292
0
        }
293
122k
        res = out;
294
122k
        if (res != NULL && outlen != NULL)
295
22.9k
            *outlen = len;
296
122k
    }
297
298
122k
err:
299
122k
    EVP_MAC_CTX_free(ctx);
300
122k
    EVP_MAC_free(mac);
301
122k
    return res;
302
122k
}