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 # ====================================================================
10 # sha1_block procedure for ARMv4.
14 # Size/performance trade-off
15 # ====================================================================
16 # impl size in bytes comp cycles[*] measured performance
17 # ====================================================================
19 # armv4-small 392/+29% 1958/+64% 2250/+96%
20 # armv4-compact 740/+89% 1552/+26% 1840/+22%
21 # armv4-large 1420/+92% 1307/+19% 1370/+34%[***]
22 # full unroll ~5100/+260% ~1260/+4% ~1300/+5%
23 # ====================================================================
24 # thumb = same as 'small' but in Thumb instructions[**] and
25 # with recurring code in two private functions;
26 # small = detached Xload/update, loops are folded;
27 # compact = detached Xload/update, 5x unroll;
28 # large = interleaved Xload/update, 5x unroll;
29 # full unroll = interleaved Xload/update, full unroll, estimated[!];
31 # [*] Manually counted instructions in "grand" loop body. Measured
32 # performance is affected by prologue and epilogue overhead,
33 # i-cache availability, branch penalties, etc.
34 # [**] While each Thumb instruction is twice smaller, they are not as
35 # diverse as ARM ones: e.g., there are only two arithmetic
36 # instructions with 3 arguments, no [fixed] rotate, addressing
37 # modes are limited. As result it takes more instructions to do
38 # the same job in Thumb, therefore the code is never twice as
39 # small and always slower.
40 # [***] which is also ~35% better than compiler generated code. Dual-
41 # issue Cortex A8 core was measured to process input block in
46 # Rescheduling for dual-issue pipeline resulted in 13% improvement on
47 # Cortex A8 core and in absolute terms ~870 cycles per input block
48 # [or 13.6 cycles per byte].
52 # Profiler-assisted and platform-specific optimization resulted in 10%
53 # improvement on Cortex A8 core and 12.2 cycles per byte.
57 # Add NEON implementation (see sha1-586.pl for background info). On
58 # Cortex A8 it was measured to process one byte in 6.7 cycles or >80%
59 # faster than integer-only code. Because [fully unrolled] NEON code
60 # is ~2.5x larger and there are some redundant instructions executed
61 # when processing last block, improvement is not as big for smallest
62 # blocks, only ~30%. Snapdragon S4 is a tad faster, 6.4 cycles per
63 # byte, which is also >80% faster than integer-only code.
67 # Add ARMv8 code path performing at 2.35 cpb on Apple A7.
69 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
70 open STDOUT,">$output";
89 my ($a,$b,$c,$d,$e,$opt1,$opt2)=@_;
94 add $e,$K,$e,ror#2 @ E+=K_xx_xx
97 eor $t2,$t2,$t3 @ 1 cycle stall
98 eor $t1,$c,$d @ F_xx_xx
100 add $e,$e,$a,ror#27 @ E+=ROR(A,27)
101 eor $t0,$t0,$t2,ror#31
105 add $e,$e,$t0 @ E+=X[i]
110 my ($a,$b,$c,$d,$e)=@_;
116 add $e,$K,$e,ror#2 @ E+=K_00_19
118 orr $t0,$t0,$t1,lsl#8
119 eor $t1,$c,$d @ F_xx_xx
120 orr $t0,$t0,$t2,lsl#16
121 add $e,$e,$a,ror#27 @ E+=ROR(A,27)
122 orr $t0,$t0,$t3,lsl#24
124 ldr $t0,[$inp],#4 @ handles unaligned
125 add $e,$K,$e,ror#2 @ E+=K_00_19
126 eor $t1,$c,$d @ F_xx_xx
127 add $e,$e,$a,ror#27 @ E+=ROR(A,27)
129 rev $t0,$t0 @ byte swap
133 add $e,$e,$t0 @ E+=X[i]
134 eor $t1,$t1,$d,ror#2 @ F_00_19(B,C,D)
136 add $e,$e,$t1 @ E+=F_00_19(B,C,D)
141 my ($a,$b,$c,$d,$e)=@_;
142 &Xupdate(@_,"and $t1,$b,$t1,ror#2");
144 eor $t1,$t1,$d,ror#2 @ F_00_19(B,C,D)
145 add $e,$e,$t1 @ E+=F_00_19(B,C,D)
150 my ($a,$b,$c,$d,$e)=@_;
151 &Xupdate(@_,"eor $t1,$b,$t1,ror#2");
153 add $e,$e,$t1 @ E+=F_20_39(B,C,D)
158 my ($a,$b,$c,$d,$e)=@_;
159 &Xupdate(@_,"and $t1,$b,$t1,ror#2","and $t2,$c,$d");
161 add $e,$e,$t1 @ E+=F_40_59(B,C,D)
167 #include "arm_arch.h"
172 .global sha1_block_data_order
173 .type sha1_block_data_order,%function
176 sha1_block_data_order:
178 sub r3,pc,#8 @ sha1_block_data_order
179 ldr r12,.LOPENSSL_armcap
180 ldr r12,[r3,r12] @ OPENSSL_armcap_P
186 stmdb sp!,{r4-r12,lr}
187 add $len,$inp,$len,lsl#6 @ $len to point at the end of $inp
188 ldmia $ctx,{$a,$b,$c,$d,$e}
195 mov $e,$e,ror#30 @ [6]
198 for($i=0;$i<5;$i++) {
199 &BODY_00_15(@V); unshift(@V,pop(@V));
203 bne .L_00_15 @ [((11+4)*5+2)*3]
206 &BODY_00_15(@V); unshift(@V,pop(@V));
207 &BODY_16_19(@V); unshift(@V,pop(@V));
208 &BODY_16_19(@V); unshift(@V,pop(@V));
209 &BODY_16_19(@V); unshift(@V,pop(@V));
210 &BODY_16_19(@V); unshift(@V,pop(@V));
213 ldr $K,.LK_20_39 @ [+15+16*4]
214 cmn sp,#0 @ [+3], clear carry to denote 20_39
217 for($i=0;$i<5;$i++) {
218 &BODY_20_39(@V); unshift(@V,pop(@V));
221 teq $Xi,sp @ preserve carry
222 bne .L_20_39_or_60_79 @ [+((12+3)*5+2)*4]
223 bcs .L_done @ [+((12+3)*5+2)*4], spare 300 bytes
226 sub sp,sp,#20*4 @ [+2]
229 for($i=0;$i<5;$i++) {
230 &BODY_40_59(@V); unshift(@V,pop(@V));
234 bne .L_40_59 @ [+((12+5)*5+2)*4]
238 cmp sp,#0 @ set carry to denote 60_79
239 b .L_20_39_or_60_79 @ [+4], spare 300 bytes
241 add sp,sp,#80*4 @ "deallocate" stack frame
242 ldmia $ctx,{$K,$t0,$t1,$t2,$t3}
248 stmia $ctx,{$a,$b,$c,$d,$e}
250 bne .Lloop @ [+18], total 1307
253 ldmia sp!,{r4-r12,pc}
255 ldmia sp!,{r4-r12,lr}
257 moveq pc,lr @ be binary compatible with V4, yet
258 bx lr @ interoperable with Thumb ISA:-)
260 .size sha1_block_data_order,.-sha1_block_data_order
263 .LK_00_19: .word 0x5a827999
264 .LK_20_39: .word 0x6ed9eba1
265 .LK_40_59: .word 0x8f1bbcdc
266 .LK_60_79: .word 0xca62c1d6
268 .word OPENSSL_armcap_P-sha1_block_data_order
269 .asciz "SHA1 block transform for ARMv4/NEON/ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
272 #####################################################################
276 my @V=($a,$b,$c,$d,$e);
277 my ($K_XX_XX,$Ki,$t0,$t1,$Xfer,$saved_sp)=map("r$_",(8..12,14));
279 my @X=map("q$_",(8..11,0..3));
280 my @Tx=("q12","q13");
281 my ($K,$zero)=("q14","q15");
284 sub AUTOLOAD() # thunk [simplified] x86-style perlasm
285 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://; $opcode =~ s/_/\./;
287 $arg = "#$arg" if ($arg*1 eq $arg);
288 $code .= "\t$opcode\t".join(',',@_,$arg)."\n";
293 '($a,$b,$c,$d,$e)=@V;'. # '$code.="@ $j\n";'.
295 '&add ($e,$e,$Ki)', # e+=X[i]+K
297 '&ldr ($Ki,sprintf "[sp,#%d]",4*(($j+1)&15))',
298 '&add ($e,$e,$a,"ror#27")', # e+=ROR(A,27)
299 '&eor ($t1,$t1,$t0)', # F_00_19
300 '&mov ($b,$b,"ror#2")', # b=ROR(b,2)
301 '&add ($e,$e,$t1);'. # e+=F_00_19
302 '$j++; unshift(@V,pop(@V));'
307 '($a,$b,$c,$d,$e)=@V;'. # '$code.="@ $j\n";'.
309 '&add ($e,$e,$Ki)', # e+=X[i]+K
310 '&ldr ($Ki,sprintf "[sp,#%d]",4*(($j+1)&15)) if ($j<79)',
311 '&eor ($t1,$t0,$c)', # F_20_39
312 '&add ($e,$e,$a,"ror#27")', # e+=ROR(A,27)
313 '&mov ($b,$b,"ror#2")', # b=ROR(b,2)
314 '&add ($e,$e,$t1);'. # e+=F_20_39
315 '$j++; unshift(@V,pop(@V));'
320 '($a,$b,$c,$d,$e)=@V;'. # '$code.="@ $j\n";'.
321 '&add ($e,$e,$Ki)', # e+=X[i]+K
323 '&ldr ($Ki,sprintf "[sp,#%d]",4*(($j+1)&15))',
324 '&add ($e,$e,$a,"ror#27")', # e+=ROR(A,27)
328 '&mov ($b,$b,"ror#2")', # b=ROR(b,2)
329 '&add ($e,$e,$t1);'. # e+=F_40_59
330 '$j++; unshift(@V,pop(@V));'
337 my @insns = (&$body,&$body,&$body,&$body);
340 &vext_8 (@X[0],@X[-4&7],@X[-3&7],8); # compose "X[-14]" in "X[0]"
344 &vadd_i32 (@Tx[1],@X[-1&7],$K);
346 &vld1_32 ("{$K\[]}","[$K_XX_XX,:32]!") if ($Xi%5==0);
348 &vext_8 (@Tx[0],@X[-1&7],$zero,4); # "X[-3]", 3 words
352 &veor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
355 &veor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
358 &veor (@Tx[0],@Tx[0],@X[0]); # "X[0]"^="X[-3]"^"X[-8]
361 &vst1_32 ("{@Tx[1]}","[$Xfer,:128]!"); # X[]+K xfer
362 &sub ($Xfer,$Xfer,64) if ($Xi%4==0);
365 &vext_8 (@Tx[1],$zero,@Tx[0],4); # "X[0]"<<96, extract one dword
368 &vadd_i32 (@X[0],@Tx[0],@Tx[0]);
371 &vsri_32 (@X[0],@Tx[0],31); # "X[0]"<<<=1
375 &vshr_u32 (@Tx[0],@Tx[1],30);
378 &vshl_u32 (@Tx[1],@Tx[1],2);
381 &veor (@X[0],@X[0],@Tx[0]);
384 &veor (@X[0],@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
386 foreach (@insns) { eval; } # remaining instructions [if any]
388 $Xi++; push(@X,shift(@X)); # "rotate" X[]
394 my @insns = (&$body,&$body,&$body,&$body);
397 &vext_8 (@Tx[0],@X[-2&7],@X[-1&7],8); # compose "X[-6]"
401 &veor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
404 &veor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
407 &vadd_i32 (@Tx[1],@X[-1&7],$K);
409 &vld1_32 ("{$K\[]}","[$K_XX_XX,:32]!") if ($Xi%5==0);
411 &veor (@Tx[0],@Tx[0],@X[0]); # "X[-6]"^="X[0]"
414 &vshr_u32 (@X[0],@Tx[0],30);
417 &vst1_32 ("{@Tx[1]}","[$Xfer,:128]!"); # X[]+K xfer
418 &sub ($Xfer,$Xfer,64) if ($Xi%4==0);
421 &vsli_32 (@X[0],@Tx[0],2); # "X[0]"="X[-6]"<<<2
423 foreach (@insns) { eval; } # remaining instructions [if any]
425 $Xi++; push(@X,shift(@X)); # "rotate" X[]
431 my @insns = (&$body,&$body,&$body,&$body);
434 &vadd_i32 (@Tx[1],@X[-1&7],$K);
437 &vst1_32 ("{@Tx[1]}","[$Xfer,:128]!");
438 &sub ($Xfer,$Xfer,64);
441 &sub ($K_XX_XX,$K_XX_XX,16); # rewind $K_XX_XX
442 &subeq ($inp,$inp,64); # reload last block to avoid SEGV
443 &vld1_8 ("{@X[-4&7]-@X[-3&7]}","[$inp]!");
446 &vld1_8 ("{@X[-2&7]-@X[-1&7]}","[$inp]!");
449 &vld1_32 ("{$K\[]}","[$K_XX_XX,:32]!"); # load K_00_19
452 &vrev32_8 (@X[-4&7],@X[-4&7]);
454 foreach (@insns) { eval; } # remaining instructions
462 my @insns = (&$body,&$body,&$body,&$body);
465 &vrev32_8 (@X[($Xi-3)&7],@X[($Xi-3)&7]);
468 &vadd_i32 (@X[$Xi&7],@X[($Xi-4)&7],$K);
471 &vst1_32 ("{@X[$Xi&7]}","[$Xfer,:128]!");# X[]+K xfer to IALU
473 foreach (@insns) { eval; }
482 .type sha1_block_data_order_neon,%function
484 sha1_block_data_order_neon:
486 stmdb sp!,{r4-r12,lr}
487 add $len,$inp,$len,lsl#6 @ $len to point at the end of $inp
488 @ dmb @ errata #451034 on early Cortex A8
489 @ vstmdb sp!,{d8-d15} @ ABI specification says so
491 sub sp,sp,#64 @ alloca
492 adr $K_XX_XX,.LK_00_19
493 bic sp,sp,#15 @ align for 128-bit stores
495 ldmia $ctx,{$a,$b,$c,$d,$e} @ load context
498 vld1.8 {@X[-4&7]-@X[-3&7]},[$inp]! @ handles unaligned
499 veor $zero,$zero,$zero
500 vld1.8 {@X[-2&7]-@X[-1&7]},[$inp]!
501 vld1.32 {${K}\[]},[$K_XX_XX,:32]! @ load K_00_19
502 vrev32.8 @X[-4&7],@X[-4&7] @ yes, even on
503 vrev32.8 @X[-3&7],@X[-3&7] @ big-endian...
504 vrev32.8 @X[-2&7],@X[-2&7]
505 vadd.i32 @X[0],@X[-4&7],$K
506 vrev32.8 @X[-1&7],@X[-1&7]
507 vadd.i32 @X[1],@X[-3&7],$K
508 vst1.32 {@X[0]},[$Xfer,:128]!
509 vadd.i32 @X[2],@X[-2&7],$K
510 vst1.32 {@X[1]},[$Xfer,:128]!
511 vst1.32 {@X[2]},[$Xfer,:128]!
512 ldr $Ki,[sp] @ big RAW stall
516 &Xupdate_16_31(\&body_00_19);
517 &Xupdate_16_31(\&body_00_19);
518 &Xupdate_16_31(\&body_00_19);
519 &Xupdate_16_31(\&body_00_19);
520 &Xupdate_32_79(\&body_00_19);
521 &Xupdate_32_79(\&body_20_39);
522 &Xupdate_32_79(\&body_20_39);
523 &Xupdate_32_79(\&body_20_39);
524 &Xupdate_32_79(\&body_20_39);
525 &Xupdate_32_79(\&body_20_39);
526 &Xupdate_32_79(\&body_40_59);
527 &Xupdate_32_79(\&body_40_59);
528 &Xupdate_32_79(\&body_40_59);
529 &Xupdate_32_79(\&body_40_59);
530 &Xupdate_32_79(\&body_40_59);
531 &Xupdate_32_79(\&body_20_39);
532 &Xuplast_80(\&body_20_39);
533 &Xloop(\&body_20_39);
534 &Xloop(\&body_20_39);
535 &Xloop(\&body_20_39);
537 ldmia $ctx,{$Ki,$t0,$t1,$Xfer} @ accumulate context
546 stmia $ctx,{$a,$b,$c,$d,$e}
550 @ vldmia sp!,{d8-d15}
551 ldmia sp!,{r4-r12,pc}
552 .size sha1_block_data_order_neon,.-sha1_block_data_order_neon
556 #####################################################################
560 my ($ABCD,$E,$E0,$E1)=map("q$_",(0..3));
561 my @MSG=map("q$_",(4..7));
562 my @Kxx=map("q$_",(8..11));
563 my ($W0,$W1,$ABCD_SAVE)=map("q$_",(12..14));
567 .type sha1_block_data_order_armv8,%function
569 sha1_block_data_order_armv8:
571 vstmdb sp!,{d8-d15} @ ABI specification says so
575 vld1.32 {$ABCD},[$ctx]!
576 vld1.32 {$E\[0]},[$ctx]
578 vld1.32 {@Kxx[0]\[]},[r3,:32]!
579 vld1.32 {@Kxx[1]\[]},[r3,:32]!
580 vld1.32 {@Kxx[2]\[]},[r3,:32]!
581 vld1.32 {@Kxx[3]\[]},[r3,:32]
584 vld1.8 {@MSG[0]-@MSG[1]},[$inp]!
585 vld1.8 {@MSG[2]-@MSG[3]},[$inp]!
586 vrev32.8 @MSG[0],@MSG[0]
587 vrev32.8 @MSG[1],@MSG[1]
589 vadd.i32 $W0,@Kxx[0],@MSG[0]
590 vrev32.8 @MSG[2],@MSG[2]
591 vmov $ABCD_SAVE,$ABCD @ offload
594 vadd.i32 $W1,@Kxx[0],@MSG[1]
595 vrev32.8 @MSG[3],@MSG[3]
598 vadd.i32 $W0,@Kxx[$j],@MSG[2]
599 sha1su0 @MSG[0],@MSG[1],@MSG[2]
601 for ($j=0,$i=1;$i<20-3;$i++) {
602 my $f=("c","p","m","p")[$i/5];
606 vadd.i32 $W1,@Kxx[$j],@MSG[3]
607 sha1su1 @MSG[0],@MSG[3]
609 $code.=<<___ if ($i<20-4);
610 sha1su0 @MSG[1],@MSG[2],@MSG[3]
612 ($E0,$E1)=($E1,$E0); ($W0,$W1)=($W1,$W0);
613 push(@MSG,shift(@MSG)); $j++ if ((($i+3)%5)==0);
618 vadd.i32 $W1,@Kxx[$j],@MSG[3]
627 vadd.i32 $ABCD,$ABCD,$ABCD_SAVE
630 vst1.32 {$ABCD},[$ctx]!
631 vst1.32 {$E\[0]},[$ctx]
635 .size sha1_block_data_order_armv8,.-sha1_block_data_order_armv8
640 .comm OPENSSL_armcap_P,4,4
644 "sha1c" => 0xf2000c40, "sha1p" => 0xf2100c40,
645 "sha1m" => 0xf2200c40, "sha1su0" => 0xf2300c40,
646 "sha1h" => 0xf3b902c0, "sha1su1" => 0xf3ba0380 );
649 my ($mnemonic,$arg)=@_;
651 $arg =~ m/q([0-9]+)(?:,\s*q([0-9]+))?,\s*q([0-9]+)/o
653 sprintf ".long\t0x%08x\t@ %s %s",
654 $opcode{$mnemonic}|(($1&7)<<13)|(($1&8)<<19)
655 |(($2&7)<<17)|(($2&8)<<4)
656 |(($3&7)<<1) |(($3&8)<<2),
661 foreach (split($/,$code)) {
662 s/{q([0-9]+)\[\]}/sprintf "{d%d[],d%d[]}",2*$1,2*$1+1/eo or
663 s/{q([0-9]+)\[0\]}/sprintf "{d%d[0]}",2*$1/eo;
665 s/\b(sha1\w+)\s+(q.*)/unsha1($1,$2)/geo;
667 s/\bbx\s+lr\b/.word\t0xe12fff1e/o; # make it possible to compile with -march=armv4
672 close STDOUT; # enforce flush