hash.c 976 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/xxhash.h>
  3. #include <linux/unaligned/access_ok.h>
  4. #include <linux/types.h>
  5. #include <u-boot/sha256.h>
  6. #include <u-boot/crc.h>
  7. static u32 btrfs_crc32c_table[256];
  8. void btrfs_hash_init(void)
  9. {
  10. static int inited = 0;
  11. if (!inited) {
  12. crc32c_init(btrfs_crc32c_table, 0x82F63B78);
  13. inited = 1;
  14. }
  15. }
  16. int hash_sha256(const u8 *buf, size_t length, u8 *out)
  17. {
  18. sha256_context ctx;
  19. sha256_starts(&ctx);
  20. sha256_update(&ctx, buf, length);
  21. sha256_finish(&ctx, out);
  22. return 0;
  23. }
  24. int hash_xxhash(const u8 *buf, size_t length, u8 *out)
  25. {
  26. u64 hash;
  27. hash = xxh64(buf, length, 0);
  28. put_unaligned_le64(hash, out);
  29. return 0;
  30. }
  31. int hash_crc32c(const u8 *buf, size_t length, u8 *out)
  32. {
  33. u32 crc;
  34. crc = crc32c_cal((u32)~0, (char *)buf, length, btrfs_crc32c_table);
  35. put_unaligned_le32(~crc, out);
  36. return 0;
  37. }
  38. u32 crc32c(u32 seed, const void * data, size_t len)
  39. {
  40. return crc32c_cal(seed, data, len, btrfs_crc32c_table);
  41. }