summaryrefslogtreecommitdiff
path: root/lib/sha256_common.c
blob: 7041abd26d963450830c2fbb4348ee0bfdb33bf9 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * FIPS-180-2 compliant SHA-256 implementation
 *
 * Copyright (C) 2001-2003  Christophe Devine
 */

#ifndef USE_HOSTCC
#include <u-boot/schedule.h>
#endif /* USE_HOSTCC */
#include <string.h>
#include <u-boot/sha256.h>

#include <linux/compiler_attributes.h>

/*
 * Output = SHA-256( input buffer ). Trigger the watchdog every 'chunk_sz'
 * bytes of input processed.
 */
void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
		    unsigned char *output, unsigned int chunk_sz)
{
	sha256_context ctx;
#if !defined(USE_HOSTCC) && \
	(defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG))
	const unsigned char *end;
	unsigned char *curr;
	int chunk;
#endif

	sha256_starts(&ctx);

#if !defined(USE_HOSTCC) && \
	(defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG))
	curr = (unsigned char *)input;
	end = input + ilen;
	while (curr < end) {
		chunk = end - curr;
		if (chunk > chunk_sz)
			chunk = chunk_sz;
		sha256_update(&ctx, curr, chunk);
		curr += chunk;
		schedule();
	}
#else
	sha256_update(&ctx, input, ilen);
#endif

	sha256_finish(&ctx, output);
}