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"
31 #include MBEDTLS_CONFIG_FILE
34 #if defined(MBEDTLS_MD_C)
36 #include "mbedtls/md_internal.h"
38 #if defined(MBEDTLS_MD2_C)
39 #include "mbedtls/md2.h"
42 #if defined(MBEDTLS_MD4_C)
43 #include "mbedtls/md4.h"
46 #if defined(MBEDTLS_MD5_C)
47 #include "mbedtls/md5.h"
50 #if defined(MBEDTLS_RIPEMD160_C)
51 #include "mbedtls/ripemd160.h"
54 #if defined(MBEDTLS_SHA1_C)
55 #include "mbedtls/sha1.h"
58 #if defined(MBEDTLS_SHA256_C)
59 #include "mbedtls/sha256.h"
62 #if defined(MBEDTLS_SHA512_C)
63 #include "mbedtls/sha512.h"
66 #if defined(MBEDTLS_PLATFORM_C)
67 #include "mbedtls/platform.h"
70 #define mbedtls_calloc calloc
71 #define mbedtls_free free
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
,
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
) );
97 mbedtls_md2_init( (mbedtls_md2_context
*) ctx
);
102 static void md2_ctx_free( void *ctx
)
104 mbedtls_md2_free( (mbedtls_md2_context
*) 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
)
118 return( mbedtls_internal_md2_process( (mbedtls_md2_context
*) ctx
) );
121 const mbedtls_md_info_t mbedtls_md2_info
= {
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
,
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
) );
161 mbedtls_md4_init( (mbedtls_md4_context
*) ctx
);
166 static void md4_ctx_free( void *ctx
)
168 mbedtls_md4_free( (mbedtls_md4_context
*) 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
= {
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
,
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
) );
223 mbedtls_md5_init( (mbedtls_md5_context
*) ctx
);
228 static void md5_ctx_free( void *ctx
)
230 mbedtls_md5_free( (mbedtls_md5_context
*) 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
= {
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
,
272 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context
*) ctx
,
276 static int ripemd160_finish_wrap( void *ctx
, unsigned char *output
)
278 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context
*) ctx
,
282 static void *ripemd160_ctx_alloc( void )
284 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context
) );
287 mbedtls_ripemd160_init( (mbedtls_ripemd160_context
*) ctx
);
292 static void ripemd160_ctx_free( void *ctx
)
294 mbedtls_ripemd160_free( (mbedtls_ripemd160_context
*) 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
,
315 ripemd160_starts_wrap
,
316 ripemd160_update_wrap
,
317 ripemd160_finish_wrap
,
318 mbedtls_ripemd160_ret
,
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
,
337 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context
*) ctx
,
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
) );
351 mbedtls_sha1_init( (mbedtls_sha1_context
*) 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
);
368 static int sha1_process_wrap( void *ctx
, const unsigned char *data
)
370 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context
*) ctx
,
374 const mbedtls_md_info_t mbedtls_sha1_info
= {
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
,
404 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context
*) ctx
,
408 static int sha224_finish_wrap( void *ctx
, unsigned char *output
)
410 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context
*) ctx
,
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
) );
425 mbedtls_sha256_init( (mbedtls_sha256_context
*) ctx
);
430 static void sha224_ctx_free( void *ctx
)
432 mbedtls_sha256_free( (mbedtls_sha256_context
*) 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
,
448 const mbedtls_md_info_t mbedtls_sha224_info
= {
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
= {
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
,
501 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context
*) ctx
,
505 static int sha384_finish_wrap( void *ctx
, unsigned char *output
)
507 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context
*) ctx
,
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
) );
522 mbedtls_sha512_init( (mbedtls_sha512_context
*) ctx
);
527 static void sha384_ctx_free( void *ctx
)
529 mbedtls_sha512_free( (mbedtls_sha512_context
*) 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
,
545 const mbedtls_md_info_t mbedtls_sha384_info
= {
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
= {
586 #endif /* MBEDTLS_SHA512_C */
588 #endif /* MBEDTLS_MD_C */