chacha20_glue.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 (!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. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  72. crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), 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 = blkcipher_walk_done(desc, &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 = blkcipher_walk_done(desc, &walk, 0);
  84. }
  85. kernel_fpu_end();
  86. return err;
  87. }
  88. static struct crypto_alg alg = {
  89. .cra_name = "chacha20",
  90. .cra_driver_name = "chacha20-simd",
  91. .cra_priority = 300,
  92. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  93. .cra_blocksize = 1,
  94. .cra_type = &crypto_blkcipher_type,
  95. .cra_ctxsize = sizeof(struct chacha20_ctx),
  96. .cra_alignmask = sizeof(u32) - 1,
  97. .cra_module = THIS_MODULE,
  98. .cra_u = {
  99. .blkcipher = {
  100. .min_keysize = CHACHA20_KEY_SIZE,
  101. .max_keysize = CHACHA20_KEY_SIZE,
  102. .ivsize = CHACHA20_IV_SIZE,
  103. .geniv = "seqiv",
  104. .setkey = crypto_chacha20_setkey,
  105. .encrypt = chacha20_simd,
  106. .decrypt = chacha20_simd,
  107. },
  108. },
  109. };
  110. static int __init chacha20_simd_mod_init(void)
  111. {
  112. if (!boot_cpu_has(X86_FEATURE_SSSE3))
  113. return -ENODEV;
  114. #ifdef CONFIG_AS_AVX2
  115. chacha20_use_avx2 = cpu_has_avx && cpu_has_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");