Coverage Report

Created: 2025-12-08 06:22

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
    rv = s390x_HMAC_init(ctx, key, len);
57
    if (rv >= 1)
58
        return rv;
59
#endif
60
61
0
    if (key != NULL) {
62
0
        reset = 1;
63
64
0
        j = EVP_MD_get_block_size(md);
65
0
        if (!ossl_assert(j <= (int)sizeof(keytmp)))
66
0
            return 0;
67
0
        if (j < 0)
68
0
            return 0;
69
0
        if (j < len) {
70
0
            if (!EVP_DigestInit_ex(ctx->md_ctx, md, NULL)
71
0
                    || !EVP_DigestUpdate(ctx->md_ctx, key, len)
72
0
                    || !EVP_DigestFinal_ex(ctx->md_ctx, keytmp,
73
0
                                           &keytmp_length))
74
0
                return 0;
75
0
        } else {
76
0
            if (len < 0 || len > (int)sizeof(keytmp))
77
0
                return 0;
78
0
            memcpy(keytmp, key, len);
79
0
            keytmp_length = len;
80
0
        }
81
0
        if (keytmp_length != HMAC_MAX_MD_CBLOCK_SIZE)
82
0
            memset(&keytmp[keytmp_length], 0,
83
0
                   HMAC_MAX_MD_CBLOCK_SIZE - keytmp_length);
84
85
0
        for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
86
0
            pad[i] = 0x36 ^ keytmp[i];
87
0
        if (!EVP_DigestInit_ex(ctx->i_ctx, md, NULL)
88
0
                || !EVP_DigestUpdate(ctx->i_ctx, pad,
89
0
                                     EVP_MD_get_block_size(md)))
90
0
            goto err;
91
92
0
        for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
93
0
            pad[i] = 0x5c ^ keytmp[i];
94
0
        if (!EVP_DigestInit_ex(ctx->o_ctx, md, NULL)
95
0
                || !EVP_DigestUpdate(ctx->o_ctx, pad,
96
0
                                     EVP_MD_get_block_size(md)))
97
0
            goto err;
98
0
    }
99
0
    if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
100
0
        goto err;
101
0
    rv = 1;
102
0
 err:
103
0
    if (reset) {
104
0
        OPENSSL_cleanse(keytmp, sizeof(keytmp));
105
0
        OPENSSL_cleanse(pad, sizeof(pad));
106
0
    }
107
0
    return rv;
108
0
}
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
0
{
121
0
    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
0
    return EVP_DigestUpdate(ctx->md_ctx, data, len);
130
0
}
131
132
int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
133
0
{
134
0
    unsigned int i;
135
0
    unsigned char buf[EVP_MAX_MD_SIZE];
136
137
0
    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
0
    if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
146
0
        goto err;
147
0
    if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
148
0
        goto err;
149
0
    if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
150
0
        goto err;
151
0
    if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
152
0
        goto err;
153
0
    return 1;
154
0
 err:
155
0
    return 0;
156
0
}
157
158
size_t HMAC_size(const HMAC_CTX *ctx)
159
0
{
160
0
    int size = EVP_MD_get_size((ctx)->md);
161
162
0
    return (size < 0) ? 0 : size;
163
0
}
164
165
HMAC_CTX *HMAC_CTX_new(void)
166
0
{
167
0
    HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
168
169
0
    if (ctx != NULL) {
170
0
        if (!HMAC_CTX_reset(ctx)) {
171
0
            HMAC_CTX_free(ctx);
172
0
            return NULL;
173
0
        }
174
0
    }
175
0
    return ctx;
176
0
}
177
178
static void hmac_ctx_cleanup(HMAC_CTX *ctx)
179
0
{
180
0
    EVP_MD_CTX_reset(ctx->i_ctx);
181
0
    EVP_MD_CTX_reset(ctx->o_ctx);
182
0
    EVP_MD_CTX_reset(ctx->md_ctx);
183
0
    ctx->md = NULL;
184
185
#ifdef OPENSSL_HMAC_S390X
186
    s390x_HMAC_CTX_cleanup(ctx);
187
#endif
188
0
}
189
190
void HMAC_CTX_free(HMAC_CTX *ctx)
191
0
{
192
0
    if (ctx != NULL) {
193
0
        hmac_ctx_cleanup(ctx);
194
0
        EVP_MD_CTX_free(ctx->i_ctx);
195
0
        EVP_MD_CTX_free(ctx->o_ctx);
196
0
        EVP_MD_CTX_free(ctx->md_ctx);
197
0
        OPENSSL_free(ctx);
198
0
    }
199
0
}
200
201
static int hmac_ctx_alloc_mds(HMAC_CTX *ctx)
202
0
{
203
0
    if (ctx->i_ctx == NULL)
204
0
        ctx->i_ctx = EVP_MD_CTX_new();
205
0
    if (ctx->i_ctx == NULL)
206
0
        return 0;
207
0
    if (ctx->o_ctx == NULL)
208
0
        ctx->o_ctx = EVP_MD_CTX_new();
209
0
    if (ctx->o_ctx == NULL)
210
0
        return 0;
211
0
    if (ctx->md_ctx == NULL)
212
0
        ctx->md_ctx = EVP_MD_CTX_new();
213
0
    if (ctx->md_ctx == NULL)
214
0
        return 0;
215
0
    return 1;
216
0
}
217
218
int HMAC_CTX_reset(HMAC_CTX *ctx)
219
0
{
220
0
    hmac_ctx_cleanup(ctx);
221
0
    if (!hmac_ctx_alloc_mds(ctx)) {
222
0
        hmac_ctx_cleanup(ctx);
223
0
        return 0;
224
0
    }
225
0
    return 1;
226
0
}
227
228
int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
229
0
{
230
0
    if (!hmac_ctx_alloc_mds(dctx))
231
0
        goto err;
232
0
    if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
233
0
        goto err;
234
0
    if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
235
0
        goto err;
236
0
    if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
237
0
        goto err;
238
0
    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
0
    return 1;
246
0
 err:
247
0
    hmac_ctx_cleanup(dctx);
248
0
    return 0;
249
0
}
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
}