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