blob: 04388fce249860b788f2234005f09c126c18ac32 [file] [log] [blame]
Raymond Maof51f3552024-10-03 14:50:19 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Hash shim layer on MbedTLS Crypto library
4 *
5 * Copyright (c) 2024 Linaro Limited
6 * Author: Raymond Mao <raymond.mao@linaro.org>
7 */
8#include "compiler.h"
9
10#ifndef USE_HOSTCC
11#include <watchdog.h>
12#endif /* USE_HOSTCC */
13#include <u-boot/md5.h>
14
15void MD5Init(MD5Context *ctx)
16{
17 mbedtls_md5_init(ctx);
18 mbedtls_md5_starts(ctx);
19}
20
21void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len)
22{
23 mbedtls_md5_update(ctx, buf, len);
24}
25
26void MD5Final(unsigned char digest[16], MD5Context *ctx)
27{
28 mbedtls_md5_finish(ctx, digest);
29 mbedtls_md5_free(ctx);
30}
31
32void md5_wd(const unsigned char *input, unsigned int len,
33 unsigned char output[16], unsigned int chunk_sz)
34{
35 MD5Context context;
36
37 MD5Init(&context);
38
39 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
40 const unsigned char *curr = input;
41 const unsigned char *end = input + len;
42 int chunk;
43
44 while (curr < end) {
45 chunk = end - curr;
46 if (chunk > chunk_sz)
47 chunk = chunk_sz;
48 MD5Update(&context, curr, chunk);
49 curr += chunk;
50 schedule();
51 }
52 } else {
53 MD5Update(&context, input, len);
54 }
55
56 MD5Final(output, &context);
57}