1#!/usr/bin/env perl 2 3# ==================================================================== 4# Written by Andy Polyakov <appro@openssl.org> 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# I let hardware handle unaligned input, except on page boundaries 11# (see below for details). Otherwise straightforward implementation 12# with X vector in register bank. 13 14# sha256 | sha512 15# -m64 -m32 | -m64 -m32 16# --------------------------------------+----------------------- 17# PPC970,gcc-4.0.0 +50% +38% | +40% +410%(*) 18# Power6,xlc-7 +150% +90% | +100% +430%(*) 19# 20# (*) 64-bit code in 32-bit application context, which actually is 21# on TODO list. It should be noted that for safe deployment in 22# 32-bit *mutli-threaded* context asyncronous signals should be 23# blocked upon entry to SHA512 block routine. This is because 24# 32-bit signaling procedure invalidates upper halves of GPRs. 25# Context switch procedure preserves them, but not signaling:-( 26 27# Second version is true multi-thread safe. Trouble with the original 28# version was that it was using thread local storage pointer register. 29# Well, it scrupulously preserved it, but the problem would arise the 30# moment asynchronous signal was delivered and signal handler would 31# dereference the TLS pointer. While it's never the case in openssl 32# application or test suite, we have to respect this scenario and not 33# use TLS pointer register. Alternative would be to require caller to 34# block signals prior calling this routine. For the record, in 32-bit 35# context R2 serves as TLS pointer, while in 64-bit context - R13. 36 37$flavour=shift; 38$output =shift; 39 40if ($flavour =~ /64/) { 41 $SIZE_T=8; 42 $LRSAVE=2*$SIZE_T; 43 $STU="stdu"; 44 $UCMP="cmpld"; 45 $SHL="sldi"; 46 $POP="ld"; 47 $PUSH="std"; 48} elsif ($flavour =~ /32/) { 49 $SIZE_T=4; 50 $LRSAVE=$SIZE_T; 51 $STU="stwu"; 52 $UCMP="cmplw"; 53 $SHL="slwi"; 54 $POP="lwz"; 55 $PUSH="stw"; 56} else { die "nonsense $flavour"; } 57 58$LITTLE_ENDIAN = ($flavour=~/le$/) ? $SIZE_T : 0; 59 60$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; 61( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or 62( $xlate="${dir}../../perlasm/ppc-xlate.pl" and -f $xlate) or 63die "can't locate ppc-xlate.pl"; 64 65open STDOUT,"| $^X $xlate $flavour $output" || die "can't call $xlate: $!"; 66 67if ($output =~ /512/) { 68 $func="sha512_block_ppc"; 69 $SZ=8; 70 @Sigma0=(28,34,39); 71 @Sigma1=(14,18,41); 72 @sigma0=(1, 8, 7); 73 @sigma1=(19,61, 6); 74 $rounds=80; 75 $LD="ld"; 76 $ST="std"; 77 $ROR="rotrdi"; 78 $SHR="srdi"; 79} else { 80 $func="sha256_block_ppc"; 81 $SZ=4; 82 @Sigma0=( 2,13,22); 83 @Sigma1=( 6,11,25); 84 @sigma0=( 7,18, 3); 85 @sigma1=(17,19,10); 86 $rounds=64; 87 $LD="lwz"; 88 $ST="stw"; 89 $ROR="rotrwi"; 90 $SHR="srwi"; 91} 92 93$FRAME=32*$SIZE_T+16*$SZ; 94$LOCALS=6*$SIZE_T; 95 96$sp ="r1"; 97$toc="r2"; 98$ctx="r3"; # zapped by $a0 99$inp="r4"; # zapped by $a1 100$num="r5"; # zapped by $t0 101 102$T ="r0"; 103$a0 ="r3"; 104$a1 ="r4"; 105$t0 ="r5"; 106$t1 ="r6"; 107$Tbl="r7"; 108 109$A ="r8"; 110$B ="r9"; 111$C ="r10"; 112$D ="r11"; 113$E ="r12"; 114$F =$t1; $t1 = "r0"; # stay away from "r13"; 115$G ="r14"; 116$H ="r15"; 117 118@V=($A,$B,$C,$D,$E,$F,$G,$H); 119@X=("r16","r17","r18","r19","r20","r21","r22","r23", 120 "r24","r25","r26","r27","r28","r29","r30","r31"); 121 122$inp="r31" if($SZ==4 || $SIZE_T==8); # reassigned $inp! aliases with @X[15] 123 124sub ROUND_00_15 { 125my ($i,$a,$b,$c,$d,$e,$f,$g,$h)=@_; 126$code.=<<___; 127 $ROR $a0,$e,$Sigma1[0] 128 $ROR $a1,$e,$Sigma1[1] 129 and $t0,$f,$e 130 xor $a0,$a0,$a1 131 add $h,$h,$t1 132 andc $t1,$g,$e 133 $ROR $a1,$a1,`$Sigma1[2]-$Sigma1[1]` 134 or $t0,$t0,$t1 ; Ch(e,f,g) 135 add $h,$h,@X[$i%16] 136 xor $a0,$a0,$a1 ; Sigma1(e) 137 add $h,$h,$t0 138 add $h,$h,$a0 139 140 $ROR $a0,$a,$Sigma0[0] 141 $ROR $a1,$a,$Sigma0[1] 142 and $t0,$a,$b 143 and $t1,$a,$c 144 xor $a0,$a0,$a1 145 $ROR $a1,$a1,`$Sigma0[2]-$Sigma0[1]` 146 xor $t0,$t0,$t1 147 and $t1,$b,$c 148 xor $a0,$a0,$a1 ; Sigma0(a) 149 add $d,$d,$h 150 xor $t0,$t0,$t1 ; Maj(a,b,c) 151___ 152$code.=<<___ if ($i<15); 153 $LD $t1,`($i+1)*$SZ`($Tbl) 154___ 155$code.=<<___; 156 add $h,$h,$a0 157 add $h,$h,$t0 158 159___ 160} 161 162sub ROUND_16_xx { 163my ($i,$a,$b,$c,$d,$e,$f,$g,$h)=@_; 164$i-=16; 165$code.=<<___; 166 $ROR $a0,@X[($i+1)%16],$sigma0[0] 167 $ROR $a1,@X[($i+1)%16],$sigma0[1] 168 $ROR $t0,@X[($i+14)%16],$sigma1[0] 169 $ROR $t1,@X[($i+14)%16],$sigma1[1] 170 xor $a0,$a0,$a1 171 $SHR $a1,@X[($i+1)%16],$sigma0[2] 172 xor $t0,$t0,$t1 173 $SHR $t1,@X[($i+14)%16],$sigma1[2] 174 add @X[$i],@X[$i],@X[($i+9)%16] 175 xor $a0,$a0,$a1 ; sigma0(X[(i+1)&0x0f]) 176 xor $t0,$t0,$t1 ; sigma1(X[(i+14)&0x0f]) 177 $LD $t1,`$i*$SZ`($Tbl) 178 add @X[$i],@X[$i],$a0 179 add @X[$i],@X[$i],$t0 180___ 181&ROUND_00_15($i+16,$a,$b,$c,$d,$e,$f,$g,$h); 182} 183 184$code=<<___; 185.machine "any" 186.text 187 188.globl $func 189.align 6 190$func: 191 $STU $sp,-$FRAME($sp) 192 mflr r0 193 $SHL $num,$num,`log(16*$SZ)/log(2)` 194 195 $PUSH $ctx,`$FRAME-$SIZE_T*22`($sp) 196 197 $PUSH r14,`$FRAME-$SIZE_T*18`($sp) 198 $PUSH r15,`$FRAME-$SIZE_T*17`($sp) 199 $PUSH r16,`$FRAME-$SIZE_T*16`($sp) 200 $PUSH r17,`$FRAME-$SIZE_T*15`($sp) 201 $PUSH r18,`$FRAME-$SIZE_T*14`($sp) 202 $PUSH r19,`$FRAME-$SIZE_T*13`($sp) 203 $PUSH r20,`$FRAME-$SIZE_T*12`($sp) 204 $PUSH r21,`$FRAME-$SIZE_T*11`($sp) 205 $PUSH r22,`$FRAME-$SIZE_T*10`($sp) 206 $PUSH r23,`$FRAME-$SIZE_T*9`($sp) 207 $PUSH r24,`$FRAME-$SIZE_T*8`($sp) 208 $PUSH r25,`$FRAME-$SIZE_T*7`($sp) 209 $PUSH r26,`$FRAME-$SIZE_T*6`($sp) 210 $PUSH r27,`$FRAME-$SIZE_T*5`($sp) 211 $PUSH r28,`$FRAME-$SIZE_T*4`($sp) 212 $PUSH r29,`$FRAME-$SIZE_T*3`($sp) 213 $PUSH r30,`$FRAME-$SIZE_T*2`($sp) 214 $PUSH r31,`$FRAME-$SIZE_T*1`($sp) 215 $PUSH r0,`$FRAME+$LRSAVE`($sp) 216___ 217 218if ($SZ==4 || $SIZE_T==8) { 219$code.=<<___; 220 $LD $A,`0*$SZ`($ctx) 221 mr $inp,r4 ; incarnate $inp 222 $LD $B,`1*$SZ`($ctx) 223 $LD $C,`2*$SZ`($ctx) 224 $LD $D,`3*$SZ`($ctx) 225 $LD $E,`4*$SZ`($ctx) 226 $LD $F,`5*$SZ`($ctx) 227 $LD $G,`6*$SZ`($ctx) 228 $LD $H,`7*$SZ`($ctx) 229___ 230} else { 231 for ($i=16;$i<32;$i++) { 232 $code.=<<___; 233 lwz r$i,`$LITTLE_ENDIAN^(4*($i-16))`($ctx) 234___ 235 } 236} 237 238$code.=<<___; 239 bl LPICmeup 240LPICedup: 241 andi. r0,$inp,3 242 bne Lunaligned 243Laligned: 244 add $num,$inp,$num 245 $PUSH $num,`$FRAME-$SIZE_T*24`($sp) ; end pointer 246 $PUSH $inp,`$FRAME-$SIZE_T*23`($sp) ; inp pointer 247 bl Lsha2_block_private 248 b Ldone 249 250; PowerPC specification allows an implementation to be ill-behaved 251; upon unaligned access which crosses page boundary. "Better safe 252; than sorry" principle makes me treat it specially. But I don't 253; look for particular offending word, but rather for the input 254; block which crosses the boundary. Once found that block is aligned 255; and hashed separately... 256.align 4 257Lunaligned: 258 subfic $t1,$inp,4096 259 andi. $t1,$t1,`4096-16*$SZ` ; distance to closest page boundary 260 beq Lcross_page 261 $UCMP $num,$t1 262 ble Laligned ; didn't cross the page boundary 263 subfc $num,$t1,$num 264 add $t1,$inp,$t1 265 $PUSH $num,`$FRAME-$SIZE_T*25`($sp) ; save real remaining num 266 $PUSH $t1,`$FRAME-$SIZE_T*24`($sp) ; intermediate end pointer 267 $PUSH $inp,`$FRAME-$SIZE_T*23`($sp) ; inp pointer 268 bl Lsha2_block_private 269 ; $inp equals to the intermediate end pointer here 270 $POP $num,`$FRAME-$SIZE_T*25`($sp) ; restore real remaining num 271Lcross_page: 272 li $t1,`16*$SZ/4` 273 mtctr $t1 274___ 275if ($SZ==4 || $SIZE_T==8) { 276$code.=<<___; 277 addi r20,$sp,$LOCALS ; aligned spot below the frame 278Lmemcpy: 279 lbz r16,0($inp) 280 lbz r17,1($inp) 281 lbz r18,2($inp) 282 lbz r19,3($inp) 283 addi $inp,$inp,4 284 stb r16,0(r20) 285 stb r17,1(r20) 286 stb r18,2(r20) 287 stb r19,3(r20) 288 addi r20,r20,4 289 bdnz Lmemcpy 290___ 291} else { 292$code.=<<___; 293 addi r12,$sp,$LOCALS ; aligned spot below the frame 294Lmemcpy: 295 lbz r8,0($inp) 296 lbz r9,1($inp) 297 lbz r10,2($inp) 298 lbz r11,3($inp) 299 addi $inp,$inp,4 300 stb r8,0(r12) 301 stb r9,1(r12) 302 stb r10,2(r12) 303 stb r11,3(r12) 304 addi r12,r12,4 305 bdnz Lmemcpy 306___ 307} 308 309$code.=<<___; 310 $PUSH $inp,`$FRAME-$SIZE_T*26`($sp) ; save real inp 311 addi $t1,$sp,`$LOCALS+16*$SZ` ; fictitious end pointer 312 addi $inp,$sp,$LOCALS ; fictitious inp pointer 313 $PUSH $num,`$FRAME-$SIZE_T*25`($sp) ; save real num 314 $PUSH $t1,`$FRAME-$SIZE_T*24`($sp) ; end pointer 315 $PUSH $inp,`$FRAME-$SIZE_T*23`($sp) ; inp pointer 316 bl Lsha2_block_private 317 $POP $inp,`$FRAME-$SIZE_T*26`($sp) ; restore real inp 318 $POP $num,`$FRAME-$SIZE_T*25`($sp) ; restore real num 319 addic. $num,$num,`-16*$SZ` ; num-- 320 bne Lunaligned 321 322Ldone: 323 $POP r0,`$FRAME+$LRSAVE`($sp) 324 $POP r14,`$FRAME-$SIZE_T*18`($sp) 325 $POP r15,`$FRAME-$SIZE_T*17`($sp) 326 $POP r16,`$FRAME-$SIZE_T*16`($sp) 327 $POP r17,`$FRAME-$SIZE_T*15`($sp) 328 $POP r18,`$FRAME-$SIZE_T*14`($sp) 329 $POP r19,`$FRAME-$SIZE_T*13`($sp) 330 $POP r20,`$FRAME-$SIZE_T*12`($sp) 331 $POP r21,`$FRAME-$SIZE_T*11`($sp) 332 $POP r22,`$FRAME-$SIZE_T*10`($sp) 333 $POP r23,`$FRAME-$SIZE_T*9`($sp) 334 $POP r24,`$FRAME-$SIZE_T*8`($sp) 335 $POP r25,`$FRAME-$SIZE_T*7`($sp) 336 $POP r26,`$FRAME-$SIZE_T*6`($sp) 337 $POP r27,`$FRAME-$SIZE_T*5`($sp) 338 $POP r28,`$FRAME-$SIZE_T*4`($sp) 339 $POP r29,`$FRAME-$SIZE_T*3`($sp) 340 $POP r30,`$FRAME-$SIZE_T*2`($sp) 341 $POP r31,`$FRAME-$SIZE_T*1`($sp) 342 mtlr r0 343 addi $sp,$sp,$FRAME 344 blr 345 .long 0 346 .byte 0,12,4,1,0x80,18,3,0 347 .long 0 348___ 349 350if ($SZ==4 || $SIZE_T==8) { 351$code.=<<___; 352.align 4 353Lsha2_block_private: 354 $LD $t1,0($Tbl) 355___ 356for($i=0;$i<16;$i++) { 357$code.=<<___ if ($SZ==4 && !$LITTLE_ENDIAN); 358 lwz @X[$i],`$i*$SZ`($inp) 359___ 360$code.=<<___ if ($SZ==4 && $LITTLE_ENDIAN); 361 lwz $a0,`$i*$SZ`($inp) 362 rotlwi @X[$i],$a0,8 363 rlwimi @X[$i],$a0,24,0,7 364 rlwimi @X[$i],$a0,24,16,23 365___ 366# 64-bit loads are split to 2x32-bit ones, as CPU can't handle 367# unaligned 64-bit loads, only 32-bit ones... 368$code.=<<___ if ($SZ==8 && !$LITTLE_ENDIAN); 369 lwz $t0,`$i*$SZ`($inp) 370 lwz @X[$i],`$i*$SZ+4`($inp) 371 insrdi @X[$i],$t0,32,0 372___ 373$code.=<<___ if ($SZ==8 && $LITTLE_ENDIAN); 374 lwz $a0,`$i*$SZ`($inp) 375 lwz $a1,`$i*$SZ+4`($inp) 376 rotlwi $t0,$a0,8 377 rotlwi @X[$i],$a1,8 378 rlwimi $t0,$a0,24,0,7 379 rlwimi @X[$i],$a1,24,0,7 380 rlwimi $t0,$a0,24,16,23 381 rlwimi @X[$i],$a1,24,16,23 382 insrdi @X[$i],$t0,32,0 383___ 384 &ROUND_00_15($i,@V); 385 unshift(@V,pop(@V)); 386} 387$code.=<<___; 388 li $t0,`$rounds/16-1` 389 mtctr $t0 390.align 4 391Lrounds: 392 addi $Tbl,$Tbl,`16*$SZ` 393___ 394for(;$i<32;$i++) { 395 &ROUND_16_xx($i,@V); 396 unshift(@V,pop(@V)); 397} 398$code.=<<___; 399 bdnz Lrounds 400 401 $POP $ctx,`$FRAME-$SIZE_T*22`($sp) 402 $POP $inp,`$FRAME-$SIZE_T*23`($sp) ; inp pointer 403 $POP $num,`$FRAME-$SIZE_T*24`($sp) ; end pointer 404 subi $Tbl,$Tbl,`($rounds-16)*$SZ` ; rewind Tbl 405 406 $LD r16,`0*$SZ`($ctx) 407 $LD r17,`1*$SZ`($ctx) 408 $LD r18,`2*$SZ`($ctx) 409 $LD r19,`3*$SZ`($ctx) 410 $LD r20,`4*$SZ`($ctx) 411 $LD r21,`5*$SZ`($ctx) 412 $LD r22,`6*$SZ`($ctx) 413 addi $inp,$inp,`16*$SZ` ; advance inp 414 $LD r23,`7*$SZ`($ctx) 415 add $A,$A,r16 416 add $B,$B,r17 417 $PUSH $inp,`$FRAME-$SIZE_T*23`($sp) 418 add $C,$C,r18 419 $ST $A,`0*$SZ`($ctx) 420 add $D,$D,r19 421 $ST $B,`1*$SZ`($ctx) 422 add $E,$E,r20 423 $ST $C,`2*$SZ`($ctx) 424 add $F,$F,r21 425 $ST $D,`3*$SZ`($ctx) 426 add $G,$G,r22 427 $ST $E,`4*$SZ`($ctx) 428 add $H,$H,r23 429 $ST $F,`5*$SZ`($ctx) 430 $ST $G,`6*$SZ`($ctx) 431 $UCMP $inp,$num 432 $ST $H,`7*$SZ`($ctx) 433 bne Lsha2_block_private 434 blr 435 .long 0 436 .byte 0,12,0x14,0,0,0,0,0 437.size $func,.-$func 438___ 439} else { 440######################################################################## 441# SHA512 for PPC32, X vector is off-loaded to stack... 442# 443# | sha512 444# | -m32 445# ----------------------+----------------------- 446# PPC74x0,gcc-4.0.1 | +48% 447# POWER6,gcc-4.4.6 | +124%(*) 448# POWER7,gcc-4.4.6 | +79%(*) 449# e300,gcc-4.1.0 | +167% 450# 451# (*) ~1/3 of -m64 result [and ~20% better than -m32 code generated 452# by xlc-12.1] 453 454my $XOFF=$LOCALS; 455 456my @V=map("r$_",(16..31)); # A..H 457 458my ($s0,$s1,$t0,$t1,$t2,$t3,$a0,$a1,$a2,$a3)=map("r$_",(0,5,6,8..12,14,15)); 459my ($x0,$x1)=("r3","r4"); # zaps $ctx and $inp 460 461sub ROUND_00_15_ppc32 { 462my ($i, $ahi,$alo,$bhi,$blo,$chi,$clo,$dhi,$dlo, 463 $ehi,$elo,$fhi,$flo,$ghi,$glo,$hhi,$hlo)=@_; 464 465$code.=<<___; 466 lwz $t2,`$SZ*($i%16)+($LITTLE_ENDIAN^4)`($Tbl) 467 xor $a0,$flo,$glo 468 lwz $t3,`$SZ*($i%16)+($LITTLE_ENDIAN^0)`($Tbl) 469 xor $a1,$fhi,$ghi 470 addc $hlo,$hlo,$t0 ; h+=x[i] 471 stw $t0,`$XOFF+0+$SZ*($i%16)`($sp) ; save x[i] 472 473 srwi $s0,$elo,$Sigma1[0] 474 srwi $s1,$ehi,$Sigma1[0] 475 and $a0,$a0,$elo 476 adde $hhi,$hhi,$t1 477 and $a1,$a1,$ehi 478 stw $t1,`$XOFF+4+$SZ*($i%16)`($sp) 479 srwi $t0,$elo,$Sigma1[1] 480 srwi $t1,$ehi,$Sigma1[1] 481 addc $hlo,$hlo,$t2 ; h+=K512[i] 482 insrwi $s0,$ehi,$Sigma1[0],0 483 insrwi $s1,$elo,$Sigma1[0],0 484 xor $a0,$a0,$glo ; Ch(e,f,g) 485 adde $hhi,$hhi,$t3 486 xor $a1,$a1,$ghi 487 insrwi $t0,$ehi,$Sigma1[1],0 488 insrwi $t1,$elo,$Sigma1[1],0 489 addc $hlo,$hlo,$a0 ; h+=Ch(e,f,g) 490 srwi $t2,$ehi,$Sigma1[2]-32 491 srwi $t3,$elo,$Sigma1[2]-32 492 xor $s0,$s0,$t0 493 xor $s1,$s1,$t1 494 insrwi $t2,$elo,$Sigma1[2]-32,0 495 insrwi $t3,$ehi,$Sigma1[2]-32,0 496 xor $a0,$alo,$blo ; a^b, b^c in next round 497 adde $hhi,$hhi,$a1 498 xor $a1,$ahi,$bhi 499 xor $s0,$s0,$t2 ; Sigma1(e) 500 xor $s1,$s1,$t3 501 502 srwi $t0,$alo,$Sigma0[0] 503 and $a2,$a2,$a0 504 addc $hlo,$hlo,$s0 ; h+=Sigma1(e) 505 and $a3,$a3,$a1 506 srwi $t1,$ahi,$Sigma0[0] 507 srwi $s0,$ahi,$Sigma0[1]-32 508 adde $hhi,$hhi,$s1 509 srwi $s1,$alo,$Sigma0[1]-32 510 insrwi $t0,$ahi,$Sigma0[0],0 511 insrwi $t1,$alo,$Sigma0[0],0 512 xor $a2,$a2,$blo ; Maj(a,b,c) 513 addc $dlo,$dlo,$hlo ; d+=h 514 xor $a3,$a3,$bhi 515 insrwi $s0,$alo,$Sigma0[1]-32,0 516 insrwi $s1,$ahi,$Sigma0[1]-32,0 517 adde $dhi,$dhi,$hhi 518 srwi $t2,$ahi,$Sigma0[2]-32 519 srwi $t3,$alo,$Sigma0[2]-32 520 xor $s0,$s0,$t0 521 addc $hlo,$hlo,$a2 ; h+=Maj(a,b,c) 522 xor $s1,$s1,$t1 523 insrwi $t2,$alo,$Sigma0[2]-32,0 524 insrwi $t3,$ahi,$Sigma0[2]-32,0 525 adde $hhi,$hhi,$a3 526___ 527$code.=<<___ if ($i>=15); 528 lwz $t0,`$XOFF+0+$SZ*(($i+2)%16)`($sp) 529 lwz $t1,`$XOFF+4+$SZ*(($i+2)%16)`($sp) 530___ 531$code.=<<___ if ($i<15 && !$LITTLE_ENDIAN); 532 lwz $t1,`$SZ*($i+1)+0`($inp) 533 lwz $t0,`$SZ*($i+1)+4`($inp) 534___ 535$code.=<<___ if ($i<15 && $LITTLE_ENDIAN); 536 lwz $a2,`$SZ*($i+1)+0`($inp) 537 lwz $a3,`$SZ*($i+1)+4`($inp) 538 rotlwi $t1,$a2,8 539 rotlwi $t0,$a3,8 540 rlwimi $t1,$a2,24,0,7 541 rlwimi $t0,$a3,24,0,7 542 rlwimi $t1,$a2,24,16,23 543 rlwimi $t0,$a3,24,16,23 544___ 545$code.=<<___; 546 xor $s0,$s0,$t2 ; Sigma0(a) 547 xor $s1,$s1,$t3 548 addc $hlo,$hlo,$s0 ; h+=Sigma0(a) 549 adde $hhi,$hhi,$s1 550___ 551$code.=<<___ if ($i==15); 552 lwz $x0,`$XOFF+0+$SZ*(($i+1)%16)`($sp) 553 lwz $x1,`$XOFF+4+$SZ*(($i+1)%16)`($sp) 554___ 555} 556sub ROUND_16_xx_ppc32 { 557my ($i, $ahi,$alo,$bhi,$blo,$chi,$clo,$dhi,$dlo, 558 $ehi,$elo,$fhi,$flo,$ghi,$glo,$hhi,$hlo)=@_; 559 560$code.=<<___; 561 srwi $s0,$t0,$sigma0[0] 562 srwi $s1,$t1,$sigma0[0] 563 srwi $t2,$t0,$sigma0[1] 564 srwi $t3,$t1,$sigma0[1] 565 insrwi $s0,$t1,$sigma0[0],0 566 insrwi $s1,$t0,$sigma0[0],0 567 srwi $a0,$t0,$sigma0[2] 568 insrwi $t2,$t1,$sigma0[1],0 569 insrwi $t3,$t0,$sigma0[1],0 570 insrwi $a0,$t1,$sigma0[2],0 571 xor $s0,$s0,$t2 572 lwz $t2,`$XOFF+0+$SZ*(($i+14)%16)`($sp) 573 srwi $a1,$t1,$sigma0[2] 574 xor $s1,$s1,$t3 575 lwz $t3,`$XOFF+4+$SZ*(($i+14)%16)`($sp) 576 xor $a0,$a0,$s0 577 srwi $s0,$t2,$sigma1[0] 578 xor $a1,$a1,$s1 579 srwi $s1,$t3,$sigma1[0] 580 addc $x0,$x0,$a0 ; x[i]+=sigma0(x[i+1]) 581 srwi $a0,$t3,$sigma1[1]-32 582 insrwi $s0,$t3,$sigma1[0],0 583 insrwi $s1,$t2,$sigma1[0],0 584 adde $x1,$x1,$a1 585 srwi $a1,$t2,$sigma1[1]-32 586 587 insrwi $a0,$t2,$sigma1[1]-32,0 588 srwi $t2,$t2,$sigma1[2] 589 insrwi $a1,$t3,$sigma1[1]-32,0 590 insrwi $t2,$t3,$sigma1[2],0 591 xor $s0,$s0,$a0 592 lwz $a0,`$XOFF+0+$SZ*(($i+9)%16)`($sp) 593 srwi $t3,$t3,$sigma1[2] 594 xor $s1,$s1,$a1 595 lwz $a1,`$XOFF+4+$SZ*(($i+9)%16)`($sp) 596 xor $s0,$s0,$t2 597 addc $x0,$x0,$a0 ; x[i]+=x[i+9] 598 xor $s1,$s1,$t3 599 adde $x1,$x1,$a1 600 addc $x0,$x0,$s0 ; x[i]+=sigma1(x[i+14]) 601 adde $x1,$x1,$s1 602___ 603 ($t0,$t1,$x0,$x1) = ($x0,$x1,$t0,$t1); 604 &ROUND_00_15_ppc32(@_); 605} 606 607$code.=<<___; 608.align 4 609Lsha2_block_private: 610___ 611$code.=<<___ if (!$LITTLE_ENDIAN); 612 lwz $t1,0($inp) 613 xor $a2,@V[3],@V[5] ; B^C, magic seed 614 lwz $t0,4($inp) 615 xor $a3,@V[2],@V[4] 616___ 617$code.=<<___ if ($LITTLE_ENDIAN); 618 lwz $a1,0($inp) 619 xor $a2,@V[3],@V[5] ; B^C, magic seed 620 lwz $a0,4($inp) 621 xor $a3,@V[2],@V[4] 622 rotlwi $t1,$a1,8 623 rotlwi $t0,$a0,8 624 rlwimi $t1,$a1,24,0,7 625 rlwimi $t0,$a0,24,0,7 626 rlwimi $t1,$a1,24,16,23 627 rlwimi $t0,$a0,24,16,23 628___ 629for($i=0;$i<16;$i++) { 630 &ROUND_00_15_ppc32($i,@V); 631 unshift(@V,pop(@V)); unshift(@V,pop(@V)); 632 ($a0,$a1,$a2,$a3) = ($a2,$a3,$a0,$a1); 633} 634$code.=<<___; 635 li $a0,`$rounds/16-1` 636 mtctr $a0 637.align 4 638Lrounds: 639 addi $Tbl,$Tbl,`16*$SZ` 640___ 641for(;$i<32;$i++) { 642 &ROUND_16_xx_ppc32($i,@V); 643 unshift(@V,pop(@V)); unshift(@V,pop(@V)); 644 ($a0,$a1,$a2,$a3) = ($a2,$a3,$a0,$a1); 645} 646$code.=<<___; 647 bdnz Lrounds 648 649 $POP $ctx,`$FRAME-$SIZE_T*22`($sp) 650 $POP $inp,`$FRAME-$SIZE_T*23`($sp) ; inp pointer 651 $POP $num,`$FRAME-$SIZE_T*24`($sp) ; end pointer 652 subi $Tbl,$Tbl,`($rounds-16)*$SZ` ; rewind Tbl 653 654 lwz $t0,`$LITTLE_ENDIAN^0`($ctx) 655 lwz $t1,`$LITTLE_ENDIAN^4`($ctx) 656 lwz $t2,`$LITTLE_ENDIAN^8`($ctx) 657 lwz $t3,`$LITTLE_ENDIAN^12`($ctx) 658 lwz $a0,`$LITTLE_ENDIAN^16`($ctx) 659 lwz $a1,`$LITTLE_ENDIAN^20`($ctx) 660 lwz $a2,`$LITTLE_ENDIAN^24`($ctx) 661 addc @V[1],@V[1],$t1 662 lwz $a3,`$LITTLE_ENDIAN^28`($ctx) 663 adde @V[0],@V[0],$t0 664 lwz $t0,`$LITTLE_ENDIAN^32`($ctx) 665 addc @V[3],@V[3],$t3 666 lwz $t1,`$LITTLE_ENDIAN^36`($ctx) 667 adde @V[2],@V[2],$t2 668 lwz $t2,`$LITTLE_ENDIAN^40`($ctx) 669 addc @V[5],@V[5],$a1 670 lwz $t3,`$LITTLE_ENDIAN^44`($ctx) 671 adde @V[4],@V[4],$a0 672 lwz $a0,`$LITTLE_ENDIAN^48`($ctx) 673 addc @V[7],@V[7],$a3 674 lwz $a1,`$LITTLE_ENDIAN^52`($ctx) 675 adde @V[6],@V[6],$a2 676 lwz $a2,`$LITTLE_ENDIAN^56`($ctx) 677 addc @V[9],@V[9],$t1 678 lwz $a3,`$LITTLE_ENDIAN^60`($ctx) 679 adde @V[8],@V[8],$t0 680 stw @V[0],`$LITTLE_ENDIAN^0`($ctx) 681 stw @V[1],`$LITTLE_ENDIAN^4`($ctx) 682 addc @V[11],@V[11],$t3 683 stw @V[2],`$LITTLE_ENDIAN^8`($ctx) 684 stw @V[3],`$LITTLE_ENDIAN^12`($ctx) 685 adde @V[10],@V[10],$t2 686 stw @V[4],`$LITTLE_ENDIAN^16`($ctx) 687 stw @V[5],`$LITTLE_ENDIAN^20`($ctx) 688 addc @V[13],@V[13],$a1 689 stw @V[6],`$LITTLE_ENDIAN^24`($ctx) 690 stw @V[7],`$LITTLE_ENDIAN^28`($ctx) 691 adde @V[12],@V[12],$a0 692 stw @V[8],`$LITTLE_ENDIAN^32`($ctx) 693 stw @V[9],`$LITTLE_ENDIAN^36`($ctx) 694 addc @V[15],@V[15],$a3 695 stw @V[10],`$LITTLE_ENDIAN^40`($ctx) 696 stw @V[11],`$LITTLE_ENDIAN^44`($ctx) 697 adde @V[14],@V[14],$a2 698 stw @V[12],`$LITTLE_ENDIAN^48`($ctx) 699 stw @V[13],`$LITTLE_ENDIAN^52`($ctx) 700 stw @V[14],`$LITTLE_ENDIAN^56`($ctx) 701 stw @V[15],`$LITTLE_ENDIAN^60`($ctx) 702 703 addi $inp,$inp,`16*$SZ` ; advance inp 704 $PUSH $inp,`$FRAME-$SIZE_T*23`($sp) 705 $UCMP $inp,$num 706 bne Lsha2_block_private 707 blr 708 .long 0 709 .byte 0,12,0x14,0,0,0,0,0 710.size $func,.-$func 711___ 712} 713 714# Ugly hack here, because PPC assembler syntax seem to vary too 715# much from platforms to platform... 716$code.=<<___; 717.align 6 718LPICmeup: 719 mflr r0 720 bcl 20,31,\$+4 721 mflr $Tbl ; vvvvvv "distance" between . and 1st data entry 722 addi $Tbl,$Tbl,`64-8` 723 mtlr r0 724 blr 725 .long 0 726 .byte 0,12,0x14,0,0,0,0,0 727 .space `64-9*4` 728___ 729$code.=<<___ if ($SZ==8); 730 .quad 0x428a2f98d728ae22,0x7137449123ef65cd 731 .quad 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc 732 .quad 0x3956c25bf348b538,0x59f111f1b605d019 733 .quad 0x923f82a4af194f9b,0xab1c5ed5da6d8118 734 .quad 0xd807aa98a3030242,0x12835b0145706fbe 735 .quad 0x243185be4ee4b28c,0x550c7dc3d5ffb4e2 736 .quad 0x72be5d74f27b896f,0x80deb1fe3b1696b1 737 .quad 0x9bdc06a725c71235,0xc19bf174cf692694 738 .quad 0xe49b69c19ef14ad2,0xefbe4786384f25e3 739 .quad 0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65 740 .quad 0x2de92c6f592b0275,0x4a7484aa6ea6e483 741 .quad 0x5cb0a9dcbd41fbd4,0x76f988da831153b5 742 .quad 0x983e5152ee66dfab,0xa831c66d2db43210 743 .quad 0xb00327c898fb213f,0xbf597fc7beef0ee4 744 .quad 0xc6e00bf33da88fc2,0xd5a79147930aa725 745 .quad 0x06ca6351e003826f,0x142929670a0e6e70 746 .quad 0x27b70a8546d22ffc,0x2e1b21385c26c926 747 .quad 0x4d2c6dfc5ac42aed,0x53380d139d95b3df 748 .quad 0x650a73548baf63de,0x766a0abb3c77b2a8 749 .quad 0x81c2c92e47edaee6,0x92722c851482353b 750 .quad 0xa2bfe8a14cf10364,0xa81a664bbc423001 751 .quad 0xc24b8b70d0f89791,0xc76c51a30654be30 752 .quad 0xd192e819d6ef5218,0xd69906245565a910 753 .quad 0xf40e35855771202a,0x106aa07032bbd1b8 754 .quad 0x19a4c116b8d2d0c8,0x1e376c085141ab53 755 .quad 0x2748774cdf8eeb99,0x34b0bcb5e19b48a8 756 .quad 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb 757 .quad 0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3 758 .quad 0x748f82ee5defb2fc,0x78a5636f43172f60 759 .quad 0x84c87814a1f0ab72,0x8cc702081a6439ec 760 .quad 0x90befffa23631e28,0xa4506cebde82bde9 761 .quad 0xbef9a3f7b2c67915,0xc67178f2e372532b 762 .quad 0xca273eceea26619c,0xd186b8c721c0c207 763 .quad 0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178 764 .quad 0x06f067aa72176fba,0x0a637dc5a2c898a6 765 .quad 0x113f9804bef90dae,0x1b710b35131c471b 766 .quad 0x28db77f523047d84,0x32caab7b40c72493 767 .quad 0x3c9ebe0a15c9bebc,0x431d67c49c100d4c 768 .quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a 769 .quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817 770___ 771$code.=<<___ if ($SZ==4); 772 .long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 773 .long 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5 774 .long 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3 775 .long 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174 776 .long 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc 777 .long 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da 778 .long 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7 779 .long 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967 780 .long 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13 781 .long 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85 782 .long 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3 783 .long 0xd192e819,0xd6990624,0xf40e3585,0x106aa070 784 .long 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5 785 .long 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3 786 .long 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208 787 .long 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 788___ 789 790$code =~ s/\`([^\`]*)\`/eval $1/gem; 791print $code; 792close STDOUT; 793