ghash-s390x.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #! /usr/bin/env perl
  2. # Copyright 2010-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. # September 2010.
  15. #
  16. # The module implements "4-bit" GCM GHASH function and underlying
  17. # single multiplication operation in GF(2^128). "4-bit" means that it
  18. # uses 256 bytes per-key table [+128 bytes shared table]. Performance
  19. # was measured to be ~18 cycles per processed byte on z10, which is
  20. # almost 40% better than gcc-generated code. It should be noted that
  21. # 18 cycles is worse result than expected: loop is scheduled for 12
  22. # and the result should be close to 12. In the lack of instruction-
  23. # level profiling data it's impossible to tell why...
  24. # November 2010.
  25. #
  26. # Adapt for -m31 build. If kernel supports what's called "highgprs"
  27. # feature on Linux [see /proc/cpuinfo], it's possible to use 64-bit
  28. # instructions and achieve "64-bit" performance even in 31-bit legacy
  29. # application context. The feature is not specific to any particular
  30. # processor, as long as it's "z-CPU". Latter implies that the code
  31. # remains z/Architecture specific. On z990 it was measured to perform
  32. # 2.8x better than 32-bit code generated by gcc 4.3.
  33. # March 2011.
  34. #
  35. # Support for hardware KIMD-GHASH is verified to produce correct
  36. # result and therefore is engaged. On z196 it was measured to process
  37. # 8KB buffer ~7 faster than software implementation. It's not as
  38. # impressive for smaller buffer sizes and for smallest 16-bytes buffer
  39. # it's actually almost 2 times slower. Which is the reason why
  40. # KIMD-GHASH is not used in gcm_gmult_4bit.
  41. $flavour = shift;
  42. if ($flavour =~ /3[12]/) {
  43. $SIZE_T=4;
  44. $g="";
  45. } else {
  46. $SIZE_T=8;
  47. $g="g";
  48. }
  49. while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
  50. open STDOUT,">$output";
  51. $softonly=0;
  52. $Zhi="%r0";
  53. $Zlo="%r1";
  54. $Xi="%r2"; # argument block
  55. $Htbl="%r3";
  56. $inp="%r4";
  57. $len="%r5";
  58. $rem0="%r6"; # variables
  59. $rem1="%r7";
  60. $nlo="%r8";
  61. $nhi="%r9";
  62. $xi="%r10";
  63. $cnt="%r11";
  64. $tmp="%r12";
  65. $x78="%r13";
  66. $rem_4bit="%r14";
  67. $sp="%r15";
  68. $code.=<<___;
  69. #include "s390x_arch.h"
  70. .text
  71. .globl gcm_gmult_4bit
  72. .align 32
  73. gcm_gmult_4bit:
  74. ___
  75. $code.=<<___ if(!$softonly && 0); # hardware is slow for single block...
  76. larl %r1,OPENSSL_s390xcap_P
  77. lghi %r0,0
  78. lg %r1,S390X_KIMD+8(%r1) # load second word of kimd capabilities
  79. # vector
  80. tmhh %r1,0x4000 # check for function 65
  81. jz .Lsoft_gmult
  82. stg %r0,16($sp) # arrange 16 bytes of zero input
  83. stg %r0,24($sp)
  84. lghi %r0,S390X_GHASH # function 65
  85. la %r1,0($Xi) # H lies right after Xi in gcm128_context
  86. la $inp,16($sp)
  87. lghi $len,16
  88. .long 0xb93e0004 # kimd %r0,$inp
  89. brc 1,.-4 # pay attention to "partial completion"
  90. br %r14
  91. .align 32
  92. .Lsoft_gmult:
  93. ___
  94. $code.=<<___;
  95. stm${g} %r6,%r14,6*$SIZE_T($sp)
  96. aghi $Xi,-1
  97. lghi $len,1
  98. lghi $x78,`0xf<<3`
  99. larl $rem_4bit,rem_4bit
  100. lg $Zlo,8+1($Xi) # Xi
  101. j .Lgmult_shortcut
  102. .type gcm_gmult_4bit,\@function
  103. .size gcm_gmult_4bit,(.-gcm_gmult_4bit)
  104. .globl gcm_ghash_4bit
  105. .align 32
  106. gcm_ghash_4bit:
  107. ___
  108. $code.=<<___ if(!$softonly);
  109. larl %r1,OPENSSL_s390xcap_P
  110. lg %r0,S390X_KIMD+8(%r1) # load second word of kimd capabilities
  111. # vector
  112. tmhh %r0,0x4000 # check for function 65
  113. jz .Lsoft_ghash
  114. lghi %r0,S390X_GHASH # function 65
  115. la %r1,0($Xi) # H lies right after Xi in gcm128_context
  116. .long 0xb93e0004 # kimd %r0,$inp
  117. brc 1,.-4 # pay attention to "partial completion"
  118. br %r14
  119. .align 32
  120. .Lsoft_ghash:
  121. ___
  122. $code.=<<___ if ($flavour =~ /3[12]/);
  123. llgfr $len,$len
  124. ___
  125. $code.=<<___;
  126. stm${g} %r6,%r14,6*$SIZE_T($sp)
  127. aghi $Xi,-1
  128. srlg $len,$len,4
  129. lghi $x78,`0xf<<3`
  130. larl $rem_4bit,rem_4bit
  131. lg $Zlo,8+1($Xi) # Xi
  132. lg $Zhi,0+1($Xi)
  133. lghi $tmp,0
  134. .Louter:
  135. xg $Zhi,0($inp) # Xi ^= inp
  136. xg $Zlo,8($inp)
  137. xgr $Zhi,$tmp
  138. stg $Zlo,8+1($Xi)
  139. stg $Zhi,0+1($Xi)
  140. .Lgmult_shortcut:
  141. lghi $tmp,0xf0
  142. sllg $nlo,$Zlo,4
  143. srlg $xi,$Zlo,8 # extract second byte
  144. ngr $nlo,$tmp
  145. lgr $nhi,$Zlo
  146. lghi $cnt,14
  147. ngr $nhi,$tmp
  148. lg $Zlo,8($nlo,$Htbl)
  149. lg $Zhi,0($nlo,$Htbl)
  150. sllg $nlo,$xi,4
  151. sllg $rem0,$Zlo,3
  152. ngr $nlo,$tmp
  153. ngr $rem0,$x78
  154. ngr $xi,$tmp
  155. sllg $tmp,$Zhi,60
  156. srlg $Zlo,$Zlo,4
  157. srlg $Zhi,$Zhi,4
  158. xg $Zlo,8($nhi,$Htbl)
  159. xg $Zhi,0($nhi,$Htbl)
  160. lgr $nhi,$xi
  161. sllg $rem1,$Zlo,3
  162. xgr $Zlo,$tmp
  163. ngr $rem1,$x78
  164. sllg $tmp,$Zhi,60
  165. j .Lghash_inner
  166. .align 16
  167. .Lghash_inner:
  168. srlg $Zlo,$Zlo,4
  169. srlg $Zhi,$Zhi,4
  170. xg $Zlo,8($nlo,$Htbl)
  171. llgc $xi,0($cnt,$Xi)
  172. xg $Zhi,0($nlo,$Htbl)
  173. sllg $nlo,$xi,4
  174. xg $Zhi,0($rem0,$rem_4bit)
  175. nill $nlo,0xf0
  176. sllg $rem0,$Zlo,3
  177. xgr $Zlo,$tmp
  178. ngr $rem0,$x78
  179. nill $xi,0xf0
  180. sllg $tmp,$Zhi,60
  181. srlg $Zlo,$Zlo,4
  182. srlg $Zhi,$Zhi,4
  183. xg $Zlo,8($nhi,$Htbl)
  184. xg $Zhi,0($nhi,$Htbl)
  185. lgr $nhi,$xi
  186. xg $Zhi,0($rem1,$rem_4bit)
  187. sllg $rem1,$Zlo,3
  188. xgr $Zlo,$tmp
  189. ngr $rem1,$x78
  190. sllg $tmp,$Zhi,60
  191. brct $cnt,.Lghash_inner
  192. srlg $Zlo,$Zlo,4
  193. srlg $Zhi,$Zhi,4
  194. xg $Zlo,8($nlo,$Htbl)
  195. xg $Zhi,0($nlo,$Htbl)
  196. sllg $xi,$Zlo,3
  197. xg $Zhi,0($rem0,$rem_4bit)
  198. xgr $Zlo,$tmp
  199. ngr $xi,$x78
  200. sllg $tmp,$Zhi,60
  201. srlg $Zlo,$Zlo,4
  202. srlg $Zhi,$Zhi,4
  203. xg $Zlo,8($nhi,$Htbl)
  204. xg $Zhi,0($nhi,$Htbl)
  205. xgr $Zlo,$tmp
  206. xg $Zhi,0($rem1,$rem_4bit)
  207. lg $tmp,0($xi,$rem_4bit)
  208. la $inp,16($inp)
  209. sllg $tmp,$tmp,4 # correct last rem_4bit[rem]
  210. brctg $len,.Louter
  211. xgr $Zhi,$tmp
  212. stg $Zlo,8+1($Xi)
  213. stg $Zhi,0+1($Xi)
  214. lm${g} %r6,%r14,6*$SIZE_T($sp)
  215. br %r14
  216. .type gcm_ghash_4bit,\@function
  217. .size gcm_ghash_4bit,(.-gcm_ghash_4bit)
  218. .align 64
  219. rem_4bit:
  220. .long `0x0000<<12`,0,`0x1C20<<12`,0,`0x3840<<12`,0,`0x2460<<12`,0
  221. .long `0x7080<<12`,0,`0x6CA0<<12`,0,`0x48C0<<12`,0,`0x54E0<<12`,0
  222. .long `0xE100<<12`,0,`0xFD20<<12`,0,`0xD940<<12`,0,`0xC560<<12`,0
  223. .long `0x9180<<12`,0,`0x8DA0<<12`,0,`0xA9C0<<12`,0,`0xB5E0<<12`,0
  224. .type rem_4bit,\@object
  225. .size rem_4bit,(.-rem_4bit)
  226. .string "GHASH for s390x, CRYPTOGAMS by <appro\@openssl.org>"
  227. ___
  228. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  229. print $code;
  230. close STDOUT;