[AMSTREAM] Sync with Wine Staging 3.9. CORE-14656
[reactos.git] / dll / 3rdparty / mbedtls / md_wrap.c
1 /**
2 * \file md_wrap.c
3 *
4 * \brief Generic message digest wrapper for mbed TLS
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
9 * SPDX-License-Identifier: GPL-2.0
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 * This file is part of mbed TLS (https://tls.mbed.org)
26 */
27
28 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include "mbedtls/config.h"
30 #else
31 #include MBEDTLS_CONFIG_FILE
32 #endif
33
34 #if defined(MBEDTLS_MD_C)
35
36 #include "mbedtls/md_internal.h"
37
38 #if defined(MBEDTLS_MD2_C)
39 #include "mbedtls/md2.h"
40 #endif
41
42 #if defined(MBEDTLS_MD4_C)
43 #include "mbedtls/md4.h"
44 #endif
45
46 #if defined(MBEDTLS_MD5_C)
47 #include "mbedtls/md5.h"
48 #endif
49
50 #if defined(MBEDTLS_RIPEMD160_C)
51 #include "mbedtls/ripemd160.h"
52 #endif
53
54 #if defined(MBEDTLS_SHA1_C)
55 #include "mbedtls/sha1.h"
56 #endif
57
58 #if defined(MBEDTLS_SHA256_C)
59 #include "mbedtls/sha256.h"
60 #endif
61
62 #if defined(MBEDTLS_SHA512_C)
63 #include "mbedtls/sha512.h"
64 #endif
65
66 #if defined(MBEDTLS_PLATFORM_C)
67 #include "mbedtls/platform.h"
68 #else
69 #include <stdlib.h>
70 #define mbedtls_calloc calloc
71 #define mbedtls_free free
72 #endif
73
74 #if defined(MBEDTLS_MD2_C)
75
76 static void md2_starts_wrap( void *ctx )
77 {
78 mbedtls_md2_starts( (mbedtls_md2_context *) ctx );
79 }
80
81 static void md2_update_wrap( void *ctx, const unsigned char *input,
82 size_t ilen )
83 {
84 mbedtls_md2_update( (mbedtls_md2_context *) ctx, input, ilen );
85 }
86
87 static void md2_finish_wrap( void *ctx, unsigned char *output )
88 {
89 mbedtls_md2_finish( (mbedtls_md2_context *) ctx, output );
90 }
91
92 static void *md2_ctx_alloc( void )
93 {
94 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
95
96 if( ctx != NULL )
97 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
98
99 return( ctx );
100 }
101
102 static void md2_ctx_free( void *ctx )
103 {
104 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
105 mbedtls_free( ctx );
106 }
107
108 static void md2_clone_wrap( void *dst, const void *src )
109 {
110 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
111 (const mbedtls_md2_context *) src );
112 }
113
114 static void md2_process_wrap( void *ctx, const unsigned char *data )
115 {
116 ((void) data);
117
118 mbedtls_md2_process( (mbedtls_md2_context *) ctx );
119 }
120
121 const mbedtls_md_info_t mbedtls_md2_info = {
122 MBEDTLS_MD_MD2,
123 "MD2",
124 16,
125 16,
126 md2_starts_wrap,
127 md2_update_wrap,
128 md2_finish_wrap,
129 mbedtls_md2,
130 md2_ctx_alloc,
131 md2_ctx_free,
132 md2_clone_wrap,
133 md2_process_wrap,
134 };
135
136 #endif /* MBEDTLS_MD2_C */
137
138 #if defined(MBEDTLS_MD4_C)
139
140 static void md4_starts_wrap( void *ctx )
141 {
142 mbedtls_md4_starts( (mbedtls_md4_context *) ctx );
143 }
144
145 static void md4_update_wrap( void *ctx, const unsigned char *input,
146 size_t ilen )
147 {
148 mbedtls_md4_update( (mbedtls_md4_context *) ctx, input, ilen );
149 }
150
151 static void md4_finish_wrap( void *ctx, unsigned char *output )
152 {
153 mbedtls_md4_finish( (mbedtls_md4_context *) ctx, output );
154 }
155
156 static void *md4_ctx_alloc( void )
157 {
158 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
159
160 if( ctx != NULL )
161 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
162
163 return( ctx );
164 }
165
166 static void md4_ctx_free( void *ctx )
167 {
168 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
169 mbedtls_free( ctx );
170 }
171
172 static void md4_clone_wrap( void *dst, const void *src )
173 {
174 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
175 (const mbedtls_md4_context *) src );
176 }
177
178 static void md4_process_wrap( void *ctx, const unsigned char *data )
179 {
180 mbedtls_md4_process( (mbedtls_md4_context *) ctx, data );
181 }
182
183 const mbedtls_md_info_t mbedtls_md4_info = {
184 MBEDTLS_MD_MD4,
185 "MD4",
186 16,
187 64,
188 md4_starts_wrap,
189 md4_update_wrap,
190 md4_finish_wrap,
191 mbedtls_md4,
192 md4_ctx_alloc,
193 md4_ctx_free,
194 md4_clone_wrap,
195 md4_process_wrap,
196 };
197
198 #endif /* MBEDTLS_MD4_C */
199
200 #if defined(MBEDTLS_MD5_C)
201
202 static void md5_starts_wrap( void *ctx )
203 {
204 mbedtls_md5_starts( (mbedtls_md5_context *) ctx );
205 }
206
207 static void md5_update_wrap( void *ctx, const unsigned char *input,
208 size_t ilen )
209 {
210 mbedtls_md5_update( (mbedtls_md5_context *) ctx, input, ilen );
211 }
212
213 static void md5_finish_wrap( void *ctx, unsigned char *output )
214 {
215 mbedtls_md5_finish( (mbedtls_md5_context *) ctx, output );
216 }
217
218 static void *md5_ctx_alloc( void )
219 {
220 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
221
222 if( ctx != NULL )
223 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
224
225 return( ctx );
226 }
227
228 static void md5_ctx_free( void *ctx )
229 {
230 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
231 mbedtls_free( ctx );
232 }
233
234 static void md5_clone_wrap( void *dst, const void *src )
235 {
236 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
237 (const mbedtls_md5_context *) src );
238 }
239
240 static void md5_process_wrap( void *ctx, const unsigned char *data )
241 {
242 mbedtls_md5_process( (mbedtls_md5_context *) ctx, data );
243 }
244
245 const mbedtls_md_info_t mbedtls_md5_info = {
246 MBEDTLS_MD_MD5,
247 "MD5",
248 16,
249 64,
250 md5_starts_wrap,
251 md5_update_wrap,
252 md5_finish_wrap,
253 mbedtls_md5,
254 md5_ctx_alloc,
255 md5_ctx_free,
256 md5_clone_wrap,
257 md5_process_wrap,
258 };
259
260 #endif /* MBEDTLS_MD5_C */
261
262 #if defined(MBEDTLS_RIPEMD160_C)
263
264 static void ripemd160_starts_wrap( void *ctx )
265 {
266 mbedtls_ripemd160_starts( (mbedtls_ripemd160_context *) ctx );
267 }
268
269 static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
270 size_t ilen )
271 {
272 mbedtls_ripemd160_update( (mbedtls_ripemd160_context *) ctx, input, ilen );
273 }
274
275 static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
276 {
277 mbedtls_ripemd160_finish( (mbedtls_ripemd160_context *) ctx, output );
278 }
279
280 static void *ripemd160_ctx_alloc( void )
281 {
282 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
283
284 if( ctx != NULL )
285 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
286
287 return( ctx );
288 }
289
290 static void ripemd160_ctx_free( void *ctx )
291 {
292 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
293 mbedtls_free( ctx );
294 }
295
296 static void ripemd160_clone_wrap( void *dst, const void *src )
297 {
298 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
299 (const mbedtls_ripemd160_context *) src );
300 }
301
302 static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
303 {
304 mbedtls_ripemd160_process( (mbedtls_ripemd160_context *) ctx, data );
305 }
306
307 const mbedtls_md_info_t mbedtls_ripemd160_info = {
308 MBEDTLS_MD_RIPEMD160,
309 "RIPEMD160",
310 20,
311 64,
312 ripemd160_starts_wrap,
313 ripemd160_update_wrap,
314 ripemd160_finish_wrap,
315 mbedtls_ripemd160,
316 ripemd160_ctx_alloc,
317 ripemd160_ctx_free,
318 ripemd160_clone_wrap,
319 ripemd160_process_wrap,
320 };
321
322 #endif /* MBEDTLS_RIPEMD160_C */
323
324 #if defined(MBEDTLS_SHA1_C)
325
326 static void sha1_starts_wrap( void *ctx )
327 {
328 mbedtls_sha1_starts( (mbedtls_sha1_context *) ctx );
329 }
330
331 static void sha1_update_wrap( void *ctx, const unsigned char *input,
332 size_t ilen )
333 {
334 mbedtls_sha1_update( (mbedtls_sha1_context *) ctx, input, ilen );
335 }
336
337 static void sha1_finish_wrap( void *ctx, unsigned char *output )
338 {
339 mbedtls_sha1_finish( (mbedtls_sha1_context *) ctx, output );
340 }
341
342 static void *sha1_ctx_alloc( void )
343 {
344 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
345
346 if( ctx != NULL )
347 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
348
349 return( ctx );
350 }
351
352 static void sha1_clone_wrap( void *dst, const void *src )
353 {
354 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
355 (const mbedtls_sha1_context *) src );
356 }
357
358 static void sha1_ctx_free( void *ctx )
359 {
360 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
361 mbedtls_free( ctx );
362 }
363
364 static void sha1_process_wrap( void *ctx, const unsigned char *data )
365 {
366 mbedtls_sha1_process( (mbedtls_sha1_context *) ctx, data );
367 }
368
369 const mbedtls_md_info_t mbedtls_sha1_info = {
370 MBEDTLS_MD_SHA1,
371 "SHA1",
372 20,
373 64,
374 sha1_starts_wrap,
375 sha1_update_wrap,
376 sha1_finish_wrap,
377 mbedtls_sha1,
378 sha1_ctx_alloc,
379 sha1_ctx_free,
380 sha1_clone_wrap,
381 sha1_process_wrap,
382 };
383
384 #endif /* MBEDTLS_SHA1_C */
385
386 /*
387 * Wrappers for generic message digests
388 */
389 #if defined(MBEDTLS_SHA256_C)
390
391 static void sha224_starts_wrap( void *ctx )
392 {
393 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 1 );
394 }
395
396 static void sha224_update_wrap( void *ctx, const unsigned char *input,
397 size_t ilen )
398 {
399 mbedtls_sha256_update( (mbedtls_sha256_context *) ctx, input, ilen );
400 }
401
402 static void sha224_finish_wrap( void *ctx, unsigned char *output )
403 {
404 mbedtls_sha256_finish( (mbedtls_sha256_context *) ctx, output );
405 }
406
407 static void sha224_wrap( const unsigned char *input, size_t ilen,
408 unsigned char *output )
409 {
410 mbedtls_sha256( input, ilen, output, 1 );
411 }
412
413 static void *sha224_ctx_alloc( void )
414 {
415 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
416
417 if( ctx != NULL )
418 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
419
420 return( ctx );
421 }
422
423 static void sha224_ctx_free( void *ctx )
424 {
425 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
426 mbedtls_free( ctx );
427 }
428
429 static void sha224_clone_wrap( void *dst, const void *src )
430 {
431 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
432 (const mbedtls_sha256_context *) src );
433 }
434
435 static void sha224_process_wrap( void *ctx, const unsigned char *data )
436 {
437 mbedtls_sha256_process( (mbedtls_sha256_context *) ctx, data );
438 }
439
440 const mbedtls_md_info_t mbedtls_sha224_info = {
441 MBEDTLS_MD_SHA224,
442 "SHA224",
443 28,
444 64,
445 sha224_starts_wrap,
446 sha224_update_wrap,
447 sha224_finish_wrap,
448 sha224_wrap,
449 sha224_ctx_alloc,
450 sha224_ctx_free,
451 sha224_clone_wrap,
452 sha224_process_wrap,
453 };
454
455 static void sha256_starts_wrap( void *ctx )
456 {
457 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 0 );
458 }
459
460 static void sha256_wrap( const unsigned char *input, size_t ilen,
461 unsigned char *output )
462 {
463 mbedtls_sha256( input, ilen, output, 0 );
464 }
465
466 const mbedtls_md_info_t mbedtls_sha256_info = {
467 MBEDTLS_MD_SHA256,
468 "SHA256",
469 32,
470 64,
471 sha256_starts_wrap,
472 sha224_update_wrap,
473 sha224_finish_wrap,
474 sha256_wrap,
475 sha224_ctx_alloc,
476 sha224_ctx_free,
477 sha224_clone_wrap,
478 sha224_process_wrap,
479 };
480
481 #endif /* MBEDTLS_SHA256_C */
482
483 #if defined(MBEDTLS_SHA512_C)
484
485 static void sha384_starts_wrap( void *ctx )
486 {
487 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 1 );
488 }
489
490 static void sha384_update_wrap( void *ctx, const unsigned char *input,
491 size_t ilen )
492 {
493 mbedtls_sha512_update( (mbedtls_sha512_context *) ctx, input, ilen );
494 }
495
496 static void sha384_finish_wrap( void *ctx, unsigned char *output )
497 {
498 mbedtls_sha512_finish( (mbedtls_sha512_context *) ctx, output );
499 }
500
501 static void sha384_wrap( const unsigned char *input, size_t ilen,
502 unsigned char *output )
503 {
504 mbedtls_sha512( input, ilen, output, 1 );
505 }
506
507 static void *sha384_ctx_alloc( void )
508 {
509 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
510
511 if( ctx != NULL )
512 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
513
514 return( ctx );
515 }
516
517 static void sha384_ctx_free( void *ctx )
518 {
519 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
520 mbedtls_free( ctx );
521 }
522
523 static void sha384_clone_wrap( void *dst, const void *src )
524 {
525 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
526 (const mbedtls_sha512_context *) src );
527 }
528
529 static void sha384_process_wrap( void *ctx, const unsigned char *data )
530 {
531 mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
532 }
533
534 const mbedtls_md_info_t mbedtls_sha384_info = {
535 MBEDTLS_MD_SHA384,
536 "SHA384",
537 48,
538 128,
539 sha384_starts_wrap,
540 sha384_update_wrap,
541 sha384_finish_wrap,
542 sha384_wrap,
543 sha384_ctx_alloc,
544 sha384_ctx_free,
545 sha384_clone_wrap,
546 sha384_process_wrap,
547 };
548
549 static void sha512_starts_wrap( void *ctx )
550 {
551 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 0 );
552 }
553
554 static void sha512_wrap( const unsigned char *input, size_t ilen,
555 unsigned char *output )
556 {
557 mbedtls_sha512( input, ilen, output, 0 );
558 }
559
560 const mbedtls_md_info_t mbedtls_sha512_info = {
561 MBEDTLS_MD_SHA512,
562 "SHA512",
563 64,
564 128,
565 sha512_starts_wrap,
566 sha384_update_wrap,
567 sha384_finish_wrap,
568 sha512_wrap,
569 sha384_ctx_alloc,
570 sha384_ctx_free,
571 sha384_clone_wrap,
572 sha384_process_wrap,
573 };
574
575 #endif /* MBEDTLS_SHA512_C */
576
577 #endif /* MBEDTLS_MD_C */