Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/providers/implementations/signature/mac_legacy_sig.c
Line
Count
Source
1
/*
2
 * Copyright 2019-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
/* We need to use some engine deprecated APIs */
11
#define OPENSSL_SUPPRESS_DEPRECATED
12
13
#include <openssl/crypto.h>
14
#include <openssl/evp.h>
15
#include <openssl/core_dispatch.h>
16
#include <openssl/core_names.h>
17
#include <openssl/params.h>
18
#include <openssl/err.h>
19
#include <openssl/proverr.h>
20
#ifndef FIPS_MODULE
21
#include <openssl/engine.h>
22
#endif
23
#include "prov/implementations.h"
24
#include "prov/provider_ctx.h"
25
#include "prov/macsignature.h"
26
#include "prov/providercommon.h"
27
28
static OSSL_FUNC_signature_newctx_fn mac_hmac_newctx;
29
static OSSL_FUNC_signature_newctx_fn mac_siphash_newctx;
30
static OSSL_FUNC_signature_newctx_fn mac_poly1305_newctx;
31
static OSSL_FUNC_signature_newctx_fn mac_cmac_newctx;
32
static OSSL_FUNC_signature_digest_sign_init_fn mac_digest_sign_init;
33
static OSSL_FUNC_signature_digest_sign_update_fn mac_digest_sign_update;
34
static OSSL_FUNC_signature_digest_sign_final_fn mac_digest_sign_final;
35
static OSSL_FUNC_signature_freectx_fn mac_freectx;
36
static OSSL_FUNC_signature_dupctx_fn mac_dupctx;
37
static OSSL_FUNC_signature_set_ctx_params_fn mac_set_ctx_params;
38
static OSSL_FUNC_signature_settable_ctx_params_fn mac_hmac_settable_ctx_params;
39
static OSSL_FUNC_signature_settable_ctx_params_fn mac_siphash_settable_ctx_params;
40
static OSSL_FUNC_signature_settable_ctx_params_fn mac_poly1305_settable_ctx_params;
41
static OSSL_FUNC_signature_settable_ctx_params_fn mac_cmac_settable_ctx_params;
42
43
typedef struct {
44
    OSSL_LIB_CTX *libctx;
45
    char *propq;
46
    MAC_KEY *key;
47
    EVP_MAC_CTX *macctx;
48
} PROV_MAC_CTX;
49
50
static void *mac_newctx(void *provctx, const char *propq, const char *macname)
51
18.9k
{
52
18.9k
    PROV_MAC_CTX *pmacctx;
53
18.9k
    EVP_MAC *mac = NULL;
54
55
18.9k
    if (!ossl_prov_is_running())
56
0
        return NULL;
57
58
18.9k
    pmacctx = OPENSSL_zalloc(sizeof(PROV_MAC_CTX));
59
18.9k
    if (pmacctx == NULL)
60
0
        return NULL;
61
62
18.9k
    pmacctx->libctx = PROV_LIBCTX_OF(provctx);
63
18.9k
    if (propq != NULL && (pmacctx->propq = OPENSSL_strdup(propq)) == NULL)
64
0
        goto err;
65
66
18.9k
    mac = EVP_MAC_fetch(pmacctx->libctx, macname, propq);
67
18.9k
    if (mac == NULL)
68
0
        goto err;
69
70
18.9k
    pmacctx->macctx = EVP_MAC_CTX_new(mac);
71
18.9k
    if (pmacctx->macctx == NULL)
72
0
        goto err;
73
74
18.9k
    EVP_MAC_free(mac);
75
76
18.9k
    return pmacctx;
77
78
0
err:
79
0
    OPENSSL_free(pmacctx->propq);
80
0
    OPENSSL_free(pmacctx);
81
0
    EVP_MAC_free(mac);
82
0
    return NULL;
83
18.9k
}
84
85
#define MAC_NEWCTX(funcname, macname)                                      \
86
    static void *mac_##funcname##_newctx(void *provctx, const char *propq) \
87
18.9k
    {                                                                      \
88
18.9k
        return mac_newctx(provctx, propq, macname);                        \
89
18.9k
    }
mac_legacy_sig.c:mac_hmac_newctx
Line
Count
Source
87
18.9k
    {                                                                      \
88
18.9k
        return mac_newctx(provctx, propq, macname);                        \
89
18.9k
    }
Unexecuted instantiation: mac_legacy_sig.c:mac_siphash_newctx
Unexecuted instantiation: mac_legacy_sig.c:mac_poly1305_newctx
Unexecuted instantiation: mac_legacy_sig.c:mac_cmac_newctx
90
91
MAC_NEWCTX(hmac, "HMAC")
92
MAC_NEWCTX(siphash, "SIPHASH")
93
MAC_NEWCTX(poly1305, "POLY1305")
94
MAC_NEWCTX(cmac, "CMAC")
95
96
static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey,
97
    const OSSL_PARAM params[])
98
15.3k
{
99
15.3k
    PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
100
15.3k
    const char *ciphername = NULL, *engine = NULL;
101
102
15.3k
    if (!ossl_prov_is_running()
103
15.3k
        || pmacctx == NULL)
104
0
        return 0;
105
106
15.3k
    if (pmacctx->key == NULL && vkey == NULL) {
107
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
108
0
        return 0;
109
0
    }
110
111
15.3k
    if (vkey != NULL) {
112
15.3k
        if (!ossl_mac_key_up_ref(vkey))
113
0
            return 0;
114
15.3k
        ossl_mac_key_free(pmacctx->key);
115
15.3k
        pmacctx->key = vkey;
116
15.3k
    }
117
118
15.3k
    if (pmacctx->key->cipher.cipher != NULL)
119
0
        ciphername = (char *)EVP_CIPHER_get0_name(pmacctx->key->cipher.cipher);
120
15.3k
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
121
15.3k
    if (pmacctx->key->cipher.engine != NULL)
122
0
        engine = (char *)ENGINE_get_id(pmacctx->key->cipher.engine);
123
15.3k
#endif
124
125
15.3k
    if (!ossl_prov_set_macctx(pmacctx->macctx,
126
15.3k
            (char *)ciphername,
127
15.3k
            (char *)mdname,
128
15.3k
            (char *)engine,
129
15.3k
            pmacctx->key->properties))
130
0
        return 0;
131
132
15.3k
    if (!EVP_MAC_init(pmacctx->macctx, pmacctx->key->priv_key,
133
15.3k
            pmacctx->key->priv_key_len, params))
134
0
        return 0;
135
136
15.3k
    return 1;
137
15.3k
}
138
139
int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,
140
    size_t datalen)
141
941k
{
142
941k
    PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
143
144
941k
    if (pmacctx == NULL || pmacctx->macctx == NULL)
145
0
        return 0;
146
147
941k
    return EVP_MAC_update(pmacctx->macctx, data, datalen);
148
941k
}
149
150
int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,
151
    size_t macsize)
152
470k
{
153
470k
    PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
154
155
470k
    if (!ossl_prov_is_running() || pmacctx == NULL || pmacctx->macctx == NULL)
156
0
        return 0;
157
158
470k
    return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);
159
470k
}
160
161
static void mac_freectx(void *vpmacctx)
162
960k
{
163
960k
    PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
164
165
960k
    OPENSSL_free(ctx->propq);
166
960k
    EVP_MAC_CTX_free(ctx->macctx);
167
960k
    ossl_mac_key_free(ctx->key);
168
960k
    OPENSSL_free(ctx);
169
960k
}
170
171
static void *mac_dupctx(void *vpmacctx)
172
941k
{
173
941k
    PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;
174
941k
    PROV_MAC_CTX *dstctx;
175
176
941k
    if (!ossl_prov_is_running())
177
0
        return NULL;
178
179
941k
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
180
941k
    if (dstctx == NULL)
181
0
        return NULL;
182
183
941k
    *dstctx = *srcctx;
184
941k
    dstctx->propq = NULL;
185
941k
    dstctx->key = NULL;
186
941k
    dstctx->macctx = NULL;
187
188
941k
    if (srcctx->propq != NULL && (dstctx->propq = OPENSSL_strdup(srcctx->propq)) == NULL)
189
0
        goto err;
190
191
941k
    if (srcctx->key != NULL && !ossl_mac_key_up_ref(srcctx->key))
192
0
        goto err;
193
941k
    dstctx->key = srcctx->key;
194
195
941k
    if (srcctx->macctx != NULL) {
196
941k
        dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);
197
941k
        if (dstctx->macctx == NULL)
198
0
            goto err;
199
941k
    }
200
201
941k
    return dstctx;
202
0
err:
203
0
    mac_freectx(dstctx);
204
0
    return NULL;
205
941k
}
206
207
static int mac_set_ctx_params(void *vpmacctx, const OSSL_PARAM params[])
208
163k
{
209
163k
    PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
210
211
163k
    return EVP_MAC_CTX_set_params(ctx->macctx, params);
212
163k
}
213
214
static const OSSL_PARAM *mac_settable_ctx_params(ossl_unused void *ctx,
215
    void *provctx,
216
    const char *macname)
217
27
{
218
27
    EVP_MAC *mac = EVP_MAC_fetch(PROV_LIBCTX_OF(provctx), macname,
219
27
        NULL);
220
27
    const OSSL_PARAM *params;
221
222
27
    if (mac == NULL)
223
0
        return NULL;
224
225
27
    params = EVP_MAC_settable_ctx_params(mac);
226
27
    EVP_MAC_free(mac);
227
228
27
    return params;
229
27
}
230
231
#define MAC_SETTABLE_CTX_PARAMS(funcname, macname)                           \
232
    static const OSSL_PARAM *mac_##funcname##_settable_ctx_params(void *ctx, \
233
        void *provctx)                                                       \
234
27
    {                                                                        \
235
27
        return mac_settable_ctx_params(ctx, provctx, macname);               \
236
27
    }
mac_legacy_sig.c:mac_hmac_settable_ctx_params
Line
Count
Source
234
7
    {                                                                        \
235
7
        return mac_settable_ctx_params(ctx, provctx, macname);               \
236
7
    }
mac_legacy_sig.c:mac_siphash_settable_ctx_params
Line
Count
Source
234
5
    {                                                                        \
235
5
        return mac_settable_ctx_params(ctx, provctx, macname);               \
236
5
    }
mac_legacy_sig.c:mac_poly1305_settable_ctx_params
Line
Count
Source
234
10
    {                                                                        \
235
10
        return mac_settable_ctx_params(ctx, provctx, macname);               \
236
10
    }
mac_legacy_sig.c:mac_cmac_settable_ctx_params
Line
Count
Source
234
5
    {                                                                        \
235
5
        return mac_settable_ctx_params(ctx, provctx, macname);               \
236
5
    }
237
238
MAC_SETTABLE_CTX_PARAMS(hmac, "HMAC")
239
MAC_SETTABLE_CTX_PARAMS(siphash, "SIPHASH")
240
MAC_SETTABLE_CTX_PARAMS(poly1305, "POLY1305")
241
MAC_SETTABLE_CTX_PARAMS(cmac, "CMAC")
242
243
#define MAC_SIGNATURE_FUNCTIONS(funcname)                                        \
244
    const OSSL_DISPATCH ossl_mac_legacy_##funcname##_signature_functions[] = {   \
245
        { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \
246
        { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,                                  \
247
            (void (*)(void))mac_digest_sign_init },                              \
248
        { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,                                \
249
            (void (*)(void))mac_digest_sign_update },                            \
250
        { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,                                 \
251
            (void (*)(void))mac_digest_sign_final },                             \
252
        { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx },            \
253
        { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx },              \
254
        { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS,                                    \
255
            (void (*)(void))mac_set_ctx_params },                                \
256
        { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,                               \
257
            (void (*)(void))mac_##funcname##_settable_ctx_params },              \
258
        OSSL_DISPATCH_END                                                        \
259
    };
260
261
MAC_SIGNATURE_FUNCTIONS(hmac)
262
MAC_SIGNATURE_FUNCTIONS(siphash)
263
MAC_SIGNATURE_FUNCTIONS(poly1305)
264
MAC_SIGNATURE_FUNCTIONS(cmac)