Coverage Report

Created: 2026-11-16 07:12

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