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];
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 init256(EVP_MD_CTX *ctx)
120 return SHA256_Init(EVP_MD_CTX_md_data(ctx));
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:-)
128 static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
130 return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
133 static int final256(EVP_MD_CTX *ctx, unsigned char *md)
135 return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
138 static const EVP_MD sha224_md = {
140 NID_sha224WithRSAEncryption,
141 SHA224_DIGEST_LENGTH,
142 EVP_MD_FLAG_DIGALGID_ABSENT,
149 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
152 const EVP_MD *EVP_sha224(void)
157 static const EVP_MD sha256_md = {
159 NID_sha256WithRSAEncryption,
160 SHA256_DIGEST_LENGTH,
161 EVP_MD_FLAG_DIGALGID_ABSENT,
168 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
171 const EVP_MD *EVP_sha256(void)
176 static int init384(EVP_MD_CTX *ctx)
178 return SHA384_Init(EVP_MD_CTX_md_data(ctx));
181 static int init512(EVP_MD_CTX *ctx)
183 return SHA512_Init(EVP_MD_CTX_md_data(ctx));
186 /* See comment in SHA224/256 section */
187 static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
189 return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
192 static int final512(EVP_MD_CTX *ctx, unsigned char *md)
194 return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
197 static const EVP_MD sha384_md = {
199 NID_sha384WithRSAEncryption,
200 SHA384_DIGEST_LENGTH,
201 EVP_MD_FLAG_DIGALGID_ABSENT,
208 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
211 const EVP_MD *EVP_sha384(void)
216 static const EVP_MD sha512_md = {
218 NID_sha512WithRSAEncryption,
219 SHA512_DIGEST_LENGTH,
220 EVP_MD_FLAG_DIGALGID_ABSENT,
227 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
230 const EVP_MD *EVP_sha512(void)