Debian Codegraph @

external/boringssl/crypto/aes/asm/aesv8-armx.pl

raw
1 #!/usr/bin/env perl
2 #
3 # ====================================================================
4 # Written by Andy Polyakov <[email protected]> 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 # ====================================================================
9 #
10 # This module implements support for ARMv8 AES instructions. The
11 # module is endian-agnostic in sense that it supports both big- and
12 # little-endian cases. As does it support both 32- and 64-bit modes
13 # of operation. Latter is achieved by limiting amount of utilized
14 # registers to 16, which implies additional NEON load and integer
15 # instructions. This has no effect on mighty Apple A7, where results
16 # are literally equal to the theoretical estimates based on AES
17 # instruction latencies and issue rates. On Cortex-A53, an in-order
18 # execution core, this costs up to 10-15%, which is partially
19 # compensated by implementing dedicated code path for 128-bit
20 # CBC encrypt case. On Cortex-A57 parallelizable mode performance
21 # seems to be limited by sheer amount of NEON instructions...
22 #
23 # Performance in cycles per byte processed with 128-bit key:
24 #
25 # CBC enc CBC dec CTR
26 # Apple A7 2.39 1.20 1.20
27 # Cortex-A53 1.32 1.29 1.46
28 # Cortex-A57(*) 1.95 0.85 0.93
29 # Denver 1.96 0.86 0.80
30 #
31 # (*) original 3.64/1.34/1.32 results were for r0p0 revision
32 # and are still same even for updated module;
33
34 $flavour = shift;
35 $output = shift;
36
37 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
38 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
39 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
40 die "can't locate arm-xlate.pl";
41
42 open OUT,"| \"$^X\" $xlate $flavour $output";
43 *STDOUT=*OUT;
44
45 $prefix="aes_v8";
46
47 $code=<<___;
48 #include <openssl/arm_arch.h>
49
50 #if __ARM_MAX_ARCH__>=7
51 .text
52 ___
53 $code.=<<___ if ($flavour =~ /64/);
54 .arch armv8-a+crypto
55 ___
56 $code.=".arch armv7-a\n.fpu neon\n.code 32\n" if ($flavour !~ /64/);
57 #^^^^^^ this is done to simplify adoption by not depending
58 # on latest binutils.
59
60 # Assembler mnemonics are an eclectic mix of 32- and 64-bit syntax,
61 # NEON is mostly 32-bit mnemonics, integer - mostly 64. Goal is to
62 # maintain both 32- and 64-bit codes within single module and
63 # transliterate common code to either flavour with regex vodoo.
64 #
65 {{{
66 my ($inp,$bits,$out,$ptr,$rounds)=("x0","w1","x2","x3","w12");
67 my ($zero,$rcon,$mask,$in0,$in1,$tmp,$key)=
68 $flavour=~/64/? map("q$_",(0..6)) : map("q$_",(0..3,8..10));
69
70
71 $code.=<<___;
72 .align 5
73 .Lrcon:
74 .long 0x01,0x01,0x01,0x01
75 .long 0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d // rotate-n-splat
76 .long 0x1b,0x1b,0x1b,0x1b
77
78 .globl ${prefix}_set_encrypt_key
79 .type ${prefix}_set_encrypt_key,%function
80 .align 5
81 ${prefix}_set_encrypt_key:
82 .Lenc_key:
83 ___
84 $code.=<<___ if ($flavour =~ /64/);
85 stp x29,x30,[sp,#-16]!
86 add x29,sp,#0
87 ___
88 $code.=<<___;
89 mov $ptr,#-1
90 cmp $inp,#0
91 b.eq .Lenc_key_abort
92 cmp $out,#0
93 b.eq .Lenc_key_abort
94 mov $ptr,#-2
95 cmp $bits,#128
96 b.lt .Lenc_key_abort
97 cmp $bits,#256
98 b.gt .Lenc_key_abort
99 tst $bits,#0x3f
100 b.ne .Lenc_key_abort
101
102 adr $ptr,.Lrcon
103 cmp $bits,#192
104
105 veor $zero,$zero,$zero
106 vld1.8 {$in0},[$inp],#16
107 mov $bits,#8 // reuse $bits
108 vld1.32 {$rcon,$mask},[$ptr],#32
109
110 b.lt .Loop128
111 b.eq .L192
112 b .L256
113
114 .align 4
115 .Loop128:
116 vtbl.8 $key,{$in0},$mask
117 vext.8 $tmp,$zero,$in0,#12
118 vst1.32 {$in0},[$out],#16
119 aese $key,$zero
120 subs $bits,$bits,#1
121
122 veor $in0,$in0,$tmp
123 vext.8 $tmp,$zero,$tmp,#12
124 veor $in0,$in0,$tmp
125 vext.8 $tmp,$zero,$tmp,#12
126 veor $key,$key,$rcon
127 veor $in0,$in0,$tmp
128 vshl.u8 $rcon,$rcon,#1
129 veor $in0,$in0,$key
130 b.ne .Loop128
131
132 vld1.32 {$rcon},[$ptr]
133
134 vtbl.8 $key,{$in0},$mask
135 vext.8 $tmp,$zero,$in0,#12
136 vst1.32 {$in0},[$out],#16
137 aese $key,$zero
138
139 veor $in0,$in0,$tmp
140 vext.8 $tmp,$zero,$tmp,#12
141 veor $in0,$in0,$tmp
142 vext.8 $tmp,$zero,$tmp,#12
143 veor $key,$key,$rcon
144 veor $in0,$in0,$tmp
145 vshl.u8 $rcon,$rcon,#1
146 veor $in0,$in0,$key
147
148 vtbl.8 $key,{$in0},$mask
149 vext.8 $tmp,$zero,$in0,#12
150 vst1.32 {$in0},[$out],#16
151 aese $key,$zero
152
153 veor $in0,$in0,$tmp
154 vext.8 $tmp,$zero,$tmp,#12
155 veor $in0,$in0,$tmp
156 vext.8 $tmp,$zero,$tmp,#12
157 veor $key,$key,$rcon
158 veor $in0,$in0,$tmp
159 veor $in0,$in0,$key
160 vst1.32 {$in0},[$out]
161 add $out,$out,#0x50
162
163 mov $rounds,#10
164 b .Ldone
165
166 .align 4
167 .L192:
168 vld1.8 {$in1},[$inp],#8
169 vmov.i8 $key,#8 // borrow $key
170 vst1.32 {$in0},[$out],#16
171 vsub.i8 $mask,$mask,$key // adjust the mask
172
173 .Loop192:
174 vtbl.8 $key,{$in1},$mask
175 vext.8 $tmp,$zero,$in0,#12
176 vst1.32 {$in1},[$out],#8
177 aese $key,$zero
178 subs $bits,$bits,#1
179
180 veor $in0,$in0,$tmp
181 vext.8 $tmp,$zero,$tmp,#12
182 veor $in0,$in0,$tmp
183 vext.8 $tmp,$zero,$tmp,#12
184 veor $in0,$in0,$tmp
185
186 vdup.32 $tmp,${in0}[3]
187 veor $tmp,$tmp,$in1
188 veor $key,$key,$rcon
189 vext.8 $in1,$zero,$in1,#12
190 vshl.u8 $rcon,$rcon,#1
191 veor $in1,$in1,$tmp
192 veor $in0,$in0,$key
193 veor $in1,$in1,$key
194 vst1.32 {$in0},[$out],#16
195 b.ne .Loop192
196
197 mov $rounds,#12
198 add $out,$out,#0x20
199 b .Ldone
200
201 .align 4
202 .L256:
203 vld1.8 {$in1},[$inp]
204 mov $bits,#7
205 mov $rounds,#14
206 vst1.32 {$in0},[$out],#16
207
208 .Loop256:
209 vtbl.8 $key,{$in1},$mask
210 vext.8 $tmp,$zero,$in0,#12
211 vst1.32 {$in1},[$out],#16
212 aese $key,$zero
213 subs $bits,$bits,#1
214
215 veor $in0,$in0,$tmp
216 vext.8 $tmp,$zero,$tmp,#12
217 veor $in0,$in0,$tmp
218 vext.8 $tmp,$zero,$tmp,#12
219 veor $key,$key,$rcon
220 veor $in0,$in0,$tmp
221 vshl.u8 $rcon,$rcon,#1
222 veor $in0,$in0,$key
223 vst1.32 {$in0},[$out],#16
224 b.eq .Ldone
225
226 vdup.32 $key,${in0}[3] // just splat
227 vext.8 $tmp,$zero,$in1,#12
228 aese $key,$zero
229
230 veor $in1,$in1,$tmp
231 vext.8 $tmp,$zero,$tmp,#12
232 veor $in1,$in1,$tmp
233 vext.8 $tmp,$zero,$tmp,#12
234 veor $in1,$in1,$tmp
235
236 veor $in1,$in1,$key
237 b .Loop256
238
239 .Ldone:
240 str $rounds,[$out]
241 mov $ptr,#0
242
243 .Lenc_key_abort:
244 mov x0,$ptr // return value
245 `"ldr x29,[sp],#16" if ($flavour =~ /64/)`
246 ret
247 .size ${prefix}_set_encrypt_key,.-${prefix}_set_encrypt_key
248
249 .globl ${prefix}_set_decrypt_key
250 .type ${prefix}_set_decrypt_key,%function
251 .align 5
252 ${prefix}_set_decrypt_key:
253 ___
254 $code.=<<___ if ($flavour =~ /64/);
255 stp x29,x30,[sp,#-16]!
256 add x29,sp,#0
257 ___
258 $code.=<<___ if ($flavour !~ /64/);
259 stmdb sp!,{r4,lr}
260 ___
261 $code.=<<___;
262 bl .Lenc_key
263
264 cmp x0,#0
265 b.ne .Ldec_key_abort
266
267 sub $out,$out,#240 // restore original $out
268 mov x4,#-16
269 add $inp,$out,x12,lsl#4 // end of key schedule
270
271 vld1.32 {v0.16b},[$out]
272 vld1.32 {v1.16b},[$inp]
273 vst1.32 {v0.16b},[$inp],x4
274 vst1.32 {v1.16b},[$out],#16
275
276 .Loop_imc:
277 vld1.32 {v0.16b},[$out]
278 vld1.32 {v1.16b},[$inp]
279 aesimc v0.16b,v0.16b
280 aesimc v1.16b,v1.16b
281 vst1.32 {v0.16b},[$inp],x4
282 vst1.32 {v1.16b},[$out],#16
283 cmp $inp,$out
284 b.hi .Loop_imc
285
286 vld1.32 {v0.16b},[$out]
287 aesimc v0.16b,v0.16b
288 vst1.32 {v0.16b},[$inp]
289
290 eor x0,x0,x0 // return value
291 .Ldec_key_abort:
292 ___
293 $code.=<<___ if ($flavour !~ /64/);
294 ldmia sp!,{r4,pc}
295 ___
296 $code.=<<___ if ($flavour =~ /64/);
297 ldp x29,x30,[sp],#16
298 ret
299 ___
300 $code.=<<___;
301 .size ${prefix}_set_decrypt_key,.-${prefix}_set_decrypt_key
302 ___
303 }}}
304 {{{
305 sub gen_block () {
306 my $dir = shift;
307 my ($e,$mc) = $dir eq "en" ? ("e","mc") : ("d","imc");
308 my ($inp,$out,$key)=map("x$_",(0..2));
309 my $rounds="w3";
310 my ($rndkey0,$rndkey1,$inout)=map("q$_",(0..3));
311
312 $code.=<<___;
313 .globl ${prefix}_${dir}crypt
314 .type ${prefix}_${dir}crypt,%function
315 .align 5
316 ${prefix}_${dir}crypt:
317 ldr $rounds,[$key,#240]
318 vld1.32 {$rndkey0},[$key],#16
319 vld1.8 {$inout},[$inp]
320 sub $rounds,$rounds,#2
321 vld1.32 {$rndkey1},[$key],#16
322
323 .Loop_${dir}c:
324 aes$e $inout,$rndkey0
325 aes$mc $inout,$inout
326 vld1.32 {$rndkey0},[$key],#16
327 subs $rounds,$rounds,#2
328 aes$e $inout,$rndkey1
329 aes$mc $inout,$inout
330 vld1.32 {$rndkey1},[$key],#16
331 b.gt .Loop_${dir}c
332
333 aes$e $inout,$rndkey0
334 aes$mc $inout,$inout
335 vld1.32 {$rndkey0},[$key]
336 aes$e $inout,$rndkey1
337 veor $inout,$inout,$rndkey0
338
339 vst1.8 {$inout},[$out]
340 ret
341 .size ${prefix}_${dir}crypt,.-${prefix}_${dir}crypt
342 ___
343 }
344 &gen_block("en");
345 &gen_block("de");
346 }}}
347 {{{
348 my ($inp,$out,$len,$key,$ivp)=map("x$_",(0..4)); my $enc="w5";
349 my ($rounds,$cnt,$key_,$step,$step1)=($enc,"w6","x7","x8","x12");
350 my ($dat0,$dat1,$in0,$in1,$tmp0,$tmp1,$ivec,$rndlast)=map("q$_",(0..7));
351
352 my ($dat,$tmp,$rndzero_n_last)=($dat0,$tmp0,$tmp1);
353 my ($key4,$key5,$key6,$key7)=("x6","x12","x14",$key);
354
355 ### q8-q15 preloaded key schedule
356
357 $code.=<<___;
358 .globl ${prefix}_cbc_encrypt
359 .type ${prefix}_cbc_encrypt,%function
360 .align 5
361 ${prefix}_cbc_encrypt:
362 ___
363 $code.=<<___ if ($flavour =~ /64/);
364 stp x29,x30,[sp,#-16]!
365 add x29,sp,#0
366 ___
367 $code.=<<___ if ($flavour !~ /64/);
368 mov ip,sp
369 stmdb sp!,{r4-r8,lr}
370 vstmdb sp!,{d8-d15} @ ABI specification says so
371 ldmia ip,{r4-r5} @ load remaining args
372 ___
373 $code.=<<___;
374 subs $len,$len,#16
375 mov $step,#16
376 b.lo .Lcbc_abort
377 cclr $step,eq
378
379 cmp $enc,#0 // en- or decrypting?
380 ldr $rounds,[$key,#240]
381 and $len,$len,#-16
382 vld1.8 {$ivec},[$ivp]
383 vld1.8 {$dat},[$inp],$step
384
385 vld1.32 {q8-q9},[$key] // load key schedule...
386 sub $rounds,$rounds,#6
387 add $key_,$key,x5,lsl#4 // pointer to last 7 round keys
388 sub $rounds,$rounds,#2
389 vld1.32 {q10-q11},[$key_],#32
390 vld1.32 {q12-q13},[$key_],#32
391 vld1.32 {q14-q15},[$key_],#32
392 vld1.32 {$rndlast},[$key_]
393
394 add $key_,$key,#32
395 mov $cnt,$rounds
396 b.eq .Lcbc_dec
397
398 cmp $rounds,#2
399 veor $dat,$dat,$ivec
400 veor $rndzero_n_last,q8,$rndlast
401 b.eq .Lcbc_enc128
402
403 vld1.32 {$in0-$in1},[$key_]
404 add $key_,$key,#16
405 add $key4,$key,#16*4
406 add $key5,$key,#16*5
407 aese $dat,q8
408 aesmc $dat,$dat
409 add $key6,$key,#16*6
410 add $key7,$key,#16*7
411 b .Lenter_cbc_enc
412
413 .align 4
414 .Loop_cbc_enc:
415 aese $dat,q8
416 aesmc $dat,$dat
417 vst1.8 {$ivec},[$out],#16
418 .Lenter_cbc_enc:
419 aese $dat,q9
420 aesmc $dat,$dat
421 aese $dat,$in0
422 aesmc $dat,$dat
423 vld1.32 {q8},[$key4]
424 cmp $rounds,#4
425 aese $dat,$in1
426 aesmc $dat,$dat
427 vld1.32 {q9},[$key5]
428 b.eq .Lcbc_enc192
429
430 aese $dat,q8
431 aesmc $dat,$dat
432 vld1.32 {q8},[$key6]
433 aese $dat,q9
434 aesmc $dat,$dat
435 vld1.32 {q9},[$key7]
436 nop
437
438 .Lcbc_enc192:
439 aese $dat,q8
440 aesmc $dat,$dat
441 subs $len,$len,#16
442 aese $dat,q9
443 aesmc $dat,$dat
444 cclr $step,eq
445 aese $dat,q10
446 aesmc $dat,$dat
447 aese $dat,q11
448 aesmc $dat,$dat
449 vld1.8 {q8},[$inp],$step
450 aese $dat,q12
451 aesmc $dat,$dat
452 veor q8,q8,$rndzero_n_last
453 aese $dat,q13
454 aesmc $dat,$dat
455 vld1.32 {q9},[$key_] // re-pre-load rndkey[1]
456 aese $dat,q14
457 aesmc $dat,$dat
458 aese $dat,q15
459 veor $ivec,$dat,$rndlast
460 b.hs .Loop_cbc_enc
461
462 vst1.8 {$ivec},[$out],#16
463 b .Lcbc_done
464
465 .align 5
466 .Lcbc_enc128:
467 vld1.32 {$in0-$in1},[$key_]
468 aese $dat,q8
469 aesmc $dat,$dat
470 b .Lenter_cbc_enc128
471 .Loop_cbc_enc128:
472 aese $dat,q8
473 aesmc $dat,$dat
474 vst1.8 {$ivec},[$out],#16
475 .Lenter_cbc_enc128:
476 aese $dat,q9
477 aesmc $dat,$dat
478 subs $len,$len,#16
479 aese $dat,$in0
480 aesmc $dat,$dat
481 cclr $step,eq
482 aese $dat,$in1
483 aesmc $dat,$dat
484 aese $dat,q10
485 aesmc $dat,$dat
486 aese $dat,q11
487 aesmc $dat,$dat
488 vld1.8 {q8},[$inp],$step
489 aese $dat,q12
490 aesmc $dat,$dat
491 aese $dat,q13
492 aesmc $dat,$dat
493 aese $dat,q14
494 aesmc $dat,$dat
495 veor q8,q8,$rndzero_n_last
496 aese $dat,q15
497 veor $ivec,$dat,$rndlast
498 b.hs .Loop_cbc_enc128
499
500 vst1.8 {$ivec},[$out],#16
501 b .Lcbc_done
502 ___
503 {
504 my ($dat2,$in2,$tmp2)=map("q$_",(10,11,9));
505 $code.=<<___;
506 .align 5
507 .Lcbc_dec:
508 vld1.8 {$dat2},[$inp],#16
509 subs $len,$len,#32 // bias
510 add $cnt,$rounds,#2
511 vorr $in1,$dat,$dat
512 vorr $dat1,$dat,$dat
513 vorr $in2,$dat2,$dat2
514 b.lo .Lcbc_dec_tail
515
516 vorr $dat1,$dat2,$dat2
517 vld1.8 {$dat2},[$inp],#16
518 vorr $in0,$dat,$dat
519 vorr $in1,$dat1,$dat1
520 vorr $in2,$dat2,$dat2
521
522 .Loop3x_cbc_dec:
523 aesd $dat0,q8
524 aesimc $dat0,$dat0
525 aesd $dat1,q8
526 aesimc $dat1,$dat1
527 aesd $dat2,q8
528 aesimc $dat2,$dat2
529 vld1.32 {q8},[$key_],#16
530 subs $cnt,$cnt,#2
531 aesd $dat0,q9
532 aesimc $dat0,$dat0
533 aesd $dat1,q9
534 aesimc $dat1,$dat1
535 aesd $dat2,q9
536 aesimc $dat2,$dat2
537 vld1.32 {q9},[$key_],#16
538 b.gt .Loop3x_cbc_dec
539
540 aesd $dat0,q8
541 aesimc $dat0,$dat0
542 aesd $dat1,q8
543 aesimc $dat1,$dat1
544 aesd $dat2,q8
545 aesimc $dat2,$dat2
546 veor $tmp0,$ivec,$rndlast
547 subs $len,$len,#0x30
548 veor $tmp1,$in0,$rndlast
549 mov.lo x6,$len // x6, $cnt, is zero at this point
550 aesd $dat0,q9
551 aesimc $dat0,$dat0
552 aesd $dat1,q9
553 aesimc $dat1,$dat1
554 aesd $dat2,q9
555 aesimc $dat2,$dat2
556 veor $tmp2,$in1,$rndlast
557 add $inp,$inp,x6 // $inp is adjusted in such way that
558 // at exit from the loop $dat1-$dat2
559 // are loaded with last "words"
560 vorr $ivec,$in2,$in2
561 mov $key_,$key
562 aesd $dat0,q12
563 aesimc $dat0,$dat0
564 aesd $dat1,q12
565 aesimc $dat1,$dat1
566 aesd $dat2,q12
567 aesimc $dat2,$dat2
568 vld1.8 {$in0},[$inp],#16
569 aesd $dat0,q13
570 aesimc $dat0,$dat0
571 aesd $dat1,q13
572 aesimc $dat1,$dat1
573 aesd $dat2,q13
574 aesimc $dat2,$dat2
575 vld1.8 {$in1},[$inp],#16
576 aesd $dat0,q14
577 aesimc $dat0,$dat0
578 aesd $dat1,q14
579 aesimc $dat1,$dat1
580 aesd $dat2,q14
581 aesimc $dat2,$dat2
582 vld1.8 {$in2},[$inp],#16
583 aesd $dat0,q15
584 aesd $dat1,q15
585 aesd $dat2,q15
586 vld1.32 {q8},[$key_],#16 // re-pre-load rndkey[0]
587 add $cnt,$rounds,#2
588 veor $tmp0,$tmp0,$dat0
589 veor $tmp1,$tmp1,$dat1
590 veor $dat2,$dat2,$tmp2
591 vld1.32 {q9},[$key_],#16 // re-pre-load rndkey[1]
592 vst1.8 {$tmp0},[$out],#16
593 vorr $dat0,$in0,$in0
594 vst1.8 {$tmp1},[$out],#16
595 vorr $dat1,$in1,$in1
596 vst1.8 {$dat2},[$out],#16
597 vorr $dat2,$in2,$in2
598 b.hs .Loop3x_cbc_dec
599
600 cmn $len,#0x30
601 b.eq .Lcbc_done
602 nop
603
604 .Lcbc_dec_tail:
605 aesd $dat1,q8
606 aesimc $dat1,$dat1
607 aesd $dat2,q8
608 aesimc $dat2,$dat2
609 vld1.32 {q8},[$key_],#16
610 subs $cnt,$cnt,#2
611 aesd $dat1,q9
612 aesimc $dat1,$dat1
613 aesd $dat2,q9
614 aesimc $dat2,$dat2
615 vld1.32 {q9},[$key_],#16
616 b.gt .Lcbc_dec_tail
617
618 aesd $dat1,q8
619 aesimc $dat1,$dat1
620 aesd $dat2,q8
621 aesimc $dat2,$dat2
622 aesd $dat1,q9
623 aesimc $dat1,$dat1
624 aesd $dat2,q9
625 aesimc $dat2,$dat2
626 aesd $dat1,q12
627 aesimc $dat1,$dat1
628 aesd $dat2,q12
629 aesimc $dat2,$dat2
630 cmn $len,#0x20
631 aesd $dat1,q13
632 aesimc $dat1,$dat1
633 aesd $dat2,q13
634 aesimc $dat2,$dat2
635 veor $tmp1,$ivec,$rndlast
636 aesd $dat1,q14
637 aesimc $dat1,$dat1
638 aesd $dat2,q14
639 aesimc $dat2,$dat2
640 veor $tmp2,$in1,$rndlast
641 aesd $dat1,q15
642 aesd $dat2,q15
643 b.eq .Lcbc_dec_one
644 veor $tmp1,$tmp1,$dat1
645 veor $tmp2,$tmp2,$dat2
646 vorr $ivec,$in2,$in2
647 vst1.8 {$tmp1},[$out],#16
648 vst1.8 {$tmp2},[$out],#16
649 b .Lcbc_done
650
651 .Lcbc_dec_one:
652 veor $tmp1,$tmp1,$dat2
653 vorr $ivec,$in2,$in2
654 vst1.8 {$tmp1},[$out],#16
655
656 .Lcbc_done:
657 vst1.8 {$ivec},[$ivp]
658 .Lcbc_abort:
659 ___
660 }
661 $code.=<<___ if ($flavour !~ /64/);
662 vldmia sp!,{d8-d15}
663 ldmia sp!,{r4-r8,pc}
664 ___
665 $code.=<<___ if ($flavour =~ /64/);
666 ldr x29,[sp],#16
667 ret
668 ___
669 $code.=<<___;
670 .size ${prefix}_cbc_encrypt,.-${prefix}_cbc_encrypt
671 ___
672 }}}
673 {{{
674 my ($inp,$out,$len,$key,$ivp)=map("x$_",(0..4));
675 my ($rounds,$cnt,$key_)=("w5","w6","x7");
676 my ($ctr,$tctr0,$tctr1,$tctr2)=map("w$_",(8..10,12));
677 my $step="x12"; # aliases with $tctr2
678
679 my ($dat0,$dat1,$in0,$in1,$tmp0,$tmp1,$ivec,$rndlast)=map("q$_",(0..7));
680 my ($dat2,$in2,$tmp2)=map("q$_",(10,11,9));
681
682 my ($dat,$tmp)=($dat0,$tmp0);
683
684 ### q8-q15 preloaded key schedule
685
686 $code.=<<___;
687 .globl ${prefix}_ctr32_encrypt_blocks
688 .type ${prefix}_ctr32_encrypt_blocks,%function
689 .align 5
690 ${prefix}_ctr32_encrypt_blocks:
691 ___
692 $code.=<<___ if ($flavour =~ /64/);
693 stp x29,x30,[sp,#-16]!
694 add x29,sp,#0
695 ___
696 $code.=<<___ if ($flavour !~ /64/);
697 mov ip,sp
698 stmdb sp!,{r4-r10,lr}
699 vstmdb sp!,{d8-d15} @ ABI specification says so
700 ldr r4, [ip] @ load remaining arg
701 ___
702 $code.=<<___;
703 ldr $rounds,[$key,#240]
704
705 ldr $ctr, [$ivp, #12]
706 vld1.32 {$dat0},[$ivp]
707
708 vld1.32 {q8-q9},[$key] // load key schedule...
709 sub $rounds,$rounds,#4
710 mov $step,#16
711 cmp $len,#2
712 add $key_,$key,x5,lsl#4 // pointer to last 5 round keys
713 sub $rounds,$rounds,#2
714 vld1.32 {q12-q13},[$key_],#32
715 vld1.32 {q14-q15},[$key_],#32
716 vld1.32 {$rndlast},[$key_]
717 add $key_,$key,#32
718 mov $cnt,$rounds
719 cclr $step,lo
720 #ifndef __ARMEB__
721 rev $ctr, $ctr
722 #endif
723 vorr $dat1,$dat0,$dat0
724 add $tctr1, $ctr, #1
725 vorr $dat2,$dat0,$dat0
726 add $ctr, $ctr, #2
727 vorr $ivec,$dat0,$dat0
728 rev $tctr1, $tctr1
729 vmov.32 ${dat1}[3],$tctr1
730 b.ls .Lctr32_tail
731 rev $tctr2, $ctr
732 sub $len,$len,#3 // bias
733 vmov.32 ${dat2}[3],$tctr2
734 b .Loop3x_ctr32
735
736 .align 4
737 .Loop3x_ctr32:
738 aese $dat0,q8
739 aesmc $dat0,$dat0
740 aese $dat1,q8
741 aesmc $dat1,$dat1
742 aese $dat2,q8
743 aesmc $dat2,$dat2
744 vld1.32 {q8},[$key_],#16
745 subs $cnt,$cnt,#2
746 aese $dat0,q9
747 aesmc $dat0,$dat0
748 aese $dat1,q9
749 aesmc $dat1,$dat1
750 aese $dat2,q9
751 aesmc $dat2,$dat2
752 vld1.32 {q9},[$key_],#16
753 b.gt .Loop3x_ctr32
754
755 aese $dat0,q8
756 aesmc $tmp0,$dat0
757 aese $dat1,q8
758 aesmc $tmp1,$dat1
759 vld1.8 {$in0},[$inp],#16
760 vorr $dat0,$ivec,$ivec
761 aese $dat2,q8
762 aesmc $dat2,$dat2
763 vld1.8 {$in1},[$inp],#16
764 vorr $dat1,$ivec,$ivec
765 aese $tmp0,q9
766 aesmc $tmp0,$tmp0
767 aese $tmp1,q9
768 aesmc $tmp1,$tmp1
769 vld1.8 {$in2},[$inp],#16
770 mov $key_,$key
771 aese $dat2,q9
772 aesmc $tmp2,$dat2
773 vorr $dat2,$ivec,$ivec
774 add $tctr0,$ctr,#1
775 aese $tmp0,q12
776 aesmc $tmp0,$tmp0
777 aese $tmp1,q12
778 aesmc $tmp1,$tmp1
779 veor $in0,$in0,$rndlast
780 add $tctr1,$ctr,#2
781 aese $tmp2,q12
782 aesmc $tmp2,$tmp2
783 veor $in1,$in1,$rndlast
784 add $ctr,$ctr,#3
785 aese $tmp0,q13
786 aesmc $tmp0,$tmp0
787 aese $tmp1,q13
788 aesmc $tmp1,$tmp1
789 veor $in2,$in2,$rndlast
790 rev $tctr0,$tctr0
791 aese $tmp2,q13
792 aesmc $tmp2,$tmp2
793 vmov.32 ${dat0}[3], $tctr0
794 rev $tctr1,$tctr1
795 aese $tmp0,q14
796 aesmc $tmp0,$tmp0
797 aese $tmp1,q14
798 aesmc $tmp1,$tmp1
799 vmov.32 ${dat1}[3], $tctr1
800 rev $tctr2,$ctr
801 aese $tmp2,q14
802 aesmc $tmp2,$tmp2
803 vmov.32 ${dat2}[3], $tctr2
804 subs $len,$len,#3
805 aese $tmp0,q15
806 aese $tmp1,q15
807 aese $tmp2,q15
808
809 veor $in0,$in0,$tmp0
810 vld1.32 {q8},[$key_],#16 // re-pre-load rndkey[0]
811 vst1.8 {$in0},[$out],#16
812 veor $in1,$in1,$tmp1
813 mov $cnt,$rounds
814 vst1.8 {$in1},[$out],#16
815 veor $in2,$in2,$tmp2
816 vld1.32 {q9},[$key_],#16 // re-pre-load rndkey[1]
817 vst1.8 {$in2},[$out],#16
818 b.hs .Loop3x_ctr32
819
820 adds $len,$len,#3
821 b.eq .Lctr32_done
822 cmp $len,#1
823 mov $step,#16
824 cclr $step,eq
825
826 .Lctr32_tail:
827 aese $dat0,q8
828 aesmc $dat0,$dat0
829 aese $dat1,q8
830 aesmc $dat1,$dat1
831 vld1.32 {q8},[$key_],#16
832 subs $cnt,$cnt,#2
833 aese $dat0,q9
834 aesmc $dat0,$dat0
835 aese $dat1,q9
836 aesmc $dat1,$dat1
837 vld1.32 {q9},[$key_],#16
838 b.gt .Lctr32_tail
839
840 aese $dat0,q8
841 aesmc $dat0,$dat0
842 aese $dat1,q8
843 aesmc $dat1,$dat1
844 aese $dat0,q9
845 aesmc $dat0,$dat0
846 aese $dat1,q9
847 aesmc $dat1,$dat1
848 vld1.8 {$in0},[$inp],$step
849 aese $dat0,q12
850 aesmc $dat0,$dat0
851 aese $dat1,q12
852 aesmc $dat1,$dat1
853 vld1.8 {$in1},[$inp]
854 aese $dat0,q13
855 aesmc $dat0,$dat0
856 aese $dat1,q13
857 aesmc $dat1,$dat1
858 veor $in0,$in0,$rndlast
859 aese $dat0,q14
860 aesmc $dat0,$dat0
861 aese $dat1,q14
862 aesmc $dat1,$dat1
863 veor $in1,$in1,$rndlast
864 aese $dat0,q15
865 aese $dat1,q15
866
867 cmp $len,#1
868 veor $in0,$in0,$dat0
869 veor $in1,$in1,$dat1
870 vst1.8 {$in0},[$out],#16
871 b.eq .Lctr32_done
872 vst1.8 {$in1},[$out]
873
874 .Lctr32_done:
875 ___
876 $code.=<<___ if ($flavour !~ /64/);
877 vldmia sp!,{d8-d15}
878 ldmia sp!,{r4-r10,pc}
879 ___
880 $code.=<<___ if ($flavour =~ /64/);
881 ldr x29,[sp],#16
882 ret
883 ___
884 $code.=<<___;
885 .size ${prefix}_ctr32_encrypt_blocks,.-${prefix}_ctr32_encrypt_blocks
886 ___
887 }}}
888 $code.=<<___;
889 #endif
890 ___
891 ########################################
892 if ($flavour =~ /64/) { ######## 64-bit code
893 my %opcode = (
894 "aesd" => 0x4e285800, "aese" => 0x4e284800,
895 "aesimc"=> 0x4e287800, "aesmc" => 0x4e286800 );
896
897 local *unaes = sub {
898 my ($mnemonic,$arg)=@_;
899
900 $arg =~ m/[qv]([0-9]+)[^,]*,\s*[qv]([0-9]+)/o &&
901 sprintf ".inst\t0x%08x\t//%s %s",
902 $opcode{$mnemonic}|$1|($2<<5),
903 $mnemonic,$arg;
904 };
905
906 foreach(split("\n",$code)) {
907 s/\`([^\`]*)\`/eval($1)/geo;
908
909 s/\bq([0-9]+)\b/"v".($1<8?$1:$1+8).".16b"/geo; # old->new registers
910 s/@\s/\/\//o; # old->new style commentary
911
912 #s/[v]?(aes\w+)\s+([qv].*)/unaes($1,$2)/geo or
913 s/cclr\s+([wx])([^,]+),\s*([a-z]+)/csel $1$2,$1zr,$1$2,$3/o or
914 s/mov\.([a-z]+)\s+([wx][0-9]+),\s*([wx][0-9]+)/csel $2,$3,$2,$1/o or
915 s/vmov\.i8/movi/o or # fix up legacy mnemonics
916 s/vext\.8/ext/o or
917 s/vrev32\.8/rev32/o or
918 s/vtst\.8/cmtst/o or
919 s/vshr/ushr/o or
920 s/^(\s+)v/$1/o or # strip off v prefix
921 s/\bbx\s+lr\b/ret/o;
922
923 # fix up remainig legacy suffixes
924 s/\.[ui]?8//o;
925 m/\],#8/o and s/\.16b/\.8b/go;
926 s/\.[ui]?32//o and s/\.16b/\.4s/go;
927 s/\.[ui]?64//o and s/\.16b/\.2d/go;
928 s/\.[42]([sd])\[([0-3])\]/\.$1\[$2\]/o;
929
930 print $_,"\n";
931 }
932 } else { ######## 32-bit code
933 my %opcode = (
934 "aesd" => 0xf3b00340, "aese" => 0xf3b00300,
935 "aesimc"=> 0xf3b003c0, "aesmc" => 0xf3b00380 );
936
937 local *unaes = sub {
938 my ($mnemonic,$arg)=@_;
939
940 if ($arg =~ m/[qv]([0-9]+)[^,]*,\s*[qv]([0-9]+)/o) {
941 my $word = $opcode{$mnemonic}|(($1&7)<<13)|(($1&8)<<19)
942 |(($2&7)<<1) |(($2&8)<<2);
943 # since ARMv7 instructions are always encoded little-endian.
944 # correct solution is to use .inst directive, but older
945 # assemblers don't implement it:-(
946 sprintf ".byte\t0x%02x,0x%02x,0x%02x,0x%02x\t@ %s %s",
947 $word&0xff,($word>>8)&0xff,
948 ($word>>16)&0xff,($word>>24)&0xff,
949 $mnemonic,$arg;
950 }
951 };
952
953 sub unvtbl {
954 my $arg=shift;
955
956 $arg =~ m/q([0-9]+),\s*\{q([0-9]+)\},\s*q([0-9]+)/o &&
957 sprintf "vtbl.8 d%d,{q%d},d%d\n\t".
958 "vtbl.8 d%d,{q%d},d%d", 2*$1,$2,2*$3, 2*$1+1,$2,2*$3+1;
959 }
960
961 sub unvdup32 {
962 my $arg=shift;
963
964 $arg =~ m/q([0-9]+),\s*q([0-9]+)\[([0-3])\]/o &&
965 sprintf "vdup.32 q%d,d%d[%d]",$1,2*$2+($3>>1),$3&1;
966 }
967
968 sub unvmov32 {
969 my $arg=shift;
970
971 $arg =~ m/q([0-9]+)\[([0-3])\],(.*)/o &&
972 sprintf "vmov.32 d%d[%d],%s",2*$1+($2>>1),$2&1,$3;
973 }
974
975 foreach(split("\n",$code)) {
976 s/\`([^\`]*)\`/eval($1)/geo;
977
978 s/\b[wx]([0-9]+)\b/r$1/go; # new->old registers
979 s/\bv([0-9])\.[12468]+[bsd]\b/q$1/go; # new->old registers
980 s/\/\/\s?/@ /o; # new->old style commentary
981
982 # fix up remainig new-style suffixes
983 s/\{q([0-9]+)\},\s*\[(.+)\],#8/sprintf "{d%d},[$2]!",2*$1/eo or
984 s/\],#[0-9]+/]!/o;
985
986 s/[v]?(aes\w+)\s+([qv].*)/unaes($1,$2)/geo or
987 s/cclr\s+([^,]+),\s*([a-z]+)/mov$2 $1,#0/o or
988 s/vtbl\.8\s+(.*)/unvtbl($1)/geo or
989 s/vdup\.32\s+(.*)/unvdup32($1)/geo or
990 s/vmov\.32\s+(.*)/unvmov32($1)/geo or
991 s/^(\s+)b\./$1b/o or
992 s/^(\s+)mov\./$1mov/o or
993 s/^(\s+)ret/$1bx\tlr/o;
994
995 print $_,"\n";
996 }
997 }
998
999 close STDOUT;