Coverage Report

Created: 2025-07-01 06:54

/work/mbedtls-2.28.8/library/md.c
Line
Count
Source (jump to first uncovered line)
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
156
const int *mbedtls_md_list(void)
157
0
{
158
0
    return supported_digests;
159
0
}
160
161
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
162
0
{
163
0
    if (NULL == md_name) {
164
0
        return NULL;
165
0
    }
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
0
#if defined(MBEDTLS_MD5_C)
179
0
    if (!strcmp("MD5", md_name)) {
180
0
        return mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
181
0
    }
182
0
#endif
183
0
#if defined(MBEDTLS_RIPEMD160_C)
184
0
    if (!strcmp("RIPEMD160", md_name)) {
185
0
        return mbedtls_md_info_from_type(MBEDTLS_MD_RIPEMD160);
186
0
    }
187
0
#endif
188
0
#if defined(MBEDTLS_SHA1_C)
189
0
    if (!strcmp("SHA1", md_name) || !strcmp("SHA", md_name)) {
190
0
        return mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
191
0
    }
192
0
#endif
193
0
#if defined(MBEDTLS_SHA256_C)
194
0
    if (!strcmp("SHA224", md_name)) {
195
0
        return mbedtls_md_info_from_type(MBEDTLS_MD_SHA224);
196
0
    }
197
0
    if (!strcmp("SHA256", md_name)) {
198
0
        return mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
199
0
    }
200
0
#endif
201
0
#if defined(MBEDTLS_SHA512_C)
202
0
#if !defined(MBEDTLS_SHA512_NO_SHA384)
203
0
    if (!strcmp("SHA384", md_name)) {
204
0
        return mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
205
0
    }
206
0
#endif
207
0
    if (!strcmp("SHA512", md_name)) {
208
0
        return mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
209
0
    }
210
0
#endif
211
0
    return NULL;
212
0
}
213
214
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
215
0
{
216
0
    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
0
#if defined(MBEDTLS_MD5_C)
226
0
        case MBEDTLS_MD_MD5:
227
0
            return &mbedtls_md5_info;
228
0
#endif
229
0
#if defined(MBEDTLS_RIPEMD160_C)
230
0
        case MBEDTLS_MD_RIPEMD160:
231
0
            return &mbedtls_ripemd160_info;
232
0
#endif
233
0
#if defined(MBEDTLS_SHA1_C)
234
0
        case MBEDTLS_MD_SHA1:
235
0
            return &mbedtls_sha1_info;
236
0
#endif
237
0
#if defined(MBEDTLS_SHA256_C)
238
0
        case MBEDTLS_MD_SHA224:
239
0
            return &mbedtls_sha224_info;
240
0
        case MBEDTLS_MD_SHA256:
241
0
            return &mbedtls_sha256_info;
242
0
#endif
243
0
#if defined(MBEDTLS_SHA512_C)
244
0
#if !defined(MBEDTLS_SHA512_NO_SHA384)
245
0
        case MBEDTLS_MD_SHA384:
246
0
            return &mbedtls_sha384_info;
247
0
#endif
248
0
        case MBEDTLS_MD_SHA512:
249
0
            return &mbedtls_sha512_info;
250
0
#endif
251
0
        default:
252
0
            return NULL;
253
0
    }
254
0
}
255
256
void mbedtls_md_init(mbedtls_md_context_t *ctx)
257
0
{
258
0
    memset(ctx, 0, sizeof(mbedtls_md_context_t));
259
0
}
260
261
void mbedtls_md_free(mbedtls_md_context_t *ctx)
262
0
{
263
0
    if (ctx == NULL || ctx->md_info == NULL) {
264
0
        return;
265
0
    }
266
267
0
    if (ctx->md_ctx != NULL) {
268
0
        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
0
#if defined(MBEDTLS_MD5_C)
280
0
            case MBEDTLS_MD_MD5:
281
0
                mbedtls_md5_free(ctx->md_ctx);
282
0
                break;
283
0
#endif
284
0
#if defined(MBEDTLS_RIPEMD160_C)
285
0
            case MBEDTLS_MD_RIPEMD160:
286
0
                mbedtls_ripemd160_free(ctx->md_ctx);
287
0
                break;
288
0
#endif
289
0
#if defined(MBEDTLS_SHA1_C)
290
0
            case MBEDTLS_MD_SHA1:
291
0
                mbedtls_sha1_free(ctx->md_ctx);
292
0
                break;
293
0
#endif
294
0
#if defined(MBEDTLS_SHA256_C)
295
0
            case MBEDTLS_MD_SHA224:
296
0
            case MBEDTLS_MD_SHA256:
297
0
                mbedtls_sha256_free(ctx->md_ctx);
298
0
                break;
299
0
#endif
300
0
#if defined(MBEDTLS_SHA512_C)
301
0
#if !defined(MBEDTLS_SHA512_NO_SHA384)
302
0
            case MBEDTLS_MD_SHA384:
303
0
#endif
304
0
            case MBEDTLS_MD_SHA512:
305
0
                mbedtls_sha512_free(ctx->md_ctx);
306
0
                break;
307
0
#endif
308
0
            default:
309
                /* Shouldn't happen */
310
0
                break;
311
0
        }
312
0
        mbedtls_free(ctx->md_ctx);
313
0
    }
314
315
0
    if (ctx->hmac_ctx != NULL) {
316
0
        mbedtls_platform_zeroize(ctx->hmac_ctx,
317
0
                                 2 * ctx->md_info->block_size);
318
0
        mbedtls_free(ctx->hmac_ctx);
319
0
    }
320
321
0
    mbedtls_platform_zeroize(ctx, sizeof(mbedtls_md_context_t));
322
0
}
323
324
int mbedtls_md_clone(mbedtls_md_context_t *dst,
325
                     const mbedtls_md_context_t *src)
326
0
{
327
0
    if (dst == NULL || dst->md_info == NULL ||
328
0
        src == NULL || src->md_info == NULL ||
329
0
        dst->md_info != src->md_info) {
330
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
331
0
    }
332
333
0
    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
0
#if defined(MBEDTLS_MD5_C)
345
0
        case MBEDTLS_MD_MD5:
346
0
            mbedtls_md5_clone(dst->md_ctx, src->md_ctx);
347
0
            break;
348
0
#endif
349
0
#if defined(MBEDTLS_RIPEMD160_C)
350
0
        case MBEDTLS_MD_RIPEMD160:
351
0
            mbedtls_ripemd160_clone(dst->md_ctx, src->md_ctx);
352
0
            break;
353
0
#endif
354
0
#if defined(MBEDTLS_SHA1_C)
355
0
        case MBEDTLS_MD_SHA1:
356
0
            mbedtls_sha1_clone(dst->md_ctx, src->md_ctx);
357
0
            break;
358
0
#endif
359
0
#if defined(MBEDTLS_SHA256_C)
360
0
        case MBEDTLS_MD_SHA224:
361
0
        case MBEDTLS_MD_SHA256:
362
0
            mbedtls_sha256_clone(dst->md_ctx, src->md_ctx);
363
0
            break;
364
0
#endif
365
0
#if defined(MBEDTLS_SHA512_C)
366
0
#if !defined(MBEDTLS_SHA512_NO_SHA384)
367
0
        case MBEDTLS_MD_SHA384:
368
0
#endif
369
0
        case MBEDTLS_MD_SHA512:
370
0
            mbedtls_sha512_clone(dst->md_ctx, src->md_ctx);
371
0
            break;
372
0
#endif
373
0
        default:
374
0
            return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
375
0
    }
376
377
0
    return 0;
378
0
}
379
380
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
381
int mbedtls_md_init_ctx(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info)
382
0
{
383
0
    return mbedtls_md_setup(ctx, md_info, 1);
384
0
}
385
#endif
386
387
#define ALLOC(type)                                                   \
388
0
    do {                                                                \
389
0
        ctx->md_ctx = mbedtls_calloc(1, sizeof(mbedtls_##type##_context)); \
390
0
        if (ctx->md_ctx == NULL)                                       \
391
0
        return MBEDTLS_ERR_MD_ALLOC_FAILED;                      \
392
0
        mbedtls_##type##_init(ctx->md_ctx);                           \
393
0
    }                                                                   \
394
0
    while (0)
395
396
int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac)
397
0
{
398
0
    if (md_info == NULL || ctx == NULL) {
399
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
400
0
    }
401
402
0
    ctx->md_info = md_info;
403
0
    ctx->md_ctx = NULL;
404
0
    ctx->hmac_ctx = NULL;
405
406
0
    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
0
#if defined(MBEDTLS_MD5_C)
418
0
        case MBEDTLS_MD_MD5:
419
0
            ALLOC(md5);
420
0
            break;
421
0
#endif
422
0
#if defined(MBEDTLS_RIPEMD160_C)
423
0
        case MBEDTLS_MD_RIPEMD160:
424
0
            ALLOC(ripemd160);
425
0
            break;
426
0
#endif
427
0
#if defined(MBEDTLS_SHA1_C)
428
0
        case MBEDTLS_MD_SHA1:
429
0
            ALLOC(sha1);
430
0
            break;
431
0
#endif
432
0
#if defined(MBEDTLS_SHA256_C)
433
0
        case MBEDTLS_MD_SHA224:
434
0
        case MBEDTLS_MD_SHA256:
435
0
            ALLOC(sha256);
436
0
            break;
437
0
#endif
438
0
#if defined(MBEDTLS_SHA512_C)
439
0
#if !defined(MBEDTLS_SHA512_NO_SHA384)
440
0
        case MBEDTLS_MD_SHA384:
441
0
#endif
442
0
        case MBEDTLS_MD_SHA512:
443
0
            ALLOC(sha512);
444
0
            break;
445
0
#endif
446
0
        default:
447
0
            return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
448
0
    }
449
450
0
    if (hmac != 0) {
451
0
        ctx->hmac_ctx = mbedtls_calloc(2, md_info->block_size);
452
0
        if (ctx->hmac_ctx == NULL) {
453
0
            mbedtls_md_free(ctx);
454
0
            return MBEDTLS_ERR_MD_ALLOC_FAILED;
455
0
        }
456
0
    }
457
458
0
    return 0;
459
0
}
460
#undef ALLOC
461
462
int mbedtls_md_starts(mbedtls_md_context_t *ctx)
463
0
{
464
0
    if (ctx == NULL || ctx->md_info == NULL) {
465
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
466
0
    }
467
468
0
    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
0
#if defined(MBEDTLS_MD5_C)
478
0
        case MBEDTLS_MD_MD5:
479
0
            return mbedtls_md5_starts_ret(ctx->md_ctx);
480
0
#endif
481
0
#if defined(MBEDTLS_RIPEMD160_C)
482
0
        case MBEDTLS_MD_RIPEMD160:
483
0
            return mbedtls_ripemd160_starts_ret(ctx->md_ctx);
484
0
#endif
485
0
#if defined(MBEDTLS_SHA1_C)
486
0
        case MBEDTLS_MD_SHA1:
487
0
            return mbedtls_sha1_starts_ret(ctx->md_ctx);
488
0
#endif
489
0
#if defined(MBEDTLS_SHA256_C)
490
0
        case MBEDTLS_MD_SHA224:
491
0
            return mbedtls_sha256_starts_ret(ctx->md_ctx, 1);
492
0
        case MBEDTLS_MD_SHA256:
493
0
            return mbedtls_sha256_starts_ret(ctx->md_ctx, 0);
494
0
#endif
495
0
#if defined(MBEDTLS_SHA512_C)
496
0
#if !defined(MBEDTLS_SHA512_NO_SHA384)
497
0
        case MBEDTLS_MD_SHA384:
498
0
            return mbedtls_sha512_starts_ret(ctx->md_ctx, 1);
499
0
#endif
500
0
        case MBEDTLS_MD_SHA512:
501
0
            return mbedtls_sha512_starts_ret(ctx->md_ctx, 0);
502
0
#endif
503
0
        default:
504
0
            return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
505
0
    }
506
0
}
507
508
int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
509
0
{
510
0
    if (ctx == NULL || ctx->md_info == NULL) {
511
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
512
0
    }
513
514
0
    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
0
#if defined(MBEDTLS_MD5_C)
524
0
        case MBEDTLS_MD_MD5:
525
0
            return mbedtls_md5_update_ret(ctx->md_ctx, input, ilen);
526
0
#endif
527
0
#if defined(MBEDTLS_RIPEMD160_C)
528
0
        case MBEDTLS_MD_RIPEMD160:
529
0
            return mbedtls_ripemd160_update_ret(ctx->md_ctx, input, ilen);
530
0
#endif
531
0
#if defined(MBEDTLS_SHA1_C)
532
0
        case MBEDTLS_MD_SHA1:
533
0
            return mbedtls_sha1_update_ret(ctx->md_ctx, input, ilen);
534
0
#endif
535
0
#if defined(MBEDTLS_SHA256_C)
536
0
        case MBEDTLS_MD_SHA224:
537
0
        case MBEDTLS_MD_SHA256:
538
0
            return mbedtls_sha256_update_ret(ctx->md_ctx, input, ilen);
539
0
#endif
540
0
#if defined(MBEDTLS_SHA512_C)
541
0
#if !defined(MBEDTLS_SHA512_NO_SHA384)
542
0
        case MBEDTLS_MD_SHA384:
543
0
#endif
544
0
        case MBEDTLS_MD_SHA512:
545
0
            return mbedtls_sha512_update_ret(ctx->md_ctx, input, ilen);
546
0
#endif
547
0
        default:
548
0
            return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
549
0
    }
550
0
}
551
552
int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output)
553
0
{
554
0
    if (ctx == NULL || ctx->md_info == NULL) {
555
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
556
0
    }
557
558
0
    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
0
#if defined(MBEDTLS_MD5_C)
568
0
        case MBEDTLS_MD_MD5:
569
0
            return mbedtls_md5_finish_ret(ctx->md_ctx, output);
570
0
#endif
571
0
#if defined(MBEDTLS_RIPEMD160_C)
572
0
        case MBEDTLS_MD_RIPEMD160:
573
0
            return mbedtls_ripemd160_finish_ret(ctx->md_ctx, output);
574
0
#endif
575
0
#if defined(MBEDTLS_SHA1_C)
576
0
        case MBEDTLS_MD_SHA1:
577
0
            return mbedtls_sha1_finish_ret(ctx->md_ctx, output);
578
0
#endif
579
0
#if defined(MBEDTLS_SHA256_C)
580
0
        case MBEDTLS_MD_SHA224:
581
0
        case MBEDTLS_MD_SHA256:
582
0
            return mbedtls_sha256_finish_ret(ctx->md_ctx, output);
583
0
#endif
584
0
#if defined(MBEDTLS_SHA512_C)
585
0
#if !defined(MBEDTLS_SHA512_NO_SHA384)
586
0
        case MBEDTLS_MD_SHA384:
587
0
#endif
588
0
        case MBEDTLS_MD_SHA512:
589
0
            return mbedtls_sha512_finish_ret(ctx->md_ctx, output);
590
0
#endif
591
0
        default:
592
0
            return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
593
0
    }
594
0
}
595
596
int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
597
               unsigned char *output)
598
0
{
599
0
    if (md_info == NULL) {
600
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
601
0
    }
602
603
0
    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
0
#if defined(MBEDTLS_MD5_C)
613
0
        case MBEDTLS_MD_MD5:
614
0
            return mbedtls_md5_ret(input, ilen, output);
615
0
#endif
616
0
#if defined(MBEDTLS_RIPEMD160_C)
617
0
        case MBEDTLS_MD_RIPEMD160:
618
0
            return mbedtls_ripemd160_ret(input, ilen, output);
619
0
#endif
620
0
#if defined(MBEDTLS_SHA1_C)
621
0
        case MBEDTLS_MD_SHA1:
622
0
            return mbedtls_sha1_ret(input, ilen, output);
623
0
#endif
624
0
#if defined(MBEDTLS_SHA256_C)
625
0
        case MBEDTLS_MD_SHA224:
626
0
            return mbedtls_sha256_ret(input, ilen, output, 1);
627
0
        case MBEDTLS_MD_SHA256:
628
0
            return mbedtls_sha256_ret(input, ilen, output, 0);
629
0
#endif
630
0
#if defined(MBEDTLS_SHA512_C)
631
0
#if !defined(MBEDTLS_SHA512_NO_SHA384)
632
0
        case MBEDTLS_MD_SHA384:
633
0
            return mbedtls_sha512_ret(input, ilen, output, 1);
634
0
#endif
635
0
        case MBEDTLS_MD_SHA512:
636
0
            return mbedtls_sha512_ret(input, ilen, output, 0);
637
0
#endif
638
0
        default:
639
0
            return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
640
0
    }
641
0
}
642
643
#if defined(MBEDTLS_FS_IO)
644
int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, unsigned char *output)
645
0
{
646
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
647
0
    FILE *f;
648
0
    size_t n;
649
0
    mbedtls_md_context_t ctx;
650
0
    unsigned char buf[1024];
651
652
0
    if (md_info == NULL) {
653
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
654
0
    }
655
656
0
    if ((f = fopen(path, "rb")) == NULL) {
657
0
        return MBEDTLS_ERR_MD_FILE_IO_ERROR;
658
0
    }
659
660
0
    mbedtls_md_init(&ctx);
661
662
0
    if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
663
0
        goto cleanup;
664
0
    }
665
666
0
    if ((ret = mbedtls_md_starts(&ctx)) != 0) {
667
0
        goto cleanup;
668
0
    }
669
670
0
    while ((n = fread(buf, 1, sizeof(buf), f)) > 0) {
671
0
        if ((ret = mbedtls_md_update(&ctx, buf, n)) != 0) {
672
0
            goto cleanup;
673
0
        }
674
0
    }
675
676
0
    if (ferror(f) != 0) {
677
0
        ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
678
0
    } else {
679
0
        ret = mbedtls_md_finish(&ctx, output);
680
0
    }
681
682
0
cleanup:
683
0
    mbedtls_platform_zeroize(buf, sizeof(buf));
684
0
    fclose(f);
685
0
    mbedtls_md_free(&ctx);
686
687
0
    return ret;
688
0
}
689
#endif /* MBEDTLS_FS_IO */
690
691
int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen)
692
0
{
693
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
694
0
    unsigned char sum[MBEDTLS_MD_MAX_SIZE];
695
0
    unsigned char *ipad, *opad;
696
0
    size_t i;
697
698
0
    if (ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL) {
699
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
700
0
    }
701
702
0
    if (keylen > (size_t) ctx->md_info->block_size) {
703
0
        if ((ret = mbedtls_md_starts(ctx)) != 0) {
704
0
            goto cleanup;
705
0
        }
706
0
        if ((ret = mbedtls_md_update(ctx, key, keylen)) != 0) {
707
0
            goto cleanup;
708
0
        }
709
0
        if ((ret = mbedtls_md_finish(ctx, sum)) != 0) {
710
0
            goto cleanup;
711
0
        }
712
713
0
        keylen = ctx->md_info->size;
714
0
        key = sum;
715
0
    }
716
717
0
    ipad = (unsigned char *) ctx->hmac_ctx;
718
0
    opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
719
720
0
    memset(ipad, 0x36, ctx->md_info->block_size);
721
0
    memset(opad, 0x5C, ctx->md_info->block_size);
722
723
0
    for (i = 0; i < keylen; i++) {
724
0
        ipad[i] = (unsigned char) (ipad[i] ^ key[i]);
725
0
        opad[i] = (unsigned char) (opad[i] ^ key[i]);
726
0
    }
727
728
0
    if ((ret = mbedtls_md_starts(ctx)) != 0) {
729
0
        goto cleanup;
730
0
    }
731
0
    if ((ret = mbedtls_md_update(ctx, ipad,
732
0
                                 ctx->md_info->block_size)) != 0) {
733
0
        goto cleanup;
734
0
    }
735
736
0
cleanup:
737
0
    mbedtls_platform_zeroize(sum, sizeof(sum));
738
739
0
    return ret;
740
0
}
741
742
int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
743
0
{
744
0
    if (ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL) {
745
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
746
0
    }
747
748
0
    return mbedtls_md_update(ctx, input, ilen);
749
0
}
750
751
int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output)
752
0
{
753
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
754
0
    unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
755
0
    unsigned char *opad;
756
757
0
    if (ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL) {
758
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
759
0
    }
760
761
0
    opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
762
763
0
    if ((ret = mbedtls_md_finish(ctx, tmp)) != 0) {
764
0
        return ret;
765
0
    }
766
0
    if ((ret = mbedtls_md_starts(ctx)) != 0) {
767
0
        return ret;
768
0
    }
769
0
    if ((ret = mbedtls_md_update(ctx, opad,
770
0
                                 ctx->md_info->block_size)) != 0) {
771
0
        return ret;
772
0
    }
773
0
    if ((ret = mbedtls_md_update(ctx, tmp,
774
0
                                 ctx->md_info->size)) != 0) {
775
0
        return ret;
776
0
    }
777
0
    return mbedtls_md_finish(ctx, output);
778
0
}
779
780
int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx)
781
0
{
782
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
783
0
    unsigned char *ipad;
784
785
0
    if (ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL) {
786
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
787
0
    }
788
789
0
    ipad = (unsigned char *) ctx->hmac_ctx;
790
791
0
    if ((ret = mbedtls_md_starts(ctx)) != 0) {
792
0
        return ret;
793
0
    }
794
0
    return mbedtls_md_update(ctx, ipad, ctx->md_info->block_size);
795
0
}
796
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
0
{
802
0
    mbedtls_md_context_t ctx;
803
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
804
805
0
    if (md_info == NULL) {
806
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
807
0
    }
808
809
0
    mbedtls_md_init(&ctx);
810
811
0
    if ((ret = mbedtls_md_setup(&ctx, md_info, 1)) != 0) {
812
0
        goto cleanup;
813
0
    }
814
815
0
    if ((ret = mbedtls_md_hmac_starts(&ctx, key, keylen)) != 0) {
816
0
        goto cleanup;
817
0
    }
818
0
    if ((ret = mbedtls_md_hmac_update(&ctx, input, ilen)) != 0) {
819
0
        goto cleanup;
820
0
    }
821
0
    if ((ret = mbedtls_md_hmac_finish(&ctx, output)) != 0) {
822
0
        goto cleanup;
823
0
    }
824
825
0
cleanup:
826
0
    mbedtls_md_free(&ctx);
827
828
0
    return ret;
829
0
}
830
831
int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data)
832
0
{
833
0
    if (ctx == NULL || ctx->md_info == NULL) {
834
0
        return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
835
0
    }
836
837
0
    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
0
#if defined(MBEDTLS_MD5_C)
847
0
        case MBEDTLS_MD_MD5:
848
0
            return mbedtls_internal_md5_process(ctx->md_ctx, data);
849
0
#endif
850
0
#if defined(MBEDTLS_RIPEMD160_C)
851
0
        case MBEDTLS_MD_RIPEMD160:
852
0
            return mbedtls_internal_ripemd160_process(ctx->md_ctx, data);
853
0
#endif
854
0
#if defined(MBEDTLS_SHA1_C)
855
0
        case MBEDTLS_MD_SHA1:
856
0
            return mbedtls_internal_sha1_process(ctx->md_ctx, data);
857
0
#endif
858
0
#if defined(MBEDTLS_SHA256_C)
859
0
        case MBEDTLS_MD_SHA224:
860
0
        case MBEDTLS_MD_SHA256:
861
0
            return mbedtls_internal_sha256_process(ctx->md_ctx, data);
862
0
#endif
863
0
#if defined(MBEDTLS_SHA512_C)
864
0
#if !defined(MBEDTLS_SHA512_NO_SHA384)
865
0
        case MBEDTLS_MD_SHA384:
866
0
#endif
867
0
        case MBEDTLS_MD_SHA512:
868
0
            return mbedtls_internal_sha512_process(ctx->md_ctx, data);
869
0
#endif
870
0
        default:
871
0
            return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
872
0
    }
873
0
}
874
875
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
876
0
{
877
0
    if (md_info == NULL) {
878
0
        return 0;
879
0
    }
880
881
0
    return md_info->size;
882
0
}
883
884
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
885
0
{
886
0
    if (md_info == NULL) {
887
0
        return MBEDTLS_MD_NONE;
888
0
    }
889
890
0
    return md_info->type;
891
0
}
892
893
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
894
0
{
895
0
    if (md_info == NULL) {
896
0
        return NULL;
897
0
    }
898
899
0
    return md_info->name;
900
0
}
901
902
#endif /* MBEDTLS_MD_C */