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: Apache-2.0
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * This file is part of mbed TLS (https://tls.mbed.org)
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "mbedtls/config.h"
29 #include MBEDTLS_CONFIG_FILE
32 #if defined(MBEDTLS_MD_C)
34 #include "mbedtls/md_internal.h"
36 #if defined(MBEDTLS_MD2_C)
37 #include "mbedtls/md2.h"
40 #if defined(MBEDTLS_MD4_C)
41 #include "mbedtls/md4.h"
44 #if defined(MBEDTLS_MD5_C)
45 #include "mbedtls/md5.h"
48 #if defined(MBEDTLS_RIPEMD160_C)
49 #include "mbedtls/ripemd160.h"
52 #if defined(MBEDTLS_SHA1_C)
53 #include "mbedtls/sha1.h"
56 #if defined(MBEDTLS_SHA256_C)
57 #include "mbedtls/sha256.h"
60 #if defined(MBEDTLS_SHA512_C)
61 #include "mbedtls/sha512.h"
64 #if defined(MBEDTLS_PLATFORM_C)
65 #include "mbedtls/platform.h"
68 #define mbedtls_calloc calloc
69 #define mbedtls_free free
72 #if defined(MBEDTLS_MD2_C)
74 static int md2_starts_wrap( void *ctx )
76 return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
79 static int md2_update_wrap( void *ctx, const unsigned char *input,
82 return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
85 static int md2_finish_wrap( void *ctx, unsigned char *output )
87 return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
90 static void *md2_ctx_alloc( void )
92 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
95 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
100 static void md2_ctx_free( void *ctx )
102 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
106 static void md2_clone_wrap( void *dst, const void *src )
108 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
109 (const mbedtls_md2_context *) src );
112 static int md2_process_wrap( void *ctx, const unsigned char *data )
116 return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) );
119 const mbedtls_md_info_t mbedtls_md2_info = {
134 #endif /* MBEDTLS_MD2_C */
136 #if defined(MBEDTLS_MD4_C)
138 static int md4_starts_wrap( void *ctx )
140 return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
143 static int md4_update_wrap( void *ctx, const unsigned char *input,
146 return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
149 static int md4_finish_wrap( void *ctx, unsigned char *output )
151 return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
154 static void *md4_ctx_alloc( void )
156 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
159 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
164 static void md4_ctx_free( void *ctx )
166 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
170 static void md4_clone_wrap( void *dst, const void *src )
172 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
173 (const mbedtls_md4_context *) src );
176 static int md4_process_wrap( void *ctx, const unsigned char *data )
178 return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
181 const mbedtls_md_info_t mbedtls_md4_info = {
196 #endif /* MBEDTLS_MD4_C */
198 #if defined(MBEDTLS_MD5_C)
200 static int md5_starts_wrap( void *ctx )
202 return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
205 static int md5_update_wrap( void *ctx, const unsigned char *input,
208 return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
211 static int md5_finish_wrap( void *ctx, unsigned char *output )
213 return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
216 static void *md5_ctx_alloc( void )
218 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
221 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
226 static void md5_ctx_free( void *ctx )
228 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
232 static void md5_clone_wrap( void *dst, const void *src )
234 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
235 (const mbedtls_md5_context *) src );
238 static int md5_process_wrap( void *ctx, const unsigned char *data )
240 return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
243 const mbedtls_md_info_t mbedtls_md5_info = {
258 #endif /* MBEDTLS_MD5_C */
260 #if defined(MBEDTLS_RIPEMD160_C)
262 static int ripemd160_starts_wrap( void *ctx )
264 return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
267 static int ripemd160_update_wrap( void *ctx, const unsigned char *input,
270 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
274 static int ripemd160_finish_wrap( void *ctx, unsigned char *output )
276 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
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 int ripemd160_process_wrap( void *ctx, const unsigned char *data )
304 return( mbedtls_internal_ripemd160_process(
305 (mbedtls_ripemd160_context *) ctx, data ) );
308 const mbedtls_md_info_t mbedtls_ripemd160_info = {
309 MBEDTLS_MD_RIPEMD160,
313 ripemd160_starts_wrap,
314 ripemd160_update_wrap,
315 ripemd160_finish_wrap,
316 mbedtls_ripemd160_ret,
319 ripemd160_clone_wrap,
320 ripemd160_process_wrap,
323 #endif /* MBEDTLS_RIPEMD160_C */
325 #if defined(MBEDTLS_SHA1_C)
327 static int sha1_starts_wrap( void *ctx )
329 return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
332 static int sha1_update_wrap( void *ctx, const unsigned char *input,
335 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
339 static int sha1_finish_wrap( void *ctx, unsigned char *output )
341 return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
344 static void *sha1_ctx_alloc( void )
346 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
349 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
354 static void sha1_clone_wrap( void *dst, const void *src )
356 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
357 (const mbedtls_sha1_context *) src );
360 static void sha1_ctx_free( void *ctx )
362 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
366 static int sha1_process_wrap( void *ctx, const unsigned char *data )
368 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
372 const mbedtls_md_info_t mbedtls_sha1_info = {
387 #endif /* MBEDTLS_SHA1_C */
390 * Wrappers for generic message digests
392 #if defined(MBEDTLS_SHA256_C)
394 static int sha224_starts_wrap( void *ctx )
396 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
399 static int sha224_update_wrap( void *ctx, const unsigned char *input,
402 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
406 static int sha224_finish_wrap( void *ctx, unsigned char *output )
408 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
412 static int sha224_wrap( const unsigned char *input, size_t ilen,
413 unsigned char *output )
415 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
418 static void *sha224_ctx_alloc( void )
420 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
423 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
428 static void sha224_ctx_free( void *ctx )
430 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
434 static void sha224_clone_wrap( void *dst, const void *src )
436 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
437 (const mbedtls_sha256_context *) src );
440 static int sha224_process_wrap( void *ctx, const unsigned char *data )
442 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
446 const mbedtls_md_info_t mbedtls_sha224_info = {
461 static int sha256_starts_wrap( void *ctx )
463 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
466 static int sha256_wrap( const unsigned char *input, size_t ilen,
467 unsigned char *output )
469 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
472 const mbedtls_md_info_t mbedtls_sha256_info = {
487 #endif /* MBEDTLS_SHA256_C */
489 #if defined(MBEDTLS_SHA512_C)
491 static int sha384_starts_wrap( void *ctx )
493 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
496 static int sha384_update_wrap( void *ctx, const unsigned char *input,
499 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
503 static int sha384_finish_wrap( void *ctx, unsigned char *output )
505 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
509 static int sha384_wrap( const unsigned char *input, size_t ilen,
510 unsigned char *output )
512 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
515 static void *sha384_ctx_alloc( void )
517 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
520 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
525 static void sha384_ctx_free( void *ctx )
527 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
531 static void sha384_clone_wrap( void *dst, const void *src )
533 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
534 (const mbedtls_sha512_context *) src );
537 static int sha384_process_wrap( void *ctx, const unsigned char *data )
539 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
543 const mbedtls_md_info_t mbedtls_sha384_info = {
558 static int sha512_starts_wrap( void *ctx )
560 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
563 static int sha512_wrap( const unsigned char *input, size_t ilen,
564 unsigned char *output )
566 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
569 const mbedtls_md_info_t mbedtls_sha512_info = {
584 #endif /* MBEDTLS_SHA512_C */
586 #endif /* MBEDTLS_MD_C */