Coverage Report

Created: 2026-04-01 06:39

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