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