2 # Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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 # ====================================================================
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.
21 # (*) this means that this module is inappropriate for PPC403? Does
22 # anybody know if pre-POWER3 can sustain unaligned load?
25 # ----------------------------------
26 # PPC970,gcc-4.0.0 +76% +59%
27 # Power6,xlc-7 +68% +33%
31 if ($flavour =~ /64/) {
38 } elsif ($flavour =~ /32/) {
45 } else { die "nonsense $flavour"; }
47 # Define endianness based on flavour
49 $LITTLE_ENDIAN = ($flavour=~/le$/) ? $SIZE_T : 0;
51 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
52 ( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
53 ( $xlate="${dir}../../perlasm/ppc-xlate.pl" and -f $xlate) or
54 die "can't locate ppc-xlate.pl";
56 open STDOUT,"| $^X $xlate $flavour ".shift || die "can't call $xlate: $!";
77 @V=($A,$B,$C,$D,$E,$T);
78 @X=("r16","r17","r18","r19","r20","r21","r22","r23",
79 "r24","r25","r26","r27","r28","r29","r30","r31");
82 my ($dst, $src, $temp_reg) = @_;
83 $code.=<<___ if (!$LITTLE_ENDIAN);
86 $code.=<<___ if ($LITTLE_ENDIAN);
88 rotlwi $dst,$temp_reg,8
89 rlwimi $dst,$temp_reg,24,0,7
90 rlwimi $dst,$temp_reg,24,16,23
95 my ($i,$a,$b,$c,$d,$e,$f)=@_;
98 # Since the last value of $f is discarded, we can use
99 # it as a temp reg to swap byte-order when needed.
100 loadbe("@X[$i]","`$i*4`($inp)",$f) if ($i==0);
101 loadbe("@X[$j]","`$j*4`($inp)",$f) if ($i<15);
102 $code.=<<___ if ($i<15);
113 $code.=<<___ if ($i>=15);
116 xor @X[$j%16],@X[$j%16],@X[($j+2)%16]
119 xor @X[$j%16],@X[$j%16],@X[($j+8)%16]
124 xor @X[$j%16],@X[$j%16],@X[($j+13)%16]
126 rotlwi @X[$j%16],@X[$j%16],1
131 my ($i,$a,$b,$c,$d,$e,$f)=@_;
133 $code.=<<___ if ($i<79);
137 xor @X[$j%16],@X[$j%16],@X[($j+2)%16]
140 xor @X[$j%16],@X[$j%16],@X[($j+8)%16]
143 xor @X[$j%16],@X[$j%16],@X[($j+13)%16]
145 rotlwi @X[$j%16],@X[$j%16],1
147 $code.=<<___ if ($i==79);
165 my ($i,$a,$b,$c,$d,$e,$f)=@_;
170 xor @X[$j%16],@X[$j%16],@X[($j+2)%16]
173 xor @X[$j%16],@X[$j%16],@X[($j+8)%16]
177 xor @X[$j%16],@X[$j%16],@X[($j+13)%16]
180 rotlwi @X[$j%16],@X[$j%16],1
189 .globl .sha1_block_data_order
191 .sha1_block_data_order:
192 $STU $sp,-$FRAME($sp)
194 $PUSH r15,`$FRAME-$SIZE_T*17`($sp)
195 $PUSH r16,`$FRAME-$SIZE_T*16`($sp)
196 $PUSH r17,`$FRAME-$SIZE_T*15`($sp)
197 $PUSH r18,`$FRAME-$SIZE_T*14`($sp)
198 $PUSH r19,`$FRAME-$SIZE_T*13`($sp)
199 $PUSH r20,`$FRAME-$SIZE_T*12`($sp)
200 $PUSH r21,`$FRAME-$SIZE_T*11`($sp)
201 $PUSH r22,`$FRAME-$SIZE_T*10`($sp)
202 $PUSH r23,`$FRAME-$SIZE_T*9`($sp)
203 $PUSH r24,`$FRAME-$SIZE_T*8`($sp)
204 $PUSH r25,`$FRAME-$SIZE_T*7`($sp)
205 $PUSH r26,`$FRAME-$SIZE_T*6`($sp)
206 $PUSH r27,`$FRAME-$SIZE_T*5`($sp)
207 $PUSH r28,`$FRAME-$SIZE_T*4`($sp)
208 $PUSH r29,`$FRAME-$SIZE_T*3`($sp)
209 $PUSH r30,`$FRAME-$SIZE_T*2`($sp)
210 $PUSH r31,`$FRAME-$SIZE_T*1`($sp)
211 $PUSH r0,`$FRAME+$LRSAVE`($sp)
221 bl Lsha1_block_private
224 ; PowerPC specification allows an implementation to be ill-behaved
225 ; upon unaligned access which crosses page boundary. "Better safe
226 ; than sorry" principle makes me treat it specially. But I don't
227 ; look for particular offending word, but rather for 64-byte input
228 ; block which crosses the boundary. Once found that block is aligned
229 ; and hashed separately...
233 andi. $t1,$t1,4095 ; distance to closest page boundary
234 srwi. $t1,$t1,6 ; t1/=64
237 ble Laligned ; didn't cross the page boundary
240 bl Lsha1_block_private
244 addi r20,$sp,$LOCALS ; spot within the frame
258 $PUSH $inp,`$FRAME-$SIZE_T*18`($sp)
260 addi $inp,$sp,$LOCALS
262 bl Lsha1_block_private
263 $POP $inp,`$FRAME-$SIZE_T*18`($sp)
268 $POP r0,`$FRAME+$LRSAVE`($sp)
269 $POP r15,`$FRAME-$SIZE_T*17`($sp)
270 $POP r16,`$FRAME-$SIZE_T*16`($sp)
271 $POP r17,`$FRAME-$SIZE_T*15`($sp)
272 $POP r18,`$FRAME-$SIZE_T*14`($sp)
273 $POP r19,`$FRAME-$SIZE_T*13`($sp)
274 $POP r20,`$FRAME-$SIZE_T*12`($sp)
275 $POP r21,`$FRAME-$SIZE_T*11`($sp)
276 $POP r22,`$FRAME-$SIZE_T*10`($sp)
277 $POP r23,`$FRAME-$SIZE_T*9`($sp)
278 $POP r24,`$FRAME-$SIZE_T*8`($sp)
279 $POP r25,`$FRAME-$SIZE_T*7`($sp)
280 $POP r26,`$FRAME-$SIZE_T*6`($sp)
281 $POP r27,`$FRAME-$SIZE_T*5`($sp)
282 $POP r28,`$FRAME-$SIZE_T*4`($sp)
283 $POP r29,`$FRAME-$SIZE_T*3`($sp)
284 $POP r30,`$FRAME-$SIZE_T*2`($sp)
285 $POP r31,`$FRAME-$SIZE_T*1`($sp)
290 .byte 0,12,4,1,0x80,18,3,0
294 # This is private block function, which uses tailored calling
295 # interface, namely upon entry SHA_CTX is pre-loaded to given
296 # registers and counter register contains amount of chunks to
302 $code.=<<___; # load K_00_19
306 for($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
307 $code.=<<___; # load K_20_39
311 for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
312 $code.=<<___; # load K_40_59
316 for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
317 $code.=<<___; # load K_60_79
321 for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
338 addi $inp,$inp,`16*4`
339 bdnz Lsha1_block_private
342 .byte 0,12,0x14,0,0,0,0,0
343 .size .sha1_block_data_order,.-.sha1_block_data_order
346 .asciz "SHA1 block transform for PPC, CRYPTOGAMS by <appro\@fy.chalmers.se>"
349 $code =~ s/\`([^\`]*)\`/eval $1/gem;