sha512-s390x.pl 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #! /usr/bin/env perl
  2. # Copyright 2007-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. # SHA256/512 block procedures for s390x.
  15. # April 2007.
  16. #
  17. # sha256_block_data_order is reportedly >3 times faster than gcc 3.3
  18. # generated code (must be a bug in compiler, as improvement is
  19. # "pathologically" high, in particular in comparison to other SHA
  20. # modules). But the real twist is that it detects if hardware support
  21. # for SHA256 is available and in such case utilizes it. Then the
  22. # performance can reach >6.5x of assembler one for larger chunks.
  23. #
  24. # sha512_block_data_order is ~70% faster than gcc 3.3 generated code.
  25. # January 2009.
  26. #
  27. # Add support for hardware SHA512 and reschedule instructions to
  28. # favour dual-issue z10 pipeline. Hardware SHA256/512 is ~4.7x faster
  29. # than software.
  30. # November 2010.
  31. #
  32. # Adapt for -m31 build. If kernel supports what's called "highgprs"
  33. # feature on Linux [see /proc/cpuinfo], it's possible to use 64-bit
  34. # instructions and achieve "64-bit" performance even in 31-bit legacy
  35. # application context. The feature is not specific to any particular
  36. # processor, as long as it's "z-CPU". Latter implies that the code
  37. # remains z/Architecture specific. On z990 SHA256 was measured to
  38. # perform 2.4x and SHA512 - 13x better than code generated by gcc 4.3.
  39. $flavour = shift;
  40. if ($flavour =~ /3[12]/) {
  41. $SIZE_T=4;
  42. $g="";
  43. } else {
  44. $SIZE_T=8;
  45. $g="g";
  46. }
  47. $t0="%r0";
  48. $t1="%r1";
  49. $ctx="%r2"; $t2="%r2";
  50. $inp="%r3";
  51. $len="%r4"; # used as index in inner loop
  52. $A="%r5";
  53. $B="%r6";
  54. $C="%r7";
  55. $D="%r8";
  56. $E="%r9";
  57. $F="%r10";
  58. $G="%r11";
  59. $H="%r12"; @V=($A,$B,$C,$D,$E,$F,$G,$H);
  60. $tbl="%r13";
  61. $T1="%r14";
  62. $sp="%r15";
  63. while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
  64. open STDOUT,">$output";
  65. if ($output =~ /512/) {
  66. $label="512";
  67. $SZ=8;
  68. $LD="lg"; # load from memory
  69. $ST="stg"; # store to memory
  70. $ADD="alg"; # add with memory operand
  71. $ROT="rllg"; # rotate left
  72. $SHR="srlg"; # logical right shift [see even at the end]
  73. @Sigma0=(25,30,36);
  74. @Sigma1=(23,46,50);
  75. @sigma0=(56,63, 7);
  76. @sigma1=( 3,45, 6);
  77. $rounds=80;
  78. $kimdfunc=3; # 0 means unknown/unsupported/unimplemented/disabled
  79. } else {
  80. $label="256";
  81. $SZ=4;
  82. $LD="llgf"; # load from memory
  83. $ST="st"; # store to memory
  84. $ADD="al"; # add with memory operand
  85. $ROT="rll"; # rotate left
  86. $SHR="srl"; # logical right shift
  87. @Sigma0=(10,19,30);
  88. @Sigma1=( 7,21,26);
  89. @sigma0=(14,25, 3);
  90. @sigma1=(13,15,10);
  91. $rounds=64;
  92. $kimdfunc=2; # magic function code for kimd instruction
  93. }
  94. $Func="sha${label}_block_data_order";
  95. $Table="K${label}";
  96. $stdframe=16*$SIZE_T+4*8;
  97. $frame=$stdframe+16*$SZ;
  98. sub BODY_00_15 {
  99. my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_;
  100. $code.=<<___ if ($i<16);
  101. $LD $T1,`$i*$SZ`($inp) ### $i
  102. ___
  103. $code.=<<___;
  104. $ROT $t0,$e,$Sigma1[0]
  105. $ROT $t1,$e,$Sigma1[1]
  106. lgr $t2,$f
  107. xgr $t0,$t1
  108. $ROT $t1,$t1,`$Sigma1[2]-$Sigma1[1]`
  109. xgr $t2,$g
  110. $ST $T1,`$stdframe+$SZ*($i%16)`($sp)
  111. xgr $t0,$t1 # Sigma1(e)
  112. algr $T1,$h # T1+=h
  113. ngr $t2,$e
  114. lgr $t1,$a
  115. algr $T1,$t0 # T1+=Sigma1(e)
  116. $ROT $h,$a,$Sigma0[0]
  117. xgr $t2,$g # Ch(e,f,g)
  118. $ADD $T1,`$i*$SZ`($len,$tbl) # T1+=K[i]
  119. $ROT $t0,$a,$Sigma0[1]
  120. algr $T1,$t2 # T1+=Ch(e,f,g)
  121. ogr $t1,$b
  122. xgr $h,$t0
  123. lgr $t2,$a
  124. ngr $t1,$c
  125. $ROT $t0,$t0,`$Sigma0[2]-$Sigma0[1]`
  126. xgr $h,$t0 # h=Sigma0(a)
  127. ngr $t2,$b
  128. algr $h,$T1 # h+=T1
  129. ogr $t2,$t1 # Maj(a,b,c)
  130. algr $d,$T1 # d+=T1
  131. algr $h,$t2 # h+=Maj(a,b,c)
  132. ___
  133. }
  134. sub BODY_16_XX {
  135. my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_;
  136. $code.=<<___;
  137. $LD $T1,`$stdframe+$SZ*(($i+1)%16)`($sp) ### $i
  138. $LD $t1,`$stdframe+$SZ*(($i+14)%16)`($sp)
  139. $ROT $t0,$T1,$sigma0[0]
  140. $SHR $T1,$sigma0[2]
  141. $ROT $t2,$t0,`$sigma0[1]-$sigma0[0]`
  142. xgr $T1,$t0
  143. $ROT $t0,$t1,$sigma1[0]
  144. xgr $T1,$t2 # sigma0(X[i+1])
  145. $SHR $t1,$sigma1[2]
  146. $ADD $T1,`$stdframe+$SZ*($i%16)`($sp) # +=X[i]
  147. xgr $t1,$t0
  148. $ROT $t0,$t0,`$sigma1[1]-$sigma1[0]`
  149. $ADD $T1,`$stdframe+$SZ*(($i+9)%16)`($sp) # +=X[i+9]
  150. xgr $t1,$t0 # sigma1(X[i+14])
  151. algr $T1,$t1 # +=sigma1(X[i+14])
  152. ___
  153. &BODY_00_15(@_);
  154. }
  155. $code.=<<___;
  156. #include "s390x_arch.h"
  157. .text
  158. .align 64
  159. .type $Table,\@object
  160. $Table:
  161. ___
  162. $code.=<<___ if ($SZ==4);
  163. .long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
  164. .long 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
  165. .long 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3
  166. .long 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174
  167. .long 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc
  168. .long 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da
  169. .long 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7
  170. .long 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967
  171. .long 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13
  172. .long 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85
  173. .long 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3
  174. .long 0xd192e819,0xd6990624,0xf40e3585,0x106aa070
  175. .long 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5
  176. .long 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3
  177. .long 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
  178. .long 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
  179. ___
  180. $code.=<<___ if ($SZ==8);
  181. .quad 0x428a2f98d728ae22,0x7137449123ef65cd
  182. .quad 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
  183. .quad 0x3956c25bf348b538,0x59f111f1b605d019
  184. .quad 0x923f82a4af194f9b,0xab1c5ed5da6d8118
  185. .quad 0xd807aa98a3030242,0x12835b0145706fbe
  186. .quad 0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
  187. .quad 0x72be5d74f27b896f,0x80deb1fe3b1696b1
  188. .quad 0x9bdc06a725c71235,0xc19bf174cf692694
  189. .quad 0xe49b69c19ef14ad2,0xefbe4786384f25e3
  190. .quad 0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
  191. .quad 0x2de92c6f592b0275,0x4a7484aa6ea6e483
  192. .quad 0x5cb0a9dcbd41fbd4,0x76f988da831153b5
  193. .quad 0x983e5152ee66dfab,0xa831c66d2db43210
  194. .quad 0xb00327c898fb213f,0xbf597fc7beef0ee4
  195. .quad 0xc6e00bf33da88fc2,0xd5a79147930aa725
  196. .quad 0x06ca6351e003826f,0x142929670a0e6e70
  197. .quad 0x27b70a8546d22ffc,0x2e1b21385c26c926
  198. .quad 0x4d2c6dfc5ac42aed,0x53380d139d95b3df
  199. .quad 0x650a73548baf63de,0x766a0abb3c77b2a8
  200. .quad 0x81c2c92e47edaee6,0x92722c851482353b
  201. .quad 0xa2bfe8a14cf10364,0xa81a664bbc423001
  202. .quad 0xc24b8b70d0f89791,0xc76c51a30654be30
  203. .quad 0xd192e819d6ef5218,0xd69906245565a910
  204. .quad 0xf40e35855771202a,0x106aa07032bbd1b8
  205. .quad 0x19a4c116b8d2d0c8,0x1e376c085141ab53
  206. .quad 0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
  207. .quad 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
  208. .quad 0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
  209. .quad 0x748f82ee5defb2fc,0x78a5636f43172f60
  210. .quad 0x84c87814a1f0ab72,0x8cc702081a6439ec
  211. .quad 0x90befffa23631e28,0xa4506cebde82bde9
  212. .quad 0xbef9a3f7b2c67915,0xc67178f2e372532b
  213. .quad 0xca273eceea26619c,0xd186b8c721c0c207
  214. .quad 0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
  215. .quad 0x06f067aa72176fba,0x0a637dc5a2c898a6
  216. .quad 0x113f9804bef90dae,0x1b710b35131c471b
  217. .quad 0x28db77f523047d84,0x32caab7b40c72493
  218. .quad 0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
  219. .quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a
  220. .quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817
  221. ___
  222. $code.=<<___;
  223. .size $Table,.-$Table
  224. .globl $Func
  225. .type $Func,\@function
  226. $Func:
  227. sllg $len,$len,`log(16*$SZ)/log(2)`
  228. ___
  229. $code.=<<___ if ($kimdfunc);
  230. larl %r1,OPENSSL_s390xcap_P
  231. lg %r0,S390X_KIMD(%r1) # check kimd capabilities
  232. tmhh %r0,`0x8000>>$kimdfunc`
  233. jz .Lsoftware
  234. lghi %r0,$kimdfunc
  235. lgr %r1,$ctx
  236. lgr %r2,$inp
  237. lgr %r3,$len
  238. .long 0xb93e0002 # kimd %r0,%r2
  239. brc 1,.-4 # pay attention to "partial completion"
  240. br %r14
  241. .align 16
  242. .Lsoftware:
  243. ___
  244. $code.=<<___;
  245. lghi %r1,-$frame
  246. la $len,0($len,$inp)
  247. stm${g} $ctx,%r15,`2*$SIZE_T`($sp)
  248. lgr %r0,$sp
  249. la $sp,0(%r1,$sp)
  250. st${g} %r0,0($sp)
  251. larl $tbl,$Table
  252. $LD $A,`0*$SZ`($ctx)
  253. $LD $B,`1*$SZ`($ctx)
  254. $LD $C,`2*$SZ`($ctx)
  255. $LD $D,`3*$SZ`($ctx)
  256. $LD $E,`4*$SZ`($ctx)
  257. $LD $F,`5*$SZ`($ctx)
  258. $LD $G,`6*$SZ`($ctx)
  259. $LD $H,`7*$SZ`($ctx)
  260. .Lloop:
  261. lghi $len,0
  262. ___
  263. for ($i=0;$i<16;$i++) { &BODY_00_15($i,@V); unshift(@V,pop(@V)); }
  264. $code.=".Lrounds_16_xx:\n";
  265. for (;$i<32;$i++) { &BODY_16_XX($i,@V); unshift(@V,pop(@V)); }
  266. $code.=<<___;
  267. aghi $len,`16*$SZ`
  268. lghi $t0,`($rounds-16)*$SZ`
  269. clgr $len,$t0
  270. jne .Lrounds_16_xx
  271. l${g} $ctx,`$frame+2*$SIZE_T`($sp)
  272. la $inp,`16*$SZ`($inp)
  273. $ADD $A,`0*$SZ`($ctx)
  274. $ADD $B,`1*$SZ`($ctx)
  275. $ADD $C,`2*$SZ`($ctx)
  276. $ADD $D,`3*$SZ`($ctx)
  277. $ADD $E,`4*$SZ`($ctx)
  278. $ADD $F,`5*$SZ`($ctx)
  279. $ADD $G,`6*$SZ`($ctx)
  280. $ADD $H,`7*$SZ`($ctx)
  281. $ST $A,`0*$SZ`($ctx)
  282. $ST $B,`1*$SZ`($ctx)
  283. $ST $C,`2*$SZ`($ctx)
  284. $ST $D,`3*$SZ`($ctx)
  285. $ST $E,`4*$SZ`($ctx)
  286. $ST $F,`5*$SZ`($ctx)
  287. $ST $G,`6*$SZ`($ctx)
  288. $ST $H,`7*$SZ`($ctx)
  289. cl${g} $inp,`$frame+4*$SIZE_T`($sp)
  290. jne .Lloop
  291. lm${g} %r6,%r15,`$frame+6*$SIZE_T`($sp)
  292. br %r14
  293. .size $Func,.-$Func
  294. .string "SHA${label} block transform for s390x, CRYPTOGAMS by <appro\@openssl.org>"
  295. ___
  296. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  297. # unlike 32-bit shift 64-bit one takes three arguments
  298. $code =~ s/(srlg\s+)(%r[0-9]+),/$1$2,$2,/gm;
  299. print $code;
  300. close STDOUT;