Commit | Line | Data |
---|---|---|
14a958e6 FDBM |
1 | /* |
2 | * Copyright (C) 2014 Filipe David Borba Manana <fdmanana@gmail.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | */ | |
13 | ||
14 | #include <crypto/hash.h> | |
15 | #include <linux/err.h> | |
16 | #include "hash.h" | |
17 | ||
18 | static struct crypto_shash *tfm; | |
19 | ||
20 | int __init btrfs_hash_init(void) | |
21 | { | |
22 | tfm = crypto_alloc_shash("crc32c", 0, 0); | |
14a958e6 | 23 | |
6f84e236 | 24 | return PTR_ERR_OR_ZERO(tfm); |
14a958e6 FDBM |
25 | } |
26 | ||
5f9e1059 JM |
27 | const char* btrfs_crc32c_impl(void) |
28 | { | |
29 | return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm)); | |
30 | } | |
31 | ||
14a958e6 FDBM |
32 | void btrfs_hash_exit(void) |
33 | { | |
34 | crypto_free_shash(tfm); | |
35 | } | |
36 | ||
37 | u32 btrfs_crc32c(u32 crc, const void *address, unsigned int length) | |
38 | { | |
0458a953 VT |
39 | SHASH_DESC_ON_STACK(shash, tfm); |
40 | u32 *ctx = (u32 *)shash_desc_ctx(shash); | |
d41519a6 | 41 | u32 retval; |
14a958e6 FDBM |
42 | int err; |
43 | ||
0458a953 VT |
44 | shash->tfm = tfm; |
45 | shash->flags = 0; | |
46 | *ctx = crc; | |
14a958e6 | 47 | |
0458a953 | 48 | err = crypto_shash_update(shash, address, length); |
14a958e6 FDBM |
49 | BUG_ON(err); |
50 | ||
d41519a6 DM |
51 | retval = *ctx; |
52 | barrier_data(ctx); | |
53 | return retval; | |
14a958e6 | 54 | } |