chacha-p10-glue.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PowerPC P10 (ppc64le) accelerated ChaCha and XChaCha stream ciphers,
  4. * including ChaCha20 (RFC7539)
  5. *
  6. * Copyright 2023- IBM Corp. All rights reserved.
  7. */
  8. #include <crypto/algapi.h>
  9. #include <crypto/internal/chacha.h>
  10. #include <crypto/internal/simd.h>
  11. #include <crypto/internal/skcipher.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/cpufeature.h>
  15. #include <linux/sizes.h>
  16. #include <asm/simd.h>
  17. #include <asm/switch_to.h>
  18. asmlinkage void chacha_p10le_8x(u32 *state, u8 *dst, const u8 *src,
  19. unsigned int len, int nrounds);
  20. static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_p10);
  21. static void vsx_begin(void)
  22. {
  23. preempt_disable();
  24. enable_kernel_vsx();
  25. }
  26. static void vsx_end(void)
  27. {
  28. disable_kernel_vsx();
  29. preempt_enable();
  30. }
  31. static void chacha_p10_do_8x(u32 *state, u8 *dst, const u8 *src,
  32. unsigned int bytes, int nrounds)
  33. {
  34. unsigned int l = bytes & ~0x0FF;
  35. if (l > 0) {
  36. chacha_p10le_8x(state, dst, src, l, nrounds);
  37. bytes -= l;
  38. src += l;
  39. dst += l;
  40. state[12] += l / CHACHA_BLOCK_SIZE;
  41. }
  42. if (bytes > 0)
  43. chacha_crypt_generic(state, dst, src, bytes, nrounds);
  44. }
  45. void hchacha_block_arch(const u32 *state, u32 *stream, int nrounds)
  46. {
  47. hchacha_block_generic(state, stream, nrounds);
  48. }
  49. EXPORT_SYMBOL(hchacha_block_arch);
  50. void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv)
  51. {
  52. chacha_init_generic(state, key, iv);
  53. }
  54. EXPORT_SYMBOL(chacha_init_arch);
  55. void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src, unsigned int bytes,
  56. int nrounds)
  57. {
  58. if (!static_branch_likely(&have_p10) || bytes <= CHACHA_BLOCK_SIZE ||
  59. !crypto_simd_usable())
  60. return chacha_crypt_generic(state, dst, src, bytes, nrounds);
  61. do {
  62. unsigned int todo = min_t(unsigned int, bytes, SZ_4K);
  63. vsx_begin();
  64. chacha_p10_do_8x(state, dst, src, todo, nrounds);
  65. vsx_end();
  66. bytes -= todo;
  67. src += todo;
  68. dst += todo;
  69. } while (bytes);
  70. }
  71. EXPORT_SYMBOL(chacha_crypt_arch);
  72. static int chacha_p10_stream_xor(struct skcipher_request *req,
  73. const struct chacha_ctx *ctx, const u8 *iv)
  74. {
  75. struct skcipher_walk walk;
  76. u32 state[16];
  77. int err;
  78. err = skcipher_walk_virt(&walk, req, false);
  79. if (err)
  80. return err;
  81. chacha_init_generic(state, ctx->key, iv);
  82. while (walk.nbytes > 0) {
  83. unsigned int nbytes = walk.nbytes;
  84. if (nbytes < walk.total)
  85. nbytes = rounddown(nbytes, walk.stride);
  86. if (!crypto_simd_usable()) {
  87. chacha_crypt_generic(state, walk.dst.virt.addr,
  88. walk.src.virt.addr, nbytes,
  89. ctx->nrounds);
  90. } else {
  91. vsx_begin();
  92. chacha_p10_do_8x(state, walk.dst.virt.addr,
  93. walk.src.virt.addr, nbytes, ctx->nrounds);
  94. vsx_end();
  95. }
  96. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  97. if (err)
  98. break;
  99. }
  100. return err;
  101. }
  102. static int chacha_p10(struct skcipher_request *req)
  103. {
  104. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  105. struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
  106. return chacha_p10_stream_xor(req, ctx, req->iv);
  107. }
  108. static int xchacha_p10(struct skcipher_request *req)
  109. {
  110. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  111. struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
  112. struct chacha_ctx subctx;
  113. u32 state[16];
  114. u8 real_iv[16];
  115. chacha_init_generic(state, ctx->key, req->iv);
  116. hchacha_block_arch(state, subctx.key, ctx->nrounds);
  117. subctx.nrounds = ctx->nrounds;
  118. memcpy(&real_iv[0], req->iv + 24, 8);
  119. memcpy(&real_iv[8], req->iv + 16, 8);
  120. return chacha_p10_stream_xor(req, &subctx, real_iv);
  121. }
  122. static struct skcipher_alg algs[] = {
  123. {
  124. .base.cra_name = "chacha20",
  125. .base.cra_driver_name = "chacha20-p10",
  126. .base.cra_priority = 300,
  127. .base.cra_blocksize = 1,
  128. .base.cra_ctxsize = sizeof(struct chacha_ctx),
  129. .base.cra_module = THIS_MODULE,
  130. .min_keysize = CHACHA_KEY_SIZE,
  131. .max_keysize = CHACHA_KEY_SIZE,
  132. .ivsize = CHACHA_IV_SIZE,
  133. .chunksize = CHACHA_BLOCK_SIZE,
  134. .setkey = chacha20_setkey,
  135. .encrypt = chacha_p10,
  136. .decrypt = chacha_p10,
  137. }, {
  138. .base.cra_name = "xchacha20",
  139. .base.cra_driver_name = "xchacha20-p10",
  140. .base.cra_priority = 300,
  141. .base.cra_blocksize = 1,
  142. .base.cra_ctxsize = sizeof(struct chacha_ctx),
  143. .base.cra_module = THIS_MODULE,
  144. .min_keysize = CHACHA_KEY_SIZE,
  145. .max_keysize = CHACHA_KEY_SIZE,
  146. .ivsize = XCHACHA_IV_SIZE,
  147. .chunksize = CHACHA_BLOCK_SIZE,
  148. .setkey = chacha20_setkey,
  149. .encrypt = xchacha_p10,
  150. .decrypt = xchacha_p10,
  151. }, {
  152. .base.cra_name = "xchacha12",
  153. .base.cra_driver_name = "xchacha12-p10",
  154. .base.cra_priority = 300,
  155. .base.cra_blocksize = 1,
  156. .base.cra_ctxsize = sizeof(struct chacha_ctx),
  157. .base.cra_module = THIS_MODULE,
  158. .min_keysize = CHACHA_KEY_SIZE,
  159. .max_keysize = CHACHA_KEY_SIZE,
  160. .ivsize = XCHACHA_IV_SIZE,
  161. .chunksize = CHACHA_BLOCK_SIZE,
  162. .setkey = chacha12_setkey,
  163. .encrypt = xchacha_p10,
  164. .decrypt = xchacha_p10,
  165. }
  166. };
  167. static int __init chacha_p10_init(void)
  168. {
  169. if (!cpu_has_feature(CPU_FTR_ARCH_31))
  170. return 0;
  171. static_branch_enable(&have_p10);
  172. return crypto_register_skciphers(algs, ARRAY_SIZE(algs));
  173. }
  174. static void __exit chacha_p10_exit(void)
  175. {
  176. if (!static_branch_likely(&have_p10))
  177. return;
  178. crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
  179. }
  180. module_init(chacha_p10_init);
  181. module_exit(chacha_p10_exit);
  182. MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (P10 accelerated)");
  183. MODULE_AUTHOR("Danny Tsen <dtsen@linux.ibm.com>");
  184. MODULE_LICENSE("GPL v2");
  185. MODULE_ALIAS_CRYPTO("chacha20");
  186. MODULE_ALIAS_CRYPTO("chacha20-p10");
  187. MODULE_ALIAS_CRYPTO("xchacha20");
  188. MODULE_ALIAS_CRYPTO("xchacha20-p10");
  189. MODULE_ALIAS_CRYPTO("xchacha12");
  190. MODULE_ALIAS_CRYPTO("xchacha12-p10");