blob: e60debb7df3c4cb4b6c0ef50896b8163fc6cfc1b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher646257d2014-03-03 12:19:26 +01002/*
3 * Copyright (c) 2013, Andreas Oetken.
Heiko Schocher646257d2014-03-03 12:19:26 +01004 */
5
Heiko Schocher29a23f92014-03-03 12:19:30 +01006#ifndef USE_HOSTCC
Heiko Schocher646257d2014-03-03 12:19:26 +01007#include <common.h>
8#include <fdtdec.h>
Heiko Schocher646257d2014-03-03 12:19:26 +01009#include <asm/byteorder.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090010#include <linux/errno.h>
Heiko Schocher646257d2014-03-03 12:19:26 +010011#include <asm/unaligned.h>
Ruchika Guptab37b46f2015-01-23 16:01:59 +053012#include <hash.h>
Heiko Schocher29a23f92014-03-03 12:19:30 +010013#else
14#include "fdt_host.h"
Ruchika Guptab37b46f2015-01-23 16:01:59 +053015#endif
16#include <u-boot/rsa.h>
Heiko Schocher646257d2014-03-03 12:19:26 +010017
Ruchika Guptab37b46f2015-01-23 16:01:59 +053018int hash_calculate(const char *name,
19 const struct image_region region[],
20 int region_count, uint8_t *checksum)
Heiko Schocher646257d2014-03-03 12:19:26 +010021{
Ruchika Guptab37b46f2015-01-23 16:01:59 +053022 struct hash_algo *algo;
23 int ret = 0;
24 void *ctx;
Heiko Schocher646257d2014-03-03 12:19:26 +010025 uint32_t i;
26 i = 0;
27
Ruchika Guptab37b46f2015-01-23 16:01:59 +053028 ret = hash_progressive_lookup_algo(name, &algo);
29 if (ret)
30 return ret;
Heiko Schocher646257d2014-03-03 12:19:26 +010031
Ruchika Guptab37b46f2015-01-23 16:01:59 +053032 ret = algo->hash_init(algo, &ctx);
33 if (ret)
34 return ret;
Heiko Schocher646257d2014-03-03 12:19:26 +010035
Ruchika Guptab37b46f2015-01-23 16:01:59 +053036 for (i = 0; i < region_count - 1; i++) {
37 ret = algo->hash_update(algo, ctx, region[i].data,
38 region[i].size, 0);
39 if (ret)
40 return ret;
41 }
42
43 ret = algo->hash_update(algo, ctx, region[i].data, region[i].size, 1);
44 if (ret)
45 return ret;
46 ret = algo->hash_finish(algo, ctx, checksum, algo->digest_size);
47 if (ret)
48 return ret;
49
50 return 0;
Heiko Schocher646257d2014-03-03 12:19:26 +010051}