Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/hmac/hmac.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
#include <stdio.h>
11
#include <stdlib.h>
12
#include <string.h>
13
#include "internal/cryptlib.h"
14
#include <openssl/hmac.h>
15
#include <openssl/opensslconf.h>
16
#include "hmac_local.h"
17
18
int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
19
                 const EVP_MD *md, ENGINE *impl)
20
0
{
21
0
    int rv = 0, reset = 0;
22
0
    int i, j;
23
0
    unsigned char pad[HMAC_MAX_MD_CBLOCK_SIZE];
24
0
    unsigned int keytmp_length;
25
0
    unsigned char keytmp[HMAC_MAX_MD_CBLOCK_SIZE];
26
27
    /* If we are changing MD then we must have a key */
28
0
    if (md != NULL && md != ctx->md && (key == NULL || len < 0))
29
0
        return 0;
30
31
0
    if (md != NULL) {
32
0
        ctx->md = md;
33
0
    } else if (ctx->md) {
34
0
        md = ctx->md;
35
0
    } else {
36
0
        return 0;
37
0
    }
38
39
    /*
40
     * The HMAC construction is not allowed  to be used with the
41
     * extendable-output functions (XOF) shake128 and shake256.
42
     */
43
0
    if ((EVP_MD_meth_get_flags(md) & EVP_MD_FLAG_XOF) != 0)
44
0
        return 0;
45
46
0
    if (key != NULL) {
47
0
        reset = 1;
48
49
0
        j = EVP_MD_block_size(md);
50
0
        if (!ossl_assert(j <= (int)sizeof(keytmp)))
51
0
            return 0;
52
0
        if (j < len) {
53
0
            if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl)
54
0
                    || !EVP_DigestUpdate(ctx->md_ctx, key, len)
55
0
                    || !EVP_DigestFinal_ex(ctx->md_ctx, keytmp,
56
0
                                           &keytmp_length))
57
0
                return 0;
58
0
        } else {
59
0
            if (len < 0 || len > (int)sizeof(keytmp))
60
0
                return 0;
61
0
            memcpy(keytmp, key, len);
62
0
            keytmp_length = len;
63
0
        }
64
0
        if (keytmp_length != HMAC_MAX_MD_CBLOCK_SIZE)
65
0
            memset(&keytmp[keytmp_length], 0,
66
0
                   HMAC_MAX_MD_CBLOCK_SIZE - keytmp_length);
67
68
0
        for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
69
0
            pad[i] = 0x36 ^ keytmp[i];
70
0
        if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl)
71
0
                || !EVP_DigestUpdate(ctx->i_ctx, pad, EVP_MD_block_size(md)))
72
0
            goto err;
73
74
0
        for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
75
0
            pad[i] = 0x5c ^ keytmp[i];
76
0
        if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl)
77
0
                || !EVP_DigestUpdate(ctx->o_ctx, pad, EVP_MD_block_size(md)))
78
0
            goto err;
79
0
    }
80
0
    if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
81
0
        goto err;
82
0
    rv = 1;
83
0
 err:
84
0
    if (reset) {
85
0
        OPENSSL_cleanse(keytmp, sizeof(keytmp));
86
0
        OPENSSL_cleanse(pad, sizeof(pad));
87
0
    }
88
0
    return rv;
89
0
}
90
91
#if OPENSSL_API_COMPAT < 0x10100000L
92
int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
93
0
{
94
0
    if (key && md)
95
0
        HMAC_CTX_reset(ctx);
96
0
    return HMAC_Init_ex(ctx, key, len, md, NULL);
97
0
}
98
#endif
99
100
int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
101
0
{
102
0
    if (!ctx->md)
103
0
        return 0;
104
0
    return EVP_DigestUpdate(ctx->md_ctx, data, len);
105
0
}
106
107
int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
108
0
{
109
0
    unsigned int i;
110
0
    unsigned char buf[EVP_MAX_MD_SIZE];
111
112
0
    if (!ctx->md)
113
0
        goto err;
114
115
0
    if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
116
0
        goto err;
117
0
    if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
118
0
        goto err;
119
0
    if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
120
0
        goto err;
121
0
    if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
122
0
        goto err;
123
0
    return 1;
124
0
 err:
125
0
    return 0;
126
0
}
127
128
size_t HMAC_size(const HMAC_CTX *ctx)
129
0
{
130
0
    int size = EVP_MD_size((ctx)->md);
131
132
0
    return (size < 0) ? 0 : size;
133
0
}
134
135
HMAC_CTX *HMAC_CTX_new(void)
136
0
{
137
0
    HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
138
139
0
    if (ctx != NULL) {
140
0
        if (!HMAC_CTX_reset(ctx)) {
141
0
            HMAC_CTX_free(ctx);
142
0
            return NULL;
143
0
        }
144
0
    }
145
0
    return ctx;
146
0
}
147
148
static void hmac_ctx_cleanup(HMAC_CTX *ctx)
149
0
{
150
0
    EVP_MD_CTX_reset(ctx->i_ctx);
151
0
    EVP_MD_CTX_reset(ctx->o_ctx);
152
0
    EVP_MD_CTX_reset(ctx->md_ctx);
153
0
    ctx->md = NULL;
154
0
}
155
156
void HMAC_CTX_free(HMAC_CTX *ctx)
157
0
{
158
0
    if (ctx != NULL) {
159
0
        hmac_ctx_cleanup(ctx);
160
0
        EVP_MD_CTX_free(ctx->i_ctx);
161
0
        EVP_MD_CTX_free(ctx->o_ctx);
162
0
        EVP_MD_CTX_free(ctx->md_ctx);
163
0
        OPENSSL_free(ctx);
164
0
    }
165
0
}
166
167
static int hmac_ctx_alloc_mds(HMAC_CTX *ctx)
168
0
{
169
0
    if (ctx->i_ctx == NULL)
170
0
        ctx->i_ctx = EVP_MD_CTX_new();
171
0
    if (ctx->i_ctx == NULL)
172
0
        return 0;
173
0
    if (ctx->o_ctx == NULL)
174
0
        ctx->o_ctx = EVP_MD_CTX_new();
175
0
    if (ctx->o_ctx == NULL)
176
0
        return 0;
177
0
    if (ctx->md_ctx == NULL)
178
0
        ctx->md_ctx = EVP_MD_CTX_new();
179
0
    if (ctx->md_ctx == NULL)
180
0
        return 0;
181
0
    return 1;
182
0
}
183
184
int HMAC_CTX_reset(HMAC_CTX *ctx)
185
0
{
186
0
    hmac_ctx_cleanup(ctx);
187
0
    if (!hmac_ctx_alloc_mds(ctx)) {
188
0
        hmac_ctx_cleanup(ctx);
189
0
        return 0;
190
0
    }
191
0
    return 1;
192
0
}
193
194
int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
195
0
{
196
0
    if (!hmac_ctx_alloc_mds(dctx))
197
0
        goto err;
198
0
    if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
199
0
        goto err;
200
0
    if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
201
0
        goto err;
202
0
    if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
203
0
        goto err;
204
0
    dctx->md = sctx->md;
205
0
    return 1;
206
0
 err:
207
0
    hmac_ctx_cleanup(dctx);
208
0
    return 0;
209
0
}
210
211
unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
212
                    const unsigned char *d, size_t n, unsigned char *md,
213
                    unsigned int *md_len)
214
0
{
215
0
    HMAC_CTX *c = NULL;
216
0
    static unsigned char m[EVP_MAX_MD_SIZE];
217
0
    static const unsigned char dummy_key[1] = {'\0'};
218
219
0
    if (md == NULL)
220
0
        md = m;
221
0
    if ((c = HMAC_CTX_new()) == NULL)
222
0
        goto err;
223
224
    /* For HMAC_Init_ex, NULL key signals reuse. */
225
0
    if (key == NULL && key_len == 0) {
226
0
        key = dummy_key;
227
0
    }
228
229
0
    if (!HMAC_Init_ex(c, key, key_len, evp_md, NULL))
230
0
        goto err;
231
0
    if (!HMAC_Update(c, d, n))
232
0
        goto err;
233
0
    if (!HMAC_Final(c, md, md_len))
234
0
        goto err;
235
0
    HMAC_CTX_free(c);
236
0
    return md;
237
0
 err:
238
0
    HMAC_CTX_free(c);
239
0
    return NULL;
240
0
}
241
242
void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
243
0
{
244
0
    EVP_MD_CTX_set_flags(ctx->i_ctx, flags);
245
0
    EVP_MD_CTX_set_flags(ctx->o_ctx, flags);
246
0
    EVP_MD_CTX_set_flags(ctx->md_ctx, flags);
247
0
}
248
249
const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx)
250
0
{
251
0
    return ctx->md;
252
0
}