crc32_generic.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <asm/unaligned.h>
  30. #include <linux/crc32.h>
  31. #include <crypto/internal/hash.h>
  32. #include <linux/init.h>
  33. #include <linux/module.h>
  34. #include <linux/string.h>
  35. #include <linux/kernel.h>
  36. #define CHKSUM_BLOCK_SIZE 1
  37. #define CHKSUM_DIGEST_SIZE 4
  38. /** No default init with ~0 */
  39. static int crc32_cra_init(struct crypto_tfm *tfm)
  40. {
  41. u32 *key = crypto_tfm_ctx(tfm);
  42. *key = 0;
  43. return 0;
  44. }
  45. /*
  46. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  47. * If your algorithm starts with ~0, then XOR with ~0 before you set
  48. * the seed.
  49. */
  50. static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
  51. unsigned int keylen)
  52. {
  53. u32 *mctx = crypto_shash_ctx(hash);
  54. if (keylen != sizeof(u32))
  55. return -EINVAL;
  56. *mctx = get_unaligned_le32(key);
  57. return 0;
  58. }
  59. static int crc32_init(struct shash_desc *desc)
  60. {
  61. u32 *mctx = crypto_shash_ctx(desc->tfm);
  62. u32 *crcp = shash_desc_ctx(desc);
  63. *crcp = *mctx;
  64. return 0;
  65. }
  66. static int crc32_update(struct shash_desc *desc, const u8 *data,
  67. unsigned int len)
  68. {
  69. u32 *crcp = shash_desc_ctx(desc);
  70. *crcp = crc32_le(*crcp, data, len);
  71. return 0;
  72. }
  73. /* No final XOR 0xFFFFFFFF, like crc32_le */
  74. static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
  75. u8 *out)
  76. {
  77. put_unaligned_le32(crc32_le(*crcp, data, len), out);
  78. return 0;
  79. }
  80. static int crc32_finup(struct shash_desc *desc, const u8 *data,
  81. unsigned int len, u8 *out)
  82. {
  83. return __crc32_finup(shash_desc_ctx(desc), data, len, out);
  84. }
  85. static int crc32_final(struct shash_desc *desc, u8 *out)
  86. {
  87. u32 *crcp = shash_desc_ctx(desc);
  88. put_unaligned_le32(*crcp, out);
  89. return 0;
  90. }
  91. static int crc32_digest(struct shash_desc *desc, const u8 *data,
  92. unsigned int len, u8 *out)
  93. {
  94. return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
  95. out);
  96. }
  97. static struct shash_alg alg = {
  98. .setkey = crc32_setkey,
  99. .init = crc32_init,
  100. .update = crc32_update,
  101. .final = crc32_final,
  102. .finup = crc32_finup,
  103. .digest = crc32_digest,
  104. .descsize = sizeof(u32),
  105. .digestsize = CHKSUM_DIGEST_SIZE,
  106. .base = {
  107. .cra_name = "crc32",
  108. .cra_driver_name = "crc32-generic",
  109. .cra_priority = 100,
  110. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  111. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  112. .cra_ctxsize = sizeof(u32),
  113. .cra_module = THIS_MODULE,
  114. .cra_init = crc32_cra_init,
  115. }
  116. };
  117. static int __init crc32_mod_init(void)
  118. {
  119. return crypto_register_shash(&alg);
  120. }
  121. static void __exit crc32_mod_fini(void)
  122. {
  123. crypto_unregister_shash(&alg);
  124. }
  125. subsys_initcall(crc32_mod_init);
  126. module_exit(crc32_mod_fini);
  127. MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
  128. MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
  129. MODULE_LICENSE("GPL");
  130. MODULE_ALIAS_CRYPTO("crc32");
  131. MODULE_ALIAS_CRYPTO("crc32-generic");