blob: 2bf28e2dafb45b0646de5855ce5f0ec2f94404ce [file] [log] [blame]
Heiko Schocheredaf9b12014-03-03 12:19:26 +01001/*
2 * Copyright (c) 2013, Andreas Oetken.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
Heiko Schocherd7b42322014-03-03 12:19:30 +01007#ifndef USE_HOSTCC
Heiko Schocheredaf9b12014-03-03 12:19:26 +01008#include <common.h>
9#include <fdtdec.h>
Heiko Schocheredaf9b12014-03-03 12:19:26 +010010#include <asm/byteorder.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Heiko Schocheredaf9b12014-03-03 12:19:26 +010012#include <asm/unaligned.h>
Ruchika Gupta2c8987f2015-01-23 16:01:59 +053013#include <hash.h>
Heiko Schocherd7b42322014-03-03 12:19:30 +010014#else
15#include "fdt_host.h"
Ruchika Gupta2c8987f2015-01-23 16:01:59 +053016#endif
17#include <u-boot/rsa.h>
Heiko Schocheredaf9b12014-03-03 12:19:26 +010018
Ruchika Gupta2c8987f2015-01-23 16:01:59 +053019int hash_calculate(const char *name,
20 const struct image_region region[],
21 int region_count, uint8_t *checksum)
Heiko Schocheredaf9b12014-03-03 12:19:26 +010022{
Ruchika Gupta2c8987f2015-01-23 16:01:59 +053023 struct hash_algo *algo;
24 int ret = 0;
25 void *ctx;
Heiko Schocheredaf9b12014-03-03 12:19:26 +010026 uint32_t i;
27 i = 0;
28
Ruchika Gupta2c8987f2015-01-23 16:01:59 +053029 ret = hash_progressive_lookup_algo(name, &algo);
30 if (ret)
31 return ret;
Heiko Schocheredaf9b12014-03-03 12:19:26 +010032
Ruchika Gupta2c8987f2015-01-23 16:01:59 +053033 ret = algo->hash_init(algo, &ctx);
34 if (ret)
35 return ret;
36
37 for (i = 0; i < region_count - 1; i++) {
38 ret = algo->hash_update(algo, ctx, region[i].data,
39 region[i].size, 0);
40 if (ret)
41 return ret;
42 }
43
44 ret = algo->hash_update(algo, ctx, region[i].data, region[i].size, 1);
45 if (ret)
46 return ret;
47 ret = algo->hash_finish(algo, ctx, checksum, algo->digest_size);
48 if (ret)
49 return ret;
Heiko Schocheredaf9b12014-03-03 12:19:26 +010050
Ruchika Gupta2c8987f2015-01-23 16:01:59 +053051 return 0;
Heiko Schocheredaf9b12014-03-03 12:19:26 +010052}