2 # Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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 # ====================================================================
23 # Replace 3xNEON+1xIALU code path with 4+1. 4+1 is actually fastest
24 # option on most(*), but not all, processors, yet 6+2 is retained.
25 # This is because penalties are considered tolerable in comparison to
26 # improvement on processors where 6+2 helps. Most notably +37% on
27 # ThunderX2. It's server-oriented processor which will have to serve
28 # as many requests as possible. While others are mostly clients, when
29 # performance doesn't have to be absolute top-notch, just fast enough,
30 # as majority of time is spent "entertaining" relatively slow human.
32 # Performance in cycles per byte out of large buffer.
34 # IALU/gcc-4.9 4xNEON+1xIALU 6xNEON+2xIALU
36 # Apple A7 5.50/+49% 2.72 1.60
37 # Cortex-A53 8.40/+80% 4.06 4.45(*)
38 # Cortex-A57 8.06/+43% 4.15 4.40(*)
39 # Denver 4.50/+82% 2.30 2.70(*)
40 # X-Gene 9.50/+46% 8.20 8.90(*)
41 # Mongoose 8.00/+44% 2.74 3.12(*)
42 # Kryo 8.17/+50% 4.47 4.65(*)
43 # ThunderX2 7.22/+48% 5.64 4.10
45 # (*) slower than 4+1:-(
47 # $output is the last argument if it looks like a file (it has an extension)
48 # $flavour is the first argument if it doesn't look like a file
49 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
50 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
52 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
53 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
54 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
55 die "can't locate arm-xlate.pl";
57 open OUT,"| \"$^X\" $xlate $flavour \"$output\""
58 or die "can't call $xlate: $!";
61 sub AUTOLOAD() # thunk [simplified] x86-style perlasm
62 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://; $opcode =~ s/_/\./;
64 $arg = "#$arg" if ($arg*1 eq $arg);
65 $code .= "\t$opcode\t".join(',',@_,$arg)."\n";
68 my ($out,$inp,$len,$key,$ctr) = map("x$_",(0..4));
70 my @x=map("x$_",(5..17,19..21));
71 my @d=map("x$_",(22..28,30));
74 my ($a0,$b0,$c0,$d0)=@_;
75 my ($a1,$b1,$c1,$d1)=map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
76 my ($a2,$b2,$c2,$d2)=map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
77 my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
80 "&add_32 (@x[$a0],@x[$a0],@x[$b0])",
81 "&add_32 (@x[$a1],@x[$a1],@x[$b1])",
82 "&add_32 (@x[$a2],@x[$a2],@x[$b2])",
83 "&add_32 (@x[$a3],@x[$a3],@x[$b3])",
84 "&eor_32 (@x[$d0],@x[$d0],@x[$a0])",
85 "&eor_32 (@x[$d1],@x[$d1],@x[$a1])",
86 "&eor_32 (@x[$d2],@x[$d2],@x[$a2])",
87 "&eor_32 (@x[$d3],@x[$d3],@x[$a3])",
88 "&ror_32 (@x[$d0],@x[$d0],16)",
89 "&ror_32 (@x[$d1],@x[$d1],16)",
90 "&ror_32 (@x[$d2],@x[$d2],16)",
91 "&ror_32 (@x[$d3],@x[$d3],16)",
93 "&add_32 (@x[$c0],@x[$c0],@x[$d0])",
94 "&add_32 (@x[$c1],@x[$c1],@x[$d1])",
95 "&add_32 (@x[$c2],@x[$c2],@x[$d2])",
96 "&add_32 (@x[$c3],@x[$c3],@x[$d3])",
97 "&eor_32 (@x[$b0],@x[$b0],@x[$c0])",
98 "&eor_32 (@x[$b1],@x[$b1],@x[$c1])",
99 "&eor_32 (@x[$b2],@x[$b2],@x[$c2])",
100 "&eor_32 (@x[$b3],@x[$b3],@x[$c3])",
101 "&ror_32 (@x[$b0],@x[$b0],20)",
102 "&ror_32 (@x[$b1],@x[$b1],20)",
103 "&ror_32 (@x[$b2],@x[$b2],20)",
104 "&ror_32 (@x[$b3],@x[$b3],20)",
106 "&add_32 (@x[$a0],@x[$a0],@x[$b0])",
107 "&add_32 (@x[$a1],@x[$a1],@x[$b1])",
108 "&add_32 (@x[$a2],@x[$a2],@x[$b2])",
109 "&add_32 (@x[$a3],@x[$a3],@x[$b3])",
110 "&eor_32 (@x[$d0],@x[$d0],@x[$a0])",
111 "&eor_32 (@x[$d1],@x[$d1],@x[$a1])",
112 "&eor_32 (@x[$d2],@x[$d2],@x[$a2])",
113 "&eor_32 (@x[$d3],@x[$d3],@x[$a3])",
114 "&ror_32 (@x[$d0],@x[$d0],24)",
115 "&ror_32 (@x[$d1],@x[$d1],24)",
116 "&ror_32 (@x[$d2],@x[$d2],24)",
117 "&ror_32 (@x[$d3],@x[$d3],24)",
119 "&add_32 (@x[$c0],@x[$c0],@x[$d0])",
120 "&add_32 (@x[$c1],@x[$c1],@x[$d1])",
121 "&add_32 (@x[$c2],@x[$c2],@x[$d2])",
122 "&add_32 (@x[$c3],@x[$c3],@x[$d3])",
123 "&eor_32 (@x[$b0],@x[$b0],@x[$c0])",
124 "&eor_32 (@x[$b1],@x[$b1],@x[$c1])",
125 "&eor_32 (@x[$b2],@x[$b2],@x[$c2])",
126 "&eor_32 (@x[$b3],@x[$b3],@x[$c3])",
127 "&ror_32 (@x[$b0],@x[$b0],25)",
128 "&ror_32 (@x[$b1],@x[$b1],25)",
129 "&ror_32 (@x[$b2],@x[$b2],25)",
130 "&ror_32 (@x[$b3],@x[$b3],25)"
135 #include "arm_arch.h"
137 .extern OPENSSL_armcap_P
138 .hidden OPENSSL_armcap_P
140 .extern ChaCha20_ctr32_sve
147 .quad 0x3320646e61707865,0x6b20657479622d32 // endian-neutral
151 .long 0x02010003,0x06050407,0x0a09080b,0x0e0d0c0f
152 .asciz "ChaCha20 for ARMv8, CRYPTOGAMS by \@dot-asm"
154 .globl ChaCha20_ctr32_dflt
155 .type ChaCha20_ctr32_dflt,%function
158 AARCH64_SIGN_LINK_REGISTER
162 adrp x17,OPENSSL_armcap_P
163 ldr w17,[x17,#:lo12:OPENSSL_armcap_P]
170 stp x29,x30,[sp,#-96]!
181 ldp @d[0],@d[1],[@x[0]] // load sigma
182 ldp @d[2],@d[3],[$key] // load key
183 ldp @d[4],@d[5],[$key,#16]
184 ldp @d[6],@d[7],[$ctr] // load counter
195 mov.32 @x[0],@d[0] // unpack key block
217 foreach (&ROUND(0, 4, 8,12)) { eval; }
218 foreach (&ROUND(0, 5,10,15)) { eval; }
222 add.32 @x[0],@x[0],@d[0] // accumulate key block
223 add @x[1],@x[1],@d[0],lsr#32
224 add.32 @x[2],@x[2],@d[1]
225 add @x[3],@x[3],@d[1],lsr#32
226 add.32 @x[4],@x[4],@d[2]
227 add @x[5],@x[5],@d[2],lsr#32
228 add.32 @x[6],@x[6],@d[3]
229 add @x[7],@x[7],@d[3],lsr#32
230 add.32 @x[8],@x[8],@d[4]
231 add @x[9],@x[9],@d[4],lsr#32
232 add.32 @x[10],@x[10],@d[5]
233 add @x[11],@x[11],@d[5],lsr#32
234 add.32 @x[12],@x[12],@d[6]
235 add @x[13],@x[13],@d[6],lsr#32
236 add.32 @x[14],@x[14],@d[7]
237 add @x[15],@x[15],@d[7],lsr#32
241 add @x[0],@x[0],@x[1],lsl#32 // pack
242 add @x[2],@x[2],@x[3],lsl#32
243 ldp @x[1],@x[3],[$inp,#0] // load input
244 add @x[4],@x[4],@x[5],lsl#32
245 add @x[6],@x[6],@x[7],lsl#32
246 ldp @x[5],@x[7],[$inp,#16]
247 add @x[8],@x[8],@x[9],lsl#32
248 add @x[10],@x[10],@x[11],lsl#32
249 ldp @x[9],@x[11],[$inp,#32]
250 add @x[12],@x[12],@x[13],lsl#32
251 add @x[14],@x[14],@x[15],lsl#32
252 ldp @x[13],@x[15],[$inp,#48]
264 eor @x[0],@x[0],@x[1]
265 eor @x[2],@x[2],@x[3]
266 eor @x[4],@x[4],@x[5]
267 eor @x[6],@x[6],@x[7]
268 eor @x[8],@x[8],@x[9]
269 eor @x[10],@x[10],@x[11]
270 eor @x[12],@x[12],@x[13]
271 eor @x[14],@x[14],@x[15]
273 stp @x[0],@x[2],[$out,#0] // store output
274 add @d[6],@d[6],#1 // increment counter
275 stp @x[4],@x[6],[$out,#16]
276 stp @x[8],@x[10],[$out,#32]
277 stp @x[12],@x[14],[$out,#48]
282 ldp x19,x20,[x29,#16]
284 ldp x21,x22,[x29,#32]
285 ldp x23,x24,[x29,#48]
286 ldp x25,x26,[x29,#64]
287 ldp x27,x28,[x29,#80]
290 AARCH64_VALIDATE_LINK_REGISTER
303 add @x[0],@x[0],@x[1],lsl#32 // pack
304 add @x[2],@x[2],@x[3],lsl#32
305 add @x[4],@x[4],@x[5],lsl#32
306 add @x[6],@x[6],@x[7],lsl#32
307 add @x[8],@x[8],@x[9],lsl#32
308 add @x[10],@x[10],@x[11],lsl#32
309 add @x[12],@x[12],@x[13],lsl#32
310 add @x[14],@x[14],@x[15],lsl#32
321 stp @x[0],@x[2],[sp,#0]
322 stp @x[4],@x[6],[sp,#16]
323 stp @x[8],@x[10],[sp,#32]
324 stp @x[12],@x[14],[sp,#48]
339 ldp x19,x20,[x29,#16]
341 ldp x21,x22,[x29,#32]
342 ldp x23,x24,[x29,#48]
343 ldp x25,x26,[x29,#64]
344 ldp x27,x28,[x29,#80]
346 AARCH64_VALIDATE_LINK_REGISTER
348 .size ChaCha20_ctr32_dflt,.-ChaCha20_ctr32_dflt
350 .globl ChaCha20_ctr32
351 .type ChaCha20_ctr32,%function
354 AARCH64_SIGN_LINK_REGISTER
359 adrp x17,OPENSSL_armcap_P
360 ldr w17,[x17,#:lo12:OPENSSL_armcap_P]
363 stp x29,x30,[sp,#-16]!
365 // SVE handling will inevitably increment the counter
366 // Neon/Scalar code that follows to process tail data needs to
367 // use new counter, unfortunately the input counter buffer
368 // pointed to by ctr is meant to be read-only per API contract
369 // we have to copy the buffer to stack to be writable by SVE
373 bl ChaCha20_ctr32_sve
375 bl ChaCha20_ctr32_dflt
379 AARCH64_VALIDATE_LINK_REGISTER
383 .size ChaCha20_ctr32,.-ChaCha20_ctr32
387 my @K = map("v$_.4s",(0..3));
388 my ($xt0,$xt1,$xt2,$xt3, $CTR,$ROT24) = map("v$_.4s",(4..9));
389 my @X = map("v$_.4s",(16,20,24,28, 17,21,25,29, 18,22,26,30, 19,23,27,31));
390 my ($xa0,$xa1,$xa2,$xa3, $xb0,$xb1,$xb2,$xb3,
391 $xc0,$xc1,$xc2,$xc3, $xd0,$xd1,$xd2,$xd3) = @X;
393 sub NEON_lane_ROUND {
394 my ($a0,$b0,$c0,$d0)=@_;
395 my ($a1,$b1,$c1,$d1)=map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
396 my ($a2,$b2,$c2,$d2)=map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
397 my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
398 my @x=map("'$_'",@X);
401 "&add (@x[$a0],@x[$a0],@x[$b0])", # Q1
402 "&add (@x[$a1],@x[$a1],@x[$b1])", # Q2
403 "&add (@x[$a2],@x[$a2],@x[$b2])", # Q3
404 "&add (@x[$a3],@x[$a3],@x[$b3])", # Q4
405 "&eor (@x[$d0],@x[$d0],@x[$a0])",
406 "&eor (@x[$d1],@x[$d1],@x[$a1])",
407 "&eor (@x[$d2],@x[$d2],@x[$a2])",
408 "&eor (@x[$d3],@x[$d3],@x[$a3])",
409 "&rev32_16 (@x[$d0],@x[$d0])",
410 "&rev32_16 (@x[$d1],@x[$d1])",
411 "&rev32_16 (@x[$d2],@x[$d2])",
412 "&rev32_16 (@x[$d3],@x[$d3])",
414 "&add (@x[$c0],@x[$c0],@x[$d0])",
415 "&add (@x[$c1],@x[$c1],@x[$d1])",
416 "&add (@x[$c2],@x[$c2],@x[$d2])",
417 "&add (@x[$c3],@x[$c3],@x[$d3])",
418 "&eor ('$xt0',@x[$b0],@x[$c0])",
419 "&eor ('$xt1',@x[$b1],@x[$c1])",
420 "&eor ('$xt2',@x[$b2],@x[$c2])",
421 "&eor ('$xt3',@x[$b3],@x[$c3])",
422 "&ushr (@x[$b0],'$xt0',20)",
423 "&ushr (@x[$b1],'$xt1',20)",
424 "&ushr (@x[$b2],'$xt2',20)",
425 "&ushr (@x[$b3],'$xt3',20)",
426 "&sli (@x[$b0],'$xt0',12)",
427 "&sli (@x[$b1],'$xt1',12)",
428 "&sli (@x[$b2],'$xt2',12)",
429 "&sli (@x[$b3],'$xt3',12)",
431 "&add (@x[$a0],@x[$a0],@x[$b0])",
432 "&add (@x[$a1],@x[$a1],@x[$b1])",
433 "&add (@x[$a2],@x[$a2],@x[$b2])",
434 "&add (@x[$a3],@x[$a3],@x[$b3])",
435 "&eor ('$xt0',@x[$d0],@x[$a0])",
436 "&eor ('$xt1',@x[$d1],@x[$a1])",
437 "&eor ('$xt2',@x[$d2],@x[$a2])",
438 "&eor ('$xt3',@x[$d3],@x[$a3])",
439 "&tbl (@x[$d0],'{$xt0}','$ROT24')",
440 "&tbl (@x[$d1],'{$xt1}','$ROT24')",
441 "&tbl (@x[$d2],'{$xt2}','$ROT24')",
442 "&tbl (@x[$d3],'{$xt3}','$ROT24')",
444 "&add (@x[$c0],@x[$c0],@x[$d0])",
445 "&add (@x[$c1],@x[$c1],@x[$d1])",
446 "&add (@x[$c2],@x[$c2],@x[$d2])",
447 "&add (@x[$c3],@x[$c3],@x[$d3])",
448 "&eor ('$xt0',@x[$b0],@x[$c0])",
449 "&eor ('$xt1',@x[$b1],@x[$c1])",
450 "&eor ('$xt2',@x[$b2],@x[$c2])",
451 "&eor ('$xt3',@x[$b3],@x[$c3])",
452 "&ushr (@x[$b0],'$xt0',25)",
453 "&ushr (@x[$b1],'$xt1',25)",
454 "&ushr (@x[$b2],'$xt2',25)",
455 "&ushr (@x[$b3],'$xt3',25)",
456 "&sli (@x[$b0],'$xt0',7)",
457 "&sli (@x[$b1],'$xt1',7)",
458 "&sli (@x[$b2],'$xt2',7)",
459 "&sli (@x[$b3],'$xt3',7)"
468 .type ChaCha20_neon,%function
471 AARCH64_SIGN_LINK_REGISTER
473 stp x29,x30,[sp,#-96]!
483 b.hs .L512_or_more_neon
487 ldp @d[0],@d[1],[@x[0]] // load sigma
488 ld1 {@K[0]},[@x[0]],#16
489 ldp @d[2],@d[3],[$key] // load key
490 ldp @d[4],@d[5],[$key,#16]
491 ld1 {@K[1],@K[2]},[$key]
492 ldp @d[6],@d[7],[$ctr] // load counter
494 stp d8,d9,[sp] // meet ABI requirements
495 ld1 {$CTR,$ROT24},[@x[0]]
507 dup $xa0,@{K[0]}[0] // unpack key block
546 my @plus_one=&ROUND(0,4,8,12);
547 foreach (&NEON_lane_ROUND(0,4,8,12)) { eval; eval(shift(@plus_one)); }
549 @plus_one=&ROUND(0,5,10,15);
550 foreach (&NEON_lane_ROUND(0,5,10,15)) { eval; eval(shift(@plus_one)); }
556 zip1 $xt0,$xa0,$xa1 // transpose data
560 zip1.64 $xa0,$xt0,$xt1
561 zip2.64 $xa1,$xt0,$xt1
562 zip1.64 $xa2,$xt2,$xt3
563 zip2.64 $xa3,$xt2,$xt3
569 zip1.64 $xb0,$xt0,$xt1
570 zip2.64 $xb1,$xt0,$xt1
571 zip1.64 $xb2,$xt2,$xt3
572 zip2.64 $xb3,$xt2,$xt3
575 add.32 @x[0],@x[0],@d[0] // accumulate key block
577 add @x[1],@x[1],@d[0],lsr#32
579 add.32 @x[2],@x[2],@d[1]
581 add @x[3],@x[3],@d[1],lsr#32
582 zip1.64 $xc0,$xt0,$xt1
583 add.32 @x[4],@x[4],@d[2]
584 zip2.64 $xc1,$xt0,$xt1
585 add @x[5],@x[5],@d[2],lsr#32
586 zip1.64 $xc2,$xt2,$xt3
587 add.32 @x[6],@x[6],@d[3]
588 zip2.64 $xc3,$xt2,$xt3
589 add @x[7],@x[7],@d[3],lsr#32
592 add.32 @x[8],@x[8],@d[4]
594 add @x[9],@x[9],@d[4],lsr#32
596 add.32 @x[10],@x[10],@d[5]
598 add @x[11],@x[11],@d[5],lsr#32
599 zip1.64 $xd0,$xt0,$xt1
600 add.32 @x[12],@x[12],@d[6]
601 zip2.64 $xd1,$xt0,$xt1
602 add @x[13],@x[13],@d[6],lsr#32
603 zip1.64 $xd2,$xt2,$xt3
604 add.32 @x[14],@x[14],@d[7]
605 zip2.64 $xd3,$xt2,$xt3
606 add @x[15],@x[15],@d[7],lsr#32
610 add @x[0],@x[0],@x[1],lsl#32 // pack
611 add @x[2],@x[2],@x[3],lsl#32
612 ldp @x[1],@x[3],[$inp,#0] // load input
613 add $xa0,$xa0,@K[0] // accumulate key block
614 add @x[4],@x[4],@x[5],lsl#32
615 add @x[6],@x[6],@x[7],lsl#32
616 ldp @x[5],@x[7],[$inp,#16]
618 add @x[8],@x[8],@x[9],lsl#32
619 add @x[10],@x[10],@x[11],lsl#32
620 ldp @x[9],@x[11],[$inp,#32]
622 add @x[12],@x[12],@x[13],lsl#32
623 add @x[14],@x[14],@x[15],lsl#32
624 ldp @x[13],@x[15],[$inp,#48]
637 ld1.8 {$xt0-$xt3},[$inp],#64
638 eor @x[0],@x[0],@x[1]
640 eor @x[2],@x[2],@x[3]
642 eor @x[4],@x[4],@x[5]
644 eor @x[6],@x[6],@x[7]
646 eor @x[8],@x[8],@x[9]
649 eor @x[10],@x[10],@x[11]
651 eor @x[12],@x[12],@x[13]
653 eor @x[14],@x[14],@x[15]
655 add $CTR,$CTR,$xt0 // += 5
656 ld1.8 {$xt0-$xt3},[$inp],#64
658 stp @x[0],@x[2],[$out,#0] // store output
659 add @d[6],@d[6],#5 // increment counter
660 stp @x[4],@x[6],[$out,#16]
661 stp @x[8],@x[10],[$out,#32]
662 stp @x[12],@x[14],[$out,#48]
665 st1.8 {$xa0-$xd0},[$out],#64
670 ld1.8 {$xa0-$xd0},[$inp],#64
676 st1.8 {$xa1-$xd1},[$out],#64
681 ld1.8 {$xa1-$xd1},[$inp],#64
687 st1.8 {$xa2-$xd2},[$out],#64
693 st1.8 {$xa3-$xd3},[$out],#64
695 b.hi .Loop_outer_neon
697 ldp d8,d9,[sp] // meet ABI requirements
699 ldp x19,x20,[x29,#16]
701 ldp x21,x22,[x29,#32]
702 ldp x23,x24,[x29,#48]
703 ldp x25,x26,[x29,#64]
704 ldp x27,x28,[x29,#80]
706 AARCH64_VALIDATE_LINK_REGISTER
712 ldp d8,d9,[sp] // meet ABI requirements
716 add @x[0],@x[0],@x[1],lsl#32 // pack
717 add @x[2],@x[2],@x[3],lsl#32
718 ldp @x[1],@x[3],[$inp,#0] // load input
719 add @x[4],@x[4],@x[5],lsl#32
720 add @x[6],@x[6],@x[7],lsl#32
721 ldp @x[5],@x[7],[$inp,#16]
722 add @x[8],@x[8],@x[9],lsl#32
723 add @x[10],@x[10],@x[11],lsl#32
724 ldp @x[9],@x[11],[$inp,#32]
725 add @x[12],@x[12],@x[13],lsl#32
726 add @x[14],@x[14],@x[15],lsl#32
727 ldp @x[13],@x[15],[$inp,#48]
739 eor @x[0],@x[0],@x[1]
740 eor @x[2],@x[2],@x[3]
741 eor @x[4],@x[4],@x[5]
742 eor @x[6],@x[6],@x[7]
743 eor @x[8],@x[8],@x[9]
744 eor @x[10],@x[10],@x[11]
745 eor @x[12],@x[12],@x[13]
746 eor @x[14],@x[14],@x[15]
748 stp @x[0],@x[2],[$out,#0] // store output
749 add $xa0,$xa0,@K[0] // accumulate key block
750 stp @x[4],@x[6],[$out,#16]
752 stp @x[8],@x[10],[$out,#32]
754 stp @x[12],@x[14],[$out,#48]
762 ld1.8 {$xt0-$xt3},[$inp],#64
767 st1.8 {$xa0-$xd0},[$out],#64
778 ld1.8 {$xt0-$xt3},[$inp],#64
783 st1.8 {$xa1-$xd1},[$out],#64
794 ld1.8 {$xt0-$xt3},[$inp],#64
799 st1.8 {$xa2-$xd2},[$out],#64
809 st1.8 {$xa0-$xd0},[sp]
823 cbnz $len,.Loop_tail_neon
831 ldp x19,x20,[x29,#16]
833 ldp x21,x22,[x29,#32]
834 ldp x23,x24,[x29,#48]
835 ldp x25,x26,[x29,#64]
836 ldp x27,x28,[x29,#80]
838 AARCH64_VALIDATE_LINK_REGISTER
840 .size ChaCha20_neon,.-ChaCha20_neon
843 my @K = map("v$_.4s",(0..6));
844 my ($T0,$T1,$T2,$T3,$T4,$T5)=@K;
845 my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,
846 $A3,$B3,$C3,$D3,$A4,$B4,$C4,$D4,$A5,$B5,$C5,$D5) = map("v$_.4s",(8..31));
852 my ($a,$b,$c,$d,$t)=@_;
855 "&add ('$a','$a','$b')",
856 "&eor ('$d','$d','$a')",
857 "&rev32_16 ('$d','$d')", # vrot ($d,16)
859 "&add ('$c','$c','$d')",
860 "&eor ('$t','$b','$c')",
861 "&ushr ('$b','$t',20)",
862 "&sli ('$b','$t',12)",
864 "&add ('$a','$a','$b')",
865 "&eor ('$d','$d','$a')",
866 "&tbl ('$d','{$d}','$rot24')",
868 "&add ('$c','$c','$d')",
869 "&eor ('$t','$b','$c')",
870 "&ushr ('$b','$t',25)",
871 "&sli ('$b','$t',7)",
873 "&ext ('$c','$c','$c',8)",
874 "&ext ('$d','$d','$d',$odd?4:12)",
875 "&ext ('$b','$b','$b',$odd?12:4)"
880 .type ChaCha20_512_neon,%function
883 AARCH64_SIGN_LINK_REGISTER
884 stp x29,x30,[sp,#-96]!
898 ldp @d[0],@d[1],[@x[0]] // load sigma
899 ld1 {@K[0]},[@x[0]],#16
900 ldp @d[2],@d[3],[$key] // load key
901 ldp @d[4],@d[5],[$key,#16]
902 ld1 {@K[1],@K[2]},[$key]
903 ldp @d[6],@d[7],[$ctr] // load counter
905 ld1 {$ONE}[0],[@x[0]]
906 add $key,@x[0],#16 // .Lrot24
916 add @K[3],@K[3],$ONE // += 1
917 stp @K[0],@K[1],[sp,#0] // off-load key block, invariant part
918 add @K[3],@K[3],$ONE // not typo
923 shl $ONE,$ONE,#2 // 1 -> 4
925 stp d8,d9,[sp,#128+0] // meet ABI requirements
926 stp d10,d11,[sp,#128+16]
927 stp d12,d13,[sp,#128+32]
928 stp d14,d15,[sp,#128+48]
930 sub $len,$len,#512 // not typo
932 .Loop_outer_512_neon:
940 mov.32 @x[0],@d[0] // unpack key block
963 add $D4,$D0,$ONE // +4
965 add $D5,$D1,$ONE // +4
972 stp @K[3],@K[4],[sp,#48] // off-load key block, variable part
974 stp @K[5],@K[6],[sp,#80]
982 my @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
983 my @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
984 my @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
985 my @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,0);
986 my @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,0);
987 my @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,0);
988 my @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
989 my $diff = ($#thread0+1)*6 - $#thread67 - 1;
993 eval; eval(shift(@thread67));
994 eval(shift(@thread1)); eval(shift(@thread67));
995 eval(shift(@thread2)); eval(shift(@thread67));
996 eval(shift(@thread3)); eval(shift(@thread67));
997 eval(shift(@thread4)); eval(shift(@thread67));
998 eval(shift(@thread5)); eval(shift(@thread67));
1001 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
1002 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
1003 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
1004 @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,1);
1005 @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,1);
1006 @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,1);
1007 @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
1009 foreach (@thread0) {
1010 eval; eval(shift(@thread67));
1011 eval(shift(@thread1)); eval(shift(@thread67));
1012 eval(shift(@thread2)); eval(shift(@thread67));
1013 eval(shift(@thread3)); eval(shift(@thread67));
1014 eval(shift(@thread4)); eval(shift(@thread67));
1015 eval(shift(@thread5)); eval(shift(@thread67));
1018 cbnz $ctr,.Loop_upper_neon
1020 add.32 @x[0],@x[0],@d[0] // accumulate key block
1021 add @x[1],@x[1],@d[0],lsr#32
1022 add.32 @x[2],@x[2],@d[1]
1023 add @x[3],@x[3],@d[1],lsr#32
1024 add.32 @x[4],@x[4],@d[2]
1025 add @x[5],@x[5],@d[2],lsr#32
1026 add.32 @x[6],@x[6],@d[3]
1027 add @x[7],@x[7],@d[3],lsr#32
1028 add.32 @x[8],@x[8],@d[4]
1029 add @x[9],@x[9],@d[4],lsr#32
1030 add.32 @x[10],@x[10],@d[5]
1031 add @x[11],@x[11],@d[5],lsr#32
1032 add.32 @x[12],@x[12],@d[6]
1033 add @x[13],@x[13],@d[6],lsr#32
1034 add.32 @x[14],@x[14],@d[7]
1035 add @x[15],@x[15],@d[7],lsr#32
1037 add @x[0],@x[0],@x[1],lsl#32 // pack
1038 add @x[2],@x[2],@x[3],lsl#32
1039 ldp @x[1],@x[3],[$inp,#0] // load input
1040 add @x[4],@x[4],@x[5],lsl#32
1041 add @x[6],@x[6],@x[7],lsl#32
1042 ldp @x[5],@x[7],[$inp,#16]
1043 add @x[8],@x[8],@x[9],lsl#32
1044 add @x[10],@x[10],@x[11],lsl#32
1045 ldp @x[9],@x[11],[$inp,#32]
1046 add @x[12],@x[12],@x[13],lsl#32
1047 add @x[14],@x[14],@x[15],lsl#32
1048 ldp @x[13],@x[15],[$inp,#48]
1050 #ifdef __AARCH64EB__
1060 eor @x[0],@x[0],@x[1]
1061 eor @x[2],@x[2],@x[3]
1062 eor @x[4],@x[4],@x[5]
1063 eor @x[6],@x[6],@x[7]
1064 eor @x[8],@x[8],@x[9]
1065 eor @x[10],@x[10],@x[11]
1066 eor @x[12],@x[12],@x[13]
1067 eor @x[14],@x[14],@x[15]
1069 stp @x[0],@x[2],[$out,#0] // store output
1070 add @d[6],@d[6],#1 // increment counter
1071 mov.32 @x[0],@d[0] // unpack key block
1073 stp @x[4],@x[6],[$out,#16]
1076 stp @x[8],@x[10],[$out,#32]
1079 stp @x[12],@x[14],[$out,#48]
1086 lsr @x[11],@d[5],#32
1088 lsr @x[13],@d[6],#32
1090 lsr @x[15],@d[7],#32
1096 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
1097 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
1098 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
1099 @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,0);
1100 @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,0);
1101 @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,0);
1102 @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
1104 foreach (@thread0) {
1105 eval; eval(shift(@thread67));
1106 eval(shift(@thread1)); eval(shift(@thread67));
1107 eval(shift(@thread2)); eval(shift(@thread67));
1108 eval(shift(@thread3)); eval(shift(@thread67));
1109 eval(shift(@thread4)); eval(shift(@thread67));
1110 eval(shift(@thread5)); eval(shift(@thread67));
1113 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
1114 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
1115 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
1116 @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,1);
1117 @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,1);
1118 @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,1);
1119 @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
1121 foreach (@thread0) {
1122 eval; eval(shift(@thread67));
1123 eval(shift(@thread1)); eval(shift(@thread67));
1124 eval(shift(@thread2)); eval(shift(@thread67));
1125 eval(shift(@thread3)); eval(shift(@thread67));
1126 eval(shift(@thread4)); eval(shift(@thread67));
1127 eval(shift(@thread5)); eval(shift(@thread67));
1130 cbnz $ctr,.Loop_lower_neon
1132 add.32 @x[0],@x[0],@d[0] // accumulate key block
1133 ldp @K[0],@K[1],[sp,#0]
1134 add @x[1],@x[1],@d[0],lsr#32
1135 ldp @K[2],@K[3],[sp,#32]
1136 add.32 @x[2],@x[2],@d[1]
1137 ldp @K[4],@K[5],[sp,#64]
1138 add @x[3],@x[3],@d[1],lsr#32
1141 add.32 @x[4],@x[4],@d[2]
1143 add @x[5],@x[5],@d[2],lsr#32
1145 add.32 @x[6],@x[6],@d[3]
1147 add @x[7],@x[7],@d[3],lsr#32
1149 add.32 @x[8],@x[8],@d[4]
1151 add @x[9],@x[9],@d[4],lsr#32
1153 add.32 @x[10],@x[10],@d[5]
1155 add @x[11],@x[11],@d[5],lsr#32
1157 add.32 @x[12],@x[12],@d[6]
1159 add @x[13],@x[13],@d[6],lsr#32
1161 add.32 @x[14],@x[14],@d[7]
1163 add @x[15],@x[15],@d[7],lsr#32
1164 add $D4,$D4,$ONE // +4
1165 add @x[0],@x[0],@x[1],lsl#32 // pack
1166 add $D5,$D5,$ONE // +4
1167 add @x[2],@x[2],@x[3],lsl#32
1169 ldp @x[1],@x[3],[$inp,#0] // load input
1171 add @x[4],@x[4],@x[5],lsl#32
1173 add @x[6],@x[6],@x[7],lsl#32
1175 ldp @x[5],@x[7],[$inp,#16]
1177 add @x[8],@x[8],@x[9],lsl#32
1179 add @x[10],@x[10],@x[11],lsl#32
1181 ldp @x[9],@x[11],[$inp,#32]
1183 add @x[12],@x[12],@x[13],lsl#32
1185 add @x[14],@x[14],@x[15],lsl#32
1187 ldp @x[13],@x[15],[$inp,#48]
1192 #ifdef __AARCH64EB__
1202 ld1.8 {$T0-$T3},[$inp],#64
1203 eor @x[0],@x[0],@x[1]
1204 eor @x[2],@x[2],@x[3]
1205 eor @x[4],@x[4],@x[5]
1206 eor @x[6],@x[6],@x[7]
1207 eor @x[8],@x[8],@x[9]
1209 eor @x[10],@x[10],@x[11]
1211 eor @x[12],@x[12],@x[13]
1213 eor @x[14],@x[14],@x[15]
1215 ld1.8 {$T0-$T3},[$inp],#64
1217 stp @x[0],@x[2],[$out,#0] // store output
1218 add @d[6],@d[6],#7 // increment counter
1219 stp @x[4],@x[6],[$out,#16]
1220 stp @x[8],@x[10],[$out,#32]
1221 stp @x[12],@x[14],[$out,#48]
1223 st1.8 {$A0-$D0},[$out],#64
1225 ld1.8 {$A0-$D0},[$inp],#64
1230 st1.8 {$A1-$D1},[$out],#64
1232 ld1.8 {$A1-$D1},[$inp],#64
1234 ldp @K[0],@K[1],[sp,#0]
1236 ldp @K[2],@K[3],[sp,#32]
1239 st1.8 {$A2-$D2},[$out],#64
1241 ld1.8 {$A2-$D2},[$inp],#64
1246 st1.8 {$A3-$D3},[$out],#64
1248 ld1.8 {$A3-$D3},[$inp],#64
1253 st1.8 {$A4-$D4},[$out],#64
1255 shl $A0,$ONE,#1 // 4 -> 8
1260 st1.8 {$A5-$D5},[$out],#64
1262 add @K[3],@K[3],$A0 // += 8
1267 b.hs .Loop_outer_512_neon
1270 ushr $ONE,$ONE,#1 // 4 -> 2
1272 ldp d10,d11,[sp,#128+16] // meet ABI requirements
1273 ldp d12,d13,[sp,#128+32]
1274 ldp d14,d15,[sp,#128+48]
1276 stp @K[0],@K[0],[sp,#0] // wipe off-load area
1277 stp @K[0],@K[0],[sp,#32]
1278 stp @K[0],@K[0],[sp,#64]
1280 b.eq .Ldone_512_neon
1282 sub $key,$key,#16 // .Lone
1285 sub @K[3],@K[3],$ONE // -= 2
1286 ld1 {$CTR,$ROT24},[$key]
1287 b.hs .Loop_outer_neon
1289 ldp d8,d9,[sp,#0] // meet ABI requirements
1290 eor @K[1],@K[1],@K[1]
1291 eor @K[2],@K[2],@K[2]
1292 eor @K[3],@K[3],@K[3]
1293 eor @K[4],@K[4],@K[4]
1294 eor @K[5],@K[5],@K[5]
1295 eor @K[6],@K[6],@K[6]
1299 ldp d8,d9,[sp,#128+0] // meet ABI requirements
1300 ldp x19,x20,[x29,#16]
1302 ldp x21,x22,[x29,#32]
1303 ldp x23,x24,[x29,#48]
1304 ldp x25,x26,[x29,#64]
1305 ldp x27,x28,[x29,#80]
1306 ldp x29,x30,[sp],#96
1307 AARCH64_VALIDATE_LINK_REGISTER
1309 .size ChaCha20_512_neon,.-ChaCha20_512_neon
1314 foreach (split("\n",$code)) {
1315 s/\`([^\`]*)\`/eval $1/geo;
1317 (s/\b([a-z]+)\.32\b/$1/ and (s/x([0-9]+)/w$1/g or 1)) or
1318 (m/\b(eor|ext|mov|tbl)\b/ and (s/\.4s/\.16b/g or 1)) or
1319 (s/\b((?:ld|st)1)\.8\b/$1/ and (s/\.4s/\.16b/g or 1)) or
1320 (m/\b(ld|st)[rp]\b/ and (s/v([0-9]+)\.4s/q$1/g or 1)) or
1321 (m/\b(dup|ld1)\b/ and (s/\.4(s}?\[[0-3]\])/.$1/g or 1)) or
1322 (s/\b(zip[12])\.64\b/$1/ and (s/\.4s/\.2d/g or 1)) or
1323 (s/\brev32\.16\b/rev32/ and (s/\.4s/\.8h/g or 1));
1325 #s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo;
1329 close STDOUT or die "error closing STDOUT: $!"; # flush