2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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"
19 static int init(EVP_MD_CTX *ctx)
21 return SHA1_Init(EVP_MD_CTX_md_data(ctx));
24 static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
26 return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count);
29 static int final(EVP_MD_CTX *ctx, unsigned char *md)
31 return SHA1_Final(md, EVP_MD_CTX_md_data(ctx));
34 static int ctrl(EVP_MD_CTX *ctx, int cmd, int mslen, void *ms)
36 unsigned char padtmp[40];
37 unsigned char sha1tmp[SHA_DIGEST_LENGTH];
41 if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
47 sha1 = EVP_MD_CTX_md_data(ctx);
49 /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
53 /* At this point hash contains all handshake messages, update
54 * with master secret and pad_1.
57 if (SHA1_Update(sha1, ms, mslen) <= 0)
60 /* Set padtmp to pad_1 value */
61 memset(padtmp, 0x36, sizeof(padtmp));
63 if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
66 if (!SHA1_Final(sha1tmp, sha1))
69 /* Reinitialise context */
74 if (SHA1_Update(sha1, ms, mslen) <= 0)
77 /* Set padtmp to pad_2 value */
78 memset(padtmp, 0x5c, sizeof(padtmp));
80 if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
83 if (!SHA1_Update(sha1, sha1tmp, sizeof(sha1tmp)))
86 /* Now when ctx is finalised it will return the SSL v3 hash value */
87 OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
93 static const EVP_MD sha1_md = {
95 NID_sha1WithRSAEncryption,
97 EVP_MD_FLAG_DIGALGID_ABSENT,
104 sizeof(EVP_MD *) + sizeof(SHA_CTX),
108 const EVP_MD *EVP_sha1(void)
113 static int init224(EVP_MD_CTX *ctx)
115 return SHA224_Init(EVP_MD_CTX_md_data(ctx));
118 static int update224(EVP_MD_CTX *ctx, const void *data, size_t count)
120 return SHA224_Update(EVP_MD_CTX_md_data(ctx), data, count);
123 static int final224(EVP_MD_CTX *ctx, unsigned char *md)
125 return SHA224_Final(md, EVP_MD_CTX_md_data(ctx));
128 static int init256(EVP_MD_CTX *ctx)
130 return SHA256_Init(EVP_MD_CTX_md_data(ctx));
133 static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
135 return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
138 static int final256(EVP_MD_CTX *ctx, unsigned char *md)
140 return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
143 static const EVP_MD sha224_md = {
145 NID_sha224WithRSAEncryption,
146 SHA224_DIGEST_LENGTH,
147 EVP_MD_FLAG_DIGALGID_ABSENT,
154 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
157 const EVP_MD *EVP_sha224(void)
162 static const EVP_MD sha256_md = {
164 NID_sha256WithRSAEncryption,
165 SHA256_DIGEST_LENGTH,
166 EVP_MD_FLAG_DIGALGID_ABSENT,
173 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
176 const EVP_MD *EVP_sha256(void)
181 static int init384(EVP_MD_CTX *ctx)
183 return SHA384_Init(EVP_MD_CTX_md_data(ctx));
186 static int update384(EVP_MD_CTX *ctx, const void *data, size_t count)
188 return SHA384_Update(EVP_MD_CTX_md_data(ctx), data, count);
191 static int final384(EVP_MD_CTX *ctx, unsigned char *md)
193 return SHA384_Final(md, EVP_MD_CTX_md_data(ctx));
196 static int init512(EVP_MD_CTX *ctx)
198 return SHA512_Init(EVP_MD_CTX_md_data(ctx));
201 /* See comment in SHA224/256 section */
202 static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
204 return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
207 static int final512(EVP_MD_CTX *ctx, unsigned char *md)
209 return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
212 static const EVP_MD sha384_md = {
214 NID_sha384WithRSAEncryption,
215 SHA384_DIGEST_LENGTH,
216 EVP_MD_FLAG_DIGALGID_ABSENT,
223 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
226 const EVP_MD *EVP_sha384(void)
231 static const EVP_MD sha512_md = {
233 NID_sha512WithRSAEncryption,
234 SHA512_DIGEST_LENGTH,
235 EVP_MD_FLAG_DIGALGID_ABSENT,
242 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
245 const EVP_MD *EVP_sha512(void)