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