hash.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. if (IS_ERR(tfm))
  21. return PTR_ERR(tfm);
  22. return 0;
  23. }
  24. void btrfs_hash_exit(void)
  25. {
  26. crypto_free_shash(tfm);
  27. }
  28. u32 btrfs_crc32c(u32 crc, const void *address, unsigned int length)
  29. {
  30. struct {
  31. struct shash_desc shash;
  32. char ctx[crypto_shash_descsize(tfm)];
  33. } desc;
  34. int err;
  35. desc.shash.tfm = tfm;
  36. desc.shash.flags = 0;
  37. *(u32 *)desc.ctx = crc;
  38. err = crypto_shash_update(&desc.shash, address, length);
  39. BUG_ON(err);
  40. return *(u32 *)desc.ctx;
  41. }