2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include "internal/cryptlib.h"
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"
20 static int init(EVP_MD_CTX
*ctx
)
22 return SHA1_Init(EVP_MD_CTX_md_data(ctx
));
25 static int update(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
27 return SHA1_Update(EVP_MD_CTX_md_data(ctx
), data
, count
);
30 static int final(EVP_MD_CTX
*ctx
, unsigned char *md
)
32 return SHA1_Final(md
, EVP_MD_CTX_md_data(ctx
));
35 static int ctrl(EVP_MD_CTX
*ctx
, int cmd
, int mslen
, void *ms
)
37 unsigned char padtmp
[40];
38 unsigned char sha1tmp
[SHA_DIGEST_LENGTH
];
42 if (cmd
!= EVP_CTRL_SSL3_MASTER_SECRET
)
48 sha1
= EVP_MD_CTX_md_data(ctx
);
50 /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
54 /* At this point hash contains all handshake messages, update
55 * with master secret and pad_1.
58 if (SHA1_Update(sha1
, ms
, mslen
) <= 0)
61 /* Set padtmp to pad_1 value */
62 memset(padtmp
, 0x36, sizeof(padtmp
));
64 if (!SHA1_Update(sha1
, padtmp
, sizeof(padtmp
)))
67 if (!SHA1_Final(sha1tmp
, sha1
))
70 /* Reinitialise context */
75 if (SHA1_Update(sha1
, ms
, mslen
) <= 0)
78 /* Set padtmp to pad_2 value */
79 memset(padtmp
, 0x5c, sizeof(padtmp
));
81 if (!SHA1_Update(sha1
, padtmp
, sizeof(padtmp
)))
84 if (!SHA1_Update(sha1
, sha1tmp
, sizeof(sha1tmp
)))
87 /* Now when ctx is finalised it will return the SSL v3 hash value */
88 OPENSSL_cleanse(sha1tmp
, sizeof(sha1tmp
));
94 static const EVP_MD sha1_md
= {
96 NID_sha1WithRSAEncryption
,
98 EVP_MD_FLAG_DIGALGID_ABSENT
,
105 sizeof(EVP_MD
*) + sizeof(SHA_CTX
),
109 const EVP_MD
*EVP_sha1(void)
114 static int init224(EVP_MD_CTX
*ctx
)
116 return SHA224_Init(EVP_MD_CTX_md_data(ctx
));
119 static int update224(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
121 return SHA224_Update(EVP_MD_CTX_md_data(ctx
), data
, count
);
124 static int final224(EVP_MD_CTX
*ctx
, unsigned char *md
)
126 return SHA224_Final(md
, EVP_MD_CTX_md_data(ctx
));
129 static int init256(EVP_MD_CTX
*ctx
)
131 return SHA256_Init(EVP_MD_CTX_md_data(ctx
));
134 static int update256(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
136 return SHA256_Update(EVP_MD_CTX_md_data(ctx
), data
, count
);
139 static int final256(EVP_MD_CTX
*ctx
, unsigned char *md
)
141 return SHA256_Final(md
, EVP_MD_CTX_md_data(ctx
));
144 static const EVP_MD sha224_md
= {
146 NID_sha224WithRSAEncryption
,
147 SHA224_DIGEST_LENGTH
,
148 EVP_MD_FLAG_DIGALGID_ABSENT
,
155 sizeof(EVP_MD
*) + sizeof(SHA256_CTX
),
158 const EVP_MD
*EVP_sha224(void)
163 static const EVP_MD sha256_md
= {
165 NID_sha256WithRSAEncryption
,
166 SHA256_DIGEST_LENGTH
,
167 EVP_MD_FLAG_DIGALGID_ABSENT
,
174 sizeof(EVP_MD
*) + sizeof(SHA256_CTX
),
177 const EVP_MD
*EVP_sha256(void)
182 static int init512_224(EVP_MD_CTX
*ctx
)
184 return sha512_224_init(EVP_MD_CTX_md_data(ctx
));
187 static int init512_256(EVP_MD_CTX
*ctx
)
189 return sha512_256_init(EVP_MD_CTX_md_data(ctx
));
192 static int init384(EVP_MD_CTX
*ctx
)
194 return SHA384_Init(EVP_MD_CTX_md_data(ctx
));
197 static int update384(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
199 return SHA384_Update(EVP_MD_CTX_md_data(ctx
), data
, count
);
202 static int final384(EVP_MD_CTX
*ctx
, unsigned char *md
)
204 return SHA384_Final(md
, EVP_MD_CTX_md_data(ctx
));
207 static int init512(EVP_MD_CTX
*ctx
)
209 return SHA512_Init(EVP_MD_CTX_md_data(ctx
));
212 /* See comment in SHA224/256 section */
213 static int update512(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
215 return SHA512_Update(EVP_MD_CTX_md_data(ctx
), data
, count
);
218 static int final512(EVP_MD_CTX
*ctx
, unsigned char *md
)
220 return SHA512_Final(md
, EVP_MD_CTX_md_data(ctx
));
223 static const EVP_MD sha512_224_md
= {
225 NID_sha512_224WithRSAEncryption
,
226 SHA224_DIGEST_LENGTH
,
227 EVP_MD_FLAG_DIGALGID_ABSENT
,
234 sizeof(EVP_MD
*) + sizeof(SHA512_CTX
),
237 const EVP_MD
*EVP_sha512_224(void)
239 return &sha512_224_md
;
242 static const EVP_MD sha512_256_md
= {
244 NID_sha512_256WithRSAEncryption
,
245 SHA256_DIGEST_LENGTH
,
246 EVP_MD_FLAG_DIGALGID_ABSENT
,
253 sizeof(EVP_MD
*) + sizeof(SHA512_CTX
),
256 const EVP_MD
*EVP_sha512_256(void)
258 return &sha512_256_md
;
261 static const EVP_MD sha384_md
= {
263 NID_sha384WithRSAEncryption
,
264 SHA384_DIGEST_LENGTH
,
265 EVP_MD_FLAG_DIGALGID_ABSENT
,
272 sizeof(EVP_MD
*) + sizeof(SHA512_CTX
),
275 const EVP_MD
*EVP_sha384(void)
280 static const EVP_MD sha512_md
= {
282 NID_sha512WithRSAEncryption
,
283 SHA512_DIGEST_LENGTH
,
284 EVP_MD_FLAG_DIGALGID_ABSENT
,
291 sizeof(EVP_MD
*) + sizeof(SHA512_CTX
),
294 const EVP_MD
*EVP_sha512(void)