84a00bf2afe80b04f5f6e96d16cbba19a7784594
[openssl.git] / crypto / sha / asm / sha1-armv8.pl
1 #! /usr/bin/env perl
2 # Copyright 2014-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 # SHA1 for ARMv8.
18 #
19 # Performance in cycles per processed byte and improvement coefficient
20 # over code generated with "default" compiler:
21 #
22 #               hardware-assisted       software(*)
23 # Apple A7      2.31                    4.13 (+14%)
24 # Cortex-A53    2.24                    8.03 (+97%)
25 # Cortex-A57    2.35                    7.88 (+74%)
26 # Denver        2.13                    3.97 (+0%)(**)
27 # X-Gene                                8.80 (+200%)
28 # Mongoose      2.05                    6.50 (+160%)
29 #
30 # (*)   Software results are presented mostly for reference purposes.
31 # (**)  Keep in mind that Denver relies on binary translation, which
32 #       optimizes compiler output at run-time.
33
34 $flavour = shift;
35 $output  = shift;
36
37 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
38 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
39 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
40 die "can't locate arm-xlate.pl";
41
42 open OUT,"| \"$^X\" $xlate $flavour $output";
43 *STDOUT=*OUT;
44
45 ($ctx,$inp,$num)=("x0","x1","x2");
46 @Xw=map("w$_",(3..17,19));
47 @Xx=map("x$_",(3..17,19));
48 @V=($A,$B,$C,$D,$E)=map("w$_",(20..24));
49 ($t0,$t1,$t2,$K)=map("w$_",(25..28));
50
51
52 sub BODY_00_19 {
53 my ($i,$a,$b,$c,$d,$e)=@_;
54 my $j=($i+2)&15;
55
56 $code.=<<___ if ($i<15 && !($i&1));
57         lsr     @Xx[$i+1],@Xx[$i],#32
58 ___
59 $code.=<<___ if ($i<14 && !($i&1));
60         ldr     @Xx[$i+2],[$inp,#`($i+2)*4-64`]
61 ___
62 $code.=<<___ if ($i<14 && ($i&1));
63 #ifdef  __ARMEB__
64         ror     @Xx[$i+1],@Xx[$i+1],#32
65 #else
66         rev32   @Xx[$i+1],@Xx[$i+1]
67 #endif
68 ___
69 $code.=<<___ if ($i<14);
70         bic     $t0,$d,$b
71         and     $t1,$c,$b
72         ror     $t2,$a,#27
73         add     $d,$d,$K                // future e+=K
74         orr     $t0,$t0,$t1
75         add     $e,$e,$t2               // e+=rot(a,5)
76         ror     $b,$b,#2
77         add     $d,$d,@Xw[($i+1)&15]    // future e+=X[i]
78         add     $e,$e,$t0               // e+=F(b,c,d)
79 ___
80 $code.=<<___ if ($i==19);
81         movz    $K,#0xeba1
82         movk    $K,#0x6ed9,lsl#16
83 ___
84 $code.=<<___ if ($i>=14);
85          eor    @Xw[$j],@Xw[$j],@Xw[($j+2)&15]
86         bic     $t0,$d,$b
87         and     $t1,$c,$b
88         ror     $t2,$a,#27
89          eor    @Xw[$j],@Xw[$j],@Xw[($j+8)&15]
90         add     $d,$d,$K                // future e+=K
91         orr     $t0,$t0,$t1
92         add     $e,$e,$t2               // e+=rot(a,5)
93          eor    @Xw[$j],@Xw[$j],@Xw[($j+13)&15]
94         ror     $b,$b,#2
95         add     $d,$d,@Xw[($i+1)&15]    // future e+=X[i]
96         add     $e,$e,$t0               // e+=F(b,c,d)
97          ror    @Xw[$j],@Xw[$j],#31
98 ___
99 }
100
101 sub BODY_40_59 {
102 my ($i,$a,$b,$c,$d,$e)=@_;
103 my $j=($i+2)&15;
104
105 $code.=<<___ if ($i==59);
106         movz    $K,#0xc1d6
107         movk    $K,#0xca62,lsl#16
108 ___
109 $code.=<<___;
110         orr     $t0,$b,$c
111         and     $t1,$b,$c
112          eor    @Xw[$j],@Xw[$j],@Xw[($j+2)&15]
113         ror     $t2,$a,#27
114         and     $t0,$t0,$d
115         add     $d,$d,$K                // future e+=K
116          eor    @Xw[$j],@Xw[$j],@Xw[($j+8)&15]
117         add     $e,$e,$t2               // e+=rot(a,5)
118         orr     $t0,$t0,$t1
119         ror     $b,$b,#2
120          eor    @Xw[$j],@Xw[$j],@Xw[($j+13)&15]
121         add     $d,$d,@Xw[($i+1)&15]    // future e+=X[i]
122         add     $e,$e,$t0               // e+=F(b,c,d)
123          ror    @Xw[$j],@Xw[$j],#31
124 ___
125 }
126
127 sub BODY_20_39 {
128 my ($i,$a,$b,$c,$d,$e)=@_;
129 my $j=($i+2)&15;
130
131 $code.=<<___ if ($i==39);
132         movz    $K,#0xbcdc
133         movk    $K,#0x8f1b,lsl#16
134 ___
135 $code.=<<___ if ($i<78);
136          eor    @Xw[$j],@Xw[$j],@Xw[($j+2)&15]
137         eor     $t0,$d,$b
138         ror     $t2,$a,#27
139         add     $d,$d,$K                // future e+=K
140          eor    @Xw[$j],@Xw[$j],@Xw[($j+8)&15]
141         eor     $t0,$t0,$c
142         add     $e,$e,$t2               // e+=rot(a,5)
143         ror     $b,$b,#2
144          eor    @Xw[$j],@Xw[$j],@Xw[($j+13)&15]
145         add     $d,$d,@Xw[($i+1)&15]    // future e+=X[i]
146         add     $e,$e,$t0               // e+=F(b,c,d)
147          ror    @Xw[$j],@Xw[$j],#31
148 ___
149 $code.=<<___ if ($i==78);
150         ldp     @Xw[1],@Xw[2],[$ctx]
151         eor     $t0,$d,$b
152         ror     $t2,$a,#27
153         add     $d,$d,$K                // future e+=K
154         eor     $t0,$t0,$c
155         add     $e,$e,$t2               // e+=rot(a,5)
156         ror     $b,$b,#2
157         add     $d,$d,@Xw[($i+1)&15]    // future e+=X[i]
158         add     $e,$e,$t0               // e+=F(b,c,d)
159 ___
160 $code.=<<___ if ($i==79);
161         ldp     @Xw[3],@Xw[4],[$ctx,#8]
162         eor     $t0,$d,$b
163         ror     $t2,$a,#27
164         eor     $t0,$t0,$c
165         add     $e,$e,$t2               // e+=rot(a,5)
166         ror     $b,$b,#2
167         ldr     @Xw[5],[$ctx,#16]
168         add     $e,$e,$t0               // e+=F(b,c,d)
169 ___
170 }
171
172 $code.=<<___;
173 #include "arm_arch.h"
174
175 .text
176
177 .extern OPENSSL_armcap_P
178 .globl  sha1_block_data_order
179 .type   sha1_block_data_order,%function
180 .align  6
181 sha1_block_data_order:
182 #ifdef  __ILP32__
183         ldrsw   x16,.LOPENSSL_armcap_P
184 #else
185         ldr     x16,.LOPENSSL_armcap_P
186 #endif
187         adr     x17,.LOPENSSL_armcap_P
188         add     x16,x16,x17
189         ldr     w16,[x16]
190         tst     w16,#ARMV8_SHA1
191         b.ne    .Lv8_entry
192
193         stp     x29,x30,[sp,#-96]!
194         add     x29,sp,#0
195         stp     x19,x20,[sp,#16]
196         stp     x21,x22,[sp,#32]
197         stp     x23,x24,[sp,#48]
198         stp     x25,x26,[sp,#64]
199         stp     x27,x28,[sp,#80]
200
201         ldp     $A,$B,[$ctx]
202         ldp     $C,$D,[$ctx,#8]
203         ldr     $E,[$ctx,#16]
204
205 .Loop:
206         ldr     @Xx[0],[$inp],#64
207         movz    $K,#0x7999
208         sub     $num,$num,#1
209         movk    $K,#0x5a82,lsl#16
210 #ifdef  __ARMEB__
211         ror     $Xx[0],@Xx[0],#32
212 #else
213         rev32   @Xx[0],@Xx[0]
214 #endif
215         add     $E,$E,$K                // warm it up
216         add     $E,$E,@Xw[0]
217 ___
218 for($i=0;$i<20;$i++)    { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
219 for(;$i<40;$i++)        { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
220 for(;$i<60;$i++)        { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
221 for(;$i<80;$i++)        { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
222 $code.=<<___;
223         add     $B,$B,@Xw[2]
224         add     $C,$C,@Xw[3]
225         add     $A,$A,@Xw[1]
226         add     $D,$D,@Xw[4]
227         add     $E,$E,@Xw[5]
228         stp     $A,$B,[$ctx]
229         stp     $C,$D,[$ctx,#8]
230         str     $E,[$ctx,#16]
231         cbnz    $num,.Loop
232
233         ldp     x19,x20,[sp,#16]
234         ldp     x21,x22,[sp,#32]
235         ldp     x23,x24,[sp,#48]
236         ldp     x25,x26,[sp,#64]
237         ldp     x27,x28,[sp,#80]
238         ldr     x29,[sp],#96
239         ret
240 .size   sha1_block_data_order,.-sha1_block_data_order
241 ___
242 {{{
243 my ($ABCD,$E,$E0,$E1)=map("v$_.16b",(0..3));
244 my @MSG=map("v$_.16b",(4..7));
245 my @Kxx=map("v$_.4s",(16..19));
246 my ($W0,$W1)=("v20.4s","v21.4s");
247 my $ABCD_SAVE="v22.16b";
248
249 $code.=<<___;
250 .type   sha1_block_armv8,%function
251 .align  6
252 sha1_block_armv8:
253 .Lv8_entry:
254         stp     x29,x30,[sp,#-16]!
255         add     x29,sp,#0
256
257         adr     x4,.Lconst
258         eor     $E,$E,$E
259         ld1.32  {$ABCD},[$ctx],#16
260         ld1.32  {$E}[0],[$ctx]
261         sub     $ctx,$ctx,#16
262         ld1.32  {@Kxx[0]-@Kxx[3]},[x4]
263
264 .Loop_hw:
265         ld1     {@MSG[0]-@MSG[3]},[$inp],#64
266         sub     $num,$num,#1
267         rev32   @MSG[0],@MSG[0]
268         rev32   @MSG[1],@MSG[1]
269
270         add.i32 $W0,@Kxx[0],@MSG[0]
271         rev32   @MSG[2],@MSG[2]
272         orr     $ABCD_SAVE,$ABCD,$ABCD  // offload
273
274         add.i32 $W1,@Kxx[0],@MSG[1]
275         rev32   @MSG[3],@MSG[3]
276         sha1h   $E1,$ABCD
277         sha1c   $ABCD,$E,$W0            // 0
278         add.i32 $W0,@Kxx[$j],@MSG[2]
279         sha1su0 @MSG[0],@MSG[1],@MSG[2]
280 ___
281 for ($j=0,$i=1;$i<20-3;$i++) {
282 my $f=("c","p","m","p")[$i/5];
283 $code.=<<___;
284         sha1h   $E0,$ABCD               // $i
285         sha1$f  $ABCD,$E1,$W1
286         add.i32 $W1,@Kxx[$j],@MSG[3]
287         sha1su1 @MSG[0],@MSG[3]
288 ___
289 $code.=<<___ if ($i<20-4);
290         sha1su0 @MSG[1],@MSG[2],@MSG[3]
291 ___
292         ($E0,$E1)=($E1,$E0);            ($W0,$W1)=($W1,$W0);
293         push(@MSG,shift(@MSG));         $j++ if ((($i+3)%5)==0);
294 }
295 $code.=<<___;
296         sha1h   $E0,$ABCD               // $i
297         sha1p   $ABCD,$E1,$W1
298         add.i32 $W1,@Kxx[$j],@MSG[3]
299
300         sha1h   $E1,$ABCD               // 18
301         sha1p   $ABCD,$E0,$W0
302
303         sha1h   $E0,$ABCD               // 19
304         sha1p   $ABCD,$E1,$W1
305
306         add.i32 $E,$E,$E0
307         add.i32 $ABCD,$ABCD,$ABCD_SAVE
308
309         cbnz    $num,.Loop_hw
310
311         st1.32  {$ABCD},[$ctx],#16
312         st1.32  {$E}[0],[$ctx]
313
314         ldr     x29,[sp],#16
315         ret
316 .size   sha1_block_armv8,.-sha1_block_armv8
317 .align  6
318 .Lconst:
319 .long   0x5a827999,0x5a827999,0x5a827999,0x5a827999     //K_00_19
320 .long   0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1     //K_20_39
321 .long   0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc     //K_40_59
322 .long   0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6     //K_60_79
323 .LOPENSSL_armcap_P:
324 #ifdef  __ILP32__
325 .long   OPENSSL_armcap_P-.
326 #else
327 .quad   OPENSSL_armcap_P-.
328 #endif
329 .asciz  "SHA1 block transform for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
330 .align  2
331 .comm   OPENSSL_armcap_P,4,4
332 ___
333 }}}
334
335 {   my  %opcode = (
336         "sha1c"         => 0x5e000000,  "sha1p"         => 0x5e001000,
337         "sha1m"         => 0x5e002000,  "sha1su0"       => 0x5e003000,
338         "sha1h"         => 0x5e280800,  "sha1su1"       => 0x5e281800   );
339
340     sub unsha1 {
341         my ($mnemonic,$arg)=@_;
342
343         $arg =~ m/[qv]([0-9]+)[^,]*,\s*[qv]([0-9]+)[^,]*(?:,\s*[qv]([0-9]+))?/o
344         &&
345         sprintf ".inst\t0x%08x\t//%s %s",
346                         $opcode{$mnemonic}|$1|($2<<5)|($3<<16),
347                         $mnemonic,$arg;
348     }
349 }
350
351 foreach(split("\n",$code)) {
352
353         s/\`([^\`]*)\`/eval($1)/geo;
354
355         s/\b(sha1\w+)\s+([qv].*)/unsha1($1,$2)/geo;
356
357         s/\.\w?32\b//o          and s/\.16b/\.4s/go;
358         m/(ld|st)1[^\[]+\[0\]/o and s/\.4s/\.s/go;
359
360         print $_,"\n";
361 }
362
363 close STDOUT;