blob: 2271a766ee6eb9b05a1593ed323fe26294bb2e26 [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002 * \file mbedtls_md.c
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic message digest wrapper for mbed TLS
Paul Bakker17373852011-01-06 14:20:01 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker17373852011-01-06 14:20:01 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/md.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050035#include "mbedtls/platform_util.h"
Paul Bakker17373852011-01-06 14:20:01 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010038#include "mbedtls/platform.h"
39#else
Paul Bakker17373852011-01-06 14:20:01 +000040#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020041#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define mbedtls_free free
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010043#endif
44
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
Paul Bakker17373852011-01-06 14:20:01 +000046
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020047#if defined(MBEDTLS_FS_IO)
48#include <stdio.h>
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000049#endif
50
Hanno Beckerd03949e2019-07-26 14:38:44 +010051#if defined(MBEDTLS_MD2_C)
52#include "mbedtls/md2.h"
53#endif
54
55#if defined(MBEDTLS_MD4_C)
56#include "mbedtls/md4.h"
57#endif
58
59#if defined(MBEDTLS_MD5_C)
60#include "mbedtls/md5.h"
61#endif
62
63#if defined(MBEDTLS_RIPEMD160_C)
64#include "mbedtls/ripemd160.h"
65#endif
66
67#if defined(MBEDTLS_SHA1_C)
68#include "mbedtls/sha1.h"
69#endif
70
71#if defined(MBEDTLS_SHA256_C)
72#include "mbedtls/sha256.h"
73#endif
74
75#if defined(MBEDTLS_SHA512_C)
76#include "mbedtls/sha512.h"
77#endif
78
79/*
80 * Message-digest information macro definition
81 */
82
83/* SHA-256 */
84#define MBEDTLS_MD_INFO_SHA256_TYPE MBEDTLS_MD_SHA256
85#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 sha256_starts_wrap
89#define MBEDTLS_MD_INFO_SHA256_UPDATE_FUNC sha224_update_wrap
90#define MBEDTLS_MD_INFO_SHA256_FINISH_FUNC sha224_finish_wrap
91#define MBEDTLS_MD_INFO_SHA256_DIGEST_FUNC sha256_wrap
92#define MBEDTLS_MD_INFO_SHA256_ALLOC_FUNC sha224_ctx_alloc
93#define MBEDTLS_MD_INFO_SHA256_FREE_FUNC sha224_ctx_free
94#define MBEDTLS_MD_INFO_SHA256_CLONE_FUNC sha224_clone_wrap
95#define MBEDTLS_MD_INFO_SHA256_PROCESS_FUNC sha224_process_wrap
96
97/*
98 * Helper macros to extract fields from ciphersuites.
99 */
100
101#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_SSL_CONF_SINGLE_HASH ).
118 * where MBEDTLS_SSL_CONF_SINGLE_HASH expands to MBEDTLS_MD_INFO_XXX. */
119#define MBEDTLS_MD_INFO_TYPE( MD ) MBEDTLS_MD_INFO_TYPE_T( MD )
120#define MBEDTLS_MD_INFO_NAME( MD ) MBEDTLS_MD_INFO_NAME_T( MD )
121#define MBEDTLS_MD_INFO_SIZE( MD ) MBEDTLS_MD_INFO_SIZE_T( MD )
122#define MBEDTLS_MD_INFO_BLOCKSIZE( MD ) MBEDTLS_MD_INFO_BLOCKSIZE_T( MD )
123#define MBEDTLS_MD_INFO_STARTS_FUNC( MD ) MBEDTLS_MD_INFO_STARTS_FUNC_T( MD )
124#define MBEDTLS_MD_INFO_UPDATE_FUNC( MD ) MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD )
125#define MBEDTLS_MD_INFO_FINISH_FUNC( MD ) MBEDTLS_MD_INFO_FINISH_FUNC_T( MD )
126#define MBEDTLS_MD_INFO_DIGEST_FUNC( MD ) MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD )
127#define MBEDTLS_MD_INFO_ALLOC_FUNC( MD ) MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD )
128#define MBEDTLS_MD_INFO_FREE_FUNC( MD ) MBEDTLS_MD_INFO_FREE_FUNC_T( MD )
129#define MBEDTLS_MD_INFO_CLONE_FUNC( MD ) MBEDTLS_MD_INFO_CLONE_FUNC_T( MD )
130#define MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD )
131
132/**
133 * Message digest information.
134 * Allows message digest functions to be called in a generic way.
135 */
136
137typedef int mbedtls_md_starts_func_t( void *ctx );
138typedef int mbedtls_md_update_func_t( void *ctx,
139 const unsigned char *input,
140 size_t ilen );
141typedef int mbedtls_md_finish_func_t( void *ctx, unsigned char *output );
142typedef int mbedtls_md_digest_func_t( const unsigned char *input,
143 size_t ilen,
144 unsigned char *output );
145typedef void* mbedtls_md_ctx_alloc_func_t( void );
146typedef void mbedtls_md_ctx_free_func_t( void *ctx );
147typedef void mbedtls_md_clone_func_t( void *st, const void *src );
148typedef int mbedtls_md_process_func_t( void *ctx,
149 const unsigned char *input );
150
151struct mbedtls_md_info_t
152{
153 /** Digest identifier */
154 mbedtls_md_type_t type;
155
156 /** Name of the message digest */
157 const char * name;
158
159 /** Output length of the digest function in bytes */
160 int size;
161
162 /** Block length of the digest function in bytes */
163 int block_size;
164
165 /** Digest initialisation function */
166 mbedtls_md_starts_func_t *starts_func;
167
168 /** Digest update function */
169 mbedtls_md_update_func_t *update_func;
170
171 /** Digest finalisation function */
172 mbedtls_md_finish_func_t *finish_func;
173
174 /** Generic digest function */
175 mbedtls_md_digest_func_t *digest_func;
176
177 /** Allocate a new context */
178 mbedtls_md_ctx_alloc_func_t *ctx_alloc_func;
179
180 /** Free the given context */
181 mbedtls_md_ctx_free_func_t *ctx_free_func;
182
183 /** Clone state from a context */
184 mbedtls_md_clone_func_t *clone_func;
185
186 /** Internal use only */
187 mbedtls_md_process_func_t *process_func;
188};
189
190/**
191 * \brief This macro builds an instance of ::mbedtls_md_info_t
192 * from an \c MBEDTLS_MD_INFO_XXX identifier.
193 */
194#define MBEDTLS_MD_INFO( MD ) \
195 { MBEDTLS_MD_INFO_TYPE( MD ), \
196 MBEDTLS_MD_INFO_NAME( MD ), \
197 MBEDTLS_MD_INFO_SIZE( MD ), \
198 MBEDTLS_MD_INFO_BLOCKSIZE( MD ), \
199 MBEDTLS_MD_INFO_STARTS_FUNC( MD ), \
200 MBEDTLS_MD_INFO_UPDATE_FUNC( MD ), \
201 MBEDTLS_MD_INFO_FINISH_FUNC( MD ), \
202 MBEDTLS_MD_INFO_DIGEST_FUNC( MD ), \
203 MBEDTLS_MD_INFO_ALLOC_FUNC( MD ), \
204 MBEDTLS_MD_INFO_FREE_FUNC( MD ), \
205 MBEDTLS_MD_INFO_CLONE_FUNC( MD ), \
206 MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) }
207
208/*
209 *
210 * Definitions of MD information structures for various digests.
211 *
212 */
213
214/*
215 * MD-2
216 */
217
218#if defined(MBEDTLS_MD2_C)
219
220static int md2_starts_wrap( void *ctx )
221{
222 return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
223}
224
225static int md2_update_wrap( void *ctx, const unsigned char *input,
226 size_t ilen )
227{
228 return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
229}
230
231static int md2_finish_wrap( void *ctx, unsigned char *output )
232{
233 return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
234}
235
236static void *md2_ctx_alloc( void )
237{
238 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
239
240 if( ctx != NULL )
241 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
242
243 return( ctx );
244}
245
246static void md2_ctx_free( void *ctx )
247{
248 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
249 mbedtls_free( ctx );
250}
251
252static void md2_clone_wrap( void *dst, const void *src )
253{
254 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
255 (const mbedtls_md2_context *) src );
256}
257
258static int md2_process_wrap( void *ctx, const unsigned char *data )
259{
260 ((void) data);
261
262 return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) );
263}
264
265const mbedtls_md_info_t mbedtls_md2_info = {
266 MBEDTLS_MD_MD2,
267 "MD2",
268 16,
269 16,
270 md2_starts_wrap,
271 md2_update_wrap,
272 md2_finish_wrap,
273 mbedtls_md2_ret,
274 md2_ctx_alloc,
275 md2_ctx_free,
276 md2_clone_wrap,
277 md2_process_wrap,
278};
279
280#endif /* MBEDTLS_MD2_C */
281
282/*
283 * MD-4
284 */
285
286#if defined(MBEDTLS_MD4_C)
287
288static int md4_starts_wrap( void *ctx )
289{
290 return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
291}
292
293static int md4_update_wrap( void *ctx, const unsigned char *input,
294 size_t ilen )
295{
296 return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
297}
298
299static int md4_finish_wrap( void *ctx, unsigned char *output )
300{
301 return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
302}
303
304static void *md4_ctx_alloc( void )
305{
306 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
307
308 if( ctx != NULL )
309 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
310
311 return( ctx );
312}
313
314static void md4_ctx_free( void *ctx )
315{
316 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
317 mbedtls_free( ctx );
318}
319
320static void md4_clone_wrap( void *dst, const void *src )
321{
322 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
323 (const mbedtls_md4_context *) src );
324}
325
326static int md4_process_wrap( void *ctx, const unsigned char *data )
327{
328 return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
329}
330
331const mbedtls_md_info_t mbedtls_md4_info = {
332 MBEDTLS_MD_MD4,
333 "MD4",
334 16,
335 64,
336 md4_starts_wrap,
337 md4_update_wrap,
338 md4_finish_wrap,
339 mbedtls_md4_ret,
340 md4_ctx_alloc,
341 md4_ctx_free,
342 md4_clone_wrap,
343 md4_process_wrap,
344};
345
346#endif /* MBEDTLS_MD4_C */
347
348/*
349 * MD-5
350 */
351
352#if defined(MBEDTLS_MD5_C)
353
354static int md5_starts_wrap( void *ctx )
355{
356 return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
357}
358
359static int md5_update_wrap( void *ctx, const unsigned char *input,
360 size_t ilen )
361{
362 return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
363}
364
365static int md5_finish_wrap( void *ctx, unsigned char *output )
366{
367 return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
368}
369
370static void *md5_ctx_alloc( void )
371{
372 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
373
374 if( ctx != NULL )
375 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
376
377 return( ctx );
378}
379
380static void md5_ctx_free( void *ctx )
381{
382 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
383 mbedtls_free( ctx );
384}
385
386static void md5_clone_wrap( void *dst, const void *src )
387{
388 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
389 (const mbedtls_md5_context *) src );
390}
391
392static int md5_process_wrap( void *ctx, const unsigned char *data )
393{
394 return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
395}
396
397const mbedtls_md_info_t mbedtls_md5_info = {
398 MBEDTLS_MD_MD5,
399 "MD5",
400 16,
401 64,
402 md5_starts_wrap,
403 md5_update_wrap,
404 md5_finish_wrap,
405 mbedtls_md5_ret,
406 md5_ctx_alloc,
407 md5_ctx_free,
408 md5_clone_wrap,
409 md5_process_wrap,
410};
411
412#endif /* MBEDTLS_MD5_C */
413
414/*
415 * RIPEMD-160
416 */
417
418#if defined(MBEDTLS_RIPEMD160_C)
419
420static int ripemd160_starts_wrap( void *ctx )
421{
422 return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
423}
424
425static int ripemd160_update_wrap( void *ctx, const unsigned char *input,
426 size_t ilen )
427{
428 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
429 input, ilen ) );
430}
431
432static int ripemd160_finish_wrap( void *ctx, unsigned char *output )
433{
434 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
435 output ) );
436}
437
438static void *ripemd160_ctx_alloc( void )
439{
440 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
441
442 if( ctx != NULL )
443 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
444
445 return( ctx );
446}
447
448static void ripemd160_ctx_free( void *ctx )
449{
450 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
451 mbedtls_free( ctx );
452}
453
454static void ripemd160_clone_wrap( void *dst, const void *src )
455{
456 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
457 (const mbedtls_ripemd160_context *) src );
458}
459
460static int ripemd160_process_wrap( void *ctx, const unsigned char *data )
461{
462 return( mbedtls_internal_ripemd160_process(
463 (mbedtls_ripemd160_context *) ctx, data ) );
464}
465
466const mbedtls_md_info_t mbedtls_ripemd160_info = {
467 MBEDTLS_MD_RIPEMD160,
468 "RIPEMD160",
469 20,
470 64,
471 ripemd160_starts_wrap,
472 ripemd160_update_wrap,
473 ripemd160_finish_wrap,
474 mbedtls_ripemd160_ret,
475 ripemd160_ctx_alloc,
476 ripemd160_ctx_free,
477 ripemd160_clone_wrap,
478 ripemd160_process_wrap,
479};
480
481#endif /* MBEDTLS_RIPEMD160_C */
482
483/*
484 * SHA-1
485 */
486
487#if defined(MBEDTLS_SHA1_C)
488
489static int sha1_starts_wrap( void *ctx )
490{
491 return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
492}
493
494static int sha1_update_wrap( void *ctx, const unsigned char *input,
495 size_t ilen )
496{
497 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
498 input, ilen ) );
499}
500
501static int sha1_finish_wrap( void *ctx, unsigned char *output )
502{
503 return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
504}
505
506static void *sha1_ctx_alloc( void )
507{
508 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
509
510 if( ctx != NULL )
511 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
512
513 return( ctx );
514}
515
516static void sha1_clone_wrap( void *dst, const void *src )
517{
518 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
519 (const mbedtls_sha1_context *) src );
520}
521
522static void sha1_ctx_free( void *ctx )
523{
524 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
525 mbedtls_free( ctx );
526}
527
528static int sha1_process_wrap( void *ctx, const unsigned char *data )
529{
530 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
531 data ) );
532}
533
534const mbedtls_md_info_t mbedtls_sha1_info = {
535 MBEDTLS_MD_SHA1,
536 "SHA1",
537 20,
538 64,
539 sha1_starts_wrap,
540 sha1_update_wrap,
541 sha1_finish_wrap,
542 mbedtls_sha1_ret,
543 sha1_ctx_alloc,
544 sha1_ctx_free,
545 sha1_clone_wrap,
546 sha1_process_wrap,
547};
548
549#endif /* MBEDTLS_SHA1_C */
550
551/*
552 * SHA-224 and SHA-256
553 */
554
555#if defined(MBEDTLS_SHA256_C)
556
557#if !defined(MBEDTLS_SHA256_NO_SHA224)
558static int sha224_starts_wrap( void *ctx )
559{
560 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
561}
562#endif /* !MBEDTLS_SHA256_NO_SHA224 */
563
564static int sha224_update_wrap( void *ctx, const unsigned char *input,
565 size_t ilen )
566{
567 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
568 input, ilen ) );
569}
570
571static int sha224_finish_wrap( void *ctx, unsigned char *output )
572{
573 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
574 output ) );
575}
576
577#if !defined(MBEDTLS_SHA256_NO_SHA224)
578static int sha224_wrap( const unsigned char *input, size_t ilen,
579 unsigned char *output )
580{
581 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
582}
583#endif /* !MBEDTLS_SHA256_NO_SHA224 */
584
585static void *sha224_ctx_alloc( void )
586{
587 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
588
589 if( ctx != NULL )
590 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
591
592 return( ctx );
593}
594
595static void sha224_ctx_free( void *ctx )
596{
597 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
598 mbedtls_free( ctx );
599}
600
601static void sha224_clone_wrap( void *dst, const void *src )
602{
603 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
604 (const mbedtls_sha256_context *) src );
605}
606
607static int sha224_process_wrap( void *ctx, const unsigned char *data )
608{
609 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
610 data ) );
611}
612
613#if !defined(MBEDTLS_SHA256_NO_SHA224)
614const mbedtls_md_info_t mbedtls_sha224_info = {
615 MBEDTLS_MD_SHA224,
616 "SHA224",
617 28,
618 64,
619 sha224_starts_wrap,
620 sha224_update_wrap,
621 sha224_finish_wrap,
622 sha224_wrap,
623 sha224_ctx_alloc,
624 sha224_ctx_free,
625 sha224_clone_wrap,
626 sha224_process_wrap,
627};
628#endif /* !MBEDTLS_SHA256_NO_SHA224 */
629
630static int sha256_starts_wrap( void *ctx )
631{
632 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
633}
634
635static int sha256_wrap( const unsigned char *input, size_t ilen,
636 unsigned char *output )
637{
638 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
639}
640
641const mbedtls_md_info_t mbedtls_sha256_info =
642 MBEDTLS_MD_INFO( MBEDTLS_MD_INFO_SHA256 );
643
644#endif /* MBEDTLS_SHA256_C */
645
646/*
647 * SHA-384 and SHA-512
648 */
649
650#if defined(MBEDTLS_SHA512_C)
651
652static int sha384_starts_wrap( void *ctx )
653{
654 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
655}
656
657static int sha384_update_wrap( void *ctx, const unsigned char *input,
658 size_t ilen )
659{
660 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
661 input, ilen ) );
662}
663
664static int sha384_finish_wrap( void *ctx, unsigned char *output )
665{
666 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
667 output ) );
668}
669
670static int sha384_wrap( const unsigned char *input, size_t ilen,
671 unsigned char *output )
672{
673 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
674}
675
676static void *sha384_ctx_alloc( void )
677{
678 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
679
680 if( ctx != NULL )
681 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
682
683 return( ctx );
684}
685
686static void sha384_ctx_free( void *ctx )
687{
688 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
689 mbedtls_free( ctx );
690}
691
692static void sha384_clone_wrap( void *dst, const void *src )
693{
694 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
695 (const mbedtls_sha512_context *) src );
696}
697
698static int sha384_process_wrap( void *ctx, const unsigned char *data )
699{
700 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
701 data ) );
702}
703
704const mbedtls_md_info_t mbedtls_sha384_info = {
705 MBEDTLS_MD_SHA384,
706 "SHA384",
707 48,
708 128,
709 sha384_starts_wrap,
710 sha384_update_wrap,
711 sha384_finish_wrap,
712 sha384_wrap,
713 sha384_ctx_alloc,
714 sha384_ctx_free,
715 sha384_clone_wrap,
716 sha384_process_wrap,
717};
718
719static int sha512_starts_wrap( void *ctx )
720{
721 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
722}
723
724static int sha512_wrap( const unsigned char *input, size_t ilen,
725 unsigned char *output )
726{
727 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
728}
729
730const mbedtls_md_info_t mbedtls_sha512_info = {
731 MBEDTLS_MD_SHA512,
732 "SHA512",
733 64,
734 128,
735 sha512_starts_wrap,
736 sha384_update_wrap,
737 sha384_finish_wrap,
738 sha512_wrap,
739 sha384_ctx_alloc,
740 sha384_ctx_free,
741 sha384_clone_wrap,
742 sha384_process_wrap,
743};
744
745#endif /* MBEDTLS_SHA512_C */
746
747/*
748 * Getter functions for MD info structure.
749 */
750
751static inline mbedtls_md_type_t mbedtls_md_info_type(
752 mbedtls_md_handle_t info )
753{
754 return( info->type );
755}
756
757static inline const char * mbedtls_md_info_name(
758 mbedtls_md_handle_t info )
759{
760 return( info->name );
761}
762
763static inline int mbedtls_md_info_size(
764 mbedtls_md_handle_t info )
765{
766 return( info->size );
767}
768
769static inline int mbedtls_md_info_block_size(
770 mbedtls_md_handle_t info )
771{
772 return( info->block_size );
773}
774
775static inline mbedtls_md_starts_func_t *mbedtls_md_info_starts_func(
776 mbedtls_md_handle_t info )
777{
778 return( info->starts_func );
779}
780
781static inline mbedtls_md_update_func_t *mbedtls_md_info_update_func(
782 mbedtls_md_handle_t info )
783{
784 return( info->update_func );
785}
786
787static inline mbedtls_md_finish_func_t *mbedtls_md_info_finish_func(
788 mbedtls_md_handle_t info )
789{
790 return( info->finish_func );
791}
792
793static inline mbedtls_md_digest_func_t *mbedtls_md_info_digest_func(
794 mbedtls_md_handle_t info )
795{
796 return( info->digest_func );
797}
798
799static inline mbedtls_md_ctx_alloc_func_t *mbedtls_md_info_ctx_alloc_func(
800 mbedtls_md_handle_t info )
801{
802 return( info->ctx_alloc_func );
803}
804
805static inline mbedtls_md_ctx_free_func_t *mbedtls_md_info_ctx_free_func(
806 mbedtls_md_handle_t info )
807{
808 return( info->ctx_free_func );
809}
810
811static inline mbedtls_md_clone_func_t *mbedtls_md_info_clone_func(
812 mbedtls_md_handle_t info )
813{
814 return( info->clone_func );
815}
816
817static inline mbedtls_md_process_func_t *mbedtls_md_info_process_func(
818 mbedtls_md_handle_t info )
819{
820 return( info->process_func );
821}
Hanno Becker0e7fc312019-07-17 11:23:12 +0100822
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200823/*
824 * Reminder: update profiles in x509_crt.c when adding a new hash!
825 */
Paul Bakker72f62662011-01-16 21:27:44 +0000826static const int supported_digests[] = {
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SHA512_C)
829 MBEDTLS_MD_SHA512,
830 MBEDTLS_MD_SHA384,
Paul Bakker72f62662011-01-16 21:27:44 +0000831#endif
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833#if defined(MBEDTLS_SHA256_C)
834 MBEDTLS_MD_SHA256,
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200835#if !defined(MBEDTLS_SHA256_NO_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 MBEDTLS_MD_SHA224,
Paul Bakker72f62662011-01-16 21:27:44 +0000837#endif
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200838#endif /* MBEDTLS_SHA256_C */
Paul Bakker72f62662011-01-16 21:27:44 +0000839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#if defined(MBEDTLS_SHA1_C)
841 MBEDTLS_MD_SHA1,
Paul Bakker72f62662011-01-16 21:27:44 +0000842#endif
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_RIPEMD160_C)
845 MBEDTLS_MD_RIPEMD160,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200846#endif
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_MD5_C)
849 MBEDTLS_MD_MD5,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200850#endif
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_MD4_C)
853 MBEDTLS_MD_MD4,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200854#endif
855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#if defined(MBEDTLS_MD2_C)
857 MBEDTLS_MD_MD2,
Manuel Pégourié-Gonnardbd772542014-07-07 14:02:33 +0200858#endif
859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 MBEDTLS_MD_NONE
Paul Bakker72f62662011-01-16 21:27:44 +0000861};
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863const int *mbedtls_md_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000864{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200865 return( supported_digests );
Paul Bakker72f62662011-01-16 21:27:44 +0000866}
867
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100868mbedtls_md_handle_t mbedtls_md_info_from_string( const char *md_name )
Paul Bakker17373852011-01-06 14:20:01 +0000869{
870 if( NULL == md_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200871 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000872
873 /* Get the appropriate digest information */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200875 if( !strcmp( "MD2", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
Paul Bakker17373852011-01-06 14:20:01 +0000877#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200879 if( !strcmp( "MD4", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
Paul Bakker17373852011-01-06 14:20:01 +0000881#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200883 if( !strcmp( "MD5", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
Paul Bakker17373852011-01-06 14:20:01 +0000885#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200887 if( !strcmp( "RIPEMD160", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100889#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200891 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakker17373852011-01-06 14:20:01 +0000893#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200895#if !defined(MBEDTLS_SHA256_NO_SHA224)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200896 if( !strcmp( "SHA224", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200898#endif
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200899 if( !strcmp( "SHA256", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200901#endif /* MBEDTLS_SHA256_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200903 if( !strcmp( "SHA384", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200905 if( !strcmp( "SHA512", md_name ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
Paul Bakker17373852011-01-06 14:20:01 +0000907#endif
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200908 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000909}
910
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100911mbedtls_md_handle_t mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
Paul Bakker17373852011-01-06 14:20:01 +0000912{
913 switch( md_type )
914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_MD2_C)
916 case MBEDTLS_MD_MD2:
917 return( &mbedtls_md2_info );
Paul Bakker17373852011-01-06 14:20:01 +0000918#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#if defined(MBEDTLS_MD4_C)
920 case MBEDTLS_MD_MD4:
921 return( &mbedtls_md4_info );
Paul Bakker17373852011-01-06 14:20:01 +0000922#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#if defined(MBEDTLS_MD5_C)
924 case MBEDTLS_MD_MD5:
925 return( &mbedtls_md5_info );
Paul Bakker17373852011-01-06 14:20:01 +0000926#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_RIPEMD160_C)
928 case MBEDTLS_MD_RIPEMD160:
929 return( &mbedtls_ripemd160_info );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#if defined(MBEDTLS_SHA1_C)
932 case MBEDTLS_MD_SHA1:
933 return( &mbedtls_sha1_info );
Paul Bakker17373852011-01-06 14:20:01 +0000934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200936#if !defined(MBEDTLS_SHA256_NO_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 case MBEDTLS_MD_SHA224:
938 return( &mbedtls_sha224_info );
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 case MBEDTLS_MD_SHA256:
941 return( &mbedtls_sha256_info );
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200942#endif /* MBEDTLS_SHA256_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#if defined(MBEDTLS_SHA512_C)
944 case MBEDTLS_MD_SHA384:
945 return( &mbedtls_sha384_info );
946 case MBEDTLS_MD_SHA512:
947 return( &mbedtls_sha512_info );
Paul Bakker17373852011-01-06 14:20:01 +0000948#endif
949 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200950 return( NULL );
Paul Bakker17373852011-01-06 14:20:01 +0000951 }
952}
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954void mbedtls_md_init( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200957}
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959void mbedtls_md_free( mbedtls_md_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200960{
Hanno Beckerd3827c72019-09-03 12:56:37 +0100961 if( ctx == NULL || mbedtls_md_get_handle( ctx ) == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200962 return;
963
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100964 if( ctx->md_ctx != NULL )
Hanno Beckerd3827c72019-09-03 12:56:37 +0100965 {
966 mbedtls_md_info_ctx_free_func(
967 mbedtls_md_get_handle( ctx ) )( ctx->md_ctx );
968 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200969
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100970 if( ctx->hmac_ctx != NULL )
971 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500972 mbedtls_platform_zeroize( ctx->hmac_ctx,
Hanno Beckerd3827c72019-09-03 12:56:37 +0100973 2 * mbedtls_md_info_block_size( mbedtls_md_get_handle( ctx ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_free( ctx->hmac_ctx );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100975 }
976
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500977 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200978}
979
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200980int mbedtls_md_clone( mbedtls_md_context_t *dst,
981 const mbedtls_md_context_t *src )
982{
Hanno Beckerd3827c72019-09-03 12:56:37 +0100983 if( dst == NULL || mbedtls_md_get_handle( dst ) == MBEDTLS_MD_INVALID_HANDLE ||
984 src == NULL || mbedtls_md_get_handle( src ) == MBEDTLS_MD_INVALID_HANDLE ||
985 mbedtls_md_get_handle( dst ) != mbedtls_md_get_handle( src ) )
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200986 {
987 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
988 }
989
Hanno Beckerd3827c72019-09-03 12:56:37 +0100990 mbedtls_md_info_clone_func( mbedtls_md_get_handle( dst ) )
991 ( dst->md_ctx, src->md_ctx );
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200992 return( 0 );
993}
994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100996int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, mbedtls_md_handle_t md_info )
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100997{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 return mbedtls_md_setup( ctx, md_info, 1 );
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100999}
1000#endif
1001
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001002int mbedtls_md_setup( mbedtls_md_context_t *ctx, mbedtls_md_handle_t md_info, int hmac )
Paul Bakker17373852011-01-06 14:20:01 +00001003{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001004 if( md_info == MBEDTLS_MD_INVALID_HANDLE || ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +00001006
Hanno Becker530387e2019-07-17 14:10:26 +01001007 ctx->md_ctx = mbedtls_md_info_ctx_alloc_func( md_info )();
1008 if( ctx->md_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Paul Bakker17373852011-01-06 14:20:01 +00001010
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +01001011 if( hmac != 0 )
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +01001012 {
Hanno Beckerd3827c72019-09-03 12:56:37 +01001013 ctx->hmac_ctx = mbedtls_calloc( 2,
1014 mbedtls_md_info_block_size( md_info ) );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +01001015 if( ctx->hmac_ctx == NULL )
1016 {
Hanno Becker530387e2019-07-17 14:10:26 +01001017 mbedtls_md_info_ctx_free_func( md_info )( ctx->md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +01001019 }
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +01001020 }
1021
Paul Bakker17373852011-01-06 14:20:01 +00001022 ctx->md_info = md_info;
1023
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001024 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +00001025}
1026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027int mbedtls_md_starts( mbedtls_md_context_t *ctx )
Paul Bakker562535d2011-01-20 16:42:01 +00001028{
Hanno Beckerd3827c72019-09-03 12:56:37 +01001029 mbedtls_md_handle_t md_info;
1030 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker562535d2011-01-20 16:42:01 +00001032
Hanno Beckerd3827c72019-09-03 12:56:37 +01001033 md_info = mbedtls_md_get_handle( ctx );
1034 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
1035 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
1036
1037 return( mbedtls_md_info_starts_func( md_info )( ctx->md_ctx ) );
Paul Bakker562535d2011-01-20 16:42:01 +00001038}
1039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +00001041{
Hanno Beckerd3827c72019-09-03 12:56:37 +01001042 mbedtls_md_handle_t md_info;
1043 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +00001045
Hanno Beckerd3827c72019-09-03 12:56:37 +01001046 md_info = mbedtls_md_get_handle( ctx );
1047 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
1048 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
1049
1050 return( mbedtls_md_info_update_func( md_info )( ctx->md_ctx,
1051 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +00001052}
1053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +00001055{
Hanno Beckerd3827c72019-09-03 12:56:37 +01001056 mbedtls_md_handle_t md_info;
1057 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +00001059
Hanno Beckerd3827c72019-09-03 12:56:37 +01001060 md_info = mbedtls_md_get_handle( ctx );
1061 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
1062 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
1063
1064 return( mbedtls_md_info_finish_func( md_info )( ctx->md_ctx,
1065 output ) );
Paul Bakker17373852011-01-06 14:20:01 +00001066}
1067
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001068int mbedtls_md( mbedtls_md_handle_t md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +00001069 unsigned char *output )
1070{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001071 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +00001073
Hanno Becker530387e2019-07-17 14:10:26 +01001074 return( mbedtls_md_info_digest_func( md_info )(
1075 input, ilen, output) );
Paul Bakker17373852011-01-06 14:20:01 +00001076}
1077
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +02001078#if defined(MBEDTLS_FS_IO)
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001079int mbedtls_md_file( mbedtls_md_handle_t md_info, const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +00001080{
Paul Bakker9c021ad2011-06-09 15:55:11 +00001081 int ret;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +02001082 FILE *f;
1083 size_t n;
1084 mbedtls_md_context_t ctx;
1085 unsigned char buf[1024];
Paul Bakker9c021ad2011-06-09 15:55:11 +00001086
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001087 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +00001089
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +02001090 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnardbcc03082015-06-24 00:09:29 +02001091 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
1092
1093 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +02001094
1095 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
1096 goto cleanup;
1097
Hanno Becker530387e2019-07-17 14:10:26 +01001098 ret = mbedtls_md_info_starts_func( md_info )( ctx.md_ctx );
1099 if( ret != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001100 goto cleanup;
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +02001101
1102 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Hanno Becker530387e2019-07-17 14:10:26 +01001103 {
1104 ret = mbedtls_md_info_update_func( md_info )( ctx.md_ctx,
1105 buf, n );
1106 if( ret != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001107 goto cleanup;
Hanno Becker530387e2019-07-17 14:10:26 +01001108 }
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +02001109
1110 if( ferror( f ) != 0 )
Hanno Becker530387e2019-07-17 14:10:26 +01001111 {
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +02001112 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Hanno Becker530387e2019-07-17 14:10:26 +01001113 }
Andres Amaya Garciaeb132b62017-06-23 16:30:31 +01001114 else
Hanno Becker530387e2019-07-17 14:10:26 +01001115 {
1116 ret = mbedtls_md_info_finish_func( md_info )( ctx.md_ctx,
1117 output );
1118 }
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +02001119
1120cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001121 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +02001122 fclose( f );
1123 mbedtls_md_free( &ctx );
Paul Bakker9c021ad2011-06-09 15:55:11 +00001124
Paul Bakker8913f822012-01-14 18:07:41 +00001125 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +00001126}
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +02001127#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +00001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +00001130{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001131 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +01001133 unsigned char *ipad, *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001134 size_t i;
1135
Hanno Becker530387e2019-07-17 14:10:26 +01001136 mbedtls_md_starts_func_t *starts;
1137 mbedtls_md_update_func_t *update;
1138 mbedtls_md_finish_func_t *finish;
1139
Hanno Beckerd3827c72019-09-03 12:56:37 +01001140 mbedtls_md_handle_t md_info;
1141
1142 if( ctx == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +00001144
Hanno Beckerd3827c72019-09-03 12:56:37 +01001145 md_info = mbedtls_md_get_handle( ctx );
1146 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
1147 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Hanno Becker530387e2019-07-17 14:10:26 +01001148
Hanno Beckerd3827c72019-09-03 12:56:37 +01001149 starts = mbedtls_md_info_starts_func( md_info );
1150 update = mbedtls_md_info_update_func( md_info );
1151 finish = mbedtls_md_info_finish_func( md_info );
1152
1153 if( keylen > (size_t) mbedtls_md_info_block_size( md_info ) )
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001154 {
Hanno Becker530387e2019-07-17 14:10:26 +01001155 if( ( ret = starts( ctx->md_ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001156 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001157
Hanno Becker530387e2019-07-17 14:10:26 +01001158 if( ( ret = update( ctx->md_ctx, key, keylen ) ) )
1159 goto cleanup;
1160
1161 if( ( ret = finish( ctx->md_ctx, sum ) ) != 0 )
1162 goto cleanup;
1163
Hanno Beckerd3827c72019-09-03 12:56:37 +01001164 keylen = mbedtls_md_info_size( md_info );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001165 key = sum;
1166 }
1167
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +01001168 ipad = (unsigned char *) ctx->hmac_ctx;
Hanno Beckerd3827c72019-09-03 12:56:37 +01001169 opad = (unsigned char *) ctx->hmac_ctx +
1170 mbedtls_md_info_block_size( md_info );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +01001171
Hanno Beckerd3827c72019-09-03 12:56:37 +01001172 memset( ipad, 0x36, mbedtls_md_info_block_size( md_info ) );
1173 memset( opad, 0x5C, mbedtls_md_info_block_size( md_info ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001174
1175 for( i = 0; i < keylen; i++ )
1176 {
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +01001177 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
1178 opad[i] = (unsigned char)( opad[i] ^ key[i] );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001179 }
1180
Hanno Becker530387e2019-07-17 14:10:26 +01001181 if( ( ret = starts( ctx->md_ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001182 goto cleanup;
Hanno Becker530387e2019-07-17 14:10:26 +01001183
Hanno Beckerd3827c72019-09-03 12:56:37 +01001184 if( ( ret = update( ctx->md_ctx, ipad,
1185 mbedtls_md_info_block_size( md_info ) ) ) != 0 )
1186 {
Andres Amaya Garcia42e5e102017-07-20 16:27:03 +01001187 goto cleanup;
Hanno Beckerd3827c72019-09-03 12:56:37 +01001188 }
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001189
1190cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001191 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001192
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001193 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +00001194}
1195
Hanno Beckerd3827c72019-09-03 12:56:37 +01001196int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx,
1197 const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +00001198{
Hanno Beckerd3827c72019-09-03 12:56:37 +01001199 mbedtls_md_handle_t md_info;
Paul Bakker17373852011-01-06 14:20:01 +00001200
Hanno Beckerd3827c72019-09-03 12:56:37 +01001201 if( ctx == NULL || ctx->hmac_ctx == NULL )
1202 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
1203
1204 md_info = mbedtls_md_get_handle( ctx );
1205 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
1206 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
1207
1208 return( mbedtls_md_info_update_func( md_info )(
Hanno Becker530387e2019-07-17 14:10:26 +01001209 ctx->md_ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +00001210}
1211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +00001213{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001214 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +01001216 unsigned char *opad;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001217
Hanno Becker530387e2019-07-17 14:10:26 +01001218 mbedtls_md_starts_func_t *starts;
1219 mbedtls_md_update_func_t *update;
1220 mbedtls_md_finish_func_t *finish;
1221
Hanno Beckerd3827c72019-09-03 12:56:37 +01001222 mbedtls_md_handle_t md_info;
1223
1224 if( ctx == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +00001226
Hanno Beckerd3827c72019-09-03 12:56:37 +01001227 md_info = mbedtls_md_get_handle( ctx );
1228 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
1229 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +01001230
Hanno Beckerd3827c72019-09-03 12:56:37 +01001231 starts = mbedtls_md_info_starts_func( md_info );
1232 update = mbedtls_md_info_update_func( md_info );
1233 finish = mbedtls_md_info_finish_func( md_info );
1234
1235 opad = (unsigned char *) ctx->hmac_ctx +
1236 mbedtls_md_info_block_size( md_info );
Hanno Becker530387e2019-07-17 14:10:26 +01001237
1238 if( ( ret = finish( ctx->md_ctx, tmp ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001239 return( ret );
Hanno Becker530387e2019-07-17 14:10:26 +01001240
1241 if( ( ret = starts( ctx->md_ctx ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001242 return( ret );
Hanno Becker530387e2019-07-17 14:10:26 +01001243
Hanno Beckerd3827c72019-09-03 12:56:37 +01001244 if( ( ret = update( ctx->md_ctx, opad,
1245 mbedtls_md_info_block_size( md_info ) ) ) != 0 )
1246 {
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001247 return( ret );
Hanno Beckerd3827c72019-09-03 12:56:37 +01001248 }
Hanno Becker530387e2019-07-17 14:10:26 +01001249
Hanno Beckerd3827c72019-09-03 12:56:37 +01001250 if( ( ret = update( ctx->md_ctx, tmp,
1251 mbedtls_md_info_size( md_info ) ) ) != 0 )
1252 {
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001253 return( ret );
Hanno Beckerd3827c72019-09-03 12:56:37 +01001254 }
Hanno Becker530387e2019-07-17 14:10:26 +01001255
1256 if( ( ret = finish( ctx->md_ctx, output ) ) != 0 )
1257 return( ret );
1258
1259 return( 0 );
Paul Bakker17373852011-01-06 14:20:01 +00001260}
1261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
Paul Bakker17373852011-01-06 14:20:01 +00001263{
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001264 int ret;
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +01001265 unsigned char *ipad;
1266
Hanno Beckerd3827c72019-09-03 12:56:37 +01001267 mbedtls_md_handle_t md_info;
1268
1269 if( ctx == NULL || ctx->hmac_ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Hanno Beckerd3827c72019-09-03 12:56:37 +01001271
1272 md_info = mbedtls_md_get_handle( ctx );
1273 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
1274 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +00001275
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +01001276 ipad = (unsigned char *) ctx->hmac_ctx;
1277
Hanno Beckerd3827c72019-09-03 12:56:37 +01001278 ret = mbedtls_md_info_starts_func( md_info )( ctx->md_ctx );
Hanno Becker530387e2019-07-17 14:10:26 +01001279 if( ret != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001280 return( ret );
Hanno Becker530387e2019-07-17 14:10:26 +01001281
Hanno Beckerd3827c72019-09-03 12:56:37 +01001282 ret = mbedtls_md_info_update_func( md_info )(
1283 ctx->md_ctx, ipad,
1284 mbedtls_md_info_block_size( md_info ) );
Hanno Becker530387e2019-07-17 14:10:26 +01001285 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +00001286}
1287
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001288int mbedtls_md_hmac( mbedtls_md_handle_t md_info,
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001289 const unsigned char *key, size_t keylen,
1290 const unsigned char *input, size_t ilen,
1291 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +00001292{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001294 int ret;
1295
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001296 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker17373852011-01-06 14:20:01 +00001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001302 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001303
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001304 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
1305 goto cleanup;
1306 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
1307 goto cleanup;
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +01001308 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
1309 goto cleanup;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +01001310
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001311cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +00001313
Andres Amaya Garcia0dd4fa02017-06-28 14:16:07 +01001314 return( ret );
Paul Bakker17373852011-01-06 14:20:01 +00001315}
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +01001318{
Hanno Beckerd3827c72019-09-03 12:56:37 +01001319 mbedtls_md_handle_t md_info;
1320 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Paul Bakker1bd3ae82013-03-13 10:26:44 +01001322
Hanno Beckerd3827c72019-09-03 12:56:37 +01001323 md_info = mbedtls_md_get_handle( ctx );
1324 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
1325 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
1326
1327 return( mbedtls_md_info_process_func( md_info )(
Hanno Becker530387e2019-07-17 14:10:26 +01001328 ctx->md_ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +01001329}
1330
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001331unsigned char mbedtls_md_get_size( mbedtls_md_handle_t md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01001332{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001333 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01001334 return( 0 );
1335
Hanno Becker530387e2019-07-17 14:10:26 +01001336 return mbedtls_md_info_size( md_info );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01001337}
1338
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001339mbedtls_md_type_t mbedtls_md_get_type( mbedtls_md_handle_t md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01001340{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001341 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01001343
Hanno Becker530387e2019-07-17 14:10:26 +01001344 return mbedtls_md_info_type( md_info );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01001345}
1346
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001347const char *mbedtls_md_get_name( mbedtls_md_handle_t md_info )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01001348{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001349 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01001350 return( NULL );
1351
Hanno Becker530387e2019-07-17 14:10:26 +01001352 return mbedtls_md_info_name( md_info );
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01001353}
1354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#endif /* MBEDTLS_MD_C */