ghash-clmulni-intel_asm.S 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. .align 16
  21. .Lbswap_mask:
  22. .octa 0x000102030405060708090a0b0c0d0e0f
  23. .Lpoly:
  24. .octa 0xc2000000000000000000000000000001
  25. .Ltwo_one:
  26. .octa 0x00000001000000000000000000000001
  27. #define DATA %xmm0
  28. #define SHASH %xmm1
  29. #define T1 %xmm2
  30. #define T2 %xmm3
  31. #define T3 %xmm4
  32. #define BSWAP %xmm5
  33. #define IN1 %xmm6
  34. .text
  35. /*
  36. * __clmul_gf128mul_ble: internal ABI
  37. * input:
  38. * DATA: operand1
  39. * SHASH: operand2, hash_key << 1 mod poly
  40. * output:
  41. * DATA: operand1 * operand2 mod poly
  42. * changed:
  43. * T1
  44. * T2
  45. * T3
  46. */
  47. __clmul_gf128mul_ble:
  48. movaps DATA, T1
  49. pshufd $0b01001110, DATA, T2
  50. pshufd $0b01001110, SHASH, T3
  51. pxor DATA, T2
  52. pxor SHASH, T3
  53. PCLMULQDQ 0x00 SHASH DATA # DATA = a0 * b0
  54. PCLMULQDQ 0x11 SHASH T1 # T1 = a1 * b1
  55. PCLMULQDQ 0x00 T3 T2 # T2 = (a1 + a0) * (b1 + b0)
  56. pxor DATA, T2
  57. pxor T1, T2 # T2 = a0 * b1 + a1 * b0
  58. movaps T2, T3
  59. pslldq $8, T3
  60. psrldq $8, T2
  61. pxor T3, DATA
  62. pxor T2, T1 # <T1:DATA> is result of
  63. # carry-less multiplication
  64. # first phase of the reduction
  65. movaps DATA, T3
  66. psllq $1, T3
  67. pxor DATA, T3
  68. psllq $5, T3
  69. pxor DATA, T3
  70. psllq $57, T3
  71. movaps T3, T2
  72. pslldq $8, T2
  73. psrldq $8, T3
  74. pxor T2, DATA
  75. pxor T3, T1
  76. # second phase of the reduction
  77. movaps DATA, T2
  78. psrlq $5, T2
  79. pxor DATA, T2
  80. psrlq $1, T2
  81. pxor DATA, T2
  82. psrlq $1, T2
  83. pxor T2, T1
  84. pxor T1, DATA
  85. ret
  86. /* void clmul_ghash_mul(char *dst, const be128 *shash) */
  87. ENTRY(clmul_ghash_mul)
  88. movups (%rdi), DATA
  89. movups (%rsi), SHASH
  90. movaps .Lbswap_mask, BSWAP
  91. PSHUFB_XMM BSWAP DATA
  92. call __clmul_gf128mul_ble
  93. PSHUFB_XMM BSWAP DATA
  94. movups DATA, (%rdi)
  95. ret
  96. /*
  97. * void clmul_ghash_update(char *dst, const char *src, unsigned int srclen,
  98. * const be128 *shash);
  99. */
  100. ENTRY(clmul_ghash_update)
  101. cmp $16, %rdx
  102. jb .Lupdate_just_ret # check length
  103. movaps .Lbswap_mask, BSWAP
  104. movups (%rdi), DATA
  105. movups (%rcx), SHASH
  106. PSHUFB_XMM BSWAP DATA
  107. .align 4
  108. .Lupdate_loop:
  109. movups (%rsi), IN1
  110. PSHUFB_XMM BSWAP IN1
  111. pxor IN1, DATA
  112. call __clmul_gf128mul_ble
  113. sub $16, %rdx
  114. add $16, %rsi
  115. cmp $16, %rdx
  116. jge .Lupdate_loop
  117. PSHUFB_XMM BSWAP DATA
  118. movups DATA, (%rdi)
  119. .Lupdate_just_ret:
  120. ret
  121. /*
  122. * void clmul_ghash_setkey(be128 *shash, const u8 *key);
  123. *
  124. * Calculate hash_key << 1 mod poly
  125. */
  126. ENTRY(clmul_ghash_setkey)
  127. movaps .Lbswap_mask, BSWAP
  128. movups (%rsi), %xmm0
  129. PSHUFB_XMM BSWAP %xmm0
  130. movaps %xmm0, %xmm1
  131. psllq $1, %xmm0
  132. psrlq $63, %xmm1
  133. movaps %xmm1, %xmm2
  134. pslldq $8, %xmm1
  135. psrldq $8, %xmm2
  136. por %xmm1, %xmm0
  137. # reduction
  138. pshufd $0b00100100, %xmm2, %xmm1
  139. pcmpeqd .Ltwo_one, %xmm1
  140. pand .Lpoly, %xmm1
  141. pxor %xmm1, %xmm0
  142. movups %xmm0, (%rdi)
  143. ret