s390x assembly pack: accelerate X25519, X448, Ed25519 and Ed448
[openssl.git] / crypto / evp / m_sha1.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12
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"
19
20 static int init(EVP_MD_CTX *ctx)
21 {
22     return SHA1_Init(EVP_MD_CTX_md_data(ctx));
23 }
24
25 static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
26 {
27     return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count);
28 }
29
30 static int final(EVP_MD_CTX *ctx, unsigned char *md)
31 {
32     return SHA1_Final(md, EVP_MD_CTX_md_data(ctx));
33 }
34
35 static int ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
36 {
37     return sha1_ctrl(ctx != NULL ? EVP_MD_CTX_md_data(ctx) : NULL, cmd, p1, p2);
38 }
39
40 static const EVP_MD sha1_md = {
41     NID_sha1,
42     NID_sha1WithRSAEncryption,
43     SHA_DIGEST_LENGTH,
44     EVP_MD_FLAG_DIGALGID_ABSENT,
45     init,
46     update,
47     final,
48     NULL,
49     NULL,
50     SHA_CBLOCK,
51     sizeof(EVP_MD *) + sizeof(SHA_CTX),
52     ctrl
53 };
54
55 const EVP_MD *EVP_sha1(void)
56 {
57     return &sha1_md;
58 }
59
60 static int init224(EVP_MD_CTX *ctx)
61 {
62     return SHA224_Init(EVP_MD_CTX_md_data(ctx));
63 }
64
65 static int update224(EVP_MD_CTX *ctx, const void *data, size_t count)
66 {
67     return SHA224_Update(EVP_MD_CTX_md_data(ctx), data, count);
68 }
69
70 static int final224(EVP_MD_CTX *ctx, unsigned char *md)
71 {
72     return SHA224_Final(md, EVP_MD_CTX_md_data(ctx));
73 }
74
75 static int init256(EVP_MD_CTX *ctx)
76 {
77     return SHA256_Init(EVP_MD_CTX_md_data(ctx));
78 }
79
80 static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
81 {
82     return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
83 }
84
85 static int final256(EVP_MD_CTX *ctx, unsigned char *md)
86 {
87     return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
88 }
89
90 static const EVP_MD sha224_md = {
91     NID_sha224,
92     NID_sha224WithRSAEncryption,
93     SHA224_DIGEST_LENGTH,
94     EVP_MD_FLAG_DIGALGID_ABSENT,
95     init224,
96     update224,
97     final224,
98     NULL,
99     NULL,
100     SHA256_CBLOCK,
101     sizeof(EVP_MD *) + sizeof(SHA256_CTX),
102 };
103
104 const EVP_MD *EVP_sha224(void)
105 {
106     return &sha224_md;
107 }
108
109 static const EVP_MD sha256_md = {
110     NID_sha256,
111     NID_sha256WithRSAEncryption,
112     SHA256_DIGEST_LENGTH,
113     EVP_MD_FLAG_DIGALGID_ABSENT,
114     init256,
115     update256,
116     final256,
117     NULL,
118     NULL,
119     SHA256_CBLOCK,
120     sizeof(EVP_MD *) + sizeof(SHA256_CTX),
121 };
122
123 const EVP_MD *EVP_sha256(void)
124 {
125     return &sha256_md;
126 }
127
128 static int init512_224(EVP_MD_CTX *ctx)
129 {
130     return sha512_224_init(EVP_MD_CTX_md_data(ctx));
131 }
132
133 static int init512_256(EVP_MD_CTX *ctx)
134 {
135     return sha512_256_init(EVP_MD_CTX_md_data(ctx));
136 }
137
138 static int init384(EVP_MD_CTX *ctx)
139 {
140     return SHA384_Init(EVP_MD_CTX_md_data(ctx));
141 }
142
143 static int update384(EVP_MD_CTX *ctx, const void *data, size_t count)
144 {
145     return SHA384_Update(EVP_MD_CTX_md_data(ctx), data, count);
146 }
147
148 static int final384(EVP_MD_CTX *ctx, unsigned char *md)
149 {
150     return SHA384_Final(md, EVP_MD_CTX_md_data(ctx));
151 }
152
153 static int init512(EVP_MD_CTX *ctx)
154 {
155     return SHA512_Init(EVP_MD_CTX_md_data(ctx));
156 }
157
158 /* See comment in SHA224/256 section */
159 static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
160 {
161     return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
162 }
163
164 static int final512(EVP_MD_CTX *ctx, unsigned char *md)
165 {
166     return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
167 }
168
169 static const EVP_MD sha512_224_md = {
170     NID_sha512_224,
171     NID_sha512_224WithRSAEncryption,
172     SHA224_DIGEST_LENGTH,
173     EVP_MD_FLAG_DIGALGID_ABSENT,
174     init512_224,
175     update512,
176     final512,
177     NULL,
178     NULL,
179     SHA512_CBLOCK,
180     sizeof(EVP_MD *) + sizeof(SHA512_CTX),
181 };
182
183 const EVP_MD *EVP_sha512_224(void)
184 {
185     return &sha512_224_md;
186 }
187
188 static const EVP_MD sha512_256_md = {
189     NID_sha512_256,
190     NID_sha512_256WithRSAEncryption,
191     SHA256_DIGEST_LENGTH,
192     EVP_MD_FLAG_DIGALGID_ABSENT,
193     init512_256,
194     update512,
195     final512,
196     NULL,
197     NULL,
198     SHA512_CBLOCK,
199     sizeof(EVP_MD *) + sizeof(SHA512_CTX),
200 };
201
202 const EVP_MD *EVP_sha512_256(void)
203 {
204     return &sha512_256_md;
205 }
206
207 static const EVP_MD sha384_md = {
208     NID_sha384,
209     NID_sha384WithRSAEncryption,
210     SHA384_DIGEST_LENGTH,
211     EVP_MD_FLAG_DIGALGID_ABSENT,
212     init384,
213     update384,
214     final384,
215     NULL,
216     NULL,
217     SHA512_CBLOCK,
218     sizeof(EVP_MD *) + sizeof(SHA512_CTX),
219 };
220
221 const EVP_MD *EVP_sha384(void)
222 {
223     return &sha384_md;
224 }
225
226 static const EVP_MD sha512_md = {
227     NID_sha512,
228     NID_sha512WithRSAEncryption,
229     SHA512_DIGEST_LENGTH,
230     EVP_MD_FLAG_DIGALGID_ABSENT,
231     init512,
232     update512,
233     final512,
234     NULL,
235     NULL,
236     SHA512_CBLOCK,
237     sizeof(EVP_MD *) + sizeof(SHA512_CTX),
238 };
239
240 const EVP_MD *EVP_sha512(void)
241 {
242     return &sha512_md;
243 }