Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/evp/m_sha1.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 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 "internal/cryptlib.h"
12
13
#include <openssl/evp.h>
14
#include <openssl/objects.h>
15
#include <openssl/sha.h>
16
#include <openssl/rsa.h>
17
#include "internal/evp_int.h"
18
#include "internal/sha.h"
19
20
static int init(EVP_MD_CTX *ctx)
21
0
{
22
0
    return SHA1_Init(EVP_MD_CTX_md_data(ctx));
23
0
}
24
25
static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
26
0
{
27
0
    return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count);
28
0
}
29
30
static int final(EVP_MD_CTX *ctx, unsigned char *md)
31
0
{
32
0
    return SHA1_Final(md, EVP_MD_CTX_md_data(ctx));
33
0
}
34
35
static int ctrl(EVP_MD_CTX *ctx, int cmd, int mslen, void *ms)
36
0
{
37
0
    unsigned char padtmp[40];
38
0
    unsigned char sha1tmp[SHA_DIGEST_LENGTH];
39
0
40
0
    SHA_CTX *sha1;
41
0
42
0
    if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
43
0
        return -2;
44
0
45
0
    if (ctx == NULL)
46
0
        return 0;
47
0
48
0
    sha1 = EVP_MD_CTX_md_data(ctx);
49
0
50
0
    /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
51
0
    if (mslen != 48)
52
0
        return 0;
53
0
54
0
    /* At this point hash contains all handshake messages, update
55
0
     * with master secret and pad_1.
56
0
     */
57
0
58
0
    if (SHA1_Update(sha1, ms, mslen) <= 0)
59
0
        return 0;
60
0
61
0
    /* Set padtmp to pad_1 value */
62
0
    memset(padtmp, 0x36, sizeof(padtmp));
63
0
64
0
    if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
65
0
        return 0;
66
0
67
0
    if (!SHA1_Final(sha1tmp, sha1))
68
0
        return 0;
69
0
70
0
    /* Reinitialise context */
71
0
72
0
    if (!SHA1_Init(sha1))
73
0
        return 0;
74
0
75
0
    if (SHA1_Update(sha1, ms, mslen) <= 0)
76
0
        return 0;
77
0
78
0
    /* Set padtmp to pad_2 value */
79
0
    memset(padtmp, 0x5c, sizeof(padtmp));
80
0
81
0
    if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
82
0
        return 0;
83
0
84
0
    if (!SHA1_Update(sha1, sha1tmp, sizeof(sha1tmp)))
85
0
        return 0;
86
0
87
0
    /* Now when ctx is finalised it will return the SSL v3 hash value */
88
0
    OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
89
0
90
0
    return 1;
91
0
92
0
}
93
94
static const EVP_MD sha1_md = {
95
    NID_sha1,
96
    NID_sha1WithRSAEncryption,
97
    SHA_DIGEST_LENGTH,
98
    EVP_MD_FLAG_DIGALGID_ABSENT,
99
    init,
100
    update,
101
    final,
102
    NULL,
103
    NULL,
104
    SHA_CBLOCK,
105
    sizeof(EVP_MD *) + sizeof(SHA_CTX),
106
    ctrl
107
};
108
109
const EVP_MD *EVP_sha1(void)
110
0
{
111
0
    return &sha1_md;
112
0
}
113
114
static int init224(EVP_MD_CTX *ctx)
115
0
{
116
0
    return SHA224_Init(EVP_MD_CTX_md_data(ctx));
117
0
}
118
119
static int update224(EVP_MD_CTX *ctx, const void *data, size_t count)
120
0
{
121
0
    return SHA224_Update(EVP_MD_CTX_md_data(ctx), data, count);
122
0
}
123
124
static int final224(EVP_MD_CTX *ctx, unsigned char *md)
125
0
{
126
0
    return SHA224_Final(md, EVP_MD_CTX_md_data(ctx));
127
0
}
128
129
static int init256(EVP_MD_CTX *ctx)
130
0
{
131
0
    return SHA256_Init(EVP_MD_CTX_md_data(ctx));
132
0
}
133
134
static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
135
0
{
136
0
    return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
137
0
}
138
139
static int final256(EVP_MD_CTX *ctx, unsigned char *md)
140
0
{
141
0
    return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
142
0
}
143
144
static const EVP_MD sha224_md = {
145
    NID_sha224,
146
    NID_sha224WithRSAEncryption,
147
    SHA224_DIGEST_LENGTH,
148
    EVP_MD_FLAG_DIGALGID_ABSENT,
149
    init224,
150
    update224,
151
    final224,
152
    NULL,
153
    NULL,
154
    SHA256_CBLOCK,
155
    sizeof(EVP_MD *) + sizeof(SHA256_CTX),
156
};
157
158
const EVP_MD *EVP_sha224(void)
159
0
{
160
0
    return &sha224_md;
161
0
}
162
163
static const EVP_MD sha256_md = {
164
    NID_sha256,
165
    NID_sha256WithRSAEncryption,
166
    SHA256_DIGEST_LENGTH,
167
    EVP_MD_FLAG_DIGALGID_ABSENT,
168
    init256,
169
    update256,
170
    final256,
171
    NULL,
172
    NULL,
173
    SHA256_CBLOCK,
174
    sizeof(EVP_MD *) + sizeof(SHA256_CTX),
175
};
176
177
const EVP_MD *EVP_sha256(void)
178
0
{
179
0
    return &sha256_md;
180
0
}
181
182
static int init512_224(EVP_MD_CTX *ctx)
183
0
{
184
0
    return sha512_224_init(EVP_MD_CTX_md_data(ctx));
185
0
}
186
187
static int init512_256(EVP_MD_CTX *ctx)
188
0
{
189
0
    return sha512_256_init(EVP_MD_CTX_md_data(ctx));
190
0
}
191
192
static int init384(EVP_MD_CTX *ctx)
193
0
{
194
0
    return SHA384_Init(EVP_MD_CTX_md_data(ctx));
195
0
}
196
197
static int update384(EVP_MD_CTX *ctx, const void *data, size_t count)
198
0
{
199
0
    return SHA384_Update(EVP_MD_CTX_md_data(ctx), data, count);
200
0
}
201
202
static int final384(EVP_MD_CTX *ctx, unsigned char *md)
203
0
{
204
0
    return SHA384_Final(md, EVP_MD_CTX_md_data(ctx));
205
0
}
206
207
static int init512(EVP_MD_CTX *ctx)
208
0
{
209
0
    return SHA512_Init(EVP_MD_CTX_md_data(ctx));
210
0
}
211
212
/* See comment in SHA224/256 section */
213
static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
214
0
{
215
0
    return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
216
0
}
217
218
static int final512(EVP_MD_CTX *ctx, unsigned char *md)
219
0
{
220
0
    return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
221
0
}
222
223
static const EVP_MD sha512_224_md = {
224
    NID_sha512_224,
225
    NID_sha512_224WithRSAEncryption,
226
    SHA224_DIGEST_LENGTH,
227
    EVP_MD_FLAG_DIGALGID_ABSENT,
228
    init512_224,
229
    update512,
230
    final512,
231
    NULL,
232
    NULL,
233
    SHA512_CBLOCK,
234
    sizeof(EVP_MD *) + sizeof(SHA512_CTX),
235
};
236
237
const EVP_MD *EVP_sha512_224(void)
238
0
{
239
0
    return &sha512_224_md;
240
0
}
241
242
static const EVP_MD sha512_256_md = {
243
    NID_sha512_256,
244
    NID_sha512_256WithRSAEncryption,
245
    SHA256_DIGEST_LENGTH,
246
    EVP_MD_FLAG_DIGALGID_ABSENT,
247
    init512_256,
248
    update512,
249
    final512,
250
    NULL,
251
    NULL,
252
    SHA512_CBLOCK,
253
    sizeof(EVP_MD *) + sizeof(SHA512_CTX),
254
};
255
256
const EVP_MD *EVP_sha512_256(void)
257
0
{
258
0
    return &sha512_256_md;
259
0
}
260
261
static const EVP_MD sha384_md = {
262
    NID_sha384,
263
    NID_sha384WithRSAEncryption,
264
    SHA384_DIGEST_LENGTH,
265
    EVP_MD_FLAG_DIGALGID_ABSENT,
266
    init384,
267
    update384,
268
    final384,
269
    NULL,
270
    NULL,
271
    SHA512_CBLOCK,
272
    sizeof(EVP_MD *) + sizeof(SHA512_CTX),
273
};
274
275
const EVP_MD *EVP_sha384(void)
276
0
{
277
0
    return &sha384_md;
278
0
}
279
280
static const EVP_MD sha512_md = {
281
    NID_sha512,
282
    NID_sha512WithRSAEncryption,
283
    SHA512_DIGEST_LENGTH,
284
    EVP_MD_FLAG_DIGALGID_ABSENT,
285
    init512,
286
    update512,
287
    final512,
288
    NULL,
289
    NULL,
290
    SHA512_CBLOCK,
291
    sizeof(EVP_MD *) + sizeof(SHA512_CTX),
292
};
293
294
const EVP_MD *EVP_sha512(void)
295
0
{
296
0
    return &sha512_md;
297
0
}