summaryrefslogtreecommitdiff
path: root/lib/mbedtls/md5.c
blob: 04388fce249860b788f2234005f09c126c18ac32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: GPL-2.0+
/*
 * Hash shim layer on MbedTLS Crypto library
 *
 * Copyright (c) 2024 Linaro Limited
 * Author: Raymond Mao <raymond.mao@linaro.org>
 */
#include "compiler.h"

#ifndef USE_HOSTCC
#include <watchdog.h>
#endif /* USE_HOSTCC */
#include <u-boot/md5.h>

void MD5Init(MD5Context *ctx)
{
	mbedtls_md5_init(ctx);
	mbedtls_md5_starts(ctx);
}

void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len)
{
	mbedtls_md5_update(ctx, buf, len);
}

void MD5Final(unsigned char digest[16], MD5Context *ctx)
{
	mbedtls_md5_finish(ctx, digest);
	mbedtls_md5_free(ctx);
}

void md5_wd(const unsigned char *input, unsigned int len,
	    unsigned char output[16], unsigned int chunk_sz)
{
	MD5Context context;

	MD5Init(&context);

	if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
		const unsigned char *curr = input;
		const unsigned char *end = input + len;
		int chunk;

		while (curr < end) {
			chunk = end - curr;
			if (chunk > chunk_sz)
				chunk = chunk_sz;
			MD5Update(&context, curr, chunk);
			curr += chunk;
			schedule();
		}
	} else {
		MD5Update(&context, input, len);
	}

	MD5Final(output, &context);
}