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