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;
|
---|
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 init256(EVP_MD_CTX *ctx)
|
---|
119 | {
|
---|
120 | return SHA256_Init(EVP_MD_CTX_md_data(ctx));
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Even though there're separate SHA224_[Update|Final], we call
|
---|
125 | * SHA256 functions even in SHA224 context. This is what happens
|
---|
126 | * there anyway, so we can spare few CPU cycles:-)
|
---|
127 | */
|
---|
128 | static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
|
---|
129 | {
|
---|
130 | return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
|
---|
131 | }
|
---|
132 |
|
---|
133 | static int final256(EVP_MD_CTX *ctx, unsigned char *md)
|
---|
134 | {
|
---|
135 | return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
|
---|
136 | }
|
---|
137 |
|
---|
138 | static const EVP_MD sha224_md = {
|
---|
139 | NID_sha224,
|
---|
140 | NID_sha224WithRSAEncryption,
|
---|
141 | SHA224_DIGEST_LENGTH,
|
---|
142 | EVP_MD_FLAG_DIGALGID_ABSENT,
|
---|
143 | init224,
|
---|
144 | update256,
|
---|
145 | final256,
|
---|
146 | NULL,
|
---|
147 | NULL,
|
---|
148 | SHA256_CBLOCK,
|
---|
149 | sizeof(EVP_MD *) + sizeof(SHA256_CTX),
|
---|
150 | };
|
---|
151 |
|
---|
152 | const EVP_MD *EVP_sha224(void)
|
---|
153 | {
|
---|
154 | return (&sha224_md);
|
---|
155 | }
|
---|
156 |
|
---|
157 | static const EVP_MD sha256_md = {
|
---|
158 | NID_sha256,
|
---|
159 | NID_sha256WithRSAEncryption,
|
---|
160 | SHA256_DIGEST_LENGTH,
|
---|
161 | EVP_MD_FLAG_DIGALGID_ABSENT,
|
---|
162 | init256,
|
---|
163 | update256,
|
---|
164 | final256,
|
---|
165 | NULL,
|
---|
166 | NULL,
|
---|
167 | SHA256_CBLOCK,
|
---|
168 | sizeof(EVP_MD *) + sizeof(SHA256_CTX),
|
---|
169 | };
|
---|
170 |
|
---|
171 | const EVP_MD *EVP_sha256(void)
|
---|
172 | {
|
---|
173 | return (&sha256_md);
|
---|
174 | }
|
---|
175 |
|
---|
176 | static int init384(EVP_MD_CTX *ctx)
|
---|
177 | {
|
---|
178 | return SHA384_Init(EVP_MD_CTX_md_data(ctx));
|
---|
179 | }
|
---|
180 |
|
---|
181 | static int init512(EVP_MD_CTX *ctx)
|
---|
182 | {
|
---|
183 | return SHA512_Init(EVP_MD_CTX_md_data(ctx));
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* See comment in SHA224/256 section */
|
---|
187 | static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
|
---|
188 | {
|
---|
189 | return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
|
---|
190 | }
|
---|
191 |
|
---|
192 | static int final512(EVP_MD_CTX *ctx, unsigned char *md)
|
---|
193 | {
|
---|
194 | return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
|
---|
195 | }
|
---|
196 |
|
---|
197 | static const EVP_MD sha384_md = {
|
---|
198 | NID_sha384,
|
---|
199 | NID_sha384WithRSAEncryption,
|
---|
200 | SHA384_DIGEST_LENGTH,
|
---|
201 | EVP_MD_FLAG_DIGALGID_ABSENT,
|
---|
202 | init384,
|
---|
203 | update512,
|
---|
204 | final512,
|
---|
205 | NULL,
|
---|
206 | NULL,
|
---|
207 | SHA512_CBLOCK,
|
---|
208 | sizeof(EVP_MD *) + sizeof(SHA512_CTX),
|
---|
209 | };
|
---|
210 |
|
---|
211 | const EVP_MD *EVP_sha384(void)
|
---|
212 | {
|
---|
213 | return (&sha384_md);
|
---|
214 | }
|
---|
215 |
|
---|
216 | static const EVP_MD sha512_md = {
|
---|
217 | NID_sha512,
|
---|
218 | NID_sha512WithRSAEncryption,
|
---|
219 | SHA512_DIGEST_LENGTH,
|
---|
220 | EVP_MD_FLAG_DIGALGID_ABSENT,
|
---|
221 | init512,
|
---|
222 | update512,
|
---|
223 | final512,
|
---|
224 | NULL,
|
---|
225 | NULL,
|
---|
226 | SHA512_CBLOCK,
|
---|
227 | sizeof(EVP_MD *) + sizeof(SHA512_CTX),
|
---|
228 | };
|
---|
229 |
|
---|
230 | const EVP_MD *EVP_sha512(void)
|
---|
231 | {
|
---|
232 | return (&sha512_md);
|
---|
233 | }
|
---|