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 void md2_starts_wrap( void *ctx
)
76 mbedtls_md2_starts( (mbedtls_md2_context
*) ctx
);
79 static void md2_update_wrap( void *ctx
, const unsigned char *input
,
82 mbedtls_md2_update( (mbedtls_md2_context
*) ctx
, input
, ilen
);
85 static void md2_finish_wrap( void *ctx
, unsigned char *output
)
87 mbedtls_md2_finish( (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 void md2_process_wrap( void *ctx
, const unsigned char *data
)
116 mbedtls_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 void md4_starts_wrap( void *ctx
)
140 mbedtls_md4_starts( (mbedtls_md4_context
*) ctx
);
143 static void md4_update_wrap( void *ctx
, const unsigned char *input
,
146 mbedtls_md4_update( (mbedtls_md4_context
*) ctx
, input
, ilen
);
149 static void md4_finish_wrap( void *ctx
, unsigned char *output
)
151 mbedtls_md4_finish( (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 void md4_process_wrap( void *ctx
, const unsigned char *data
)
178 mbedtls_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 void md5_starts_wrap( void *ctx
)
202 mbedtls_md5_starts( (mbedtls_md5_context
*) ctx
);
205 static void md5_update_wrap( void *ctx
, const unsigned char *input
,
208 mbedtls_md5_update( (mbedtls_md5_context
*) ctx
, input
, ilen
);
211 static void md5_finish_wrap( void *ctx
, unsigned char *output
)
213 mbedtls_md5_finish( (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 void md5_process_wrap( void *ctx
, const unsigned char *data
)
240 mbedtls_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 void ripemd160_starts_wrap( void *ctx
)
264 mbedtls_ripemd160_starts( (mbedtls_ripemd160_context
*) ctx
);
267 static void ripemd160_update_wrap( void *ctx
, const unsigned char *input
,
270 mbedtls_ripemd160_update( (mbedtls_ripemd160_context
*) ctx
, input
, ilen
);
273 static void ripemd160_finish_wrap( void *ctx
, unsigned char *output
)
275 mbedtls_ripemd160_finish( (mbedtls_ripemd160_context
*) ctx
, output
);
278 static void *ripemd160_ctx_alloc( void )
280 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context
) );
283 mbedtls_ripemd160_init( (mbedtls_ripemd160_context
*) ctx
);
288 static void ripemd160_ctx_free( void *ctx
)
290 mbedtls_ripemd160_free( (mbedtls_ripemd160_context
*) ctx
);
294 static void ripemd160_clone_wrap( void *dst
, const void *src
)
296 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context
*) dst
,
297 (const mbedtls_ripemd160_context
*) src
);
300 static void ripemd160_process_wrap( void *ctx
, const unsigned char *data
)
302 mbedtls_ripemd160_process( (mbedtls_ripemd160_context
*) ctx
, data
);
305 const mbedtls_md_info_t mbedtls_ripemd160_info
= {
306 MBEDTLS_MD_RIPEMD160
,
310 ripemd160_starts_wrap
,
311 ripemd160_update_wrap
,
312 ripemd160_finish_wrap
,
316 ripemd160_clone_wrap
,
317 ripemd160_process_wrap
,
320 #endif /* MBEDTLS_RIPEMD160_C */
322 #if defined(MBEDTLS_SHA1_C)
324 static void sha1_starts_wrap( void *ctx
)
326 mbedtls_sha1_starts( (mbedtls_sha1_context
*) ctx
);
329 static void sha1_update_wrap( void *ctx
, const unsigned char *input
,
332 mbedtls_sha1_update( (mbedtls_sha1_context
*) ctx
, input
, ilen
);
335 static void sha1_finish_wrap( void *ctx
, unsigned char *output
)
337 mbedtls_sha1_finish( (mbedtls_sha1_context
*) ctx
, output
);
340 static void *sha1_ctx_alloc( void )
342 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_sha1_context
) );
345 mbedtls_sha1_init( (mbedtls_sha1_context
*) ctx
);
350 static void sha1_clone_wrap( void *dst
, const void *src
)
352 mbedtls_sha1_clone( (mbedtls_sha1_context
*) dst
,
353 (const mbedtls_sha1_context
*) src
);
356 static void sha1_ctx_free( void *ctx
)
358 mbedtls_sha1_free( (mbedtls_sha1_context
*) ctx
);
362 static void sha1_process_wrap( void *ctx
, const unsigned char *data
)
364 mbedtls_sha1_process( (mbedtls_sha1_context
*) ctx
, data
);
367 const mbedtls_md_info_t mbedtls_sha1_info
= {
382 #endif /* MBEDTLS_SHA1_C */
385 * Wrappers for generic message digests
387 #if defined(MBEDTLS_SHA256_C)
389 static void sha224_starts_wrap( void *ctx
)
391 mbedtls_sha256_starts( (mbedtls_sha256_context
*) ctx
, 1 );
394 static void sha224_update_wrap( void *ctx
, const unsigned char *input
,
397 mbedtls_sha256_update( (mbedtls_sha256_context
*) ctx
, input
, ilen
);
400 static void sha224_finish_wrap( void *ctx
, unsigned char *output
)
402 mbedtls_sha256_finish( (mbedtls_sha256_context
*) ctx
, output
);
405 static void sha224_wrap( const unsigned char *input
, size_t ilen
,
406 unsigned char *output
)
408 mbedtls_sha256( input
, ilen
, output
, 1 );
411 static void *sha224_ctx_alloc( void )
413 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_sha256_context
) );
416 mbedtls_sha256_init( (mbedtls_sha256_context
*) ctx
);
421 static void sha224_ctx_free( void *ctx
)
423 mbedtls_sha256_free( (mbedtls_sha256_context
*) ctx
);
427 static void sha224_clone_wrap( void *dst
, const void *src
)
429 mbedtls_sha256_clone( (mbedtls_sha256_context
*) dst
,
430 (const mbedtls_sha256_context
*) src
);
433 static void sha224_process_wrap( void *ctx
, const unsigned char *data
)
435 mbedtls_sha256_process( (mbedtls_sha256_context
*) ctx
, data
);
438 const mbedtls_md_info_t mbedtls_sha224_info
= {
453 static void sha256_starts_wrap( void *ctx
)
455 mbedtls_sha256_starts( (mbedtls_sha256_context
*) ctx
, 0 );
458 static void sha256_wrap( const unsigned char *input
, size_t ilen
,
459 unsigned char *output
)
461 mbedtls_sha256( input
, ilen
, output
, 0 );
464 const mbedtls_md_info_t mbedtls_sha256_info
= {
479 #endif /* MBEDTLS_SHA256_C */
481 #if defined(MBEDTLS_SHA512_C)
483 static void sha384_starts_wrap( void *ctx
)
485 mbedtls_sha512_starts( (mbedtls_sha512_context
*) ctx
, 1 );
488 static void sha384_update_wrap( void *ctx
, const unsigned char *input
,
491 mbedtls_sha512_update( (mbedtls_sha512_context
*) ctx
, input
, ilen
);
494 static void sha384_finish_wrap( void *ctx
, unsigned char *output
)
496 mbedtls_sha512_finish( (mbedtls_sha512_context
*) ctx
, output
);
499 static void sha384_wrap( const unsigned char *input
, size_t ilen
,
500 unsigned char *output
)
502 mbedtls_sha512( input
, ilen
, output
, 1 );
505 static void *sha384_ctx_alloc( void )
507 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_sha512_context
) );
510 mbedtls_sha512_init( (mbedtls_sha512_context
*) ctx
);
515 static void sha384_ctx_free( void *ctx
)
517 mbedtls_sha512_free( (mbedtls_sha512_context
*) ctx
);
521 static void sha384_clone_wrap( void *dst
, const void *src
)
523 mbedtls_sha512_clone( (mbedtls_sha512_context
*) dst
,
524 (const mbedtls_sha512_context
*) src
);
527 static void sha384_process_wrap( void *ctx
, const unsigned char *data
)
529 mbedtls_sha512_process( (mbedtls_sha512_context
*) ctx
, data
);
532 const mbedtls_md_info_t mbedtls_sha384_info
= {
547 static void sha512_starts_wrap( void *ctx
)
549 mbedtls_sha512_starts( (mbedtls_sha512_context
*) ctx
, 0 );
552 static void sha512_wrap( const unsigned char *input
, size_t ilen
,
553 unsigned char *output
)
555 mbedtls_sha512( input
, ilen
, output
, 0 );
558 const mbedtls_md_info_t mbedtls_sha512_info
= {
573 #endif /* MBEDTLS_SHA512_C */
575 #endif /* MBEDTLS_MD_C */