ghash-s390x.pl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #! /usr/bin/env perl
  2. # Copyright 2010-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. # $output is the last argument if it looks like a file (it has an extension)
  42. # $flavour is the first argument if it doesn't look like a file
  43. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  44. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  45. if ($flavour =~ /3[12]/) {
  46. $SIZE_T=4;
  47. $g="";
  48. } else {
  49. $SIZE_T=8;
  50. $g="g";
  51. }
  52. $output and open STDOUT,">$output";
  53. $softonly=0;
  54. $Zhi="%r0";
  55. $Zlo="%r1";
  56. $Xi="%r2"; # argument block
  57. $Htbl="%r3";
  58. $inp="%r4";
  59. $len="%r5";
  60. $rem0="%r6"; # variables
  61. $rem1="%r7";
  62. $nlo="%r8";
  63. $nhi="%r9";
  64. $xi="%r10";
  65. $cnt="%r11";
  66. $tmp="%r12";
  67. $x78="%r13";
  68. $rem_4bit="%r14";
  69. $sp="%r15";
  70. $code.=<<___;
  71. #include "s390x_arch.h"
  72. .text
  73. .globl gcm_gmult_4bit
  74. .align 32
  75. gcm_gmult_4bit:
  76. ___
  77. $code.=<<___ if(!$softonly && 0); # hardware is slow for single block...
  78. larl %r1,OPENSSL_s390xcap_P
  79. lghi %r0,0
  80. lg %r1,S390X_KIMD+8(%r1) # load second word of kimd capabilities
  81. # vector
  82. tmhh %r1,0x4000 # check for function 65
  83. jz .Lsoft_gmult
  84. stg %r0,16($sp) # arrange 16 bytes of zero input
  85. stg %r0,24($sp)
  86. lghi %r0,S390X_GHASH # function 65
  87. la %r1,0($Xi) # H lies right after Xi in gcm128_context
  88. la $inp,16($sp)
  89. lghi $len,16
  90. .long 0xb93e0004 # kimd %r0,$inp
  91. brc 1,.-4 # pay attention to "partial completion"
  92. br %r14
  93. .align 32
  94. .Lsoft_gmult:
  95. ___
  96. $code.=<<___;
  97. stm${g} %r6,%r14,6*$SIZE_T($sp)
  98. aghi $Xi,-1
  99. lghi $len,1
  100. lghi $x78,`0xf<<3`
  101. larl $rem_4bit,rem_4bit
  102. lg $Zlo,8+1($Xi) # Xi
  103. j .Lgmult_shortcut
  104. .type gcm_gmult_4bit,\@function
  105. .size gcm_gmult_4bit,(.-gcm_gmult_4bit)
  106. .globl gcm_ghash_4bit
  107. .align 32
  108. gcm_ghash_4bit:
  109. ___
  110. $code.=<<___ if(!$softonly);
  111. larl %r1,OPENSSL_s390xcap_P
  112. lg %r0,S390X_KIMD+8(%r1) # load second word of kimd capabilities
  113. # vector
  114. tmhh %r0,0x4000 # check for function 65
  115. jz .Lsoft_ghash
  116. lghi %r0,S390X_GHASH # 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. $code.=<<___ 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. sllg $tmp,$Zhi,60
  167. j .Lghash_inner
  168. .align 16
  169. .Lghash_inner:
  170. srlg $Zlo,$Zlo,4
  171. srlg $Zhi,$Zhi,4
  172. xg $Zlo,8($nlo,$Htbl)
  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. sllg $tmp,$Zhi,60
  193. brct $cnt,.Lghash_inner
  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 or die "error closing STDOUT: $!";