crct10dif_generic.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Cryptographic API.
  3. *
  4. * T10 Data Integrity Field CRC16 Crypto Transform
  5. *
  6. * Copyright (c) 2007 Oracle Corporation. All rights reserved.
  7. * Written by Martin K. Petersen <martin.petersen@oracle.com>
  8. * Copyright (C) 2013 Intel Corporation
  9. * Author: Tim Chen <tim.c.chen@linux.intel.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/crc-t10dif.h>
  28. #include <crypto/internal/hash.h>
  29. #include <linux/init.h>
  30. #include <linux/kernel.h>
  31. struct chksum_desc_ctx {
  32. __u16 crc;
  33. };
  34. /*
  35. * Steps through buffer one byte at at time, calculates reflected
  36. * crc using table.
  37. */
  38. static int chksum_init(struct shash_desc *desc)
  39. {
  40. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  41. ctx->crc = 0;
  42. return 0;
  43. }
  44. static int chksum_update(struct shash_desc *desc, const u8 *data,
  45. unsigned int length)
  46. {
  47. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  48. ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
  49. return 0;
  50. }
  51. static int chksum_final(struct shash_desc *desc, u8 *out)
  52. {
  53. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  54. *(__u16 *)out = ctx->crc;
  55. return 0;
  56. }
  57. static int __chksum_finup(__u16 *crcp, const u8 *data, unsigned int len,
  58. u8 *out)
  59. {
  60. *(__u16 *)out = crc_t10dif_generic(*crcp, data, len);
  61. return 0;
  62. }
  63. static int chksum_finup(struct shash_desc *desc, const u8 *data,
  64. unsigned int len, u8 *out)
  65. {
  66. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  67. return __chksum_finup(&ctx->crc, data, len, out);
  68. }
  69. static int chksum_digest(struct shash_desc *desc, const u8 *data,
  70. unsigned int length, u8 *out)
  71. {
  72. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  73. return __chksum_finup(&ctx->crc, data, length, out);
  74. }
  75. static struct shash_alg alg = {
  76. .digestsize = CRC_T10DIF_DIGEST_SIZE,
  77. .init = chksum_init,
  78. .update = chksum_update,
  79. .final = chksum_final,
  80. .finup = chksum_finup,
  81. .digest = chksum_digest,
  82. .descsize = sizeof(struct chksum_desc_ctx),
  83. .base = {
  84. .cra_name = "crct10dif",
  85. .cra_driver_name = "crct10dif-generic",
  86. .cra_priority = 100,
  87. .cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
  88. .cra_module = THIS_MODULE,
  89. }
  90. };
  91. static int __init crct10dif_mod_init(void)
  92. {
  93. return crypto_register_shash(&alg);
  94. }
  95. static void __exit crct10dif_mod_fini(void)
  96. {
  97. crypto_unregister_shash(&alg);
  98. }
  99. module_init(crct10dif_mod_init);
  100. module_exit(crct10dif_mod_fini);
  101. MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
  102. MODULE_DESCRIPTION("T10 DIF CRC calculation.");
  103. MODULE_LICENSE("GPL");
  104. MODULE_ALIAS_CRYPTO("crct10dif");
  105. MODULE_ALIAS_CRYPTO("crct10dif-generic");