| 1 |
#! /usr/bin/env perl |
| 2 |
# Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# https://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
|
| 17 |
# ==================================================================== |
| 18 |
# Written by Andy Polyakov <[email protected]> for the OpenSSL |
| 19 |
# project. |
| 20 |
# ==================================================================== |
| 21 |
|
| 22 |
# SHA256 block procedure for ARMv4. May 2007. |
| 23 |
|
| 24 |
# Performance is ~2x better than gcc 3.4 generated code and in "abso- |
| 25 |
# lute" terms is ~2250 cycles per 64-byte block or ~35 cycles per |
| 26 |
# byte [on single-issue Xscale PXA250 core]. |
| 27 |
|
| 28 |
# July 2010. |
| 29 |
# |
| 30 |
# Rescheduling for dual-issue pipeline resulted in 22% improvement on |
| 31 |
# Cortex A8 core and ~20 cycles per processed byte. |
| 32 |
|
| 33 |
# February 2011. |
| 34 |
# |
| 35 |
# Profiler-assisted and platform-specific optimization resulted in 16% |
| 36 |
# improvement on Cortex A8 core and ~15.4 cycles per processed byte. |
| 37 |
|
| 38 |
# September 2013. |
| 39 |
# |
| 40 |
# Add NEON implementation. On Cortex A8 it was measured to process one |
| 41 |
# byte in 12.5 cycles or 23% faster than integer-only code. Snapdragon |
| 42 |
# S4 does it in 12.5 cycles too, but it's 50% faster than integer-only |
| 43 |
# code (meaning that latter performs sub-optimally, nothing was done |
| 44 |
# about it). |
| 45 |
|
| 46 |
# May 2014. |
| 47 |
# |
| 48 |
# Add ARMv8 code path performing at 2.0 cpb on Apple A7. |
| 49 |
|
| 50 |
$flavour = shift; |
| 51 |
if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; } |
| 52 |
else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} } |
| 53 |
|
| 54 |
if ($flavour && $flavour ne "void") { |
| 55 |
$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; |
| 56 |
( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or |
| 57 |
( $xlate="${dir}../../../perlasm/arm-xlate.pl" and -f $xlate) or |
| 58 |
die "can't locate arm-xlate.pl"; |
| 59 |
|
| 60 |
open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""; |
| 61 |
*STDOUT=*OUT; |
| 62 |
} else { |
| 63 |
open OUT,">$output"; |
| 64 |
*STDOUT=*OUT; |
| 65 |
} |
| 66 |
|
| 67 |
$ctx="r0"; $t0="r0"; |
| 68 |
$inp="r1"; $t4="r1"; |
| 69 |
$len="r2"; $t1="r2"; |
| 70 |
$T1="r3"; $t3="r3"; |
| 71 |
$A="r4"; |
| 72 |
$B="r5"; |
| 73 |
$C="r6"; |
| 74 |
$D="r7"; |
| 75 |
$E="r8"; |
| 76 |
$F="r9"; |
| 77 |
$G="r10"; |
| 78 |
$H="r11"; |
| 79 |
@V=($A,$B,$C,$D,$E,$F,$G,$H); |
| 80 |
$t2="r12"; |
| 81 |
$Ktbl="r14"; |
| 82 |
|
| 83 |
@Sigma0=( 2,13,22); |
| 84 |
@Sigma1=( 6,11,25); |
| 85 |
@sigma0=( 7,18, 3); |
| 86 |
@sigma1=(17,19,10); |
| 87 |
|
| 88 |
sub BODY_00_15 { |
| 89 |
my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_; |
| 90 |
|
| 91 |
$code.=<<___ if ($i<16); |
| 92 |
#if __ARM_ARCH>=7 |
| 93 |
@ ldr $t1,[$inp],#4 @ $i |
| 94 |
# if $i==15 |
| 95 |
str $inp,[sp,#17*4] @ make room for $t4 |
| 96 |
# endif |
| 97 |
eor $t0,$e,$e,ror#`$Sigma1[1]-$Sigma1[0]` |
| 98 |
add $a,$a,$t2 @ h+=Maj(a,b,c) from the past |
| 99 |
eor $t0,$t0,$e,ror#`$Sigma1[2]-$Sigma1[0]` @ Sigma1(e) |
| 100 |
# ifndef __ARMEB__ |
| 101 |
rev $t1,$t1 |
| 102 |
# endif |
| 103 |
#else |
| 104 |
@ ldrb $t1,[$inp,#3] @ $i |
| 105 |
add $a,$a,$t2 @ h+=Maj(a,b,c) from the past |
| 106 |
ldrb $t2,[$inp,#2] |
| 107 |
ldrb $t0,[$inp,#1] |
| 108 |
orr $t1,$t1,$t2,lsl#8 |
| 109 |
ldrb $t2,[$inp],#4 |
| 110 |
orr $t1,$t1,$t0,lsl#16 |
| 111 |
# if $i==15 |
| 112 |
str $inp,[sp,#17*4] @ make room for $t4 |
| 113 |
# endif |
| 114 |
eor $t0,$e,$e,ror#`$Sigma1[1]-$Sigma1[0]` |
| 115 |
orr $t1,$t1,$t2,lsl#24 |
| 116 |
eor $t0,$t0,$e,ror#`$Sigma1[2]-$Sigma1[0]` @ Sigma1(e) |
| 117 |
#endif |
| 118 |
___ |
| 119 |
$code.=<<___; |
| 120 |
ldr $t2,[$Ktbl],#4 @ *K256++ |
| 121 |
add $h,$h,$t1 @ h+=X[i] |
| 122 |
str $t1,[sp,#`$i%16`*4] |
| 123 |
eor $t1,$f,$g |
| 124 |
add $h,$h,$t0,ror#$Sigma1[0] @ h+=Sigma1(e) |
| 125 |
and $t1,$t1,$e |
| 126 |
add $h,$h,$t2 @ h+=K256[i] |
| 127 |
eor $t1,$t1,$g @ Ch(e,f,g) |
| 128 |
eor $t0,$a,$a,ror#`$Sigma0[1]-$Sigma0[0]` |
| 129 |
add $h,$h,$t1 @ h+=Ch(e,f,g) |
| 130 |
#if $i==31 |
| 131 |
and $t2,$t2,#0xff |
| 132 |
cmp $t2,#0xf2 @ done? |
| 133 |
#endif |
| 134 |
#if $i<15 |
| 135 |
# if __ARM_ARCH>=7 |
| 136 |
ldr $t1,[$inp],#4 @ prefetch |
| 137 |
# else |
| 138 |
ldrb $t1,[$inp,#3] |
| 139 |
# endif |
| 140 |
eor $t2,$a,$b @ a^b, b^c in next round |
| 141 |
#else |
| 142 |
ldr $t1,[sp,#`($i+2)%16`*4] @ from future BODY_16_xx |
| 143 |
eor $t2,$a,$b @ a^b, b^c in next round |
| 144 |
ldr $t4,[sp,#`($i+15)%16`*4] @ from future BODY_16_xx |
| 145 |
#endif |
| 146 |
eor $t0,$t0,$a,ror#`$Sigma0[2]-$Sigma0[0]` @ Sigma0(a) |
| 147 |
and $t3,$t3,$t2 @ (b^c)&=(a^b) |
| 148 |
add $d,$d,$h @ d+=h |
| 149 |
eor $t3,$t3,$b @ Maj(a,b,c) |
| 150 |
add $h,$h,$t0,ror#$Sigma0[0] @ h+=Sigma0(a) |
| 151 |
@ add $h,$h,$t3 @ h+=Maj(a,b,c) |
| 152 |
___ |
| 153 |
($t2,$t3)=($t3,$t2); |
| 154 |
} |
| 155 |
|
| 156 |
sub BODY_16_XX { |
| 157 |
my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_; |
| 158 |
|
| 159 |
$code.=<<___; |
| 160 |
@ ldr $t1,[sp,#`($i+1)%16`*4] @ $i |
| 161 |
@ ldr $t4,[sp,#`($i+14)%16`*4] |
| 162 |
mov $t0,$t1,ror#$sigma0[0] |
| 163 |
add $a,$a,$t2 @ h+=Maj(a,b,c) from the past |
| 164 |
mov $t2,$t4,ror#$sigma1[0] |
| 165 |
eor $t0,$t0,$t1,ror#$sigma0[1] |
| 166 |
eor $t2,$t2,$t4,ror#$sigma1[1] |
| 167 |
eor $t0,$t0,$t1,lsr#$sigma0[2] @ sigma0(X[i+1]) |
| 168 |
ldr $t1,[sp,#`($i+0)%16`*4] |
| 169 |
eor $t2,$t2,$t4,lsr#$sigma1[2] @ sigma1(X[i+14]) |
| 170 |
ldr $t4,[sp,#`($i+9)%16`*4] |
| 171 |
|
| 172 |
add $t2,$t2,$t0 |
| 173 |
eor $t0,$e,$e,ror#`$Sigma1[1]-$Sigma1[0]` @ from BODY_00_15 |
| 174 |
add $t1,$t1,$t2 |
| 175 |
eor $t0,$t0,$e,ror#`$Sigma1[2]-$Sigma1[0]` @ Sigma1(e) |
| 176 |
add $t1,$t1,$t4 @ X[i] |
| 177 |
___ |
| 178 |
&BODY_00_15(@_); |
| 179 |
} |
| 180 |
|
| 181 |
$code=<<___; |
| 182 |
#ifdef __KERNEL__ |
| 183 |
# define __ARM_ARCH __LINUX_ARM_ARCH__ |
| 184 |
# define __ARM_MAX_ARCH__ 7 |
| 185 |
#endif |
| 186 |
|
| 187 |
@ Silence ARMv8 deprecated IT instruction warnings. This file is used by both |
| 188 |
@ ARMv7 and ARMv8 processors. It does have ARMv8-only code, but those |
| 189 |
@ instructions are manually-encoded. (See unsha256.) |
| 190 |
.arch armv7-a |
| 191 |
|
| 192 |
.text |
| 193 |
#if defined(__thumb2__) |
| 194 |
.syntax unified |
| 195 |
.thumb |
| 196 |
#else |
| 197 |
.code 32 |
| 198 |
#endif |
| 199 |
|
| 200 |
.type K256,%object |
| 201 |
.align 5 |
| 202 |
K256: |
| 203 |
.word 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 |
| 204 |
.word 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5 |
| 205 |
.word 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3 |
| 206 |
.word 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174 |
| 207 |
.word 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc |
| 208 |
.word 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da |
| 209 |
.word 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7 |
| 210 |
.word 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967 |
| 211 |
.word 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13 |
| 212 |
.word 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85 |
| 213 |
.word 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3 |
| 214 |
.word 0xd192e819,0xd6990624,0xf40e3585,0x106aa070 |
| 215 |
.word 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5 |
| 216 |
.word 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3 |
| 217 |
.word 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208 |
| 218 |
.word 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 |
| 219 |
.size K256,.-K256 |
| 220 |
.word 0 @ terminator |
| 221 |
.align 5 |
| 222 |
|
| 223 |
.global sha256_block_data_order_nohw |
| 224 |
.type sha256_block_data_order_nohw,%function |
| 225 |
sha256_block_data_order_nohw: |
| 226 |
add $len,$inp,$len,lsl#6 @ len to point at the end of inp |
| 227 |
stmdb sp!,{$ctx,$inp,$len,r4-r11,lr} |
| 228 |
ldmia $ctx,{$A,$B,$C,$D,$E,$F,$G,$H} |
| 229 |
adr $Ktbl,K256 |
| 230 |
sub sp,sp,#16*4 @ alloca(X[16]) |
| 231 |
.Loop: |
| 232 |
# if __ARM_ARCH>=7 |
| 233 |
ldr $t1,[$inp],#4 |
| 234 |
# else |
| 235 |
ldrb $t1,[$inp,#3] |
| 236 |
# endif |
| 237 |
eor $t3,$B,$C @ magic |
| 238 |
eor $t2,$t2,$t2 |
| 239 |
___ |
| 240 |
for($i=0;$i<16;$i++) { &BODY_00_15($i,@V); unshift(@V,pop(@V)); } |
| 241 |
$code.=".Lrounds_16_xx:\n"; |
| 242 |
for (;$i<32;$i++) { &BODY_16_XX($i,@V); unshift(@V,pop(@V)); } |
| 243 |
$code.=<<___; |
| 244 |
#if __ARM_ARCH>=7 |
| 245 |
ite eq @ Thumb2 thing, sanity check in ARM |
| 246 |
#endif |
| 247 |
ldreq $t3,[sp,#16*4] @ pull ctx |
| 248 |
bne .Lrounds_16_xx |
| 249 |
|
| 250 |
add $A,$A,$t2 @ h+=Maj(a,b,c) from the past |
| 251 |
ldr $t0,[$t3,#0] |
| 252 |
ldr $t1,[$t3,#4] |
| 253 |
ldr $t2,[$t3,#8] |
| 254 |
add $A,$A,$t0 |
| 255 |
ldr $t0,[$t3,#12] |
| 256 |
add $B,$B,$t1 |
| 257 |
ldr $t1,[$t3,#16] |
| 258 |
add $C,$C,$t2 |
| 259 |
ldr $t2,[$t3,#20] |
| 260 |
add $D,$D,$t0 |
| 261 |
ldr $t0,[$t3,#24] |
| 262 |
add $E,$E,$t1 |
| 263 |
ldr $t1,[$t3,#28] |
| 264 |
add $F,$F,$t2 |
| 265 |
ldr $inp,[sp,#17*4] @ pull inp |
| 266 |
ldr $t2,[sp,#18*4] @ pull inp+len |
| 267 |
add $G,$G,$t0 |
| 268 |
add $H,$H,$t1 |
| 269 |
stmia $t3,{$A,$B,$C,$D,$E,$F,$G,$H} |
| 270 |
cmp $inp,$t2 |
| 271 |
sub $Ktbl,$Ktbl,#256 @ rewind Ktbl |
| 272 |
bne .Loop |
| 273 |
|
| 274 |
add sp,sp,#`16+3`*4 @ destroy frame |
| 275 |
#if __ARM_ARCH>=5 |
| 276 |
ldmia sp!,{r4-r11,pc} |
| 277 |
#else |
| 278 |
ldmia sp!,{r4-r11,lr} |
| 279 |
tst lr,#1 |
| 280 |
moveq pc,lr @ be binary compatible with V4, yet |
| 281 |
bx lr @ interoperable with Thumb ISA:-) |
| 282 |
#endif |
| 283 |
.size sha256_block_data_order_nohw,.-sha256_block_data_order_nohw |
| 284 |
___ |
| 285 |
###################################################################### |
| 286 |
# NEON stuff |
| 287 |
# |
| 288 |
{{{ |
| 289 |
my @X=map("q$_",(0..3)); |
| 290 |
my ($T0,$T1,$T2,$T3,$T4,$T5)=("q8","q9","q10","q11","d24","d25"); |
| 291 |
my $Xfer=$t4; |
| 292 |
my $j=0; |
| 293 |
|
| 294 |
sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; } |
| 295 |
sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; } |
| 296 |
|
| 297 |
sub AUTOLOAD() # thunk [simplified] x86-style perlasm |
| 298 |
{ my $opcode = $AUTOLOAD; $opcode =~ s/.*:://; $opcode =~ s/_/\./; |
| 299 |
my $arg = pop; |
| 300 |
$arg = "#$arg" if ($arg*1 eq $arg); |
| 301 |
$code .= "\t$opcode\t".join(',',@_,$arg)."\n"; |
| 302 |
} |
| 303 |
|
| 304 |
sub Xupdate() |
| 305 |
{ use integer; |
| 306 |
my $body = shift; |
| 307 |
my @insns = (&$body,&$body,&$body,&$body); |
| 308 |
my ($a,$b,$c,$d,$e,$f,$g,$h); |
| 309 |
|
| 310 |
&vext_8 ($T0,@X[0],@X[1],4); # X[1..4] |
| 311 |
eval(shift(@insns)); |
| 312 |
eval(shift(@insns)); |
| 313 |
eval(shift(@insns)); |
| 314 |
&vext_8 ($T1,@X[2],@X[3],4); # X[9..12] |
| 315 |
eval(shift(@insns)); |
| 316 |
eval(shift(@insns)); |
| 317 |
eval(shift(@insns)); |
| 318 |
&vshr_u32 ($T2,$T0,$sigma0[0]); |
| 319 |
eval(shift(@insns)); |
| 320 |
eval(shift(@insns)); |
| 321 |
&vadd_i32 (@X[0],@X[0],$T1); # X[0..3] += X[9..12] |
| 322 |
eval(shift(@insns)); |
| 323 |
eval(shift(@insns)); |
| 324 |
&vshr_u32 ($T1,$T0,$sigma0[2]); |
| 325 |
eval(shift(@insns)); |
| 326 |
eval(shift(@insns)); |
| 327 |
&vsli_32 ($T2,$T0,32-$sigma0[0]); |
| 328 |
eval(shift(@insns)); |
| 329 |
eval(shift(@insns)); |
| 330 |
&vshr_u32 ($T3,$T0,$sigma0[1]); |
| 331 |
eval(shift(@insns)); |
| 332 |
eval(shift(@insns)); |
| 333 |
&veor ($T1,$T1,$T2); |
| 334 |
eval(shift(@insns)); |
| 335 |
eval(shift(@insns)); |
| 336 |
&vsli_32 ($T3,$T0,32-$sigma0[1]); |
| 337 |
eval(shift(@insns)); |
| 338 |
eval(shift(@insns)); |
| 339 |
&vshr_u32 ($T4,&Dhi(@X[3]),$sigma1[0]); |
| 340 |
eval(shift(@insns)); |
| 341 |
eval(shift(@insns)); |
| 342 |
&veor ($T1,$T1,$T3); # sigma0(X[1..4]) |
| 343 |
eval(shift(@insns)); |
| 344 |
eval(shift(@insns)); |
| 345 |
&vsli_32 ($T4,&Dhi(@X[3]),32-$sigma1[0]); |
| 346 |
eval(shift(@insns)); |
| 347 |
eval(shift(@insns)); |
| 348 |
&vshr_u32 ($T5,&Dhi(@X[3]),$sigma1[2]); |
| 349 |
eval(shift(@insns)); |
| 350 |
eval(shift(@insns)); |
| 351 |
&vadd_i32 (@X[0],@X[0],$T1); # X[0..3] += sigma0(X[1..4]) |
| 352 |
eval(shift(@insns)); |
| 353 |
eval(shift(@insns)); |
| 354 |
&veor ($T5,$T5,$T4); |
| 355 |
eval(shift(@insns)); |
| 356 |
eval(shift(@insns)); |
| 357 |
&vshr_u32 ($T4,&Dhi(@X[3]),$sigma1[1]); |
| 358 |
eval(shift(@insns)); |
| 359 |
eval(shift(@insns)); |
| 360 |
&vsli_32 ($T4,&Dhi(@X[3]),32-$sigma1[1]); |
| 361 |
eval(shift(@insns)); |
| 362 |
eval(shift(@insns)); |
| 363 |
&veor ($T5,$T5,$T4); # sigma1(X[14..15]) |
| 364 |
eval(shift(@insns)); |
| 365 |
eval(shift(@insns)); |
| 366 |
&vadd_i32 (&Dlo(@X[0]),&Dlo(@X[0]),$T5);# X[0..1] += sigma1(X[14..15]) |
| 367 |
eval(shift(@insns)); |
| 368 |
eval(shift(@insns)); |
| 369 |
&vshr_u32 ($T4,&Dlo(@X[0]),$sigma1[0]); |
| 370 |
eval(shift(@insns)); |
| 371 |
eval(shift(@insns)); |
| 372 |
&vsli_32 ($T4,&Dlo(@X[0]),32-$sigma1[0]); |
| 373 |
eval(shift(@insns)); |
| 374 |
eval(shift(@insns)); |
| 375 |
&vshr_u32 ($T5,&Dlo(@X[0]),$sigma1[2]); |
| 376 |
eval(shift(@insns)); |
| 377 |
eval(shift(@insns)); |
| 378 |
&veor ($T5,$T5,$T4); |
| 379 |
eval(shift(@insns)); |
| 380 |
eval(shift(@insns)); |
| 381 |
&vshr_u32 ($T4,&Dlo(@X[0]),$sigma1[1]); |
| 382 |
eval(shift(@insns)); |
| 383 |
eval(shift(@insns)); |
| 384 |
&vld1_32 ("{$T0}","[$Ktbl,:128]!"); |
| 385 |
eval(shift(@insns)); |
| 386 |
eval(shift(@insns)); |
| 387 |
&vsli_32 ($T4,&Dlo(@X[0]),32-$sigma1[1]); |
| 388 |
eval(shift(@insns)); |
| 389 |
eval(shift(@insns)); |
| 390 |
&veor ($T5,$T5,$T4); # sigma1(X[16..17]) |
| 391 |
eval(shift(@insns)); |
| 392 |
eval(shift(@insns)); |
| 393 |
&vadd_i32 (&Dhi(@X[0]),&Dhi(@X[0]),$T5);# X[2..3] += sigma1(X[16..17]) |
| 394 |
eval(shift(@insns)); |
| 395 |
eval(shift(@insns)); |
| 396 |
&vadd_i32 ($T0,$T0,@X[0]); |
| 397 |
while($#insns>=2) { eval(shift(@insns)); } |
| 398 |
&vst1_32 ("{$T0}","[$Xfer,:128]!"); |
| 399 |
eval(shift(@insns)); |
| 400 |
eval(shift(@insns)); |
| 401 |
|
| 402 |
push(@X,shift(@X)); # "rotate" X[] |
| 403 |
} |
| 404 |
|
| 405 |
sub Xpreload() |
| 406 |
{ use integer; |
| 407 |
my $body = shift; |
| 408 |
my @insns = (&$body,&$body,&$body,&$body); |
| 409 |
my ($a,$b,$c,$d,$e,$f,$g,$h); |
| 410 |
|
| 411 |
eval(shift(@insns)); |
| 412 |
eval(shift(@insns)); |
| 413 |
eval(shift(@insns)); |
| 414 |
eval(shift(@insns)); |
| 415 |
&vld1_32 ("{$T0}","[$Ktbl,:128]!"); |
| 416 |
eval(shift(@insns)); |
| 417 |
eval(shift(@insns)); |
| 418 |
eval(shift(@insns)); |
| 419 |
eval(shift(@insns)); |
| 420 |
&vrev32_8 (@X[0],@X[0]); |
| 421 |
eval(shift(@insns)); |
| 422 |
eval(shift(@insns)); |
| 423 |
eval(shift(@insns)); |
| 424 |
eval(shift(@insns)); |
| 425 |
&vadd_i32 ($T0,$T0,@X[0]); |
| 426 |
foreach (@insns) { eval; } # remaining instructions |
| 427 |
&vst1_32 ("{$T0}","[$Xfer,:128]!"); |
| 428 |
|
| 429 |
push(@X,shift(@X)); # "rotate" X[] |
| 430 |
} |
| 431 |
|
| 432 |
sub body_00_15 () { |
| 433 |
( |
| 434 |
'($a,$b,$c,$d,$e,$f,$g,$h)=@V;'. |
| 435 |
'&add ($h,$h,$t1)', # h+=X[i]+K[i] |
| 436 |
'&eor ($t1,$f,$g)', |
| 437 |
'&eor ($t0,$e,$e,"ror#".($Sigma1[1]-$Sigma1[0]))', |
| 438 |
'&add ($a,$a,$t2)', # h+=Maj(a,b,c) from the past |
| 439 |
'&and ($t1,$t1,$e)', |
| 440 |
'&eor ($t2,$t0,$e,"ror#".($Sigma1[2]-$Sigma1[0]))', # Sigma1(e) |
| 441 |
'&eor ($t0,$a,$a,"ror#".($Sigma0[1]-$Sigma0[0]))', |
| 442 |
'&eor ($t1,$t1,$g)', # Ch(e,f,g) |
| 443 |
'&add ($h,$h,$t2,"ror#$Sigma1[0]")', # h+=Sigma1(e) |
| 444 |
'&eor ($t2,$a,$b)', # a^b, b^c in next round |
| 445 |
'&eor ($t0,$t0,$a,"ror#".($Sigma0[2]-$Sigma0[0]))', # Sigma0(a) |
| 446 |
'&add ($h,$h,$t1)', # h+=Ch(e,f,g) |
| 447 |
'&ldr ($t1,sprintf "[sp,#%d]",4*(($j+1)&15)) if (($j&15)!=15);'. |
| 448 |
'&ldr ($t1,"[$Ktbl]") if ($j==15);'. |
| 449 |
'&ldr ($t1,"[sp,#64]") if ($j==31)', |
| 450 |
'&and ($t3,$t3,$t2)', # (b^c)&=(a^b) |
| 451 |
'&add ($d,$d,$h)', # d+=h |
| 452 |
'&add ($h,$h,$t0,"ror#$Sigma0[0]");'. # h+=Sigma0(a) |
| 453 |
'&eor ($t3,$t3,$b)', # Maj(a,b,c) |
| 454 |
'$j++; unshift(@V,pop(@V)); ($t2,$t3)=($t3,$t2);' |
| 455 |
) |
| 456 |
} |
| 457 |
|
| 458 |
$code.=<<___; |
| 459 |
#if __ARM_MAX_ARCH__>=7 |
| 460 |
.arch armv7-a |
| 461 |
.fpu neon |
| 462 |
|
| 463 |
.LK256_shortcut_neon: |
| 464 |
@ PC is 8 bytes ahead in Arm mode and 4 bytes ahead in Thumb mode. |
| 465 |
#if defined(__thumb2__) |
| 466 |
.word K256-(.LK256_add_neon+4) |
| 467 |
#else |
| 468 |
.word K256-(.LK256_add_neon+8) |
| 469 |
#endif |
| 470 |
|
| 471 |
.global sha256_block_data_order_neon |
| 472 |
.type sha256_block_data_order_neon,%function |
| 473 |
.align 5 |
| 474 |
.skip 16 |
| 475 |
sha256_block_data_order_neon: |
| 476 |
stmdb sp!,{r4-r12,lr} |
| 477 |
|
| 478 |
sub $H,sp,#16*4+16 |
| 479 |
|
| 480 |
@ K256 is just at the boundary of being easily referenced by an ADR from |
| 481 |
@ this function. In Arm mode, when building with __ARM_ARCH=6, it does |
| 482 |
@ not fit. By moving code around, we could make it fit, but this is too |
| 483 |
@ fragile. For simplicity, just load the offset from |
| 484 |
@ .LK256_shortcut_neon. |
| 485 |
@ |
| 486 |
@ TODO(davidben): adrl would avoid a load, but clang-assembler does not |
| 487 |
@ support it. We might be able to emulate it with a macro, but Android's |
| 488 |
@ did not work when I tried it. |
| 489 |
@ https://android.googlesource.com/platform/ndk/+/refs/heads/main/docs/ClangMigration.md#arm |
| 490 |
ldr $Ktbl,.LK256_shortcut_neon |
| 491 |
.LK256_add_neon: |
| 492 |
add $Ktbl,pc,$Ktbl |
| 493 |
|
| 494 |
bic $H,$H,#15 @ align for 128-bit stores |
| 495 |
mov $t2,sp |
| 496 |
mov sp,$H @ alloca |
| 497 |
add $len,$inp,$len,lsl#6 @ len to point at the end of inp |
| 498 |
|
| 499 |
vld1.8 {@X[0]},[$inp]! |
| 500 |
vld1.8 {@X[1]},[$inp]! |
| 501 |
vld1.8 {@X[2]},[$inp]! |
| 502 |
vld1.8 {@X[3]},[$inp]! |
| 503 |
vld1.32 {$T0},[$Ktbl,:128]! |
| 504 |
vld1.32 {$T1},[$Ktbl,:128]! |
| 505 |
vld1.32 {$T2},[$Ktbl,:128]! |
| 506 |
vld1.32 {$T3},[$Ktbl,:128]! |
| 507 |
vrev32.8 @X[0],@X[0] @ yes, even on |
| 508 |
str $ctx,[sp,#64] |
| 509 |
vrev32.8 @X[1],@X[1] @ big-endian |
| 510 |
str $inp,[sp,#68] |
| 511 |
mov $Xfer,sp |
| 512 |
vrev32.8 @X[2],@X[2] |
| 513 |
str $len,[sp,#72] |
| 514 |
vrev32.8 @X[3],@X[3] |
| 515 |
str $t2,[sp,#76] @ save original sp |
| 516 |
vadd.i32 $T0,$T0,@X[0] |
| 517 |
vadd.i32 $T1,$T1,@X[1] |
| 518 |
vst1.32 {$T0},[$Xfer,:128]! |
| 519 |
vadd.i32 $T2,$T2,@X[2] |
| 520 |
vst1.32 {$T1},[$Xfer,:128]! |
| 521 |
vadd.i32 $T3,$T3,@X[3] |
| 522 |
vst1.32 {$T2},[$Xfer,:128]! |
| 523 |
vst1.32 {$T3},[$Xfer,:128]! |
| 524 |
|
| 525 |
ldmia $ctx,{$A-$H} |
| 526 |
sub $Xfer,$Xfer,#64 |
| 527 |
ldr $t1,[sp,#0] |
| 528 |
eor $t2,$t2,$t2 |
| 529 |
eor $t3,$B,$C |
| 530 |
b .L_00_48 |
| 531 |
|
| 532 |
.align 4 |
| 533 |
.L_00_48: |
| 534 |
___ |
| 535 |
&Xupdate(\&body_00_15); |
| 536 |
&Xupdate(\&body_00_15); |
| 537 |
&Xupdate(\&body_00_15); |
| 538 |
&Xupdate(\&body_00_15); |
| 539 |
$code.=<<___; |
| 540 |
teq $t1,#0 @ check for K256 terminator |
| 541 |
ldr $t1,[sp,#0] |
| 542 |
sub $Xfer,$Xfer,#64 |
| 543 |
bne .L_00_48 |
| 544 |
|
| 545 |
ldr $inp,[sp,#68] |
| 546 |
ldr $t0,[sp,#72] |
| 547 |
sub $Ktbl,$Ktbl,#256 @ rewind $Ktbl |
| 548 |
teq $inp,$t0 |
| 549 |
it eq |
| 550 |
subeq $inp,$inp,#64 @ avoid SEGV |
| 551 |
vld1.8 {@X[0]},[$inp]! @ load next input block |
| 552 |
vld1.8 {@X[1]},[$inp]! |
| 553 |
vld1.8 {@X[2]},[$inp]! |
| 554 |
vld1.8 {@X[3]},[$inp]! |
| 555 |
it ne |
| 556 |
strne $inp,[sp,#68] |
| 557 |
mov $Xfer,sp |
| 558 |
___ |
| 559 |
&Xpreload(\&body_00_15); |
| 560 |
&Xpreload(\&body_00_15); |
| 561 |
&Xpreload(\&body_00_15); |
| 562 |
&Xpreload(\&body_00_15); |
| 563 |
$code.=<<___; |
| 564 |
ldr $t0,[$t1,#0] |
| 565 |
add $A,$A,$t2 @ h+=Maj(a,b,c) from the past |
| 566 |
ldr $t2,[$t1,#4] |
| 567 |
ldr $t3,[$t1,#8] |
| 568 |
ldr $t4,[$t1,#12] |
| 569 |
add $A,$A,$t0 @ accumulate |
| 570 |
ldr $t0,[$t1,#16] |
| 571 |
add $B,$B,$t2 |
| 572 |
ldr $t2,[$t1,#20] |
| 573 |
add $C,$C,$t3 |
| 574 |
ldr $t3,[$t1,#24] |
| 575 |
add $D,$D,$t4 |
| 576 |
ldr $t4,[$t1,#28] |
| 577 |
add $E,$E,$t0 |
| 578 |
str $A,[$t1],#4 |
| 579 |
add $F,$F,$t2 |
| 580 |
str $B,[$t1],#4 |
| 581 |
add $G,$G,$t3 |
| 582 |
str $C,[$t1],#4 |
| 583 |
add $H,$H,$t4 |
| 584 |
str $D,[$t1],#4 |
| 585 |
stmia $t1,{$E-$H} |
| 586 |
|
| 587 |
ittte ne |
| 588 |
movne $Xfer,sp |
| 589 |
ldrne $t1,[sp,#0] |
| 590 |
eorne $t2,$t2,$t2 |
| 591 |
ldreq sp,[sp,#76] @ restore original sp |
| 592 |
itt ne |
| 593 |
eorne $t3,$B,$C |
| 594 |
bne .L_00_48 |
| 595 |
|
| 596 |
ldmia sp!,{r4-r12,pc} |
| 597 |
.size sha256_block_data_order_neon,.-sha256_block_data_order_neon |
| 598 |
#endif |
| 599 |
___ |
| 600 |
}}} |
| 601 |
|
| 602 |
$code.=<<___; |
| 603 |
.asciz "SHA256 block transform for ARMv4/NEON/ARMv8, CRYPTOGAMS by <appro\@openssl.org>" |
| 604 |
.align 2 |
| 605 |
___ |
| 606 |
|
| 607 |
open SELF,$0; |
| 608 |
while(<SELF>) { |
| 609 |
next if (/^#!/); |
| 610 |
last if (!s/^#/@/ and !/^$/); |
| 611 |
print; |
| 612 |
} |
| 613 |
close SELF; |
| 614 |
|
| 615 |
foreach (split($/,$code)) { |
| 616 |
|
| 617 |
s/\`([^\`]*)\`/eval $1/geo; |
| 618 |
|
| 619 |
s/\bret\b/bx lr/go or |
| 620 |
s/\bbx\s+lr\b/.word\t0xe12fff1e/go; # make it possible to compile with -march=armv4 |
| 621 |
|
| 622 |
print $_,"\n"; |
| 623 |
} |
| 624 |
|
| 625 |
close STDOUT or die "error closing STDOUT: $!"; # enforce flush |