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