ghash-clmulni-intel_asm.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/i387.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. .byte 0x66, 0x0f, 0x3a, 0x44, 0xc1, 0x00
  55. # pclmulqdq $0x11, SHASH, T1 # T1 = a1 * b1
  56. .byte 0x66, 0x0f, 0x3a, 0x44, 0xd1, 0x11
  57. # pclmulqdq $0x00, T3, T2 # T2 = (a1 + a0) * (b1 + b0)
  58. .byte 0x66, 0x0f, 0x3a, 0x44, 0xdc, 0x00
  59. pxor DATA, T2
  60. pxor T1, T2 # T2 = a0 * b1 + a1 * b0
  61. movaps T2, T3
  62. pslldq $8, T3
  63. psrldq $8, T2
  64. pxor T3, DATA
  65. pxor T2, T1 # <T1:DATA> is result of
  66. # carry-less multiplication
  67. # first phase of the reduction
  68. movaps DATA, T3
  69. psllq $1, T3
  70. pxor DATA, T3
  71. psllq $5, T3
  72. pxor DATA, T3
  73. psllq $57, T3
  74. movaps T3, T2
  75. pslldq $8, T2
  76. psrldq $8, T3
  77. pxor T2, DATA
  78. pxor T3, T1
  79. # second phase of the reduction
  80. movaps DATA, T2
  81. psrlq $5, T2
  82. pxor DATA, T2
  83. psrlq $1, T2
  84. pxor DATA, T2
  85. psrlq $1, T2
  86. pxor T2, T1
  87. pxor T1, DATA
  88. ret
  89. /* void clmul_ghash_mul(char *dst, const be128 *shash) */
  90. ENTRY(clmul_ghash_mul)
  91. movups (%rdi), DATA
  92. movups (%rsi), SHASH
  93. movaps .Lbswap_mask, BSWAP
  94. # pshufb BSWAP, DATA
  95. PSHUFB_XMM5_XMM0
  96. call __clmul_gf128mul_ble
  97. # pshufb BSWAP, DATA
  98. .byte 0x66, 0x0f, 0x38, 0x00, 0xc5
  99. movups DATA, (%rdi)
  100. ret
  101. /*
  102. * void clmul_ghash_update(char *dst, const char *src, unsigned int srclen,
  103. * const be128 *shash);
  104. */
  105. ENTRY(clmul_ghash_update)
  106. cmp $16, %rdx
  107. jb .Lupdate_just_ret # check length
  108. movaps .Lbswap_mask, BSWAP
  109. movups (%rdi), DATA
  110. movups (%rcx), SHASH
  111. # pshufb BSWAP, DATA
  112. PSHUFB_XMM5_XMM0
  113. .align 4
  114. .Lupdate_loop:
  115. movups (%rsi), IN1
  116. # pshufb BSWAP, IN1
  117. PSHUFB_XMM5_XMM6
  118. pxor IN1, DATA
  119. call __clmul_gf128mul_ble
  120. sub $16, %rdx
  121. add $16, %rsi
  122. cmp $16, %rdx
  123. jge .Lupdate_loop
  124. # pshufb BSWAP, DATA
  125. PSHUFB_XMM5_XMM0
  126. movups DATA, (%rdi)
  127. .Lupdate_just_ret:
  128. ret
  129. /*
  130. * void clmul_ghash_setkey(be128 *shash, const u8 *key);
  131. *
  132. * Calculate hash_key << 1 mod poly
  133. */
  134. ENTRY(clmul_ghash_setkey)
  135. movaps .Lbswap_mask, BSWAP
  136. movups (%rsi), %xmm0
  137. # pshufb BSWAP, %xmm0
  138. PSHUFB_XMM5_XMM0
  139. movaps %xmm0, %xmm1
  140. psllq $1, %xmm0
  141. psrlq $63, %xmm1
  142. movaps %xmm1, %xmm2
  143. pslldq $8, %xmm1
  144. psrldq $8, %xmm2
  145. por %xmm1, %xmm0
  146. # reduction
  147. pshufd $0b00100100, %xmm2, %xmm1
  148. pcmpeqd .Ltwo_one, %xmm1
  149. pand .Lpoly, %xmm1
  150. pxor %xmm1, %xmm0
  151. movups %xmm0, (%rdi)
  152. ret