1321502cdSda73024#!/usr/bin/env perl 2321502cdSda73024# 3321502cdSda73024# ==================================================================== 4321502cdSda73024# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL 5321502cdSda73024# project. The module is, however, dual licensed under OpenSSL and 6321502cdSda73024# CRYPTOGAMS licenses depending on where you obtain it. For further 7321502cdSda73024# details see http://www.openssl.org/~appro/cryptogams/. 8321502cdSda73024# ==================================================================== 9321502cdSda73024# 10321502cdSda73024# sha1_block procedure for x86_64. 11321502cdSda73024# 12321502cdSda73024# It was brought to my attention that on EM64T compiler-generated code 13321502cdSda73024# was far behind 32-bit assembler implementation. This is unlike on 14321502cdSda73024# Opteron where compiler-generated code was only 15% behind 32-bit 15321502cdSda73024# assembler, which originally made it hard to motivate the effort. 16321502cdSda73024# There was suggestion to mechanically translate 32-bit code, but I 17321502cdSda73024# dismissed it, reasoning that x86_64 offers enough register bank 18321502cdSda73024# capacity to fully utilize SHA-1 parallelism. Therefore this fresh 19321502cdSda73024# implementation:-) However! While 64-bit code does performs better 20321502cdSda73024# on Opteron, I failed to beat 32-bit assembler on EM64T core. Well, 21321502cdSda73024# x86_64 does offer larger *addressable* bank, but out-of-order core 22321502cdSda73024# reaches for even more registers through dynamic aliasing, and EM64T 23321502cdSda73024# core must have managed to run-time optimize even 32-bit code just as 24321502cdSda73024# good as 64-bit one. Performance improvement is summarized in the 25321502cdSda73024# following table: 26321502cdSda73024# 27321502cdSda73024# gcc 3.4 32-bit asm cycles/byte 28321502cdSda73024# Opteron +45% +20% 6.8 29321502cdSda73024# Xeon P4 +65% +0% 9.9 30321502cdSda73024# Core2 +60% +10% 7.0 31321502cdSda73024 32321502cdSda73024# 33321502cdSda73024# OpenSolaris OS modifications 34321502cdSda73024# 35321502cdSda73024# Sun elects to use this software under the BSD license. 36321502cdSda73024# 37321502cdSda73024# This source originates from OpenSSL file sha1-x86_64.pl at 38321502cdSda73024# ftp://ftp.openssl.org/snapshot/openssl-0.9.8-stable-SNAP-20080131.tar.gz 39321502cdSda73024# (presumably for future OpenSSL release 0.9.8h), with these changes: 40321502cdSda73024# 41321502cdSda73024# 1. Added perl "use strict" and declared variables. 42321502cdSda73024# 43321502cdSda73024# 2. Added OpenSolaris ENTRY_NP/SET_SIZE macros from 44321502cdSda73024# /usr/include/sys/asm_linkage.h, .ident keywords, and lint(1B) guards. 45321502cdSda73024# 468de5c4f4SDan OpenSolaris Anderson# 3. Removed x86_64-xlate.pl script (not needed for as(1) or gas(1) assemblers). 47321502cdSda73024# 48321502cdSda73024 49321502cdSda73024use strict; 50321502cdSda73024my ($code, $ctx, $inp, $num, $xi, $t0, $t1, $i, @V, $A, $B, $C, $D, $E, $T); 51321502cdSda73024my $output = shift; 52321502cdSda73024open STDOUT,">$output"; 53321502cdSda73024 54321502cdSda73024 55321502cdSda73024# 56321502cdSda73024# void sha1_block_data_order(SHA1_CTX *ctx, const void *inpp, size_t blocks); 57321502cdSda73024# 58321502cdSda73024 59321502cdSda73024# Arguments: 60321502cdSda73024$ctx="%rdi"; # 1st arg 61321502cdSda73024$inp="%rsi"; # 2nd arg 62321502cdSda73024$num="%rdx"; # 3rd arg 63321502cdSda73024 64321502cdSda73024# reassign arguments in order to produce more compact code 65321502cdSda73024$ctx="%r8"; 66321502cdSda73024$inp="%r9"; 67321502cdSda73024$num="%r10"; 68321502cdSda73024 69321502cdSda73024# Temporaries: 70321502cdSda73024$xi="%eax"; 71321502cdSda73024$t0="%ebx"; 72321502cdSda73024$t1="%ecx"; 73321502cdSda73024# State information from SHA-1 context: 74321502cdSda73024$A="%edx"; 75321502cdSda73024$B="%esi"; 76321502cdSda73024$C="%edi"; 77321502cdSda73024$D="%ebp"; 78321502cdSda73024$E="%r11d"; 79321502cdSda73024# Temporary: 80321502cdSda73024$T="%r12d"; 81321502cdSda73024 82321502cdSda73024@V=($A,$B,$C,$D,$E,$T); 83321502cdSda73024 84321502cdSda73024sub PROLOGUE { 85321502cdSda73024my $func=shift; 86321502cdSda73024$code.=<<___; 87321502cdSda73024ENTRY_NP($func) 88321502cdSda73024 push %rbx 89321502cdSda73024 push %rbp 90321502cdSda73024 push %r12 91321502cdSda73024 mov %rsp,%rax 92321502cdSda73024 mov %rdi,$ctx # reassigned argument 93321502cdSda73024 sub \$`8+16*4`,%rsp 94321502cdSda73024 mov %rsi,$inp # reassigned argument 95321502cdSda73024 and \$-64,%rsp 96321502cdSda73024 mov %rdx,$num # reassigned argument 97321502cdSda73024 mov %rax,`16*4`(%rsp) 98321502cdSda73024 99321502cdSda73024 mov 0($ctx),$A 100321502cdSda73024 mov 4($ctx),$B 101321502cdSda73024 mov 8($ctx),$C 102321502cdSda73024 mov 12($ctx),$D 103321502cdSda73024 mov 16($ctx),$E 104321502cdSda73024___ 105321502cdSda73024} 106321502cdSda73024 107321502cdSda73024sub EPILOGUE { 108321502cdSda73024my $func=shift; 109321502cdSda73024$code.=<<___; 110321502cdSda73024 mov `16*4`(%rsp),%rsp 111321502cdSda73024 pop %r12 112321502cdSda73024 pop %rbp 113321502cdSda73024 pop %rbx 114321502cdSda73024 ret 115321502cdSda73024SET_SIZE($func) 116321502cdSda73024___ 117321502cdSda73024} 118321502cdSda73024 119321502cdSda73024sub BODY_00_19 { 120321502cdSda73024my ($i,$a,$b,$c,$d,$e,$f,$host)=@_; 121321502cdSda73024my $j=$i+1; 122321502cdSda73024$code.=<<___ if ($i==0); 123321502cdSda73024 mov `4*$i`($inp),$xi 124321502cdSda73024 `"bswap $xi" if(!defined($host))` 125321502cdSda73024 mov $xi,`4*$i`(%rsp) 126321502cdSda73024___ 127321502cdSda73024$code.=<<___ if ($i<15); 1288de5c4f4SDan OpenSolaris Anderson lea 0x5a827999($xi,$e),$f 129321502cdSda73024 mov $c,$t0 130321502cdSda73024 mov `4*$j`($inp),$xi 131321502cdSda73024 mov $a,$e 132321502cdSda73024 xor $d,$t0 133321502cdSda73024 `"bswap $xi" if(!defined($host))` 134321502cdSda73024 rol \$5,$e 135321502cdSda73024 and $b,$t0 136321502cdSda73024 mov $xi,`4*$j`(%rsp) 137321502cdSda73024 add $e,$f 138321502cdSda73024 xor $d,$t0 139321502cdSda73024 rol \$30,$b 140321502cdSda73024 add $t0,$f 141321502cdSda73024___ 142321502cdSda73024$code.=<<___ if ($i>=15); 1438de5c4f4SDan OpenSolaris Anderson lea 0x5a827999($xi,$e),$f 144321502cdSda73024 mov `4*($j%16)`(%rsp),$xi 145321502cdSda73024 mov $c,$t0 146321502cdSda73024 mov $a,$e 147321502cdSda73024 xor `4*(($j+2)%16)`(%rsp),$xi 148321502cdSda73024 xor $d,$t0 149321502cdSda73024 rol \$5,$e 150321502cdSda73024 xor `4*(($j+8)%16)`(%rsp),$xi 151321502cdSda73024 and $b,$t0 152321502cdSda73024 add $e,$f 153321502cdSda73024 xor `4*(($j+13)%16)`(%rsp),$xi 154321502cdSda73024 xor $d,$t0 155321502cdSda73024 rol \$30,$b 156321502cdSda73024 add $t0,$f 157321502cdSda73024 rol \$1,$xi 158321502cdSda73024 mov $xi,`4*($j%16)`(%rsp) 159321502cdSda73024___ 160321502cdSda73024} 161321502cdSda73024 162321502cdSda73024sub BODY_20_39 { 163321502cdSda73024my ($i,$a,$b,$c,$d,$e,$f)=@_; 164321502cdSda73024my $j=$i+1; 165*9b793925STheo Schlossnaglemy $K=($i<40)?"0x6ed9eba1":"-0x359d3e2a"; 166321502cdSda73024$code.=<<___ if ($i<79); 1678de5c4f4SDan OpenSolaris Anderson lea $K($xi,$e),$f 168321502cdSda73024 mov `4*($j%16)`(%rsp),$xi 169321502cdSda73024 mov $c,$t0 170321502cdSda73024 mov $a,$e 171321502cdSda73024 xor `4*(($j+2)%16)`(%rsp),$xi 172321502cdSda73024 xor $b,$t0 173321502cdSda73024 rol \$5,$e 174321502cdSda73024 xor `4*(($j+8)%16)`(%rsp),$xi 175321502cdSda73024 xor $d,$t0 176321502cdSda73024 add $e,$f 177321502cdSda73024 xor `4*(($j+13)%16)`(%rsp),$xi 178321502cdSda73024 rol \$30,$b 179321502cdSda73024 add $t0,$f 180321502cdSda73024 rol \$1,$xi 181321502cdSda73024___ 182321502cdSda73024$code.=<<___ if ($i<76); 183321502cdSda73024 mov $xi,`4*($j%16)`(%rsp) 184321502cdSda73024___ 185321502cdSda73024$code.=<<___ if ($i==79); 1868de5c4f4SDan OpenSolaris Anderson lea $K($xi,$e),$f 187321502cdSda73024 mov $c,$t0 188321502cdSda73024 mov $a,$e 189321502cdSda73024 xor $b,$t0 190321502cdSda73024 rol \$5,$e 191321502cdSda73024 xor $d,$t0 192321502cdSda73024 add $e,$f 193321502cdSda73024 rol \$30,$b 194321502cdSda73024 add $t0,$f 195321502cdSda73024___ 196321502cdSda73024} 197321502cdSda73024 198321502cdSda73024sub BODY_40_59 { 199321502cdSda73024my ($i,$a,$b,$c,$d,$e,$f)=@_; 200321502cdSda73024my $j=$i+1; 201321502cdSda73024$code.=<<___; 202*9b793925STheo Schlossnagle lea -0x70e44324($xi,$e),$f 203321502cdSda73024 mov `4*($j%16)`(%rsp),$xi 204321502cdSda73024 mov $b,$t0 205321502cdSda73024 mov $b,$t1 206321502cdSda73024 xor `4*(($j+2)%16)`(%rsp),$xi 207321502cdSda73024 mov $a,$e 208321502cdSda73024 and $c,$t0 209321502cdSda73024 xor `4*(($j+8)%16)`(%rsp),$xi 210321502cdSda73024 or $c,$t1 211321502cdSda73024 rol \$5,$e 212321502cdSda73024 xor `4*(($j+13)%16)`(%rsp),$xi 213321502cdSda73024 and $d,$t1 214321502cdSda73024 add $e,$f 215321502cdSda73024 rol \$1,$xi 216321502cdSda73024 or $t1,$t0 217321502cdSda73024 rol \$30,$b 218321502cdSda73024 mov $xi,`4*($j%16)`(%rsp) 219321502cdSda73024 add $t0,$f 220321502cdSda73024___ 221321502cdSda73024} 222321502cdSda73024 2238de5c4f4SDan OpenSolaris Anderson 2248de5c4f4SDan OpenSolaris Anderson# 2258de5c4f4SDan OpenSolaris Anderson# Execution begins here 2268de5c4f4SDan OpenSolaris Anderson# 2278de5c4f4SDan OpenSolaris Anderson 228321502cdSda73024$code=<<___; 2298de5c4f4SDan OpenSolaris Anderson#if defined(lint) || defined(__lint) 2308de5c4f4SDan OpenSolaris Anderson#include <sys/stdint.h> 2318de5c4f4SDan OpenSolaris Anderson#include <sys/sha1.h> 2328de5c4f4SDan OpenSolaris Anderson 2338de5c4f4SDan OpenSolaris Anderson/* ARGSUSED */ 2348de5c4f4SDan OpenSolaris Andersonvoid 2358de5c4f4SDan OpenSolaris Andersonsha1_block_data_order(SHA1_CTX *ctx, const void *inpp, size_t blocks) 2368de5c4f4SDan OpenSolaris Anderson{ 2378de5c4f4SDan OpenSolaris Anderson} 2388de5c4f4SDan OpenSolaris Anderson 2398de5c4f4SDan OpenSolaris Anderson#else 240321502cdSda73024#include <sys/asm_linkage.h> 241321502cdSda73024___ 242321502cdSda73024 243321502cdSda73024 244321502cdSda73024&PROLOGUE("sha1_block_data_order"); 245321502cdSda73024$code.=".align 4\n.Lloop:\n"; 246321502cdSda73024for($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); } 247321502cdSda73024for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); } 248321502cdSda73024for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); } 249321502cdSda73024for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); } 250321502cdSda73024$code.=<<___; 251321502cdSda73024 / Update and save state information in SHA-1 context 252321502cdSda73024 add 0($ctx),$E 253321502cdSda73024 add 4($ctx),$T 254321502cdSda73024 add 8($ctx),$A 255321502cdSda73024 add 12($ctx),$B 256321502cdSda73024 add 16($ctx),$C 257321502cdSda73024 mov $E,0($ctx) 258321502cdSda73024 mov $T,4($ctx) 259321502cdSda73024 mov $A,8($ctx) 260321502cdSda73024 mov $B,12($ctx) 261321502cdSda73024 mov $C,16($ctx) 262321502cdSda73024 263321502cdSda73024 xchg $E,$A # mov $E,$A 264321502cdSda73024 xchg $T,$B # mov $T,$B 265321502cdSda73024 xchg $E,$C # mov $A,$C 266321502cdSda73024 xchg $T,$D # mov $B,$D 267321502cdSda73024 # mov $C,$E 268321502cdSda73024 lea `16*4`($inp),$inp 269321502cdSda73024 sub \$1,$num 270321502cdSda73024 jnz .Lloop 271321502cdSda73024___ 272321502cdSda73024&EPILOGUE("sha1_block_data_order"); 273321502cdSda73024$code.=<<___; 274321502cdSda73024.asciz "SHA1 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>" 275321502cdSda73024 2768de5c4f4SDan OpenSolaris Anderson#endif /* lint || __lint */ 277321502cdSda73024___ 278321502cdSda73024 279321502cdSda73024#################################################################### 280321502cdSda73024 281321502cdSda73024$code =~ s/\`([^\`]*)\`/eval $1/gem; 282321502cdSda73024print $code; 283321502cdSda73024close STDOUT; 284