Coverage Report

Created: 2025-06-13 06:56

/src/openssl/providers/implementations/macs/hmac_prov.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2018-2024 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
#include <openssl/proverr.h>
24
#include <openssl/err.h>
25
26
#include "internal/ssl3_cbc.h"
27
28
#include "prov/implementations.h"
29
#include "prov/provider_ctx.h"
30
#include "prov/provider_util.h"
31
#include "prov/providercommon.h"
32
#include "prov/securitycheck.h"
33
34
/*
35
 * Forward declaration of everything implemented here.  This is not strictly
36
 * necessary for the compiler, but provides an assurance that the signatures
37
 * of the functions in the dispatch table are correct.
38
 */
39
static OSSL_FUNC_mac_newctx_fn hmac_new;
40
static OSSL_FUNC_mac_dupctx_fn hmac_dup;
41
static OSSL_FUNC_mac_freectx_fn hmac_free;
42
static OSSL_FUNC_mac_gettable_ctx_params_fn hmac_gettable_ctx_params;
43
static OSSL_FUNC_mac_get_ctx_params_fn hmac_get_ctx_params;
44
static OSSL_FUNC_mac_settable_ctx_params_fn hmac_settable_ctx_params;
45
static OSSL_FUNC_mac_set_ctx_params_fn hmac_set_ctx_params;
46
static OSSL_FUNC_mac_init_fn hmac_init;
47
static OSSL_FUNC_mac_update_fn hmac_update;
48
static OSSL_FUNC_mac_final_fn hmac_final;
49
50
/* local HMAC context structure */
51
52
/* typedef EVP_MAC_IMPL */
53
struct hmac_data_st {
54
    void *provctx;
55
    HMAC_CTX *ctx;               /* HMAC context */
56
    PROV_DIGEST digest;
57
    unsigned char *key;
58
    size_t keylen;
59
    /* Length of full TLS record including the MAC and any padding */
60
    size_t tls_data_size;
61
    unsigned char tls_header[13];
62
    int tls_header_set;
63
    unsigned char tls_mac_out[EVP_MAX_MD_SIZE];
64
    size_t tls_mac_out_size;
65
#ifdef FIPS_MODULE
66
    /*
67
     * 'internal' is set to 1 if HMAC is used inside another algorithm such as a
68
     * KDF. In this case it is the parent algorithm that is responsible for
69
     * performing any conditional FIPS indicator related checks for the HMAC.
70
     */
71
    int internal;
72
#endif
73
    OSSL_FIPS_IND_DECLARE
74
};
75
76
static void *hmac_new(void *provctx)
77
0
{
78
0
    struct hmac_data_st *macctx;
79
80
0
    if (!ossl_prov_is_running())
81
0
        return NULL;
82
83
0
    if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
84
0
        || (macctx->ctx = HMAC_CTX_new()) == NULL) {
85
0
        OPENSSL_free(macctx);
86
0
        return NULL;
87
0
    }
88
0
    macctx->provctx = provctx;
89
0
    OSSL_FIPS_IND_INIT(macctx)
90
91
0
    return macctx;
92
0
}
93
94
static void hmac_free(void *vmacctx)
95
0
{
96
0
    struct hmac_data_st *macctx = vmacctx;
97
98
0
    if (macctx != NULL) {
99
0
        HMAC_CTX_free(macctx->ctx);
100
0
        ossl_prov_digest_reset(&macctx->digest);
101
0
        OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
102
0
        OPENSSL_free(macctx);
103
0
    }
104
0
}
105
106
static void *hmac_dup(void *vsrc)
107
0
{
108
0
    struct hmac_data_st *src = vsrc;
109
0
    struct hmac_data_st *dst;
110
0
    HMAC_CTX *ctx;
111
112
0
    if (!ossl_prov_is_running())
113
0
        return NULL;
114
0
    dst = hmac_new(src->provctx);
115
0
    if (dst == NULL)
116
0
        return NULL;
117
118
0
    ctx = dst->ctx;
119
0
    *dst = *src;
120
0
    dst->ctx = ctx;
121
0
    dst->key = NULL;
122
0
    memset(&dst->digest, 0, sizeof(dst->digest));
123
124
0
    if (!HMAC_CTX_copy(dst->ctx, src->ctx)
125
0
        || !ossl_prov_digest_copy(&dst->digest, &src->digest)) {
126
0
        hmac_free(dst);
127
0
        return NULL;
128
0
    }
129
0
    if (src->key != NULL) {
130
        /* There is no "secure" OPENSSL_memdup */
131
0
        dst->key = OPENSSL_secure_malloc(src->keylen > 0 ? src->keylen : 1);
132
0
        if (dst->key == NULL) {
133
0
            hmac_free(dst);
134
0
            return 0;
135
0
        }
136
0
        memcpy(dst->key, src->key, src->keylen);
137
0
    }
138
0
    return dst;
139
0
}
140
141
static size_t hmac_size(struct hmac_data_st *macctx)
142
0
{
143
0
    return HMAC_size(macctx->ctx);
144
0
}
145
146
static int hmac_block_size(struct hmac_data_st *macctx)
147
0
{
148
0
    const EVP_MD *md = ossl_prov_digest_md(&macctx->digest);
149
150
0
    if (md == NULL)
151
0
        return 0;
152
0
    return EVP_MD_block_size(md);
153
0
}
154
155
static int hmac_setkey(struct hmac_data_st *macctx,
156
                       const unsigned char *key, size_t keylen)
157
0
{
158
0
    const EVP_MD *digest;
159
160
#ifdef FIPS_MODULE
161
    /*
162
     * KDF's pass a salt rather than a key,
163
     * which is why it skips the key check unless "HMAC" is fetched directly.
164
     */
165
    if (!macctx->internal) {
166
        OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(macctx->provctx);
167
        int approved = ossl_mac_check_key_size(keylen);
168
169
        if (!approved) {
170
            if (!OSSL_FIPS_IND_ON_UNAPPROVED(macctx, OSSL_FIPS_IND_SETTABLE0,
171
                                             libctx, "HMAC", "keysize",
172
                                             ossl_fips_config_hmac_key_check)) {
173
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
174
                return 0;
175
            }
176
        }
177
    }
178
#endif
179
180
0
    if (macctx->key != NULL)
181
0
        OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
182
    /* Keep a copy of the key in case we need it for TLS HMAC */
183
0
    macctx->key = OPENSSL_secure_malloc(keylen > 0 ? keylen : 1);
184
0
    if (macctx->key == NULL)
185
0
        return 0;
186
187
0
    memcpy(macctx->key, key, keylen);
188
0
    macctx->keylen = keylen;
189
190
0
    digest = ossl_prov_digest_md(&macctx->digest);
191
    /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
192
0
    if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL))
193
0
        return HMAC_Init_ex(macctx->ctx, key, keylen, digest,
194
0
                            ossl_prov_digest_engine(&macctx->digest));
195
0
    return 1;
196
0
}
197
198
static int hmac_init(void *vmacctx, const unsigned char *key,
199
                     size_t keylen, const OSSL_PARAM params[])
200
0
{
201
0
    struct hmac_data_st *macctx = vmacctx;
202
203
0
    if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params))
204
0
        return 0;
205
206
0
    if (key != NULL)
207
0
        return hmac_setkey(macctx, key, keylen);
208
209
    /* Just reinit the HMAC context */
210
0
    return HMAC_Init_ex(macctx->ctx, NULL, 0, NULL, NULL);
211
0
}
212
213
static int hmac_update(void *vmacctx, const unsigned char *data,
214
                       size_t datalen)
215
0
{
216
0
    struct hmac_data_st *macctx = vmacctx;
217
218
0
    if (macctx->tls_data_size > 0) {
219
        /* We're doing a TLS HMAC */
220
0
        if (!macctx->tls_header_set) {
221
            /* We expect the first update call to contain the TLS header */
222
0
            if (datalen != sizeof(macctx->tls_header))
223
0
                return 0;
224
0
            memcpy(macctx->tls_header, data, datalen);
225
0
            macctx->tls_header_set = 1;
226
0
            return 1;
227
0
        }
228
        /* macctx->tls_data_size is datalen plus the padding length */
229
0
        if (macctx->tls_data_size < datalen)
230
0
            return 0;
231
232
0
        return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest),
233
0
                                      macctx->tls_mac_out,
234
0
                                      &macctx->tls_mac_out_size,
235
0
                                      macctx->tls_header,
236
0
                                      data,
237
0
                                      datalen,
238
0
                                      macctx->tls_data_size,
239
0
                                      macctx->key,
240
0
                                      macctx->keylen,
241
0
                                      0);
242
0
    }
243
244
0
    return HMAC_Update(macctx->ctx, data, datalen);
245
0
}
246
247
static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl,
248
                      size_t outsize)
249
0
{
250
0
    unsigned int hlen;
251
0
    struct hmac_data_st *macctx = vmacctx;
252
253
0
    if (!ossl_prov_is_running())
254
0
        return 0;
255
0
    if (macctx->tls_data_size > 0) {
256
0
        if (macctx->tls_mac_out_size == 0)
257
0
            return 0;
258
0
        if (outl != NULL)
259
0
            *outl = macctx->tls_mac_out_size;
260
0
        memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size);
261
0
        return 1;
262
0
    }
263
0
    if (!HMAC_Final(macctx->ctx, out, &hlen))
264
0
        return 0;
265
0
    *outl = hlen;
266
0
    return 1;
267
0
}
268
269
static const OSSL_PARAM known_gettable_ctx_params[] = {
270
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
271
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
272
    OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
273
    OSSL_PARAM_END
274
};
275
static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *ctx,
276
                                                  ossl_unused void *provctx)
277
0
{
278
0
    return known_gettable_ctx_params;
279
0
}
280
281
static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
282
0
{
283
0
    struct hmac_data_st *macctx = vmacctx;
284
0
    OSSL_PARAM *p;
285
286
0
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
287
0
            && !OSSL_PARAM_set_size_t(p, hmac_size(macctx)))
288
0
        return 0;
289
290
0
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
291
0
            && !OSSL_PARAM_set_int(p, hmac_block_size(macctx)))
292
0
        return 0;
293
294
#ifdef FIPS_MODULE
295
    p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_FIPS_APPROVED_INDICATOR);
296
    if (p != NULL) {
297
        int approved = 0;
298
299
        if (!macctx->internal)
300
            approved = OSSL_FIPS_IND_GET(macctx)->approved;
301
        if (!OSSL_PARAM_set_int(p, approved))
302
            return 0;
303
    }
304
#endif
305
0
    return 1;
306
0
}
307
308
static const OSSL_PARAM known_settable_ctx_params[] = {
309
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0),
310
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
311
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
312
    OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_NOINIT, NULL),
313
    OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_ONESHOT, NULL),
314
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL),
315
    OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_MAC_PARAM_FIPS_KEY_CHECK)
316
    OSSL_PARAM_END
317
};
318
static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *ctx,
319
                                                  ossl_unused void *provctx)
320
0
{
321
0
    return known_settable_ctx_params;
322
0
}
323
324
/*
325
 * ALL parameters should be set before init().
326
 */
327
static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
328
0
{
329
0
    struct hmac_data_st *macctx = vmacctx;
330
0
    OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
331
0
    const OSSL_PARAM *p;
332
333
0
    if (ossl_param_is_empty(params))
334
0
        return 1;
335
336
0
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(macctx, OSSL_FIPS_IND_SETTABLE0, params,
337
0
                                     OSSL_MAC_PARAM_FIPS_KEY_CHECK))
338
0
        return 0;
339
340
0
    if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx))
341
0
        return 0;
342
343
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
344
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING)
345
0
            return 0;
346
347
0
        if (!hmac_setkey(macctx, p->data, p->data_size))
348
0
            return 0;
349
0
    }
350
351
0
    if ((p = OSSL_PARAM_locate_const(params,
352
0
                                     OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) {
353
0
        if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size))
354
0
            return 0;
355
0
    }
356
0
    return 1;
357
0
}
358
359
const OSSL_DISPATCH ossl_hmac_functions[] = {
360
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new },
361
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
362
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
363
    { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
364
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
365
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
366
    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
367
      (void (*)(void))hmac_gettable_ctx_params },
368
    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
369
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
370
      (void (*)(void))hmac_settable_ctx_params },
371
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },
372
    OSSL_DISPATCH_END
373
};
374
375
#ifdef FIPS_MODULE
376
static OSSL_FUNC_mac_newctx_fn hmac_internal_new;
377
378
static void *hmac_internal_new(void *provctx)
379
{
380
    struct hmac_data_st *macctx = hmac_new(provctx);
381
382
    if (macctx != NULL)
383
        macctx->internal = 1;
384
    return macctx;
385
}
386
387
const OSSL_DISPATCH ossl_hmac_internal_functions[] = {
388
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_internal_new },
389
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
390
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
391
    { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
392
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
393
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
394
    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
395
      (void (*)(void))hmac_gettable_ctx_params },
396
    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
397
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
398
      (void (*)(void))hmac_settable_ctx_params },
399
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },
400
    OSSL_DISPATCH_END
401
};
402
403
#endif /* FIPS_MODULE */