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