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