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