crc32c.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Cryptographic API.
  3. *
  4. * CRC32C chksum
  5. *
  6. * This module file is a wrapper to invoke the lib/crc32c routines.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/string.h>
  17. #include <linux/crypto.h>
  18. #include <linux/crc32c.h>
  19. #include <asm/byteorder.h>
  20. #define CHKSUM_BLOCK_SIZE 32
  21. #define CHKSUM_DIGEST_SIZE 4
  22. struct chksum_ctx {
  23. u32 crc;
  24. };
  25. /*
  26. * Steps through buffer one byte at at time, calculates reflected
  27. * crc using table.
  28. */
  29. static void chksum_init(void *ctx)
  30. {
  31. struct chksum_ctx *mctx = ctx;
  32. mctx->crc = ~(u32)0; /* common usage */
  33. }
  34. /*
  35. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  36. * If your algorithm starts with ~0, then XOR with ~0 before you set
  37. * the seed.
  38. */
  39. static int chksum_setkey(void *ctx, const u8 *key, unsigned int keylen,
  40. u32 *flags)
  41. {
  42. struct chksum_ctx *mctx = ctx;
  43. if (keylen != sizeof(mctx->crc)) {
  44. if (flags)
  45. *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
  46. return -EINVAL;
  47. }
  48. mctx->crc = __cpu_to_le32(*(u32 *)key);
  49. return 0;
  50. }
  51. static void chksum_update(void *ctx, const u8 *data, unsigned int length)
  52. {
  53. struct chksum_ctx *mctx = ctx;
  54. u32 mcrc;
  55. mcrc = crc32c(mctx->crc, data, (size_t)length);
  56. mctx->crc = mcrc;
  57. }
  58. static void chksum_final(void *ctx, u8 *out)
  59. {
  60. struct chksum_ctx *mctx = ctx;
  61. u32 mcrc = (mctx->crc ^ ~(u32)0);
  62. *(u32 *)out = __le32_to_cpu(mcrc);
  63. }
  64. static struct crypto_alg alg = {
  65. .cra_name = "crc32c",
  66. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  67. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  68. .cra_ctxsize = sizeof(struct chksum_ctx),
  69. .cra_module = THIS_MODULE,
  70. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  71. .cra_u = {
  72. .digest = {
  73. .dia_digestsize= CHKSUM_DIGEST_SIZE,
  74. .dia_setkey = chksum_setkey,
  75. .dia_init = chksum_init,
  76. .dia_update = chksum_update,
  77. .dia_final = chksum_final
  78. }
  79. }
  80. };
  81. static int __init init(void)
  82. {
  83. return crypto_register_alg(&alg);
  84. }
  85. static void __exit fini(void)
  86. {
  87. crypto_unregister_alg(&alg);
  88. }
  89. module_init(init);
  90. module_exit(fini);
  91. MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
  92. MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
  93. MODULE_LICENSE("GPL");