Coverage Report

Created: 2018-08-29 13:53

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