Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/chacha/asm/chacha-loongarch64.pl

raw
1 #! /usr/bin/env perl
2 # Author: Min Zhou <[email protected]>
3 # Copyright 2023-2025 The OpenSSL Project Authors. All Rights Reserved.
4 #
5 # Licensed under the Apache License 2.0 (the "License"). You may not use
6 # this file except in compliance with the License. You can obtain a copy
7 # in the file LICENSE in the source distribution or at
8 # https://www.openssl.org/source/license.html
9
10 use strict;
11
12 my $code;
13
14 # Here is the scalar register layout for LoongArch.
15 my ($zero,$ra,$tp,$sp,$fp)=map("\$r$_",(0..3,22));
16 my ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$r$_",(4..11));
17 my ($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$x)=map("\$r$_",(12..21));
18 my ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8)=map("\$r$_",(23..31));
19
20 # The saved floating-point registers in the LP64D ABI. In LoongArch
21 # with vector extension, the low 64 bits of a vector register alias with
22 # the corresponding FPR. So we must save and restore the corresponding
23 # FPR if we'll write into a vector register. The ABI only requires
24 # saving and restoring the FPR (i.e. 64 bits of the corresponding vector
25 # register), not the entire vector register.
26 my ($fs0,$fs1,$fs2,$fs3,$fs4,$fs5,$fs6,$fs7)=map("\$f$_",(24..31));
27
28 # Here is the 128-bit vector register layout for LSX extension.
29 my ($vr0,$vr1,$vr2,$vr3,$vr4,$vr5,$vr6,$vr7,$vr8,$vr9,$vr10,
30 $vr11,$vr12,$vr13,$vr14,$vr15,$vr16,$vr17,$vr18,$vr19,
31 $vr20,$vr21,$vr22,$vr23,$vr24,$vr25,$vr26,$vr27,$vr28,
32 $vr29,$vr30,$vr31)=map("\$vr$_",(0..31));
33
34 # Here is the 256-bit vector register layout for LASX extension.
35 my ($xr0,$xr1,$xr2,$xr3,$xr4,$xr5,$xr6,$xr7,$xr8,$xr9,$xr10,
36 $xr11,$xr12,$xr13,$xr14,$xr15,$xr16,$xr17,$xr18,$xr19,
37 $xr20,$xr21,$xr22,$xr23,$xr24,$xr25,$xr26,$xr27,$xr28,
38 $xr29,$xr30,$xr31)=map("\$xr$_",(0..31));
39
40 # $output is the last argument if it looks like a file (it has an extension)
41 my $output;
42 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
43 open STDOUT,">$output";
44
45 # Input parameter block
46 my ($out, $inp, $len, $key, $counter) = ($a0, $a1, $a2, $a3, $a4);
47
48 $code .= <<EOF;
49 #include "loongarch_arch.h"
50
51 .text
52
53 .extern OPENSSL_loongarch_hwcap_P
54
55 .align 6
56 .Lsigma:
57 .ascii "expand 32-byte k"
58 .Linc8x:
59 .long 0,1,2,3,4,5,6,7
60 .Linc4x:
61 .long 0,1,2,3
62
63 .globl ChaCha20_ctr32
64 .type ChaCha20_ctr32 function
65
66 .align 6
67 ChaCha20_ctr32:
68 # $a0 = arg #1 (out pointer)
69 # $a1 = arg #2 (inp pointer)
70 # $a2 = arg #3 (len)
71 # $a3 = arg #4 (key array)
72 # $a4 = arg #5 (counter array)
73
74 beqz $len,.Lno_data
75 ori $t3,$zero,64
76 la.global $t0,OPENSSL_loongarch_hwcap_P
77 ld.w $t0,$t0,0
78
79 bleu $len,$t3,.LChaCha20_1x # goto 1x when len <= 64
80
81 andi $t0,$t0,LOONGARCH_HWCAP_LASX | LOONGARCH_HWCAP_LSX
82 beqz $t0,.LChaCha20_1x
83
84 addi.d $sp,$sp,-64
85 fst.d $fs0,$sp,0
86 fst.d $fs1,$sp,8
87 fst.d $fs2,$sp,16
88 fst.d $fs3,$sp,24
89 fst.d $fs4,$sp,32
90 fst.d $fs5,$sp,40
91 fst.d $fs6,$sp,48
92 fst.d $fs7,$sp,56
93
94 andi $t1,$t0,LOONGARCH_HWCAP_LASX
95 bnez $t1,.LChaCha20_8x
96
97 b .LChaCha20_4x
98
99 EOF
100
101 ########################################################################
102 # Scalar code path that handles all lengths.
103 {
104 # Load the initial states in array @x[*] and update directly
105 my @x = ($t0, $t1, $t2, $t3, $t4, $t5, $t6, $t7,
106 $s0, $s1, $s2, $s3, $s4, $s5, $s6, $s7);
107
108 sub ROUND {
109 my ($a0,$b0,$c0,$d0) = @_;
110 my ($a1,$b1,$c1,$d1) = map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
111 my ($a2,$b2,$c2,$d2) = map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
112 my ($a3,$b3,$c3,$d3) = map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
113
114 $code .= <<EOF;
115 add.w @x[$a0],@x[$a0],@x[$b0]
116 xor @x[$d0],@x[$d0],@x[$a0]
117 rotri.w @x[$d0],@x[$d0],16 # rotate left 16 bits
118 add.w @x[$a1],@x[$a1],@x[$b1]
119 xor @x[$d1],@x[$d1],@x[$a1]
120 rotri.w @x[$d1],@x[$d1],16
121
122 add.w @x[$c0],@x[$c0],@x[$d0]
123 xor @x[$b0],@x[$b0],@x[$c0]
124 rotri.w @x[$b0],@x[$b0],20 # rotate left 12 bits
125 add.w @x[$c1],@x[$c1],@x[$d1]
126 xor @x[$b1],@x[$b1],@x[$c1]
127 rotri.w @x[$b1],@x[$b1],20
128
129 add.w @x[$a0],@x[$a0],@x[$b0]
130 xor @x[$d0],@x[$d0],@x[$a0]
131 rotri.w @x[$d0],@x[$d0],24 # rotate left 8 bits
132 add.w @x[$a1],@x[$a1],@x[$b1]
133 xor @x[$d1],@x[$d1],@x[$a1]
134 rotri.w @x[$d1],@x[$d1],24
135
136 add.w @x[$c0],@x[$c0],@x[$d0]
137 xor @x[$b0],@x[$b0],@x[$c0]
138 rotri.w @x[$b0],@x[$b0],25 # rotate left 7 bits
139 add.w @x[$c1],@x[$c1],@x[$d1]
140 xor @x[$b1],@x[$b1],@x[$c1]
141 rotri.w @x[$b1],@x[$b1],25
142
143 add.w @x[$a2],@x[$a2],@x[$b2]
144 xor @x[$d2],@x[$d2],@x[$a2]
145 rotri.w @x[$d2],@x[$d2],16
146 add.w @x[$a3],@x[$a3],@x[$b3]
147 xor @x[$d3],@x[$d3],@x[$a3]
148 rotri.w @x[$d3],@x[$d3],16
149
150 add.w @x[$c2],@x[$c2],@x[$d2]
151 xor @x[$b2],@x[$b2],@x[$c2]
152 rotri.w @x[$b2],@x[$b2],20
153 add.w @x[$c3],@x[$c3],@x[$d3]
154 xor @x[$b3],@x[$b3],@x[$c3]
155 rotri.w @x[$b3],@x[$b3],20
156
157 add.w @x[$a2],@x[$a2],@x[$b2]
158 xor @x[$d2],@x[$d2],@x[$a2]
159 rotri.w @x[$d2],@x[$d2],24
160 add.w @x[$a3],@x[$a3],@x[$b3]
161 xor @x[$d3],@x[$d3],@x[$a3]
162 rotri.w @x[$d3],@x[$d3],24
163
164 add.w @x[$c2],@x[$c2],@x[$d2]
165 xor @x[$b2],@x[$b2],@x[$c2]
166 rotri.w @x[$b2],@x[$b2],25
167 add.w @x[$c3],@x[$c3],@x[$d3]
168 xor @x[$b3],@x[$b3],@x[$c3]
169 rotri.w @x[$b3],@x[$b3],25
170
171 EOF
172 }
173
174 $code .= <<EOF;
175 .align 6
176 .LChaCha20_1x:
177 addi.d $sp,$sp,-256
178 st.d $s0,$sp,0
179 st.d $s1,$sp,8
180 st.d $s2,$sp,16
181 st.d $s3,$sp,24
182 st.d $s4,$sp,32
183 st.d $s5,$sp,40
184 st.d $s6,$sp,48
185 st.d $s7,$sp,56
186 st.d $s8,$sp,64
187
188 # Save the initial block counter in $s8
189 ld.w $s8,$counter,0
190 b .Loop_outer_1x
191
192 .align 5
193 .Loop_outer_1x:
194 # Load constants
195 la.local $t8,.Lsigma
196 ld.w @x[0],$t8,0 # 'expa'
197 ld.w @x[1],$t8,4 # 'nd 3'
198 ld.w @x[2],$t8,8 # '2-by'
199 ld.w @x[3],$t8,12 # 'te k'
200
201 # Load key
202 ld.w @x[4],$key,4*0
203 ld.w @x[5],$key,4*1
204 ld.w @x[6],$key,4*2
205 ld.w @x[7],$key,4*3
206 ld.w @x[8],$key,4*4
207 ld.w @x[9],$key,4*5
208 ld.w @x[10],$key,4*6
209 ld.w @x[11],$key,4*7
210
211 # Load block counter
212 move @x[12],$s8
213
214 # Load nonce
215 ld.w @x[13],$counter,4*1
216 ld.w @x[14],$counter,4*2
217 ld.w @x[15],$counter,4*3
218
219 # Update states in \@x[*] for 20 rounds
220 ori $t8,$zero,10
221 b .Loop_1x
222
223 .align 5
224 .Loop_1x:
225 EOF
226
227 &ROUND (0, 4, 8, 12);
228 &ROUND (0, 5, 10, 15);
229
230 $code .= <<EOF;
231 addi.w $t8,$t8,-1
232 bnez $t8,.Loop_1x
233
234 # Get the final states by adding the initial states
235 la.local $t8,.Lsigma
236 ld.w $a7,$t8,4*0
237 ld.w $a6,$t8,4*1
238 ld.w $a5,$t8,4*2
239 add.w @x[0],@x[0],$a7
240 add.w @x[1],@x[1],$a6
241 add.w @x[2],@x[2],$a5
242 ld.w $a7,$t8,4*3
243 add.w @x[3],@x[3],$a7
244
245 ld.w $t8,$key,4*0
246 ld.w $a7,$key,4*1
247 ld.w $a6,$key,4*2
248 ld.w $a5,$key,4*3
249 add.w @x[4],@x[4],$t8
250 add.w @x[5],@x[5],$a7
251 add.w @x[6],@x[6],$a6
252 add.w @x[7],@x[7],$a5
253
254 ld.w $t8,$key,4*4
255 ld.w $a7,$key,4*5
256 ld.w $a6,$key,4*6
257 ld.w $a5,$key,4*7
258 add.w @x[8],@x[8],$t8
259 add.w @x[9],@x[9],$a7
260 add.w @x[10],@x[10],$a6
261 add.w @x[11],@x[11],$a5
262
263 add.w @x[12],@x[12],$s8
264
265 ld.w $t8,$counter,4*1
266 ld.w $a7,$counter,4*2
267 ld.w $a6,$counter,4*3
268 add.w @x[13],@x[13],$t8
269 add.w @x[14],@x[14],$a7
270 add.w @x[15],@x[15],$a6
271
272 ori $t8,$zero,64
273 bltu $len,$t8,.Ltail_1x
274
275 # Get the encrypted message by xor states with plaintext
276 ld.w $t8,$inp,4*0
277 ld.w $a7,$inp,4*1
278 ld.w $a6,$inp,4*2
279 ld.w $a5,$inp,4*3
280 xor $t8,$t8,@x[0]
281 xor $a7,$a7,@x[1]
282 xor $a6,$a6,@x[2]
283 xor $a5,$a5,@x[3]
284 st.w $t8,$out,4*0
285 st.w $a7,$out,4*1
286 st.w $a6,$out,4*2
287 st.w $a5,$out,4*3
288
289 ld.w $t8,$inp,4*4
290 ld.w $a7,$inp,4*5
291 ld.w $a6,$inp,4*6
292 ld.w $a5,$inp,4*7
293 xor $t8,$t8,@x[4]
294 xor $a7,$a7,@x[5]
295 xor $a6,$a6,@x[6]
296 xor $a5,$a5,@x[7]
297 st.w $t8,$out,4*4
298 st.w $a7,$out,4*5
299 st.w $a6,$out,4*6
300 st.w $a5,$out,4*7
301
302 ld.w $t8,$inp,4*8
303 ld.w $a7,$inp,4*9
304 ld.w $a6,$inp,4*10
305 ld.w $a5,$inp,4*11
306 xor $t8,$t8,@x[8]
307 xor $a7,$a7,@x[9]
308 xor $a6,$a6,@x[10]
309 xor $a5,$a5,@x[11]
310 st.w $t8,$out,4*8
311 st.w $a7,$out,4*9
312 st.w $a6,$out,4*10
313 st.w $a5,$out,4*11
314
315 ld.w $t8,$inp,4*12
316 ld.w $a7,$inp,4*13
317 ld.w $a6,$inp,4*14
318 ld.w $a5,$inp,4*15
319 xor $t8,$t8,@x[12]
320 xor $a7,$a7,@x[13]
321 xor $a6,$a6,@x[14]
322 xor $a5,$a5,@x[15]
323 st.w $t8,$out,4*12
324 st.w $a7,$out,4*13
325 st.w $a6,$out,4*14
326 st.w $a5,$out,4*15
327
328 addi.d $len,$len,-64
329 beqz $len,.Ldone_1x
330 addi.d $inp,$inp,64
331 addi.d $out,$out,64
332 addi.w $s8,$s8,1
333 b .Loop_outer_1x
334
335 .align 4
336 .Ltail_1x:
337 # Handle the tail for 1x (1 <= tail_len <= 63)
338 addi.d $a7,$sp,72
339 st.w @x[0],$a7,4*0
340 st.w @x[1],$a7,4*1
341 st.w @x[2],$a7,4*2
342 st.w @x[3],$a7,4*3
343 st.w @x[4],$a7,4*4
344 st.w @x[5],$a7,4*5
345 st.w @x[6],$a7,4*6
346 st.w @x[7],$a7,4*7
347 st.w @x[8],$a7,4*8
348 st.w @x[9],$a7,4*9
349 st.w @x[10],$a7,4*10
350 st.w @x[11],$a7,4*11
351 st.w @x[12],$a7,4*12
352 st.w @x[13],$a7,4*13
353 st.w @x[14],$a7,4*14
354 st.w @x[15],$a7,4*15
355
356 move $t8,$zero
357
358 .Loop_tail_1x:
359 # Xor input with states byte by byte
360 ldx.bu $a6,$inp,$t8
361 ldx.bu $a5,$a7,$t8
362 xor $a6,$a6,$a5
363 stx.b $a6,$out,$t8
364 addi.w $t8,$t8,1
365 addi.d $len,$len,-1
366 bnez $len,.Loop_tail_1x
367 b .Ldone_1x
368
369 .Ldone_1x:
370 ld.d $s0,$sp,0
371 ld.d $s1,$sp,8
372 ld.d $s2,$sp,16
373 ld.d $s3,$sp,24
374 ld.d $s4,$sp,32
375 ld.d $s5,$sp,40
376 ld.d $s6,$sp,48
377 ld.d $s7,$sp,56
378 ld.d $s8,$sp,64
379 addi.d $sp,$sp,256
380
381 b .Lend
382
383 EOF
384 }
385
386 ########################################################################
387 # 128-bit LSX code path that handles all lengths.
388 {
389 # Load the initial states in array @x[*] and update directly.
390 my @x = ($vr0, $vr1, $vr2, $vr3, $vr4, $vr5, $vr6, $vr7,
391 $vr8, $vr9, $vr10, $vr11, $vr12, $vr13, $vr14, $vr15);
392
393 # Save the initial states in array @y[*]
394 my @y = ($vr16, $vr17, $vr18, $vr19, $vr20, $vr21, $vr22, $vr23,
395 $vr24, $vr25, $vr26, $vr27, $vr28, $vr29, $vr30, $vr31);
396
397 sub ROUND_4x {
398 my ($a0,$b0,$c0,$d0) = @_;
399 my ($a1,$b1,$c1,$d1) = map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
400 my ($a2,$b2,$c2,$d2) = map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
401 my ($a3,$b3,$c3,$d3) = map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
402
403 $code .= <<EOF;
404 vadd.w @x[$a0],@x[$a0],@x[$b0]
405 vxor.v @x[$d0],@x[$d0],@x[$a0]
406 vrotri.w @x[$d0],@x[$d0],16 # rotate left 16 bits
407 vadd.w @x[$a1],@x[$a1],@x[$b1]
408 vxor.v @x[$d1],@x[$d1],@x[$a1]
409 vrotri.w @x[$d1],@x[$d1],16
410
411 vadd.w @x[$c0],@x[$c0],@x[$d0]
412 vxor.v @x[$b0],@x[$b0],@x[$c0]
413 vrotri.w @x[$b0],@x[$b0],20 # rotate left 12 bits
414 vadd.w @x[$c1],@x[$c1],@x[$d1]
415 vxor.v @x[$b1],@x[$b1],@x[$c1]
416 vrotri.w @x[$b1],@x[$b1],20
417
418 vadd.w @x[$a0],@x[$a0],@x[$b0]
419 vxor.v @x[$d0],@x[$d0],@x[$a0]
420 vrotri.w @x[$d0],@x[$d0],24 # rotate left 8 bits
421 vadd.w @x[$a1],@x[$a1],@x[$b1]
422 vxor.v @x[$d1],@x[$d1],@x[$a1]
423 vrotri.w @x[$d1],@x[$d1],24
424
425 vadd.w @x[$c0],@x[$c0],@x[$d0]
426 vxor.v @x[$b0],@x[$b0],@x[$c0]
427 vrotri.w @x[$b0],@x[$b0],25 # rotate left 7 bits
428 vadd.w @x[$c1],@x[$c1],@x[$d1]
429 vxor.v @x[$b1],@x[$b1],@x[$c1]
430 vrotri.w @x[$b1],@x[$b1],25
431
432 vadd.w @x[$a2],@x[$a2],@x[$b2]
433 vxor.v @x[$d2],@x[$d2],@x[$a2]
434 vrotri.w @x[$d2],@x[$d2],16
435 vadd.w @x[$a3],@x[$a3],@x[$b3]
436 vxor.v @x[$d3],@x[$d3],@x[$a3]
437 vrotri.w @x[$d3],@x[$d3],16
438
439 vadd.w @x[$c2],@x[$c2],@x[$d2]
440 vxor.v @x[$b2],@x[$b2],@x[$c2]
441 vrotri.w @x[$b2],@x[$b2],20
442 vadd.w @x[$c3],@x[$c3],@x[$d3]
443 vxor.v @x[$b3],@x[$b3],@x[$c3]
444 vrotri.w @x[$b3],@x[$b3],20
445
446 vadd.w @x[$a2],@x[$a2],@x[$b2]
447 vxor.v @x[$d2],@x[$d2],@x[$a2]
448 vrotri.w @x[$d2],@x[$d2],24
449 vadd.w @x[$a3],@x[$a3],@x[$b3]
450 vxor.v @x[$d3],@x[$d3],@x[$a3]
451 vrotri.w @x[$d3],@x[$d3],24
452
453 vadd.w @x[$c2],@x[$c2],@x[$d2]
454 vxor.v @x[$b2],@x[$b2],@x[$c2]
455 vrotri.w @x[$b2],@x[$b2],25
456 vadd.w @x[$c3],@x[$c3],@x[$d3]
457 vxor.v @x[$b3],@x[$b3],@x[$c3]
458 vrotri.w @x[$b3],@x[$b3],25
459
460 EOF
461 }
462
463 $code .= <<EOF;
464 .align 6
465 .LChaCha20_4x:
466 addi.d $sp,$sp,-128
467
468 # Save the initial block counter in $t4
469 ld.w $t4,$counter,0
470 b .Loop_outer_4x
471
472 .align 5
473 .Loop_outer_4x:
474 # Load constant
475 la.local $t8,.Lsigma
476 vldrepl.w @x[0],$t8,4*0 # 'expa'
477 vldrepl.w @x[1],$t8,4*1 # 'nd 3'
478 vldrepl.w @x[2],$t8,4*2 # '2-by'
479 vldrepl.w @x[3],$t8,4*3 # 'te k'
480
481 # Load key
482 vldrepl.w @x[4],$key,4*0
483 vldrepl.w @x[5],$key,4*1
484 vldrepl.w @x[6],$key,4*2
485 vldrepl.w @x[7],$key,4*3
486 vldrepl.w @x[8],$key,4*4
487 vldrepl.w @x[9],$key,4*5
488 vldrepl.w @x[10],$key,4*6
489 vldrepl.w @x[11],$key,4*7
490
491 # Load block counter
492 vreplgr2vr.w @x[12],$t4
493
494 # Load nonce
495 vldrepl.w @x[13],$counter,4*1
496 vldrepl.w @x[14],$counter,4*2
497 vldrepl.w @x[15],$counter,4*3
498
499 # Get the correct block counter for each block
500 la.local $t8,.Linc4x
501 vld @y[0],$t8,0
502 vadd.w @x[12],@x[12],@y[0]
503
504 # Copy the initial states from \@x[*] to \@y[*]
505 vori.b @y[0],@x[0],0
506 vori.b @y[1],@x[1],0
507 vori.b @y[2],@x[2],0
508 vori.b @y[3],@x[3],0
509 vori.b @y[4],@x[4],0
510 vori.b @y[5],@x[5],0
511 vori.b @y[6],@x[6],0
512 vori.b @y[7],@x[7],0
513 vori.b @y[8],@x[8],0
514 vori.b @y[9],@x[9],0
515 vori.b @y[10],@x[10],0
516 vori.b @y[11],@x[11],0
517 vori.b @y[12],@x[12],0
518 vori.b @y[13],@x[13],0
519 vori.b @y[14],@x[14],0
520 vori.b @y[15],@x[15],0
521
522 # Update states in \@x[*] for 20 rounds
523 ori $t8,$zero,10
524 b .Loop_4x
525
526 .align 5
527 .Loop_4x:
528 EOF
529
530 &ROUND_4x (0, 4, 8, 12);
531 &ROUND_4x (0, 5, 10, 15);
532
533 $code .= <<EOF;
534 addi.w $t8,$t8,-1
535 bnez $t8,.Loop_4x
536
537 # Get the final states by adding the initial states
538 vadd.w @x[0],@x[0],@y[0]
539 vadd.w @x[1],@x[1],@y[1]
540 vadd.w @x[2],@x[2],@y[2]
541 vadd.w @x[3],@x[3],@y[3]
542 vadd.w @x[4],@x[4],@y[4]
543 vadd.w @x[5],@x[5],@y[5]
544 vadd.w @x[6],@x[6],@y[6]
545 vadd.w @x[7],@x[7],@y[7]
546 vadd.w @x[8],@x[8],@y[8]
547 vadd.w @x[9],@x[9],@y[9]
548 vadd.w @x[10],@x[10],@y[10]
549 vadd.w @x[11],@x[11],@y[11]
550 vadd.w @x[12],@x[12],@y[12]
551 vadd.w @x[13],@x[13],@y[13]
552 vadd.w @x[14],@x[14],@y[14]
553 vadd.w @x[15],@x[15],@y[15]
554
555 # Get the transpose of \@x[*] and save them in \@x[*]
556 vilvl.w @y[0],@x[1],@x[0]
557 vilvh.w @y[1],@x[1],@x[0]
558 vilvl.w @y[2],@x[3],@x[2]
559 vilvh.w @y[3],@x[3],@x[2]
560 vilvl.w @y[4],@x[5],@x[4]
561 vilvh.w @y[5],@x[5],@x[4]
562 vilvl.w @y[6],@x[7],@x[6]
563 vilvh.w @y[7],@x[7],@x[6]
564 vilvl.w @y[8],@x[9],@x[8]
565 vilvh.w @y[9],@x[9],@x[8]
566 vilvl.w @y[10],@x[11],@x[10]
567 vilvh.w @y[11],@x[11],@x[10]
568 vilvl.w @y[12],@x[13],@x[12]
569 vilvh.w @y[13],@x[13],@x[12]
570 vilvl.w @y[14],@x[15],@x[14]
571 vilvh.w @y[15],@x[15],@x[14]
572
573 vilvl.d @x[0],@y[2],@y[0]
574 vilvh.d @x[1],@y[2],@y[0]
575 vilvl.d @x[2],@y[3],@y[1]
576 vilvh.d @x[3],@y[3],@y[1]
577 vilvl.d @x[4],@y[6],@y[4]
578 vilvh.d @x[5],@y[6],@y[4]
579 vilvl.d @x[6],@y[7],@y[5]
580 vilvh.d @x[7],@y[7],@y[5]
581 vilvl.d @x[8],@y[10],@y[8]
582 vilvh.d @x[9],@y[10],@y[8]
583 vilvl.d @x[10],@y[11],@y[9]
584 vilvh.d @x[11],@y[11],@y[9]
585 vilvl.d @x[12],@y[14],@y[12]
586 vilvh.d @x[13],@y[14],@y[12]
587 vilvl.d @x[14],@y[15],@y[13]
588 vilvh.d @x[15],@y[15],@y[13]
589 EOF
590
591 # Adjust the order of elements in @x[*] for ease of use.
592 @x = (@x[0],@x[4],@x[8],@x[12],@x[1],@x[5],@x[9],@x[13],
593 @x[2],@x[6],@x[10],@x[14],@x[3],@x[7],@x[11],@x[15]);
594
595 $code .= <<EOF;
596 ori $t8,$zero,64*4
597 bltu $len,$t8,.Ltail_4x
598
599 # Get the encrypted message by xor states with plaintext
600 vld @y[0],$inp,16*0
601 vld @y[1],$inp,16*1
602 vld @y[2],$inp,16*2
603 vld @y[3],$inp,16*3
604 vxor.v @y[0],@y[0],@x[0]
605 vxor.v @y[1],@y[1],@x[1]
606 vxor.v @y[2],@y[2],@x[2]
607 vxor.v @y[3],@y[3],@x[3]
608 vst @y[0],$out,16*0
609 vst @y[1],$out,16*1
610 vst @y[2],$out,16*2
611 vst @y[3],$out,16*3
612
613 vld @y[0],$inp,16*4
614 vld @y[1],$inp,16*5
615 vld @y[2],$inp,16*6
616 vld @y[3],$inp,16*7
617 vxor.v @y[0],@y[0],@x[4]
618 vxor.v @y[1],@y[1],@x[5]
619 vxor.v @y[2],@y[2],@x[6]
620 vxor.v @y[3],@y[3],@x[7]
621 vst @y[0],$out,16*4
622 vst @y[1],$out,16*5
623 vst @y[2],$out,16*6
624 vst @y[3],$out,16*7
625
626 vld @y[0],$inp,16*8
627 vld @y[1],$inp,16*9
628 vld @y[2],$inp,16*10
629 vld @y[3],$inp,16*11
630 vxor.v @y[0],@y[0],@x[8]
631 vxor.v @y[1],@y[1],@x[9]
632 vxor.v @y[2],@y[2],@x[10]
633 vxor.v @y[3],@y[3],@x[11]
634 vst @y[0],$out,16*8
635 vst @y[1],$out,16*9
636 vst @y[2],$out,16*10
637 vst @y[3],$out,16*11
638
639 vld @y[0],$inp,16*12
640 vld @y[1],$inp,16*13
641 vld @y[2],$inp,16*14
642 vld @y[3],$inp,16*15
643 vxor.v @y[0],@y[0],@x[12]
644 vxor.v @y[1],@y[1],@x[13]
645 vxor.v @y[2],@y[2],@x[14]
646 vxor.v @y[3],@y[3],@x[15]
647 vst @y[0],$out,16*12
648 vst @y[1],$out,16*13
649 vst @y[2],$out,16*14
650 vst @y[3],$out,16*15
651
652 addi.d $len,$len,-64*4
653 beqz $len,.Ldone_4x
654 addi.d $inp,$inp,64*4
655 addi.d $out,$out,64*4
656 addi.w $t4,$t4,4
657 b .Loop_outer_4x
658
659 .Ltail_4x:
660 # Handle the tail for 4x (1 <= tail_len <= 255)
661 ori $t8,$zero,192
662 bgeu $len,$t8,.L192_or_more4x
663 ori $t8,$zero,128
664 bgeu $len,$t8,.L128_or_more4x
665 ori $t8,$zero,64
666 bgeu $len,$t8,.L64_or_more4x
667
668 vst @x[0],$sp,16*0
669 vst @x[1],$sp,16*1
670 vst @x[2],$sp,16*2
671 vst @x[3],$sp,16*3
672 move $t8,$zero
673 b .Loop_tail_4x
674
675 .align 5
676 .L64_or_more4x:
677 vld @y[0],$inp,16*0
678 vld @y[1],$inp,16*1
679 vld @y[2],$inp,16*2
680 vld @y[3],$inp,16*3
681 vxor.v @y[0],@y[0],@x[0]
682 vxor.v @y[1],@y[1],@x[1]
683 vxor.v @y[2],@y[2],@x[2]
684 vxor.v @y[3],@y[3],@x[3]
685 vst @y[0],$out,16*0
686 vst @y[1],$out,16*1
687 vst @y[2],$out,16*2
688 vst @y[3],$out,16*3
689
690 addi.d $len,$len,-64
691 beqz $len,.Ldone_4x
692 addi.d $inp,$inp,64
693 addi.d $out,$out,64
694 vst @x[4],$sp,16*0
695 vst @x[5],$sp,16*1
696 vst @x[6],$sp,16*2
697 vst @x[7],$sp,16*3
698 move $t8,$zero
699 b .Loop_tail_4x
700
701 .align 5
702 .L128_or_more4x:
703 vld @y[0],$inp,16*0
704 vld @y[1],$inp,16*1
705 vld @y[2],$inp,16*2
706 vld @y[3],$inp,16*3
707 vxor.v @y[0],@y[0],@x[0]
708 vxor.v @y[1],@y[1],@x[1]
709 vxor.v @y[2],@y[2],@x[2]
710 vxor.v @y[3],@y[3],@x[3]
711 vst @y[0],$out,16*0
712 vst @y[1],$out,16*1
713 vst @y[2],$out,16*2
714 vst @y[3],$out,16*3
715
716 vld @y[0],$inp,16*4
717 vld @y[1],$inp,16*5
718 vld @y[2],$inp,16*6
719 vld @y[3],$inp,16*7
720 vxor.v @y[0],@y[0],@x[4]
721 vxor.v @y[1],@y[1],@x[5]
722 vxor.v @y[2],@y[2],@x[6]
723 vxor.v @y[3],@y[3],@x[7]
724 vst @y[0],$out,16*4
725 vst @y[1],$out,16*5
726 vst @y[2],$out,16*6
727 vst @y[3],$out,16*7
728
729 addi.d $len,$len,-128
730 beqz $len,.Ldone_4x
731 addi.d $inp,$inp,128
732 addi.d $out,$out,128
733 vst @x[8],$sp,16*0
734 vst @x[9],$sp,16*1
735 vst @x[10],$sp,16*2
736 vst @x[11],$sp,16*3
737 move $t8,$zero
738 b .Loop_tail_4x
739
740 .align 5
741 .L192_or_more4x:
742 vld @y[0],$inp,16*0
743 vld @y[1],$inp,16*1
744 vld @y[2],$inp,16*2
745 vld @y[3],$inp,16*3
746 vxor.v @y[0],@y[0],@x[0]
747 vxor.v @y[1],@y[1],@x[1]
748 vxor.v @y[2],@y[2],@x[2]
749 vxor.v @y[3],@y[3],@x[3]
750 vst @y[0],$out,16*0
751 vst @y[1],$out,16*1
752 vst @y[2],$out,16*2
753 vst @y[3],$out,16*3
754
755 vld @y[0],$inp,16*4
756 vld @y[1],$inp,16*5
757 vld @y[2],$inp,16*6
758 vld @y[3],$inp,16*7
759 vxor.v @y[0],@y[0],@x[4]
760 vxor.v @y[1],@y[1],@x[5]
761 vxor.v @y[2],@y[2],@x[6]
762 vxor.v @y[3],@y[3],@x[7]
763 vst @y[0],$out,16*4
764 vst @y[1],$out,16*5
765 vst @y[2],$out,16*6
766 vst @y[3],$out,16*7
767
768 vld @y[0],$inp,16*8
769 vld @y[1],$inp,16*9
770 vld @y[2],$inp,16*10
771 vld @y[3],$inp,16*11
772 vxor.v @y[0],@y[0],@x[8]
773 vxor.v @y[1],@y[1],@x[9]
774 vxor.v @y[2],@y[2],@x[10]
775 vxor.v @y[3],@y[3],@x[11]
776 vst @y[0],$out,16*8
777 vst @y[1],$out,16*9
778 vst @y[2],$out,16*10
779 vst @y[3],$out,16*11
780
781 addi.d $len,$len,-192
782 beqz $len,.Ldone_4x
783 addi.d $inp,$inp,192
784 addi.d $out,$out,192
785 vst @x[12],$sp,16*0
786 vst @x[13],$sp,16*1
787 vst @x[14],$sp,16*2
788 vst @x[15],$sp,16*3
789 move $t8,$zero
790 b .Loop_tail_4x
791
792 .Loop_tail_4x:
793 # Xor input with states byte by byte
794 ldx.bu $t5,$inp,$t8
795 ldx.bu $t6,$sp,$t8
796 xor $t5,$t5,$t6
797 stx.b $t5,$out,$t8
798 addi.w $t8,$t8,1
799 addi.d $len,$len,-1
800 bnez $len,.Loop_tail_4x
801 b .Ldone_4x
802
803 .Ldone_4x:
804 addi.d $sp,$sp,128
805 b .Lrestore_saved_fpr
806
807 EOF
808 }
809
810 ########################################################################
811 # 256-bit LASX code path that handles all lengths.
812 {
813 # Load the initial states in array @x[*] and update directly.
814 my @x = ($xr0, $xr1, $xr2, $xr3, $xr4, $xr5, $xr6, $xr7,
815 $xr8, $xr9, $xr10, $xr11, $xr12, $xr13, $xr14, $xr15);
816
817 # Save the initial states in array @y[*]
818 my @y = ($xr16, $xr17, $xr18, $xr19, $xr20, $xr21, $xr22, $xr23,
819 $xr24, $xr25, $xr26, $xr27, $xr28, $xr29, $xr30, $xr31);
820
821 sub ROUND_8x {
822 my ($a0,$b0,$c0,$d0) = @_;
823 my ($a1,$b1,$c1,$d1) = map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
824 my ($a2,$b2,$c2,$d2) = map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
825 my ($a3,$b3,$c3,$d3) = map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
826
827 $code .= <<EOF;
828 xvadd.w @x[$a0],@x[$a0],@x[$b0]
829 xvxor.v @x[$d0],@x[$d0],@x[$a0]
830 xvrotri.w @x[$d0],@x[$d0],16 # rotate left 16 bits
831 xvadd.w @x[$a1],@x[$a1],@x[$b1]
832 xvxor.v @x[$d1],@x[$d1],@x[$a1]
833 xvrotri.w @x[$d1],@x[$d1],16
834
835 xvadd.w @x[$c0],@x[$c0],@x[$d0]
836 xvxor.v @x[$b0],@x[$b0],@x[$c0]
837 xvrotri.w @x[$b0],@x[$b0],20 # rotate left 12 bits
838 xvadd.w @x[$c1],@x[$c1],@x[$d1]
839 xvxor.v @x[$b1],@x[$b1],@x[$c1]
840 xvrotri.w @x[$b1],@x[$b1],20
841
842 xvadd.w @x[$a0],@x[$a0],@x[$b0]
843 xvxor.v @x[$d0],@x[$d0],@x[$a0]
844 xvrotri.w @x[$d0],@x[$d0],24 # rotate left 8 bits
845 xvadd.w @x[$a1],@x[$a1],@x[$b1]
846 xvxor.v @x[$d1],@x[$d1],@x[$a1]
847 xvrotri.w @x[$d1],@x[$d1],24
848
849 xvadd.w @x[$c0],@x[$c0],@x[$d0]
850 xvxor.v @x[$b0],@x[$b0],@x[$c0]
851 xvrotri.w @x[$b0],@x[$b0],25 # rotate left 7 bits
852 xvadd.w @x[$c1],@x[$c1],@x[$d1]
853 xvxor.v @x[$b1],@x[$b1],@x[$c1]
854 xvrotri.w @x[$b1],@x[$b1],25
855
856 xvadd.w @x[$a2],@x[$a2],@x[$b2]
857 xvxor.v @x[$d2],@x[$d2],@x[$a2]
858 xvrotri.w @x[$d2],@x[$d2],16
859 xvadd.w @x[$a3],@x[$a3],@x[$b3]
860 xvxor.v @x[$d3],@x[$d3],@x[$a3]
861 xvrotri.w @x[$d3],@x[$d3],16
862
863 xvadd.w @x[$c2],@x[$c2],@x[$d2]
864 xvxor.v @x[$b2],@x[$b2],@x[$c2]
865 xvrotri.w @x[$b2],@x[$b2],20
866 xvadd.w @x[$c3],@x[$c3],@x[$d3]
867 xvxor.v @x[$b3],@x[$b3],@x[$c3]
868 xvrotri.w @x[$b3],@x[$b3],20
869
870 xvadd.w @x[$a2],@x[$a2],@x[$b2]
871 xvxor.v @x[$d2],@x[$d2],@x[$a2]
872 xvrotri.w @x[$d2],@x[$d2],24
873 xvadd.w @x[$a3],@x[$a3],@x[$b3]
874 xvxor.v @x[$d3],@x[$d3],@x[$a3]
875 xvrotri.w @x[$d3],@x[$d3],24
876
877 xvadd.w @x[$c2],@x[$c2],@x[$d2]
878 xvxor.v @x[$b2],@x[$b2],@x[$c2]
879 xvrotri.w @x[$b2],@x[$b2],25
880 xvadd.w @x[$c3],@x[$c3],@x[$d3]
881 xvxor.v @x[$b3],@x[$b3],@x[$c3]
882 xvrotri.w @x[$b3],@x[$b3],25
883
884 EOF
885 }
886
887 $code .= <<EOF;
888 .align 6
889 .LChaCha20_8x:
890 addi.d $sp,$sp,-128
891
892 # Save the initial block counter in $t4
893 ld.w $t4,$counter,0
894 b .Loop_outer_8x
895
896 .align 5
897 .Loop_outer_8x:
898 # Load constant
899 la.local $t8,.Lsigma
900 xvldrepl.w @x[0],$t8,4*0 # 'expa'
901 xvldrepl.w @x[1],$t8,4*1 # 'nd 3'
902 xvldrepl.w @x[2],$t8,4*2 # '2-by'
903 xvldrepl.w @x[3],$t8,4*3 # 'te k'
904
905 # Load key
906 xvldrepl.w @x[4],$key,4*0
907 xvldrepl.w @x[5],$key,4*1
908 xvldrepl.w @x[6],$key,4*2
909 xvldrepl.w @x[7],$key,4*3
910 xvldrepl.w @x[8],$key,4*4
911 xvldrepl.w @x[9],$key,4*5
912 xvldrepl.w @x[10],$key,4*6
913 xvldrepl.w @x[11],$key,4*7
914
915 # Load block counter
916 xvreplgr2vr.w @x[12],$t4
917
918 # Load nonce
919 xvldrepl.w @x[13],$counter,4*1
920 xvldrepl.w @x[14],$counter,4*2
921 xvldrepl.w @x[15],$counter,4*3
922
923 # Get the correct block counter for each block
924 la.local $t8,.Linc8x
925 xvld @y[0],$t8,0
926 xvadd.w @x[12],@x[12],@y[0]
927
928 # Copy the initial states from \@x[*] to \@y[*]
929 xvori.b @y[0],@x[0],0
930 xvori.b @y[1],@x[1],0
931 xvori.b @y[2],@x[2],0
932 xvori.b @y[3],@x[3],0
933 xvori.b @y[4],@x[4],0
934 xvori.b @y[5],@x[5],0
935 xvori.b @y[6],@x[6],0
936 xvori.b @y[7],@x[7],0
937 xvori.b @y[8],@x[8],0
938 xvori.b @y[9],@x[9],0
939 xvori.b @y[10],@x[10],0
940 xvori.b @y[11],@x[11],0
941 xvori.b @y[12],@x[12],0
942 xvori.b @y[13],@x[13],0
943 xvori.b @y[14],@x[14],0
944 xvori.b @y[15],@x[15],0
945
946 # Update states in \@x[*] for 20 rounds
947 ori $t8,$zero,10
948 b .Loop_8x
949
950 .align 5
951 .Loop_8x:
952 EOF
953
954 &ROUND_8x (0, 4, 8, 12);
955 &ROUND_8x (0, 5, 10, 15);
956
957 $code .= <<EOF;
958 addi.w $t8,$t8,-1
959 bnez $t8,.Loop_8x
960
961 # Get the final states by adding the initial states
962 xvadd.w @x[0],@x[0],@y[0]
963 xvadd.w @x[1],@x[1],@y[1]
964 xvadd.w @x[2],@x[2],@y[2]
965 xvadd.w @x[3],@x[3],@y[3]
966 xvadd.w @x[4],@x[4],@y[4]
967 xvadd.w @x[5],@x[5],@y[5]
968 xvadd.w @x[6],@x[6],@y[6]
969 xvadd.w @x[7],@x[7],@y[7]
970 xvadd.w @x[8],@x[8],@y[8]
971 xvadd.w @x[9],@x[9],@y[9]
972 xvadd.w @x[10],@x[10],@y[10]
973 xvadd.w @x[11],@x[11],@y[11]
974 xvadd.w @x[12],@x[12],@y[12]
975 xvadd.w @x[13],@x[13],@y[13]
976 xvadd.w @x[14],@x[14],@y[14]
977 xvadd.w @x[15],@x[15],@y[15]
978
979 # Get the transpose of \@x[*] and save them in \@y[*]
980 xvilvl.w @y[0],@x[1],@x[0]
981 xvilvh.w @y[1],@x[1],@x[0]
982 xvilvl.w @y[2],@x[3],@x[2]
983 xvilvh.w @y[3],@x[3],@x[2]
984 xvilvl.w @y[4],@x[5],@x[4]
985 xvilvh.w @y[5],@x[5],@x[4]
986 xvilvl.w @y[6],@x[7],@x[6]
987 xvilvh.w @y[7],@x[7],@x[6]
988 xvilvl.w @y[8],@x[9],@x[8]
989 xvilvh.w @y[9],@x[9],@x[8]
990 xvilvl.w @y[10],@x[11],@x[10]
991 xvilvh.w @y[11],@x[11],@x[10]
992 xvilvl.w @y[12],@x[13],@x[12]
993 xvilvh.w @y[13],@x[13],@x[12]
994 xvilvl.w @y[14],@x[15],@x[14]
995 xvilvh.w @y[15],@x[15],@x[14]
996
997 xvilvl.d @x[0],@y[2],@y[0]
998 xvilvh.d @x[1],@y[2],@y[0]
999 xvilvl.d @x[2],@y[3],@y[1]
1000 xvilvh.d @x[3],@y[3],@y[1]
1001 xvilvl.d @x[4],@y[6],@y[4]
1002 xvilvh.d @x[5],@y[6],@y[4]
1003 xvilvl.d @x[6],@y[7],@y[5]
1004 xvilvh.d @x[7],@y[7],@y[5]
1005 xvilvl.d @x[8],@y[10],@y[8]
1006 xvilvh.d @x[9],@y[10],@y[8]
1007 xvilvl.d @x[10],@y[11],@y[9]
1008 xvilvh.d @x[11],@y[11],@y[9]
1009 xvilvl.d @x[12],@y[14],@y[12]
1010 xvilvh.d @x[13],@y[14],@y[12]
1011 xvilvl.d @x[14],@y[15],@y[13]
1012 xvilvh.d @x[15],@y[15],@y[13]
1013
1014 xvori.b @y[0],@x[4],0
1015 xvpermi.q @y[0],@x[0],0x20
1016 xvori.b @y[1],@x[5],0
1017 xvpermi.q @y[1],@x[1],0x20
1018 xvori.b @y[2],@x[6],0
1019 xvpermi.q @y[2],@x[2],0x20
1020 xvori.b @y[3],@x[7],0
1021 xvpermi.q @y[3],@x[3],0x20
1022 xvori.b @y[4],@x[4],0
1023 xvpermi.q @y[4],@x[0],0x31
1024 xvori.b @y[5],@x[5],0
1025 xvpermi.q @y[5],@x[1],0x31
1026 xvori.b @y[6],@x[6],0
1027 xvpermi.q @y[6],@x[2],0x31
1028 xvori.b @y[7],@x[7],0
1029 xvpermi.q @y[7],@x[3],0x31
1030 xvori.b @y[8],@x[12],0
1031 xvpermi.q @y[8],@x[8],0x20
1032 xvori.b @y[9],@x[13],0
1033 xvpermi.q @y[9],@x[9],0x20
1034 xvori.b @y[10],@x[14],0
1035 xvpermi.q @y[10],@x[10],0x20
1036 xvori.b @y[11],@x[15],0
1037 xvpermi.q @y[11],@x[11],0x20
1038 xvori.b @y[12],@x[12],0
1039 xvpermi.q @y[12],@x[8],0x31
1040 xvori.b @y[13],@x[13],0
1041 xvpermi.q @y[13],@x[9],0x31
1042 xvori.b @y[14],@x[14],0
1043 xvpermi.q @y[14],@x[10],0x31
1044 xvori.b @y[15],@x[15],0
1045 xvpermi.q @y[15],@x[11],0x31
1046
1047 EOF
1048
1049 # Adjust the order of elements in @y[*] for ease of use.
1050 @y = (@y[0],@y[8],@y[1],@y[9],@y[2],@y[10],@y[3],@y[11],
1051 @y[4],@y[12],@y[5],@y[13],@y[6],@y[14],@y[7],@y[15]);
1052
1053 $code .= <<EOF;
1054 ori $t8,$zero,64*8
1055 bltu $len,$t8,.Ltail_8x
1056
1057 # Get the encrypted message by xor states with plaintext
1058 xvld @x[0],$inp,32*0
1059 xvld @x[1],$inp,32*1
1060 xvld @x[2],$inp,32*2
1061 xvld @x[3],$inp,32*3
1062 xvxor.v @x[0],@x[0],@y[0]
1063 xvxor.v @x[1],@x[1],@y[1]
1064 xvxor.v @x[2],@x[2],@y[2]
1065 xvxor.v @x[3],@x[3],@y[3]
1066 xvst @x[0],$out,32*0
1067 xvst @x[1],$out,32*1
1068 xvst @x[2],$out,32*2
1069 xvst @x[3],$out,32*3
1070
1071 xvld @x[0],$inp,32*4
1072 xvld @x[1],$inp,32*5
1073 xvld @x[2],$inp,32*6
1074 xvld @x[3],$inp,32*7
1075 xvxor.v @x[0],@x[0],@y[4]
1076 xvxor.v @x[1],@x[1],@y[5]
1077 xvxor.v @x[2],@x[2],@y[6]
1078 xvxor.v @x[3],@x[3],@y[7]
1079 xvst @x[0],$out,32*4
1080 xvst @x[1],$out,32*5
1081 xvst @x[2],$out,32*6
1082 xvst @x[3],$out,32*7
1083
1084 xvld @x[0],$inp,32*8
1085 xvld @x[1],$inp,32*9
1086 xvld @x[2],$inp,32*10
1087 xvld @x[3],$inp,32*11
1088 xvxor.v @x[0],@x[0],@y[8]
1089 xvxor.v @x[1],@x[1],@y[9]
1090 xvxor.v @x[2],@x[2],@y[10]
1091 xvxor.v @x[3],@x[3],@y[11]
1092 xvst @x[0],$out,32*8
1093 xvst @x[1],$out,32*9
1094 xvst @x[2],$out,32*10
1095 xvst @x[3],$out,32*11
1096
1097 xvld @x[0],$inp,32*12
1098 xvld @x[1],$inp,32*13
1099 xvld @x[2],$inp,32*14
1100 xvld @x[3],$inp,32*15
1101 xvxor.v @x[0],@x[0],@y[12]
1102 xvxor.v @x[1],@x[1],@y[13]
1103 xvxor.v @x[2],@x[2],@y[14]
1104 xvxor.v @x[3],@x[3],@y[15]
1105 xvst @x[0],$out,32*12
1106 xvst @x[1],$out,32*13
1107 xvst @x[2],$out,32*14
1108 xvst @x[3],$out,32*15
1109
1110 addi.d $len,$len,-64*8
1111 beqz $len,.Ldone_8x
1112 addi.d $inp,$inp,64*8
1113 addi.d $out,$out,64*8
1114 addi.w $t4,$t4,8
1115 b .Loop_outer_8x
1116
1117 .Ltail_8x:
1118 # Handle the tail for 8x (1 <= tail_len <= 511)
1119 ori $t8,$zero,448
1120 bgeu $len,$t8,.L448_or_more8x
1121 ori $t8,$zero,384
1122 bgeu $len,$t8,.L384_or_more8x
1123 ori $t8,$zero,320
1124 bgeu $len,$t8,.L320_or_more8x
1125 ori $t8,$zero,256
1126 bgeu $len,$t8,.L256_or_more8x
1127 ori $t8,$zero,192
1128 bgeu $len,$t8,.L192_or_more8x
1129 ori $t8,$zero,128
1130 bgeu $len,$t8,.L128_or_more8x
1131 ori $t8,$zero,64
1132 bgeu $len,$t8,.L64_or_more8x
1133
1134 xvst @y[0],$sp,32*0
1135 xvst @y[1],$sp,32*1
1136 move $t8,$zero
1137 b .Loop_tail_8x
1138
1139 .align 5
1140 .L64_or_more8x:
1141 xvld @x[0],$inp,32*0
1142 xvld @x[1],$inp,32*1
1143 xvxor.v @x[0],@x[0],@y[0]
1144 xvxor.v @x[1],@x[1],@y[1]
1145 xvst @x[0],$out,32*0
1146 xvst @x[1],$out,32*1
1147
1148 addi.d $len,$len,-64
1149 beqz $len,.Ldone_8x
1150 addi.d $inp,$inp,64
1151 addi.d $out,$out,64
1152 xvst @y[2],$sp,32*0
1153 xvst @y[3],$sp,32*1
1154 move $t8,$zero
1155 b .Loop_tail_8x
1156
1157 .align 5
1158 .L128_or_more8x:
1159 xvld @x[0],$inp,32*0
1160 xvld @x[1],$inp,32*1
1161 xvld @x[2],$inp,32*2
1162 xvld @x[3],$inp,32*3
1163 xvxor.v @x[0],@x[0],@y[0]
1164 xvxor.v @x[1],@x[1],@y[1]
1165 xvxor.v @x[2],@x[2],@y[2]
1166 xvxor.v @x[3],@x[3],@y[3]
1167 xvst @x[0],$out,32*0
1168 xvst @x[1],$out,32*1
1169 xvst @x[2],$out,32*2
1170 xvst @x[3],$out,32*3
1171
1172 addi.d $len,$len,-128
1173 beqz $len,.Ldone_8x
1174 addi.d $inp,$inp,128
1175 addi.d $out,$out,128
1176 xvst @y[4],$sp,32*0
1177 xvst @y[5],$sp,32*1
1178 move $t8,$zero
1179 b .Loop_tail_8x
1180
1181 .align 5
1182 .L192_or_more8x:
1183 xvld @x[0],$inp,32*0
1184 xvld @x[1],$inp,32*1
1185 xvld @x[2],$inp,32*2
1186 xvld @x[3],$inp,32*3
1187 xvxor.v @x[0],@x[0],@y[0]
1188 xvxor.v @x[1],@x[1],@y[1]
1189 xvxor.v @x[2],@x[2],@y[2]
1190 xvxor.v @x[3],@x[3],@y[3]
1191 xvst @x[0],$out,32*0
1192 xvst @x[1],$out,32*1
1193 xvst @x[2],$out,32*2
1194 xvst @x[3],$out,32*3
1195
1196 xvld @x[0],$inp,32*4
1197 xvld @x[1],$inp,32*5
1198 xvxor.v @x[0],@x[0],@y[4]
1199 xvxor.v @x[1],@x[1],@y[5]
1200 xvst @x[0],$out,32*4
1201 xvst @x[1],$out,32*5
1202
1203 addi.d $len,$len,-192
1204 beqz $len,.Ldone_8x
1205 addi.d $inp,$inp,192
1206 addi.d $out,$out,192
1207 xvst @y[6],$sp,32*0
1208 xvst @y[7],$sp,32*1
1209 move $t8,$zero
1210 b .Loop_tail_8x
1211
1212 .align 5
1213 .L256_or_more8x:
1214 xvld @x[0],$inp,32*0
1215 xvld @x[1],$inp,32*1
1216 xvld @x[2],$inp,32*2
1217 xvld @x[3],$inp,32*3
1218 xvxor.v @x[0],@x[0],@y[0]
1219 xvxor.v @x[1],@x[1],@y[1]
1220 xvxor.v @x[2],@x[2],@y[2]
1221 xvxor.v @x[3],@x[3],@y[3]
1222 xvst @x[0],$out,32*0
1223 xvst @x[1],$out,32*1
1224 xvst @x[2],$out,32*2
1225 xvst @x[3],$out,32*3
1226
1227 xvld @x[0],$inp,32*4
1228 xvld @x[1],$inp,32*5
1229 xvld @x[2],$inp,32*6
1230 xvld @x[3],$inp,32*7
1231 xvxor.v @x[0],@x[0],@y[4]
1232 xvxor.v @x[1],@x[1],@y[5]
1233 xvxor.v @x[2],@x[2],@y[6]
1234 xvxor.v @x[3],@x[3],@y[7]
1235 xvst @x[0],$out,32*4
1236 xvst @x[1],$out,32*5
1237 xvst @x[2],$out,32*6
1238 xvst @x[3],$out,32*7
1239
1240 addi.d $len,$len,-256
1241 beqz $len,.Ldone_8x
1242 addi.d $inp,$inp,256
1243 addi.d $out,$out,256
1244 xvst @y[8],$sp,32*0
1245 xvst @y[9],$sp,32*1
1246 move $t8,$zero
1247 b .Loop_tail_8x
1248
1249 .align 5
1250 .L320_or_more8x:
1251 xvld @x[0],$inp,32*0
1252 xvld @x[1],$inp,32*1
1253 xvld @x[2],$inp,32*2
1254 xvld @x[3],$inp,32*3
1255 xvxor.v @x[0],@x[0],@y[0]
1256 xvxor.v @x[1],@x[1],@y[1]
1257 xvxor.v @x[2],@x[2],@y[2]
1258 xvxor.v @x[3],@x[3],@y[3]
1259 xvst @x[0],$out,32*0
1260 xvst @x[1],$out,32*1
1261 xvst @x[2],$out,32*2
1262 xvst @x[3],$out,32*3
1263
1264 xvld @x[0],$inp,32*4
1265 xvld @x[1],$inp,32*5
1266 xvld @x[2],$inp,32*6
1267 xvld @x[3],$inp,32*7
1268 xvxor.v @x[0],@x[0],@y[4]
1269 xvxor.v @x[1],@x[1],@y[5]
1270 xvxor.v @x[2],@x[2],@y[6]
1271 xvxor.v @x[3],@x[3],@y[7]
1272 xvst @x[0],$out,32*4
1273 xvst @x[1],$out,32*5
1274 xvst @x[2],$out,32*6
1275 xvst @x[3],$out,32*7
1276
1277 xvld @x[0],$inp,32*8
1278 xvld @x[1],$inp,32*9
1279 xvxor.v @x[0],@x[0],@y[8]
1280 xvxor.v @x[1],@x[1],@y[9]
1281 xvst @x[0],$out,32*8
1282 xvst @x[1],$out,32*9
1283
1284 addi.d $len,$len,-320
1285 beqz $len,.Ldone_8x
1286 addi.d $inp,$inp,320
1287 addi.d $out,$out,320
1288 xvst @y[10],$sp,32*0
1289 xvst @y[11],$sp,32*1
1290 move $t8,$zero
1291 b .Loop_tail_8x
1292
1293 .align 5
1294 .L384_or_more8x:
1295 xvld @x[0],$inp,32*0
1296 xvld @x[1],$inp,32*1
1297 xvld @x[2],$inp,32*2
1298 xvld @x[3],$inp,32*3
1299 xvxor.v @x[0],@x[0],@y[0]
1300 xvxor.v @x[1],@x[1],@y[1]
1301 xvxor.v @x[2],@x[2],@y[2]
1302 xvxor.v @x[3],@x[3],@y[3]
1303 xvst @x[0],$out,32*0
1304 xvst @x[1],$out,32*1
1305 xvst @x[2],$out,32*2
1306 xvst @x[3],$out,32*3
1307
1308 xvld @x[0],$inp,32*4
1309 xvld @x[1],$inp,32*5
1310 xvld @x[2],$inp,32*6
1311 xvld @x[3],$inp,32*7
1312 xvxor.v @x[0],@x[0],@y[4]
1313 xvxor.v @x[1],@x[1],@y[5]
1314 xvxor.v @x[2],@x[2],@y[6]
1315 xvxor.v @x[3],@x[3],@y[7]
1316 xvst @x[0],$out,32*4
1317 xvst @x[1],$out,32*5
1318 xvst @x[2],$out,32*6
1319 xvst @x[3],$out,32*7
1320
1321 xvld @x[0],$inp,32*8
1322 xvld @x[1],$inp,32*9
1323 xvld @x[2],$inp,32*10
1324 xvld @x[3],$inp,32*11
1325 xvxor.v @x[0],@x[0],@y[8]
1326 xvxor.v @x[1],@x[1],@y[9]
1327 xvxor.v @x[2],@x[2],@y[10]
1328 xvxor.v @x[3],@x[3],@y[11]
1329 xvst @x[0],$out,32*8
1330 xvst @x[1],$out,32*9
1331 xvst @x[2],$out,32*10
1332 xvst @x[3],$out,32*11
1333
1334 addi.d $len,$len,-384
1335 beqz $len,.Ldone_8x
1336 addi.d $inp,$inp,384
1337 addi.d $out,$out,384
1338 xvst @y[12],$sp,32*0
1339 xvst @y[13],$sp,32*1
1340 move $t8,$zero
1341 b .Loop_tail_8x
1342
1343 .align 5
1344 .L448_or_more8x:
1345 xvld @x[0],$inp,32*0
1346 xvld @x[1],$inp,32*1
1347 xvld @x[2],$inp,32*2
1348 xvld @x[3],$inp,32*3
1349 xvxor.v @x[0],@x[0],@y[0]
1350 xvxor.v @x[1],@x[1],@y[1]
1351 xvxor.v @x[2],@x[2],@y[2]
1352 xvxor.v @x[3],@x[3],@y[3]
1353 xvst @x[0],$out,32*0
1354 xvst @x[1],$out,32*1
1355 xvst @x[2],$out,32*2
1356 xvst @x[3],$out,32*3
1357
1358 xvld @x[0],$inp,32*4
1359 xvld @x[1],$inp,32*5
1360 xvld @x[2],$inp,32*6
1361 xvld @x[3],$inp,32*7
1362 xvxor.v @x[0],@x[0],@y[4]
1363 xvxor.v @x[1],@x[1],@y[5]
1364 xvxor.v @x[2],@x[2],@y[6]
1365 xvxor.v @x[3],@x[3],@y[7]
1366 xvst @x[0],$out,32*4
1367 xvst @x[1],$out,32*5
1368 xvst @x[2],$out,32*6
1369 xvst @x[3],$out,32*7
1370
1371 xvld @x[0],$inp,32*8
1372 xvld @x[1],$inp,32*9
1373 xvld @x[2],$inp,32*10
1374 xvld @x[3],$inp,32*11
1375 xvxor.v @x[0],@x[0],@y[8]
1376 xvxor.v @x[1],@x[1],@y[9]
1377 xvxor.v @x[2],@x[2],@y[10]
1378 xvxor.v @x[3],@x[3],@y[11]
1379 xvst @x[0],$out,32*8
1380 xvst @x[1],$out,32*9
1381 xvst @x[2],$out,32*10
1382 xvst @x[3],$out,32*11
1383
1384 xvld @x[0],$inp,32*12
1385 xvld @x[1],$inp,32*13
1386 xvxor.v @x[0],@x[0],@y[12]
1387 xvxor.v @x[1],@x[1],@y[13]
1388 xvst @x[0],$out,32*12
1389 xvst @x[1],$out,32*13
1390
1391 addi.d $len,$len,-448
1392 beqz $len,.Ldone_8x
1393 addi.d $inp,$inp,448
1394 addi.d $out,$out,448
1395 xvst @y[14],$sp,32*0
1396 xvst @y[15],$sp,32*1
1397 move $t8,$zero
1398 b .Loop_tail_8x
1399
1400 .Loop_tail_8x:
1401 # Xor input with states byte by byte
1402 ldx.bu $t5,$inp,$t8
1403 ldx.bu $t6,$sp,$t8
1404 xor $t5,$t5,$t6
1405 stx.b $t5,$out,$t8
1406 addi.w $t8,$t8,1
1407 addi.d $len,$len,-1
1408 bnez $len,.Loop_tail_8x
1409 b .Ldone_8x
1410
1411 .Ldone_8x:
1412 addi.d $sp,$sp,128
1413 b .Lrestore_saved_fpr
1414
1415 EOF
1416 }
1417
1418 $code .= <<EOF;
1419 .Lrestore_saved_fpr:
1420 fld.d $fs0,$sp,0
1421 fld.d $fs1,$sp,8
1422 fld.d $fs2,$sp,16
1423 fld.d $fs3,$sp,24
1424 fld.d $fs4,$sp,32
1425 fld.d $fs5,$sp,40
1426 fld.d $fs6,$sp,48
1427 fld.d $fs7,$sp,56
1428 addi.d $sp,$sp,64
1429 .Lno_data:
1430 .Lend:
1431 jr $ra
1432 .size ChaCha20_ctr32,.-ChaCha20_ctr32
1433 EOF
1434
1435 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
1436
1437 print $code;
1438
1439 close STDOUT;