chacha20_glue.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * ChaCha20 256-bit cipher algorithm, RFC7539, SIMD glue code
  3. *
  4. * Copyright (C) 2015 Martin Willi
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <crypto/algapi.h>
  12. #include <crypto/chacha20.h>
  13. #include <crypto/internal/skcipher.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <asm/fpu/api.h>
  17. #include <asm/simd.h>
  18. #define CHACHA20_STATE_ALIGN 16
  19. asmlinkage void chacha20_block_xor_ssse3(u32 *state, u8 *dst, const u8 *src);
  20. asmlinkage void chacha20_4block_xor_ssse3(u32 *state, u8 *dst, const u8 *src);
  21. #ifdef CONFIG_AS_AVX2
  22. asmlinkage void chacha20_8block_xor_avx2(u32 *state, u8 *dst, const u8 *src);
  23. static bool chacha20_use_avx2;
  24. #endif
  25. static void chacha20_dosimd(u32 *state, u8 *dst, const u8 *src,
  26. unsigned int bytes)
  27. {
  28. u8 buf[CHACHA20_BLOCK_SIZE];
  29. #ifdef CONFIG_AS_AVX2
  30. if (chacha20_use_avx2) {
  31. while (bytes >= CHACHA20_BLOCK_SIZE * 8) {
  32. chacha20_8block_xor_avx2(state, dst, src);
  33. bytes -= CHACHA20_BLOCK_SIZE * 8;
  34. src += CHACHA20_BLOCK_SIZE * 8;
  35. dst += CHACHA20_BLOCK_SIZE * 8;
  36. state[12] += 8;
  37. }
  38. }
  39. #endif
  40. while (bytes >= CHACHA20_BLOCK_SIZE * 4) {
  41. chacha20_4block_xor_ssse3(state, dst, src);
  42. bytes -= CHACHA20_BLOCK_SIZE * 4;
  43. src += CHACHA20_BLOCK_SIZE * 4;
  44. dst += CHACHA20_BLOCK_SIZE * 4;
  45. state[12] += 4;
  46. }
  47. while (bytes >= CHACHA20_BLOCK_SIZE) {
  48. chacha20_block_xor_ssse3(state, dst, src);
  49. bytes -= CHACHA20_BLOCK_SIZE;
  50. src += CHACHA20_BLOCK_SIZE;
  51. dst += CHACHA20_BLOCK_SIZE;
  52. state[12]++;
  53. }
  54. if (bytes) {
  55. memcpy(buf, src, bytes);
  56. chacha20_block_xor_ssse3(state, buf, buf);
  57. memcpy(dst, buf, bytes);
  58. }
  59. }
  60. static int chacha20_simd(struct skcipher_request *req)
  61. {
  62. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  63. struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
  64. u32 *state, state_buf[16 + 2] __aligned(8);
  65. struct skcipher_walk walk;
  66. int err;
  67. BUILD_BUG_ON(CHACHA20_STATE_ALIGN != 16);
  68. state = PTR_ALIGN(state_buf + 0, CHACHA20_STATE_ALIGN);
  69. if (req->cryptlen <= CHACHA20_BLOCK_SIZE || !may_use_simd())
  70. return crypto_chacha20_crypt(req);
  71. err = skcipher_walk_virt(&walk, req, true);
  72. crypto_chacha20_init(state, ctx, walk.iv);
  73. kernel_fpu_begin();
  74. while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
  75. chacha20_dosimd(state, walk.dst.virt.addr, walk.src.virt.addr,
  76. rounddown(walk.nbytes, CHACHA20_BLOCK_SIZE));
  77. err = skcipher_walk_done(&walk,
  78. walk.nbytes % CHACHA20_BLOCK_SIZE);
  79. }
  80. if (walk.nbytes) {
  81. chacha20_dosimd(state, walk.dst.virt.addr, walk.src.virt.addr,
  82. walk.nbytes);
  83. err = skcipher_walk_done(&walk, 0);
  84. }
  85. kernel_fpu_end();
  86. return err;
  87. }
  88. static struct skcipher_alg alg = {
  89. .base.cra_name = "chacha20",
  90. .base.cra_driver_name = "chacha20-simd",
  91. .base.cra_priority = 300,
  92. .base.cra_blocksize = 1,
  93. .base.cra_ctxsize = sizeof(struct chacha20_ctx),
  94. .base.cra_module = THIS_MODULE,
  95. .min_keysize = CHACHA20_KEY_SIZE,
  96. .max_keysize = CHACHA20_KEY_SIZE,
  97. .ivsize = CHACHA20_IV_SIZE,
  98. .chunksize = CHACHA20_BLOCK_SIZE,
  99. .setkey = crypto_chacha20_setkey,
  100. .encrypt = chacha20_simd,
  101. .decrypt = chacha20_simd,
  102. };
  103. static int __init chacha20_simd_mod_init(void)
  104. {
  105. if (!boot_cpu_has(X86_FEATURE_SSSE3))
  106. return -ENODEV;
  107. #ifdef CONFIG_AS_AVX2
  108. chacha20_use_avx2 = boot_cpu_has(X86_FEATURE_AVX) &&
  109. boot_cpu_has(X86_FEATURE_AVX2) &&
  110. cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL);
  111. #endif
  112. return crypto_register_skcipher(&alg);
  113. }
  114. static void __exit chacha20_simd_mod_fini(void)
  115. {
  116. crypto_unregister_skcipher(&alg);
  117. }
  118. module_init(chacha20_simd_mod_init);
  119. module_exit(chacha20_simd_mod_fini);
  120. MODULE_LICENSE("GPL");
  121. MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
  122. MODULE_DESCRIPTION("chacha20 cipher algorithm, SIMD accelerated");
  123. MODULE_ALIAS_CRYPTO("chacha20");
  124. MODULE_ALIAS_CRYPTO("chacha20-simd");