crct10dif_generic.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 crc, const u8 *data, unsigned int len, u8 *out)
  58. {
  59. *(__u16 *)out = crc_t10dif_generic(crc, data, len);
  60. return 0;
  61. }
  62. static int chksum_finup(struct shash_desc *desc, const u8 *data,
  63. unsigned int len, u8 *out)
  64. {
  65. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  66. return __chksum_finup(ctx->crc, data, len, out);
  67. }
  68. static int chksum_digest(struct shash_desc *desc, const u8 *data,
  69. unsigned int length, u8 *out)
  70. {
  71. return __chksum_finup(0, data, length, out);
  72. }
  73. static struct shash_alg alg = {
  74. .digestsize = CRC_T10DIF_DIGEST_SIZE,
  75. .init = chksum_init,
  76. .update = chksum_update,
  77. .final = chksum_final,
  78. .finup = chksum_finup,
  79. .digest = chksum_digest,
  80. .descsize = sizeof(struct chksum_desc_ctx),
  81. .base = {
  82. .cra_name = "crct10dif",
  83. .cra_driver_name = "crct10dif-generic",
  84. .cra_priority = 100,
  85. .cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
  86. .cra_module = THIS_MODULE,
  87. }
  88. };
  89. static int __init crct10dif_mod_init(void)
  90. {
  91. return crypto_register_shash(&alg);
  92. }
  93. static void __exit crct10dif_mod_fini(void)
  94. {
  95. crypto_unregister_shash(&alg);
  96. }
  97. module_init(crct10dif_mod_init);
  98. module_exit(crct10dif_mod_fini);
  99. MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
  100. MODULE_DESCRIPTION("T10 DIF CRC calculation.");
  101. MODULE_LICENSE("GPL");
  102. MODULE_ALIAS_CRYPTO("crct10dif");
  103. MODULE_ALIAS_CRYPTO("crct10dif-generic");