crct10dif-pclmul_glue.c 3.8 KB

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