Coverage Report

Created: 2023-09-25 06:41

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