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 <asm/unaligned.h>
  38. #include <crypto/internal/hash.h>
  39. #include <linux/init.h>
  40. #include <linux/module.h>
  41. #include <linux/string.h>
  42. #include <linux/kernel.h>
  43. #include <linux/crc32.h>
  44. #define CHKSUM_BLOCK_SIZE 1
  45. #define CHKSUM_DIGEST_SIZE 4
  46. struct chksum_ctx {
  47. u32 key;
  48. };
  49. struct chksum_desc_ctx {
  50. u32 crc;
  51. };
  52. /*
  53. * Steps through buffer one byte at at time, calculates reflected
  54. * crc using table.
  55. */
  56. static int chksum_init(struct shash_desc *desc)
  57. {
  58. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  59. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  60. ctx->crc = mctx->key;
  61. return 0;
  62. }
  63. /*
  64. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  65. * If your algorithm starts with ~0, then XOR with ~0 before you set
  66. * the seed.
  67. */
  68. static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
  69. unsigned int keylen)
  70. {
  71. struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
  72. if (keylen != sizeof(mctx->key)) {
  73. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  74. return -EINVAL;
  75. }
  76. mctx->key = get_unaligned_le32(key);
  77. return 0;
  78. }
  79. static int chksum_update(struct shash_desc *desc, const u8 *data,
  80. unsigned int length)
  81. {
  82. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  83. ctx->crc = __crc32c_le(ctx->crc, data, length);
  84. return 0;
  85. }
  86. static int chksum_final(struct shash_desc *desc, u8 *out)
  87. {
  88. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  89. put_unaligned_le32(~ctx->crc, out);
  90. return 0;
  91. }
  92. static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
  93. {
  94. put_unaligned_le32(~__crc32c_le(*crcp, data, len), out);
  95. return 0;
  96. }
  97. static int chksum_finup(struct shash_desc *desc, const u8 *data,
  98. unsigned int len, u8 *out)
  99. {
  100. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  101. return __chksum_finup(&ctx->crc, data, len, out);
  102. }
  103. static int chksum_digest(struct shash_desc *desc, const u8 *data,
  104. unsigned int length, u8 *out)
  105. {
  106. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  107. return __chksum_finup(&mctx->key, data, length, out);
  108. }
  109. static int crc32c_cra_init(struct crypto_tfm *tfm)
  110. {
  111. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  112. mctx->key = ~0;
  113. return 0;
  114. }
  115. static struct shash_alg alg = {
  116. .digestsize = CHKSUM_DIGEST_SIZE,
  117. .setkey = chksum_setkey,
  118. .init = chksum_init,
  119. .update = chksum_update,
  120. .final = chksum_final,
  121. .finup = chksum_finup,
  122. .digest = chksum_digest,
  123. .descsize = sizeof(struct chksum_desc_ctx),
  124. .base = {
  125. .cra_name = "crc32c",
  126. .cra_driver_name = "crc32c-generic",
  127. .cra_priority = 100,
  128. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  129. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  130. .cra_ctxsize = sizeof(struct chksum_ctx),
  131. .cra_module = THIS_MODULE,
  132. .cra_init = crc32c_cra_init,
  133. }
  134. };
  135. static int __init crc32c_mod_init(void)
  136. {
  137. return crypto_register_shash(&alg);
  138. }
  139. static void __exit crc32c_mod_fini(void)
  140. {
  141. crypto_unregister_shash(&alg);
  142. }
  143. module_init(crc32c_mod_init);
  144. module_exit(crc32c_mod_fini);
  145. MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
  146. MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
  147. MODULE_LICENSE("GPL");
  148. MODULE_ALIAS_CRYPTO("crc32c");
  149. MODULE_ALIAS_CRYPTO("crc32c-generic");