crc32c_generic.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * CRC32C chksum
  6. *
  7. *@Article{castagnoli-crc,
  8. * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
  9. * title = {{Optimization of Cyclic Redundancy-Check Codes with 24
  10. * and 32 Parity Bits}},
  11. * journal = IEEE Transactions on Communication,
  12. * year = {1993},
  13. * volume = {41},
  14. * number = {6},
  15. * pages = {},
  16. * month = {June},
  17. *}
  18. * Used by the iSCSI driver, possibly others, and derived from
  19. * the iscsi-crc.c module of the linux-iscsi driver at
  20. * http://linux-iscsi.sourceforge.net.
  21. *
  22. * Following the example of lib/crc32, this function is intended to be
  23. * flexible and useful for all users. Modules that currently have their
  24. * own crc32c, but hopefully may be able to use this one are:
  25. * net/sctp (please add all your doco to here if you change to
  26. * use this one!)
  27. * <endoflist>
  28. *
  29. * Copyright (c) 2004 Cisco Systems, Inc.
  30. * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
  31. */
  32. #include <asm/unaligned.h>
  33. #include <crypto/internal/hash.h>
  34. #include <linux/init.h>
  35. #include <linux/module.h>
  36. #include <linux/string.h>
  37. #include <linux/kernel.h>
  38. #include <linux/crc32.h>
  39. #define CHKSUM_BLOCK_SIZE 1
  40. #define CHKSUM_DIGEST_SIZE 4
  41. struct chksum_ctx {
  42. u32 key;
  43. };
  44. struct chksum_desc_ctx {
  45. u32 crc;
  46. };
  47. /*
  48. * Steps through buffer one byte at a time, calculates reflected
  49. * crc using table.
  50. */
  51. static int chksum_init(struct shash_desc *desc)
  52. {
  53. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  54. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  55. ctx->crc = mctx->key;
  56. return 0;
  57. }
  58. /*
  59. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  60. * If your algorithm starts with ~0, then XOR with ~0 before you set
  61. * the seed.
  62. */
  63. static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
  64. unsigned int keylen)
  65. {
  66. struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
  67. if (keylen != sizeof(mctx->key))
  68. return -EINVAL;
  69. mctx->key = get_unaligned_le32(key);
  70. return 0;
  71. }
  72. static int chksum_update(struct shash_desc *desc, const u8 *data,
  73. unsigned int length)
  74. {
  75. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  76. ctx->crc = __crc32c_le(ctx->crc, data, length);
  77. return 0;
  78. }
  79. static int chksum_final(struct shash_desc *desc, u8 *out)
  80. {
  81. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  82. put_unaligned_le32(~ctx->crc, out);
  83. return 0;
  84. }
  85. static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
  86. {
  87. put_unaligned_le32(~__crc32c_le(*crcp, data, len), out);
  88. return 0;
  89. }
  90. static int chksum_finup(struct shash_desc *desc, const u8 *data,
  91. unsigned int len, u8 *out)
  92. {
  93. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  94. return __chksum_finup(&ctx->crc, data, len, out);
  95. }
  96. static int chksum_digest(struct shash_desc *desc, const u8 *data,
  97. unsigned int length, u8 *out)
  98. {
  99. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  100. return __chksum_finup(&mctx->key, data, length, out);
  101. }
  102. static int crc32c_cra_init(struct crypto_tfm *tfm)
  103. {
  104. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  105. mctx->key = ~0;
  106. return 0;
  107. }
  108. static struct shash_alg alg = {
  109. .digestsize = CHKSUM_DIGEST_SIZE,
  110. .setkey = chksum_setkey,
  111. .init = chksum_init,
  112. .update = chksum_update,
  113. .final = chksum_final,
  114. .finup = chksum_finup,
  115. .digest = chksum_digest,
  116. .descsize = sizeof(struct chksum_desc_ctx),
  117. .base = {
  118. .cra_name = "crc32c",
  119. .cra_driver_name = "crc32c-generic",
  120. .cra_priority = 100,
  121. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  122. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  123. .cra_ctxsize = sizeof(struct chksum_ctx),
  124. .cra_module = THIS_MODULE,
  125. .cra_init = crc32c_cra_init,
  126. }
  127. };
  128. static int __init crc32c_mod_init(void)
  129. {
  130. return crypto_register_shash(&alg);
  131. }
  132. static void __exit crc32c_mod_fini(void)
  133. {
  134. crypto_unregister_shash(&alg);
  135. }
  136. subsys_initcall(crc32c_mod_init);
  137. module_exit(crc32c_mod_fini);
  138. MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
  139. MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
  140. MODULE_LICENSE("GPL");
  141. MODULE_ALIAS_CRYPTO("crc32c");
  142. MODULE_ALIAS_CRYPTO("crc32c-generic");