chacha20-neon-glue.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/chacha20.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[CHACHA20_BLOCK_SIZE];
  34. while (bytes >= CHACHA20_BLOCK_SIZE * 4) {
  35. kernel_neon_begin();
  36. chacha20_4block_xor_neon(state, dst, src);
  37. kernel_neon_end();
  38. bytes -= CHACHA20_BLOCK_SIZE * 4;
  39. src += CHACHA20_BLOCK_SIZE * 4;
  40. dst += CHACHA20_BLOCK_SIZE * 4;
  41. state[12] += 4;
  42. }
  43. if (!bytes)
  44. return;
  45. kernel_neon_begin();
  46. while (bytes >= CHACHA20_BLOCK_SIZE) {
  47. chacha20_block_xor_neon(state, dst, src);
  48. bytes -= CHACHA20_BLOCK_SIZE;
  49. src += CHACHA20_BLOCK_SIZE;
  50. dst += CHACHA20_BLOCK_SIZE;
  51. state[12]++;
  52. }
  53. if (bytes) {
  54. memcpy(buf, src, bytes);
  55. chacha20_block_xor_neon(state, buf, buf);
  56. memcpy(dst, buf, bytes);
  57. }
  58. kernel_neon_end();
  59. }
  60. static int chacha20_neon(struct skcipher_request *req)
  61. {
  62. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  63. struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
  64. struct skcipher_walk walk;
  65. u32 state[16];
  66. int err;
  67. if (!may_use_simd() || req->cryptlen <= CHACHA20_BLOCK_SIZE)
  68. return crypto_chacha20_crypt(req);
  69. err = skcipher_walk_virt(&walk, req, false);
  70. crypto_chacha20_init(state, ctx, walk.iv);
  71. while (walk.nbytes > 0) {
  72. unsigned int nbytes = walk.nbytes;
  73. if (nbytes < walk.total)
  74. nbytes = round_down(nbytes, walk.stride);
  75. chacha20_doneon(state, walk.dst.virt.addr, walk.src.virt.addr,
  76. nbytes);
  77. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  78. }
  79. return err;
  80. }
  81. static struct skcipher_alg alg = {
  82. .base.cra_name = "chacha20",
  83. .base.cra_driver_name = "chacha20-neon",
  84. .base.cra_priority = 300,
  85. .base.cra_blocksize = 1,
  86. .base.cra_ctxsize = sizeof(struct chacha20_ctx),
  87. .base.cra_module = THIS_MODULE,
  88. .min_keysize = CHACHA20_KEY_SIZE,
  89. .max_keysize = CHACHA20_KEY_SIZE,
  90. .ivsize = CHACHA20_IV_SIZE,
  91. .chunksize = CHACHA20_BLOCK_SIZE,
  92. .walksize = 4 * CHACHA20_BLOCK_SIZE,
  93. .setkey = crypto_chacha20_setkey,
  94. .encrypt = chacha20_neon,
  95. .decrypt = chacha20_neon,
  96. };
  97. static int __init chacha20_simd_mod_init(void)
  98. {
  99. if (!(elf_hwcap & HWCAP_ASIMD))
  100. return -ENODEV;
  101. return crypto_register_skcipher(&alg);
  102. }
  103. static void __exit chacha20_simd_mod_fini(void)
  104. {
  105. crypto_unregister_skcipher(&alg);
  106. }
  107. module_init(chacha20_simd_mod_init);
  108. module_exit(chacha20_simd_mod_fini);
  109. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  110. MODULE_LICENSE("GPL v2");
  111. MODULE_ALIAS_CRYPTO("chacha20");