Coverage Report

Created: 2026-07-16 06:59

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