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