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