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 void md2_starts_wrap( void *ctx
)
78 mbedtls_md2_starts( (mbedtls_md2_context
*) ctx
);
81 static void md2_update_wrap( void *ctx
, const unsigned char *input
,
84 mbedtls_md2_update( (mbedtls_md2_context
*) ctx
, input
, ilen
);
87 static void md2_finish_wrap( void *ctx
, unsigned char *output
)
89 mbedtls_md2_finish( (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 void md2_process_wrap( void *ctx
, const unsigned char *data
)
118 mbedtls_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 void md4_starts_wrap( void *ctx
)
142 mbedtls_md4_starts( (mbedtls_md4_context
*) ctx
);
145 static void md4_update_wrap( void *ctx
, const unsigned char *input
,
148 mbedtls_md4_update( (mbedtls_md4_context
*) ctx
, input
, ilen
);
151 static void md4_finish_wrap( void *ctx
, unsigned char *output
)
153 mbedtls_md4_finish( (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 void md4_process_wrap( void *ctx
, const unsigned char *data
)
180 mbedtls_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 void md5_starts_wrap( void *ctx
)
204 mbedtls_md5_starts( (mbedtls_md5_context
*) ctx
);
207 static void md5_update_wrap( void *ctx
, const unsigned char *input
,
210 mbedtls_md5_update( (mbedtls_md5_context
*) ctx
, input
, ilen
);
213 static void md5_finish_wrap( void *ctx
, unsigned char *output
)
215 mbedtls_md5_finish( (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 void md5_process_wrap( void *ctx
, const unsigned char *data
)
242 mbedtls_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 void ripemd160_starts_wrap( void *ctx
)
266 mbedtls_ripemd160_starts( (mbedtls_ripemd160_context
*) ctx
);
269 static void ripemd160_update_wrap( void *ctx
, const unsigned char *input
,
272 mbedtls_ripemd160_update( (mbedtls_ripemd160_context
*) ctx
, input
, ilen
);
275 static void ripemd160_finish_wrap( void *ctx
, unsigned char *output
)
277 mbedtls_ripemd160_finish( (mbedtls_ripemd160_context
*) ctx
, output
);
280 static void *ripemd160_ctx_alloc( void )
282 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context
) );
285 mbedtls_ripemd160_init( (mbedtls_ripemd160_context
*) ctx
);
290 static void ripemd160_ctx_free( void *ctx
)
292 mbedtls_ripemd160_free( (mbedtls_ripemd160_context
*) ctx
);
296 static void ripemd160_clone_wrap( void *dst
, const void *src
)
298 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context
*) dst
,
299 (const mbedtls_ripemd160_context
*) src
);
302 static void ripemd160_process_wrap( void *ctx
, const unsigned char *data
)
304 mbedtls_ripemd160_process( (mbedtls_ripemd160_context
*) ctx
, data
);
307 const mbedtls_md_info_t mbedtls_ripemd160_info
= {
308 MBEDTLS_MD_RIPEMD160
,
312 ripemd160_starts_wrap
,
313 ripemd160_update_wrap
,
314 ripemd160_finish_wrap
,
318 ripemd160_clone_wrap
,
319 ripemd160_process_wrap
,
322 #endif /* MBEDTLS_RIPEMD160_C */
324 #if defined(MBEDTLS_SHA1_C)
326 static void sha1_starts_wrap( void *ctx
)
328 mbedtls_sha1_starts( (mbedtls_sha1_context
*) ctx
);
331 static void sha1_update_wrap( void *ctx
, const unsigned char *input
,
334 mbedtls_sha1_update( (mbedtls_sha1_context
*) ctx
, input
, ilen
);
337 static void sha1_finish_wrap( void *ctx
, unsigned char *output
)
339 mbedtls_sha1_finish( (mbedtls_sha1_context
*) ctx
, output
);
342 static void *sha1_ctx_alloc( void )
344 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_sha1_context
) );
347 mbedtls_sha1_init( (mbedtls_sha1_context
*) ctx
);
352 static void sha1_clone_wrap( void *dst
, const void *src
)
354 mbedtls_sha1_clone( (mbedtls_sha1_context
*) dst
,
355 (const mbedtls_sha1_context
*) src
);
358 static void sha1_ctx_free( void *ctx
)
360 mbedtls_sha1_free( (mbedtls_sha1_context
*) ctx
);
364 static void sha1_process_wrap( void *ctx
, const unsigned char *data
)
366 mbedtls_sha1_process( (mbedtls_sha1_context
*) ctx
, data
);
369 const mbedtls_md_info_t mbedtls_sha1_info
= {
384 #endif /* MBEDTLS_SHA1_C */
387 * Wrappers for generic message digests
389 #if defined(MBEDTLS_SHA256_C)
391 static void sha224_starts_wrap( void *ctx
)
393 mbedtls_sha256_starts( (mbedtls_sha256_context
*) ctx
, 1 );
396 static void sha224_update_wrap( void *ctx
, const unsigned char *input
,
399 mbedtls_sha256_update( (mbedtls_sha256_context
*) ctx
, input
, ilen
);
402 static void sha224_finish_wrap( void *ctx
, unsigned char *output
)
404 mbedtls_sha256_finish( (mbedtls_sha256_context
*) ctx
, output
);
407 static void sha224_wrap( const unsigned char *input
, size_t ilen
,
408 unsigned char *output
)
410 mbedtls_sha256( input
, ilen
, output
, 1 );
413 static void *sha224_ctx_alloc( void )
415 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_sha256_context
) );
418 mbedtls_sha256_init( (mbedtls_sha256_context
*) ctx
);
423 static void sha224_ctx_free( void *ctx
)
425 mbedtls_sha256_free( (mbedtls_sha256_context
*) ctx
);
429 static void sha224_clone_wrap( void *dst
, const void *src
)
431 mbedtls_sha256_clone( (mbedtls_sha256_context
*) dst
,
432 (const mbedtls_sha256_context
*) src
);
435 static void sha224_process_wrap( void *ctx
, const unsigned char *data
)
437 mbedtls_sha256_process( (mbedtls_sha256_context
*) ctx
, data
);
440 const mbedtls_md_info_t mbedtls_sha224_info
= {
455 static void sha256_starts_wrap( void *ctx
)
457 mbedtls_sha256_starts( (mbedtls_sha256_context
*) ctx
, 0 );
460 static void sha256_wrap( const unsigned char *input
, size_t ilen
,
461 unsigned char *output
)
463 mbedtls_sha256( input
, ilen
, output
, 0 );
466 const mbedtls_md_info_t mbedtls_sha256_info
= {
481 #endif /* MBEDTLS_SHA256_C */
483 #if defined(MBEDTLS_SHA512_C)
485 static void sha384_starts_wrap( void *ctx
)
487 mbedtls_sha512_starts( (mbedtls_sha512_context
*) ctx
, 1 );
490 static void sha384_update_wrap( void *ctx
, const unsigned char *input
,
493 mbedtls_sha512_update( (mbedtls_sha512_context
*) ctx
, input
, ilen
);
496 static void sha384_finish_wrap( void *ctx
, unsigned char *output
)
498 mbedtls_sha512_finish( (mbedtls_sha512_context
*) ctx
, output
);
501 static void sha384_wrap( const unsigned char *input
, size_t ilen
,
502 unsigned char *output
)
504 mbedtls_sha512( input
, ilen
, output
, 1 );
507 static void *sha384_ctx_alloc( void )
509 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_sha512_context
) );
512 mbedtls_sha512_init( (mbedtls_sha512_context
*) ctx
);
517 static void sha384_ctx_free( void *ctx
)
519 mbedtls_sha512_free( (mbedtls_sha512_context
*) ctx
);
523 static void sha384_clone_wrap( void *dst
, const void *src
)
525 mbedtls_sha512_clone( (mbedtls_sha512_context
*) dst
,
526 (const mbedtls_sha512_context
*) src
);
529 static void sha384_process_wrap( void *ctx
, const unsigned char *data
)
531 mbedtls_sha512_process( (mbedtls_sha512_context
*) ctx
, data
);
534 const mbedtls_md_info_t mbedtls_sha384_info
= {
549 static void sha512_starts_wrap( void *ctx
)
551 mbedtls_sha512_starts( (mbedtls_sha512_context
*) ctx
, 0 );
554 static void sha512_wrap( const unsigned char *input
, size_t ilen
,
555 unsigned char *output
)
557 mbedtls_sha512( input
, ilen
, output
, 0 );
560 const mbedtls_md_info_t mbedtls_sha512_info
= {
575 #endif /* MBEDTLS_SHA512_C */
577 #endif /* MBEDTLS_MD_C */