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 p1, void *p2)
37 return sha1_ctrl(ctx != NULL ? EVP_MD_CTX_md_data(ctx) : NULL, cmd, p1, p2);
40 static const EVP_MD sha1_md = {
42 NID_sha1WithRSAEncryption,
44 EVP_MD_FLAG_DIGALGID_ABSENT,
51 sizeof(EVP_MD *) + sizeof(SHA_CTX),
55 const EVP_MD *EVP_sha1(void)
60 static int init224(EVP_MD_CTX *ctx)
62 return SHA224_Init(EVP_MD_CTX_md_data(ctx));
65 static int update224(EVP_MD_CTX *ctx, const void *data, size_t count)
67 return SHA224_Update(EVP_MD_CTX_md_data(ctx), data, count);
70 static int final224(EVP_MD_CTX *ctx, unsigned char *md)
72 return SHA224_Final(md, EVP_MD_CTX_md_data(ctx));
75 static int init256(EVP_MD_CTX *ctx)
77 return SHA256_Init(EVP_MD_CTX_md_data(ctx));
80 static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
82 return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
85 static int final256(EVP_MD_CTX *ctx, unsigned char *md)
87 return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
90 static const EVP_MD sha224_md = {
92 NID_sha224WithRSAEncryption,
94 EVP_MD_FLAG_DIGALGID_ABSENT,
101 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
104 const EVP_MD *EVP_sha224(void)
109 static const EVP_MD sha256_md = {
111 NID_sha256WithRSAEncryption,
112 SHA256_DIGEST_LENGTH,
113 EVP_MD_FLAG_DIGALGID_ABSENT,
120 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
123 const EVP_MD *EVP_sha256(void)
128 static int init512_224(EVP_MD_CTX *ctx)
130 return sha512_224_init(EVP_MD_CTX_md_data(ctx));
133 static int init512_256(EVP_MD_CTX *ctx)
135 return sha512_256_init(EVP_MD_CTX_md_data(ctx));
138 static int init384(EVP_MD_CTX *ctx)
140 return SHA384_Init(EVP_MD_CTX_md_data(ctx));
143 static int update384(EVP_MD_CTX *ctx, const void *data, size_t count)
145 return SHA384_Update(EVP_MD_CTX_md_data(ctx), data, count);
148 static int final384(EVP_MD_CTX *ctx, unsigned char *md)
150 return SHA384_Final(md, EVP_MD_CTX_md_data(ctx));
153 static int init512(EVP_MD_CTX *ctx)
155 return SHA512_Init(EVP_MD_CTX_md_data(ctx));
158 /* See comment in SHA224/256 section */
159 static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
161 return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
164 static int final512(EVP_MD_CTX *ctx, unsigned char *md)
166 return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
169 static const EVP_MD sha512_224_md = {
171 NID_sha512_224WithRSAEncryption,
172 SHA224_DIGEST_LENGTH,
173 EVP_MD_FLAG_DIGALGID_ABSENT,
180 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
183 const EVP_MD *EVP_sha512_224(void)
185 return &sha512_224_md;
188 static const EVP_MD sha512_256_md = {
190 NID_sha512_256WithRSAEncryption,
191 SHA256_DIGEST_LENGTH,
192 EVP_MD_FLAG_DIGALGID_ABSENT,
199 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
202 const EVP_MD *EVP_sha512_256(void)
204 return &sha512_256_md;
207 static const EVP_MD sha384_md = {
209 NID_sha384WithRSAEncryption,
210 SHA384_DIGEST_LENGTH,
211 EVP_MD_FLAG_DIGALGID_ABSENT,
218 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
221 const EVP_MD *EVP_sha384(void)
226 static const EVP_MD sha512_md = {
228 NID_sha512WithRSAEncryption,
229 SHA512_DIGEST_LENGTH,
230 EVP_MD_FLAG_DIGALGID_ABSENT,
237 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
240 const EVP_MD *EVP_sha512(void)