Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/providers/implementations/macs/gmac_prov.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 <stdlib.h>
11
#include <openssl/core_dispatch.h>
12
#include <openssl/core_names.h>
13
#include <openssl/params.h>
14
#include <openssl/evp.h>
15
#include <openssl/err.h>
16
#include <openssl/proverr.h>
17
18
#include "prov/implementations.h"
19
#include "prov/provider_ctx.h"
20
#include "prov/provider_util.h"
21
#include "prov/providercommon.h"
22
#include "fips/fipsindicator.h"
23
24
/*
25
 * Forward declaration of everything implemented here.  This is not strictly
26
 * necessary for the compiler, but provides an assurance that the signatures
27
 * of the functions in the dispatch table are correct.
28
 */
29
static OSSL_FUNC_mac_newctx_fn gmac_new;
30
static OSSL_FUNC_mac_dupctx_fn gmac_dup;
31
static OSSL_FUNC_mac_freectx_fn gmac_free;
32
static OSSL_FUNC_mac_gettable_params_fn gmac_gettable_params;
33
static OSSL_FUNC_mac_get_params_fn gmac_get_params;
34
static OSSL_FUNC_mac_settable_ctx_params_fn gmac_settable_ctx_params;
35
static OSSL_FUNC_mac_set_ctx_params_fn gmac_set_ctx_params;
36
static OSSL_FUNC_mac_init_fn gmac_init;
37
static OSSL_FUNC_mac_update_fn gmac_update;
38
static OSSL_FUNC_mac_final_fn gmac_final;
39
40
/* local GMAC pkey structure */
41
42
struct gmac_data_st {
43
    void *provctx;
44
    EVP_CIPHER_CTX *ctx; /* Cipher context */
45
    PROV_CIPHER cipher;
46
};
47
48
static void gmac_free(void *vmacctx)
49
272
{
50
272
    struct gmac_data_st *macctx = vmacctx;
51
52
272
    if (macctx != NULL) {
53
272
        EVP_CIPHER_CTX_free(macctx->ctx);
54
272
        ossl_prov_cipher_reset(&macctx->cipher);
55
272
        OPENSSL_free(macctx);
56
272
    }
57
272
}
58
59
static void *gmac_new(void *provctx)
60
272
{
61
272
    struct gmac_data_st *macctx;
62
63
272
    if (!ossl_prov_is_running())
64
0
        return NULL;
65
66
272
    if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
67
272
        || (macctx->ctx = EVP_CIPHER_CTX_new()) == NULL) {
68
0
        gmac_free(macctx);
69
0
        return NULL;
70
0
    }
71
272
    macctx->provctx = provctx;
72
73
272
    return macctx;
74
272
}
75
76
static void *gmac_dup(void *vsrc)
77
0
{
78
0
    struct gmac_data_st *src = vsrc;
79
0
    struct gmac_data_st *dst;
80
81
0
    if (!ossl_prov_is_running())
82
0
        return NULL;
83
84
0
    dst = gmac_new(src->provctx);
85
0
    if (dst == NULL)
86
0
        return NULL;
87
88
0
    if (!EVP_CIPHER_CTX_copy(dst->ctx, src->ctx)
89
0
        || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
90
0
        gmac_free(dst);
91
0
        return NULL;
92
0
    }
93
0
    return dst;
94
0
}
95
96
static size_t gmac_size(void)
97
0
{
98
0
    return EVP_GCM_TLS_TAG_LEN;
99
0
}
100
101
static int gmac_setkey(struct gmac_data_st *macctx,
102
    const unsigned char *key, size_t keylen)
103
127
{
104
127
    EVP_CIPHER_CTX *ctx = macctx->ctx;
105
106
127
    if (keylen != (size_t)EVP_CIPHER_CTX_get_key_length(ctx)) {
107
77
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
108
77
        return 0;
109
77
    }
110
50
    if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL))
111
0
        return 0;
112
50
    return 1;
113
50
}
114
115
static int gmac_init(void *vmacctx, const unsigned char *key,
116
    size_t keylen, const OSSL_PARAM params[])
117
259
{
118
259
    struct gmac_data_st *macctx = vmacctx;
119
120
259
    if (!ossl_prov_is_running() || !gmac_set_ctx_params(macctx, params))
121
228
        return 0;
122
31
    if (key != NULL)
123
31
        return gmac_setkey(macctx, key, keylen);
124
0
    return EVP_EncryptInit_ex(macctx->ctx, NULL, NULL, NULL, NULL);
125
31
}
126
127
static int gmac_update(void *vmacctx, const unsigned char *data,
128
    size_t datalen)
129
0
{
130
0
    struct gmac_data_st *macctx = vmacctx;
131
0
    EVP_CIPHER_CTX *ctx = macctx->ctx;
132
0
    int outlen;
133
134
0
    if (datalen == 0)
135
0
        return 1;
136
137
0
    while (datalen > INT_MAX) {
138
0
        if (!EVP_EncryptUpdate(ctx, NULL, &outlen, data, INT_MAX))
139
0
            return 0;
140
0
        data += INT_MAX;
141
0
        datalen -= INT_MAX;
142
0
    }
143
0
    return EVP_EncryptUpdate(ctx, NULL, &outlen, data, datalen);
144
0
}
145
146
static int gmac_final(void *vmacctx, unsigned char *out, size_t *outl,
147
    size_t outsize)
148
0
{
149
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
150
0
    struct gmac_data_st *macctx = vmacctx;
151
0
    int hlen = 0;
152
153
0
    if (!ossl_prov_is_running())
154
0
        return 0;
155
156
0
    if (!EVP_EncryptFinal_ex(macctx->ctx, out, &hlen))
157
0
        return 0;
158
159
0
    hlen = gmac_size();
160
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
161
0
        out, (size_t)hlen);
162
0
    if (!EVP_CIPHER_CTX_get_params(macctx->ctx, params))
163
0
        return 0;
164
165
0
    *outl = hlen;
166
0
    return 1;
167
0
}
168
169
static const OSSL_PARAM known_gettable_params[] = {
170
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
171
    OSSL_PARAM_END
172
};
173
static const OSSL_PARAM *gmac_gettable_params(void *provctx)
174
0
{
175
0
    return known_gettable_params;
176
0
}
177
178
static int gmac_get_params(OSSL_PARAM params[])
179
0
{
180
0
    OSSL_PARAM *p;
181
182
0
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
183
0
        return OSSL_PARAM_set_size_t(p, gmac_size());
184
185
0
    return 1;
186
0
}
187
188
#ifdef FIPS_MODULE
189
static const OSSL_PARAM known_gettable_ctx_params[] = {
190
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
191
    OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
192
        OSSL_PARAM_END
193
};
194
195
static const OSSL_PARAM *gmac_gettable_ctx_params(ossl_unused void *ctx,
196
    ossl_unused void *provctx)
197
{
198
    return known_gettable_ctx_params;
199
}
200
201
static int gmac_get_ctx_params(void *ctx, OSSL_PARAM params[])
202
{
203
    OSSL_PARAM *p;
204
205
    p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE);
206
    if (p != NULL && !OSSL_PARAM_set_size_t(p, gmac_size()))
207
        return 0;
208
    return ossl_FIPS_IND_get_ctx_param_approved(ctx, params);
209
}
210
#endif
211
212
static const OSSL_PARAM known_settable_ctx_params[] = {
213
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
214
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
215
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
216
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_IV, NULL, 0),
217
    OSSL_PARAM_END
218
};
219
static const OSSL_PARAM *gmac_settable_ctx_params(ossl_unused void *ctx,
220
    ossl_unused void *provctx)
221
251
{
222
251
    return known_settable_ctx_params;
223
251
}
224
225
/*
226
 * ALL parameters should be set before init().
227
 */
228
static int gmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
229
21
{
230
21
    struct gmac_data_st *macctx = vmacctx;
231
21
    EVP_CIPHER_CTX *ctx = macctx->ctx;
232
21
    OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(macctx->provctx);
233
21
    const OSSL_PARAM *p;
234
235
21
    if (ossl_param_is_empty(params))
236
2
        return 1;
237
19
    if (ctx == NULL)
238
0
        return 0;
239
240
19
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL) {
241
16
        if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, provctx))
242
12
            return 0;
243
4
        if (EVP_CIPHER_get_mode(ossl_prov_cipher_cipher(&macctx->cipher))
244
4
            != EVP_CIPH_GCM_MODE) {
245
4
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
246
4
            return 0;
247
4
        }
248
0
        if (!EVP_EncryptInit_ex(ctx, ossl_prov_cipher_cipher(&macctx->cipher),
249
0
                ossl_prov_cipher_engine(&macctx->cipher), NULL,
250
0
                NULL))
251
0
            return 0;
252
0
    }
253
254
3
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
255
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING
256
0
            || !gmac_setkey(macctx, p->data, p->data_size))
257
0
            return 0;
258
259
3
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_IV)) != NULL) {
260
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING)
261
0
            return 0;
262
263
0
        if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
264
0
                p->data_size, NULL)
265
0
                <= 0
266
0
            || !EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, p->data))
267
0
            return 0;
268
0
    }
269
3
    return 1;
270
3
}
271
272
const OSSL_DISPATCH ossl_gmac_functions[] = {
273
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))gmac_new },
274
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))gmac_dup },
275
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))gmac_free },
276
    { OSSL_FUNC_MAC_INIT, (void (*)(void))gmac_init },
277
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))gmac_update },
278
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))gmac_final },
279
    { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))gmac_gettable_params },
280
    { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))gmac_get_params },
281
#ifdef FIPS_MODULE
282
    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
283
        (void (*)(void))gmac_gettable_ctx_params },
284
    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))gmac_get_ctx_params },
285
#endif
286
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
287
        (void (*)(void))gmac_settable_ctx_params },
288
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))gmac_set_ctx_params },
289
    OSSL_DISPATCH_END
290
};