Cleanup armsrc/string.c and string.h (#964)
[legacy-proxmark3.git] / common / mbedtls / md_wrap.c
blob97067b042594ea7914c08ac0619536a4dfdb24f7
1 /**
2 * \file md_wrap.c
4 * \brief Generic message digest wrapper for mbed TLS
6 * \author Adriaan de Jong <dejong@fox-it.com>
8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
9 * SPDX-License-Identifier: GPL-2.0
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.
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.
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.
25 * This file is part of mbed TLS (https://tls.mbed.org)
28 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include "mbedtls/config.h"
30 #else
31 #include MBEDTLS_CONFIG_FILE
32 #endif
34 #if defined(MBEDTLS_MD_C)
36 #include "mbedtls/md_internal.h"
38 #if defined(MBEDTLS_MD2_C)
39 #include "mbedtls/md2.h"
40 #endif
42 #if defined(MBEDTLS_MD4_C)
43 #include "mbedtls/md4.h"
44 #endif
46 #if defined(MBEDTLS_MD5_C)
47 #include "mbedtls/md5.h"
48 #endif
50 #if defined(MBEDTLS_RIPEMD160_C)
51 #include "mbedtls/ripemd160.h"
52 #endif
54 #if defined(MBEDTLS_SHA1_C)
55 #include "mbedtls/sha1.h"
56 #endif
58 #if defined(MBEDTLS_SHA256_C)
59 #include "mbedtls/sha256.h"
60 #endif
62 #if defined(MBEDTLS_SHA512_C)
63 #include "mbedtls/sha512.h"
64 #endif
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
74 #if defined(MBEDTLS_MD2_C)
76 static int md2_starts_wrap( void *ctx )
78 return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
81 static int md2_update_wrap( void *ctx, const unsigned char *input,
82 size_t ilen )
84 return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
87 static int md2_finish_wrap( void *ctx, unsigned char *output )
89 return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
92 static void *md2_ctx_alloc( void )
94 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
96 if( ctx != NULL )
97 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
99 return( ctx );
102 static void md2_ctx_free( void *ctx )
104 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
105 mbedtls_free( ctx );
108 static void md2_clone_wrap( void *dst, const void *src )
110 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
111 (const mbedtls_md2_context *) src );
114 static int md2_process_wrap( void *ctx, const unsigned char *data )
116 ((void) data);
118 return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) );
121 const mbedtls_md_info_t mbedtls_md2_info = {
122 MBEDTLS_MD_MD2,
123 "MD2",
126 md2_starts_wrap,
127 md2_update_wrap,
128 md2_finish_wrap,
129 mbedtls_md2_ret,
130 md2_ctx_alloc,
131 md2_ctx_free,
132 md2_clone_wrap,
133 md2_process_wrap,
136 #endif /* MBEDTLS_MD2_C */
138 #if defined(MBEDTLS_MD4_C)
140 static int md4_starts_wrap( void *ctx )
142 return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
145 static int md4_update_wrap( void *ctx, const unsigned char *input,
146 size_t ilen )
148 return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
151 static int md4_finish_wrap( void *ctx, unsigned char *output )
153 return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
156 static void *md4_ctx_alloc( void )
158 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
160 if( ctx != NULL )
161 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
163 return( ctx );
166 static void md4_ctx_free( void *ctx )
168 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
169 mbedtls_free( ctx );
172 static void md4_clone_wrap( void *dst, const void *src )
174 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
175 (const mbedtls_md4_context *) src );
178 static int md4_process_wrap( void *ctx, const unsigned char *data )
180 return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
183 const mbedtls_md_info_t mbedtls_md4_info = {
184 MBEDTLS_MD_MD4,
185 "MD4",
188 md4_starts_wrap,
189 md4_update_wrap,
190 md4_finish_wrap,
191 mbedtls_md4_ret,
192 md4_ctx_alloc,
193 md4_ctx_free,
194 md4_clone_wrap,
195 md4_process_wrap,
198 #endif /* MBEDTLS_MD4_C */
200 #if defined(MBEDTLS_MD5_C)
202 static int md5_starts_wrap( void *ctx )
204 return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
207 static int md5_update_wrap( void *ctx, const unsigned char *input,
208 size_t ilen )
210 return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
213 static int md5_finish_wrap( void *ctx, unsigned char *output )
215 return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
218 static void *md5_ctx_alloc( void )
220 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
222 if( ctx != NULL )
223 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
225 return( ctx );
228 static void md5_ctx_free( void *ctx )
230 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
231 mbedtls_free( ctx );
234 static void md5_clone_wrap( void *dst, const void *src )
236 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
237 (const mbedtls_md5_context *) src );
240 static int md5_process_wrap( void *ctx, const unsigned char *data )
242 return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
245 const mbedtls_md_info_t mbedtls_md5_info = {
246 MBEDTLS_MD_MD5,
247 "MD5",
250 md5_starts_wrap,
251 md5_update_wrap,
252 md5_finish_wrap,
253 mbedtls_md5_ret,
254 md5_ctx_alloc,
255 md5_ctx_free,
256 md5_clone_wrap,
257 md5_process_wrap,
260 #endif /* MBEDTLS_MD5_C */
262 #if defined(MBEDTLS_RIPEMD160_C)
264 static int ripemd160_starts_wrap( void *ctx )
266 return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
269 static int ripemd160_update_wrap( void *ctx, const unsigned char *input,
270 size_t ilen )
272 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
273 input, ilen ) );
276 static int ripemd160_finish_wrap( void *ctx, unsigned char *output )
278 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
279 output ) );
282 static void *ripemd160_ctx_alloc( void )
284 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
286 if( ctx != NULL )
287 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
289 return( ctx );
292 static void ripemd160_ctx_free( void *ctx )
294 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
295 mbedtls_free( ctx );
298 static void ripemd160_clone_wrap( void *dst, const void *src )
300 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
301 (const mbedtls_ripemd160_context *) src );
304 static int ripemd160_process_wrap( void *ctx, const unsigned char *data )
306 return( mbedtls_internal_ripemd160_process(
307 (mbedtls_ripemd160_context *) ctx, data ) );
310 const mbedtls_md_info_t mbedtls_ripemd160_info = {
311 MBEDTLS_MD_RIPEMD160,
312 "RIPEMD160",
315 ripemd160_starts_wrap,
316 ripemd160_update_wrap,
317 ripemd160_finish_wrap,
318 mbedtls_ripemd160_ret,
319 ripemd160_ctx_alloc,
320 ripemd160_ctx_free,
321 ripemd160_clone_wrap,
322 ripemd160_process_wrap,
325 #endif /* MBEDTLS_RIPEMD160_C */
327 #if defined(MBEDTLS_SHA1_C)
329 static int sha1_starts_wrap( void *ctx )
331 return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
334 static int sha1_update_wrap( void *ctx, const unsigned char *input,
335 size_t ilen )
337 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
338 input, ilen ) );
341 static int sha1_finish_wrap( void *ctx, unsigned char *output )
343 return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
346 static void *sha1_ctx_alloc( void )
348 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
350 if( ctx != NULL )
351 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
353 return( ctx );
356 static void sha1_clone_wrap( void *dst, const void *src )
358 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
359 (const mbedtls_sha1_context *) src );
362 static void sha1_ctx_free( void *ctx )
364 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
365 mbedtls_free( ctx );
368 static int sha1_process_wrap( void *ctx, const unsigned char *data )
370 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
371 data ) );
374 const mbedtls_md_info_t mbedtls_sha1_info = {
375 MBEDTLS_MD_SHA1,
376 "SHA1",
379 sha1_starts_wrap,
380 sha1_update_wrap,
381 sha1_finish_wrap,
382 mbedtls_sha1_ret,
383 sha1_ctx_alloc,
384 sha1_ctx_free,
385 sha1_clone_wrap,
386 sha1_process_wrap,
389 #endif /* MBEDTLS_SHA1_C */
392 * Wrappers for generic message digests
394 #if defined(MBEDTLS_SHA256_C)
396 static int sha224_starts_wrap( void *ctx )
398 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
401 static int sha224_update_wrap( void *ctx, const unsigned char *input,
402 size_t ilen )
404 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
405 input, ilen ) );
408 static int sha224_finish_wrap( void *ctx, unsigned char *output )
410 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
411 output ) );
414 static int sha224_wrap( const unsigned char *input, size_t ilen,
415 unsigned char *output )
417 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
420 static void *sha224_ctx_alloc( void )
422 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
424 if( ctx != NULL )
425 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
427 return( ctx );
430 static void sha224_ctx_free( void *ctx )
432 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
433 mbedtls_free( ctx );
436 static void sha224_clone_wrap( void *dst, const void *src )
438 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
439 (const mbedtls_sha256_context *) src );
442 static int sha224_process_wrap( void *ctx, const unsigned char *data )
444 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
445 data ) );
448 const mbedtls_md_info_t mbedtls_sha224_info = {
449 MBEDTLS_MD_SHA224,
450 "SHA224",
453 sha224_starts_wrap,
454 sha224_update_wrap,
455 sha224_finish_wrap,
456 sha224_wrap,
457 sha224_ctx_alloc,
458 sha224_ctx_free,
459 sha224_clone_wrap,
460 sha224_process_wrap,
463 static int sha256_starts_wrap( void *ctx )
465 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
468 static int sha256_wrap( const unsigned char *input, size_t ilen,
469 unsigned char *output )
471 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
474 const mbedtls_md_info_t mbedtls_sha256_info = {
475 MBEDTLS_MD_SHA256,
476 "SHA256",
479 sha256_starts_wrap,
480 sha224_update_wrap,
481 sha224_finish_wrap,
482 sha256_wrap,
483 sha224_ctx_alloc,
484 sha224_ctx_free,
485 sha224_clone_wrap,
486 sha224_process_wrap,
489 #endif /* MBEDTLS_SHA256_C */
491 #if defined(MBEDTLS_SHA512_C)
493 static int sha384_starts_wrap( void *ctx )
495 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
498 static int sha384_update_wrap( void *ctx, const unsigned char *input,
499 size_t ilen )
501 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
502 input, ilen ) );
505 static int sha384_finish_wrap( void *ctx, unsigned char *output )
507 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
508 output ) );
511 static int sha384_wrap( const unsigned char *input, size_t ilen,
512 unsigned char *output )
514 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
517 static void *sha384_ctx_alloc( void )
519 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
521 if( ctx != NULL )
522 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
524 return( ctx );
527 static void sha384_ctx_free( void *ctx )
529 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
530 mbedtls_free( ctx );
533 static void sha384_clone_wrap( void *dst, const void *src )
535 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
536 (const mbedtls_sha512_context *) src );
539 static int sha384_process_wrap( void *ctx, const unsigned char *data )
541 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
542 data ) );
545 const mbedtls_md_info_t mbedtls_sha384_info = {
546 MBEDTLS_MD_SHA384,
547 "SHA384",
549 128,
550 sha384_starts_wrap,
551 sha384_update_wrap,
552 sha384_finish_wrap,
553 sha384_wrap,
554 sha384_ctx_alloc,
555 sha384_ctx_free,
556 sha384_clone_wrap,
557 sha384_process_wrap,
560 static int sha512_starts_wrap( void *ctx )
562 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
565 static int sha512_wrap( const unsigned char *input, size_t ilen,
566 unsigned char *output )
568 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
571 const mbedtls_md_info_t mbedtls_sha512_info = {
572 MBEDTLS_MD_SHA512,
573 "SHA512",
575 128,
576 sha512_starts_wrap,
577 sha384_update_wrap,
578 sha384_finish_wrap,
579 sha512_wrap,
580 sha384_ctx_alloc,
581 sha384_ctx_free,
582 sha384_clone_wrap,
583 sha384_process_wrap,
586 #endif /* MBEDTLS_SHA512_C */
588 #endif /* MBEDTLS_MD_C */