Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/providers/implementations/macs/hmac_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
/*
11
 * HMAC low level APIs are deprecated for public use, but still ok for internal
12
 * use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <string.h>
17
18
#include <openssl/core_dispatch.h>
19
#include <openssl/core_names.h>
20
#include <openssl/params.h>
21
#include <openssl/evp.h>
22
#include <openssl/hmac.h>
23
24
#include "prov/implementations.h"
25
#include "prov/provider_ctx.h"
26
#include "prov/provider_util.h"
27
#include "prov/providercommon.h"
28
29
/*
30
 * Forward declaration of everything implemented here.  This is not strictly
31
 * necessary for the compiler, but provides an assurance that the signatures
32
 * of the functions in the dispatch table are correct.
33
 */
34
static OSSL_FUNC_mac_newctx_fn hmac_new;
35
static OSSL_FUNC_mac_dupctx_fn hmac_dup;
36
static OSSL_FUNC_mac_freectx_fn hmac_free;
37
static OSSL_FUNC_mac_gettable_ctx_params_fn hmac_gettable_ctx_params;
38
static OSSL_FUNC_mac_get_ctx_params_fn hmac_get_ctx_params;
39
static OSSL_FUNC_mac_settable_ctx_params_fn hmac_settable_ctx_params;
40
static OSSL_FUNC_mac_set_ctx_params_fn hmac_set_ctx_params;
41
static OSSL_FUNC_mac_init_fn hmac_init;
42
static OSSL_FUNC_mac_update_fn hmac_update;
43
static OSSL_FUNC_mac_final_fn hmac_final;
44
45
/* local HMAC context structure */
46
47
/* typedef EVP_MAC_IMPL */
48
struct hmac_data_st {
49
    void *provctx;
50
    HMAC_CTX *ctx;               /* HMAC context */
51
    PROV_DIGEST digest;
52
    unsigned char *key;
53
    size_t keylen;
54
    /* Length of full TLS record including the MAC and any padding */
55
    size_t tls_data_size;
56
    unsigned char tls_header[13];
57
    int tls_header_set;
58
    unsigned char tls_mac_out[EVP_MAX_MD_SIZE];
59
    size_t tls_mac_out_size;
60
};
61
62
/* Defined in ssl/s3_cbc.c */
63
int ssl3_cbc_digest_record(const EVP_MD *md,
64
                           unsigned char *md_out,
65
                           size_t *md_out_size,
66
                           const unsigned char header[13],
67
                           const unsigned char *data,
68
                           size_t data_size,
69
                           size_t data_plus_mac_plus_padding_size,
70
                           const unsigned char *mac_secret,
71
                           size_t mac_secret_length, char is_sslv3);
72
73
static void *hmac_new(void *provctx)
74
1.57M
{
75
1.57M
    struct hmac_data_st *macctx;
76
77
1.57M
    if (!ossl_prov_is_running())
78
0
        return NULL;
79
80
1.57M
    if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
81
1.57M
        || (macctx->ctx = HMAC_CTX_new()) == NULL) {
82
0
        OPENSSL_free(macctx);
83
0
        return NULL;
84
0
    }
85
1.57M
    macctx->provctx = provctx;
86
87
1.57M
    return macctx;
88
1.57M
}
89
90
static void hmac_free(void *vmacctx)
91
1.57M
{
92
1.57M
    struct hmac_data_st *macctx = vmacctx;
93
94
1.57M
    if (macctx != NULL) {
95
1.57M
        HMAC_CTX_free(macctx->ctx);
96
1.57M
        ossl_prov_digest_reset(&macctx->digest);
97
1.57M
        OPENSSL_clear_free(macctx->key, macctx->keylen);
98
1.57M
        OPENSSL_free(macctx);
99
1.57M
    }
100
1.57M
}
101
102
static void *hmac_dup(void *vsrc)
103
1.32M
{
104
1.32M
    struct hmac_data_st *src = vsrc;
105
1.32M
    struct hmac_data_st *dst;
106
1.32M
    HMAC_CTX *ctx;
107
108
1.32M
    if (!ossl_prov_is_running())
109
0
        return NULL;
110
1.32M
    dst = hmac_new(src->provctx);
111
1.32M
    if (dst == NULL)
112
0
        return NULL;
113
114
1.32M
    ctx = dst->ctx;
115
1.32M
    *dst = *src;
116
1.32M
    dst->ctx = ctx;
117
1.32M
    dst->key = NULL;
118
1.32M
    memset(&dst->digest, 0, sizeof(dst->digest));
119
120
1.32M
    if (!HMAC_CTX_copy(dst->ctx, src->ctx)
121
1.32M
        || !ossl_prov_digest_copy(&dst->digest, &src->digest)) {
122
0
        hmac_free(dst);
123
0
        return NULL;
124
0
    }
125
1.32M
    if (src->key != NULL) {
126
1.32M
        dst->key = OPENSSL_malloc(src->keylen > 0 ? src->keylen : 1);
127
1.32M
        if (dst->key == NULL) {
128
0
            hmac_free(dst);
129
0
            return 0;
130
0
        }
131
1.32M
        if (src->keylen > 0)
132
1.32M
            memcpy(dst->key, src->key, src->keylen);
133
1.32M
    }
134
1.32M
    return dst;
135
1.32M
}
136
137
static size_t hmac_size(struct hmac_data_st *macctx)
138
1.12M
{
139
1.12M
    return HMAC_size(macctx->ctx);
140
1.12M
}
141
142
static int hmac_block_size(struct hmac_data_st *macctx)
143
0
{
144
0
    const EVP_MD *md = ossl_prov_digest_md(&macctx->digest);
145
146
0
    if (md == NULL)
147
0
        return 0;
148
0
    return EVP_MD_block_size(md);
149
0
}
150
151
static int hmac_setkey(struct hmac_data_st *macctx,
152
                       const unsigned char *key, size_t keylen)
153
266k
{
154
266k
    const EVP_MD *digest;
155
156
266k
    if (macctx->key != NULL)
157
15.1k
        OPENSSL_clear_free(macctx->key, macctx->keylen);
158
    /* Keep a copy of the key in case we need it for TLS HMAC */
159
266k
    macctx->key = OPENSSL_malloc(keylen > 0 ? keylen : 1);
160
266k
    if (macctx->key == NULL)
161
0
        return 0;
162
163
266k
    if (keylen > 0)
164
254k
        memcpy(macctx->key, key, keylen);
165
266k
    macctx->keylen = keylen;
166
167
266k
    digest = ossl_prov_digest_md(&macctx->digest);
168
    /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
169
266k
    if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL))
170
266k
        return HMAC_Init_ex(macctx->ctx, key, keylen, digest,
171
266k
                            ossl_prov_digest_engine(&macctx->digest));
172
0
    return 1;
173
266k
}
174
175
static int hmac_init(void *vmacctx, const unsigned char *key,
176
                     size_t keylen, const OSSL_PARAM params[])
177
266k
{
178
266k
    struct hmac_data_st *macctx = vmacctx;
179
180
266k
    if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params))
181
106
        return 0;
182
183
266k
    if (key != NULL)
184
266k
        return hmac_setkey(macctx, key, keylen);
185
186
    /* Just reinit the HMAC context */
187
0
    return HMAC_Init_ex(macctx->ctx, NULL, 0, NULL, NULL);
188
266k
}
189
190
static int hmac_update(void *vmacctx, const unsigned char *data,
191
                       size_t datalen)
192
1.57M
{
193
1.57M
    struct hmac_data_st *macctx = vmacctx;
194
195
1.57M
    if (macctx->tls_data_size > 0) {
196
        /* We're doing a TLS HMAC */
197
274k
        if (!macctx->tls_header_set) {
198
            /* We expect the first update call to contain the TLS header */
199
137k
            if (datalen != sizeof(macctx->tls_header))
200
90
                return 0;
201
137k
            memcpy(macctx->tls_header, data, datalen);
202
137k
            macctx->tls_header_set = 1;
203
137k
            return 1;
204
137k
        }
205
        /* macctx->tls_data_size is datalen plus the padding length */
206
137k
        if (macctx->tls_data_size < datalen)
207
0
            return 0;
208
209
137k
        return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest),
210
137k
                                      macctx->tls_mac_out,
211
137k
                                      &macctx->tls_mac_out_size,
212
137k
                                      macctx->tls_header,
213
137k
                                      data,
214
137k
                                      datalen,
215
137k
                                      macctx->tls_data_size,
216
137k
                                      macctx->key,
217
137k
                                      macctx->keylen,
218
137k
                                      0);
219
137k
    }
220
221
1.29M
    return HMAC_Update(macctx->ctx, data, datalen);
222
1.57M
}
223
224
static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl,
225
                      size_t outsize)
226
1.03M
{
227
1.03M
    unsigned int hlen;
228
1.03M
    struct hmac_data_st *macctx = vmacctx;
229
230
1.03M
    if (!ossl_prov_is_running())
231
0
        return 0;
232
1.03M
    if (macctx->tls_data_size > 0) {
233
137k
        if (macctx->tls_mac_out_size == 0)
234
0
            return 0;
235
137k
        if (outl != NULL)
236
137k
            *outl = macctx->tls_mac_out_size;
237
137k
        memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size);
238
137k
        return 1;
239
137k
    }
240
901k
    if (!HMAC_Final(macctx->ctx, out, &hlen))
241
0
        return 0;
242
901k
    *outl = hlen;
243
901k
    return 1;
244
901k
}
245
246
static const OSSL_PARAM known_gettable_ctx_params[] = {
247
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
248
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
249
    OSSL_PARAM_END
250
};
251
static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *ctx,
252
                                                  ossl_unused void *provctx)
253
0
{
254
0
    return known_gettable_ctx_params;
255
0
}
256
257
static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
258
874k
{
259
874k
    struct hmac_data_st *macctx = vmacctx;
260
874k
    OSSL_PARAM *p;
261
262
874k
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
263
874k
            && !OSSL_PARAM_set_size_t(p, hmac_size(macctx)))
264
0
        return 0;
265
266
874k
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
267
0
            && !OSSL_PARAM_set_int(p, hmac_block_size(macctx)))
268
0
        return 0;
269
270
874k
    return 1;
271
874k
}
272
273
static const OSSL_PARAM known_settable_ctx_params[] = {
274
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0),
275
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
276
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
277
    OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_NOINIT, NULL),
278
    OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_ONESHOT, NULL),
279
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL),
280
    OSSL_PARAM_END
281
};
282
static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *ctx,
283
                                                  ossl_unused void *provctx)
284
149k
{
285
149k
    return known_settable_ctx_params;
286
149k
}
287
288
static int set_flag(const OSSL_PARAM params[], const char *key, int mask,
289
                    int *flags)
290
136k
{
291
136k
    const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, key);
292
136k
    int flag = 0;
293
294
136k
    if (p != NULL) {
295
0
        if (!OSSL_PARAM_get_int(p, &flag))
296
0
            return 0;
297
0
        if (flag == 0)
298
0
            *flags &= ~mask;
299
0
        else
300
0
            *flags |= mask;
301
0
    }
302
136k
    return 1;
303
136k
}
304
305
/*
306
 * ALL parameters should be set before init().
307
 */
308
static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
309
144k
{
310
144k
    struct hmac_data_st *macctx = vmacctx;
311
144k
    OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
312
144k
    const OSSL_PARAM *p;
313
144k
    int flags = 0;
314
315
144k
    if (params == NULL)
316
75.8k
        return 1;
317
318
68.2k
    if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx))
319
2
        return 0;
320
321
68.2k
    if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_NOINIT, EVP_MD_CTX_FLAG_NO_INIT,
322
68.2k
                  &flags))
323
0
        return 0;
324
68.2k
    if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_ONESHOT, EVP_MD_CTX_FLAG_ONESHOT,
325
68.2k
                  &flags))
326
0
        return 0;
327
68.2k
    if (flags)
328
0
        HMAC_CTX_set_flags(macctx->ctx, flags);
329
330
68.2k
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
331
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING)
332
0
            return 0;
333
0
        if (!hmac_setkey(macctx, p->data, p->data_size))
334
0
            return 0;
335
0
    }
336
337
68.2k
    if ((p = OSSL_PARAM_locate_const(params,
338
68.2k
                                     OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) {
339
342
        if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size))
340
0
            return 0;
341
342
    }
342
68.2k
    return 1;
343
68.2k
}
344
345
const OSSL_DISPATCH ossl_hmac_functions[] = {
346
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new },
347
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
348
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
349
    { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
350
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
351
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
352
    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
353
      (void (*)(void))hmac_gettable_ctx_params },
354
    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
355
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
356
      (void (*)(void))hmac_settable_ctx_params },
357
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },
358
    { 0, NULL }
359
};