crc32c_generic.c 4.4 KB

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