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