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 imlementation.
  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 <asm/cpufeatures.h>
  35. #include <asm/cpu_device_id.h>
  36. #include <asm/fpu/api.h>
  37. #define CHKSUM_BLOCK_SIZE 1
  38. #define CHKSUM_DIGEST_SIZE 4
  39. #define PCLMUL_MIN_LEN 64L /* minimum size of buffer
  40. * for crc32_pclmul_le_16 */
  41. #define SCALE_F 16L /* size of xmm register */
  42. #define SCALE_F_MASK (SCALE_F - 1)
  43. u32 crc32_pclmul_le_16(unsigned char const *buffer, size_t len, u32 crc32);
  44. static u32 __attribute__((pure))
  45. crc32_pclmul_le(u32 crc, unsigned char const *p, size_t len)
  46. {
  47. unsigned int iquotient;
  48. unsigned int iremainder;
  49. unsigned int prealign;
  50. if (len < PCLMUL_MIN_LEN + SCALE_F_MASK || !irq_fpu_usable())
  51. return crc32_le(crc, p, len);
  52. if ((long)p & SCALE_F_MASK) {
  53. /* align p to 16 byte */
  54. prealign = SCALE_F - ((long)p & SCALE_F_MASK);
  55. crc = crc32_le(crc, p, prealign);
  56. len -= prealign;
  57. p = (unsigned char *)(((unsigned long)p + SCALE_F_MASK) &
  58. ~SCALE_F_MASK);
  59. }
  60. iquotient = len & (~SCALE_F_MASK);
  61. iremainder = len & SCALE_F_MASK;
  62. kernel_fpu_begin();
  63. crc = crc32_pclmul_le_16(p, iquotient, crc);
  64. kernel_fpu_end();
  65. if (iremainder)
  66. crc = crc32_le(crc, p + iquotient, iremainder);
  67. return crc;
  68. }
  69. static int crc32_pclmul_cra_init(struct crypto_tfm *tfm)
  70. {
  71. u32 *key = crypto_tfm_ctx(tfm);
  72. *key = 0;
  73. return 0;
  74. }
  75. static int crc32_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
  76. unsigned int keylen)
  77. {
  78. u32 *mctx = crypto_shash_ctx(hash);
  79. if (keylen != sizeof(u32)) {
  80. crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  81. return -EINVAL;
  82. }
  83. *mctx = le32_to_cpup((__le32 *)key);
  84. return 0;
  85. }
  86. static int crc32_pclmul_init(struct shash_desc *desc)
  87. {
  88. u32 *mctx = crypto_shash_ctx(desc->tfm);
  89. u32 *crcp = shash_desc_ctx(desc);
  90. *crcp = *mctx;
  91. return 0;
  92. }
  93. static int crc32_pclmul_update(struct shash_desc *desc, const u8 *data,
  94. unsigned int len)
  95. {
  96. u32 *crcp = shash_desc_ctx(desc);
  97. *crcp = crc32_pclmul_le(*crcp, data, len);
  98. return 0;
  99. }
  100. /* No final XOR 0xFFFFFFFF, like crc32_le */
  101. static int __crc32_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
  102. u8 *out)
  103. {
  104. *(__le32 *)out = cpu_to_le32(crc32_pclmul_le(*crcp, data, len));
  105. return 0;
  106. }
  107. static int crc32_pclmul_finup(struct shash_desc *desc, const u8 *data,
  108. unsigned int len, u8 *out)
  109. {
  110. return __crc32_pclmul_finup(shash_desc_ctx(desc), data, len, out);
  111. }
  112. static int crc32_pclmul_final(struct shash_desc *desc, u8 *out)
  113. {
  114. u32 *crcp = shash_desc_ctx(desc);
  115. *(__le32 *)out = cpu_to_le32p(crcp);
  116. return 0;
  117. }
  118. static int crc32_pclmul_digest(struct shash_desc *desc, const u8 *data,
  119. unsigned int len, u8 *out)
  120. {
  121. return __crc32_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
  122. out);
  123. }
  124. static struct shash_alg alg = {
  125. .setkey = crc32_pclmul_setkey,
  126. .init = crc32_pclmul_init,
  127. .update = crc32_pclmul_update,
  128. .final = crc32_pclmul_final,
  129. .finup = crc32_pclmul_finup,
  130. .digest = crc32_pclmul_digest,
  131. .descsize = sizeof(u32),
  132. .digestsize = CHKSUM_DIGEST_SIZE,
  133. .base = {
  134. .cra_name = "crc32",
  135. .cra_driver_name = "crc32-pclmul",
  136. .cra_priority = 200,
  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_FEATURE_MATCH(X86_FEATURE_PCLMULQDQ),
  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 <alexander_boyko@xyratex.com>");
  163. MODULE_LICENSE("GPL");
  164. MODULE_ALIAS_CRYPTO("crc32");
  165. MODULE_ALIAS_CRYPTO("crc32-pclmul");