hash.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #include <crypto/hash.h>
  14. #include <linux/err.h>
  15. #include "hash.h"
  16. static struct crypto_shash *tfm;
  17. int __init btrfs_hash_init(void)
  18. {
  19. tfm = crypto_alloc_shash("crc32c", 0, 0);
  20. return PTR_ERR_OR_ZERO(tfm);
  21. }
  22. const char* btrfs_crc32c_impl(void)
  23. {
  24. return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));
  25. }
  26. void btrfs_hash_exit(void)
  27. {
  28. crypto_free_shash(tfm);
  29. }
  30. u32 btrfs_crc32c(u32 crc, const void *address, unsigned int length)
  31. {
  32. SHASH_DESC_ON_STACK(shash, tfm);
  33. u32 *ctx = (u32 *)shash_desc_ctx(shash);
  34. u32 retval;
  35. int err;
  36. shash->tfm = tfm;
  37. shash->flags = 0;
  38. *ctx = crc;
  39. err = crypto_shash_update(shash, address, length);
  40. BUG_ON(err);
  41. retval = *ctx;
  42. barrier_data(ctx);
  43. return retval;
  44. }