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