0ec4b0bcc36b030194133cda3bc631c1a180b869
[openssl.git] / crypto / sha / asm / sha512-ppc.pl
1 #!/usr/bin/env perl
2
3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> 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. The module is big-endian [which is
13 # not big deal as there're no little-endian targets left around].
14
15 #                       sha256          |       sha512
16 #                       -m64    -m32    |       -m64    -m32
17 # --------------------------------------+-----------------------
18 # PPC970,gcc-4.0.0      +50%    +38%    |       +40%    +410%(*)
19 # Power6,xlc-7          +150%   +90%    |       +100%   +430%(*)
20 #
21 # (*)   64-bit code in 32-bit application context, which actually is
22 #       on TODO list. It should be noted that for safe deployment in
23 #       32-bit *mutli-threaded* context asyncronous signals should be
24 #       blocked upon entry to SHA512 block routine. This is because
25 #       32-bit signaling procedure invalidates upper halves of GPRs.
26 #       Context switch procedure preserves them, but not signaling:-(
27
28 # Second version is true multi-thread safe. Trouble with the original
29 # version was that it was using thread local storage pointer register.
30 # Well, it scrupulously preserved it, but the problem would arise the
31 # moment asynchronous signal was delivered and signal handler would
32 # dereference the TLS pointer. While it's never the case in openssl
33 # application or test suite, we have to respect this scenario and not
34 # use TLS pointer register. Alternative would be to require caller to
35 # block signals prior calling this routine. For the record, in 32-bit
36 # context R2 serves as TLS pointer, while in 64-bit context - R13.
37
38 $output=shift;
39
40 if ($output =~ /64/) {
41         $SIZE_T=8;
42         $STU="stdu";
43         $UCMP="cmpld";
44         $SHL="sldi";
45         $POP="ld";
46         $PUSH="std";
47 } elsif ($output =~ /32/) {
48         $SIZE_T=4;
49         $STU="stwu";
50         $UCMP="cmplw";
51         $SHL="slwi";
52         $POP="lwz";
53         $PUSH="stw";
54 } else { die "nonsense $output"; }
55
56 ( defined shift || open STDOUT,"| $^X ../perlasm/ppc-xlate.pl $output" ) ||
57         die "can't call ../perlasm/ppc-xlate.pl: $!";
58
59 if ($output =~ /512/) {
60         $func="sha512_block_data_order";
61         $SZ=8;
62         @Sigma0=(28,34,39);
63         @Sigma1=(14,18,41);
64         @sigma0=(1,  8, 7);
65         @sigma1=(19,61, 6);
66         $rounds=80;
67         $LD="ld";
68         $ST="std";
69         $ROR="rotrdi";
70         $SHR="srdi";
71 } else {
72         $func="sha256_block_data_order";
73         $SZ=4;
74         @Sigma0=( 2,13,22);
75         @Sigma1=( 6,11,25);
76         @sigma0=( 7,18, 3);
77         @sigma1=(17,19,10);
78         $rounds=64;
79         $LD="lwz";
80         $ST="stw";
81         $ROR="rotrwi";
82         $SHR="srwi";
83 }
84
85 $FRAME=32*$SIZE_T;
86
87 $sp ="r1";
88 $toc="r2";
89 $ctx="r3";      # zapped by $a0
90 $inp="r4";      # zapped by $a1
91 $num="r5";      # zapped by $t0
92
93 $T  ="r0";
94 $a0 ="r3";
95 $a1 ="r4";
96 $t0 ="r5";
97 $t1 ="r6";
98 $Tbl="r7";
99
100 $A  ="r8";
101 $B  ="r9";
102 $C  ="r10";
103 $D  ="r11";
104 $E  ="r12";
105 $F  ="r13";     $F="r2" if ($SIZE_T==8);# reassigned to exempt TLS pointer
106 $G  ="r14";
107 $H  ="r15";
108
109 @V=($A,$B,$C,$D,$E,$F,$G,$H);
110 @X=("r16","r17","r18","r19","r20","r21","r22","r23",
111     "r24","r25","r26","r27","r28","r29","r30","r31");
112
113 $inp="r31";     # reassigned $inp! aliases with @X[15]
114
115 sub ROUND_00_15 {
116 my ($i,$a,$b,$c,$d,$e,$f,$g,$h)=@_;
117 $code.=<<___;
118         $LD     $T,`$i*$SZ`($Tbl)
119         $ROR    $a0,$e,$Sigma1[0]
120         $ROR    $a1,$e,$Sigma1[1]
121         and     $t0,$f,$e
122         andc    $t1,$g,$e
123         add     $T,$T,$h
124         xor     $a0,$a0,$a1
125         $ROR    $a1,$a1,`$Sigma1[2]-$Sigma1[1]`
126         or      $t0,$t0,$t1             ; Ch(e,f,g)
127         add     $T,$T,@X[$i]
128         xor     $a0,$a0,$a1             ; Sigma1(e)
129         add     $T,$T,$t0
130         add     $T,$T,$a0
131
132         $ROR    $a0,$a,$Sigma0[0]
133         $ROR    $a1,$a,$Sigma0[1]
134         and     $t0,$a,$b
135         and     $t1,$a,$c
136         xor     $a0,$a0,$a1
137         $ROR    $a1,$a1,`$Sigma0[2]-$Sigma0[1]`
138         xor     $t0,$t0,$t1
139         and     $t1,$b,$c
140         xor     $a0,$a0,$a1             ; Sigma0(a)
141         add     $d,$d,$T
142         xor     $t0,$t0,$t1             ; Maj(a,b,c)
143         add     $h,$T,$a0
144         add     $h,$h,$t0
145
146 ___
147 }
148
149 sub ROUND_16_xx {
150 my ($i,$a,$b,$c,$d,$e,$f,$g,$h)=@_;
151 $i-=16;
152 $code.=<<___;
153         $ROR    $a0,@X[($i+1)%16],$sigma0[0]
154         $ROR    $a1,@X[($i+1)%16],$sigma0[1]
155         $ROR    $t0,@X[($i+14)%16],$sigma1[0]
156         $ROR    $t1,@X[($i+14)%16],$sigma1[1]
157         xor     $a0,$a0,$a1
158         $SHR    $a1,@X[($i+1)%16],$sigma0[2]
159         xor     $t0,$t0,$t1
160         $SHR    $t1,@X[($i+14)%16],$sigma1[2]
161         add     @X[$i],@X[$i],@X[($i+9)%16]
162         xor     $a0,$a0,$a1             ; sigma0(X[(i+1)&0x0f])
163         xor     $t0,$t0,$t1             ; sigma1(X[(i+14)&0x0f])
164         add     @X[$i],@X[$i],$a0
165         add     @X[$i],@X[$i],$t0
166 ___
167 &ROUND_00_15($i,$a,$b,$c,$d,$e,$f,$g,$h);
168 }
169
170 $code=<<___;
171 .text
172
173 .globl  $func
174 .align  6
175 $func:
176         mflr    r0
177         $STU    $sp,`-($FRAME+16*$SZ)`($sp)
178         $SHL    $num,$num,`log(16*$SZ)/log(2)`
179
180         $PUSH   $ctx,`$FRAME-$SIZE_T*22`($sp)
181
182         $PUSH   r0,`$FRAME-$SIZE_T*21`($sp)
183         $PUSH   $toc,`$FRAME-$SIZE_T*20`($sp)
184         $PUSH   r13,`$FRAME-$SIZE_T*19`($sp)
185         $PUSH   r14,`$FRAME-$SIZE_T*18`($sp)
186         $PUSH   r15,`$FRAME-$SIZE_T*17`($sp)
187         $PUSH   r16,`$FRAME-$SIZE_T*16`($sp)
188         $PUSH   r17,`$FRAME-$SIZE_T*15`($sp)
189         $PUSH   r18,`$FRAME-$SIZE_T*14`($sp)
190         $PUSH   r19,`$FRAME-$SIZE_T*13`($sp)
191         $PUSH   r20,`$FRAME-$SIZE_T*12`($sp)
192         $PUSH   r21,`$FRAME-$SIZE_T*11`($sp)
193         $PUSH   r22,`$FRAME-$SIZE_T*10`($sp)
194         $PUSH   r23,`$FRAME-$SIZE_T*9`($sp)
195         $PUSH   r24,`$FRAME-$SIZE_T*8`($sp)
196         $PUSH   r25,`$FRAME-$SIZE_T*7`($sp)
197         $PUSH   r26,`$FRAME-$SIZE_T*6`($sp)
198         $PUSH   r27,`$FRAME-$SIZE_T*5`($sp)
199         $PUSH   r28,`$FRAME-$SIZE_T*4`($sp)
200         $PUSH   r29,`$FRAME-$SIZE_T*3`($sp)
201         $PUSH   r30,`$FRAME-$SIZE_T*2`($sp)
202         $PUSH   r31,`$FRAME-$SIZE_T*1`($sp)
203
204         $LD     $A,`0*$SZ`($ctx)
205         mr      $inp,r4                         ; incarnate $inp
206         $LD     $B,`1*$SZ`($ctx)
207         $LD     $C,`2*$SZ`($ctx)
208         $LD     $D,`3*$SZ`($ctx)
209         $LD     $E,`4*$SZ`($ctx)
210         $LD     $F,`5*$SZ`($ctx)
211         $LD     $G,`6*$SZ`($ctx)
212         $LD     $H,`7*$SZ`($ctx)
213
214         b       LPICmeup
215 LPICedup:
216         andi.   r0,$inp,3
217         bne     Lunaligned
218 Laligned:
219         add     $num,$inp,$num
220         $PUSH   $num,`$FRAME-$SIZE_T*24`($sp)   ; end pointer
221         $PUSH   $inp,`$FRAME-$SIZE_T*23`($sp)   ; inp pointer
222         bl      Lsha2_block_private
223 Ldone:
224         $POP    r0,`$FRAME-$SIZE_T*21`($sp)
225         $POP    $toc,`$FRAME-$SIZE_T*20`($sp)
226         $POP    r13,`$FRAME-$SIZE_T*19`($sp)
227         $POP    r14,`$FRAME-$SIZE_T*18`($sp)
228         $POP    r15,`$FRAME-$SIZE_T*17`($sp)
229         $POP    r16,`$FRAME-$SIZE_T*16`($sp)
230         $POP    r17,`$FRAME-$SIZE_T*15`($sp)
231         $POP    r18,`$FRAME-$SIZE_T*14`($sp)
232         $POP    r19,`$FRAME-$SIZE_T*13`($sp)
233         $POP    r20,`$FRAME-$SIZE_T*12`($sp)
234         $POP    r21,`$FRAME-$SIZE_T*11`($sp)
235         $POP    r22,`$FRAME-$SIZE_T*10`($sp)
236         $POP    r23,`$FRAME-$SIZE_T*9`($sp)
237         $POP    r24,`$FRAME-$SIZE_T*8`($sp)
238         $POP    r25,`$FRAME-$SIZE_T*7`($sp)
239         $POP    r26,`$FRAME-$SIZE_T*6`($sp)
240         $POP    r27,`$FRAME-$SIZE_T*5`($sp)
241         $POP    r28,`$FRAME-$SIZE_T*4`($sp)
242         $POP    r29,`$FRAME-$SIZE_T*3`($sp)
243         $POP    r30,`$FRAME-$SIZE_T*2`($sp)
244         $POP    r31,`$FRAME-$SIZE_T*1`($sp)
245         mtlr    r0
246         addi    $sp,$sp,`$FRAME+16*$SZ`
247         blr
248 ___
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 $code.=<<___;
257 .align  4
258 Lunaligned:
259         subfic  $t1,$inp,4096
260         andi.   $t1,$t1,`4096-16*$SZ`   ; distance to closest page boundary
261         beq     Lcross_page
262         $UCMP   $num,$t1
263         ble-    Laligned                ; didn't cross the page boundary
264         subfc   $num,$t1,$num
265         add     $t1,$inp,$t1
266         $PUSH   $num,`$FRAME-$SIZE_T*25`($sp)   ; save real remaining num
267         $PUSH   $t1,`$FRAME-$SIZE_T*24`($sp)    ; intermediate end pointer
268         $PUSH   $inp,`$FRAME-$SIZE_T*23`($sp)   ; inp pointer
269         bl      Lsha2_block_private
270         ; $inp equals to the intermediate end pointer here
271         $POP    $num,`$FRAME-$SIZE_T*25`($sp)   ; restore real remaining num
272 Lcross_page:
273         li      $t1,`16*$SZ/4`
274         mtctr   $t1
275         addi    r20,$sp,$FRAME                  ; aligned spot below the frame
276 Lmemcpy:
277         lbz     r16,0($inp)
278         lbz     r17,1($inp)
279         lbz     r18,2($inp)
280         lbz     r19,3($inp)
281         addi    $inp,$inp,4
282         stb     r16,0(r20)
283         stb     r17,1(r20)
284         stb     r18,2(r20)
285         stb     r19,3(r20)
286         addi    r20,r20,4
287         bdnz    Lmemcpy
288
289         $PUSH   $inp,`$FRAME-$SIZE_T*26`($sp)   ; save real inp
290         addi    $t1,$sp,`$FRAME+16*$SZ`         ; fictitious end pointer
291         addi    $inp,$sp,$FRAME                 ; fictitious inp pointer
292         $PUSH   $num,`$FRAME-$SIZE_T*25`($sp)   ; save real num
293         $PUSH   $t1,`$FRAME-$SIZE_T*24`($sp)    ; end pointer
294         $PUSH   $inp,`$FRAME-$SIZE_T*23`($sp)   ; inp pointer
295         bl      Lsha2_block_private
296         $POP    $inp,`$FRAME-$SIZE_T*26`($sp)   ; restore real inp
297         $POP    $num,`$FRAME-$SIZE_T*25`($sp)   ; restore real num
298         addic.  $num,$num,`-16*$SZ`             ; num--
299         bne-    Lunaligned
300         b       Ldone
301 ___
302
303 $code.=<<___;
304 .align  4
305 Lsha2_block_private:
306 ___
307 for($i=0;$i<16;$i++) {
308 $code.=<<___ if ($SZ==4);
309         lwz     @X[$i],`$i*$SZ`($inp)
310 ___
311 # 64-bit loads are split to 2x32-bit ones, as CPU can't handle
312 # unaligned 64-bit loads, only 32-bit ones...
313 $code.=<<___ if ($SZ==8);
314         lwz     $t0,`$i*$SZ`($inp)
315         lwz     @X[$i],`$i*$SZ+4`($inp)
316         insrdi  @X[$i],$t0,32,0
317 ___
318         &ROUND_00_15($i,@V);
319         unshift(@V,pop(@V));
320 }
321 $code.=<<___;
322         li      $T,`$rounds/16-1`
323         mtctr   $T
324 .align  4
325 Lrounds:
326         addi    $Tbl,$Tbl,`16*$SZ`
327 ___
328 for(;$i<32;$i++) {
329         &ROUND_16_xx($i,@V);
330         unshift(@V,pop(@V));
331 }
332 $code.=<<___;
333         bdnz-   Lrounds
334
335         $POP    $ctx,`$FRAME-$SIZE_T*22`($sp)
336         $POP    $inp,`$FRAME-$SIZE_T*23`($sp)   ; inp pointer
337         $POP    $num,`$FRAME-$SIZE_T*24`($sp)   ; end pointer
338         subi    $Tbl,$Tbl,`($rounds-16)*$SZ`    ; rewind Tbl
339
340         $LD     r16,`0*$SZ`($ctx)
341         $LD     r17,`1*$SZ`($ctx)
342         $LD     r18,`2*$SZ`($ctx)
343         $LD     r19,`3*$SZ`($ctx)
344         $LD     r20,`4*$SZ`($ctx)
345         $LD     r21,`5*$SZ`($ctx)
346         $LD     r22,`6*$SZ`($ctx)
347         addi    $inp,$inp,`16*$SZ`              ; advance inp
348         $LD     r23,`7*$SZ`($ctx)
349         add     $A,$A,r16
350         add     $B,$B,r17
351         $PUSH   $inp,`$FRAME-$SIZE_T*23`($sp)
352         add     $C,$C,r18
353         $ST     $A,`0*$SZ`($ctx)
354         add     $D,$D,r19
355         $ST     $B,`1*$SZ`($ctx)
356         add     $E,$E,r20
357         $ST     $C,`2*$SZ`($ctx)
358         add     $F,$F,r21
359         $ST     $D,`3*$SZ`($ctx)
360         add     $G,$G,r22
361         $ST     $E,`4*$SZ`($ctx)
362         add     $H,$H,r23
363         $ST     $F,`5*$SZ`($ctx)
364         $ST     $G,`6*$SZ`($ctx)
365         $UCMP   $inp,$num
366         $ST     $H,`7*$SZ`($ctx)
367         bne     Lsha2_block_private
368         blr
369 ___
370
371 # Ugly hack here, because PPC assembler syntax seem to vary too
372 # much from platforms to platform...
373 $code.=<<___;
374 .align  6
375 LPICmeup:
376         bl      LPIC
377         addi    $Tbl,$Tbl,`64-4`        ; "distance" between . and last nop
378         b       LPICedup
379         nop
380         nop
381         nop
382         nop
383         nop
384 LPIC:   mflr    $Tbl
385         blr
386         nop
387         nop
388         nop
389         nop
390         nop
391         nop
392 ___
393 $code.=<<___ if ($SZ==8);
394         .long   0x428a2f98,0xd728ae22,0x71374491,0x23ef65cd
395         .long   0xb5c0fbcf,0xec4d3b2f,0xe9b5dba5,0x8189dbbc
396         .long   0x3956c25b,0xf348b538,0x59f111f1,0xb605d019
397         .long   0x923f82a4,0xaf194f9b,0xab1c5ed5,0xda6d8118
398         .long   0xd807aa98,0xa3030242,0x12835b01,0x45706fbe
399         .long   0x243185be,0x4ee4b28c,0x550c7dc3,0xd5ffb4e2
400         .long   0x72be5d74,0xf27b896f,0x80deb1fe,0x3b1696b1
401         .long   0x9bdc06a7,0x25c71235,0xc19bf174,0xcf692694
402         .long   0xe49b69c1,0x9ef14ad2,0xefbe4786,0x384f25e3
403         .long   0x0fc19dc6,0x8b8cd5b5,0x240ca1cc,0x77ac9c65
404         .long   0x2de92c6f,0x592b0275,0x4a7484aa,0x6ea6e483
405         .long   0x5cb0a9dc,0xbd41fbd4,0x76f988da,0x831153b5
406         .long   0x983e5152,0xee66dfab,0xa831c66d,0x2db43210
407         .long   0xb00327c8,0x98fb213f,0xbf597fc7,0xbeef0ee4
408         .long   0xc6e00bf3,0x3da88fc2,0xd5a79147,0x930aa725
409         .long   0x06ca6351,0xe003826f,0x14292967,0x0a0e6e70
410         .long   0x27b70a85,0x46d22ffc,0x2e1b2138,0x5c26c926
411         .long   0x4d2c6dfc,0x5ac42aed,0x53380d13,0x9d95b3df
412         .long   0x650a7354,0x8baf63de,0x766a0abb,0x3c77b2a8
413         .long   0x81c2c92e,0x47edaee6,0x92722c85,0x1482353b
414         .long   0xa2bfe8a1,0x4cf10364,0xa81a664b,0xbc423001
415         .long   0xc24b8b70,0xd0f89791,0xc76c51a3,0x0654be30
416         .long   0xd192e819,0xd6ef5218,0xd6990624,0x5565a910
417         .long   0xf40e3585,0x5771202a,0x106aa070,0x32bbd1b8
418         .long   0x19a4c116,0xb8d2d0c8,0x1e376c08,0x5141ab53
419         .long   0x2748774c,0xdf8eeb99,0x34b0bcb5,0xe19b48a8
420         .long   0x391c0cb3,0xc5c95a63,0x4ed8aa4a,0xe3418acb
421         .long   0x5b9cca4f,0x7763e373,0x682e6ff3,0xd6b2b8a3
422         .long   0x748f82ee,0x5defb2fc,0x78a5636f,0x43172f60
423         .long   0x84c87814,0xa1f0ab72,0x8cc70208,0x1a6439ec
424         .long   0x90befffa,0x23631e28,0xa4506ceb,0xde82bde9
425         .long   0xbef9a3f7,0xb2c67915,0xc67178f2,0xe372532b
426         .long   0xca273ece,0xea26619c,0xd186b8c7,0x21c0c207
427         .long   0xeada7dd6,0xcde0eb1e,0xf57d4f7f,0xee6ed178
428         .long   0x06f067aa,0x72176fba,0x0a637dc5,0xa2c898a6
429         .long   0x113f9804,0xbef90dae,0x1b710b35,0x131c471b
430         .long   0x28db77f5,0x23047d84,0x32caab7b,0x40c72493
431         .long   0x3c9ebe0a,0x15c9bebc,0x431d67c4,0x9c100d4c
432         .long   0x4cc5d4be,0xcb3e42b6,0x597f299c,0xfc657e2a
433         .long   0x5fcb6fab,0x3ad6faec,0x6c44198c,0x4a475817
434 ___
435 $code.=<<___ if ($SZ==4);
436         .long   0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
437         .long   0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
438         .long   0xd807aa98,0x12835b01,0x243185be,0x550c7dc3
439         .long   0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174
440         .long   0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc
441         .long   0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da
442         .long   0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7
443         .long   0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967
444         .long   0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13
445         .long   0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85
446         .long   0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3
447         .long   0xd192e819,0xd6990624,0xf40e3585,0x106aa070
448         .long   0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5
449         .long   0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3
450         .long   0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
451         .long   0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
452 ___
453
454 $code =~ s/\`([^\`]*)\`/eval $1/gem;
455 print $code;
456 close STDOUT;