chacha20-neon-glue.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * ChaCha20 256-bit cipher algorithm, RFC7539, arm64 NEON functions
  3. *
  4. * Copyright (C) 2016 - 2017 Linaro, Ltd. <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Based on:
  11. * ChaCha20 256-bit cipher algorithm, RFC7539, SIMD glue code
  12. *
  13. * Copyright (C) 2015 Martin Willi
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. */
  20. #include <crypto/algapi.h>
  21. #include <crypto/chacha.h>
  22. #include <crypto/internal/skcipher.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <asm/hwcap.h>
  26. #include <asm/neon.h>
  27. #include <asm/simd.h>
  28. asmlinkage void chacha20_block_xor_neon(u32 *state, u8 *dst, const u8 *src);
  29. asmlinkage void chacha20_4block_xor_neon(u32 *state, u8 *dst, const u8 *src);
  30. static void chacha20_doneon(u32 *state, u8 *dst, const u8 *src,
  31. unsigned int bytes)
  32. {
  33. u8 buf[CHACHA_BLOCK_SIZE];
  34. while (bytes >= CHACHA_BLOCK_SIZE * 4) {
  35. chacha20_4block_xor_neon(state, dst, src);
  36. bytes -= CHACHA_BLOCK_SIZE * 4;
  37. src += CHACHA_BLOCK_SIZE * 4;
  38. dst += CHACHA_BLOCK_SIZE * 4;
  39. state[12] += 4;
  40. }
  41. while (bytes >= CHACHA_BLOCK_SIZE) {
  42. chacha20_block_xor_neon(state, dst, src);
  43. bytes -= CHACHA_BLOCK_SIZE;
  44. src += CHACHA_BLOCK_SIZE;
  45. dst += CHACHA_BLOCK_SIZE;
  46. state[12]++;
  47. }
  48. if (bytes) {
  49. memcpy(buf, src, bytes);
  50. chacha20_block_xor_neon(state, buf, buf);
  51. memcpy(dst, buf, bytes);
  52. }
  53. }
  54. static int chacha20_neon(struct skcipher_request *req)
  55. {
  56. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  57. struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
  58. struct skcipher_walk walk;
  59. u32 state[16];
  60. int err;
  61. if (!may_use_simd() || req->cryptlen <= CHACHA_BLOCK_SIZE)
  62. return crypto_chacha_crypt(req);
  63. err = skcipher_walk_virt(&walk, req, true);
  64. crypto_chacha_init(state, ctx, walk.iv);
  65. kernel_neon_begin();
  66. while (walk.nbytes > 0) {
  67. unsigned int nbytes = walk.nbytes;
  68. if (nbytes < walk.total)
  69. nbytes = round_down(nbytes, walk.stride);
  70. chacha20_doneon(state, walk.dst.virt.addr, walk.src.virt.addr,
  71. nbytes);
  72. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  73. }
  74. kernel_neon_end();
  75. return err;
  76. }
  77. static struct skcipher_alg alg = {
  78. .base.cra_name = "chacha20",
  79. .base.cra_driver_name = "chacha20-neon",
  80. .base.cra_priority = 300,
  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 = CHACHA_IV_SIZE,
  87. .chunksize = CHACHA_BLOCK_SIZE,
  88. .walksize = 4 * CHACHA_BLOCK_SIZE,
  89. .setkey = crypto_chacha20_setkey,
  90. .encrypt = chacha20_neon,
  91. .decrypt = chacha20_neon,
  92. };
  93. static int __init chacha20_simd_mod_init(void)
  94. {
  95. if (!(elf_hwcap & HWCAP_ASIMD))
  96. return -ENODEV;
  97. return crypto_register_skcipher(&alg);
  98. }
  99. static void __exit chacha20_simd_mod_fini(void)
  100. {
  101. crypto_unregister_skcipher(&alg);
  102. }
  103. module_init(chacha20_simd_mod_init);
  104. module_exit(chacha20_simd_mod_fini);
  105. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  106. MODULE_LICENSE("GPL v2");
  107. MODULE_ALIAS_CRYPTO("chacha20");