crc32.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* GPL HEADER START
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 only,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License version 2 for more details (a copy is included
  13. * in the LICENSE file that accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * version 2 along with this program; If not, see http://www.gnu.org/licenses
  17. *
  18. * Please visit http://www.xyratex.com/contact if you need additional
  19. * information or have any questions.
  20. *
  21. * GPL HEADER END
  22. */
  23. /*
  24. * Copyright 2012 Xyratex Technology Limited
  25. */
  26. /*
  27. * This is crypto api shash wrappers to crc32_le.
  28. */
  29. #include <linux/crc32.h>
  30. #include <crypto/internal/hash.h>
  31. #include <linux/init.h>
  32. #include <linux/module.h>
  33. #include <linux/string.h>
  34. #include <linux/kernel.h>
  35. #define CHKSUM_BLOCK_SIZE 1
  36. #define CHKSUM_DIGEST_SIZE 4
  37. static u32 __crc32_le(u32 crc, unsigned char const *p, size_t len)
  38. {
  39. return crc32_le(crc, p, len);
  40. }
  41. /** No default init with ~0 */
  42. static int crc32_cra_init(struct crypto_tfm *tfm)
  43. {
  44. u32 *key = crypto_tfm_ctx(tfm);
  45. *key = 0;
  46. return 0;
  47. }
  48. /*
  49. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  50. * If your algorithm starts with ~0, then XOR with ~0 before you set
  51. * the seed.
  52. */
  53. static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
  54. unsigned int keylen)
  55. {
  56. u32 *mctx = crypto_shash_ctx(hash);
  57. if (keylen != sizeof(u32)) {
  58. crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  59. return -EINVAL;
  60. }
  61. *mctx = le32_to_cpup((__le32 *)key);
  62. return 0;
  63. }
  64. static int crc32_init(struct shash_desc *desc)
  65. {
  66. u32 *mctx = crypto_shash_ctx(desc->tfm);
  67. u32 *crcp = shash_desc_ctx(desc);
  68. *crcp = *mctx;
  69. return 0;
  70. }
  71. static int crc32_update(struct shash_desc *desc, const u8 *data,
  72. unsigned int len)
  73. {
  74. u32 *crcp = shash_desc_ctx(desc);
  75. *crcp = __crc32_le(*crcp, data, len);
  76. return 0;
  77. }
  78. /* No final XOR 0xFFFFFFFF, like crc32_le */
  79. static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
  80. u8 *out)
  81. {
  82. *(__le32 *)out = cpu_to_le32(__crc32_le(*crcp, data, len));
  83. return 0;
  84. }
  85. static int crc32_finup(struct shash_desc *desc, const u8 *data,
  86. unsigned int len, u8 *out)
  87. {
  88. return __crc32_finup(shash_desc_ctx(desc), data, len, out);
  89. }
  90. static int crc32_final(struct shash_desc *desc, u8 *out)
  91. {
  92. u32 *crcp = shash_desc_ctx(desc);
  93. *(__le32 *)out = cpu_to_le32p(crcp);
  94. return 0;
  95. }
  96. static int crc32_digest(struct shash_desc *desc, const u8 *data,
  97. unsigned int len, u8 *out)
  98. {
  99. return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
  100. out);
  101. }
  102. static struct shash_alg alg = {
  103. .setkey = crc32_setkey,
  104. .init = crc32_init,
  105. .update = crc32_update,
  106. .final = crc32_final,
  107. .finup = crc32_finup,
  108. .digest = crc32_digest,
  109. .descsize = sizeof(u32),
  110. .digestsize = CHKSUM_DIGEST_SIZE,
  111. .base = {
  112. .cra_name = "crc32",
  113. .cra_driver_name = "crc32-table",
  114. .cra_priority = 100,
  115. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  116. .cra_ctxsize = sizeof(u32),
  117. .cra_module = THIS_MODULE,
  118. .cra_init = crc32_cra_init,
  119. }
  120. };
  121. static int __init crc32_mod_init(void)
  122. {
  123. return crypto_register_shash(&alg);
  124. }
  125. static void __exit crc32_mod_fini(void)
  126. {
  127. crypto_unregister_shash(&alg);
  128. }
  129. module_init(crc32_mod_init);
  130. module_exit(crc32_mod_fini);
  131. MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
  132. MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
  133. MODULE_LICENSE("GPL");
  134. MODULE_ALIAS_CRYPTO("crc32");