crc32_generic.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  56. return -EINVAL;
  57. }
  58. *mctx = get_unaligned_le32(key);
  59. return 0;
  60. }
  61. static int crc32_init(struct shash_desc *desc)
  62. {
  63. u32 *mctx = crypto_shash_ctx(desc->tfm);
  64. u32 *crcp = shash_desc_ctx(desc);
  65. *crcp = *mctx;
  66. return 0;
  67. }
  68. static int crc32_update(struct shash_desc *desc, const u8 *data,
  69. unsigned int len)
  70. {
  71. u32 *crcp = shash_desc_ctx(desc);
  72. *crcp = crc32_le(*crcp, data, len);
  73. return 0;
  74. }
  75. /* No final XOR 0xFFFFFFFF, like crc32_le */
  76. static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
  77. u8 *out)
  78. {
  79. put_unaligned_le32(crc32_le(*crcp, data, len), out);
  80. return 0;
  81. }
  82. static int crc32_finup(struct shash_desc *desc, const u8 *data,
  83. unsigned int len, u8 *out)
  84. {
  85. return __crc32_finup(shash_desc_ctx(desc), data, len, out);
  86. }
  87. static int crc32_final(struct shash_desc *desc, u8 *out)
  88. {
  89. u32 *crcp = shash_desc_ctx(desc);
  90. put_unaligned_le32(*crcp, out);
  91. return 0;
  92. }
  93. static int crc32_digest(struct shash_desc *desc, const u8 *data,
  94. unsigned int len, u8 *out)
  95. {
  96. return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
  97. out);
  98. }
  99. static struct shash_alg alg = {
  100. .setkey = crc32_setkey,
  101. .init = crc32_init,
  102. .update = crc32_update,
  103. .final = crc32_final,
  104. .finup = crc32_finup,
  105. .digest = crc32_digest,
  106. .descsize = sizeof(u32),
  107. .digestsize = CHKSUM_DIGEST_SIZE,
  108. .base = {
  109. .cra_name = "crc32",
  110. .cra_driver_name = "crc32-generic",
  111. .cra_priority = 100,
  112. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  113. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  114. .cra_ctxsize = sizeof(u32),
  115. .cra_module = THIS_MODULE,
  116. .cra_init = crc32_cra_init,
  117. }
  118. };
  119. static int __init crc32_mod_init(void)
  120. {
  121. return crypto_register_shash(&alg);
  122. }
  123. static void __exit crc32_mod_fini(void)
  124. {
  125. crypto_unregister_shash(&alg);
  126. }
  127. module_init(crc32_mod_init);
  128. module_exit(crc32_mod_fini);
  129. MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
  130. MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
  131. MODULE_LICENSE("GPL");
  132. MODULE_ALIAS_CRYPTO("crc32");
  133. MODULE_ALIAS_CRYPTO("crc32-generic");