2014-01-11 19:22:46 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Filipe David Borba Manana <fdmanana@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <crypto/hash.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include "hash.h"
|
|
|
|
|
|
|
|
static struct crypto_shash *tfm;
|
|
|
|
|
|
|
|
int __init btrfs_hash_init(void)
|
|
|
|
{
|
|
|
|
tfm = crypto_alloc_shash("crc32c", 0, 0);
|
|
|
|
|
2014-07-04 13:10:27 -06:00
|
|
|
return PTR_ERR_OR_ZERO(tfm);
|
2014-01-11 19:22:46 -07:00
|
|
|
}
|
|
|
|
|
2015-09-16 07:34:53 -06:00
|
|
|
const char* btrfs_crc32c_impl(void)
|
|
|
|
{
|
|
|
|
return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
2014-01-11 19:22:46 -07:00
|
|
|
void btrfs_hash_exit(void)
|
|
|
|
{
|
|
|
|
crypto_free_shash(tfm);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 btrfs_crc32c(u32 crc, const void *address, unsigned int length)
|
|
|
|
{
|
2014-04-04 15:21:24 -06:00
|
|
|
SHASH_DESC_ON_STACK(shash, tfm);
|
|
|
|
u32 *ctx = (u32 *)shash_desc_ctx(shash);
|
2017-06-02 09:28:54 -06:00
|
|
|
u32 retval;
|
2014-01-11 19:22:46 -07:00
|
|
|
int err;
|
|
|
|
|
2014-04-04 15:21:24 -06:00
|
|
|
shash->tfm = tfm;
|
|
|
|
shash->flags = 0;
|
|
|
|
*ctx = crc;
|
2014-01-11 19:22:46 -07:00
|
|
|
|
2014-04-04 15:21:24 -06:00
|
|
|
err = crypto_shash_update(shash, address, length);
|
2014-01-11 19:22:46 -07:00
|
|
|
BUG_ON(err);
|
|
|
|
|
2017-06-02 09:28:54 -06:00
|
|
|
retval = *ctx;
|
|
|
|
barrier_data(ctx);
|
|
|
|
return retval;
|
2014-01-11 19:22:46 -07:00
|
|
|
}
|