hash-checksum.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Andreas Oetken.
  4. */
  5. #ifndef USE_HOSTCC
  6. #include <common.h>
  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. int hash_calculate(const char *name,
  18. const struct image_region *region,
  19. int region_count, uint8_t *checksum)
  20. {
  21. struct hash_algo *algo;
  22. int ret = 0;
  23. void *ctx;
  24. int i;
  25. if (region_count < 1)
  26. return -EINVAL;
  27. ret = hash_progressive_lookup_algo(name, &algo);
  28. if (ret)
  29. return ret;
  30. ret = algo->hash_init(algo, &ctx);
  31. if (ret)
  32. return ret;
  33. for (i = 0; i < region_count - 1; i++) {
  34. ret = algo->hash_update(algo, ctx, region[i].data,
  35. region[i].size, 0);
  36. if (ret)
  37. return ret;
  38. }
  39. ret = algo->hash_update(algo, ctx, region[i].data, region[i].size, 1);
  40. if (ret)
  41. return ret;
  42. ret = algo->hash_finish(algo, ctx, checksum, algo->digest_size);
  43. if (ret)
  44. return ret;
  45. return 0;
  46. }