chacha-glue.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * s390 ChaCha stream cipher.
  4. *
  5. * Copyright IBM Corp. 2021
  6. */
  7. #define KMSG_COMPONENT "chacha_s390"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <crypto/internal/chacha.h>
  10. #include <crypto/internal/skcipher.h>
  11. #include <crypto/algapi.h>
  12. #include <linux/cpufeature.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/sizes.h>
  16. #include <asm/fpu/api.h>
  17. #include "chacha-s390.h"
  18. static void chacha20_crypt_s390(u32 *state, u8 *dst, const u8 *src,
  19. unsigned int nbytes, const u32 *key,
  20. u32 *counter)
  21. {
  22. struct kernel_fpu vxstate;
  23. kernel_fpu_begin(&vxstate, KERNEL_VXR);
  24. chacha20_vx(dst, src, nbytes, key, counter);
  25. kernel_fpu_end(&vxstate, KERNEL_VXR);
  26. *counter += round_up(nbytes, CHACHA_BLOCK_SIZE) / CHACHA_BLOCK_SIZE;
  27. }
  28. static int chacha20_s390(struct skcipher_request *req)
  29. {
  30. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  31. struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
  32. u32 state[CHACHA_STATE_WORDS] __aligned(16);
  33. struct skcipher_walk walk;
  34. unsigned int nbytes;
  35. int rc;
  36. rc = skcipher_walk_virt(&walk, req, false);
  37. chacha_init_generic(state, ctx->key, req->iv);
  38. while (walk.nbytes > 0) {
  39. nbytes = walk.nbytes;
  40. if (nbytes < walk.total)
  41. nbytes = round_down(nbytes, walk.stride);
  42. if (nbytes <= CHACHA_BLOCK_SIZE) {
  43. chacha_crypt_generic(state, walk.dst.virt.addr,
  44. walk.src.virt.addr, nbytes,
  45. ctx->nrounds);
  46. } else {
  47. chacha20_crypt_s390(state, walk.dst.virt.addr,
  48. walk.src.virt.addr, nbytes,
  49. &state[4], &state[12]);
  50. }
  51. rc = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  52. }
  53. return rc;
  54. }
  55. void hchacha_block_arch(const u32 *state, u32 *stream, int nrounds)
  56. {
  57. /* TODO: implement hchacha_block_arch() in assembly */
  58. hchacha_block_generic(state, stream, nrounds);
  59. }
  60. EXPORT_SYMBOL(hchacha_block_arch);
  61. void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv)
  62. {
  63. chacha_init_generic(state, key, iv);
  64. }
  65. EXPORT_SYMBOL(chacha_init_arch);
  66. void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src,
  67. unsigned int bytes, int nrounds)
  68. {
  69. /* s390 chacha20 implementation has 20 rounds hard-coded,
  70. * it cannot handle a block of data or less, but otherwise
  71. * it can handle data of arbitrary size
  72. */
  73. if (bytes <= CHACHA_BLOCK_SIZE || nrounds != 20 || !MACHINE_HAS_VX)
  74. chacha_crypt_generic(state, dst, src, bytes, nrounds);
  75. else
  76. chacha20_crypt_s390(state, dst, src, bytes,
  77. &state[4], &state[12]);
  78. }
  79. EXPORT_SYMBOL(chacha_crypt_arch);
  80. static struct skcipher_alg chacha_algs[] = {
  81. {
  82. .base.cra_name = "chacha20",
  83. .base.cra_driver_name = "chacha20-s390",
  84. .base.cra_priority = 900,
  85. .base.cra_blocksize = 1,
  86. .base.cra_ctxsize = sizeof(struct chacha_ctx),
  87. .base.cra_module = THIS_MODULE,
  88. .min_keysize = CHACHA_KEY_SIZE,
  89. .max_keysize = CHACHA_KEY_SIZE,
  90. .ivsize = CHACHA_IV_SIZE,
  91. .chunksize = CHACHA_BLOCK_SIZE,
  92. .setkey = chacha20_setkey,
  93. .encrypt = chacha20_s390,
  94. .decrypt = chacha20_s390,
  95. }
  96. };
  97. static int __init chacha_mod_init(void)
  98. {
  99. return IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) ?
  100. crypto_register_skciphers(chacha_algs, ARRAY_SIZE(chacha_algs)) : 0;
  101. }
  102. static void __exit chacha_mod_fini(void)
  103. {
  104. if (IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER))
  105. crypto_unregister_skciphers(chacha_algs, ARRAY_SIZE(chacha_algs));
  106. }
  107. module_cpu_feature_match(S390_CPU_FEATURE_VXRS, chacha_mod_init);
  108. module_exit(chacha_mod_fini);
  109. MODULE_DESCRIPTION("ChaCha20 stream cipher");
  110. MODULE_LICENSE("GPL v2");
  111. MODULE_ALIAS_CRYPTO("chacha20");