blob: 53742fd9a920efba797d88ffd68ae6a08bd4a20f [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
83#define MBEDTLS_MD_INFO_SHA256_NAME "SHA256"
84#define MBEDTLS_MD_INFO_SHA256_SIZE 32
85#define MBEDTLS_MD_INFO_SHA256_BLOCKSIZE 64
86#define MBEDTLS_MD_INFO_SHA256_STARTS_FUNC mbedtls_sha256_starts_wrap
87#define MBEDTLS_MD_INFO_SHA256_UPDATE_FUNC mbedtls_sha224_update_wrap
88#define MBEDTLS_MD_INFO_SHA256_FINISH_FUNC mbedtls_sha224_finish_wrap
89#define MBEDTLS_MD_INFO_SHA256_DIGEST_FUNC mbedtls_sha256_wrap
90#define MBEDTLS_MD_INFO_SHA256_ALLOC_FUNC mbedtls_sha224_ctx_alloc
91#define MBEDTLS_MD_INFO_SHA256_FREE_FUNC mbedtls_sha224_ctx_free
92#define MBEDTLS_MD_INFO_SHA256_CLONE_FUNC mbedtls_sha224_clone_wrap
93#define MBEDTLS_MD_INFO_SHA256_PROCESS_FUNC mbedtls_sha224_process_wrap
94
95/*
96 * Helper macros to extract fields from ciphersuites.
97 */
98
99#define MBEDTLS_MD_INFO_TYPE_T( MD ) MD ## _TYPE
100#define MBEDTLS_MD_INFO_NAME_T( MD ) MD ## _NAME
101#define MBEDTLS_MD_INFO_SIZE_T( MD ) MD ## _SIZE
102#define MBEDTLS_MD_INFO_BLOCKSIZE_T( MD ) MD ## _BLOCKSIZE
103#define MBEDTLS_MD_INFO_STARTS_FUNC_T( MD ) MD ## _STARTS_FUNC
104#define MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD ) MD ## _UPDATE_FUNC
105#define MBEDTLS_MD_INFO_FINISH_FUNC_T( MD ) MD ## _FINISH_FUNC
106#define MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD ) MD ## _DIGEST_FUNC
107#define MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD ) MD ## _ALLOC_FUNC
108#define MBEDTLS_MD_INFO_FREE_FUNC_T( MD ) MD ## _FREE_FUNC
109#define MBEDTLS_MD_INFO_CLONE_FUNC_T( MD ) MD ## _CLONE_FUNC
110#define MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD ) MD ## _PROCESS_FUNC
111
112/* Wrapper around MBEDTLS_MD_INFO_XXX_T() which makes sure that
113 * the argument is macro-expanded before concatenated with the
114 * field name. This allows to call these macros as
115 * MBEDTLS_MD_INFO_XXX( MBEDTLS_MD_SINGLE_HASH ).
116 * where MBEDTLS_MD_SINGLE_HASH expands to MBEDTLS_MD_INFO_XXX. */
117#define MBEDTLS_MD_INFO_TYPE( MD ) MBEDTLS_MD_INFO_TYPE_T( MD )
118#define MBEDTLS_MD_INFO_NAME( MD ) MBEDTLS_MD_INFO_NAME_T( MD )
119#define MBEDTLS_MD_INFO_SIZE( MD ) MBEDTLS_MD_INFO_SIZE_T( MD )
120#define MBEDTLS_MD_INFO_BLOCKSIZE( MD ) MBEDTLS_MD_INFO_BLOCKSIZE_T( MD )
121#define MBEDTLS_MD_INFO_STARTS_FUNC( MD ) MBEDTLS_MD_INFO_STARTS_FUNC_T( MD )
122#define MBEDTLS_MD_INFO_UPDATE_FUNC( MD ) MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD )
123#define MBEDTLS_MD_INFO_FINISH_FUNC( MD ) MBEDTLS_MD_INFO_FINISH_FUNC_T( MD )
124#define MBEDTLS_MD_INFO_DIGEST_FUNC( MD ) MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD )
125#define MBEDTLS_MD_INFO_ALLOC_FUNC( MD ) MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD )
126#define MBEDTLS_MD_INFO_FREE_FUNC( MD ) MBEDTLS_MD_INFO_FREE_FUNC_T( MD )
127#define MBEDTLS_MD_INFO_CLONE_FUNC( MD ) MBEDTLS_MD_INFO_CLONE_FUNC_T( MD )
128#define MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD )
129
130/**
131 * Message digest information.
132 * Allows message digest functions to be called in a generic way.
133 */
134
135typedef int mbedtls_md_starts_func_t( void *ctx );
136typedef int mbedtls_md_update_func_t( void *ctx,
137 const unsigned char *input,
138 size_t ilen );
139typedef int mbedtls_md_finish_func_t( void *ctx, unsigned char *output );
140typedef int mbedtls_md_digest_func_t( const unsigned char *input,
141 size_t ilen,
142 unsigned char *output );
143typedef void* mbedtls_md_ctx_alloc_func_t( void );
144typedef void mbedtls_md_ctx_free_func_t( void *ctx );
145typedef void mbedtls_md_clone_func_t( void *st, const void *src );
146typedef int mbedtls_md_process_func_t( void *ctx,
147 const unsigned char *input );
148
149#if !defined(MBEDTLS_MD_SINGLE_HASH)
150struct mbedtls_md_info_t
151{
152 /** Digest identifier */
153 mbedtls_md_type_t type;
154
155 /** Name of the message digest */
156 const char * name;
157
158 /** Output length of the digest function in bytes */
159 int size;
160
161 /** Block length of the digest function in bytes */
162 int block_size;
163
164 /** Digest initialisation function */
165 mbedtls_md_starts_func_t *starts_func;
166
167 /** Digest update function */
168 mbedtls_md_update_func_t *update_func;
169
170 /** Digest finalisation function */
171 mbedtls_md_finish_func_t *finish_func;
172
173 /** Generic digest function */
174 mbedtls_md_digest_func_t *digest_func;
175
176 /** Allocate a new context */
177 mbedtls_md_ctx_alloc_func_t *ctx_alloc_func;
178
179 /** Free the given context */
180 mbedtls_md_ctx_free_func_t *ctx_free_func;
181
182 /** Clone state from a context */
183 mbedtls_md_clone_func_t *clone_func;
184
185 /** Internal use only */
186 mbedtls_md_process_func_t *process_func;
187};
188
189/**
190 * \brief This macro builds an instance of ::mbedtls_md_info_t
191 * from an \c MBEDTLS_MD_INFO_XXX identifier.
192 */
193#define MBEDTLS_MD_INFO( MD ) \
194 { MBEDTLS_MD_INFO_TYPE( MD ), \
195 MBEDTLS_MD_INFO_NAME( MD ), \
196 MBEDTLS_MD_INFO_SIZE( MD ), \
197 MBEDTLS_MD_INFO_BLOCKSIZE( MD ), \
198 MBEDTLS_MD_INFO_STARTS_FUNC( MD ), \
199 MBEDTLS_MD_INFO_UPDATE_FUNC( MD ), \
200 MBEDTLS_MD_INFO_FINISH_FUNC( MD ), \
201 MBEDTLS_MD_INFO_DIGEST_FUNC( MD ), \
202 MBEDTLS_MD_INFO_ALLOC_FUNC( MD ), \
203 MBEDTLS_MD_INFO_FREE_FUNC( MD ), \
204 MBEDTLS_MD_INFO_CLONE_FUNC( MD ), \
205 MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) }
206
207#endif /* !MBEDTLS_MD_SINGLE_HASH */
208
209/*
210 *
211 * Definitions of MD information structures for various digests.
212 *
213 */
214
215/*
216 * MD-2
217 */
218
219#if defined(MBEDTLS_MD2_C)
220
Hanno Becker7a78fe42019-09-04 15:41:21 +0100221MBEDTLS_MD_WRAPPER int mbedtls_md2_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100222{
223 return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
224}
225
Hanno Becker7a78fe42019-09-04 15:41:21 +0100226MBEDTLS_MD_WRAPPER int mbedtls_md2_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100227 size_t ilen )
228{
229 return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
230}
231
Hanno Becker7a78fe42019-09-04 15:41:21 +0100232MBEDTLS_MD_WRAPPER int mbedtls_md2_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100233{
234 return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
235}
236
Hanno Becker7a78fe42019-09-04 15:41:21 +0100237MBEDTLS_MD_WRAPPER void* mbedtls_md2_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100238{
239 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
240
241 if( ctx != NULL )
242 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
243
244 return( ctx );
245}
246
Hanno Becker7a78fe42019-09-04 15:41:21 +0100247MBEDTLS_MD_WRAPPER void mbedtls_md2_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100248{
249 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
250 mbedtls_free( ctx );
251}
252
Hanno Becker7a78fe42019-09-04 15:41:21 +0100253MBEDTLS_MD_WRAPPER void mbedtls_md2_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100254{
255 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
256 (const mbedtls_md2_context *) src );
257}
258
Hanno Becker7a78fe42019-09-04 15:41:21 +0100259MBEDTLS_MD_WRAPPER int mbedtls_md2_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100260{
261 ((void) data);
262
263 return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) );
264}
265
266#endif /* MBEDTLS_MD2_C */
267
268/*
269 * MD-4
270 */
271
272#if defined(MBEDTLS_MD4_C)
273
Hanno Becker7a78fe42019-09-04 15:41:21 +0100274MBEDTLS_MD_WRAPPER int mbedtls_md4_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100275{
276 return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
277}
278
Hanno Becker7a78fe42019-09-04 15:41:21 +0100279MBEDTLS_MD_WRAPPER int mbedtls_md4_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100280 size_t ilen )
281{
282 return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
283}
284
Hanno Becker7a78fe42019-09-04 15:41:21 +0100285MBEDTLS_MD_WRAPPER int mbedtls_md4_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100286{
287 return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
288}
289
Hanno Becker7a78fe42019-09-04 15:41:21 +0100290MBEDTLS_MD_WRAPPER void* mbedtls_md4_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100291{
292 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
293
294 if( ctx != NULL )
295 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
296
297 return( ctx );
298}
299
Hanno Becker7a78fe42019-09-04 15:41:21 +0100300MBEDTLS_MD_WRAPPER void mbedtls_md4_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100301{
302 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
303 mbedtls_free( ctx );
304}
305
Hanno Becker7a78fe42019-09-04 15:41:21 +0100306MBEDTLS_MD_WRAPPER void mbedtls_md4_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100307{
308 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
309 (const mbedtls_md4_context *) src );
310}
311
Hanno Becker7a78fe42019-09-04 15:41:21 +0100312MBEDTLS_MD_WRAPPER int mbedtls_md4_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100313{
314 return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
315}
316
317#endif /* MBEDTLS_MD4_C */
318
319/*
320 * MD-5
321 */
322
323#if defined(MBEDTLS_MD5_C)
324
Hanno Becker7a78fe42019-09-04 15:41:21 +0100325MBEDTLS_MD_WRAPPER int mbedtls_md5_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100326{
327 return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
328}
329
Hanno Becker7a78fe42019-09-04 15:41:21 +0100330MBEDTLS_MD_WRAPPER int mbedtls_md5_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100331 size_t ilen )
332{
333 return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
334}
335
Hanno Becker7a78fe42019-09-04 15:41:21 +0100336MBEDTLS_MD_WRAPPER int mbedtls_md5_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100337{
338 return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
339}
340
Hanno Becker7a78fe42019-09-04 15:41:21 +0100341MBEDTLS_MD_WRAPPER void* mbedtls_md5_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100342{
343 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
344
345 if( ctx != NULL )
346 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
347
348 return( ctx );
349}
350
Hanno Becker7a78fe42019-09-04 15:41:21 +0100351MBEDTLS_MD_WRAPPER void mbedtls_md5_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100352{
353 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
354 mbedtls_free( ctx );
355}
356
Hanno Becker7a78fe42019-09-04 15:41:21 +0100357MBEDTLS_MD_WRAPPER void mbedtls_md5_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100358{
359 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
360 (const mbedtls_md5_context *) src );
361}
362
Hanno Becker7a78fe42019-09-04 15:41:21 +0100363MBEDTLS_MD_WRAPPER int mbedtls_md5_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100364{
365 return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
366}
367
368#endif /* MBEDTLS_MD5_C */
369
370/*
371 * RIPEMD-160
372 */
373
374#if defined(MBEDTLS_RIPEMD160_C)
375
Hanno Becker7a78fe42019-09-04 15:41:21 +0100376MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100377{
378 return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
379}
380
Hanno Becker7a78fe42019-09-04 15:41:21 +0100381MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100382 size_t ilen )
383{
384 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
385 input, ilen ) );
386}
387
Hanno Becker7a78fe42019-09-04 15:41:21 +0100388MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100389{
390 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
391 output ) );
392}
393
Hanno Becker7a78fe42019-09-04 15:41:21 +0100394MBEDTLS_MD_WRAPPER void* mbedtls_ripemd160_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100395{
396 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
397
398 if( ctx != NULL )
399 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
400
401 return( ctx );
402}
403
Hanno Becker7a78fe42019-09-04 15:41:21 +0100404MBEDTLS_MD_WRAPPER void mbedtls_ripemd160_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100405{
406 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
407 mbedtls_free( ctx );
408}
409
Hanno Becker7a78fe42019-09-04 15:41:21 +0100410MBEDTLS_MD_WRAPPER void mbedtls_ripemd160_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100411{
412 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
413 (const mbedtls_ripemd160_context *) src );
414}
415
Hanno Becker7a78fe42019-09-04 15:41:21 +0100416MBEDTLS_MD_WRAPPER int mbedtls_ripemd160_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100417{
418 return( mbedtls_internal_ripemd160_process(
419 (mbedtls_ripemd160_context *) ctx, data ) );
420}
421
422#endif /* MBEDTLS_RIPEMD160_C */
423
424/*
425 * SHA-1
426 */
427
428#if defined(MBEDTLS_SHA1_C)
429
Hanno Becker7a78fe42019-09-04 15:41:21 +0100430MBEDTLS_MD_WRAPPER int mbedtls_sha1_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100431{
432 return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
433}
434
Hanno Becker7a78fe42019-09-04 15:41:21 +0100435MBEDTLS_MD_WRAPPER int mbedtls_sha1_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100436 size_t ilen )
437{
438 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
439 input, ilen ) );
440}
441
Hanno Becker7a78fe42019-09-04 15:41:21 +0100442MBEDTLS_MD_WRAPPER int mbedtls_sha1_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100443{
444 return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
445}
446
Hanno Becker7a78fe42019-09-04 15:41:21 +0100447MBEDTLS_MD_WRAPPER void* mbedtls_sha1_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100448{
449 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
450
451 if( ctx != NULL )
452 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
453
454 return( ctx );
455}
456
Hanno Becker7a78fe42019-09-04 15:41:21 +0100457MBEDTLS_MD_WRAPPER void mbedtls_sha1_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100458{
459 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
460 (const mbedtls_sha1_context *) src );
461}
462
Hanno Becker7a78fe42019-09-04 15:41:21 +0100463MBEDTLS_MD_WRAPPER void mbedtls_sha1_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100464{
465 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
466 mbedtls_free( ctx );
467}
468
Hanno Becker7a78fe42019-09-04 15:41:21 +0100469MBEDTLS_MD_WRAPPER int mbedtls_sha1_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100470{
471 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
472 data ) );
473}
474
475#endif /* MBEDTLS_SHA1_C */
476
477/*
478 * SHA-224 and SHA-256
479 */
480
481#if defined(MBEDTLS_SHA256_C)
482
483#if !defined(MBEDTLS_SHA256_NO_SHA224)
Hanno Becker7a78fe42019-09-04 15:41:21 +0100484MBEDTLS_MD_WRAPPER int mbedtls_sha224_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100485{
486 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
487}
488#endif /* !MBEDTLS_SHA256_NO_SHA224 */
489
Hanno Becker7a78fe42019-09-04 15:41:21 +0100490MBEDTLS_MD_WRAPPER int mbedtls_sha224_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100491 size_t ilen )
492{
493 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
494 input, ilen ) );
495}
496
Hanno Becker7a78fe42019-09-04 15:41:21 +0100497MBEDTLS_MD_WRAPPER int mbedtls_sha224_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100498{
499 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
500 output ) );
501}
502
503#if !defined(MBEDTLS_SHA256_NO_SHA224)
Hanno Becker7a78fe42019-09-04 15:41:21 +0100504MBEDTLS_MD_WRAPPER int mbedtls_sha224_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100505 unsigned char *output )
506{
507 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
508}
509#endif /* !MBEDTLS_SHA256_NO_SHA224 */
510
Hanno Becker7a78fe42019-09-04 15:41:21 +0100511MBEDTLS_MD_WRAPPER void* mbedtls_sha224_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100512{
513 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
514
515 if( ctx != NULL )
516 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
517
518 return( ctx );
519}
520
Hanno Becker7a78fe42019-09-04 15:41:21 +0100521MBEDTLS_MD_WRAPPER void mbedtls_sha224_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100522{
523 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
524 mbedtls_free( ctx );
525}
526
Hanno Becker7a78fe42019-09-04 15:41:21 +0100527MBEDTLS_MD_WRAPPER void mbedtls_sha224_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100528{
529 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
530 (const mbedtls_sha256_context *) src );
531}
532
Hanno Becker7a78fe42019-09-04 15:41:21 +0100533MBEDTLS_MD_WRAPPER int mbedtls_sha224_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100534{
535 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
536 data ) );
537}
538
Hanno Becker7a78fe42019-09-04 15:41:21 +0100539MBEDTLS_MD_WRAPPER int mbedtls_sha256_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100540{
541 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
542}
543
Hanno Becker7a78fe42019-09-04 15:41:21 +0100544MBEDTLS_MD_WRAPPER int mbedtls_sha256_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100545 unsigned char *output )
546{
547 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
548}
549
550#endif /* MBEDTLS_SHA256_C */
551
552/*
553 * SHA-384 and SHA-512
554 */
555
556#if defined(MBEDTLS_SHA512_C)
557
Hanno Becker7a78fe42019-09-04 15:41:21 +0100558MBEDTLS_MD_WRAPPER int mbedtls_sha384_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100559{
560 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
561}
562
Hanno Becker7a78fe42019-09-04 15:41:21 +0100563MBEDTLS_MD_WRAPPER int mbedtls_sha384_update_wrap( void *ctx, const unsigned char *input,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100564 size_t ilen )
565{
566 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
567 input, ilen ) );
568}
569
Hanno Becker7a78fe42019-09-04 15:41:21 +0100570MBEDTLS_MD_WRAPPER int mbedtls_sha384_finish_wrap( void *ctx, unsigned char *output )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100571{
572 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
573 output ) );
574}
575
Hanno Becker7a78fe42019-09-04 15:41:21 +0100576MBEDTLS_MD_WRAPPER int mbedtls_sha384_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100577 unsigned char *output )
578{
579 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
580}
581
Hanno Becker7a78fe42019-09-04 15:41:21 +0100582MBEDTLS_MD_WRAPPER void* mbedtls_sha384_ctx_alloc( void )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100583{
584 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
585
586 if( ctx != NULL )
587 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
588
589 return( ctx );
590}
591
Hanno Becker7a78fe42019-09-04 15:41:21 +0100592MBEDTLS_MD_WRAPPER void mbedtls_sha384_ctx_free( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100593{
594 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
595 mbedtls_free( ctx );
596}
597
Hanno Becker7a78fe42019-09-04 15:41:21 +0100598MBEDTLS_MD_WRAPPER void mbedtls_sha384_clone_wrap( void *dst, const void *src )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100599{
600 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
601 (const mbedtls_sha512_context *) src );
602}
603
Hanno Becker7a78fe42019-09-04 15:41:21 +0100604MBEDTLS_MD_WRAPPER int mbedtls_sha384_process_wrap( void *ctx, const unsigned char *data )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100605{
606 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
607 data ) );
608}
609
Hanno Becker7a78fe42019-09-04 15:41:21 +0100610MBEDTLS_MD_WRAPPER int mbedtls_sha512_starts_wrap( void *ctx )
Hanno Beckerc4e42102019-09-04 12:43:22 +0100611{
612 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
613}
614
Hanno Becker7a78fe42019-09-04 15:41:21 +0100615MBEDTLS_MD_WRAPPER int mbedtls_sha512_wrap( const unsigned char *input, size_t ilen,
Hanno Beckerc4e42102019-09-04 12:43:22 +0100616 unsigned char *output )
617{
618 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
619}
620
621#endif /* MBEDTLS_SHA512_C */
622
623/*
624 * Getter functions for MD info structure.
625 */
626
627#if !defined(MBEDTLS_MD_SINGLE_HASH)
628
629MBEDTLS_ALWAYS_INLINE static inline mbedtls_md_type_t mbedtls_md_info_type(
630 mbedtls_md_handle_t info )
631{
632 return( info->type );
633}
634
635MBEDTLS_ALWAYS_INLINE static inline const char * mbedtls_md_info_name(
636 mbedtls_md_handle_t info )
637{
638 return( info->name );
639}
640
641MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_size(
642 mbedtls_md_handle_t info )
643{
644 return( info->size );
645}
646
647MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_block_size(
648 mbedtls_md_handle_t info )
649{
650 return( info->block_size );
651}
652
653MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_starts(
654 mbedtls_md_handle_t info,
655 void *ctx )
656{
657 return( info->starts_func( ctx ) );
658}
659
660MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_update(
661 mbedtls_md_handle_t info,
662 void *ctx,
663 const unsigned char *input,
664 size_t ilen )
665{
666 return( info->update_func( ctx, input, ilen ) );
667}
668
669MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_finish(
670 mbedtls_md_handle_t info,
671 void *ctx,
672 unsigned char *output )
673{
674 return( info->finish_func( ctx, output ) );
675}
676
677MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_digest(
678 mbedtls_md_handle_t info,
679 const unsigned char *input,
680 size_t ilen,
681 unsigned char *output )
682{
683 return( info->digest_func( input, ilen, output ) );
684}
685
686MBEDTLS_ALWAYS_INLINE static inline void* mbedtls_md_info_ctx_alloc(
687 mbedtls_md_handle_t info )
688{
689 return( info->ctx_alloc_func() );
690}
691
692MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_ctx_free(
693 mbedtls_md_handle_t info,
694 void *ctx )
695{
696 info->ctx_free_func( ctx );
697}
698
699MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_clone(
700 mbedtls_md_handle_t info,
701 void *dst,
702 const void *src )
703{
704 info->clone_func( dst, src );
705}
706
707MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_process(
708 mbedtls_md_handle_t info,
709 void *ctx,
710 const unsigned char *input )
711{
712 return( info->process_func( ctx, input ) );
713}
714
715#else /* !MBEDTLS_MD_SINGLE_HASH */
716
717MBEDTLS_ALWAYS_INLINE static inline mbedtls_md_type_t mbedtls_md_info_type(
718 mbedtls_md_handle_t info )
719{
720 ((void) info);
721 return( MBEDTLS_MD_INFO_TYPE( MBEDTLS_MD_SINGLE_HASH ) );
722}
723
724MBEDTLS_ALWAYS_INLINE static inline const char * mbedtls_md_info_name(
725 mbedtls_md_handle_t info )
726{
727 ((void) info);
728 return( MBEDTLS_MD_INFO_NAME( MBEDTLS_MD_SINGLE_HASH ) );
729}
730
731MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_size(
732 mbedtls_md_handle_t info )
733{
734 ((void) info);
735 return( MBEDTLS_MD_INFO_SIZE( MBEDTLS_MD_SINGLE_HASH ) );
736}
737
738MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_block_size(
739 mbedtls_md_handle_t info )
740{
741 ((void) info);
742 return( MBEDTLS_MD_INFO_BLOCKSIZE( MBEDTLS_MD_SINGLE_HASH ) );
743}
744
745MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_starts(
746 mbedtls_md_handle_t info,
747 void *ctx )
748{
749 ((void) info);
750 return( MBEDTLS_MD_INFO_STARTS_FUNC( MBEDTLS_MD_SINGLE_HASH )( ctx ) );
751}
752
753MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_update(
754 mbedtls_md_handle_t info,
755 void *ctx,
756 const unsigned char *input,
757 size_t ilen )
758{
759 ((void) info);
760 return( MBEDTLS_MD_INFO_UPDATE_FUNC( MBEDTLS_MD_SINGLE_HASH )
761 ( ctx, input, ilen ) );
762}
763
764MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_finish(
765 mbedtls_md_handle_t info,
766 void *ctx,
767 unsigned char *output )
768{
769 ((void) info);
770 return( MBEDTLS_MD_INFO_FINISH_FUNC( MBEDTLS_MD_SINGLE_HASH )
771 ( ctx, output ) );
772}
773
774MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_digest(
775 mbedtls_md_handle_t info,
776 const unsigned char *input,
777 size_t ilen,
778 unsigned char *output )
779{
780 ((void) info);
781 return( MBEDTLS_MD_INFO_DIGEST_FUNC( MBEDTLS_MD_SINGLE_HASH )
782 ( input, ilen, output ) );
783}
784
785MBEDTLS_ALWAYS_INLINE static inline void* mbedtls_md_info_ctx_alloc(
786 mbedtls_md_handle_t info )
787{
788 ((void) info);
789 return( MBEDTLS_MD_INFO_ALLOC_FUNC( MBEDTLS_MD_SINGLE_HASH )() );
790}
791
792MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_ctx_free(
793 mbedtls_md_handle_t info,
794 void *ctx )
795{
796 ((void) info);
797 MBEDTLS_MD_INFO_FREE_FUNC( MBEDTLS_MD_SINGLE_HASH )( ctx );
798}
799
800MBEDTLS_ALWAYS_INLINE static inline void mbedtls_md_info_clone(
801 mbedtls_md_handle_t info,
802 void *dst,
803 const void *src )
804{
805 ((void) info);
806 MBEDTLS_MD_INFO_CLONE_FUNC( MBEDTLS_MD_SINGLE_HASH )( dst, src );
807}
808
809MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_info_process(
810 mbedtls_md_handle_t info,
811 void *ctx,
812 const unsigned char *input )
813{
814 ((void) info);
815 return( MBEDTLS_MD_INFO_PROCESS_FUNC( MBEDTLS_MD_SINGLE_HASH )
816 ( ctx, input ) );
817}
818
819#endif /* MBEDTLS_MD_SINGLE_HASH */
820
821#ifdef __cplusplus
822}
823#endif
824
825#endif /* MBEDTLS_MD_INTERNAL_H */