Print this page
6665607 Need a SHA256/SHA384/SHA512 implementation optimized for 64-bit x86


 159 
 160 # Temporaries:
 161 $xi="%eax";
 162 $t0="%ebx";
 163 $t1="%ecx";
 164 # State information from SHA-1 context:
 165 $A="%edx";
 166 $B="%esi";
 167 $C="%edi";
 168 $D="%ebp";
 169 $E="%r11d";
 170 # Temporary:
 171 $T="%r12d";
 172 
 173 @V=($A,$B,$C,$D,$E,$T);
 174 
 175 sub PROLOGUE {
 176 my $func=shift;
 177 $code.=<<___;
 178 ENTRY_NP($func)
 179         /* EXPORT DELETE START */
 180         push    %rbx
 181         push    %rbp
 182         push    %r12
 183         mov     %rsp,%rax
 184         mov     %rdi,$ctx       # reassigned argument
 185         sub     \$`8+16*4`,%rsp
 186         mov     %rsi,$inp       # reassigned argument
 187         and     \$-64,%rsp
 188         mov     %rdx,$num       # reassigned argument
 189         mov     %rax,`16*4`(%rsp)
 190 
 191         mov     0($ctx),$A
 192         mov     4($ctx),$B
 193         mov     8($ctx),$C
 194         mov     12($ctx),$D
 195         mov     16($ctx),$E
 196 ___
 197 }
 198 
 199 sub EPILOGUE {
 200 my $func=shift;
 201 $code.=<<___;
 202         mov     `16*4`(%rsp),%rsp
 203         pop     %r12
 204         pop     %rbp
 205         pop     %rbx
 206         /* EXPORT DELETE END */
 207         ret
 208 SET_SIZE($func)
 209 ___
 210 }
 211 
 212 sub BODY_00_19 {
 213 my ($i,$a,$b,$c,$d,$e,$f,$host)=@_;
 214 my $j=$i+1;
 215 $code.=<<___ if ($i==0);
 216         mov     `4*$i`($inp),$xi        
 217         `"bswap $xi"    if(!defined($host))`
 218         mov     $xi,`4*$i`(%rsp)
 219 ___
 220         &lea_offset_eax_register_register("0x5a827999", $e, $f) if ($i < 15);
 221 $code.=<<___ if ($i<15);
 222         /lea    0x5a827999($xi,$e),$f
 223         mov     $c,$t0
 224         mov     `4*$j`($inp),$xi
 225         mov     $a,$e
 226         xor     $d,$t0


 303         mov     $b,$t1
 304         xor     `4*(($j+2)%16)`(%rsp),$xi
 305         mov     $a,$e
 306         and     $c,$t0
 307         xor     `4*(($j+8)%16)`(%rsp),$xi
 308         or      $c,$t1
 309         rol     \$5,$e
 310         xor     `4*(($j+13)%16)`(%rsp),$xi
 311         and     $d,$t1
 312         add     $e,$f
 313         rol     \$1,$xi
 314         or      $t1,$t0
 315         rol     \$30,$b
 316         mov     $xi,`4*($j%16)`(%rsp)
 317         add     $t0,$f
 318 ___
 319 }
 320 
 321 $code=<<___;
 322 #if !defined(lint) && !defined(__lint)
 323         .ident  "@(#)sha1-x86_64.pl     1.1     08/03/02 SMI"
 324 #include <sys/asm_linkage.h>
 325 ___
 326 
 327 
 328 &PROLOGUE("sha1_block_data_order");
 329 $code.=".align  4\n.Lloop:\n";
 330 for($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
 331 for(;$i<40;$i++)     { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
 332 for(;$i<60;$i++)     { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
 333 for(;$i<80;$i++)     { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
 334 $code.=<<___;
 335         / Update and save state information in SHA-1 context
 336         add     0($ctx),$E
 337         add     4($ctx),$T
 338         add     8($ctx),$A
 339         add     12($ctx),$B
 340         add     16($ctx),$C
 341         mov     $E,0($ctx)
 342         mov     $T,4($ctx)
 343         mov     $A,8($ctx)




 159 
 160 # Temporaries:
 161 $xi="%eax";
 162 $t0="%ebx";
 163 $t1="%ecx";
 164 # State information from SHA-1 context:
 165 $A="%edx";
 166 $B="%esi";
 167 $C="%edi";
 168 $D="%ebp";
 169 $E="%r11d";
 170 # Temporary:
 171 $T="%r12d";
 172 
 173 @V=($A,$B,$C,$D,$E,$T);
 174 
 175 sub PROLOGUE {
 176 my $func=shift;
 177 $code.=<<___;
 178 ENTRY_NP($func)

 179         push    %rbx
 180         push    %rbp
 181         push    %r12
 182         mov     %rsp,%rax
 183         mov     %rdi,$ctx       # reassigned argument
 184         sub     \$`8+16*4`,%rsp
 185         mov     %rsi,$inp       # reassigned argument
 186         and     \$-64,%rsp
 187         mov     %rdx,$num       # reassigned argument
 188         mov     %rax,`16*4`(%rsp)
 189 
 190         mov     0($ctx),$A
 191         mov     4($ctx),$B
 192         mov     8($ctx),$C
 193         mov     12($ctx),$D
 194         mov     16($ctx),$E
 195 ___
 196 }
 197 
 198 sub EPILOGUE {
 199 my $func=shift;
 200 $code.=<<___;
 201         mov     `16*4`(%rsp),%rsp
 202         pop     %r12
 203         pop     %rbp
 204         pop     %rbx

 205         ret
 206 SET_SIZE($func)
 207 ___
 208 }
 209 
 210 sub BODY_00_19 {
 211 my ($i,$a,$b,$c,$d,$e,$f,$host)=@_;
 212 my $j=$i+1;
 213 $code.=<<___ if ($i==0);
 214         mov     `4*$i`($inp),$xi        
 215         `"bswap $xi"    if(!defined($host))`
 216         mov     $xi,`4*$i`(%rsp)
 217 ___
 218         &lea_offset_eax_register_register("0x5a827999", $e, $f) if ($i < 15);
 219 $code.=<<___ if ($i<15);
 220         /lea    0x5a827999($xi,$e),$f
 221         mov     $c,$t0
 222         mov     `4*$j`($inp),$xi
 223         mov     $a,$e
 224         xor     $d,$t0


 301         mov     $b,$t1
 302         xor     `4*(($j+2)%16)`(%rsp),$xi
 303         mov     $a,$e
 304         and     $c,$t0
 305         xor     `4*(($j+8)%16)`(%rsp),$xi
 306         or      $c,$t1
 307         rol     \$5,$e
 308         xor     `4*(($j+13)%16)`(%rsp),$xi
 309         and     $d,$t1
 310         add     $e,$f
 311         rol     \$1,$xi
 312         or      $t1,$t0
 313         rol     \$30,$b
 314         mov     $xi,`4*($j%16)`(%rsp)
 315         add     $t0,$f
 316 ___
 317 }
 318 
 319 $code=<<___;
 320 #if !defined(lint) && !defined(__lint)
 321         .ident  "@(#)sha1-x86_64.pl     1.2     08/03/20 SMI"
 322 #include <sys/asm_linkage.h>
 323 ___
 324 
 325 
 326 &PROLOGUE("sha1_block_data_order");
 327 $code.=".align  4\n.Lloop:\n";
 328 for($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
 329 for(;$i<40;$i++)     { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
 330 for(;$i<60;$i++)     { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
 331 for(;$i<80;$i++)     { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
 332 $code.=<<___;
 333         / Update and save state information in SHA-1 context
 334         add     0($ctx),$E
 335         add     4($ctx),$T
 336         add     8($ctx),$A
 337         add     12($ctx),$B
 338         add     16($ctx),$C
 339         mov     $E,0($ctx)
 340         mov     $T,4($ctx)
 341         mov     $A,8($ctx)