1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013, Andreas Oetken.
4  */
5 
6 #ifndef USE_HOSTCC
7 #include <fdtdec.h>
8 #include <asm/byteorder.h>
9 #include <linux/errno.h>
10 #include <asm/unaligned.h>
11 #include <hash.h>
12 #else
13 #include "fdt_host.h"
14 #endif
15 #include <hash.h>
16 #include <image.h>
17 
hash_calculate(const char * name,const struct image_region * region,int region_count,uint8_t * checksum)18 int hash_calculate(const char *name,
19 		    const struct image_region *region,
20 		    int region_count, uint8_t *checksum)
21 {
22 	struct hash_algo *algo;
23 	int ret = 0;
24 	void *ctx;
25 	int i;
26 
27 	if (region_count < 1)
28 		return -EINVAL;
29 
30 	ret = hash_progressive_lookup_algo(name, &algo);
31 	if (ret)
32 		return ret;
33 
34 	ret = algo->hash_init(algo, &ctx);
35 	if (ret)
36 		return ret;
37 
38 	for (i = 0; i < region_count - 1; i++) {
39 		ret = algo->hash_update(algo, ctx, region[i].data,
40 					region[i].size, 0);
41 		if (ret)
42 			return ret;
43 	}
44 
45 	ret = algo->hash_update(algo, ctx, region[i].data, region[i].size, 1);
46 	if (ret)
47 		return ret;
48 	ret = algo->hash_finish(algo, ctx, checksum, algo->digest_size);
49 	if (ret)
50 		return ret;
51 
52 	return 0;
53 }
54