crc32c.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <linux/types.h>
  20. #include <asm/byteorder.h>
  21. #define CHKSUM_BLOCK_SIZE 32
  22. #define CHKSUM_DIGEST_SIZE 4
  23. struct chksum_ctx {
  24. u32 crc;
  25. };
  26. /*
  27. * Steps through buffer one byte at at time, calculates reflected
  28. * crc using table.
  29. */
  30. static void chksum_init(void *ctx)
  31. {
  32. struct chksum_ctx *mctx = ctx;
  33. mctx->crc = ~(u32)0; /* common usage */
  34. }
  35. /*
  36. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  37. * If your algorithm starts with ~0, then XOR with ~0 before you set
  38. * the seed.
  39. */
  40. static int chksum_setkey(void *ctx, const u8 *key, unsigned int keylen,
  41. u32 *flags)
  42. {
  43. struct chksum_ctx *mctx = ctx;
  44. if (keylen != sizeof(mctx->crc)) {
  45. if (flags)
  46. *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
  47. return -EINVAL;
  48. }
  49. mctx->crc = __cpu_to_le32(*(u32 *)key);
  50. return 0;
  51. }
  52. static void chksum_update(void *ctx, const u8 *data, unsigned int length)
  53. {
  54. struct chksum_ctx *mctx = ctx;
  55. u32 mcrc;
  56. mcrc = crc32c(mctx->crc, data, (size_t)length);
  57. mctx->crc = mcrc;
  58. }
  59. static void chksum_final(void *ctx, u8 *out)
  60. {
  61. struct chksum_ctx *mctx = ctx;
  62. u32 mcrc = (mctx->crc ^ ~(u32)0);
  63. *(u32 *)out = __le32_to_cpu(mcrc);
  64. }
  65. static struct crypto_alg alg = {
  66. .cra_name = "crc32c",
  67. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  68. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  69. .cra_ctxsize = sizeof(struct chksum_ctx),
  70. .cra_module = THIS_MODULE,
  71. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  72. .cra_u = {
  73. .digest = {
  74. .dia_digestsize= CHKSUM_DIGEST_SIZE,
  75. .dia_setkey = chksum_setkey,
  76. .dia_init = chksum_init,
  77. .dia_update = chksum_update,
  78. .dia_final = chksum_final
  79. }
  80. }
  81. };
  82. static int __init init(void)
  83. {
  84. return crypto_register_alg(&alg);
  85. }
  86. static void __exit fini(void)
  87. {
  88. crypto_unregister_alg(&alg);
  89. }
  90. module_init(init);
  91. module_exit(fini);
  92. MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
  93. MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
  94. MODULE_LICENSE("GPL");