ghash-clmulni-intel_asm.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Accelerated GHASH implementation with Intel PCLMULQDQ-NI
  3. * instructions. This file contains accelerated part of ghash
  4. * implementation. More information about PCLMULQDQ can be found at:
  5. *
  6. * http://software.intel.com/en-us/articles/carry-less-multiplication-and-its-usage-for-computing-the-gcm-mode/
  7. *
  8. * Copyright (c) 2009 Intel Corp.
  9. * Author: Huang Ying <ying.huang@intel.com>
  10. * Vinodh Gopal
  11. * Erdinc Ozturk
  12. * Deniz Karakoyunlu
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License version 2 as published
  16. * by the Free Software Foundation.
  17. */
  18. #include <linux/linkage.h>
  19. #include <asm/inst.h>
  20. .data
  21. .align 16
  22. .Lbswap_mask:
  23. .octa 0x000102030405060708090a0b0c0d0e0f
  24. #define DATA %xmm0
  25. #define SHASH %xmm1
  26. #define T1 %xmm2
  27. #define T2 %xmm3
  28. #define T3 %xmm4
  29. #define BSWAP %xmm5
  30. #define IN1 %xmm6
  31. .text
  32. /*
  33. * __clmul_gf128mul_ble: internal ABI
  34. * input:
  35. * DATA: operand1
  36. * SHASH: operand2, hash_key << 1 mod poly
  37. * output:
  38. * DATA: operand1 * operand2 mod poly
  39. * changed:
  40. * T1
  41. * T2
  42. * T3
  43. */
  44. __clmul_gf128mul_ble:
  45. movaps DATA, T1
  46. pshufd $0b01001110, DATA, T2
  47. pshufd $0b01001110, SHASH, T3
  48. pxor DATA, T2
  49. pxor SHASH, T3
  50. PCLMULQDQ 0x00 SHASH DATA # DATA = a0 * b0
  51. PCLMULQDQ 0x11 SHASH T1 # T1 = a1 * b1
  52. PCLMULQDQ 0x00 T3 T2 # T2 = (a1 + a0) * (b1 + b0)
  53. pxor DATA, T2
  54. pxor T1, T2 # T2 = a0 * b1 + a1 * b0
  55. movaps T2, T3
  56. pslldq $8, T3
  57. psrldq $8, T2
  58. pxor T3, DATA
  59. pxor T2, T1 # <T1:DATA> is result of
  60. # carry-less multiplication
  61. # first phase of the reduction
  62. movaps DATA, T3
  63. psllq $1, T3
  64. pxor DATA, T3
  65. psllq $5, T3
  66. pxor DATA, T3
  67. psllq $57, T3
  68. movaps T3, T2
  69. pslldq $8, T2
  70. psrldq $8, T3
  71. pxor T2, DATA
  72. pxor T3, T1
  73. # second phase of the reduction
  74. movaps DATA, T2
  75. psrlq $5, T2
  76. pxor DATA, T2
  77. psrlq $1, T2
  78. pxor DATA, T2
  79. psrlq $1, T2
  80. pxor T2, T1
  81. pxor T1, DATA
  82. ret
  83. /* void clmul_ghash_mul(char *dst, const be128 *shash) */
  84. ENTRY(clmul_ghash_mul)
  85. movups (%rdi), DATA
  86. movups (%rsi), SHASH
  87. movaps .Lbswap_mask, BSWAP
  88. PSHUFB_XMM BSWAP DATA
  89. call __clmul_gf128mul_ble
  90. PSHUFB_XMM BSWAP DATA
  91. movups DATA, (%rdi)
  92. ret
  93. /*
  94. * void clmul_ghash_update(char *dst, const char *src, unsigned int srclen,
  95. * const be128 *shash);
  96. */
  97. ENTRY(clmul_ghash_update)
  98. cmp $16, %rdx
  99. jb .Lupdate_just_ret # check length
  100. movaps .Lbswap_mask, BSWAP
  101. movups (%rdi), DATA
  102. movups (%rcx), SHASH
  103. PSHUFB_XMM BSWAP DATA
  104. .align 4
  105. .Lupdate_loop:
  106. movups (%rsi), IN1
  107. PSHUFB_XMM BSWAP IN1
  108. pxor IN1, DATA
  109. call __clmul_gf128mul_ble
  110. sub $16, %rdx
  111. add $16, %rsi
  112. cmp $16, %rdx
  113. jge .Lupdate_loop
  114. PSHUFB_XMM BSWAP DATA
  115. movups DATA, (%rdi)
  116. .Lupdate_just_ret:
  117. ret