crc32_generic.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2012 Xyratex Technology Limited
  4. */
  5. /*
  6. * This is crypto api shash wrappers to crc32_le.
  7. */
  8. #include <asm/unaligned.h>
  9. #include <linux/crc32.h>
  10. #include <crypto/internal/hash.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <linux/kernel.h>
  15. #define CHKSUM_BLOCK_SIZE 1
  16. #define CHKSUM_DIGEST_SIZE 4
  17. /** No default init with ~0 */
  18. static int crc32_cra_init(struct crypto_tfm *tfm)
  19. {
  20. u32 *key = crypto_tfm_ctx(tfm);
  21. *key = 0;
  22. return 0;
  23. }
  24. /*
  25. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  26. * If your algorithm starts with ~0, then XOR with ~0 before you set
  27. * the seed.
  28. */
  29. static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
  30. unsigned int keylen)
  31. {
  32. u32 *mctx = crypto_shash_ctx(hash);
  33. if (keylen != sizeof(u32))
  34. return -EINVAL;
  35. *mctx = get_unaligned_le32(key);
  36. return 0;
  37. }
  38. static int crc32_init(struct shash_desc *desc)
  39. {
  40. u32 *mctx = crypto_shash_ctx(desc->tfm);
  41. u32 *crcp = shash_desc_ctx(desc);
  42. *crcp = *mctx;
  43. return 0;
  44. }
  45. static int crc32_update(struct shash_desc *desc, const u8 *data,
  46. unsigned int len)
  47. {
  48. u32 *crcp = shash_desc_ctx(desc);
  49. *crcp = crc32_le(*crcp, data, len);
  50. return 0;
  51. }
  52. /* No final XOR 0xFFFFFFFF, like crc32_le */
  53. static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
  54. u8 *out)
  55. {
  56. put_unaligned_le32(crc32_le(*crcp, data, len), out);
  57. return 0;
  58. }
  59. static int crc32_finup(struct shash_desc *desc, const u8 *data,
  60. unsigned int len, u8 *out)
  61. {
  62. return __crc32_finup(shash_desc_ctx(desc), data, len, out);
  63. }
  64. static int crc32_final(struct shash_desc *desc, u8 *out)
  65. {
  66. u32 *crcp = shash_desc_ctx(desc);
  67. put_unaligned_le32(*crcp, out);
  68. return 0;
  69. }
  70. static int crc32_digest(struct shash_desc *desc, const u8 *data,
  71. unsigned int len, u8 *out)
  72. {
  73. return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
  74. out);
  75. }
  76. static struct shash_alg alg = {
  77. .setkey = crc32_setkey,
  78. .init = crc32_init,
  79. .update = crc32_update,
  80. .final = crc32_final,
  81. .finup = crc32_finup,
  82. .digest = crc32_digest,
  83. .descsize = sizeof(u32),
  84. .digestsize = CHKSUM_DIGEST_SIZE,
  85. .base = {
  86. .cra_name = "crc32",
  87. .cra_driver_name = "crc32-generic",
  88. .cra_priority = 100,
  89. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  90. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  91. .cra_ctxsize = sizeof(u32),
  92. .cra_module = THIS_MODULE,
  93. .cra_init = crc32_cra_init,
  94. }
  95. };
  96. static int __init crc32_mod_init(void)
  97. {
  98. return crypto_register_shash(&alg);
  99. }
  100. static void __exit crc32_mod_fini(void)
  101. {
  102. crypto_unregister_shash(&alg);
  103. }
  104. subsys_initcall(crc32_mod_init);
  105. module_exit(crc32_mod_fini);
  106. MODULE_AUTHOR("Alexander Boyko <[email protected]>");
  107. MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
  108. MODULE_LICENSE("GPL");
  109. MODULE_ALIAS_CRYPTO("crc32");
  110. MODULE_ALIAS_CRYPTO("crc32-generic");