ghash-s390x.pl 6.0 KB

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