2 * Copyright 1995-2016 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];
39 SHA_CTX *sha1 = EVP_MD_CTX_md_data(ctx);
41 if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
44 /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
48 /* At this point hash contains all handshake messages, update
49 * with master secret and pad_1.
52 if (SHA1_Update(sha1, ms, mslen) <= 0)
55 /* Set padtmp to pad_1 value */
56 memset(padtmp, 0x36, sizeof(padtmp));
58 if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
61 if (!SHA1_Final(sha1tmp, sha1))
64 /* Reinitialise context */
69 if (SHA1_Update(sha1, ms, mslen) <= 0)
72 /* Set padtmp to pad_2 value */
73 memset(padtmp, 0x5c, sizeof(padtmp));
75 if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
78 if (!SHA1_Update(sha1, sha1tmp, sizeof(sha1tmp)))
81 /* Now when ctx is finalised it will return the SSL v3 hash value */
82 OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
88 static const EVP_MD sha1_md = {
90 NID_sha1WithRSAEncryption,
92 EVP_MD_FLAG_DIGALGID_ABSENT,
99 sizeof(EVP_MD *) + sizeof(SHA_CTX),
103 const EVP_MD *EVP_sha1(void)
108 static int init224(EVP_MD_CTX *ctx)
110 return SHA224_Init(EVP_MD_CTX_md_data(ctx));
113 static int init256(EVP_MD_CTX *ctx)
115 return SHA256_Init(EVP_MD_CTX_md_data(ctx));
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:-)
123 static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
125 return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
128 static int final256(EVP_MD_CTX *ctx, unsigned char *md)
130 return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
133 static const EVP_MD sha224_md = {
135 NID_sha224WithRSAEncryption,
136 SHA224_DIGEST_LENGTH,
137 EVP_MD_FLAG_DIGALGID_ABSENT,
144 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
147 const EVP_MD *EVP_sha224(void)
152 static const EVP_MD sha256_md = {
154 NID_sha256WithRSAEncryption,
155 SHA256_DIGEST_LENGTH,
156 EVP_MD_FLAG_DIGALGID_ABSENT,
163 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
166 const EVP_MD *EVP_sha256(void)
171 static int init384(EVP_MD_CTX *ctx)
173 return SHA384_Init(EVP_MD_CTX_md_data(ctx));
176 static int init512(EVP_MD_CTX *ctx)
178 return SHA512_Init(EVP_MD_CTX_md_data(ctx));
181 /* See comment in SHA224/256 section */
182 static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
184 return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
187 static int final512(EVP_MD_CTX *ctx, unsigned char *md)
189 return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
192 static const EVP_MD sha384_md = {
194 NID_sha384WithRSAEncryption,
195 SHA384_DIGEST_LENGTH,
196 EVP_MD_FLAG_DIGALGID_ABSENT,
203 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
206 const EVP_MD *EVP_sha384(void)
211 static const EVP_MD sha512_md = {
213 NID_sha512WithRSAEncryption,
214 SHA512_DIGEST_LENGTH,
215 EVP_MD_FLAG_DIGALGID_ABSENT,
222 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
225 const EVP_MD *EVP_sha512(void)