Coverage Report

Created: 2023-09-25 06:41

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