blob: 4c08f850104877345d048ab40b2ad47a16c0de86 [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 */
82#define MBEDTLS_MD_INFO_SHA256_TYPE MBEDTLS_MD_SHA256
Hanno Becker6deddf72019-09-05 11:17:30 +010083#define MBEDTLS_MD_INFO_SHA256_CTX_TYPE mbedtls_sha256_context
Hanno Beckerc4e42102019-09-04 12:43:22 +010084#define MBEDTLS_MD_INFO_SHA256_NAME "SHA256"
85#define MBEDTLS_MD_INFO_SHA256_SIZE 32
86#define MBEDTLS_MD_INFO_SHA256_BLOCKSIZE 64
87#define MBEDTLS_MD_INFO_SHA256_STARTS_FUNC mbedtls_sha256_starts_wrap
88#define MBEDTLS_MD_INFO_SHA256_UPDATE_FUNC mbedtls_sha224_update_wrap
89#define MBEDTLS_MD_INFO_SHA256_FINISH_FUNC mbedtls_sha224_finish_wrap
90#define MBEDTLS_MD_INFO_SHA256_DIGEST_FUNC mbedtls_sha256_wrap
91#define MBEDTLS_MD_INFO_SHA256_ALLOC_FUNC mbedtls_sha224_ctx_alloc
92#define MBEDTLS_MD_INFO_SHA256_FREE_FUNC mbedtls_sha224_ctx_free
93#define MBEDTLS_MD_INFO_SHA256_CLONE_FUNC mbedtls_sha224_clone_wrap
94#define MBEDTLS_MD_INFO_SHA256_PROCESS_FUNC mbedtls_sha224_process_wrap
95
96/*
97 * Helper macros to extract fields from ciphersuites.
98 */
99
Hanno Becker6deddf72019-09-05 11:17:30 +0100100#define MBEDTLS_MD_INFO_CTX_TYPE_T( MD ) MD ## _CTX_TYPE
Hanno Beckerc4e42102019-09-04 12:43:22 +0100101#define MBEDTLS_MD_INFO_TYPE_T( MD ) MD ## _TYPE
102#define MBEDTLS_MD_INFO_NAME_T( MD ) MD ## _NAME
103#define MBEDTLS_MD_INFO_SIZE_T( MD ) MD ## _SIZE
104#define MBEDTLS_MD_INFO_BLOCKSIZE_T( MD ) MD ## _BLOCKSIZE
105#define MBEDTLS_MD_INFO_STARTS_FUNC_T( MD ) MD ## _STARTS_FUNC
106#define MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD ) MD ## _UPDATE_FUNC
107#define MBEDTLS_MD_INFO_FINISH_FUNC_T( MD ) MD ## _FINISH_FUNC
108#define MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD ) MD ## _DIGEST_FUNC
109#define MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD ) MD ## _ALLOC_FUNC
110#define MBEDTLS_MD_INFO_FREE_FUNC_T( MD ) MD ## _FREE_FUNC
111#define MBEDTLS_MD_INFO_CLONE_FUNC_T( MD ) MD ## _CLONE_FUNC
112#define MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD ) MD ## _PROCESS_FUNC
113
114/* Wrapper around MBEDTLS_MD_INFO_XXX_T() which makes sure that
115 * the argument is macro-expanded before concatenated with the
116 * field name. This allows to call these macros as
117 * MBEDTLS_MD_INFO_XXX( MBEDTLS_MD_SINGLE_HASH ).
118 * where MBEDTLS_MD_SINGLE_HASH expands to MBEDTLS_MD_INFO_XXX. */
Hanno Becker6deddf72019-09-05 11:17:30 +0100119#define MBEDTLS_MD_INFO_CTX_TYPE( MD ) MBEDTLS_MD_INFO_CTX_TYPE_T( MD )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100120#define MBEDTLS_MD_INFO_TYPE( MD ) MBEDTLS_MD_INFO_TYPE_T( MD )
121#define MBEDTLS_MD_INFO_NAME( MD ) MBEDTLS_MD_INFO_NAME_T( MD )
122#define MBEDTLS_MD_INFO_SIZE( MD ) MBEDTLS_MD_INFO_SIZE_T( MD )
123#define MBEDTLS_MD_INFO_BLOCKSIZE( MD ) MBEDTLS_MD_INFO_BLOCKSIZE_T( MD )
124#define MBEDTLS_MD_INFO_STARTS_FUNC( MD ) MBEDTLS_MD_INFO_STARTS_FUNC_T( MD )
125#define MBEDTLS_MD_INFO_UPDATE_FUNC( MD ) MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD )
126#define MBEDTLS_MD_INFO_FINISH_FUNC( MD ) MBEDTLS_MD_INFO_FINISH_FUNC_T( MD )
127#define MBEDTLS_MD_INFO_DIGEST_FUNC( MD ) MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD )
128#define MBEDTLS_MD_INFO_ALLOC_FUNC( MD ) MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD )
129#define MBEDTLS_MD_INFO_FREE_FUNC( MD ) MBEDTLS_MD_INFO_FREE_FUNC_T( MD )
130#define MBEDTLS_MD_INFO_CLONE_FUNC( MD ) MBEDTLS_MD_INFO_CLONE_FUNC_T( MD )
131#define MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD )
132
133/**
134 * Message digest information.
135 * Allows message digest functions to be called in a generic way.
136 */
137
138typedef int mbedtls_md_starts_func_t( void *ctx );
139typedef int mbedtls_md_update_func_t( void *ctx,
140 const unsigned char *input,
141 size_t ilen );
142typedef int mbedtls_md_finish_func_t( void *ctx, unsigned char *output );
143typedef int mbedtls_md_digest_func_t( const unsigned char *input,
144 size_t ilen,
145 unsigned char *output );
146typedef void* mbedtls_md_ctx_alloc_func_t( void );
147typedef void mbedtls_md_ctx_free_func_t( void *ctx );
148typedef void mbedtls_md_clone_func_t( void *st, const void *src );
149typedef int mbedtls_md_process_func_t( void *ctx,
150 const unsigned char *input );
151
152#if !defined(MBEDTLS_MD_SINGLE_HASH)
153struct mbedtls_md_info_t
154{
155 /** Digest identifier */
156 mbedtls_md_type_t type;
157
158 /** Name of the message digest */
159 const char * name;
160
161 /** Output length of the digest function in bytes */
162 int size;
163
164 /** Block length of the digest function in bytes */
165 int block_size;
166
167 /** Digest initialisation function */
168 mbedtls_md_starts_func_t *starts_func;
169
170 /** Digest update function */
171 mbedtls_md_update_func_t *update_func;
172
173 /** Digest finalisation function */
174 mbedtls_md_finish_func_t *finish_func;
175
176 /** Generic digest function */
177 mbedtls_md_digest_func_t *digest_func;
178
179 /** Allocate a new context */
180 mbedtls_md_ctx_alloc_func_t *ctx_alloc_func;
181
182 /** Free the given context */
183 mbedtls_md_ctx_free_func_t *ctx_free_func;
184
185 /** Clone state from a context */
186 mbedtls_md_clone_func_t *clone_func;
187
188 /** Internal use only */
189 mbedtls_md_process_func_t *process_func;
190};
191
192/**
193 * \brief This macro builds an instance of ::mbedtls_md_info_t
194 * from an \c MBEDTLS_MD_INFO_XXX identifier.
195 */
196#define MBEDTLS_MD_INFO( MD ) \
197 { MBEDTLS_MD_INFO_TYPE( MD ), \
198 MBEDTLS_MD_INFO_NAME( MD ), \
199 MBEDTLS_MD_INFO_SIZE( MD ), \
200 MBEDTLS_MD_INFO_BLOCKSIZE( MD ), \
201 MBEDTLS_MD_INFO_STARTS_FUNC( MD ), \
202 MBEDTLS_MD_INFO_UPDATE_FUNC( MD ), \
203 MBEDTLS_MD_INFO_FINISH_FUNC( MD ), \
204 MBEDTLS_MD_INFO_DIGEST_FUNC( MD ), \
205 MBEDTLS_MD_INFO_ALLOC_FUNC( MD ), \
206 MBEDTLS_MD_INFO_FREE_FUNC( MD ), \
207 MBEDTLS_MD_INFO_CLONE_FUNC( MD ), \
208 MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) }
209
210#endif /* !MBEDTLS_MD_SINGLE_HASH */
211
212/*
213 *
214 * Definitions of MD information structures for various digests.
215 *
216 */
217
218/*
219 * MD-2
220 */
221
222#if defined(MBEDTLS_MD2_C)
223
Hanno Becker7a78fe42019-09-04 15:41:21 +0100224MBEDTLS_MD_WRAPPER int mbedtls_md2_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100225{
226 return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
227}
228
Hanno Becker7a78fe42019-09-04 15:41:21 +0100229MBEDTLS_MD_WRAPPER int mbedtls_md2_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100230 size_t ilen )
231{
232 return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
233}
234
Hanno Becker7a78fe42019-09-04 15:41:21 +0100235MBEDTLS_MD_WRAPPER int mbedtls_md2_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100236{
237 return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
238}
239
Hanno Becker7a78fe42019-09-04 15:41:21 +0100240MBEDTLS_MD_WRAPPER void* mbedtls_md2_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100241{
242 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
243
244 if( ctx != NULL )
245 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
246
247 return( ctx );
248}
249
Hanno Becker7a78fe42019-09-04 15:41:21 +0100250MBEDTLS_MD_WRAPPER void mbedtls_md2_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100251{
252 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
253 mbedtls_free( ctx );
254}
255
Hanno Becker7a78fe42019-09-04 15:41:21 +0100256MBEDTLS_MD_WRAPPER void mbedtls_md2_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100257{
258 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
259 (const mbedtls_md2_context *) src );
260}
261
Hanno Becker7a78fe42019-09-04 15:41:21 +0100262MBEDTLS_MD_WRAPPER int mbedtls_md2_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100263{
264 ((void) data);
265
266 return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) );
267}
268
269#endif /* MBEDTLS_MD2_C */
270
271/*
272 * MD-4
273 */
274
275#if defined(MBEDTLS_MD4_C)
276
Hanno Becker7a78fe42019-09-04 15:41:21 +0100277MBEDTLS_MD_WRAPPER int mbedtls_md4_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100278{
279 return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
280}
281
Hanno Becker7a78fe42019-09-04 15:41:21 +0100282MBEDTLS_MD_WRAPPER int mbedtls_md4_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100283 size_t ilen )
284{
285 return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
286}
287
Hanno Becker7a78fe42019-09-04 15:41:21 +0100288MBEDTLS_MD_WRAPPER int mbedtls_md4_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100289{
290 return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
291}
292
Hanno Becker7a78fe42019-09-04 15:41:21 +0100293MBEDTLS_MD_WRAPPER void* mbedtls_md4_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100294{
295 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
296
297 if( ctx != NULL )
298 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
299
300 return( ctx );
301}
302
Hanno Becker7a78fe42019-09-04 15:41:21 +0100303MBEDTLS_MD_WRAPPER void mbedtls_md4_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100304{
305 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
306 mbedtls_free( ctx );
307}
308
Hanno Becker7a78fe42019-09-04 15:41:21 +0100309MBEDTLS_MD_WRAPPER void mbedtls_md4_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100310{
311 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
312 (const mbedtls_md4_context *) src );
313}
314
Hanno Becker7a78fe42019-09-04 15:41:21 +0100315MBEDTLS_MD_WRAPPER int mbedtls_md4_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100316{
317 return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
318}
319
320#endif /* MBEDTLS_MD4_C */
321
322/*
323 * MD-5
324 */
325
326#if defined(MBEDTLS_MD5_C)
327
Hanno Becker7a78fe42019-09-04 15:41:21 +0100328MBEDTLS_MD_WRAPPER int mbedtls_md5_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100329{
330 return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
331}
332
Hanno Becker7a78fe42019-09-04 15:41:21 +0100333MBEDTLS_MD_WRAPPER int mbedtls_md5_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100334 size_t ilen )
335{
336 return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
337}
338
Hanno Becker7a78fe42019-09-04 15:41:21 +0100339MBEDTLS_MD_WRAPPER int mbedtls_md5_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100340{
341 return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
342}
343
Hanno Becker7a78fe42019-09-04 15:41:21 +0100344MBEDTLS_MD_WRAPPER void* mbedtls_md5_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100345{
346 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
347
348 if( ctx != NULL )
349 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
350
351 return( ctx );
352}
353
Hanno Becker7a78fe42019-09-04 15:41:21 +0100354MBEDTLS_MD_WRAPPER void mbedtls_md5_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100355{
356 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
357 mbedtls_free( ctx );
358}
359
Hanno Becker7a78fe42019-09-04 15:41:21 +0100360MBEDTLS_MD_WRAPPER void mbedtls_md5_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100361{
362 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
363 (const mbedtls_md5_context *) src );
364}
365
Hanno Becker7a78fe42019-09-04 15:41:21 +0100366MBEDTLS_MD_WRAPPER int mbedtls_md5_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100367{
368 return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
369}
370
371#endif /* MBEDTLS_MD5_C */
372
373/*
374 * RIPEMD-160
375 */
376
377#if defined(MBEDTLS_RIPEMD160_C)
378
Hanno Becker7a78fe42019-09-04 15:41:21 +0100379MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100380{
381 return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
382}
383
Hanno Becker7a78fe42019-09-04 15:41:21 +0100384MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100385 size_t ilen )
386{
387 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
388 input, ilen ) );
389}
390
Hanno Becker7a78fe42019-09-04 15:41:21 +0100391MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100392{
393 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
394 output ) );
395}
396
Hanno Becker7a78fe42019-09-04 15:41:21 +0100397MBEDTLS_MD_WRAPPER void* mbedtls_ripemd160_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100398{
399 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
400
401 if( ctx != NULL )
402 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
403
404 return( ctx );
405}
406
Hanno Becker7a78fe42019-09-04 15:41:21 +0100407MBEDTLS_MD_WRAPPER void mbedtls_ripemd160_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100408{
409 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
410 mbedtls_free( ctx );
411}
412
Hanno Becker7a78fe42019-09-04 15:41:21 +0100413MBEDTLS_MD_WRAPPER void mbedtls_ripemd160_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100414{
415 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
416 (const mbedtls_ripemd160_context *) src );
417}
418
Hanno Becker7a78fe42019-09-04 15:41:21 +0100419MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100420{
421 return( mbedtls_internal_ripemd160_process(
422 (mbedtls_ripemd160_context *) ctx, data ) );
423}
424
425#endif /* MBEDTLS_RIPEMD160_C */
426
427/*
428 * SHA-1
429 */
430
431#if defined(MBEDTLS_SHA1_C)
432
Hanno Becker7a78fe42019-09-04 15:41:21 +0100433MBEDTLS_MD_WRAPPER int mbedtls_sha1_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100434{
435 return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
436}
437
Hanno Becker7a78fe42019-09-04 15:41:21 +0100438MBEDTLS_MD_WRAPPER int mbedtls_sha1_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100439 size_t ilen )
440{
441 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
442 input, ilen ) );
443}
444
Hanno Becker7a78fe42019-09-04 15:41:21 +0100445MBEDTLS_MD_WRAPPER int mbedtls_sha1_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100446{
447 return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
448}
449
Hanno Becker7a78fe42019-09-04 15:41:21 +0100450MBEDTLS_MD_WRAPPER void* mbedtls_sha1_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100451{
452 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
453
454 if( ctx != NULL )
455 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
456
457 return( ctx );
458}
459
Hanno Becker7a78fe42019-09-04 15:41:21 +0100460MBEDTLS_MD_WRAPPER void mbedtls_sha1_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100461{
462 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
463 (const mbedtls_sha1_context *) src );
464}
465
Hanno Becker7a78fe42019-09-04 15:41:21 +0100466MBEDTLS_MD_WRAPPER void mbedtls_sha1_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100467{
468 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
469 mbedtls_free( ctx );
470}
471
Hanno Becker7a78fe42019-09-04 15:41:21 +0100472MBEDTLS_MD_WRAPPER int mbedtls_sha1_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100473{
474 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
475 data ) );
476}
477
478#endif /* MBEDTLS_SHA1_C */
479
480/*
481 * SHA-224 and SHA-256
482 */
483
484#if defined(MBEDTLS_SHA256_C)
485
486#if !defined(MBEDTLS_SHA256_NO_SHA224)
Hanno Becker7a78fe42019-09-04 15:41:21 +0100487MBEDTLS_MD_WRAPPER int mbedtls_sha224_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100488{
489 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
490}
491#endif /* !MBEDTLS_SHA256_NO_SHA224 */
492
Hanno Becker7a78fe42019-09-04 15:41:21 +0100493MBEDTLS_MD_WRAPPER int mbedtls_sha224_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100494 size_t ilen )
495{
496 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
497 input, ilen ) );
498}
499
Hanno Becker7a78fe42019-09-04 15:41:21 +0100500MBEDTLS_MD_WRAPPER int mbedtls_sha224_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100501{
502 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
503 output ) );
504}
505
506#if !defined(MBEDTLS_SHA256_NO_SHA224)
Hanno Becker7a78fe42019-09-04 15:41:21 +0100507MBEDTLS_MD_WRAPPER int mbedtls_sha224_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100508 unsigned char *output )
509{
510 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
511}
512#endif /* !MBEDTLS_SHA256_NO_SHA224 */
513
Hanno Becker7a78fe42019-09-04 15:41:21 +0100514MBEDTLS_MD_WRAPPER void* mbedtls_sha224_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100515{
516 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
517
518 if( ctx != NULL )
519 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
520
521 return( ctx );
522}
523
Hanno Becker7a78fe42019-09-04 15:41:21 +0100524MBEDTLS_MD_WRAPPER void mbedtls_sha224_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100525{
526 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
527 mbedtls_free( ctx );
528}
529
Hanno Becker7a78fe42019-09-04 15:41:21 +0100530MBEDTLS_MD_WRAPPER void mbedtls_sha224_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100531{
532 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
533 (const mbedtls_sha256_context *) src );
534}
535
Hanno Becker7a78fe42019-09-04 15:41:21 +0100536MBEDTLS_MD_WRAPPER int mbedtls_sha224_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100537{
538 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
539 data ) );
540}
541
Hanno Becker7a78fe42019-09-04 15:41:21 +0100542MBEDTLS_MD_WRAPPER int mbedtls_sha256_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100543{
544 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
545}
546
Hanno Becker7a78fe42019-09-04 15:41:21 +0100547MBEDTLS_MD_WRAPPER int mbedtls_sha256_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100548 unsigned char *output )
549{
550 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
551}
552
553#endif /* MBEDTLS_SHA256_C */
554
555/*
556 * SHA-384 and SHA-512
557 */
558
559#if defined(MBEDTLS_SHA512_C)
560
Hanno Becker7a78fe42019-09-04 15:41:21 +0100561MBEDTLS_MD_WRAPPER int mbedtls_sha384_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100562{
563 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
564}
565
Hanno Becker7a78fe42019-09-04 15:41:21 +0100566MBEDTLS_MD_WRAPPER int mbedtls_sha384_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100567 size_t ilen )
568{
569 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
570 input, ilen ) );
571}
572
Hanno Becker7a78fe42019-09-04 15:41:21 +0100573MBEDTLS_MD_WRAPPER int mbedtls_sha384_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100574{
575 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
576 output ) );
577}
578
Hanno Becker7a78fe42019-09-04 15:41:21 +0100579MBEDTLS_MD_WRAPPER int mbedtls_sha384_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100580 unsigned char *output )
581{
582 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
583}
584
Hanno Becker7a78fe42019-09-04 15:41:21 +0100585MBEDTLS_MD_WRAPPER void* mbedtls_sha384_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100586{
587 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
588
589 if( ctx != NULL )
590 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
591
592 return( ctx );
593}
594
Hanno Becker7a78fe42019-09-04 15:41:21 +0100595MBEDTLS_MD_WRAPPER void mbedtls_sha384_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100596{
597 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
598 mbedtls_free( ctx );
599}
600
Hanno Becker7a78fe42019-09-04 15:41:21 +0100601MBEDTLS_MD_WRAPPER void mbedtls_sha384_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100602{
603 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
604 (const mbedtls_sha512_context *) src );
605}
606
Hanno Becker7a78fe42019-09-04 15:41:21 +0100607MBEDTLS_MD_WRAPPER int mbedtls_sha384_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100608{
609 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
610 data ) );
611}
612
Hanno Becker7a78fe42019-09-04 15:41:21 +0100613MBEDTLS_MD_WRAPPER int mbedtls_sha512_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100614{
615 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
616}
617
Hanno Becker7a78fe42019-09-04 15:41:21 +0100618MBEDTLS_MD_WRAPPER int mbedtls_sha512_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100619 unsigned char *output )
620{
621 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
622}
623
624#endif /* MBEDTLS_SHA512_C */
625
626/*
627 * Getter functions for MD info structure.
628 */
629
630#if !defined(MBEDTLS_MD_SINGLE_HASH)
631
632MBEDTLS_ALWAYS_INLINE static inline mbedtls_md_type_t mbedtls_md_info_type(
633 mbedtls_md_handle_t info )
634{
635 return( info->type );
636}
637
638MBEDTLS_ALWAYS_INLINE static inline const char * mbedtls_md_info_name(
639 mbedtls_md_handle_t info )
640{
641 return( info->name );
642}
643
644MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_size(
645 mbedtls_md_handle_t info )
646{
647 return( info->size );
648}
649
650MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_block_size(
651 mbedtls_md_handle_t info )
652{
653 return( info->block_size );
654}
655
656MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_starts(
657 mbedtls_md_handle_t info,
658 void *ctx )
659{
660 return( info->starts_func( ctx ) );
661}
662
663MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_update(
664 mbedtls_md_handle_t info,
665 void *ctx,
666 const unsigned char *input,
667 size_t ilen )
668{
669 return( info->update_func( ctx, input, ilen ) );
670}
671
672MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_finish(
673 mbedtls_md_handle_t info,
674 void *ctx,
675 unsigned char *output )
676{
677 return( info->finish_func( ctx, output ) );
678}
679
680MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_digest(
681 mbedtls_md_handle_t info,
682 const unsigned char *input,
683 size_t ilen,
684 unsigned char *output )
685{
686 return( info->digest_func( input, ilen, output ) );
687}
688
689MBEDTLS_ALWAYS_INLINE static inline void* mbedtls_md_info_ctx_alloc(
690 mbedtls_md_handle_t info )
691{
692 return( info->ctx_alloc_func() );
693}
694
695MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_ctx_free(
696 mbedtls_md_handle_t info,
697 void *ctx )
698{
699 info->ctx_free_func( ctx );
700}
701
702MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_clone(
703 mbedtls_md_handle_t info,
704 void *dst,
705 const void *src )
706{
707 info->clone_func( dst, src );
708}
709
710MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_process(
711 mbedtls_md_handle_t info,
712 void *ctx,
713 const unsigned char *input )
714{
715 return( info->process_func( ctx, input ) );
716}
717
718#else /* !MBEDTLS_MD_SINGLE_HASH */
719
720MBEDTLS_ALWAYS_INLINE static inline mbedtls_md_type_t mbedtls_md_info_type(
721 mbedtls_md_handle_t info )
722{
723 ((void) info);
724 return( MBEDTLS_MD_INFO_TYPE( MBEDTLS_MD_SINGLE_HASH ) );
725}
726
727MBEDTLS_ALWAYS_INLINE static inline const char * mbedtls_md_info_name(
728 mbedtls_md_handle_t info )
729{
730 ((void) info);
731 return( MBEDTLS_MD_INFO_NAME( MBEDTLS_MD_SINGLE_HASH ) );
732}
733
734MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_size(
735 mbedtls_md_handle_t info )
736{
737 ((void) info);
738 return( MBEDTLS_MD_INFO_SIZE( MBEDTLS_MD_SINGLE_HASH ) );
739}
740
741MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_block_size(
742 mbedtls_md_handle_t info )
743{
744 ((void) info);
745 return( MBEDTLS_MD_INFO_BLOCKSIZE( MBEDTLS_MD_SINGLE_HASH ) );
746}
747
748MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_starts(
749 mbedtls_md_handle_t info,
750 void *ctx )
751{
752 ((void) info);
753 return( MBEDTLS_MD_INFO_STARTS_FUNC( MBEDTLS_MD_SINGLE_HASH )( ctx ) );
754}
755
756MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_update(
757 mbedtls_md_handle_t info,
758 void *ctx,
759 const unsigned char *input,
760 size_t ilen )
761{
762 ((void) info);
763 return( MBEDTLS_MD_INFO_UPDATE_FUNC( MBEDTLS_MD_SINGLE_HASH )
764 ( ctx, input, ilen ) );
765}
766
767MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_finish(
768 mbedtls_md_handle_t info,
769 void *ctx,
770 unsigned char *output )
771{
772 ((void) info);
773 return( MBEDTLS_MD_INFO_FINISH_FUNC( MBEDTLS_MD_SINGLE_HASH )
774 ( ctx, output ) );
775}
776
777MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_digest(
778 mbedtls_md_handle_t info,
779 const unsigned char *input,
780 size_t ilen,
781 unsigned char *output )
782{
783 ((void) info);
784 return( MBEDTLS_MD_INFO_DIGEST_FUNC( MBEDTLS_MD_SINGLE_HASH )
785 ( input, ilen, output ) );
786}
787
788MBEDTLS_ALWAYS_INLINE static inline void* mbedtls_md_info_ctx_alloc(
789 mbedtls_md_handle_t info )
790{
791 ((void) info);
792 return( MBEDTLS_MD_INFO_ALLOC_FUNC( MBEDTLS_MD_SINGLE_HASH )() );
793}
794
795MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_ctx_free(
796 mbedtls_md_handle_t info,
797 void *ctx )
798{
799 ((void) info);
800 MBEDTLS_MD_INFO_FREE_FUNC( MBEDTLS_MD_SINGLE_HASH )( ctx );
801}
802
803MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_clone(
804 mbedtls_md_handle_t info,
805 void *dst,
806 const void *src )
807{
808 ((void) info);
809 MBEDTLS_MD_INFO_CLONE_FUNC( MBEDTLS_MD_SINGLE_HASH )( dst, src );
810}
811
812MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_process(
813 mbedtls_md_handle_t info,
814 void *ctx,
815 const unsigned char *input )
816{
817 ((void) info);
818 return( MBEDTLS_MD_INFO_PROCESS_FUNC( MBEDTLS_MD_SINGLE_HASH )
819 ( ctx, input ) );
820}
821
822#endif /* MBEDTLS_MD_SINGLE_HASH */
823
824#ifdef __cplusplus
825}
826#endif
827
828#endif /* MBEDTLS_MD_INTERNAL_H */