ghash-clmulni-intel_asm.S 3.2 KB

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