Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/hmac/hmac.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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 <stdio.h>
17
#include <stdlib.h>
18
#include <string.h>
19
#include "internal/cryptlib.h"
20
#include <openssl/opensslconf.h>
21
#include <openssl/hmac.h>
22
#include <openssl/core_names.h>
23
#include "hmac_local.h"
24
25
int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
26
    const EVP_MD *md, ENGINE *impl)
27
923k
{
28
923k
    int rv = 0, reset = 0;
29
923k
    int i, j;
30
923k
    unsigned char pad[HMAC_MAX_MD_CBLOCK_SIZE];
31
923k
    unsigned int keytmp_length;
32
923k
    unsigned char keytmp[HMAC_MAX_MD_CBLOCK_SIZE];
33
34
    /* If we are changing MD then we must have a key */
35
923k
    if (md != NULL && md != ctx->md && (key == NULL || len < 0))
36
0
        return 0;
37
38
923k
    if (md != NULL)
39
923k
        ctx->md = md;
40
0
    else if (ctx->md != NULL)
41
0
        md = ctx->md;
42
0
    else
43
0
        return 0;
44
45
    /*
46
     * The HMAC construction is not allowed to be used with the
47
     * extendable-output functions (XOF) shake128 and shake256.
48
     */
49
923k
    if (EVP_MD_xof(md))
50
3
        return 0;
51
52
#ifdef OPENSSL_HMAC_S390X
53
    {
54
        int ret = s390x_HMAC_init(ctx, key, len, impl);
55
        if (ret != -1) /* -1 means SW fallback */
56
            return ret;
57
    }
58
#endif
59
60
923k
    if (key != NULL) {
61
923k
        reset = 1;
62
63
923k
        j = EVP_MD_get_block_size(md);
64
923k
        if (!ossl_assert(j <= (int)sizeof(keytmp)))
65
0
            return 0;
66
923k
        if (j < 0)
67
0
            return 0;
68
923k
        if (j < len) {
69
2.98k
            if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl)
70
2.98k
                || !EVP_DigestUpdate(ctx->md_ctx, key, len)
71
2.98k
                || !EVP_DigestFinal_ex(ctx->md_ctx, keytmp,
72
2.98k
                    &keytmp_length))
73
0
                return 0;
74
920k
        } else {
75
920k
            if (len < 0 || len > (int)sizeof(keytmp))
76
0
                return 0;
77
920k
            memcpy(keytmp, key, len);
78
920k
            keytmp_length = len;
79
920k
        }
80
923k
        if (keytmp_length != HMAC_MAX_MD_CBLOCK_SIZE)
81
923k
            memset(&keytmp[keytmp_length], 0,
82
923k
                HMAC_MAX_MD_CBLOCK_SIZE - keytmp_length);
83
84
133M
        for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
85
133M
            pad[i] = 0x36 ^ keytmp[i];
86
923k
        if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl)
87
923k
            || !EVP_DigestUpdate(ctx->i_ctx, pad,
88
923k
                EVP_MD_get_block_size(md)))
89
0
            goto err;
90
91
133M
        for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
92
133M
            pad[i] = 0x5c ^ keytmp[i];
93
923k
        if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl)
94
923k
            || !EVP_DigestUpdate(ctx->o_ctx, pad,
95
923k
                EVP_MD_get_block_size(md)))
96
0
            goto err;
97
923k
    }
98
923k
    if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
99
0
        goto err;
100
923k
    rv = 1;
101
923k
err:
102
923k
    if (reset) {
103
923k
        OPENSSL_cleanse(keytmp, sizeof(keytmp));
104
923k
        OPENSSL_cleanse(pad, sizeof(pad));
105
923k
    }
106
923k
    return rv;
107
923k
}
108
109
#ifndef OPENSSL_NO_DEPRECATED_1_1_0
110
int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
111
0
{
112
0
    if (key && md)
113
0
        HMAC_CTX_reset(ctx);
114
0
    return HMAC_Init_ex(ctx, key, len, md, NULL);
115
0
}
116
#endif
117
118
int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
119
5.29M
{
120
5.29M
    if (!ctx->md)
121
0
        return 0;
122
123
#ifdef OPENSSL_HMAC_S390X
124
    if (ctx->plat.s390x.fc)
125
        return s390x_HMAC_update(ctx, data, len);
126
#endif
127
128
5.29M
    return EVP_DigestUpdate(ctx->md_ctx, data, len);
129
5.29M
}
130
131
int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
132
3.43M
{
133
3.43M
    unsigned int i;
134
3.43M
    unsigned char buf[EVP_MAX_MD_SIZE];
135
136
3.43M
    if (!ctx->md)
137
0
        goto err;
138
139
#ifdef OPENSSL_HMAC_S390X
140
    if (ctx->plat.s390x.fc)
141
        return s390x_HMAC_final(ctx, md, len);
142
#endif
143
144
3.43M
    if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
145
0
        goto err;
146
3.43M
    if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
147
0
        goto err;
148
3.43M
    if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
149
0
        goto err;
150
3.43M
    if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
151
0
        goto err;
152
3.43M
    return 1;
153
0
err:
154
0
    return 0;
155
3.43M
}
156
157
size_t HMAC_size(const HMAC_CTX *ctx)
158
1.58M
{
159
1.58M
    int size = EVP_MD_get_size((ctx)->md);
160
161
1.58M
    return (size < 0) ? 0 : size;
162
1.58M
}
163
164
HMAC_CTX *HMAC_CTX_new(void)
165
3.52M
{
166
3.52M
    HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
167
168
3.52M
    if (ctx != NULL) {
169
3.52M
        if (!HMAC_CTX_reset(ctx)) {
170
0
            HMAC_CTX_free(ctx);
171
0
            return NULL;
172
0
        }
173
3.52M
    }
174
3.52M
    return ctx;
175
3.52M
}
176
177
static void hmac_ctx_cleanup(HMAC_CTX *ctx)
178
7.04M
{
179
7.04M
    EVP_MD_CTX_reset(ctx->i_ctx);
180
7.04M
    EVP_MD_CTX_reset(ctx->o_ctx);
181
7.04M
    EVP_MD_CTX_reset(ctx->md_ctx);
182
7.04M
    ctx->md = NULL;
183
184
#ifdef OPENSSL_HMAC_S390X
185
    s390x_HMAC_CTX_cleanup(ctx);
186
#endif
187
7.04M
}
188
189
void HMAC_CTX_free(HMAC_CTX *ctx)
190
3.52M
{
191
3.52M
    if (ctx != NULL) {
192
3.52M
        hmac_ctx_cleanup(ctx);
193
3.52M
        EVP_MD_CTX_free(ctx->i_ctx);
194
3.52M
        EVP_MD_CTX_free(ctx->o_ctx);
195
3.52M
        EVP_MD_CTX_free(ctx->md_ctx);
196
3.52M
        OPENSSL_free(ctx);
197
3.52M
    }
198
3.52M
}
199
200
static int hmac_ctx_alloc_mds(HMAC_CTX *ctx)
201
6.47M
{
202
6.47M
    if (ctx->i_ctx == NULL)
203
3.52M
        ctx->i_ctx = EVP_MD_CTX_new();
204
6.47M
    if (ctx->i_ctx == NULL)
205
0
        return 0;
206
6.47M
    if (ctx->o_ctx == NULL)
207
3.52M
        ctx->o_ctx = EVP_MD_CTX_new();
208
6.47M
    if (ctx->o_ctx == NULL)
209
0
        return 0;
210
6.47M
    if (ctx->md_ctx == NULL)
211
3.52M
        ctx->md_ctx = EVP_MD_CTX_new();
212
6.47M
    if (ctx->md_ctx == NULL)
213
0
        return 0;
214
6.47M
    return 1;
215
6.47M
}
216
217
int HMAC_CTX_reset(HMAC_CTX *ctx)
218
3.52M
{
219
3.52M
    hmac_ctx_cleanup(ctx);
220
3.52M
    if (!hmac_ctx_alloc_mds(ctx)) {
221
0
        hmac_ctx_cleanup(ctx);
222
0
        return 0;
223
0
    }
224
3.52M
    return 1;
225
3.52M
}
226
227
int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
228
2.94M
{
229
2.94M
    if (!hmac_ctx_alloc_mds(dctx))
230
0
        goto err;
231
2.94M
    if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
232
0
        goto err;
233
2.94M
    if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
234
0
        goto err;
235
2.94M
    if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
236
0
        goto err;
237
2.94M
    dctx->md = sctx->md;
238
239
#ifdef OPENSSL_HMAC_S390X
240
    if (s390x_HMAC_CTX_copy(dctx, sctx) == 0)
241
        goto err;
242
#endif
243
244
2.94M
    return 1;
245
0
err:
246
0
    hmac_ctx_cleanup(dctx);
247
0
    return 0;
248
2.94M
}
249
250
unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
251
    const unsigned char *data, size_t data_len,
252
    unsigned char *md, unsigned int *md_len)
253
0
{
254
0
    static unsigned char static_md[EVP_MAX_MD_SIZE];
255
0
    int size = EVP_MD_get_size(evp_md);
256
0
    size_t temp_md_len = 0;
257
0
    unsigned char *ret = NULL;
258
259
0
    if (size > 0) {
260
0
        ret = EVP_Q_mac(NULL, "HMAC", NULL, EVP_MD_get0_name(evp_md), NULL,
261
0
            key, key_len, data, data_len,
262
0
            md == NULL ? static_md : md, size, &temp_md_len);
263
0
        if (md_len != NULL)
264
0
            *md_len = (unsigned int)temp_md_len;
265
0
    }
266
0
    return ret;
267
0
}
268
269
void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
270
0
{
271
0
    EVP_MD_CTX_set_flags(ctx->i_ctx, flags);
272
0
    EVP_MD_CTX_set_flags(ctx->o_ctx, flags);
273
0
    EVP_MD_CTX_set_flags(ctx->md_ctx, flags);
274
0
}
275
276
const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx)
277
0
{
278
0
    return ctx->md;
279
0
}