chacha20_glue.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <linux/crypto.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 blkcipher_desc *desc, struct scatterlist *dst,
  61. struct scatterlist *src, unsigned int nbytes)
  62. {
  63. u32 *state, state_buf[16 + (CHACHA20_STATE_ALIGN / sizeof(u32)) - 1];
  64. struct blkcipher_walk walk;
  65. int err;
  66. if (nbytes <= CHACHA20_BLOCK_SIZE || !may_use_simd())
  67. return crypto_chacha20_crypt(desc, dst, src, nbytes);
  68. state = (u32 *)roundup((uintptr_t)state_buf, CHACHA20_STATE_ALIGN);
  69. blkcipher_walk_init(&walk, dst, src, nbytes);
  70. err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE);
  71. crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv);
  72. kernel_fpu_begin();
  73. while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
  74. chacha20_dosimd(state, walk.dst.virt.addr, walk.src.virt.addr,
  75. rounddown(walk.nbytes, CHACHA20_BLOCK_SIZE));
  76. err = blkcipher_walk_done(desc, &walk,
  77. walk.nbytes % CHACHA20_BLOCK_SIZE);
  78. }
  79. if (walk.nbytes) {
  80. chacha20_dosimd(state, walk.dst.virt.addr, walk.src.virt.addr,
  81. walk.nbytes);
  82. err = blkcipher_walk_done(desc, &walk, 0);
  83. }
  84. kernel_fpu_end();
  85. return err;
  86. }
  87. static struct crypto_alg alg = {
  88. .cra_name = "chacha20",
  89. .cra_driver_name = "chacha20-simd",
  90. .cra_priority = 300,
  91. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  92. .cra_blocksize = 1,
  93. .cra_type = &crypto_blkcipher_type,
  94. .cra_ctxsize = sizeof(struct chacha20_ctx),
  95. .cra_alignmask = sizeof(u32) - 1,
  96. .cra_module = THIS_MODULE,
  97. .cra_u = {
  98. .blkcipher = {
  99. .min_keysize = CHACHA20_KEY_SIZE,
  100. .max_keysize = CHACHA20_KEY_SIZE,
  101. .ivsize = CHACHA20_IV_SIZE,
  102. .geniv = "seqiv",
  103. .setkey = crypto_chacha20_setkey,
  104. .encrypt = chacha20_simd,
  105. .decrypt = chacha20_simd,
  106. },
  107. },
  108. };
  109. static int __init chacha20_simd_mod_init(void)
  110. {
  111. if (!boot_cpu_has(X86_FEATURE_SSSE3))
  112. return -ENODEV;
  113. #ifdef CONFIG_AS_AVX2
  114. chacha20_use_avx2 = boot_cpu_has(X86_FEATURE_AVX) &&
  115. boot_cpu_has(X86_FEATURE_AVX2) &&
  116. cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL);
  117. #endif
  118. return crypto_register_alg(&alg);
  119. }
  120. static void __exit chacha20_simd_mod_fini(void)
  121. {
  122. crypto_unregister_alg(&alg);
  123. }
  124. module_init(chacha20_simd_mod_init);
  125. module_exit(chacha20_simd_mod_fini);
  126. MODULE_LICENSE("GPL");
  127. MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
  128. MODULE_DESCRIPTION("chacha20 cipher algorithm, SIMD accelerated");
  129. MODULE_ALIAS_CRYPTO("chacha20");
  130. MODULE_ALIAS_CRYPTO("chacha20-simd");