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