/src/boringssl/crypto/fipsmodule/digest/digests.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | | * All rights reserved. |
3 | | * |
4 | | * This package is an SSL implementation written |
5 | | * by Eric Young (eay@cryptsoft.com). |
6 | | * The implementation was written so as to conform with Netscapes SSL. |
7 | | * |
8 | | * This library is free for commercial and non-commercial use as long as |
9 | | * the following conditions are aheared to. The following conditions |
10 | | * apply to all code found in this distribution, be it the RC4, RSA, |
11 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
12 | | * included with this distribution is covered by the same copyright terms |
13 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
14 | | * |
15 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | | * the code are not to be removed. |
17 | | * If this package is used in a product, Eric Young should be given attribution |
18 | | * as the author of the parts of the library used. |
19 | | * This can be in the form of a textual message at program startup or |
20 | | * in documentation (online or textual) provided with the package. |
21 | | * |
22 | | * Redistribution and use in source and binary forms, with or without |
23 | | * modification, are permitted provided that the following conditions |
24 | | * are met: |
25 | | * 1. Redistributions of source code must retain the copyright |
26 | | * notice, this list of conditions and the following disclaimer. |
27 | | * 2. Redistributions in binary form must reproduce the above copyright |
28 | | * notice, this list of conditions and the following disclaimer in the |
29 | | * documentation and/or other materials provided with the distribution. |
30 | | * 3. All advertising materials mentioning features or use of this software |
31 | | * must display the following acknowledgement: |
32 | | * "This product includes cryptographic software written by |
33 | | * Eric Young (eay@cryptsoft.com)" |
34 | | * The word 'cryptographic' can be left out if the rouines from the library |
35 | | * being used are not cryptographic related :-). |
36 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
37 | | * the apps directory (application code) you must include an acknowledgement: |
38 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
44 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
45 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
46 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
48 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
49 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
50 | | * SUCH DAMAGE. |
51 | | * |
52 | | * The licence and distribution terms for any publically available version or |
53 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
54 | | * copied and put under another distribution licence |
55 | | * [including the GNU Public Licence.] */ |
56 | | |
57 | | #include <openssl/digest.h> |
58 | | |
59 | | #include <assert.h> |
60 | | #include <string.h> |
61 | | |
62 | | #include <openssl/md4.h> |
63 | | #include <openssl/md5.h> |
64 | | #include <openssl/nid.h> |
65 | | #include <openssl/sha.h> |
66 | | |
67 | | #include "internal.h" |
68 | | #include "../delocate.h" |
69 | | #include "../../internal.h" |
70 | | |
71 | | #if defined(NDEBUG) |
72 | | #define CHECK(x) (void) (x) |
73 | | #else |
74 | 28.3M | #define CHECK(x) assert(x) |
75 | | #endif |
76 | | |
77 | | |
78 | 25.1k | static void md4_init(EVP_MD_CTX *ctx) { |
79 | 25.1k | CHECK(MD4_Init(ctx->md_data)); |
80 | 25.1k | } |
81 | | |
82 | 30.9k | static void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
83 | 30.9k | CHECK(MD4_Update(ctx->md_data, data, count)); |
84 | 30.9k | } |
85 | | |
86 | 25.1k | static void md4_final(EVP_MD_CTX *ctx, uint8_t *out) { |
87 | 25.1k | CHECK(MD4_Final(out, ctx->md_data)); |
88 | 25.1k | } |
89 | | |
90 | 1 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_md4) { |
91 | 1 | out->type = NID_md4; |
92 | 1 | out->md_size = MD4_DIGEST_LENGTH; |
93 | 1 | out->flags = 0; |
94 | 1 | out->init = md4_init; |
95 | 1 | out->update = md4_update; |
96 | 1 | out->final = md4_final; |
97 | 1 | out->block_size = 64; |
98 | 1 | out->ctx_size = sizeof(MD4_CTX); |
99 | 1 | } |
100 | | |
101 | | |
102 | 76.5k | static void md5_init(EVP_MD_CTX *ctx) { |
103 | 76.5k | CHECK(MD5_Init(ctx->md_data)); |
104 | 76.5k | } |
105 | | |
106 | 838k | static void md5_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
107 | 838k | CHECK(MD5_Update(ctx->md_data, data, count)); |
108 | 838k | } |
109 | | |
110 | 456k | static void md5_final(EVP_MD_CTX *ctx, uint8_t *out) { |
111 | 456k | CHECK(MD5_Final(out, ctx->md_data)); |
112 | 456k | } |
113 | | |
114 | 5 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_md5) { |
115 | 5 | out->type = NID_md5; |
116 | 5 | out->md_size = MD5_DIGEST_LENGTH; |
117 | 5 | out->flags = 0; |
118 | 5 | out->init = md5_init; |
119 | 5 | out->update = md5_update; |
120 | 5 | out->final = md5_final; |
121 | 5 | out->block_size = 64; |
122 | 5 | out->ctx_size = sizeof(MD5_CTX); |
123 | 5 | } |
124 | | |
125 | | |
126 | 5.70M | static void sha1_init(EVP_MD_CTX *ctx) { |
127 | 5.70M | CHECK(SHA1_Init(ctx->md_data)); |
128 | 5.70M | } |
129 | | |
130 | 6.58M | static void sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
131 | 6.58M | CHECK(SHA1_Update(ctx->md_data, data, count)); |
132 | 6.58M | } |
133 | | |
134 | 6.04M | static void sha1_final(EVP_MD_CTX *ctx, uint8_t *md) { |
135 | 6.04M | CHECK(SHA1_Final(md, ctx->md_data)); |
136 | 6.04M | } |
137 | | |
138 | 6 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha1) { |
139 | 6 | out->type = NID_sha1; |
140 | 6 | out->md_size = SHA_DIGEST_LENGTH; |
141 | 6 | out->flags = 0; |
142 | 6 | out->init = sha1_init; |
143 | 6 | out->update = sha1_update; |
144 | 6 | out->final = sha1_final; |
145 | 6 | out->block_size = 64; |
146 | 6 | out->ctx_size = sizeof(SHA_CTX); |
147 | 6 | } |
148 | | |
149 | | |
150 | 5.26k | static void sha224_init(EVP_MD_CTX *ctx) { |
151 | 5.26k | CHECK(SHA224_Init(ctx->md_data)); |
152 | 5.26k | } |
153 | | |
154 | 5.49k | static void sha224_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
155 | 5.49k | CHECK(SHA224_Update(ctx->md_data, data, count)); |
156 | 5.49k | } |
157 | | |
158 | 5.26k | static void sha224_final(EVP_MD_CTX *ctx, uint8_t *md) { |
159 | 5.26k | CHECK(SHA224_Final(md, ctx->md_data)); |
160 | 5.26k | } |
161 | | |
162 | 1 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha224) { |
163 | 1 | out->type = NID_sha224; |
164 | 1 | out->md_size = SHA224_DIGEST_LENGTH; |
165 | 1 | out->flags = 0; |
166 | 1 | out->init = sha224_init; |
167 | 1 | out->update = sha224_update; |
168 | 1 | out->final = sha224_final; |
169 | 1 | out->block_size = 64; |
170 | 1 | out->ctx_size = sizeof(SHA256_CTX); |
171 | 1 | } |
172 | | |
173 | | |
174 | 647k | static void sha256_init(EVP_MD_CTX *ctx) { |
175 | 647k | CHECK(SHA256_Init(ctx->md_data)); |
176 | 647k | } |
177 | | |
178 | 4.45M | static void sha256_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
179 | 4.45M | CHECK(SHA256_Update(ctx->md_data, data, count)); |
180 | 4.45M | } |
181 | | |
182 | 2.00M | static void sha256_final(EVP_MD_CTX *ctx, uint8_t *md) { |
183 | 2.00M | CHECK(SHA256_Final(md, ctx->md_data)); |
184 | 2.00M | } |
185 | | |
186 | 6 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha256) { |
187 | 6 | out->type = NID_sha256; |
188 | 6 | out->md_size = SHA256_DIGEST_LENGTH; |
189 | 6 | out->flags = 0; |
190 | 6 | out->init = sha256_init; |
191 | 6 | out->update = sha256_update; |
192 | 6 | out->final = sha256_final; |
193 | 6 | out->block_size = 64; |
194 | 6 | out->ctx_size = sizeof(SHA256_CTX); |
195 | 6 | } |
196 | | |
197 | | |
198 | 286k | static void sha384_init(EVP_MD_CTX *ctx) { |
199 | 286k | CHECK(SHA384_Init(ctx->md_data)); |
200 | 286k | } |
201 | | |
202 | 607k | static void sha384_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
203 | 607k | CHECK(SHA384_Update(ctx->md_data, data, count)); |
204 | 607k | } |
205 | | |
206 | 303k | static void sha384_final(EVP_MD_CTX *ctx, uint8_t *md) { |
207 | 303k | CHECK(SHA384_Final(md, ctx->md_data)); |
208 | 303k | } |
209 | | |
210 | 5 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha384) { |
211 | 5 | out->type = NID_sha384; |
212 | 5 | out->md_size = SHA384_DIGEST_LENGTH; |
213 | 5 | out->flags = 0; |
214 | 5 | out->init = sha384_init; |
215 | 5 | out->update = sha384_update; |
216 | 5 | out->final = sha384_final; |
217 | 5 | out->block_size = 128; |
218 | 5 | out->ctx_size = sizeof(SHA512_CTX); |
219 | 5 | } |
220 | | |
221 | | |
222 | 14.3k | static void sha512_init(EVP_MD_CTX *ctx) { |
223 | 14.3k | CHECK(SHA512_Init(ctx->md_data)); |
224 | 14.3k | } |
225 | | |
226 | 16.3k | static void sha512_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
227 | 16.3k | CHECK(SHA512_Update(ctx->md_data, data, count)); |
228 | 16.3k | } |
229 | | |
230 | 14.3k | static void sha512_final(EVP_MD_CTX *ctx, uint8_t *md) { |
231 | 14.3k | CHECK(SHA512_Final(md, ctx->md_data)); |
232 | 14.3k | } |
233 | | |
234 | 5 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512) { |
235 | 5 | out->type = NID_sha512; |
236 | 5 | out->md_size = SHA512_DIGEST_LENGTH; |
237 | 5 | out->flags = 0; |
238 | 5 | out->init = sha512_init; |
239 | 5 | out->update = sha512_update; |
240 | 5 | out->final = sha512_final; |
241 | 5 | out->block_size = 128; |
242 | 5 | out->ctx_size = sizeof(SHA512_CTX); |
243 | 5 | } |
244 | | |
245 | | |
246 | 0 | static void sha512_256_init(EVP_MD_CTX *ctx) { |
247 | 0 | CHECK(SHA512_256_Init(ctx->md_data)); |
248 | 0 | } |
249 | | |
250 | 0 | static void sha512_256_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
251 | 0 | CHECK(SHA512_256_Update(ctx->md_data, data, count)); |
252 | 0 | } |
253 | | |
254 | 0 | static void sha512_256_final(EVP_MD_CTX *ctx, uint8_t *md) { |
255 | 0 | CHECK(SHA512_256_Final(md, ctx->md_data)); |
256 | 0 | } |
257 | | |
258 | 0 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512_256) { |
259 | 0 | out->type = NID_sha512_256; |
260 | 0 | out->md_size = SHA512_256_DIGEST_LENGTH; |
261 | 0 | out->flags = 0; |
262 | 0 | out->init = sha512_256_init; |
263 | 0 | out->update = sha512_256_update; |
264 | 0 | out->final = sha512_256_final; |
265 | 0 | out->block_size = 128; |
266 | 0 | out->ctx_size = sizeof(SHA512_CTX); |
267 | 0 | } |
268 | | |
269 | | |
270 | | typedef struct { |
271 | | MD5_CTX md5; |
272 | | SHA_CTX sha1; |
273 | | } MD5_SHA1_CTX; |
274 | | |
275 | 20.9k | static void md5_sha1_init(EVP_MD_CTX *md_ctx) { |
276 | 20.9k | MD5_SHA1_CTX *ctx = md_ctx->md_data; |
277 | 20.9k | CHECK(MD5_Init(&ctx->md5) && SHA1_Init(&ctx->sha1)); |
278 | 20.9k | } |
279 | | |
280 | | static void md5_sha1_update(EVP_MD_CTX *md_ctx, const void *data, |
281 | 99.3k | size_t count) { |
282 | 99.3k | MD5_SHA1_CTX *ctx = md_ctx->md_data; |
283 | 99.3k | CHECK(MD5_Update(&ctx->md5, data, count) && |
284 | 99.3k | SHA1_Update(&ctx->sha1, data, count)); |
285 | 99.3k | } |
286 | | |
287 | 40.3k | static void md5_sha1_final(EVP_MD_CTX *md_ctx, uint8_t *out) { |
288 | 40.3k | MD5_SHA1_CTX *ctx = md_ctx->md_data; |
289 | 40.3k | CHECK(MD5_Final(out, &ctx->md5) && |
290 | 40.3k | SHA1_Final(out + MD5_DIGEST_LENGTH, &ctx->sha1)); |
291 | 40.3k | } |
292 | | |
293 | 4 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_md5_sha1) { |
294 | 4 | out->type = NID_md5_sha1; |
295 | 4 | out->md_size = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH; |
296 | 4 | out->flags = 0; |
297 | 4 | out->init = md5_sha1_init; |
298 | 4 | out->update = md5_sha1_update; |
299 | 4 | out->final = md5_sha1_final; |
300 | 4 | out->block_size = 64; |
301 | 4 | out->ctx_size = sizeof(MD5_SHA1_CTX); |
302 | 4 | } |
303 | | |
304 | | #undef CHECK |