Coverage Report

Created: 2026-10-16 07:31

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