Coverage Report

Created: 2024-08-17 06:45

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