chacha-glue.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MIPS accelerated ChaCha and XChaCha stream ciphers,
  4. * including ChaCha20 (RFC7539)
  5. *
  6. * Copyright (C) 2019 Linaro, Ltd. <[email protected]>
  7. */
  8. #include <asm/byteorder.h>
  9. #include <crypto/algapi.h>
  10. #include <crypto/internal/chacha.h>
  11. #include <crypto/internal/skcipher.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. asmlinkage void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src,
  15. unsigned int bytes, int nrounds);
  16. EXPORT_SYMBOL(chacha_crypt_arch);
  17. asmlinkage void hchacha_block_arch(const u32 *state, u32 *stream, int nrounds);
  18. EXPORT_SYMBOL(hchacha_block_arch);
  19. void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv)
  20. {
  21. chacha_init_generic(state, key, iv);
  22. }
  23. EXPORT_SYMBOL(chacha_init_arch);
  24. static int chacha_mips_stream_xor(struct skcipher_request *req,
  25. const struct chacha_ctx *ctx, const u8 *iv)
  26. {
  27. struct skcipher_walk walk;
  28. u32 state[16];
  29. int err;
  30. err = skcipher_walk_virt(&walk, req, false);
  31. chacha_init_generic(state, ctx->key, iv);
  32. while (walk.nbytes > 0) {
  33. unsigned int nbytes = walk.nbytes;
  34. if (nbytes < walk.total)
  35. nbytes = round_down(nbytes, walk.stride);
  36. chacha_crypt(state, walk.dst.virt.addr, walk.src.virt.addr,
  37. nbytes, ctx->nrounds);
  38. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  39. }
  40. return err;
  41. }
  42. static int chacha_mips(struct skcipher_request *req)
  43. {
  44. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  45. struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
  46. return chacha_mips_stream_xor(req, ctx, req->iv);
  47. }
  48. static int xchacha_mips(struct skcipher_request *req)
  49. {
  50. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  51. struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
  52. struct chacha_ctx subctx;
  53. u32 state[16];
  54. u8 real_iv[16];
  55. chacha_init_generic(state, ctx->key, req->iv);
  56. hchacha_block(state, subctx.key, ctx->nrounds);
  57. subctx.nrounds = ctx->nrounds;
  58. memcpy(&real_iv[0], req->iv + 24, 8);
  59. memcpy(&real_iv[8], req->iv + 16, 8);
  60. return chacha_mips_stream_xor(req, &subctx, real_iv);
  61. }
  62. static struct skcipher_alg algs[] = {
  63. {
  64. .base.cra_name = "chacha20",
  65. .base.cra_driver_name = "chacha20-mips",
  66. .base.cra_priority = 200,
  67. .base.cra_blocksize = 1,
  68. .base.cra_ctxsize = sizeof(struct chacha_ctx),
  69. .base.cra_module = THIS_MODULE,
  70. .min_keysize = CHACHA_KEY_SIZE,
  71. .max_keysize = CHACHA_KEY_SIZE,
  72. .ivsize = CHACHA_IV_SIZE,
  73. .chunksize = CHACHA_BLOCK_SIZE,
  74. .setkey = chacha20_setkey,
  75. .encrypt = chacha_mips,
  76. .decrypt = chacha_mips,
  77. }, {
  78. .base.cra_name = "xchacha20",
  79. .base.cra_driver_name = "xchacha20-mips",
  80. .base.cra_priority = 200,
  81. .base.cra_blocksize = 1,
  82. .base.cra_ctxsize = sizeof(struct chacha_ctx),
  83. .base.cra_module = THIS_MODULE,
  84. .min_keysize = CHACHA_KEY_SIZE,
  85. .max_keysize = CHACHA_KEY_SIZE,
  86. .ivsize = XCHACHA_IV_SIZE,
  87. .chunksize = CHACHA_BLOCK_SIZE,
  88. .setkey = chacha20_setkey,
  89. .encrypt = xchacha_mips,
  90. .decrypt = xchacha_mips,
  91. }, {
  92. .base.cra_name = "xchacha12",
  93. .base.cra_driver_name = "xchacha12-mips",
  94. .base.cra_priority = 200,
  95. .base.cra_blocksize = 1,
  96. .base.cra_ctxsize = sizeof(struct chacha_ctx),
  97. .base.cra_module = THIS_MODULE,
  98. .min_keysize = CHACHA_KEY_SIZE,
  99. .max_keysize = CHACHA_KEY_SIZE,
  100. .ivsize = XCHACHA_IV_SIZE,
  101. .chunksize = CHACHA_BLOCK_SIZE,
  102. .setkey = chacha12_setkey,
  103. .encrypt = xchacha_mips,
  104. .decrypt = xchacha_mips,
  105. }
  106. };
  107. static int __init chacha_simd_mod_init(void)
  108. {
  109. return IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) ?
  110. crypto_register_skciphers(algs, ARRAY_SIZE(algs)) : 0;
  111. }
  112. static void __exit chacha_simd_mod_fini(void)
  113. {
  114. if (IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER))
  115. crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
  116. }
  117. module_init(chacha_simd_mod_init);
  118. module_exit(chacha_simd_mod_fini);
  119. MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (MIPS accelerated)");
  120. MODULE_AUTHOR("Ard Biesheuvel <[email protected]>");
  121. MODULE_LICENSE("GPL v2");
  122. MODULE_ALIAS_CRYPTO("chacha20");
  123. MODULE_ALIAS_CRYPTO("chacha20-mips");
  124. MODULE_ALIAS_CRYPTO("xchacha20");
  125. MODULE_ALIAS_CRYPTO("xchacha20-mips");
  126. MODULE_ALIAS_CRYPTO("xchacha12");
  127. MODULE_ALIAS_CRYPTO("xchacha12-mips");