Coverage Report

Created: 2025-12-10 06:24

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