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"
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 init256(EVP_MD_CTX *ctx)
121 return SHA256_Init(EVP_MD_CTX_md_data(ctx));
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:-)
129 static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
131 return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
134 static int final256(EVP_MD_CTX *ctx, unsigned char *md)
136 return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
139 static const EVP_MD sha224_md = {
141 NID_sha224WithRSAEncryption,
142 SHA224_DIGEST_LENGTH,
143 EVP_MD_FLAG_DIGALGID_ABSENT,
150 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
153 const EVP_MD *EVP_sha224(void)
158 static const EVP_MD sha256_md = {
160 NID_sha256WithRSAEncryption,
161 SHA256_DIGEST_LENGTH,
162 EVP_MD_FLAG_DIGALGID_ABSENT,
169 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
172 const EVP_MD *EVP_sha256(void)
177 static int init512_224(EVP_MD_CTX *ctx)
179 return sha512_224_init(EVP_MD_CTX_md_data(ctx));
182 static int init512_256(EVP_MD_CTX *ctx)
184 return sha512_256_init(EVP_MD_CTX_md_data(ctx));
187 static int init384(EVP_MD_CTX *ctx)
189 return SHA384_Init(EVP_MD_CTX_md_data(ctx));
192 static int init512(EVP_MD_CTX *ctx)
194 return SHA512_Init(EVP_MD_CTX_md_data(ctx));
197 /* See comment in SHA224/256 section */
198 static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
200 return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
203 static int final512(EVP_MD_CTX *ctx, unsigned char *md)
205 return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
208 static const EVP_MD sha512_224_md = {
210 NID_sha512_224WithRSAEncryption,
211 SHA224_DIGEST_LENGTH,
212 EVP_MD_FLAG_DIGALGID_ABSENT,
219 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
222 const EVP_MD *EVP_sha512_224(void)
224 return &sha512_224_md;
227 static const EVP_MD sha512_256_md = {
229 NID_sha512_256WithRSAEncryption,
230 SHA256_DIGEST_LENGTH,
231 EVP_MD_FLAG_DIGALGID_ABSENT,
238 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
241 const EVP_MD *EVP_sha512_256(void)
243 return &sha512_256_md;
246 static const EVP_MD sha384_md = {
248 NID_sha384WithRSAEncryption,
249 SHA384_DIGEST_LENGTH,
250 EVP_MD_FLAG_DIGALGID_ABSENT,
257 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
260 const EVP_MD *EVP_sha384(void)
265 static const EVP_MD sha512_md = {
267 NID_sha512WithRSAEncryption,
268 SHA512_DIGEST_LENGTH,
269 EVP_MD_FLAG_DIGALGID_ABSENT,
276 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
279 const EVP_MD *EVP_sha512(void)