crc32-pclmul_glue.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* GPL HEADER START
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 only,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License version 2 for more details (a copy is included
  13. * in the LICENSE file that accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * version 2 along with this program; If not, see http://www.gnu.org/licenses
  17. *
  18. * Please visit http://www.xyratex.com/contact if you need additional
  19. * information or have any questions.
  20. *
  21. * GPL HEADER END
  22. */
  23. /*
  24. * Copyright 2012 Xyratex Technology Limited
  25. *
  26. * Wrappers for kernel crypto shash api to pclmulqdq crc32 implementation.
  27. */
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/string.h>
  31. #include <linux/kernel.h>
  32. #include <linux/crc32.h>
  33. #include <crypto/internal/hash.h>
  34. #include <crypto/internal/simd.h>
  35. #include <asm/cpufeatures.h>
  36. #include <asm/cpu_device_id.h>
  37. #include <asm/simd.h>
  38. #define CHKSUM_BLOCK_SIZE 1
  39. #define CHKSUM_DIGEST_SIZE 4
  40. #define PCLMUL_MIN_LEN 64L /* minimum size of buffer
  41. * for crc32_pclmul_le_16 */
  42. #define SCALE_F 16L /* size of xmm register */
  43. #define SCALE_F_MASK (SCALE_F - 1)
  44. u32 crc32_pclmul_le_16(unsigned char const *buffer, size_t len, u32 crc32);
  45. static u32 __attribute__((pure))
  46. crc32_pclmul_le(u32 crc, unsigned char const *p, size_t len)
  47. {
  48. unsigned int iquotient;
  49. unsigned int iremainder;
  50. unsigned int prealign;
  51. if (len < PCLMUL_MIN_LEN + SCALE_F_MASK || !crypto_simd_usable())
  52. return crc32_le(crc, p, len);
  53. if ((long)p & SCALE_F_MASK) {
  54. /* align p to 16 byte */
  55. prealign = SCALE_F - ((long)p & SCALE_F_MASK);
  56. crc = crc32_le(crc, p, prealign);
  57. len -= prealign;
  58. p = (unsigned char *)(((unsigned long)p + SCALE_F_MASK) &
  59. ~SCALE_F_MASK);
  60. }
  61. iquotient = len & (~SCALE_F_MASK);
  62. iremainder = len & SCALE_F_MASK;
  63. kernel_fpu_begin();
  64. crc = crc32_pclmul_le_16(p, iquotient, crc);
  65. kernel_fpu_end();
  66. if (iremainder)
  67. crc = crc32_le(crc, p + iquotient, iremainder);
  68. return crc;
  69. }
  70. static int crc32_pclmul_cra_init(struct crypto_tfm *tfm)
  71. {
  72. u32 *key = crypto_tfm_ctx(tfm);
  73. *key = 0;
  74. return 0;
  75. }
  76. static int crc32_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
  77. unsigned int keylen)
  78. {
  79. u32 *mctx = crypto_shash_ctx(hash);
  80. if (keylen != sizeof(u32))
  81. return -EINVAL;
  82. *mctx = le32_to_cpup((__le32 *)key);
  83. return 0;
  84. }
  85. static int crc32_pclmul_init(struct shash_desc *desc)
  86. {
  87. u32 *mctx = crypto_shash_ctx(desc->tfm);
  88. u32 *crcp = shash_desc_ctx(desc);
  89. *crcp = *mctx;
  90. return 0;
  91. }
  92. static int crc32_pclmul_update(struct shash_desc *desc, const u8 *data,
  93. unsigned int len)
  94. {
  95. u32 *crcp = shash_desc_ctx(desc);
  96. *crcp = crc32_pclmul_le(*crcp, data, len);
  97. return 0;
  98. }
  99. /* No final XOR 0xFFFFFFFF, like crc32_le */
  100. static int __crc32_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
  101. u8 *out)
  102. {
  103. *(__le32 *)out = cpu_to_le32(crc32_pclmul_le(*crcp, data, len));
  104. return 0;
  105. }
  106. static int crc32_pclmul_finup(struct shash_desc *desc, const u8 *data,
  107. unsigned int len, u8 *out)
  108. {
  109. return __crc32_pclmul_finup(shash_desc_ctx(desc), data, len, out);
  110. }
  111. static int crc32_pclmul_final(struct shash_desc *desc, u8 *out)
  112. {
  113. u32 *crcp = shash_desc_ctx(desc);
  114. *(__le32 *)out = cpu_to_le32p(crcp);
  115. return 0;
  116. }
  117. static int crc32_pclmul_digest(struct shash_desc *desc, const u8 *data,
  118. unsigned int len, u8 *out)
  119. {
  120. return __crc32_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
  121. out);
  122. }
  123. static struct shash_alg alg = {
  124. .setkey = crc32_pclmul_setkey,
  125. .init = crc32_pclmul_init,
  126. .update = crc32_pclmul_update,
  127. .final = crc32_pclmul_final,
  128. .finup = crc32_pclmul_finup,
  129. .digest = crc32_pclmul_digest,
  130. .descsize = sizeof(u32),
  131. .digestsize = CHKSUM_DIGEST_SIZE,
  132. .base = {
  133. .cra_name = "crc32",
  134. .cra_driver_name = "crc32-pclmul",
  135. .cra_priority = 200,
  136. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  137. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  138. .cra_ctxsize = sizeof(u32),
  139. .cra_module = THIS_MODULE,
  140. .cra_init = crc32_pclmul_cra_init,
  141. }
  142. };
  143. static const struct x86_cpu_id crc32pclmul_cpu_id[] = {
  144. X86_MATCH_FEATURE(X86_FEATURE_PCLMULQDQ, NULL),
  145. {}
  146. };
  147. MODULE_DEVICE_TABLE(x86cpu, crc32pclmul_cpu_id);
  148. static int __init crc32_pclmul_mod_init(void)
  149. {
  150. if (!x86_match_cpu(crc32pclmul_cpu_id)) {
  151. pr_info("PCLMULQDQ-NI instructions are not detected.\n");
  152. return -ENODEV;
  153. }
  154. return crypto_register_shash(&alg);
  155. }
  156. static void __exit crc32_pclmul_mod_fini(void)
  157. {
  158. crypto_unregister_shash(&alg);
  159. }
  160. module_init(crc32_pclmul_mod_init);
  161. module_exit(crc32_pclmul_mod_fini);
  162. MODULE_AUTHOR("Alexander Boyko <[email protected]>");
  163. MODULE_LICENSE("GPL");
  164. MODULE_ALIAS_CRYPTO("crc32");
  165. MODULE_ALIAS_CRYPTO("crc32-pclmul");