1#!/usr/bin/env perl
2#
3# ====================================================================
4# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5# project. Rights for redistribution and usage in source and binary
6# forms are granted according to the OpenSSL license.
7# ====================================================================
8#
9# sha256/512_block procedure for x86_64.
10#
11# 40% improvement over compiler-generated code on Opteron. On EM64T
12# sha256 was observed to run >80% faster and sha512 - >40%. No magical
13# tricks, just straight implementation... I really wonder why gcc
14# [being armed with inline assembler] fails to generate as fast code.
15# The only thing which is cool about this module is that it's very
16# same instruction sequence used for both SHA-256 and SHA-512. In
17# former case the instructions operate on 32-bit operands, while in
18# latter - on 64-bit ones. All I had to do is to get one flavor right,
19# the other one passed the test right away:-)
20#
21# sha256_block runs in ~1005 cycles on Opteron, which gives you
22# asymptotic performance of 64*1000/1005=63.7MBps times CPU clock
23# frequency in GHz. sha512_block runs in ~1275 cycles, which results
24# in 128*1000/1275=100MBps per GHz. Is there room for improvement?
25# Well, if you compare it to IA-64 implementation, which maintains
26# X[16] in register bank[!], tends to 4 instructions per CPU clock
27# cycle and runs in 1003 cycles, 1275 is very good result for 3-way
28# issue Opteron pipeline and X[16] maintained in memory. So that *if*
29# there is a way to improve it, *then* the only way would be to try to
30# offload X[16] updates to SSE unit, but that would require "deeper"
31# loop unroll, which in turn would naturally cause size blow-up, not
32# to mention increased complexity! And once again, only *if* it's
33# actually possible to noticeably improve overall ILP, instruction
34# level parallelism, on a given CPU implementation in this case.
35#
36# Special note on Intel EM64T. While Opteron CPU exhibits perfect
37# perfromance ratio of 1.5 between 64- and 32-bit flavors [see above],
38# [currently available] EM64T CPUs apparently are far from it. On the
39# contrary, 64-bit version, sha512_block, is ~30% *slower* than 32-bit
40# sha256_block:-( This is presumably because 64-bit shifts/rotates
41# apparently are not atomic instructions, but implemented in microcode.
42
43#
44# OpenSolaris OS modifications
45#
46# Sun elects to use this software under the BSD license.
47#
48# This source originates from OpenSSL file sha512-x86_64.pl at
49# ftp://ftp.openssl.org/snapshot/openssl-0.9.8-stable-SNAP-20080131.tar.gz
50# (presumably for future OpenSSL release 0.9.8h), with these changes:
51#
52# 1. Added perl "use strict" and declared variables.
53#
54# 2. Added OpenSolaris ENTRY_NP/SET_SIZE macros from
55# /usr/include/sys/asm_linkage.h, .ident keywords, and lint(1B) guards.
56#
57# 3. Removed x86_64-xlate.pl script (not needed for as(1) or gas(1)
58# assemblers).  Replaced the .picmeup macro with assembler code.
59#
60# 4. Added 8 to $ctx, as OpenSolaris OS has an extra 4-byte field, "algotype",
61# at the beginning of SHA2_CTX (the next field is 8-byte aligned).
62#
63
64use strict;
65my ($code, $func, $TABLE, $SZ, @Sigma0, @Sigma1, @sigma0, @sigma1, $rounds,
66	@ROT, $A, $B, $C, $D, $E, $F, $G, $H, $T1, $a0, $a1, $a2, $i,
67	$ctx, $round, $inp, $Tbl, $_ctx, $_inp, $_end, $_rsp, $framesz);
68my $output = shift;
69open STDOUT,">$output";
70
71#
72# OpenSSL library:
73# void sha512_block_data_order(SHA512_CTX *ctx, const void *in, size_t num);
74# void sha256_block_data_order(SHA256_CTX *ctx, const void *in, size_t num);
75#
76# OpenSolaris OS:
77# void SHA512TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
78# void SHA256TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
79# Note: the OpenSolaris SHA2 structure has an extra 8 byte field at the
80# beginning (over OpenSSL's SHA512 or SHA256 structure).
81#
82
83if ($output =~ /512/) {
84	$func="SHA512TransformBlocks";
85	$TABLE="K512";
86	$SZ=8;
87	@ROT=($A,$B,$C,$D,$E,$F,$G,$H)=("%rax","%rbx","%rcx","%rdx",
88					"%r8", "%r9", "%r10","%r11");
89	($T1,$a0,$a1,$a2)=("%r12","%r13","%r14","%r15");
90	@Sigma0=(28,34,39);
91	@Sigma1=(14,18,41);
92	@sigma0=(1,  8, 7);
93	@sigma1=(19,61, 6);
94	$rounds=80;
95} else {
96	$func="SHA256TransformBlocks";
97	$TABLE="K256";
98	$SZ=4;
99	@ROT=($A,$B,$C,$D,$E,$F,$G,$H)=("%eax","%ebx","%ecx","%edx",
100					"%r8d","%r9d","%r10d","%r11d");
101	($T1,$a0,$a1,$a2)=("%r12d","%r13d","%r14d","%r15d");
102	@Sigma0=( 2,13,22);
103	@Sigma1=( 6,11,25);
104	@sigma0=( 7,18, 3);
105	@sigma1=(17,19,10);
106	$rounds=64;
107}
108
109$ctx="%rdi";	# 1st arg
110$round="%rdi";	# zaps $ctx
111$inp="%rsi";	# 2nd arg
112$Tbl="%rbp";
113
114$_ctx="16*$SZ+0*8(%rsp)";
115$_inp="16*$SZ+1*8(%rsp)";
116$_end="16*$SZ+2*8(%rsp)";
117$_rsp="16*$SZ+3*8(%rsp)";
118$framesz="16*$SZ+4*8";
119
120
121sub ROUND_00_15()
122{ my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_;
123
124$code.=<<___;
125	mov	$e,$a0
126	mov	$e,$a1
127	mov	$f,$a2
128
129	ror	\$$Sigma1[0],$a0
130	ror	\$$Sigma1[1],$a1
131	xor	$g,$a2			# f^g
132
133	xor	$a1,$a0
134	ror	\$`$Sigma1[2]-$Sigma1[1]`,$a1
135	and	$e,$a2			# (f^g)&e
136	mov	$T1,`$SZ*($i&0xf)`(%rsp)
137
138	xor	$a1,$a0			# Sigma1(e)
139	xor	$g,$a2			# Ch(e,f,g)=((f^g)&e)^g
140	add	$h,$T1			# T1+=h
141
142	mov	$a,$h
143	add	$a0,$T1			# T1+=Sigma1(e)
144
145	add	$a2,$T1			# T1+=Ch(e,f,g)
146	mov	$a,$a0
147	mov	$a,$a1
148
149	ror	\$$Sigma0[0],$h
150	ror	\$$Sigma0[1],$a0
151	mov	$a,$a2
152	add	($Tbl,$round,$SZ),$T1	# T1+=K[round]
153
154	xor	$a0,$h
155	ror	\$`$Sigma0[2]-$Sigma0[1]`,$a0
156	or	$c,$a1			# a|c
157
158	xor	$a0,$h			# h=Sigma0(a)
159	and	$c,$a2			# a&c
160	add	$T1,$d			# d+=T1
161
162	and	$b,$a1			# (a|c)&b
163	add	$T1,$h			# h+=T1
164
165	or	$a2,$a1			# Maj(a,b,c)=((a|c)&b)|(a&c)
166	lea	1($round),$round	# round++
167
168	add	$a1,$h			# h+=Maj(a,b,c)
169___
170}
171
172sub ROUND_16_XX()
173{ my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_;
174
175$code.=<<___;
176	mov	`$SZ*(($i+1)&0xf)`(%rsp),$a0
177	mov	`$SZ*(($i+14)&0xf)`(%rsp),$T1
178
179	mov	$a0,$a2
180
181	shr	\$$sigma0[2],$a0
182	ror	\$$sigma0[0],$a2
183
184	xor	$a2,$a0
185	ror	\$`$sigma0[1]-$sigma0[0]`,$a2
186
187	xor	$a2,$a0			# sigma0(X[(i+1)&0xf])
188	mov	$T1,$a1
189
190	shr	\$$sigma1[2],$T1
191	ror	\$$sigma1[0],$a1
192
193	xor	$a1,$T1
194	ror	\$`$sigma1[1]-$sigma1[0]`,$a1
195
196	xor	$a1,$T1			# sigma1(X[(i+14)&0xf])
197
198	add	$a0,$T1
199
200	add	`$SZ*(($i+9)&0xf)`(%rsp),$T1
201
202	add	`$SZ*($i&0xf)`(%rsp),$T1
203___
204	&ROUND_00_15(@_);
205}
206
207#
208# Execution begins here
209#
210
211$code=<<___;
212#if defined(lint) || defined(__lint)
213#include <sys/stdint.h>
214#include <sys/sha2.h>
215
216/* ARGSUSED */
217void
218$func(SHA2_CTX *ctx, const void *in, size_t num)
219{
220}
221
222
223#else
224#include <sys/asm_linkage.h>
225
226ENTRY_NP($func)
227	push	%rbx
228	push	%rbp
229	push	%r12
230	push	%r13
231	push	%r14
232	push	%r15
233	mov	%rsp,%rbp		# copy %rsp
234	shl	\$4,%rdx		# num*16
235	sub	\$$framesz,%rsp
236	lea	($inp,%rdx,$SZ),%rdx	# inp+num*16*$SZ
237	and	\$-64,%rsp		# align stack frame
238	add	\$8,$ctx		# Skip OpenSolaris field, "algotype"
239	mov	$ctx,$_ctx		# save ctx, 1st arg
240	mov	$inp,$_inp		# save inp, 2nd arg
241	mov	%rdx,$_end		# save end pointer, "3rd" arg
242	mov	%rbp,$_rsp		# save copy of %rsp
243
244	/.picmeup $Tbl
245	/ The .picmeup pseudo-directive, from perlasm/x86_64_xlate.pl, puts
246	/ the address of the "next" instruction into the target register
247	/ ($Tbl).  This generates these 2 instructions:
248	lea	.Llea(%rip),$Tbl
249	/nop	/ .picmeup generates a nop for mod 8 alignment--not needed here
250
251.Llea:
252	lea	$TABLE-.($Tbl),$Tbl
253
254	mov	$SZ*0($ctx),$A
255	mov	$SZ*1($ctx),$B
256	mov	$SZ*2($ctx),$C
257	mov	$SZ*3($ctx),$D
258	mov	$SZ*4($ctx),$E
259	mov	$SZ*5($ctx),$F
260	mov	$SZ*6($ctx),$G
261	mov	$SZ*7($ctx),$H
262	jmp	.Lloop
263
264.align	16
265.Lloop:
266	xor	$round,$round
267___
268	for($i=0;$i<16;$i++) {
269		$code.="	mov	$SZ*$i($inp),$T1\n";
270		$code.="	bswap	$T1\n";
271		&ROUND_00_15($i,@ROT);
272		unshift(@ROT,pop(@ROT));
273	}
274$code.=<<___;
275	jmp	.Lrounds_16_xx
276.align	16
277.Lrounds_16_xx:
278___
279	for(;$i<32;$i++) {
280		&ROUND_16_XX($i,@ROT);
281		unshift(@ROT,pop(@ROT));
282	}
283
284$code.=<<___;
285	cmp	\$$rounds,$round
286	jb	.Lrounds_16_xx
287
288	mov	$_ctx,$ctx
289	lea	16*$SZ($inp),$inp
290
291	add	$SZ*0($ctx),$A
292	add	$SZ*1($ctx),$B
293	add	$SZ*2($ctx),$C
294	add	$SZ*3($ctx),$D
295	add	$SZ*4($ctx),$E
296	add	$SZ*5($ctx),$F
297	add	$SZ*6($ctx),$G
298	add	$SZ*7($ctx),$H
299
300	cmp	$_end,$inp
301
302	mov	$A,$SZ*0($ctx)
303	mov	$B,$SZ*1($ctx)
304	mov	$C,$SZ*2($ctx)
305	mov	$D,$SZ*3($ctx)
306	mov	$E,$SZ*4($ctx)
307	mov	$F,$SZ*5($ctx)
308	mov	$G,$SZ*6($ctx)
309	mov	$H,$SZ*7($ctx)
310	jb	.Lloop
311
312	mov	$_rsp,%rsp
313	pop	%r15
314	pop	%r14
315	pop	%r13
316	pop	%r12
317	pop	%rbp
318	pop	%rbx
319
320	ret
321SET_SIZE($func)
322
323___
324
325if ($SZ==4) {
326# SHA256
327$code.=<<___;
328.align	64
329.type	$TABLE,\@object
330$TABLE:
331	.long	0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
332	.long	0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
333	.long	0xd807aa98,0x12835b01,0x243185be,0x550c7dc3
334	.long	0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174
335	.long	0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc
336	.long	0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da
337	.long	0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7
338	.long	0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967
339	.long	0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13
340	.long	0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85
341	.long	0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3
342	.long	0xd192e819,0xd6990624,0xf40e3585,0x106aa070
343	.long	0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5
344	.long	0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3
345	.long	0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
346	.long	0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
347___
348} else {
349# SHA512
350$code.=<<___;
351.align	64
352.type	$TABLE,\@object
353$TABLE:
354	.quad	0x428a2f98d728ae22,0x7137449123ef65cd
355	.quad	0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
356	.quad	0x3956c25bf348b538,0x59f111f1b605d019
357	.quad	0x923f82a4af194f9b,0xab1c5ed5da6d8118
358	.quad	0xd807aa98a3030242,0x12835b0145706fbe
359	.quad	0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
360	.quad	0x72be5d74f27b896f,0x80deb1fe3b1696b1
361	.quad	0x9bdc06a725c71235,0xc19bf174cf692694
362	.quad	0xe49b69c19ef14ad2,0xefbe4786384f25e3
363	.quad	0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
364	.quad	0x2de92c6f592b0275,0x4a7484aa6ea6e483
365	.quad	0x5cb0a9dcbd41fbd4,0x76f988da831153b5
366	.quad	0x983e5152ee66dfab,0xa831c66d2db43210
367	.quad	0xb00327c898fb213f,0xbf597fc7beef0ee4
368	.quad	0xc6e00bf33da88fc2,0xd5a79147930aa725
369	.quad	0x06ca6351e003826f,0x142929670a0e6e70
370	.quad	0x27b70a8546d22ffc,0x2e1b21385c26c926
371	.quad	0x4d2c6dfc5ac42aed,0x53380d139d95b3df
372	.quad	0x650a73548baf63de,0x766a0abb3c77b2a8
373	.quad	0x81c2c92e47edaee6,0x92722c851482353b
374	.quad	0xa2bfe8a14cf10364,0xa81a664bbc423001
375	.quad	0xc24b8b70d0f89791,0xc76c51a30654be30
376	.quad	0xd192e819d6ef5218,0xd69906245565a910
377	.quad	0xf40e35855771202a,0x106aa07032bbd1b8
378	.quad	0x19a4c116b8d2d0c8,0x1e376c085141ab53
379	.quad	0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
380	.quad	0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
381	.quad	0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
382	.quad	0x748f82ee5defb2fc,0x78a5636f43172f60
383	.quad	0x84c87814a1f0ab72,0x8cc702081a6439ec
384	.quad	0x90befffa23631e28,0xa4506cebde82bde9
385	.quad	0xbef9a3f7b2c67915,0xc67178f2e372532b
386	.quad	0xca273eceea26619c,0xd186b8c721c0c207
387	.quad	0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
388	.quad	0x06f067aa72176fba,0x0a637dc5a2c898a6
389	.quad	0x113f9804bef90dae,0x1b710b35131c471b
390	.quad	0x28db77f523047d84,0x32caab7b40c72493
391	.quad	0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
392	.quad	0x4cc5d4becb3e42b6,0x597f299cfc657e2a
393	.quad	0x5fcb6fab3ad6faec,0x6c44198c4a475817
394___
395}
396$code.=<<___;
397#endif /* !lint && !__lint */
398___
399
400$code =~ s/\`([^\`]*)\`/eval $1/gem;
401print $code;
402close STDOUT;
403