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