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