crc32c-vpmsum_glue.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <linux/crc32.h>
  2. #include <crypto/internal/hash.h>
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. #include <linux/string.h>
  6. #include <linux/kernel.h>
  7. #include <linux/cpufeature.h>
  8. #include <asm/switch_to.h>
  9. #define CHKSUM_BLOCK_SIZE 1
  10. #define CHKSUM_DIGEST_SIZE 4
  11. #define VMX_ALIGN 16
  12. #define VMX_ALIGN_MASK (VMX_ALIGN-1)
  13. #define VECTOR_BREAKPOINT 512
  14. u32 __crc32c_vpmsum(u32 crc, unsigned char const *p, size_t len);
  15. static u32 crc32c_vpmsum(u32 crc, unsigned char const *p, size_t len)
  16. {
  17. unsigned int prealign;
  18. unsigned int tail;
  19. if (len < (VECTOR_BREAKPOINT + VMX_ALIGN) || in_interrupt())
  20. return __crc32c_le(crc, p, len);
  21. if ((unsigned long)p & VMX_ALIGN_MASK) {
  22. prealign = VMX_ALIGN - ((unsigned long)p & VMX_ALIGN_MASK);
  23. crc = __crc32c_le(crc, p, prealign);
  24. len -= prealign;
  25. p += prealign;
  26. }
  27. if (len & ~VMX_ALIGN_MASK) {
  28. pagefault_disable();
  29. enable_kernel_altivec();
  30. crc = __crc32c_vpmsum(crc, p, len & ~VMX_ALIGN_MASK);
  31. pagefault_enable();
  32. }
  33. tail = len & VMX_ALIGN_MASK;
  34. if (tail) {
  35. p += len & ~VMX_ALIGN_MASK;
  36. crc = __crc32c_le(crc, p, tail);
  37. }
  38. return crc;
  39. }
  40. static int crc32c_vpmsum_cra_init(struct crypto_tfm *tfm)
  41. {
  42. u32 *key = crypto_tfm_ctx(tfm);
  43. *key = 0;
  44. return 0;
  45. }
  46. /*
  47. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  48. * If your algorithm starts with ~0, then XOR with ~0 before you set
  49. * the seed.
  50. */
  51. static int crc32c_vpmsum_setkey(struct crypto_shash *hash, const u8 *key,
  52. unsigned int keylen)
  53. {
  54. u32 *mctx = crypto_shash_ctx(hash);
  55. if (keylen != sizeof(u32)) {
  56. crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  57. return -EINVAL;
  58. }
  59. *mctx = le32_to_cpup((__le32 *)key);
  60. return 0;
  61. }
  62. static int crc32c_vpmsum_init(struct shash_desc *desc)
  63. {
  64. u32 *mctx = crypto_shash_ctx(desc->tfm);
  65. u32 *crcp = shash_desc_ctx(desc);
  66. *crcp = *mctx;
  67. return 0;
  68. }
  69. static int crc32c_vpmsum_update(struct shash_desc *desc, const u8 *data,
  70. unsigned int len)
  71. {
  72. u32 *crcp = shash_desc_ctx(desc);
  73. *crcp = crc32c_vpmsum(*crcp, data, len);
  74. return 0;
  75. }
  76. static int __crc32c_vpmsum_finup(u32 *crcp, const u8 *data, unsigned int len,
  77. u8 *out)
  78. {
  79. *(__le32 *)out = ~cpu_to_le32(crc32c_vpmsum(*crcp, data, len));
  80. return 0;
  81. }
  82. static int crc32c_vpmsum_finup(struct shash_desc *desc, const u8 *data,
  83. unsigned int len, u8 *out)
  84. {
  85. return __crc32c_vpmsum_finup(shash_desc_ctx(desc), data, len, out);
  86. }
  87. static int crc32c_vpmsum_final(struct shash_desc *desc, u8 *out)
  88. {
  89. u32 *crcp = shash_desc_ctx(desc);
  90. *(__le32 *)out = ~cpu_to_le32p(crcp);
  91. return 0;
  92. }
  93. static int crc32c_vpmsum_digest(struct shash_desc *desc, const u8 *data,
  94. unsigned int len, u8 *out)
  95. {
  96. return __crc32c_vpmsum_finup(crypto_shash_ctx(desc->tfm), data, len,
  97. out);
  98. }
  99. static struct shash_alg alg = {
  100. .setkey = crc32c_vpmsum_setkey,
  101. .init = crc32c_vpmsum_init,
  102. .update = crc32c_vpmsum_update,
  103. .final = crc32c_vpmsum_final,
  104. .finup = crc32c_vpmsum_finup,
  105. .digest = crc32c_vpmsum_digest,
  106. .descsize = sizeof(u32),
  107. .digestsize = CHKSUM_DIGEST_SIZE,
  108. .base = {
  109. .cra_name = "crc32c",
  110. .cra_driver_name = "crc32c-vpmsum",
  111. .cra_priority = 200,
  112. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  113. .cra_ctxsize = sizeof(u32),
  114. .cra_module = THIS_MODULE,
  115. .cra_init = crc32c_vpmsum_cra_init,
  116. }
  117. };
  118. static int __init crc32c_vpmsum_mod_init(void)
  119. {
  120. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  121. return -ENODEV;
  122. return crypto_register_shash(&alg);
  123. }
  124. static void __exit crc32c_vpmsum_mod_fini(void)
  125. {
  126. crypto_unregister_shash(&alg);
  127. }
  128. module_cpu_feature_match(PPC_MODULE_FEATURE_VEC_CRYPTO, crc32c_vpmsum_mod_init);
  129. module_exit(crc32c_vpmsum_mod_fini);
  130. MODULE_AUTHOR("Anton Blanchard <anton@samba.org>");
  131. MODULE_DESCRIPTION("CRC32C using vector polynomial multiply-sum instructions");
  132. MODULE_LICENSE("GPL");
  133. MODULE_ALIAS_CRYPTO("crc32c");
  134. MODULE_ALIAS_CRYPTO("crc32c-vpmsum");