blob: aae520b2aee535f069946ba332b83a3ab0d44644 [file] [log] [blame]
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001/*
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
18static struct crypto_shash *tfm;
19
20int __init btrfs_hash_init(void)
21{
22 tfm = crypto_alloc_shash("crc32c", 0, 0);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +000023
Fabian Frederick6f84e232014-07-04 21:10:27 +020024 return PTR_ERR_OR_ZERO(tfm);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +000025}
26
27void btrfs_hash_exit(void)
28{
29 crypto_free_shash(tfm);
30}
31
32u32 btrfs_crc32c(u32 crc, const void *address, unsigned int length)
33{
Vinícius Tinti0458a952014-04-04 18:21:24 -030034 SHASH_DESC_ON_STACK(shash, tfm);
35 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +000036 int err;
37
Vinícius Tinti0458a952014-04-04 18:21:24 -030038 shash->tfm = tfm;
39 shash->flags = 0;
40 *ctx = crc;
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +000041
Vinícius Tinti0458a952014-04-04 18:21:24 -030042 err = crypto_shash_update(shash, address, length);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +000043 BUG_ON(err);
44
Vinícius Tinti0458a952014-04-04 18:21:24 -030045 return *ctx;
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +000046}