1 /**
2  * \file md.c
3  *
4  * \brief Generic message digest wrapper for mbed TLS
5  *
6  * \author Adriaan de Jong <dejong@fox-it.com>
7  *
8  *  Copyright The Mbed TLS Contributors
9  *  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.
22  */
23 
24 #include "common.h"
25 
26 #if defined(MBEDTLS_MD_C)
27 
28 #include "mbedtls/md.h"
29 #include "mbedtls/md_internal.h"
30 #include "mbedtls/platform_util.h"
31 #include "mbedtls/error.h"
32 
33 #include "mbedtls/md2.h"
34 #include "mbedtls/md4.h"
35 #include "mbedtls/md5.h"
36 #include "mbedtls/ripemd160.h"
37 #include "mbedtls/sha1.h"
38 #include "mbedtls/sha256.h"
39 #include "mbedtls/sha512.h"
40 
41 #if defined(MBEDTLS_PLATFORM_C)
42 #include "mbedtls/platform.h"
43 #else
44 #include <stdlib.h>
45 #define mbedtls_calloc    calloc
46 #define mbedtls_free       free
47 #endif
48 
49 #include <string.h>
50 
51 #if defined(MBEDTLS_FS_IO)
52 #include <stdio.h>
53 #endif
54 
55 #if defined(MBEDTLS_MD2_C)
56 const mbedtls_md_info_t mbedtls_md2_info = {
57     "MD2",
58     MBEDTLS_MD_MD2,
59     16,
60     16,
61 };
62 #endif
63 
64 #if defined(MBEDTLS_MD4_C)
65 const mbedtls_md_info_t mbedtls_md4_info = {
66     "MD4",
67     MBEDTLS_MD_MD4,
68     16,
69     64,
70 };
71 #endif
72 
73 #if defined(MBEDTLS_MD5_C)
74 const mbedtls_md_info_t mbedtls_md5_info = {
75     "MD5",
76     MBEDTLS_MD_MD5,
77     16,
78     64,
79 };
80 #endif
81 
82 #if defined(MBEDTLS_RIPEMD160_C)
83 const mbedtls_md_info_t mbedtls_ripemd160_info = {
84     "RIPEMD160",
85     MBEDTLS_MD_RIPEMD160,
86     20,
87     64,
88 };
89 #endif
90 
91 #if defined(MBEDTLS_SHA1_C)
92 const mbedtls_md_info_t mbedtls_sha1_info = {
93     "SHA1",
94     MBEDTLS_MD_SHA1,
95     20,
96     64,
97 };
98 #endif
99 
100 #if defined(MBEDTLS_SHA256_C)
101 const mbedtls_md_info_t mbedtls_sha224_info = {
102     "SHA224",
103     MBEDTLS_MD_SHA224,
104     28,
105     64,
106 };
107 
108 const mbedtls_md_info_t mbedtls_sha256_info = {
109     "SHA256",
110     MBEDTLS_MD_SHA256,
111     32,
112     64,
113 };
114 #endif
115 
116 #if defined(MBEDTLS_SHA512_C)
117 #if !defined(MBEDTLS_SHA512_NO_SHA384)
118 const mbedtls_md_info_t mbedtls_sha384_info = {
119     "SHA384",
120     MBEDTLS_MD_SHA384,
121     48,
122     128,
123 };
124 #endif
125 
126 const mbedtls_md_info_t mbedtls_sha512_info = {
127     "SHA512",
128     MBEDTLS_MD_SHA512,
129     64,
130     128,
131 };
132 #endif
133 
134 /*
135  * Reminder: update profiles in x509_crt.c when adding a new hash!
136  */
137 static const int supported_digests[] = {
138 
139 #if defined(MBEDTLS_SHA512_C)
140         MBEDTLS_MD_SHA512,
141 #if !defined(MBEDTLS_SHA512_NO_SHA384)
142         MBEDTLS_MD_SHA384,
143 #endif
144 #endif
145 
146 #if defined(MBEDTLS_SHA256_C)
147         MBEDTLS_MD_SHA256,
148         MBEDTLS_MD_SHA224,
149 #endif
150 
151 #if defined(MBEDTLS_SHA1_C)
152         MBEDTLS_MD_SHA1,
153 #endif
154 
155 #if defined(MBEDTLS_RIPEMD160_C)
156         MBEDTLS_MD_RIPEMD160,
157 #endif
158 
159 #if defined(MBEDTLS_MD5_C)
160         MBEDTLS_MD_MD5,
161 #endif
162 
163 #if defined(MBEDTLS_MD4_C)
164         MBEDTLS_MD_MD4,
165 #endif
166 
167 #if defined(MBEDTLS_MD2_C)
168         MBEDTLS_MD_MD2,
169 #endif
170 
171         MBEDTLS_MD_NONE
172 };
173 
mbedtls_md_list(void)174 const int *mbedtls_md_list( void )
175 {
176     return( supported_digests );
177 }
178 
mbedtls_md_info_from_string(const char * md_name)179 const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
180 {
181     if( NULL == md_name )
182         return( NULL );
183 
184     /* Get the appropriate digest information */
185 #if defined(MBEDTLS_MD2_C)
186     if( !strcmp( "MD2", md_name ) )
187         return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
188 #endif
189 #if defined(MBEDTLS_MD4_C)
190     if( !strcmp( "MD4", md_name ) )
191         return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
192 #endif
193 #if defined(MBEDTLS_MD5_C)
194     if( !strcmp( "MD5", md_name ) )
195         return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
196 #endif
197 #if defined(MBEDTLS_RIPEMD160_C)
198     if( !strcmp( "RIPEMD160", md_name ) )
199         return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
200 #endif
201 #if defined(MBEDTLS_SHA1_C)
202     if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
203         return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
204 #endif
205 #if defined(MBEDTLS_SHA256_C)
206     if( !strcmp( "SHA224", md_name ) )
207         return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
208     if( !strcmp( "SHA256", md_name ) )
209         return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
210 #endif
211 #if defined(MBEDTLS_SHA512_C)
212 #if !defined(MBEDTLS_SHA512_NO_SHA384)
213     if( !strcmp( "SHA384", md_name ) )
214         return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
215 #endif
216     if( !strcmp( "SHA512", md_name ) )
217         return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
218 #endif
219     return( NULL );
220 }
221 
mbedtls_md_info_from_type(mbedtls_md_type_t md_type)222 const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
223 {
224     switch( md_type )
225     {
226 #if defined(MBEDTLS_MD2_C)
227         case MBEDTLS_MD_MD2:
228             return( &mbedtls_md2_info );
229 #endif
230 #if defined(MBEDTLS_MD4_C)
231         case MBEDTLS_MD_MD4:
232             return( &mbedtls_md4_info );
233 #endif
234 #if defined(MBEDTLS_MD5_C)
235         case MBEDTLS_MD_MD5:
236             return( &mbedtls_md5_info );
237 #endif
238 #if defined(MBEDTLS_RIPEMD160_C)
239         case MBEDTLS_MD_RIPEMD160:
240             return( &mbedtls_ripemd160_info );
241 #endif
242 #if defined(MBEDTLS_SHA1_C)
243         case MBEDTLS_MD_SHA1:
244             return( &mbedtls_sha1_info );
245 #endif
246 #if defined(MBEDTLS_SHA256_C)
247         case MBEDTLS_MD_SHA224:
248             return( &mbedtls_sha224_info );
249         case MBEDTLS_MD_SHA256:
250             return( &mbedtls_sha256_info );
251 #endif
252 #if defined(MBEDTLS_SHA512_C)
253 #if !defined(MBEDTLS_SHA512_NO_SHA384)
254         case MBEDTLS_MD_SHA384:
255             return( &mbedtls_sha384_info );
256 #endif
257         case MBEDTLS_MD_SHA512:
258             return( &mbedtls_sha512_info );
259 #endif
260         default:
261             return( NULL );
262     }
263 }
264 
mbedtls_md_init(mbedtls_md_context_t * ctx)265 void mbedtls_md_init( mbedtls_md_context_t *ctx )
266 {
267     memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
268 }
269 
mbedtls_md_free(mbedtls_md_context_t * ctx)270 void mbedtls_md_free( mbedtls_md_context_t *ctx )
271 {
272     if( ctx == NULL || ctx->md_info == NULL )
273         return;
274 
275     if( ctx->md_ctx != NULL )
276     {
277         switch( ctx->md_info->type )
278         {
279 #if defined(MBEDTLS_MD2_C)
280             case MBEDTLS_MD_MD2:
281                 mbedtls_md2_free( ctx->md_ctx );
282                 break;
283 #endif
284 #if defined(MBEDTLS_MD4_C)
285             case MBEDTLS_MD_MD4:
286                 mbedtls_md4_free( ctx->md_ctx );
287                 break;
288 #endif
289 #if defined(MBEDTLS_MD5_C)
290             case MBEDTLS_MD_MD5:
291                 mbedtls_md5_free( ctx->md_ctx );
292                 break;
293 #endif
294 #if defined(MBEDTLS_RIPEMD160_C)
295             case MBEDTLS_MD_RIPEMD160:
296                 mbedtls_ripemd160_free( ctx->md_ctx );
297                 break;
298 #endif
299 #if defined(MBEDTLS_SHA1_C)
300             case MBEDTLS_MD_SHA1:
301                 mbedtls_sha1_free( ctx->md_ctx );
302                 break;
303 #endif
304 #if defined(MBEDTLS_SHA256_C)
305             case MBEDTLS_MD_SHA224:
306             case MBEDTLS_MD_SHA256:
307                 mbedtls_sha256_free( ctx->md_ctx );
308                 break;
309 #endif
310 #if defined(MBEDTLS_SHA512_C)
311 #if !defined(MBEDTLS_SHA512_NO_SHA384)
312             case MBEDTLS_MD_SHA384:
313 #endif
314             case MBEDTLS_MD_SHA512:
315                 mbedtls_sha512_free( ctx->md_ctx );
316                 break;
317 #endif
318             default:
319                 /* Shouldn't happen */
320                 break;
321         }
322         mbedtls_free( ctx->md_ctx );
323     }
324 
325     if( ctx->hmac_ctx != NULL )
326     {
327         mbedtls_platform_zeroize( ctx->hmac_ctx,
328                                   2 * ctx->md_info->block_size );
329         mbedtls_free( ctx->hmac_ctx );
330     }
331 
332     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
333 }
334 
mbedtls_md_clone(mbedtls_md_context_t * dst,const mbedtls_md_context_t * src)335 int mbedtls_md_clone( mbedtls_md_context_t *dst,
336                       const mbedtls_md_context_t *src )
337 {
338     if( dst == NULL || dst->md_info == NULL ||
339         src == NULL || src->md_info == NULL ||
340         dst->md_info != src->md_info )
341     {
342         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
343     }
344 
345     switch( src->md_info->type )
346     {
347 #if defined(MBEDTLS_MD2_C)
348         case MBEDTLS_MD_MD2:
349             mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
350             break;
351 #endif
352 #if defined(MBEDTLS_MD4_C)
353         case MBEDTLS_MD_MD4:
354             mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
355             break;
356 #endif
357 #if defined(MBEDTLS_MD5_C)
358         case MBEDTLS_MD_MD5:
359             mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
360             break;
361 #endif
362 #if defined(MBEDTLS_RIPEMD160_C)
363         case MBEDTLS_MD_RIPEMD160:
364             mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
365             break;
366 #endif
367 #if defined(MBEDTLS_SHA1_C)
368         case MBEDTLS_MD_SHA1:
369             mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
370             break;
371 #endif
372 #if defined(MBEDTLS_SHA256_C)
373         case MBEDTLS_MD_SHA224:
374         case MBEDTLS_MD_SHA256:
375             mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
376             break;
377 #endif
378 #if defined(MBEDTLS_SHA512_C)
379 #if !defined(MBEDTLS_SHA512_NO_SHA384)
380         case MBEDTLS_MD_SHA384:
381 #endif
382         case MBEDTLS_MD_SHA512:
383             mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
384             break;
385 #endif
386         default:
387             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
388     }
389 
390     if( dst->hmac_ctx != NULL && src->hmac_ctx != NULL )
391         memcpy( dst->hmac_ctx, src->hmac_ctx, 2 * src->md_info->block_size );
392 
393     return( 0 );
394 }
395 
396 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_md_init_ctx(mbedtls_md_context_t * ctx,const mbedtls_md_info_t * md_info)397 int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
398 {
399     return mbedtls_md_setup( ctx, md_info, 1 );
400 }
401 #endif
402 
403 #define ALLOC( type )                                                   \
404     do {                                                                \
405         ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
406         if( ctx->md_ctx == NULL )                                       \
407             return( MBEDTLS_ERR_MD_ALLOC_FAILED );                      \
408         mbedtls_##type##_init( ctx->md_ctx );                           \
409     }                                                                   \
410     while( 0 )
411 
mbedtls_md_setup(mbedtls_md_context_t * ctx,const mbedtls_md_info_t * md_info,int hmac)412 int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
413 {
414     if( md_info == NULL || ctx == NULL )
415         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
416 
417     ctx->md_info = md_info;
418     ctx->md_ctx = NULL;
419     ctx->hmac_ctx = NULL;
420 
421     switch( md_info->type )
422     {
423 #if defined(MBEDTLS_MD2_C)
424         case MBEDTLS_MD_MD2:
425             ALLOC( md2 );
426             break;
427 #endif
428 #if defined(MBEDTLS_MD4_C)
429         case MBEDTLS_MD_MD4:
430             ALLOC( md4 );
431             break;
432 #endif
433 #if defined(MBEDTLS_MD5_C)
434         case MBEDTLS_MD_MD5:
435             ALLOC( md5 );
436             break;
437 #endif
438 #if defined(MBEDTLS_RIPEMD160_C)
439         case MBEDTLS_MD_RIPEMD160:
440             ALLOC( ripemd160 );
441             break;
442 #endif
443 #if defined(MBEDTLS_SHA1_C)
444         case MBEDTLS_MD_SHA1:
445             ALLOC( sha1 );
446             break;
447 #endif
448 #if defined(MBEDTLS_SHA256_C)
449         case MBEDTLS_MD_SHA224:
450         case MBEDTLS_MD_SHA256:
451             ALLOC( sha256 );
452             break;
453 #endif
454 #if defined(MBEDTLS_SHA512_C)
455 #if !defined(MBEDTLS_SHA512_NO_SHA384)
456         case MBEDTLS_MD_SHA384:
457 #endif
458         case MBEDTLS_MD_SHA512:
459             ALLOC( sha512 );
460             break;
461 #endif
462         default:
463             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
464     }
465 
466     if( hmac != 0 )
467     {
468         ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
469         if( ctx->hmac_ctx == NULL )
470         {
471             mbedtls_md_free( ctx );
472             return( MBEDTLS_ERR_MD_ALLOC_FAILED );
473         }
474     }
475 
476     return( 0 );
477 }
478 #undef ALLOC
479 
mbedtls_md_starts(mbedtls_md_context_t * ctx)480 int mbedtls_md_starts( mbedtls_md_context_t *ctx )
481 {
482     if( ctx == NULL || ctx->md_info == NULL )
483         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
484 
485     switch( ctx->md_info->type )
486     {
487 #if defined(MBEDTLS_MD2_C)
488         case MBEDTLS_MD_MD2:
489             return( mbedtls_md2_starts_ret( ctx->md_ctx ) );
490 #endif
491 #if defined(MBEDTLS_MD4_C)
492         case MBEDTLS_MD_MD4:
493             return( mbedtls_md4_starts_ret( ctx->md_ctx ) );
494 #endif
495 #if defined(MBEDTLS_MD5_C)
496         case MBEDTLS_MD_MD5:
497             return( mbedtls_md5_starts_ret( ctx->md_ctx ) );
498 #endif
499 #if defined(MBEDTLS_RIPEMD160_C)
500         case MBEDTLS_MD_RIPEMD160:
501             return( mbedtls_ripemd160_starts_ret( ctx->md_ctx ) );
502 #endif
503 #if defined(MBEDTLS_SHA1_C)
504         case MBEDTLS_MD_SHA1:
505             return( mbedtls_sha1_starts_ret( ctx->md_ctx ) );
506 #endif
507 #if defined(MBEDTLS_SHA256_C)
508         case MBEDTLS_MD_SHA224:
509             return( mbedtls_sha256_starts_ret( ctx->md_ctx, 1 ) );
510         case MBEDTLS_MD_SHA256:
511             return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
512 #endif
513 #if defined(MBEDTLS_SHA512_C)
514 #if !defined(MBEDTLS_SHA512_NO_SHA384)
515         case MBEDTLS_MD_SHA384:
516             return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
517 #endif
518         case MBEDTLS_MD_SHA512:
519             return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
520 #endif
521         default:
522             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
523     }
524 }
525 
mbedtls_md_update(mbedtls_md_context_t * ctx,const unsigned char * input,size_t ilen)526 int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
527 {
528     if( ctx == NULL || ctx->md_info == NULL )
529         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
530 
531     switch( ctx->md_info->type )
532     {
533 #if defined(MBEDTLS_MD2_C)
534         case MBEDTLS_MD_MD2:
535             return( mbedtls_md2_update_ret( ctx->md_ctx, input, ilen ) );
536 #endif
537 #if defined(MBEDTLS_MD4_C)
538         case MBEDTLS_MD_MD4:
539             return( mbedtls_md4_update_ret( ctx->md_ctx, input, ilen ) );
540 #endif
541 #if defined(MBEDTLS_MD5_C)
542         case MBEDTLS_MD_MD5:
543             return( mbedtls_md5_update_ret( ctx->md_ctx, input, ilen ) );
544 #endif
545 #if defined(MBEDTLS_RIPEMD160_C)
546         case MBEDTLS_MD_RIPEMD160:
547             return( mbedtls_ripemd160_update_ret( ctx->md_ctx, input, ilen ) );
548 #endif
549 #if defined(MBEDTLS_SHA1_C)
550         case MBEDTLS_MD_SHA1:
551             return( mbedtls_sha1_update_ret( ctx->md_ctx, input, ilen ) );
552 #endif
553 #if defined(MBEDTLS_SHA256_C)
554         case MBEDTLS_MD_SHA224:
555         case MBEDTLS_MD_SHA256:
556             return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
557 #endif
558 #if defined(MBEDTLS_SHA512_C)
559 #if !defined(MBEDTLS_SHA512_NO_SHA384)
560         case MBEDTLS_MD_SHA384:
561 #endif
562         case MBEDTLS_MD_SHA512:
563             return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
564 #endif
565         default:
566             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
567     }
568 }
569 
mbedtls_md_finish(mbedtls_md_context_t * ctx,unsigned char * output)570 int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
571 {
572     if( ctx == NULL || ctx->md_info == NULL )
573         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
574 
575     switch( ctx->md_info->type )
576     {
577 #if defined(MBEDTLS_MD2_C)
578         case MBEDTLS_MD_MD2:
579             return( mbedtls_md2_finish_ret( ctx->md_ctx, output ) );
580 #endif
581 #if defined(MBEDTLS_MD4_C)
582         case MBEDTLS_MD_MD4:
583             return( mbedtls_md4_finish_ret( ctx->md_ctx, output ) );
584 #endif
585 #if defined(MBEDTLS_MD5_C)
586         case MBEDTLS_MD_MD5:
587             return( mbedtls_md5_finish_ret( ctx->md_ctx, output ) );
588 #endif
589 #if defined(MBEDTLS_RIPEMD160_C)
590         case MBEDTLS_MD_RIPEMD160:
591             return( mbedtls_ripemd160_finish_ret( ctx->md_ctx, output ) );
592 #endif
593 #if defined(MBEDTLS_SHA1_C)
594         case MBEDTLS_MD_SHA1:
595             return( mbedtls_sha1_finish_ret( ctx->md_ctx, output ) );
596 #endif
597 #if defined(MBEDTLS_SHA256_C)
598         case MBEDTLS_MD_SHA224:
599         case MBEDTLS_MD_SHA256:
600             return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
601 #endif
602 #if defined(MBEDTLS_SHA512_C)
603 #if !defined(MBEDTLS_SHA512_NO_SHA384)
604         case MBEDTLS_MD_SHA384:
605 #endif
606         case MBEDTLS_MD_SHA512:
607             return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
608 #endif
609         default:
610             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
611     }
612 }
613 
mbedtls_md(const mbedtls_md_info_t * md_info,const unsigned char * input,size_t ilen,unsigned char * output)614 int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
615             unsigned char *output )
616 {
617     if( md_info == NULL )
618         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
619 
620     switch( md_info->type )
621     {
622 #if defined(MBEDTLS_MD2_C)
623         case MBEDTLS_MD_MD2:
624             return( mbedtls_md2_ret( input, ilen, output ) );
625 #endif
626 #if defined(MBEDTLS_MD4_C)
627         case MBEDTLS_MD_MD4:
628             return( mbedtls_md4_ret( input, ilen, output ) );
629 #endif
630 #if defined(MBEDTLS_MD5_C)
631         case MBEDTLS_MD_MD5:
632             return( mbedtls_md5_ret( input, ilen, output ) );
633 #endif
634 #if defined(MBEDTLS_RIPEMD160_C)
635         case MBEDTLS_MD_RIPEMD160:
636             return( mbedtls_ripemd160_ret( input, ilen, output ) );
637 #endif
638 #if defined(MBEDTLS_SHA1_C)
639         case MBEDTLS_MD_SHA1:
640             return( mbedtls_sha1_ret( input, ilen, output ) );
641 #endif
642 #if defined(MBEDTLS_SHA256_C)
643         case MBEDTLS_MD_SHA224:
644             return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
645         case MBEDTLS_MD_SHA256:
646             return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
647 #endif
648 #if defined(MBEDTLS_SHA512_C)
649 #if !defined(MBEDTLS_SHA512_NO_SHA384)
650         case MBEDTLS_MD_SHA384:
651             return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
652 #endif
653         case MBEDTLS_MD_SHA512:
654             return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
655 #endif
656         default:
657             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
658     }
659 }
660 
661 #if defined(MBEDTLS_FS_IO)
mbedtls_md_file(const mbedtls_md_info_t * md_info,const char * path,unsigned char * output)662 int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
663 {
664     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
665     FILE *f;
666     size_t n;
667     mbedtls_md_context_t ctx;
668     unsigned char buf[1024];
669 
670     if( md_info == NULL )
671         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
672 
673     if( ( f = fopen( path, "rb" ) ) == NULL )
674         return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
675 
676     mbedtls_md_init( &ctx );
677 
678     if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
679         goto cleanup;
680 
681     if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
682         goto cleanup;
683 
684     while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
685         if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
686             goto cleanup;
687 
688     if( ferror( f ) != 0 )
689         ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
690     else
691         ret = mbedtls_md_finish( &ctx, output );
692 
693 cleanup:
694     mbedtls_platform_zeroize( buf, sizeof( buf ) );
695     fclose( f );
696     mbedtls_md_free( &ctx );
697 
698     return( ret );
699 }
700 #endif /* MBEDTLS_FS_IO */
701 
mbedtls_md_hmac_starts(mbedtls_md_context_t * ctx,const unsigned char * key,size_t keylen)702 int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
703 {
704     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
705     unsigned char sum[MBEDTLS_MD_MAX_SIZE];
706     unsigned char *ipad, *opad;
707     size_t i;
708 
709     if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
710         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
711 
712     if( keylen > (size_t) ctx->md_info->block_size )
713     {
714         if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
715             goto cleanup;
716         if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
717             goto cleanup;
718         if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
719             goto cleanup;
720 
721         keylen = ctx->md_info->size;
722         key = sum;
723     }
724 
725     ipad = (unsigned char *) ctx->hmac_ctx;
726     opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
727 
728     memset( ipad, 0x36, ctx->md_info->block_size );
729     memset( opad, 0x5C, ctx->md_info->block_size );
730 
731     for( i = 0; i < keylen; i++ )
732     {
733         ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
734         opad[i] = (unsigned char)( opad[i] ^ key[i] );
735     }
736 
737     if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
738         goto cleanup;
739     if( ( ret = mbedtls_md_update( ctx, ipad,
740                                    ctx->md_info->block_size ) ) != 0 )
741         goto cleanup;
742 
743 cleanup:
744     mbedtls_platform_zeroize( sum, sizeof( sum ) );
745 
746     return( ret );
747 }
748 
mbedtls_md_hmac_update(mbedtls_md_context_t * ctx,const unsigned char * input,size_t ilen)749 int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
750 {
751     if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
752         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
753 
754     return( mbedtls_md_update( ctx, input, ilen ) );
755 }
756 
mbedtls_md_hmac_finish(mbedtls_md_context_t * ctx,unsigned char * output)757 int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
758 {
759     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
760     unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
761     unsigned char *opad;
762 
763     if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
764         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
765 
766     opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
767 
768     if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
769         return( ret );
770     if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
771         return( ret );
772     if( ( ret = mbedtls_md_update( ctx, opad,
773                                    ctx->md_info->block_size ) ) != 0 )
774         return( ret );
775     if( ( ret = mbedtls_md_update( ctx, tmp,
776                                    ctx->md_info->size ) ) != 0 )
777         return( ret );
778     return( mbedtls_md_finish( ctx, output ) );
779 }
780 
mbedtls_md_hmac_reset(mbedtls_md_context_t * ctx)781 int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
782 {
783     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
784     unsigned char *ipad;
785 
786     if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
787         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
788 
789     ipad = (unsigned char *) ctx->hmac_ctx;
790 
791     if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
792         return( ret );
793     return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
794 }
795 
mbedtls_md_hmac(const mbedtls_md_info_t * md_info,const unsigned char * key,size_t keylen,const unsigned char * input,size_t ilen,unsigned char * output)796 int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
797                      const unsigned char *key, size_t keylen,
798                      const unsigned char *input, size_t ilen,
799                      unsigned char *output )
800 {
801     mbedtls_md_context_t ctx;
802     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
803 
804     if( md_info == NULL )
805         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
806 
807     mbedtls_md_init( &ctx );
808 
809     if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
810         goto cleanup;
811 
812     if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
813         goto cleanup;
814     if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
815         goto cleanup;
816     if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
817         goto cleanup;
818 
819 cleanup:
820     mbedtls_md_free( &ctx );
821 
822     return( ret );
823 }
824 
mbedtls_md_process(mbedtls_md_context_t * ctx,const unsigned char * data)825 int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
826 {
827     if( ctx == NULL || ctx->md_info == NULL )
828         return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
829 
830     switch( ctx->md_info->type )
831     {
832 #if defined(MBEDTLS_MD2_C)
833         case MBEDTLS_MD_MD2:
834             return( mbedtls_internal_md2_process( ctx->md_ctx ) );
835 #endif
836 #if defined(MBEDTLS_MD4_C)
837         case MBEDTLS_MD_MD4:
838             return( mbedtls_internal_md4_process( ctx->md_ctx, data ) );
839 #endif
840 #if defined(MBEDTLS_MD5_C)
841         case MBEDTLS_MD_MD5:
842             return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
843 #endif
844 #if defined(MBEDTLS_RIPEMD160_C)
845         case MBEDTLS_MD_RIPEMD160:
846             return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
847 #endif
848 #if defined(MBEDTLS_SHA1_C)
849         case MBEDTLS_MD_SHA1:
850             return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
851 #endif
852 #if defined(MBEDTLS_SHA256_C)
853         case MBEDTLS_MD_SHA224:
854         case MBEDTLS_MD_SHA256:
855             return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
856 #endif
857 #if defined(MBEDTLS_SHA512_C)
858 #if !defined(MBEDTLS_SHA512_NO_SHA384)
859         case MBEDTLS_MD_SHA384:
860 #endif
861         case MBEDTLS_MD_SHA512:
862             return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
863 #endif
864         default:
865             return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
866     }
867 }
868 
mbedtls_md_get_size(const mbedtls_md_info_t * md_info)869 unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
870 {
871     if( md_info == NULL )
872         return( 0 );
873 
874     return md_info->size;
875 }
876 
mbedtls_md_get_type(const mbedtls_md_info_t * md_info)877 mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
878 {
879     if( md_info == NULL )
880         return( MBEDTLS_MD_NONE );
881 
882     return md_info->type;
883 }
884 
mbedtls_md_get_name(const mbedtls_md_info_t * md_info)885 const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
886 {
887     if( md_info == NULL )
888         return( NULL );
889 
890     return md_info->name;
891 }
892 
893 #endif /* MBEDTLS_MD_C */
894