2 # Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (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
9 # ====================================================================
10 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
11 # project. The module is, however, dual licensed under OpenSSL and
12 # CRYPTOGAMS licenses depending on where you obtain it. For further
13 # details see http://www.openssl.org/~appro/cryptogams/.
14 # ====================================================================
16 # Keccak-1600 for ARMv8.
20 # This is straightforward KECCAK_1X_ALT implementation. It makes no
21 # sense to attempt SIMD/NEON implementation for following reason.
22 # 64-bit lanes of vector registers can't be addressed as easily as in
23 # 32-bit mode. This means that 64-bit NEON is bound to be slower than
24 # 32-bit NEON, and this implementation is faster than 32-bit NEON on
25 # same processor. Even though it takes more scalar xor's and andn's,
26 # it gets compensated by availability of rotate. Not to forget that
27 # most processors achieve higher issue rate with scalar instructions.
31 # Add hardware-assisted ARMv8.2 implementation. It's KECCAK_1X_ALT
32 # variant with register permutation/rotation twist that allows to
33 # eliminate copies to temporary registers. If you look closely you'll
34 # notice that it uses only one lane of vector registers. The new
35 # instructions effectively facilitate parallel hashing, which we don't
36 # support [yet?]. But lowest-level core procedure is prepared for it.
37 # The inner round is 67 [vector] instructions, so it's not actually
38 # obvious that it will provide performance improvement [in serial
39 # hash] as long as vector instructions issue rate is limited to 1 per
42 ######################################################################
43 # Numbers are cycles per processed byte.
55 # (*) Corresponds to SHA3-256. No improvement coefficients are listed
56 # because they vary too much from compiler to compiler. Newer
57 # compiler does much better and improvement varies from 5% on
58 # Cortex-A57 to 25% on Cortex-A53. While in comparison to older
59 # compiler this code is at least 2x faster...
64 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
65 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
66 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
67 die "can't locate arm-xlate.pl";
69 open OUT,"| \"$^X\" $xlate $flavour $output";
72 my @rhotates = ([ 0, 1, 62, 28, 27 ],
73 [ 36, 44, 6, 55, 20 ],
74 [ 3, 10, 43, 25, 39 ],
75 [ 41, 45, 15, 21, 8 ],
76 [ 18, 2, 61, 56, 14 ]);
81 .align 8 // strategic alignment and padding that allows to use
82 // address value as loop termination condition...
86 .quad 0x0000000000000001
87 .quad 0x0000000000008082
88 .quad 0x800000000000808a
89 .quad 0x8000000080008000
90 .quad 0x000000000000808b
91 .quad 0x0000000080000001
92 .quad 0x8000000080008081
93 .quad 0x8000000000008009
94 .quad 0x000000000000008a
95 .quad 0x0000000000000088
96 .quad 0x0000000080008009
97 .quad 0x000000008000000a
98 .quad 0x000000008000808b
99 .quad 0x800000000000008b
100 .quad 0x8000000000008089
101 .quad 0x8000000000008003
102 .quad 0x8000000000008002
103 .quad 0x8000000000000080
104 .quad 0x000000000000800a
105 .quad 0x800000008000000a
106 .quad 0x8000000080008081
107 .quad 0x8000000000008080
108 .quad 0x0000000080000001
109 .quad 0x8000000080008008
113 my @A = map([ "x$_", "x".($_+1), "x".($_+2), "x".($_+3), "x".($_+4) ],
115 $A[3][3] = "x25"; # x18 is reserved
117 my @C = map("x$_", (26,27,28,30));
120 .type KeccakF1600_int,%function
124 .inst 0xd503233f // paciasp
125 stp $C[2],x30,[sp,#16] // 32 bytes on top are mine
129 ////////////////////////////////////////// Theta
130 eor $C[0],$A[0][0],$A[1][0]
131 stp $A[0][4],$A[1][4],[sp,#0] // offload pair...
132 eor $C[1],$A[0][1],$A[1][1]
133 eor $C[2],$A[0][2],$A[1][2]
134 eor $C[3],$A[0][3],$A[1][3]
139 eor $C[4],$A[0][4],$A[1][4]
140 eor $C[0],$C[0],$A[2][0]
141 eor $C[1],$C[1],$A[2][1]
142 eor $C[2],$C[2],$A[2][2]
143 eor $C[3],$C[3],$A[2][3]
144 eor $C[4],$C[4],$A[2][4]
145 eor $C[0],$C[0],$A[3][0]
146 eor $C[1],$C[1],$A[3][1]
147 eor $C[2],$C[2],$A[3][2]
148 eor $C[3],$C[3],$A[3][3]
149 eor $C[4],$C[4],$A[3][4]
150 eor $C[0],$C[0],$A[4][0]
151 eor $C[2],$C[2],$A[4][2]
152 eor $C[1],$C[1],$A[4][1]
153 eor $C[3],$C[3],$A[4][3]
154 eor $C[4],$C[4],$A[4][4]
156 eor $C[5],$C[0],$C[2],ror#63
158 eor $A[0][1],$A[0][1],$C[5]
159 eor $A[1][1],$A[1][1],$C[5]
160 eor $A[2][1],$A[2][1],$C[5]
161 eor $A[3][1],$A[3][1],$C[5]
162 eor $A[4][1],$A[4][1],$C[5]
164 eor $C[5],$C[1],$C[3],ror#63
165 eor $C[2],$C[2],$C[4],ror#63
166 eor $C[3],$C[3],$C[0],ror#63
167 eor $C[4],$C[4],$C[1],ror#63
169 eor $C[1], $A[0][2],$C[5] // mov $C[1],$A[0][2]
170 eor $A[1][2],$A[1][2],$C[5]
171 eor $A[2][2],$A[2][2],$C[5]
172 eor $A[3][2],$A[3][2],$C[5]
173 eor $A[4][2],$A[4][2],$C[5]
175 eor $A[0][0],$A[0][0],$C[4]
176 eor $A[1][0],$A[1][0],$C[4]
177 eor $A[2][0],$A[2][0],$C[4]
178 eor $A[3][0],$A[3][0],$C[4]
179 eor $A[4][0],$A[4][0],$C[4]
184 ldp $A[0][4],$A[1][4],[sp,#0] // re-load offloaded data
185 eor $C[0], $A[0][3],$C[2] // mov $C[0],$A[0][3]
186 eor $A[1][3],$A[1][3],$C[2]
187 eor $A[2][3],$A[2][3],$C[2]
188 eor $A[3][3],$A[3][3],$C[2]
189 eor $A[4][3],$A[4][3],$C[2]
191 eor $C[2], $A[0][4],$C[3] // mov $C[2],$A[0][4]
192 eor $A[1][4],$A[1][4],$C[3]
193 eor $A[2][4],$A[2][4],$C[3]
194 eor $A[3][4],$A[3][4],$C[3]
195 eor $A[4][4],$A[4][4],$C[3]
197 ////////////////////////////////////////// Rho+Pi
199 ror $A[0][1],$A[1][1],#64-$rhotates[1][1]
201 ror $A[0][2],$A[2][2],#64-$rhotates[2][2]
203 ror $A[0][3],$A[3][3],#64-$rhotates[3][3]
205 ror $A[0][4],$A[4][4],#64-$rhotates[4][4]
207 ror $A[1][1],$A[1][4],#64-$rhotates[1][4]
208 ror $A[2][2],$A[2][3],#64-$rhotates[2][3]
209 ror $A[3][3],$A[3][2],#64-$rhotates[3][2]
210 ror $A[4][4],$A[4][1],#64-$rhotates[4][1]
212 ror $A[1][4],$A[4][2],#64-$rhotates[4][2]
213 ror $A[2][3],$A[3][4],#64-$rhotates[3][4]
214 ror $A[3][2],$A[2][1],#64-$rhotates[2][1]
215 ror $A[4][1],$A[1][3],#64-$rhotates[1][3]
217 ror $A[4][2],$A[2][4],#64-$rhotates[2][4]
218 ror $A[3][4],$A[4][3],#64-$rhotates[4][3]
219 ror $A[2][1],$A[1][2],#64-$rhotates[1][2]
220 ror $A[1][3],$A[3][1],#64-$rhotates[3][1]
222 ror $A[2][4],$A[4][0],#64-$rhotates[4][0]
223 ror $A[4][3],$A[3][0],#64-$rhotates[3][0]
224 ror $A[1][2],$A[2][0],#64-$rhotates[2][0]
225 ror $A[3][1],$A[1][0],#64-$rhotates[1][0]
227 ror $A[1][0],$C[0],#64-$rhotates[0][3]
228 ror $A[2][0],$C[3],#64-$rhotates[0][1]
229 ror $A[3][0],$C[2],#64-$rhotates[0][4]
230 ror $A[4][0],$C[1],#64-$rhotates[0][2]
232 ////////////////////////////////////////// Chi+Iota
233 bic $C[0],$A[0][2],$A[0][1]
234 bic $C[1],$A[0][3],$A[0][2]
235 bic $C[2],$A[0][0],$A[0][4]
236 bic $C[3],$A[0][1],$A[0][0]
237 eor $A[0][0],$A[0][0],$C[0]
238 bic $C[0],$A[0][4],$A[0][3]
239 eor $A[0][1],$A[0][1],$C[1]
241 eor $A[0][3],$A[0][3],$C[2]
242 eor $A[0][4],$A[0][4],$C[3]
243 eor $A[0][2],$A[0][2],$C[0]
244 ldr $C[3],[$C[1]],#8 // Iota[i++]
246 bic $C[0],$A[1][2],$A[1][1]
247 tst $C[1],#255 // are we done?
249 bic $C[1],$A[1][3],$A[1][2]
250 bic $C[2],$A[1][0],$A[1][4]
251 eor $A[0][0],$A[0][0],$C[3] // A[0][0] ^= Iota
252 bic $C[3],$A[1][1],$A[1][0]
253 eor $A[1][0],$A[1][0],$C[0]
254 bic $C[0],$A[1][4],$A[1][3]
255 eor $A[1][1],$A[1][1],$C[1]
256 eor $A[1][3],$A[1][3],$C[2]
257 eor $A[1][4],$A[1][4],$C[3]
258 eor $A[1][2],$A[1][2],$C[0]
260 bic $C[0],$A[2][2],$A[2][1]
261 bic $C[1],$A[2][3],$A[2][2]
262 bic $C[2],$A[2][0],$A[2][4]
263 bic $C[3],$A[2][1],$A[2][0]
264 eor $A[2][0],$A[2][0],$C[0]
265 bic $C[0],$A[2][4],$A[2][3]
266 eor $A[2][1],$A[2][1],$C[1]
267 eor $A[2][3],$A[2][3],$C[2]
268 eor $A[2][4],$A[2][4],$C[3]
269 eor $A[2][2],$A[2][2],$C[0]
271 bic $C[0],$A[3][2],$A[3][1]
272 bic $C[1],$A[3][3],$A[3][2]
273 bic $C[2],$A[3][0],$A[3][4]
274 bic $C[3],$A[3][1],$A[3][0]
275 eor $A[3][0],$A[3][0],$C[0]
276 bic $C[0],$A[3][4],$A[3][3]
277 eor $A[3][1],$A[3][1],$C[1]
278 eor $A[3][3],$A[3][3],$C[2]
279 eor $A[3][4],$A[3][4],$C[3]
280 eor $A[3][2],$A[3][2],$C[0]
282 bic $C[0],$A[4][2],$A[4][1]
283 bic $C[1],$A[4][3],$A[4][2]
284 bic $C[2],$A[4][0],$A[4][4]
285 bic $C[3],$A[4][1],$A[4][0]
286 eor $A[4][0],$A[4][0],$C[0]
287 bic $C[0],$A[4][4],$A[4][3]
288 eor $A[4][1],$A[4][1],$C[1]
289 eor $A[4][3],$A[4][3],$C[2]
290 eor $A[4][4],$A[4][4],$C[3]
291 eor $A[4][2],$A[4][2],$C[0]
296 .inst 0xd50323bf // autiasp
298 .size KeccakF1600_int,.-KeccakF1600_int
300 .type KeccakF1600,%function
303 .inst 0xd503233f // paciasp
304 stp x29,x30,[sp,#-128]!
313 str x0,[sp,#32] // offload argument
315 ldp $A[0][0],$A[0][1],[x0,#16*0]
316 ldp $A[0][2],$A[0][3],[$C[0],#16*1]
317 ldp $A[0][4],$A[1][0],[$C[0],#16*2]
318 ldp $A[1][1],$A[1][2],[$C[0],#16*3]
319 ldp $A[1][3],$A[1][4],[$C[0],#16*4]
320 ldp $A[2][0],$A[2][1],[$C[0],#16*5]
321 ldp $A[2][2],$A[2][3],[$C[0],#16*6]
322 ldp $A[2][4],$A[3][0],[$C[0],#16*7]
323 ldp $A[3][1],$A[3][2],[$C[0],#16*8]
324 ldp $A[3][3],$A[3][4],[$C[0],#16*9]
325 ldp $A[4][0],$A[4][1],[$C[0],#16*10]
326 ldp $A[4][2],$A[4][3],[$C[0],#16*11]
327 ldr $A[4][4],[$C[0],#16*12]
332 stp $A[0][0],$A[0][1],[$C[0],#16*0]
333 stp $A[0][2],$A[0][3],[$C[0],#16*1]
334 stp $A[0][4],$A[1][0],[$C[0],#16*2]
335 stp $A[1][1],$A[1][2],[$C[0],#16*3]
336 stp $A[1][3],$A[1][4],[$C[0],#16*4]
337 stp $A[2][0],$A[2][1],[$C[0],#16*5]
338 stp $A[2][2],$A[2][3],[$C[0],#16*6]
339 stp $A[2][4],$A[3][0],[$C[0],#16*7]
340 stp $A[3][1],$A[3][2],[$C[0],#16*8]
341 stp $A[3][3],$A[3][4],[$C[0],#16*9]
342 stp $A[4][0],$A[4][1],[$C[0],#16*10]
343 stp $A[4][2],$A[4][3],[$C[0],#16*11]
344 str $A[4][4],[$C[0],#16*12]
346 ldp x19,x20,[x29,#16]
348 ldp x21,x22,[x29,#32]
349 ldp x23,x24,[x29,#48]
350 ldp x25,x26,[x29,#64]
351 ldp x27,x28,[x29,#80]
352 ldp x29,x30,[sp],#128
353 .inst 0xd50323bf // autiasp
355 .size KeccakF1600,.-KeccakF1600
358 .type SHA3_absorb,%function
361 .inst 0xd503233f // paciasp
362 stp x29,x30,[sp,#-128]!
371 stp x0,x1,[sp,#32] // offload arguments
374 mov $C[0],x0 // uint64_t A[5][5]
375 mov $C[1],x1 // const void *inp
376 mov $C[2],x2 // size_t len
377 mov $C[3],x3 // size_t bsz
378 ldp $A[0][0],$A[0][1],[$C[0],#16*0]
379 ldp $A[0][2],$A[0][3],[$C[0],#16*1]
380 ldp $A[0][4],$A[1][0],[$C[0],#16*2]
381 ldp $A[1][1],$A[1][2],[$C[0],#16*3]
382 ldp $A[1][3],$A[1][4],[$C[0],#16*4]
383 ldp $A[2][0],$A[2][1],[$C[0],#16*5]
384 ldp $A[2][2],$A[2][3],[$C[0],#16*6]
385 ldp $A[2][4],$A[3][0],[$C[0],#16*7]
386 ldp $A[3][1],$A[3][2],[$C[0],#16*8]
387 ldp $A[3][3],$A[3][4],[$C[0],#16*9]
388 ldp $A[4][0],$A[4][1],[$C[0],#16*10]
389 ldp $A[4][2],$A[4][3],[$C[0],#16*11]
390 ldr $A[4][4],[$C[0],#16*12]
395 subs $C[0],$C[2],$C[3] // len - bsz
398 str $C[0],[sp,#48] // save len - bsz
400 for (my $i=0; $i<24; $i+=2) {
403 ldr $C[0],[$C[1]],#8 // *inp++
407 eor $A[$i/5][$i%5],$A[$i/5][$i%5],$C[0]
410 ldr $C[0],[$C[1]],#8 // *inp++
414 eor $A[$j/5][$j%5],$A[$j/5][$j%5],$C[0]
419 ldr $C[0],[$C[1]],#8 // *inp++
423 eor $A[4][4],$A[4][4],$C[0]
426 str $C[1],[sp,#40] // save inp
430 ldr $C[1],[sp,#40] // restore arguments
431 ldp $C[2],$C[3],[sp,#48]
437 stp $A[0][0],$A[0][1],[$C[1],#16*0]
438 stp $A[0][2],$A[0][3],[$C[1],#16*1]
439 stp $A[0][4],$A[1][0],[$C[1],#16*2]
440 stp $A[1][1],$A[1][2],[$C[1],#16*3]
441 stp $A[1][3],$A[1][4],[$C[1],#16*4]
442 stp $A[2][0],$A[2][1],[$C[1],#16*5]
443 stp $A[2][2],$A[2][3],[$C[1],#16*6]
444 stp $A[2][4],$A[3][0],[$C[1],#16*7]
445 stp $A[3][1],$A[3][2],[$C[1],#16*8]
446 stp $A[3][3],$A[3][4],[$C[1],#16*9]
447 stp $A[4][0],$A[4][1],[$C[1],#16*10]
448 stp $A[4][2],$A[4][3],[$C[1],#16*11]
449 str $A[4][4],[$C[1],#16*12]
451 mov x0,$C[2] // return value
452 ldp x19,x20,[x29,#16]
454 ldp x21,x22,[x29,#32]
455 ldp x23,x24,[x29,#48]
456 ldp x25,x26,[x29,#64]
457 ldp x27,x28,[x29,#80]
458 ldp x29,x30,[sp],#128
459 .inst 0xd50323bf // autiasp
461 .size SHA3_absorb,.-SHA3_absorb
464 my ($A_flat,$out,$len,$bsz) = map("x$_",(19..22));
467 .type SHA3_squeeze,%function
470 .inst 0xd503233f // paciasp
471 stp x29,x30,[sp,#-48]!
476 mov $A_flat,x0 // put aside arguments
533 .inst 0xd50323bf // autiasp
535 .size SHA3_squeeze,.-SHA3_squeeze
539 my @A = map([ "v".$_.".16b", "v".($_+1).".16b", "v".($_+2).".16b",
540 "v".($_+3).".16b", "v".($_+4).".16b" ],
543 my @C = map("v$_.16b", (25..31));
546 .type KeccakF1600_ce,%function
555 for($i=0; $i<2; $i++) {
557 ////////////////////////////////////////////////// Theta
558 eor3 $C[0],$A[0][0],$A[1][0],$A[2][0]
559 eor3 $C[1],$A[0][1],$A[1][1],$A[2][1]
560 eor3 $C[2],$A[0][2],$A[1][2],$A[2][2]
561 eor3 $C[3],$A[0][3],$A[1][3],$A[2][3]
562 eor3 $C[4],$A[0][4],$A[1][4],$A[2][4]
563 eor3 $C[0],$C[0], $A[3][0],$A[4][0]
564 eor3 $C[1],$C[1], $A[3][1],$A[4][1]
565 eor3 $C[2],$C[2], $A[3][2],$A[4][2]
566 eor3 $C[3],$C[3], $A[3][3],$A[4][3]
567 eor3 $C[4],$C[4], $A[3][4],$A[4][4]
569 rax1 $C[5],$C[0],$C[2] // D[1]
570 rax1 $C[6],$C[1],$C[3] // D[2]
571 rax1 $C[2],$C[2],$C[4] // D[3]
572 rax1 $C[3],$C[3],$C[0] // D[4]
573 rax1 $C[4],$C[4],$C[1] // D[0]
575 ////////////////////////////////////////////////// Theta+Rho+Pi
576 xar $C[0], $A[1][1],$C[5],#64-$rhotates[1][1] // C[0]=A[0][1]
577 xar $A[1][1],$A[1][4],$C[3],#64-$rhotates[1][4]
578 xar $A[1][4],$A[4][2],$C[6],#64-$rhotates[4][2]
579 xar $A[4][2],$A[2][4],$C[3],#64-$rhotates[2][4]
580 xar $A[2][4],$A[4][0],$C[4],#64-$rhotates[4][0]
582 xar $A[4][0],$A[0][2],$C[6],#64-$rhotates[0][2]
584 xar $A[0][2],$A[2][2],$C[6],#64-$rhotates[2][2]
585 xar $A[2][2],$A[2][3],$C[2],#64-$rhotates[2][3]
586 xar $A[2][3],$A[3][4],$C[3],#64-$rhotates[3][4]
587 xar $A[3][4],$A[4][3],$C[2],#64-$rhotates[4][3]
588 xar $A[4][3],$A[3][0],$C[4],#64-$rhotates[3][0]
590 xar $A[3][0],$A[0][4],$C[3],#64-$rhotates[0][4]
592 eor $A[0][0],$A[0][0],$C[4]
595 xar $C[1], $A[3][3],$C[2],#64-$rhotates[3][3] // C[1]=A[0][3]
596 xar $A[3][3],$A[3][2],$C[6],#64-$rhotates[3][2]
597 xar $A[3][2],$A[2][1],$C[5],#64-$rhotates[2][1]
598 xar $A[2][1],$A[1][2],$C[6],#64-$rhotates[1][2]
599 xar $A[1][2],$A[2][0],$C[4],#64-$rhotates[2][0]
601 xar $A[2][0],$A[0][1],$C[5],#64-$rhotates[0][1] // *
603 xar $A[0][4],$A[4][4],$C[3],#64-$rhotates[4][4]
604 xar $A[4][4],$A[4][1],$C[5],#64-$rhotates[4][1]
605 xar $A[4][1],$A[1][3],$C[2],#64-$rhotates[1][3]
606 xar $A[1][3],$A[3][1],$C[5],#64-$rhotates[3][1]
607 xar $A[3][1],$A[1][0],$C[4],#64-$rhotates[1][0]
609 xar $C[2], $A[0][3],$C[2],#64-$rhotates[0][3] // C[2]=A[1][0]
611 ////////////////////////////////////////////////// Chi+Iota
612 dup $C[6],x11 // borrow C[6]
613 bcax $C[3], $A[0][0],$A[0][2],$C[0] // *
614 bcax $A[0][1],$C[0], $C[1], $A[0][2] // *
615 bcax $A[0][2],$A[0][2],$A[0][4],$C[1]
616 bcax $A[0][3],$C[1], $A[0][0],$A[0][4]
617 bcax $A[0][4],$A[0][4],$C[0], $A[0][0]
619 bcax $A[1][0],$C[2], $A[1][2],$A[1][1] // *
620 bcax $C[0], $A[1][1],$A[1][3],$A[1][2] // *
621 bcax $A[1][2],$A[1][2],$A[1][4],$A[1][3]
622 bcax $A[1][3],$A[1][3],$C[2], $A[1][4]
623 bcax $A[1][4],$A[1][4],$A[1][1],$C[2]
625 eor $A[0][0],$C[3],$C[6] // Iota
627 bcax $C[1], $A[2][0],$A[2][2],$A[2][1] // *
628 bcax $C[2], $A[2][1],$A[2][3],$A[2][2] // *
629 bcax $A[2][2],$A[2][2],$A[2][4],$A[2][3]
630 bcax $A[2][3],$A[2][3],$A[2][0],$A[2][4]
631 bcax $A[2][4],$A[2][4],$A[2][1],$A[2][0]
633 bcax $C[3], $A[3][0],$A[3][2],$A[3][1] // *
634 bcax $C[4], $A[3][1],$A[3][3],$A[3][2] // *
635 bcax $A[3][2],$A[3][2],$A[3][4],$A[3][3]
636 bcax $A[3][3],$A[3][3],$A[3][0],$A[3][4]
637 bcax $A[3][4],$A[3][4],$A[3][1],$A[3][0]
639 bcax $C[5], $A[4][0],$A[4][2],$A[4][1] // *
640 bcax $C[6], $A[4][1],$A[4][3],$A[4][2] // *
641 bcax $A[4][2],$A[4][2],$A[4][4],$A[4][3]
642 bcax $A[4][3],$A[4][3],$A[4][0],$A[4][4]
643 bcax $A[4][4],$A[4][4],$A[4][1],$A[4][0]
645 ( $A[1][1], $C[0]) = ( $C[0], $A[1][1]);
646 ($A[2][0],$A[2][1], $C[1],$C[2]) = ($C[1],$C[2], $A[2][0],$A[2][1]);
647 ($A[3][0],$A[3][1], $C[3],$C[4]) = ($C[3],$C[4], $A[3][0],$A[3][1]);
648 ($A[4][0],$A[4][1], $C[5],$C[6]) = ($C[5],$C[6], $A[4][0],$A[4][1]);
655 .size KeccakF1600_ce,.-KeccakF1600_ce
657 .type KeccakF1600_cext,%function
660 .inst 0xd503233f // paciasp
661 stp x29,x30,[sp,#-80]!
663 stp d8,d9,[sp,#16] // per ABI requirement
668 for($i=0; $i<24; $i+=2) { # load A[5][5]
671 ldp d$i,d$j,[x0,#8*$i]
679 for($i=0; $i<24; $i+=2) { # store A[5][5]
682 stp d$i,d$j,[x0,#8*$i]
693 .inst 0xd50323bf // autiasp
695 .size KeccakF1600_cext,.-KeccakF1600_cext
699 my ($ctx,$inp,$len,$bsz) = map("x$_",(0..3));
702 .globl SHA3_absorb_cext
703 .type SHA3_absorb_cext,%function
706 .inst 0xd503233f // paciasp
707 stp x29,x30,[sp,#-80]!
709 stp d8,d9,[sp,#16] // per ABI requirement
714 for($i=0; $i<24; $i+=2) { # load A[5][5]
717 ldp d$i,d$j,[x0,#8*$i]
726 subs $len,$len,$bsz // len - bsz
729 for (my $i=0; $i<24; $i+=2) {
732 ldr d31,[$inp],#8 // *inp++
734 rev64 v31.16b,v31.16b
736 eor $A[$i/5][$i%5],$A[$i/5][$i%5],v31.16b
738 blo .Lprocess_block_ce
739 ldr d31,[$inp],#8 // *inp++
743 eor $A[$j/5][$j%5],$A[$j/5][$j%5],v31.16b
744 beq .Lprocess_block_ce
748 ldr d31,[$inp],#8 // *inp++
752 eor $A[4][4],$A[4][4],v31.16b
763 for($i=0; $i<24; $i+=2) { # store A[5][5]
766 stp d$i,d$j,[x0,#8*$i]
771 add x0,$len,$bsz // return value
778 .inst 0xd50323bf // autiasp
780 .size SHA3_absorb_cext,.-SHA3_absorb_cext
784 my ($ctx,$out,$len,$bsz) = map("x$_",(0..3));
786 .globl SHA3_squeeze_cext
787 .type SHA3_squeeze_cext,%function
790 .inst 0xd503233f // paciasp
791 stp x29,x30,[sp,#-16]!
799 blo .Lsqueeze_tail_ce
804 beq .Lsqueeze_done_ce
821 beq .Lsqueeze_done_ce
825 beq .Lsqueeze_done_ce
829 beq .Lsqueeze_done_ce
833 beq .Lsqueeze_done_ce
837 beq .Lsqueeze_done_ce
841 beq .Lsqueeze_done_ce
846 .inst 0xd50323bf // autiasp
848 .size SHA3_squeeze_cext,.-SHA3_squeeze_cext
852 .asciz "Keccak-1600 absorb and squeeze for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
856 "rax1" => 0xce608c00, "eor3" => 0xce000000,
857 "bcax" => 0xce200000, "xar" => 0xce800000 );
860 my ($mnemonic,$arg)=@_;
862 $arg =~ m/[qv]([0-9]+)[^,]*,\s*[qv]([0-9]+)[^,]*(?:,\s*[qv]([0-9]+)[^,]*(?:,\s*[qv#]([0-9\-]+))?)?/
864 sprintf ".inst\t0x%08x\t//%s %s",
865 $opcode{$mnemonic}|$1|($2<<5)|($3<<16)|(eval($4)<<10),
870 foreach(split("\n",$code)) {
872 s/\`([^\`]*)\`/eval($1)/ge;
874 m/\bdup\b/ and s/\.16b/.2d/g or
875 s/\b(eor3|rax1|xar|bcax)\s+(v.*)/unsha3($1,$2)/ge;