u-boot/fs/btrfs/crypto/hash.c
Qu Wenruo 565a4147d1 fs: btrfs: Add more checksum algorithms
This mostly crossports crypto/hash.[ch] from btrfs-progs.

The differences are:
- No blake2 support
  No blake2 related library in U-Boot yet.

- Use uboot xxhash/sha256 directly
  No need to implement the code as U-Boot has already provided the
  interface.

This adds the support for the following csums:
- SHA256
- XXHASH

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Marek Behún <marek.behun@nic.cz>
2020-09-07 20:57:27 -04:00

55 lines
976 B
C

// SPDX-License-Identifier: GPL-2.0+
#include <linux/xxhash.h>
#include <linux/unaligned/access_ok.h>
#include <linux/types.h>
#include <u-boot/sha256.h>
#include <u-boot/crc.h>
static u32 btrfs_crc32c_table[256];
void btrfs_hash_init(void)
{
static int inited = 0;
if (!inited) {
crc32c_init(btrfs_crc32c_table, 0x82F63B78);
inited = 1;
}
}
int hash_sha256(const u8 *buf, size_t length, u8 *out)
{
sha256_context ctx;
sha256_starts(&ctx);
sha256_update(&ctx, buf, length);
sha256_finish(&ctx, out);
return 0;
}
int hash_xxhash(const u8 *buf, size_t length, u8 *out)
{
u64 hash;
hash = xxh64(buf, length, 0);
put_unaligned_le64(hash, out);
return 0;
}
int hash_crc32c(const u8 *buf, size_t length, u8 *out)
{
u32 crc;
crc = crc32c_cal((u32)~0, (char *)buf, length, btrfs_crc32c_table);
put_unaligned_le32(~crc, out);
return 0;
}
u32 crc32c(u32 seed, const void * data, size_t len)
{
return crc32c_cal(seed, data, len, btrfs_crc32c_table);
}