crc32c-vpmsum_glue.c 3.9 KB

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