ghash-s390x.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.=<<___;
  78. stm${g} %r6,%r14,6*$SIZE_T($sp)
  79. aghi $Xi,-1
  80. lghi $len,1
  81. lghi $x78,`0xf<<3`
  82. larl $rem_4bit,rem_4bit
  83. lg $Zlo,8+1($Xi) # Xi
  84. j .Lgmult_shortcut
  85. .type gcm_gmult_4bit,\@function
  86. .size gcm_gmult_4bit,(.-gcm_gmult_4bit)
  87. .globl gcm_ghash_4bit
  88. .align 32
  89. gcm_ghash_4bit:
  90. ___
  91. $code.=<<___ if(!$softonly);
  92. larl %r1,OPENSSL_s390xcap_P
  93. lg %r0,S390X_KIMD+8(%r1) # load second word of kimd capabilities
  94. # vector
  95. tmhh %r0,0x4000 # check for function 65
  96. jz .Lsoft_ghash
  97. # Do not assume this function is called from a gcm128_context.
  98. # This is not true, e.g., for AES-GCM-SIV.
  99. # Parameter Block:
  100. # Chaining Value (XI) 128byte
  101. # Key (Htable[8]) 128byte
  102. lmg %r0,%r1,0($Xi)
  103. stmg %r0,%r1,8($sp)
  104. lmg %r0,%r1,8*16($Htbl)
  105. stmg %r0,%r1,24($sp)
  106. la %r1,8($sp)
  107. lghi %r0,S390X_GHASH # function 65
  108. .long 0xb93e0004 # kimd %r0,$inp
  109. brc 1,.-4 # pay attention to "partial completion"
  110. lmg %r0,%r1,8($sp)
  111. stmg %r0,%r1,0($Xi)
  112. br %r14
  113. .align 32
  114. .Lsoft_ghash:
  115. ___
  116. $code.=<<___ if ($flavour =~ /3[12]/);
  117. llgfr $len,$len
  118. ___
  119. $code.=<<___;
  120. stm${g} %r6,%r14,6*$SIZE_T($sp)
  121. aghi $Xi,-1
  122. srlg $len,$len,4
  123. lghi $x78,`0xf<<3`
  124. larl $rem_4bit,rem_4bit
  125. lg $Zlo,8+1($Xi) # Xi
  126. lg $Zhi,0+1($Xi)
  127. lghi $tmp,0
  128. .Louter:
  129. xg $Zhi,0($inp) # Xi ^= inp
  130. xg $Zlo,8($inp)
  131. xgr $Zhi,$tmp
  132. stg $Zlo,8+1($Xi)
  133. stg $Zhi,0+1($Xi)
  134. .Lgmult_shortcut:
  135. lghi $tmp,0xf0
  136. sllg $nlo,$Zlo,4
  137. srlg $xi,$Zlo,8 # extract second byte
  138. ngr $nlo,$tmp
  139. lgr $nhi,$Zlo
  140. lghi $cnt,14
  141. ngr $nhi,$tmp
  142. lg $Zlo,8($nlo,$Htbl)
  143. lg $Zhi,0($nlo,$Htbl)
  144. sllg $nlo,$xi,4
  145. sllg $rem0,$Zlo,3
  146. ngr $nlo,$tmp
  147. ngr $rem0,$x78
  148. ngr $xi,$tmp
  149. sllg $tmp,$Zhi,60
  150. srlg $Zlo,$Zlo,4
  151. srlg $Zhi,$Zhi,4
  152. xg $Zlo,8($nhi,$Htbl)
  153. xg $Zhi,0($nhi,$Htbl)
  154. lgr $nhi,$xi
  155. sllg $rem1,$Zlo,3
  156. xgr $Zlo,$tmp
  157. ngr $rem1,$x78
  158. sllg $tmp,$Zhi,60
  159. j .Lghash_inner
  160. .align 16
  161. .Lghash_inner:
  162. srlg $Zlo,$Zlo,4
  163. srlg $Zhi,$Zhi,4
  164. xg $Zlo,8($nlo,$Htbl)
  165. llgc $xi,0($cnt,$Xi)
  166. xg $Zhi,0($nlo,$Htbl)
  167. sllg $nlo,$xi,4
  168. xg $Zhi,0($rem0,$rem_4bit)
  169. nill $nlo,0xf0
  170. sllg $rem0,$Zlo,3
  171. xgr $Zlo,$tmp
  172. ngr $rem0,$x78
  173. nill $xi,0xf0
  174. sllg $tmp,$Zhi,60
  175. srlg $Zlo,$Zlo,4
  176. srlg $Zhi,$Zhi,4
  177. xg $Zlo,8($nhi,$Htbl)
  178. xg $Zhi,0($nhi,$Htbl)
  179. lgr $nhi,$xi
  180. xg $Zhi,0($rem1,$rem_4bit)
  181. sllg $rem1,$Zlo,3
  182. xgr $Zlo,$tmp
  183. ngr $rem1,$x78
  184. sllg $tmp,$Zhi,60
  185. brct $cnt,.Lghash_inner
  186. srlg $Zlo,$Zlo,4
  187. srlg $Zhi,$Zhi,4
  188. xg $Zlo,8($nlo,$Htbl)
  189. xg $Zhi,0($nlo,$Htbl)
  190. sllg $xi,$Zlo,3
  191. xg $Zhi,0($rem0,$rem_4bit)
  192. xgr $Zlo,$tmp
  193. ngr $xi,$x78
  194. sllg $tmp,$Zhi,60
  195. srlg $Zlo,$Zlo,4
  196. srlg $Zhi,$Zhi,4
  197. xg $Zlo,8($nhi,$Htbl)
  198. xg $Zhi,0($nhi,$Htbl)
  199. xgr $Zlo,$tmp
  200. xg $Zhi,0($rem1,$rem_4bit)
  201. lg $tmp,0($xi,$rem_4bit)
  202. la $inp,16($inp)
  203. sllg $tmp,$tmp,4 # correct last rem_4bit[rem]
  204. brctg $len,.Louter
  205. xgr $Zhi,$tmp
  206. stg $Zlo,8+1($Xi)
  207. stg $Zhi,0+1($Xi)
  208. lm${g} %r6,%r14,6*$SIZE_T($sp)
  209. br %r14
  210. .type gcm_ghash_4bit,\@function
  211. .size gcm_ghash_4bit,(.-gcm_ghash_4bit)
  212. .align 64
  213. rem_4bit:
  214. .long `0x0000<<12`,0,`0x1C20<<12`,0,`0x3840<<12`,0,`0x2460<<12`,0
  215. .long `0x7080<<12`,0,`0x6CA0<<12`,0,`0x48C0<<12`,0,`0x54E0<<12`,0
  216. .long `0xE100<<12`,0,`0xFD20<<12`,0,`0xD940<<12`,0,`0xC560<<12`,0
  217. .long `0x9180<<12`,0,`0x8DA0<<12`,0,`0xA9C0<<12`,0,`0xB5E0<<12`,0
  218. .type rem_4bit,\@object
  219. .size rem_4bit,(.-rem_4bit)
  220. .string "GHASH for s390x, CRYPTOGAMS by <appro\@openssl.org>"
  221. ___
  222. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  223. print $code;
  224. close STDOUT or die "error closing STDOUT: $!";