blob: 38f89dbeadeca7636156396346ecd4baa634df1d [file] [log] [blame]
Hanno Beckerc4e42102019-09-04 12:43:22 +01001 /**
2 * \file md.h
3 *
4 * \brief This file contains the generic message-digest wrapper.
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 */
8/*
9 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 * This file is part of Mbed TLS (https://tls.mbed.org)
25 */
26
27#ifndef MBEDTLS_MD_INTERNAL_H
28#define MBEDTLS_MD_INTERNAL_H
29
30#if defined(MBEDTLS_MD2_C)
31#include "mbedtls/md2.h"
32#endif
33
34#if defined(MBEDTLS_MD4_C)
35#include "mbedtls/md4.h"
36#endif
37
38#if defined(MBEDTLS_MD5_C)
39#include "mbedtls/md5.h"
40#endif
41
42#if defined(MBEDTLS_RIPEMD160_C)
43#include "mbedtls/ripemd160.h"
44#endif
45
46#if defined(MBEDTLS_SHA1_C)
47#include "mbedtls/sha1.h"
48#endif
49
50#if defined(MBEDTLS_SHA256_C)
51#include "mbedtls/sha256.h"
52#endif
53
54#if defined(MBEDTLS_SHA512_C)
55#include "mbedtls/sha512.h"
56#endif
57
58#include "mbedtls/platform_util.h"
59
60#if defined(MBEDTLS_PLATFORM_C)
61#include "mbedtls/platform.h"
62#else
63#include <stdlib.h>
64#define mbedtls_calloc calloc
65#define mbedtls_free free
66#endif
67
68#ifdef __cplusplus
69extern "C" {
70#endif
71
Hanno Becker7a78fe42019-09-04 15:41:21 +010072#define MBEDTLS_MD_WRAPPER MBEDTLS_ALWAYS_INLINE static inline
73
Hanno Beckerc4e42102019-09-04 12:43:22 +010074/*
75 * Message-digest information macro definition
76 */
77
Hanno Becker7a7b7222019-09-04 12:47:48 +010078/* Dummy definition to keep check-names.sh happy - don't uncomment */
79//#define MBEDTLS_MD_INFO_SHA256
80
Hanno Beckerc4e42102019-09-04 12:43:22 +010081/* SHA-256 */
Hanno Becker94f48e02019-09-05 13:02:57 +010082static inline void mbedtls_md_sha256_init_free_dummy( void* ctx )
83{
84 /* Zero-initialization can be skipped. */
85 ((void) ctx);
86}
Hanno Beckerc4e42102019-09-04 12:43:22 +010087#define MBEDTLS_MD_INFO_SHA256_TYPE MBEDTLS_MD_SHA256
Hanno Becker6deddf72019-09-05 11:17:30 +010088#define MBEDTLS_MD_INFO_SHA256_CTX_TYPE mbedtls_sha256_context
Hanno Becker94f48e02019-09-05 13:02:57 +010089#if defined(MBEDTLS_MD_SINGLE_HASH)
90/* mbedtls_md_sha256_init() only zeroizes, which is redundant
91 * because mbedtls_md_context is zeroized in mbedtls_md_init(),
92 * and the mbedtls_sha256_context is embedded in mbedtls_md_context_t. */
93#define MBEDTLS_MD_INFO_SHA256_INIT_FUNC mbedtls_md_sha256_init_free_dummy
94#else
Hanno Becker4a997652019-09-05 11:55:25 +010095#define MBEDTLS_MD_INFO_SHA256_INIT_FUNC mbedtls_sha256_init
Hanno Becker94f48e02019-09-05 13:02:57 +010096#endif /* MBEDTLS_MD_SINGLE_HASH */
Hanno Beckerc4e42102019-09-04 12:43:22 +010097#define MBEDTLS_MD_INFO_SHA256_NAME "SHA256"
98#define MBEDTLS_MD_INFO_SHA256_SIZE 32
99#define MBEDTLS_MD_INFO_SHA256_BLOCKSIZE 64
100#define MBEDTLS_MD_INFO_SHA256_STARTS_FUNC mbedtls_sha256_starts_wrap
101#define MBEDTLS_MD_INFO_SHA256_UPDATE_FUNC mbedtls_sha224_update_wrap
102#define MBEDTLS_MD_INFO_SHA256_FINISH_FUNC mbedtls_sha224_finish_wrap
103#define MBEDTLS_MD_INFO_SHA256_DIGEST_FUNC mbedtls_sha256_wrap
104#define MBEDTLS_MD_INFO_SHA256_ALLOC_FUNC mbedtls_sha224_ctx_alloc
Hanno Becker94f48e02019-09-05 13:02:57 +0100105#if defined(MBEDTLS_MD_SINGLE_HASH)
106/* mbedtls_md_sha256_free() only zeroizes, which is redundant
107 * because mbedtls_md_context is zeroized in mbedtls_md_init(),
108 * and the mbedtls_sha256_context is embedded in mbedtls_md_context_t. */
109#define MBEDTLS_MD_INFO_SHA256_FREE_FUNC mbedtls_md_sha256_init_free_dummy
110#else
Hanno Beckerc4e42102019-09-04 12:43:22 +0100111#define MBEDTLS_MD_INFO_SHA256_FREE_FUNC mbedtls_sha224_ctx_free
Hanno Becker94f48e02019-09-05 13:02:57 +0100112#endif /* MBEDTLS_MD_SINGLE_HASH */
Hanno Beckerc4e42102019-09-04 12:43:22 +0100113#define MBEDTLS_MD_INFO_SHA256_CLONE_FUNC mbedtls_sha224_clone_wrap
114#define MBEDTLS_MD_INFO_SHA256_PROCESS_FUNC mbedtls_sha224_process_wrap
115
116/*
117 * Helper macros to extract fields from ciphersuites.
118 */
119
Hanno Becker6deddf72019-09-05 11:17:30 +0100120#define MBEDTLS_MD_INFO_CTX_TYPE_T( MD ) MD ## _CTX_TYPE
Hanno Becker4a997652019-09-05 11:55:25 +0100121#define MBEDTLS_MD_INFO_INIT_FUNC_T( MD ) MD ## _INIT_FUNC
Hanno Beckerc4e42102019-09-04 12:43:22 +0100122#define MBEDTLS_MD_INFO_TYPE_T( MD ) MD ## _TYPE
123#define MBEDTLS_MD_INFO_NAME_T( MD ) MD ## _NAME
124#define MBEDTLS_MD_INFO_SIZE_T( MD ) MD ## _SIZE
125#define MBEDTLS_MD_INFO_BLOCKSIZE_T( MD ) MD ## _BLOCKSIZE
126#define MBEDTLS_MD_INFO_STARTS_FUNC_T( MD ) MD ## _STARTS_FUNC
127#define MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD ) MD ## _UPDATE_FUNC
128#define MBEDTLS_MD_INFO_FINISH_FUNC_T( MD ) MD ## _FINISH_FUNC
129#define MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD ) MD ## _DIGEST_FUNC
130#define MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD ) MD ## _ALLOC_FUNC
131#define MBEDTLS_MD_INFO_FREE_FUNC_T( MD ) MD ## _FREE_FUNC
132#define MBEDTLS_MD_INFO_CLONE_FUNC_T( MD ) MD ## _CLONE_FUNC
133#define MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD ) MD ## _PROCESS_FUNC
134
Hanno Becker55fdae02019-09-06 11:57:32 +0100135/* Wrapper around MBEDTLS_MD_INFO_{FIELD}_T() which makes sure that
Hanno Beckerc4e42102019-09-04 12:43:22 +0100136 * the argument is macro-expanded before concatenated with the
137 * field name. This allows to call these macros as
Hanno Becker55fdae02019-09-06 11:57:32 +0100138 * MBEDTLS_MD_INFO_{FIELD}( MBEDTLS_MD_SINGLE_HASH ).
139 * where MBEDTLS_MD_SINGLE_HASH expands to MBEDTLS_MD_INFO_{DIGEST}. */
Hanno Becker6deddf72019-09-05 11:17:30 +0100140#define MBEDTLS_MD_INFO_CTX_TYPE( MD ) MBEDTLS_MD_INFO_CTX_TYPE_T( MD )
Hanno Becker4a997652019-09-05 11:55:25 +0100141#define MBEDTLS_MD_INFO_INIT_FUNC( MD ) MBEDTLS_MD_INFO_INIT_FUNC_T( MD )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100142#define MBEDTLS_MD_INFO_TYPE( MD ) MBEDTLS_MD_INFO_TYPE_T( MD )
143#define MBEDTLS_MD_INFO_NAME( MD ) MBEDTLS_MD_INFO_NAME_T( MD )
144#define MBEDTLS_MD_INFO_SIZE( MD ) MBEDTLS_MD_INFO_SIZE_T( MD )
145#define MBEDTLS_MD_INFO_BLOCKSIZE( MD ) MBEDTLS_MD_INFO_BLOCKSIZE_T( MD )
146#define MBEDTLS_MD_INFO_STARTS_FUNC( MD ) MBEDTLS_MD_INFO_STARTS_FUNC_T( MD )
147#define MBEDTLS_MD_INFO_UPDATE_FUNC( MD ) MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD )
148#define MBEDTLS_MD_INFO_FINISH_FUNC( MD ) MBEDTLS_MD_INFO_FINISH_FUNC_T( MD )
149#define MBEDTLS_MD_INFO_DIGEST_FUNC( MD ) MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD )
150#define MBEDTLS_MD_INFO_ALLOC_FUNC( MD ) MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD )
151#define MBEDTLS_MD_INFO_FREE_FUNC( MD ) MBEDTLS_MD_INFO_FREE_FUNC_T( MD )
152#define MBEDTLS_MD_INFO_CLONE_FUNC( MD ) MBEDTLS_MD_INFO_CLONE_FUNC_T( MD )
153#define MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD )
154
155/**
156 * Message digest information.
157 * Allows message digest functions to be called in a generic way.
158 */
159
160typedef int mbedtls_md_starts_func_t( void *ctx );
161typedef int mbedtls_md_update_func_t( void *ctx,
162 const unsigned char *input,
163 size_t ilen );
164typedef int mbedtls_md_finish_func_t( void *ctx, unsigned char *output );
165typedef int mbedtls_md_digest_func_t( const unsigned char *input,
166 size_t ilen,
167 unsigned char *output );
168typedef void* mbedtls_md_ctx_alloc_func_t( void );
169typedef void mbedtls_md_ctx_free_func_t( void *ctx );
170typedef void mbedtls_md_clone_func_t( void *st, const void *src );
171typedef int mbedtls_md_process_func_t( void *ctx,
172 const unsigned char *input );
173
174#if !defined(MBEDTLS_MD_SINGLE_HASH)
175struct mbedtls_md_info_t
176{
177 /** Digest identifier */
178 mbedtls_md_type_t type;
179
180 /** Name of the message digest */
181 const char * name;
182
183 /** Output length of the digest function in bytes */
184 int size;
185
186 /** Block length of the digest function in bytes */
187 int block_size;
188
189 /** Digest initialisation function */
190 mbedtls_md_starts_func_t *starts_func;
191
192 /** Digest update function */
193 mbedtls_md_update_func_t *update_func;
194
195 /** Digest finalisation function */
196 mbedtls_md_finish_func_t *finish_func;
197
198 /** Generic digest function */
199 mbedtls_md_digest_func_t *digest_func;
200
201 /** Allocate a new context */
202 mbedtls_md_ctx_alloc_func_t *ctx_alloc_func;
203
204 /** Free the given context */
205 mbedtls_md_ctx_free_func_t *ctx_free_func;
206
207 /** Clone state from a context */
208 mbedtls_md_clone_func_t *clone_func;
209
210 /** Internal use only */
211 mbedtls_md_process_func_t *process_func;
212};
213
214/**
215 * \brief This macro builds an instance of ::mbedtls_md_info_t
216 * from an \c MBEDTLS_MD_INFO_XXX identifier.
217 */
218#define MBEDTLS_MD_INFO( MD ) \
219 { MBEDTLS_MD_INFO_TYPE( MD ), \
220 MBEDTLS_MD_INFO_NAME( MD ), \
221 MBEDTLS_MD_INFO_SIZE( MD ), \
222 MBEDTLS_MD_INFO_BLOCKSIZE( MD ), \
223 MBEDTLS_MD_INFO_STARTS_FUNC( MD ), \
224 MBEDTLS_MD_INFO_UPDATE_FUNC( MD ), \
225 MBEDTLS_MD_INFO_FINISH_FUNC( MD ), \
226 MBEDTLS_MD_INFO_DIGEST_FUNC( MD ), \
227 MBEDTLS_MD_INFO_ALLOC_FUNC( MD ), \
228 MBEDTLS_MD_INFO_FREE_FUNC( MD ), \
229 MBEDTLS_MD_INFO_CLONE_FUNC( MD ), \
230 MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) }
231
232#endif /* !MBEDTLS_MD_SINGLE_HASH */
233
234/*
235 *
236 * Definitions of MD information structures for various digests.
237 *
238 */
239
240/*
241 * MD-2
242 */
243
244#if defined(MBEDTLS_MD2_C)
245
Hanno Becker7a78fe42019-09-04 15:41:21 +0100246MBEDTLS_MD_WRAPPER int mbedtls_md2_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100247{
248 return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
249}
250
Hanno Becker7a78fe42019-09-04 15:41:21 +0100251MBEDTLS_MD_WRAPPER int mbedtls_md2_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100252 size_t ilen )
253{
254 return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
255}
256
Hanno Becker7a78fe42019-09-04 15:41:21 +0100257MBEDTLS_MD_WRAPPER int mbedtls_md2_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100258{
259 return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
260}
261
Hanno Becker7a78fe42019-09-04 15:41:21 +0100262MBEDTLS_MD_WRAPPER void* mbedtls_md2_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100263{
264 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
265
266 if( ctx != NULL )
267 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
268
269 return( ctx );
270}
271
Hanno Becker7a78fe42019-09-04 15:41:21 +0100272MBEDTLS_MD_WRAPPER void mbedtls_md2_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100273{
274 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
275 mbedtls_free( ctx );
276}
277
Hanno Becker7a78fe42019-09-04 15:41:21 +0100278MBEDTLS_MD_WRAPPER void mbedtls_md2_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100279{
280 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
281 (const mbedtls_md2_context *) src );
282}
283
Hanno Becker7a78fe42019-09-04 15:41:21 +0100284MBEDTLS_MD_WRAPPER int mbedtls_md2_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100285{
286 ((void) data);
287
288 return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) );
289}
290
291#endif /* MBEDTLS_MD2_C */
292
293/*
294 * MD-4
295 */
296
297#if defined(MBEDTLS_MD4_C)
298
Hanno Becker7a78fe42019-09-04 15:41:21 +0100299MBEDTLS_MD_WRAPPER int mbedtls_md4_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100300{
301 return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
302}
303
Hanno Becker7a78fe42019-09-04 15:41:21 +0100304MBEDTLS_MD_WRAPPER int mbedtls_md4_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100305 size_t ilen )
306{
307 return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
308}
309
Hanno Becker7a78fe42019-09-04 15:41:21 +0100310MBEDTLS_MD_WRAPPER int mbedtls_md4_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100311{
312 return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
313}
314
Hanno Becker7a78fe42019-09-04 15:41:21 +0100315MBEDTLS_MD_WRAPPER void* mbedtls_md4_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100316{
317 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
318
319 if( ctx != NULL )
320 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
321
322 return( ctx );
323}
324
Hanno Becker7a78fe42019-09-04 15:41:21 +0100325MBEDTLS_MD_WRAPPER void mbedtls_md4_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100326{
327 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
328 mbedtls_free( ctx );
329}
330
Hanno Becker7a78fe42019-09-04 15:41:21 +0100331MBEDTLS_MD_WRAPPER void mbedtls_md4_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100332{
333 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
334 (const mbedtls_md4_context *) src );
335}
336
Hanno Becker7a78fe42019-09-04 15:41:21 +0100337MBEDTLS_MD_WRAPPER int mbedtls_md4_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100338{
339 return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
340}
341
342#endif /* MBEDTLS_MD4_C */
343
344/*
345 * MD-5
346 */
347
348#if defined(MBEDTLS_MD5_C)
349
Hanno Becker7a78fe42019-09-04 15:41:21 +0100350MBEDTLS_MD_WRAPPER int mbedtls_md5_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100351{
352 return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
353}
354
Hanno Becker7a78fe42019-09-04 15:41:21 +0100355MBEDTLS_MD_WRAPPER int mbedtls_md5_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100356 size_t ilen )
357{
358 return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
359}
360
Hanno Becker7a78fe42019-09-04 15:41:21 +0100361MBEDTLS_MD_WRAPPER int mbedtls_md5_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100362{
363 return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
364}
365
Hanno Becker7a78fe42019-09-04 15:41:21 +0100366MBEDTLS_MD_WRAPPER void* mbedtls_md5_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100367{
368 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
369
370 if( ctx != NULL )
371 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
372
373 return( ctx );
374}
375
Hanno Becker7a78fe42019-09-04 15:41:21 +0100376MBEDTLS_MD_WRAPPER void mbedtls_md5_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100377{
378 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
379 mbedtls_free( ctx );
380}
381
Hanno Becker7a78fe42019-09-04 15:41:21 +0100382MBEDTLS_MD_WRAPPER void mbedtls_md5_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100383{
384 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
385 (const mbedtls_md5_context *) src );
386}
387
Hanno Becker7a78fe42019-09-04 15:41:21 +0100388MBEDTLS_MD_WRAPPER int mbedtls_md5_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100389{
390 return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
391}
392
393#endif /* MBEDTLS_MD5_C */
394
395/*
396 * RIPEMD-160
397 */
398
399#if defined(MBEDTLS_RIPEMD160_C)
400
Hanno Becker7a78fe42019-09-04 15:41:21 +0100401MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100402{
403 return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
404}
405
Hanno Becker7a78fe42019-09-04 15:41:21 +0100406MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100407 size_t ilen )
408{
409 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
410 input, ilen ) );
411}
412
Hanno Becker7a78fe42019-09-04 15:41:21 +0100413MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100414{
415 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
416 output ) );
417}
418
Hanno Becker7a78fe42019-09-04 15:41:21 +0100419MBEDTLS_MD_WRAPPER void* mbedtls_ripemd160_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100420{
421 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
422
423 if( ctx != NULL )
424 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
425
426 return( ctx );
427}
428
Hanno Becker7a78fe42019-09-04 15:41:21 +0100429MBEDTLS_MD_WRAPPER void mbedtls_ripemd160_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100430{
431 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
432 mbedtls_free( ctx );
433}
434
Hanno Becker7a78fe42019-09-04 15:41:21 +0100435MBEDTLS_MD_WRAPPER void mbedtls_ripemd160_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100436{
437 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
438 (const mbedtls_ripemd160_context *) src );
439}
440
Hanno Becker7a78fe42019-09-04 15:41:21 +0100441MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100442{
443 return( mbedtls_internal_ripemd160_process(
444 (mbedtls_ripemd160_context *) ctx, data ) );
445}
446
447#endif /* MBEDTLS_RIPEMD160_C */
448
449/*
450 * SHA-1
451 */
452
453#if defined(MBEDTLS_SHA1_C)
454
Hanno Becker7a78fe42019-09-04 15:41:21 +0100455MBEDTLS_MD_WRAPPER int mbedtls_sha1_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100456{
457 return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
458}
459
Hanno Becker7a78fe42019-09-04 15:41:21 +0100460MBEDTLS_MD_WRAPPER int mbedtls_sha1_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100461 size_t ilen )
462{
463 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
464 input, ilen ) );
465}
466
Hanno Becker7a78fe42019-09-04 15:41:21 +0100467MBEDTLS_MD_WRAPPER int mbedtls_sha1_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100468{
469 return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
470}
471
Hanno Becker7a78fe42019-09-04 15:41:21 +0100472MBEDTLS_MD_WRAPPER void* mbedtls_sha1_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100473{
474 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
475
476 if( ctx != NULL )
477 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
478
479 return( ctx );
480}
481
Hanno Becker7a78fe42019-09-04 15:41:21 +0100482MBEDTLS_MD_WRAPPER void mbedtls_sha1_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100483{
484 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
485 (const mbedtls_sha1_context *) src );
486}
487
Hanno Becker7a78fe42019-09-04 15:41:21 +0100488MBEDTLS_MD_WRAPPER void mbedtls_sha1_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100489{
490 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
491 mbedtls_free( ctx );
492}
493
Hanno Becker7a78fe42019-09-04 15:41:21 +0100494MBEDTLS_MD_WRAPPER int mbedtls_sha1_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100495{
496 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
497 data ) );
498}
499
500#endif /* MBEDTLS_SHA1_C */
501
502/*
503 * SHA-224 and SHA-256
504 */
505
506#if defined(MBEDTLS_SHA256_C)
507
508#if !defined(MBEDTLS_SHA256_NO_SHA224)
Hanno Becker7a78fe42019-09-04 15:41:21 +0100509MBEDTLS_MD_WRAPPER int mbedtls_sha224_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100510{
511 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
512}
513#endif /* !MBEDTLS_SHA256_NO_SHA224 */
514
Hanno Becker7a78fe42019-09-04 15:41:21 +0100515MBEDTLS_MD_WRAPPER int mbedtls_sha224_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100516 size_t ilen )
517{
518 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
519 input, ilen ) );
520}
521
Hanno Becker7a78fe42019-09-04 15:41:21 +0100522MBEDTLS_MD_WRAPPER int mbedtls_sha224_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100523{
524 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
525 output ) );
526}
527
528#if !defined(MBEDTLS_SHA256_NO_SHA224)
Hanno Becker7a78fe42019-09-04 15:41:21 +0100529MBEDTLS_MD_WRAPPER int mbedtls_sha224_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100530 unsigned char *output )
531{
532 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
533}
534#endif /* !MBEDTLS_SHA256_NO_SHA224 */
535
Hanno Becker7a78fe42019-09-04 15:41:21 +0100536MBEDTLS_MD_WRAPPER void* mbedtls_sha224_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100537{
538 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
539
540 if( ctx != NULL )
541 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
542
543 return( ctx );
544}
545
Hanno Becker7a78fe42019-09-04 15:41:21 +0100546MBEDTLS_MD_WRAPPER void mbedtls_sha224_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100547{
548 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
549 mbedtls_free( ctx );
550}
551
Hanno Becker7a78fe42019-09-04 15:41:21 +0100552MBEDTLS_MD_WRAPPER void mbedtls_sha224_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100553{
554 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
555 (const mbedtls_sha256_context *) src );
556}
557
Hanno Becker7a78fe42019-09-04 15:41:21 +0100558MBEDTLS_MD_WRAPPER int mbedtls_sha224_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100559{
560 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
561 data ) );
562}
563
Hanno Becker7a78fe42019-09-04 15:41:21 +0100564MBEDTLS_MD_WRAPPER int mbedtls_sha256_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100565{
566 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
567}
568
Hanno Becker7a78fe42019-09-04 15:41:21 +0100569MBEDTLS_MD_WRAPPER int mbedtls_sha256_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100570 unsigned char *output )
571{
572 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
573}
574
575#endif /* MBEDTLS_SHA256_C */
576
577/*
578 * SHA-384 and SHA-512
579 */
580
581#if defined(MBEDTLS_SHA512_C)
582
Hanno Becker7a78fe42019-09-04 15:41:21 +0100583MBEDTLS_MD_WRAPPER int mbedtls_sha384_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100584{
585 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
586}
587
Hanno Becker7a78fe42019-09-04 15:41:21 +0100588MBEDTLS_MD_WRAPPER int mbedtls_sha384_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100589 size_t ilen )
590{
591 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
592 input, ilen ) );
593}
594
Hanno Becker7a78fe42019-09-04 15:41:21 +0100595MBEDTLS_MD_WRAPPER int mbedtls_sha384_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100596{
597 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
598 output ) );
599}
600
Hanno Becker7a78fe42019-09-04 15:41:21 +0100601MBEDTLS_MD_WRAPPER int mbedtls_sha384_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100602 unsigned char *output )
603{
604 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
605}
606
Hanno Becker7a78fe42019-09-04 15:41:21 +0100607MBEDTLS_MD_WRAPPER void* mbedtls_sha384_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100608{
609 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
610
611 if( ctx != NULL )
612 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
613
614 return( ctx );
615}
616
Hanno Becker7a78fe42019-09-04 15:41:21 +0100617MBEDTLS_MD_WRAPPER void mbedtls_sha384_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100618{
619 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
620 mbedtls_free( ctx );
621}
622
Hanno Becker7a78fe42019-09-04 15:41:21 +0100623MBEDTLS_MD_WRAPPER void mbedtls_sha384_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100624{
625 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
626 (const mbedtls_sha512_context *) src );
627}
628
Hanno Becker7a78fe42019-09-04 15:41:21 +0100629MBEDTLS_MD_WRAPPER int mbedtls_sha384_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100630{
631 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
632 data ) );
633}
634
Hanno Becker7a78fe42019-09-04 15:41:21 +0100635MBEDTLS_MD_WRAPPER int mbedtls_sha512_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100636{
637 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
638}
639
Hanno Becker7a78fe42019-09-04 15:41:21 +0100640MBEDTLS_MD_WRAPPER int mbedtls_sha512_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100641 unsigned char *output )
642{
643 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
644}
645
646#endif /* MBEDTLS_SHA512_C */
647
648/*
649 * Getter functions for MD info structure.
650 */
651
652#if !defined(MBEDTLS_MD_SINGLE_HASH)
653
654MBEDTLS_ALWAYS_INLINE static inline mbedtls_md_type_t mbedtls_md_info_type(
655 mbedtls_md_handle_t info )
656{
657 return( info->type );
658}
659
660MBEDTLS_ALWAYS_INLINE static inline const char * mbedtls_md_info_name(
661 mbedtls_md_handle_t info )
662{
663 return( info->name );
664}
665
666MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_size(
667 mbedtls_md_handle_t info )
668{
669 return( info->size );
670}
671
672MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_block_size(
673 mbedtls_md_handle_t info )
674{
675 return( info->block_size );
676}
677
678MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_starts(
679 mbedtls_md_handle_t info,
680 void *ctx )
681{
682 return( info->starts_func( ctx ) );
683}
684
685MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_update(
686 mbedtls_md_handle_t info,
687 void *ctx,
688 const unsigned char *input,
689 size_t ilen )
690{
691 return( info->update_func( ctx, input, ilen ) );
692}
693
694MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_finish(
695 mbedtls_md_handle_t info,
696 void *ctx,
697 unsigned char *output )
698{
699 return( info->finish_func( ctx, output ) );
700}
701
702MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_digest(
703 mbedtls_md_handle_t info,
704 const unsigned char *input,
705 size_t ilen,
706 unsigned char *output )
707{
708 return( info->digest_func( input, ilen, output ) );
709}
710
711MBEDTLS_ALWAYS_INLINE static inline void* mbedtls_md_info_ctx_alloc(
712 mbedtls_md_handle_t info )
713{
714 return( info->ctx_alloc_func() );
715}
716
717MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_ctx_free(
718 mbedtls_md_handle_t info,
719 void *ctx )
720{
721 info->ctx_free_func( ctx );
722}
723
724MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_clone(
725 mbedtls_md_handle_t info,
726 void *dst,
727 const void *src )
728{
729 info->clone_func( dst, src );
730}
731
732MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_process(
733 mbedtls_md_handle_t info,
734 void *ctx,
735 const unsigned char *input )
736{
737 return( info->process_func( ctx, input ) );
738}
739
740#else /* !MBEDTLS_MD_SINGLE_HASH */
741
742MBEDTLS_ALWAYS_INLINE static inline mbedtls_md_type_t mbedtls_md_info_type(
743 mbedtls_md_handle_t info )
744{
745 ((void) info);
746 return( MBEDTLS_MD_INFO_TYPE( MBEDTLS_MD_SINGLE_HASH ) );
747}
748
749MBEDTLS_ALWAYS_INLINE static inline const char * mbedtls_md_info_name(
750 mbedtls_md_handle_t info )
751{
752 ((void) info);
753 return( MBEDTLS_MD_INFO_NAME( MBEDTLS_MD_SINGLE_HASH ) );
754}
755
756MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_size(
757 mbedtls_md_handle_t info )
758{
759 ((void) info);
760 return( MBEDTLS_MD_INFO_SIZE( MBEDTLS_MD_SINGLE_HASH ) );
761}
762
763MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_block_size(
764 mbedtls_md_handle_t info )
765{
766 ((void) info);
767 return( MBEDTLS_MD_INFO_BLOCKSIZE( MBEDTLS_MD_SINGLE_HASH ) );
768}
769
770MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_starts(
771 mbedtls_md_handle_t info,
772 void *ctx )
773{
774 ((void) info);
775 return( MBEDTLS_MD_INFO_STARTS_FUNC( MBEDTLS_MD_SINGLE_HASH )( ctx ) );
776}
777
778MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_update(
779 mbedtls_md_handle_t info,
780 void *ctx,
781 const unsigned char *input,
782 size_t ilen )
783{
784 ((void) info);
785 return( MBEDTLS_MD_INFO_UPDATE_FUNC( MBEDTLS_MD_SINGLE_HASH )
786 ( ctx, input, ilen ) );
787}
788
Hanno Becker4a997652019-09-05 11:55:25 +0100789MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_init(
790 mbedtls_md_handle_t info,
791 void *ctx )
792{
793 ((void) info);
794 MBEDTLS_MD_INFO_INIT_FUNC( MBEDTLS_MD_SINGLE_HASH )( ctx );
795}
796
Hanno Beckerc4e42102019-09-04 12:43:22 +0100797MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_finish(
798 mbedtls_md_handle_t info,
799 void *ctx,
800 unsigned char *output )
801{
802 ((void) info);
803 return( MBEDTLS_MD_INFO_FINISH_FUNC( MBEDTLS_MD_SINGLE_HASH )
804 ( ctx, output ) );
805}
806
807MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_digest(
808 mbedtls_md_handle_t info,
809 const unsigned char *input,
810 size_t ilen,
811 unsigned char *output )
812{
813 ((void) info);
814 return( MBEDTLS_MD_INFO_DIGEST_FUNC( MBEDTLS_MD_SINGLE_HASH )
815 ( input, ilen, output ) );
816}
817
818MBEDTLS_ALWAYS_INLINE static inline void* mbedtls_md_info_ctx_alloc(
819 mbedtls_md_handle_t info )
820{
821 ((void) info);
822 return( MBEDTLS_MD_INFO_ALLOC_FUNC( MBEDTLS_MD_SINGLE_HASH )() );
823}
824
825MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_ctx_free(
826 mbedtls_md_handle_t info,
827 void *ctx )
828{
829 ((void) info);
830 MBEDTLS_MD_INFO_FREE_FUNC( MBEDTLS_MD_SINGLE_HASH )( ctx );
831}
832
833MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_clone(
834 mbedtls_md_handle_t info,
835 void *dst,
836 const void *src )
837{
838 ((void) info);
839 MBEDTLS_MD_INFO_CLONE_FUNC( MBEDTLS_MD_SINGLE_HASH )( dst, src );
840}
841
842MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_process(
843 mbedtls_md_handle_t info,
844 void *ctx,
845 const unsigned char *input )
846{
847 ((void) info);
848 return( MBEDTLS_MD_INFO_PROCESS_FUNC( MBEDTLS_MD_SINGLE_HASH )
849 ( ctx, input ) );
850}
851
852#endif /* MBEDTLS_MD_SINGLE_HASH */
853
854#ifdef __cplusplus
855}
856#endif
857
858#endif /* MBEDTLS_MD_INTERNAL_H */