ghash-clmulni-intel_asm.S 2.8 KB

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