3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
9 # Specific modes and adaptation for Linux kernel by Ard Biesheuvel
10 # <ard.biesheuvel@linaro.org>. Permission to use under GPL terms is
12 # ====================================================================
14 # Bit-sliced AES for ARM NEON
18 # This implementation is direct adaptation of bsaes-x86_64 module for
19 # ARM NEON. Except that this module is endian-neutral [in sense that
20 # it can be compiled for either endianness] by courtesy of vld1.8's
21 # neutrality. Initial version doesn't implement interface to OpenSSL,
22 # only low-level primitives and unsupported entry points, just enough
23 # to collect performance results, which for Cortex-A8 core are:
25 # encrypt 19.5 cycles per byte processed with 128-bit key
26 # decrypt 22.1 cycles per byte processed with 128-bit key
27 # key conv. 440 cycles per 128-bit key/0.18 of 8x block
29 # Snapdragon S4 encrypts byte in 17.6 cycles and decrypts in 19.7,
30 # which is [much] worse than anticipated (for further details see
31 # http://www.openssl.org/~appro/Snapdragon-S4.html).
33 # Cortex-A15 manages in 14.2/16.1 cycles [when integer-only code
34 # manages in 20.0 cycles].
36 # When comparing to x86_64 results keep in mind that NEON unit is
37 # [mostly] single-issue and thus can't [fully] benefit from
38 # instruction-level parallelism. And when comparing to aes-armv4
39 # results keep in mind key schedule conversion overhead (see
40 # bsaes-x86_64.pl for further details)...
46 # Add CBC, CTR and XTS subroutines, adapt for kernel use.
48 # <ard.biesheuvel@linaro.org>
51 if ($flavour=~/^\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
52 else { while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {} }
54 if ($flavour && $flavour ne "void") {
55 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
56 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
57 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
58 die "can't locate arm-xlate.pl";
60 open STDOUT,"| \"$^X\" $xlate $flavour $output";
62 open STDOUT,">$output";
65 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
66 my @XMM=map("q$_",(0..15));
69 my ($key,$rounds,$const)=("r4","r5","r6");
71 sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
72 sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
75 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
76 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
81 &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
82 &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
86 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
87 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
90 veor @b[2], @b[2], @b[1]
91 veor @b[5], @b[5], @b[6]
92 veor @b[3], @b[3], @b[0]
93 veor @b[6], @b[6], @b[2]
94 veor @b[5], @b[5], @b[0]
96 veor @b[6], @b[6], @b[3]
97 veor @b[3], @b[3], @b[7]
98 veor @b[7], @b[7], @b[5]
99 veor @b[3], @b[3], @b[4]
100 veor @b[4], @b[4], @b[5]
102 veor @b[2], @b[2], @b[7]
103 veor @b[3], @b[3], @b[1]
104 veor @b[1], @b[1], @b[5]
109 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
110 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
113 veor @b[0], @b[0], @b[6]
114 veor @b[1], @b[1], @b[4]
115 veor @b[4], @b[4], @b[6]
116 veor @b[2], @b[2], @b[0]
117 veor @b[6], @b[6], @b[1]
119 veor @b[1], @b[1], @b[5]
120 veor @b[5], @b[5], @b[3]
121 veor @b[3], @b[3], @b[7]
122 veor @b[7], @b[7], @b[5]
123 veor @b[2], @b[2], @b[5]
125 veor @b[4], @b[4], @b[7]
130 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
131 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
135 &InvInBasisChange (@b);
136 &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
137 &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
140 sub InvInBasisChange { # OutBasisChange in reverse (with twist)
141 my @b=@_[5,1,2,6,3,7,0,4];
143 veor @b[1], @b[1], @b[7]
144 veor @b[4], @b[4], @b[7]
146 veor @b[7], @b[7], @b[5]
147 veor @b[1], @b[1], @b[3]
148 veor @b[2], @b[2], @b[5]
149 veor @b[3], @b[3], @b[7]
151 veor @b[6], @b[6], @b[1]
152 veor @b[2], @b[2], @b[0]
153 veor @b[5], @b[5], @b[3]
154 veor @b[4], @b[4], @b[6]
155 veor @b[0], @b[0], @b[6]
156 veor @b[1], @b[1], @b[4]
160 sub InvOutBasisChange { # InBasisChange in reverse
161 my @b=@_[2,5,7,3,6,1,0,4];
163 veor @b[1], @b[1], @b[5]
164 veor @b[2], @b[2], @b[7]
166 veor @b[3], @b[3], @b[1]
167 veor @b[4], @b[4], @b[5]
168 veor @b[7], @b[7], @b[5]
169 veor @b[3], @b[3], @b[4]
170 veor @b[5], @b[5], @b[0]
171 veor @b[3], @b[3], @b[7]
172 veor @b[6], @b[6], @b[2]
173 veor @b[2], @b[2], @b[1]
174 veor @b[6], @b[6], @b[3]
176 veor @b[3], @b[3], @b[0]
177 veor @b[5], @b[5], @b[6]
182 #;*************************************************************
183 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
184 #;*************************************************************
185 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
197 sub Mul_GF4_N { # not used, see next subroutine
198 # multiply and scale by N
199 my ($x0,$x1,$y0,$y1,$t0)=@_;
212 # interleaved Mul_GF4_N and Mul_GF4
213 my ($x0,$x1,$y0,$y1,$t0,
214 $x2,$x3,$y2,$y3,$t1)=@_;
237 veor @t[0], @x[0], @x[2]
238 veor @t[1], @x[1], @x[3]
240 &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
242 veor @y[0], @y[0], @y[2]
243 veor @y[1], @y[1], @y[3]
245 Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
246 @x[2], @x[3], @y[2], @y[3], @t[2]);
248 veor @x[0], @x[0], @t[0]
249 veor @x[2], @x[2], @t[0]
250 veor @x[1], @x[1], @t[1]
251 veor @x[3], @x[3], @t[1]
253 veor @t[0], @x[4], @x[6]
254 veor @t[1], @x[5], @x[7]
256 &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
257 @x[6], @x[7], @y[2], @y[3], @t[2]);
259 veor @y[0], @y[0], @y[2]
260 veor @y[1], @y[1], @y[3]
262 &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
264 veor @x[4], @x[4], @t[0]
265 veor @x[6], @x[6], @t[0]
266 veor @x[5], @x[5], @t[1]
267 veor @x[7], @x[7], @t[1]
271 #;********************************************************************
272 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
273 #;********************************************************************
277 # direct optimizations from hardware
279 veor @t[3], @x[4], @x[6]
280 veor @t[2], @x[5], @x[7]
281 veor @t[1], @x[1], @x[3]
282 veor @s[1], @x[7], @x[6]
284 veor @s[0], @x[0], @x[2]
286 vorr @t[2], @t[2], @t[1]
287 veor @s[3], @t[3], @t[0]
288 vand @s[2], @t[3], @s[0]
289 vorr @t[3], @t[3], @s[0]
290 veor @s[0], @s[0], @t[1]
291 vand @t[0], @t[0], @t[1]
292 veor @t[1], @x[3], @x[2]
293 vand @s[3], @s[3], @s[0]
294 vand @s[1], @s[1], @t[1]
295 veor @t[1], @x[4], @x[5]
296 veor @s[0], @x[1], @x[0]
297 veor @t[3], @t[3], @s[1]
298 veor @t[2], @t[2], @s[1]
299 vand @s[1], @t[1], @s[0]
300 vorr @t[1], @t[1], @s[0]
301 veor @t[3], @t[3], @s[3]
302 veor @t[0], @t[0], @s[1]
303 veor @t[2], @t[2], @s[2]
304 veor @t[1], @t[1], @s[3]
305 veor @t[0], @t[0], @s[2]
306 vand @s[0], @x[7], @x[3]
307 veor @t[1], @t[1], @s[2]
308 vand @s[1], @x[6], @x[2]
309 vand @s[2], @x[5], @x[1]
310 vorr @s[3], @x[4], @x[0]
311 veor @t[3], @t[3], @s[0]
312 veor @t[1], @t[1], @s[2]
313 veor @t[0], @t[0], @s[3]
314 veor @t[2], @t[2], @s[1]
316 @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
318 @ new smaller inversion
320 vand @s[2], @t[3], @t[1]
323 veor @s[1], @t[2], @s[2]
324 veor @s[3], @t[0], @s[2]
325 veor @s[2], @t[0], @s[2] @ @s[2]=@s[3]
327 vbsl @s[1], @t[1], @t[0]
328 vbsl @s[3], @t[3], @t[2]
329 veor @t[3], @t[3], @t[2]
331 vbsl @s[0], @s[1], @s[2]
332 vbsl @t[0], @s[2], @s[1]
334 vand @s[2], @s[0], @s[3]
335 veor @t[1], @t[1], @t[0]
337 veor @s[2], @s[2], @t[3]
339 # output in s3, s2, s1, t1
341 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
343 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
344 &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
346 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
349 # AES linear components
356 vldmia $key!, {@t[0]-@t[3]}
357 veor @t[0], @t[0], @x[0]
358 veor @t[1], @t[1], @x[1]
359 vtbl.8 `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
360 vtbl.8 `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
361 vldmia $key!, {@t[0]}
362 veor @t[2], @t[2], @x[2]
363 vtbl.8 `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
364 vtbl.8 `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
365 vldmia $key!, {@t[1]}
366 veor @t[3], @t[3], @x[3]
367 vtbl.8 `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
368 vtbl.8 `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
369 vldmia $key!, {@t[2]}
370 vtbl.8 `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
371 vtbl.8 `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
372 vldmia $key!, {@t[3]}
373 veor @t[0], @t[0], @x[4]
374 veor @t[1], @t[1], @x[5]
375 vtbl.8 `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
376 vtbl.8 `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
377 veor @t[2], @t[2], @x[6]
378 vtbl.8 `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
379 vtbl.8 `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
380 veor @t[3], @t[3], @x[7]
381 vtbl.8 `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
382 vtbl.8 `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
383 vtbl.8 `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
384 vtbl.8 `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
389 # modified to emit output in order suitable for feeding back to aesenc[last]
392 my $inv=@_[16]; # optional
394 vext.8 @t[0], @x[0], @x[0], #12 @ x0 <<< 32
395 vext.8 @t[1], @x[1], @x[1], #12
396 veor @x[0], @x[0], @t[0] @ x0 ^ (x0 <<< 32)
397 vext.8 @t[2], @x[2], @x[2], #12
398 veor @x[1], @x[1], @t[1]
399 vext.8 @t[3], @x[3], @x[3], #12
400 veor @x[2], @x[2], @t[2]
401 vext.8 @t[4], @x[4], @x[4], #12
402 veor @x[3], @x[3], @t[3]
403 vext.8 @t[5], @x[5], @x[5], #12
404 veor @x[4], @x[4], @t[4]
405 vext.8 @t[6], @x[6], @x[6], #12
406 veor @x[5], @x[5], @t[5]
407 vext.8 @t[7], @x[7], @x[7], #12
408 veor @x[6], @x[6], @t[6]
410 veor @t[1], @t[1], @x[0]
411 veor @x[7], @x[7], @t[7]
412 vext.8 @x[0], @x[0], @x[0], #8 @ (x0 ^ (x0 <<< 32)) <<< 64)
413 veor @t[2], @t[2], @x[1]
414 veor @t[0], @t[0], @x[7]
415 veor @t[1], @t[1], @x[7]
416 vext.8 @x[1], @x[1], @x[1], #8
417 veor @t[5], @t[5], @x[4]
418 veor @x[0], @x[0], @t[0]
419 veor @t[6], @t[6], @x[5]
420 veor @x[1], @x[1], @t[1]
421 vext.8 @t[0], @x[4], @x[4], #8
422 veor @t[4], @t[4], @x[3]
423 vext.8 @t[1], @x[5], @x[5], #8
424 veor @t[7], @t[7], @x[6]
425 vext.8 @x[4], @x[3], @x[3], #8
426 veor @t[3], @t[3], @x[2]
427 vext.8 @x[5], @x[7], @x[7], #8
428 veor @t[4], @t[4], @x[7]
429 vext.8 @x[3], @x[6], @x[6], #8
430 veor @t[3], @t[3], @x[7]
431 vext.8 @x[6], @x[2], @x[2], #8
432 veor @x[7], @t[1], @t[5]
434 $code.=<<___ if (!$inv);
435 veor @x[2], @t[0], @t[4]
436 veor @x[4], @x[4], @t[3]
437 veor @x[5], @x[5], @t[7]
438 veor @x[3], @x[3], @t[6]
440 veor @x[6], @x[6], @t[2]
443 $code.=<<___ if ($inv);
444 veor @t[3], @t[3], @x[4]
445 veor @x[5], @x[5], @t[7]
446 veor @x[2], @x[3], @t[6]
447 veor @x[3], @t[0], @t[4]
448 veor @x[4], @x[6], @t[2]
454 sub InvMixColumns_orig {
459 @ multiplication by 0x0e
460 vext.8 @t[7], @x[7], @x[7], #12
462 veor @x[2], @x[2], @x[5] @ 2 5
463 veor @x[7], @x[7], @x[5] @ 7 5
464 vext.8 @t[0], @x[0], @x[0], #12
466 veor @x[5], @x[5], @x[0] @ 5 0 [1]
467 veor @x[0], @x[0], @x[1] @ 0 1
468 vext.8 @t[1], @x[1], @x[1], #12
469 veor @x[1], @x[1], @x[2] @ 1 25
470 veor @x[0], @x[0], @x[6] @ 01 6 [2]
471 vext.8 @t[3], @x[3], @x[3], #12
472 veor @x[1], @x[1], @x[3] @ 125 3 [4]
473 veor @x[2], @x[2], @x[0] @ 25 016 [3]
474 veor @x[3], @x[3], @x[7] @ 3 75
475 veor @x[7], @x[7], @x[6] @ 75 6 [0]
476 vext.8 @t[6], @x[6], @x[6], #12
478 veor @x[6], @x[6], @x[4] @ 6 4
479 veor @x[4], @x[4], @x[3] @ 4 375 [6]
480 veor @x[3], @x[3], @x[7] @ 375 756=36
481 veor @x[6], @x[6], @t[5] @ 64 5 [7]
482 veor @x[3], @x[3], @t[2] @ 36 2
483 vext.8 @t[5], @t[5], @t[5], #12
484 veor @x[3], @x[3], @t[4] @ 362 4 [5]
486 my @y = @x[7,5,0,2,1,3,4,6];
488 @ multiplication by 0x0b
489 veor @y[1], @y[1], @y[0]
490 veor @y[0], @y[0], @t[0]
491 vext.8 @t[2], @t[2], @t[2], #12
492 veor @y[1], @y[1], @t[1]
493 veor @y[0], @y[0], @t[5]
494 vext.8 @t[4], @t[4], @t[4], #12
495 veor @y[1], @y[1], @t[6]
496 veor @y[0], @y[0], @t[7]
497 veor @t[7], @t[7], @t[6] @ clobber t[7]
499 veor @y[3], @y[3], @t[0]
500 veor @y[1], @y[1], @y[0]
501 vext.8 @t[0], @t[0], @t[0], #12
502 veor @y[2], @y[2], @t[1]
503 veor @y[4], @y[4], @t[1]
504 vext.8 @t[1], @t[1], @t[1], #12
505 veor @y[2], @y[2], @t[2]
506 veor @y[3], @y[3], @t[2]
507 veor @y[5], @y[5], @t[2]
508 veor @y[2], @y[2], @t[7]
509 vext.8 @t[2], @t[2], @t[2], #12
510 veor @y[3], @y[3], @t[3]
511 veor @y[6], @y[6], @t[3]
512 veor @y[4], @y[4], @t[3]
513 veor @y[7], @y[7], @t[4]
514 vext.8 @t[3], @t[3], @t[3], #12
515 veor @y[5], @y[5], @t[4]
516 veor @y[7], @y[7], @t[7]
517 veor @t[7], @t[7], @t[5] @ clobber t[7] even more
518 veor @y[3], @y[3], @t[5]
519 veor @y[4], @y[4], @t[4]
521 veor @y[5], @y[5], @t[7]
522 vext.8 @t[4], @t[4], @t[4], #12
523 veor @y[6], @y[6], @t[7]
524 veor @y[4], @y[4], @t[7]
526 veor @t[7], @t[7], @t[5]
527 vext.8 @t[5], @t[5], @t[5], #12
529 @ multiplication by 0x0d
530 veor @y[4], @y[4], @y[7]
531 veor @t[7], @t[7], @t[6] @ restore t[7]
532 veor @y[7], @y[7], @t[4]
533 vext.8 @t[6], @t[6], @t[6], #12
534 veor @y[2], @y[2], @t[0]
535 veor @y[7], @y[7], @t[5]
536 vext.8 @t[7], @t[7], @t[7], #12
537 veor @y[2], @y[2], @t[2]
539 veor @y[3], @y[3], @y[1]
540 veor @y[1], @y[1], @t[1]
541 veor @y[0], @y[0], @t[0]
542 veor @y[3], @y[3], @t[0]
543 veor @y[1], @y[1], @t[5]
544 veor @y[0], @y[0], @t[5]
545 vext.8 @t[0], @t[0], @t[0], #12
546 veor @y[1], @y[1], @t[7]
547 veor @y[0], @y[0], @t[6]
548 veor @y[3], @y[3], @y[1]
549 veor @y[4], @y[4], @t[1]
550 vext.8 @t[1], @t[1], @t[1], #12
552 veor @y[7], @y[7], @t[7]
553 veor @y[4], @y[4], @t[2]
554 veor @y[5], @y[5], @t[2]
555 veor @y[2], @y[2], @t[6]
556 veor @t[6], @t[6], @t[3] @ clobber t[6]
557 vext.8 @t[2], @t[2], @t[2], #12
558 veor @y[4], @y[4], @y[7]
559 veor @y[3], @y[3], @t[6]
561 veor @y[6], @y[6], @t[6]
562 veor @y[5], @y[5], @t[5]
563 vext.8 @t[5], @t[5], @t[5], #12
564 veor @y[6], @y[6], @t[4]
565 vext.8 @t[4], @t[4], @t[4], #12
566 veor @y[5], @y[5], @t[6]
567 veor @y[6], @y[6], @t[7]
568 vext.8 @t[7], @t[7], @t[7], #12
569 veor @t[6], @t[6], @t[3] @ restore t[6]
570 vext.8 @t[3], @t[3], @t[3], #12
572 @ multiplication by 0x09
573 veor @y[4], @y[4], @y[1]
574 veor @t[1], @t[1], @y[1] @ t[1]=y[1]
575 veor @t[0], @t[0], @t[5] @ clobber t[0]
576 vext.8 @t[6], @t[6], @t[6], #12
577 veor @t[1], @t[1], @t[5]
578 veor @y[3], @y[3], @t[0]
579 veor @t[0], @t[0], @y[0] @ t[0]=y[0]
580 veor @t[1], @t[1], @t[6]
581 veor @t[6], @t[6], @t[7] @ clobber t[6]
582 veor @y[4], @y[4], @t[1]
583 veor @y[7], @y[7], @t[4]
584 veor @y[6], @y[6], @t[3]
585 veor @y[5], @y[5], @t[2]
586 veor @t[4], @t[4], @y[4] @ t[4]=y[4]
587 veor @t[3], @t[3], @y[3] @ t[3]=y[3]
588 veor @t[5], @t[5], @y[5] @ t[5]=y[5]
589 veor @t[2], @t[2], @y[2] @ t[2]=y[2]
590 veor @t[3], @t[3], @t[7]
591 veor @XMM[5], @t[5], @t[6]
592 veor @XMM[6], @t[6], @y[6] @ t[6]=y[6]
593 veor @XMM[2], @t[2], @t[6]
594 veor @XMM[7], @t[7], @y[7] @ t[7]=y[7]
598 @ vmov @XMM[2], @t[2]
601 @ vmov @XMM[5], @t[5]
602 @ vmov @XMM[6], @t[6]
603 @ vmov @XMM[7], @t[7]
611 # Thanks to Jussi Kivilinna for providing pointer to
613 # | 0e 0b 0d 09 | | 02 03 01 01 | | 05 00 04 00 |
614 # | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
615 # | 0d 09 0e 0b | | 01 01 02 03 | | 04 00 05 00 |
616 # | 0b 0d 09 0e | | 03 01 01 02 | | 00 04 00 05 |
619 @ multiplication by 0x05-0x00-0x04-0x00
620 vext.8 @t[0], @x[0], @x[0], #8
621 vext.8 @t[6], @x[6], @x[6], #8
622 vext.8 @t[7], @x[7], @x[7], #8
623 veor @t[0], @t[0], @x[0]
624 vext.8 @t[1], @x[1], @x[1], #8
625 veor @t[6], @t[6], @x[6]
626 vext.8 @t[2], @x[2], @x[2], #8
627 veor @t[7], @t[7], @x[7]
628 vext.8 @t[3], @x[3], @x[3], #8
629 veor @t[1], @t[1], @x[1]
630 vext.8 @t[4], @x[4], @x[4], #8
631 veor @t[2], @t[2], @x[2]
632 vext.8 @t[5], @x[5], @x[5], #8
633 veor @t[3], @t[3], @x[3]
634 veor @t[4], @t[4], @x[4]
635 veor @t[5], @t[5], @x[5]
637 veor @x[0], @x[0], @t[6]
638 veor @x[1], @x[1], @t[6]
639 veor @x[2], @x[2], @t[0]
640 veor @x[4], @x[4], @t[2]
641 veor @x[3], @x[3], @t[1]
642 veor @x[1], @x[1], @t[7]
643 veor @x[2], @x[2], @t[7]
644 veor @x[4], @x[4], @t[6]
645 veor @x[5], @x[5], @t[3]
646 veor @x[3], @x[3], @t[6]
647 veor @x[6], @x[6], @t[4]
648 veor @x[4], @x[4], @t[7]
649 veor @x[5], @x[5], @t[7]
650 veor @x[7], @x[7], @t[5]
652 &MixColumns (@x,@t,1); # flipped 2<->3 and 4<->6
656 my ($a,$b,$n,$mask,$t)=@_;
667 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
669 vshr.u64 $t0, $b0, #$n
670 vshr.u64 $t1, $b1, #$n
676 vshl.u64 $t0, $t0, #$n
678 vshl.u64 $t1, $t1, #$n
685 my @x=reverse(@_[0..7]);
686 my ($t0,$t1,$t2,$t3)=@_[8..11];
688 vmov.i8 $t0,#0x55 @ compose .LBS0
689 vmov.i8 $t1,#0x33 @ compose .LBS1
691 &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
692 &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
694 vmov.i8 $t0,#0x0f @ compose .LBS2
696 &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
697 &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
699 &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
700 &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
705 # include "arm_arch.h"
707 # define VFP_ABI_PUSH vstmdb sp!,{d8-d15}
708 # define VFP_ABI_POP vldmia sp!,{d8-d15}
709 # define VFP_ABI_FRAME 0x40
711 # define VFP_ABI_PUSH
713 # define VFP_ABI_FRAME 0
714 # define BSAES_ASM_EXTENDED_KEY
715 # define XTS_CHAIN_TWEAK
716 # define __ARM_ARCH__ __LINUX_ARM_ARCH__
717 # define __ARM_MAX_ARCH__ 7
724 #if __ARM_MAX_ARCH__>=7
729 .syntax unified @ ARMv7-capable assembler is expected to handle this
730 #if defined(__thumb2__) && !defined(__APPLE__)
736 .type _bsaes_decrypt8,%function
739 adr $const,_bsaes_decrypt8
740 vldmia $key!, {@XMM[9]} @ round 0 key
744 add $const,$const,#.LM0ISR-_bsaes_decrypt8
747 vldmia $const!, {@XMM[8]} @ .LM0ISR
748 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
749 veor @XMM[11], @XMM[1], @XMM[9]
750 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
751 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
752 veor @XMM[12], @XMM[2], @XMM[9]
753 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
754 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
755 veor @XMM[13], @XMM[3], @XMM[9]
756 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
757 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
758 veor @XMM[14], @XMM[4], @XMM[9]
759 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
760 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
761 veor @XMM[15], @XMM[5], @XMM[9]
762 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
763 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
764 veor @XMM[10], @XMM[6], @XMM[9]
765 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
766 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
767 veor @XMM[11], @XMM[7], @XMM[9]
768 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
769 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
770 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
771 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
773 &bitslice (@XMM[0..7, 8..11]);
775 sub $rounds,$rounds,#1
780 &ShiftRows (@XMM[0..7, 8..12]);
781 $code.=".Ldec_sbox:\n";
782 &InvSbox (@XMM[0..7, 8..15]);
784 subs $rounds,$rounds,#1
787 &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
789 vldmia $const, {@XMM[12]} @ .LISR
790 ite eq @ Thumb2 thing, sanity check in ARM
791 addeq $const,$const,#0x10
793 vldmia $const, {@XMM[12]} @ .LISRM0
798 &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
800 vldmia $key, {@XMM[8]} @ last round key
801 veor @XMM[6], @XMM[6], @XMM[8]
802 veor @XMM[4], @XMM[4], @XMM[8]
803 veor @XMM[2], @XMM[2], @XMM[8]
804 veor @XMM[7], @XMM[7], @XMM[8]
805 veor @XMM[3], @XMM[3], @XMM[8]
806 veor @XMM[5], @XMM[5], @XMM[8]
807 veor @XMM[0], @XMM[0], @XMM[8]
808 veor @XMM[1], @XMM[1], @XMM[8]
810 .size _bsaes_decrypt8,.-_bsaes_decrypt8
812 .type _bsaes_const,%object
815 .LM0ISR: @ InvShiftRows constants
816 .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
818 .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
820 .quad 0x01040b0e0205080f, 0x0306090c00070a0d
821 .LM0SR: @ ShiftRows constants
822 .quad 0x0a0e02060f03070b, 0x0004080c05090d01
824 .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
826 .quad 0x0304090e00050a0f, 0x01060b0c0207080d
828 .quad 0x02060a0e03070b0f, 0x0004080c0105090d
830 .quad 0x090d01050c000408, 0x03070b0f060a0e02
831 .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
833 .size _bsaes_const,.-_bsaes_const
835 .type _bsaes_encrypt8,%function
838 adr $const,_bsaes_encrypt8
839 vldmia $key!, {@XMM[9]} @ round 0 key
843 sub $const,$const,#_bsaes_encrypt8-.LM0SR
846 vldmia $const!, {@XMM[8]} @ .LM0SR
848 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
849 veor @XMM[11], @XMM[1], @XMM[9]
850 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
851 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
852 veor @XMM[12], @XMM[2], @XMM[9]
853 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
854 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
855 veor @XMM[13], @XMM[3], @XMM[9]
856 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
857 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
858 veor @XMM[14], @XMM[4], @XMM[9]
859 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
860 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
861 veor @XMM[15], @XMM[5], @XMM[9]
862 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
863 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
864 veor @XMM[10], @XMM[6], @XMM[9]
865 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
866 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
867 veor @XMM[11], @XMM[7], @XMM[9]
868 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
869 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
870 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
871 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
872 _bsaes_encrypt8_bitslice:
874 &bitslice (@XMM[0..7, 8..11]);
876 sub $rounds,$rounds,#1
881 &ShiftRows (@XMM[0..7, 8..12]);
882 $code.=".Lenc_sbox:\n";
883 &Sbox (@XMM[0..7, 8..15]);
885 subs $rounds,$rounds,#1
888 &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
890 vldmia $const, {@XMM[12]} @ .LSR
891 ite eq @ Thumb2 thing, samity check in ARM
892 addeq $const,$const,#0x10
894 vldmia $const, {@XMM[12]} @ .LSRM0
899 # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
900 &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
902 vldmia $key, {@XMM[8]} @ last round key
903 veor @XMM[4], @XMM[4], @XMM[8]
904 veor @XMM[6], @XMM[6], @XMM[8]
905 veor @XMM[3], @XMM[3], @XMM[8]
906 veor @XMM[7], @XMM[7], @XMM[8]
907 veor @XMM[2], @XMM[2], @XMM[8]
908 veor @XMM[5], @XMM[5], @XMM[8]
909 veor @XMM[0], @XMM[0], @XMM[8]
910 veor @XMM[1], @XMM[1], @XMM[8]
912 .size _bsaes_encrypt8,.-_bsaes_encrypt8
916 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
919 my @x=reverse(@_[0..7]);
920 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
922 &swapmove (@x[0,1],1,$bs0,$t2,$t3);
924 @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
928 #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
930 &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
932 @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
938 &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
939 &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
943 .type _bsaes_key_convert,%function
946 adr $const,_bsaes_key_convert
947 vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
951 sub $const,$const,#_bsaes_key_convert-.LM0
953 vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
955 vmov.i8 @XMM[8], #0x01 @ bit masks
956 vmov.i8 @XMM[9], #0x02
957 vmov.i8 @XMM[10], #0x04
958 vmov.i8 @XMM[11], #0x08
959 vmov.i8 @XMM[12], #0x10
960 vmov.i8 @XMM[13], #0x20
961 vldmia $const, {@XMM[14]} @ .LM0
964 vrev32.8 @XMM[7], @XMM[7]
965 vrev32.8 @XMM[15], @XMM[15]
967 sub $rounds,$rounds,#1
968 vstmia $out!, {@XMM[7]} @ save round 0 key
973 vtbl.8 `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
974 vtbl.8 `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
975 vmov.i8 @XMM[6], #0x40
976 vmov.i8 @XMM[15], #0x80
978 vtst.8 @XMM[0], @XMM[7], @XMM[8]
979 vtst.8 @XMM[1], @XMM[7], @XMM[9]
980 vtst.8 @XMM[2], @XMM[7], @XMM[10]
981 vtst.8 @XMM[3], @XMM[7], @XMM[11]
982 vtst.8 @XMM[4], @XMM[7], @XMM[12]
983 vtst.8 @XMM[5], @XMM[7], @XMM[13]
984 vtst.8 @XMM[6], @XMM[7], @XMM[6]
985 vtst.8 @XMM[7], @XMM[7], @XMM[15]
986 vld1.8 {@XMM[15]}, [$inp]! @ load next round key
987 vmvn @XMM[0], @XMM[0] @ "pnot"
988 vmvn @XMM[1], @XMM[1]
989 vmvn @XMM[5], @XMM[5]
990 vmvn @XMM[6], @XMM[6]
992 vrev32.8 @XMM[15], @XMM[15]
994 subs $rounds,$rounds,#1
995 vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
998 vmov.i8 @XMM[7],#0x63 @ compose .L63
999 @ don't save last round key
1001 .size _bsaes_key_convert,.-_bsaes_key_convert
1005 if (0) { # following four functions are unsupported interface
1006 # used for benchmarking...
1008 .globl bsaes_enc_key_convert
1009 .type bsaes_enc_key_convert,%function
1011 bsaes_enc_key_convert:
1012 stmdb sp!,{r4-r6,lr}
1013 vstmdb sp!,{d8-d15} @ ABI specification says so
1015 ldr r5,[$inp,#240] @ pass rounds
1016 mov r4,$inp @ pass key
1017 mov r12,$out @ pass key schedule
1018 bl _bsaes_key_convert
1019 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1020 vstmia r12, {@XMM[7]} @ save last round key
1023 ldmia sp!,{r4-r6,pc}
1024 .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
1026 .globl bsaes_encrypt_128
1027 .type bsaes_encrypt_128,%function
1030 stmdb sp!,{r4-r6,lr}
1031 vstmdb sp!,{d8-d15} @ ABI specification says so
1033 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1034 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1035 mov r4,$key @ pass the key
1036 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1037 mov r5,#10 @ pass rounds
1038 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1042 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1043 vst1.8 {@XMM[4]}, [$out]!
1044 vst1.8 {@XMM[6]}, [$out]!
1045 vst1.8 {@XMM[3]}, [$out]!
1046 vst1.8 {@XMM[7]}, [$out]!
1047 vst1.8 {@XMM[2]}, [$out]!
1048 subs $len,$len,#0x80
1049 vst1.8 {@XMM[5]}, [$out]!
1053 ldmia sp!,{r4-r6,pc}
1054 .size bsaes_encrypt_128,.-bsaes_encrypt_128
1056 .globl bsaes_dec_key_convert
1057 .type bsaes_dec_key_convert,%function
1059 bsaes_dec_key_convert:
1060 stmdb sp!,{r4-r6,lr}
1061 vstmdb sp!,{d8-d15} @ ABI specification says so
1063 ldr r5,[$inp,#240] @ pass rounds
1064 mov r4,$inp @ pass key
1065 mov r12,$out @ pass key schedule
1066 bl _bsaes_key_convert
1067 vldmia $out, {@XMM[6]}
1068 vstmia r12, {@XMM[15]} @ save last round key
1069 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1070 vstmia $out, {@XMM[7]}
1073 ldmia sp!,{r4-r6,pc}
1074 .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
1076 .globl bsaes_decrypt_128
1077 .type bsaes_decrypt_128,%function
1080 stmdb sp!,{r4-r6,lr}
1081 vstmdb sp!,{d8-d15} @ ABI specification says so
1083 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1084 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1085 mov r4,$key @ pass the key
1086 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1087 mov r5,#10 @ pass rounds
1088 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1092 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1093 vst1.8 {@XMM[6]}, [$out]!
1094 vst1.8 {@XMM[4]}, [$out]!
1095 vst1.8 {@XMM[2]}, [$out]!
1096 vst1.8 {@XMM[7]}, [$out]!
1097 vst1.8 {@XMM[3]}, [$out]!
1098 subs $len,$len,#0x80
1099 vst1.8 {@XMM[5]}, [$out]!
1103 ldmia sp!,{r4-r6,pc}
1104 .size bsaes_decrypt_128,.-bsaes_decrypt_128
1108 my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
1109 my ($keysched)=("sp");
1112 .extern AES_cbc_encrypt
1115 .global bsaes_cbc_encrypt
1116 .type bsaes_cbc_encrypt,%function
1130 @ it is up to the caller to make sure we are called with enc == 0
1133 stmdb sp!, {r4-r10, lr}
1135 ldr $ivp, [ip] @ IV is 1st arg on the stack
1136 mov $len, $len, lsr#4 @ len in 16 byte blocks
1137 sub sp, #0x10 @ scratch space to carry over the IV
1138 mov $fp, sp @ save sp
1140 ldr $rounds, [$key, #240] @ get # of rounds
1141 #ifndef BSAES_ASM_EXTENDED_KEY
1142 @ allocate the key schedule on the stack
1143 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1144 add r12, #`128-32` @ sifze of bit-slices key schedule
1146 @ populate the key schedule
1147 mov r4, $key @ pass key
1148 mov r5, $rounds @ pass # of rounds
1149 mov sp, r12 @ sp is $keysched
1150 bl _bsaes_key_convert
1151 vldmia $keysched, {@XMM[6]}
1152 vstmia r12, {@XMM[15]} @ save last round key
1153 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1154 vstmia $keysched, {@XMM[7]}
1156 ldr r12, [$key, #244]
1160 @ populate the key schedule
1161 str r12, [$key, #244]
1162 mov r4, $key @ pass key
1163 mov r5, $rounds @ pass # of rounds
1164 add r12, $key, #248 @ pass key schedule
1165 bl _bsaes_key_convert
1167 vldmia r4, {@XMM[6]}
1168 vstmia r12, {@XMM[15]} @ save last round key
1169 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1170 vstmia r4, {@XMM[7]}
1176 vld1.8 {@XMM[15]}, [$ivp] @ load IV
1181 subs $len, $len, #0x8
1182 bmi .Lcbc_dec_loop_finish
1184 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1185 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1186 #ifndef BSAES_ASM_EXTENDED_KEY
1187 mov r4, $keysched @ pass the key
1191 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1193 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]
1194 sub $inp, $inp, #0x60
1195 vstmia $fp, {@XMM[15]} @ put aside IV
1199 vldmia $fp, {@XMM[14]} @ reload IV
1200 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1201 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1202 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1203 veor @XMM[1], @XMM[1], @XMM[8]
1204 veor @XMM[6], @XMM[6], @XMM[9]
1205 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1206 veor @XMM[4], @XMM[4], @XMM[10]
1207 veor @XMM[2], @XMM[2], @XMM[11]
1208 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1209 veor @XMM[7], @XMM[7], @XMM[12]
1210 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1211 veor @XMM[3], @XMM[3], @XMM[13]
1212 vst1.8 {@XMM[6]}, [$out]!
1213 veor @XMM[5], @XMM[5], @XMM[14]
1214 vst1.8 {@XMM[4]}, [$out]!
1215 vst1.8 {@XMM[2]}, [$out]!
1216 vst1.8 {@XMM[7]}, [$out]!
1217 vst1.8 {@XMM[3]}, [$out]!
1218 vst1.8 {@XMM[5]}, [$out]!
1222 .Lcbc_dec_loop_finish:
1226 vld1.8 {@XMM[0]}, [$inp]! @ load input
1229 vld1.8 {@XMM[1]}, [$inp]!
1230 #ifndef BSAES_ASM_EXTENDED_KEY
1231 mov r4, $keysched @ pass the key
1236 vstmia $fp, {@XMM[15]} @ put aside IV
1238 vld1.8 {@XMM[2]}, [$inp]!
1241 vld1.8 {@XMM[3]}, [$inp]!
1243 vld1.8 {@XMM[4]}, [$inp]!
1246 vld1.8 {@XMM[5]}, [$inp]!
1248 vld1.8 {@XMM[6]}, [$inp]!
1249 sub $inp, $inp, #0x70
1253 vldmia $fp, {@XMM[14]} @ reload IV
1254 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1255 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1256 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1257 veor @XMM[1], @XMM[1], @XMM[8]
1258 veor @XMM[6], @XMM[6], @XMM[9]
1259 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1260 veor @XMM[4], @XMM[4], @XMM[10]
1261 veor @XMM[2], @XMM[2], @XMM[11]
1262 vld1.8 {@XMM[15]}, [$inp]!
1263 veor @XMM[7], @XMM[7], @XMM[12]
1264 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1265 veor @XMM[3], @XMM[3], @XMM[13]
1266 vst1.8 {@XMM[6]}, [$out]!
1267 vst1.8 {@XMM[4]}, [$out]!
1268 vst1.8 {@XMM[2]}, [$out]!
1269 vst1.8 {@XMM[7]}, [$out]!
1270 vst1.8 {@XMM[3]}, [$out]!
1274 sub $inp, $inp, #0x60
1276 vldmia $fp,{@XMM[14]} @ reload IV
1277 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1278 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1279 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1280 veor @XMM[1], @XMM[1], @XMM[8]
1281 veor @XMM[6], @XMM[6], @XMM[9]
1282 vld1.8 {@XMM[12]}, [$inp]!
1283 veor @XMM[4], @XMM[4], @XMM[10]
1284 veor @XMM[2], @XMM[2], @XMM[11]
1285 vld1.8 {@XMM[15]}, [$inp]!
1286 veor @XMM[7], @XMM[7], @XMM[12]
1287 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1288 vst1.8 {@XMM[6]}, [$out]!
1289 vst1.8 {@XMM[4]}, [$out]!
1290 vst1.8 {@XMM[2]}, [$out]!
1291 vst1.8 {@XMM[7]}, [$out]!
1295 sub $inp, $inp, #0x50
1297 vldmia $fp, {@XMM[14]} @ reload IV
1298 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1299 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1300 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1301 veor @XMM[1], @XMM[1], @XMM[8]
1302 veor @XMM[6], @XMM[6], @XMM[9]
1303 vld1.8 {@XMM[15]}, [$inp]!
1304 veor @XMM[4], @XMM[4], @XMM[10]
1305 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1306 veor @XMM[2], @XMM[2], @XMM[11]
1307 vst1.8 {@XMM[6]}, [$out]!
1308 vst1.8 {@XMM[4]}, [$out]!
1309 vst1.8 {@XMM[2]}, [$out]!
1313 sub $inp, $inp, #0x40
1315 vldmia $fp, {@XMM[14]} @ reload IV
1316 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1317 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1318 vld1.8 {@XMM[10]}, [$inp]!
1319 veor @XMM[1], @XMM[1], @XMM[8]
1320 veor @XMM[6], @XMM[6], @XMM[9]
1321 vld1.8 {@XMM[15]}, [$inp]!
1322 veor @XMM[4], @XMM[4], @XMM[10]
1323 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1324 vst1.8 {@XMM[6]}, [$out]!
1325 vst1.8 {@XMM[4]}, [$out]!
1329 sub $inp, $inp, #0x30
1331 vldmia $fp, {@XMM[14]} @ reload IV
1332 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1333 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1334 vld1.8 {@XMM[15]}, [$inp]!
1335 veor @XMM[1], @XMM[1], @XMM[8]
1336 veor @XMM[6], @XMM[6], @XMM[9]
1337 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1338 vst1.8 {@XMM[6]}, [$out]!
1342 sub $inp, $inp, #0x20
1344 vldmia $fp, {@XMM[14]} @ reload IV
1345 vld1.8 {@XMM[8]}, [$inp]! @ reload input
1346 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1347 vld1.8 {@XMM[15]}, [$inp]! @ reload input
1348 veor @XMM[1], @XMM[1], @XMM[8]
1349 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1353 sub $inp, $inp, #0x10
1354 mov $rounds, $out @ save original out pointer
1355 mov $out, $fp @ use the iv scratch space as out buffer
1357 vmov @XMM[4],@XMM[15] @ just in case ensure that IV
1358 vmov @XMM[5],@XMM[0] @ and input are preserved
1360 vld1.8 {@XMM[0]}, [$fp,:64] @ load result
1361 veor @XMM[0], @XMM[0], @XMM[4] @ ^= IV
1362 vmov @XMM[15], @XMM[5] @ @XMM[5] holds input
1363 vst1.8 {@XMM[0]}, [$rounds] @ write output
1366 #ifndef BSAES_ASM_EXTENDED_KEY
1369 .Lcbc_dec_bzero: @ wipe key schedule [if any]
1370 vstmia $keysched!, {q0-q1}
1376 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1377 vst1.8 {@XMM[15]}, [$ivp] @ return IV
1379 ldmia sp!, {r4-r10, pc}
1380 .size bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
1384 my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1385 my $const = "r6"; # shared with _bsaes_encrypt8_alt
1386 my $keysched = "sp";
1390 .global bsaes_ctr32_encrypt_blocks
1391 .type bsaes_ctr32_encrypt_blocks,%function
1393 bsaes_ctr32_encrypt_blocks:
1394 cmp $len, #8 @ use plain AES for
1395 blo .Lctr_enc_short @ small sizes
1398 stmdb sp!, {r4-r10, lr}
1400 ldr $ctr, [ip] @ ctr is 1st arg on the stack
1401 sub sp, sp, #0x10 @ scratch space to carry over the ctr
1402 mov $fp, sp @ save sp
1404 ldr $rounds, [$key, #240] @ get # of rounds
1405 #ifndef BSAES_ASM_EXTENDED_KEY
1406 @ allocate the key schedule on the stack
1407 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1408 add r12, #`128-32` @ size of bit-sliced key schedule
1410 @ populate the key schedule
1411 mov r4, $key @ pass key
1412 mov r5, $rounds @ pass # of rounds
1413 mov sp, r12 @ sp is $keysched
1414 bl _bsaes_key_convert
1415 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1416 vstmia r12, {@XMM[7]} @ save last round key
1418 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1420 mov $ctr, #:lower16:(.LREVM0SR-.LM0)
1421 add $ctr, $const, $ctr
1423 add $ctr, $const, #.LREVM0SR-.LM0 @ borrow $ctr
1425 vldmia $keysched, {@XMM[4]} @ load round0 key
1427 ldr r12, [$key, #244]
1431 @ populate the key schedule
1432 str r12, [$key, #244]
1433 mov r4, $key @ pass key
1434 mov r5, $rounds @ pass # of rounds
1435 add r12, $key, #248 @ pass key schedule
1436 bl _bsaes_key_convert
1437 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1438 vstmia r12, {@XMM[7]} @ save last round key
1441 0: add r12, $key, #248
1442 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1443 adrl $ctr, .LREVM0SR @ borrow $ctr
1444 vldmia r12, {@XMM[4]} @ load round0 key
1445 sub sp, #0x10 @ place for adjusted round0 key
1448 vmov.i32 @XMM[8],#1 @ compose 1<<96
1449 veor @XMM[9],@XMM[9],@XMM[9]
1450 vrev32.8 @XMM[0],@XMM[0]
1451 vext.8 @XMM[8],@XMM[9],@XMM[8],#4
1452 vrev32.8 @XMM[4],@XMM[4]
1453 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1454 vstmia $keysched, {@XMM[4]} @ save adjusted round0 key
1459 vadd.u32 @XMM[10], @XMM[8], @XMM[9] @ compose 3<<96
1460 vadd.u32 @XMM[1], @XMM[0], @XMM[8] @ +1
1461 vadd.u32 @XMM[2], @XMM[0], @XMM[9] @ +2
1462 vadd.u32 @XMM[3], @XMM[0], @XMM[10] @ +3
1463 vadd.u32 @XMM[4], @XMM[1], @XMM[10]
1464 vadd.u32 @XMM[5], @XMM[2], @XMM[10]
1465 vadd.u32 @XMM[6], @XMM[3], @XMM[10]
1466 vadd.u32 @XMM[7], @XMM[4], @XMM[10]
1467 vadd.u32 @XMM[10], @XMM[5], @XMM[10] @ next counter
1469 @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1470 @ to flip byte order in 32-bit counter
1472 vldmia $keysched, {@XMM[9]} @ load round0 key
1473 #ifndef BSAES_ASM_EXTENDED_KEY
1474 add r4, $keysched, #0x10 @ pass next round key
1476 add r4, $key, #`248+16`
1478 vldmia $ctr, {@XMM[8]} @ .LREVM0SR
1479 mov r5, $rounds @ pass rounds
1480 vstmia $fp, {@XMM[10]} @ save next counter
1482 mov $const, #:lower16:(.LREVM0SR-.LSR)
1483 sub $const, $ctr, $const
1485 sub $const, $ctr, #.LREVM0SR-.LSR @ pass constants
1488 bl _bsaes_encrypt8_alt
1491 blo .Lctr_enc_loop_done
1493 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ load input
1494 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1495 veor @XMM[0], @XMM[8]
1496 veor @XMM[1], @XMM[9]
1497 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1498 veor @XMM[4], @XMM[10]
1499 veor @XMM[6], @XMM[11]
1500 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1501 veor @XMM[3], @XMM[12]
1502 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1503 veor @XMM[7], @XMM[13]
1504 veor @XMM[2], @XMM[14]
1505 vst1.8 {@XMM[4]}, [$out]!
1506 veor @XMM[5], @XMM[15]
1507 vst1.8 {@XMM[6]}, [$out]!
1508 vmov.i32 @XMM[8], #1 @ compose 1<<96
1509 vst1.8 {@XMM[3]}, [$out]!
1510 veor @XMM[9], @XMM[9], @XMM[9]
1511 vst1.8 {@XMM[7]}, [$out]!
1512 vext.8 @XMM[8], @XMM[9], @XMM[8], #4
1513 vst1.8 {@XMM[2]}, [$out]!
1514 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1515 vst1.8 {@XMM[5]}, [$out]!
1516 vldmia $fp, {@XMM[0]} @ load counter
1522 .Lctr_enc_loop_done:
1524 vld1.8 {@XMM[8]}, [$inp]! @ load input
1525 veor @XMM[0], @XMM[8]
1526 vst1.8 {@XMM[0]}, [$out]! @ write output
1529 vld1.8 {@XMM[9]}, [$inp]!
1530 veor @XMM[1], @XMM[9]
1531 vst1.8 {@XMM[1]}, [$out]!
1533 vld1.8 {@XMM[10]}, [$inp]!
1534 veor @XMM[4], @XMM[10]
1535 vst1.8 {@XMM[4]}, [$out]!
1538 vld1.8 {@XMM[11]}, [$inp]!
1539 veor @XMM[6], @XMM[11]
1540 vst1.8 {@XMM[6]}, [$out]!
1542 vld1.8 {@XMM[12]}, [$inp]!
1543 veor @XMM[3], @XMM[12]
1544 vst1.8 {@XMM[3]}, [$out]!
1547 vld1.8 {@XMM[13]}, [$inp]!
1548 veor @XMM[7], @XMM[13]
1549 vst1.8 {@XMM[7]}, [$out]!
1551 vld1.8 {@XMM[14]}, [$inp]
1552 veor @XMM[2], @XMM[14]
1553 vst1.8 {@XMM[2]}, [$out]!
1558 #ifndef BSAES_ASM_EXTENDED_KEY
1559 .Lctr_enc_bzero: @ wipe key schedule [if any]
1560 vstmia $keysched!, {q0-q1}
1564 vstmia $keysched, {q0-q1}
1568 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1570 ldmia sp!, {r4-r10, pc} @ return
1574 ldr ip, [sp] @ ctr pointer is passed on stack
1575 stmdb sp!, {r4-r8, lr}
1577 mov r4, $inp @ copy arguments
1581 ldr r8, [ip, #12] @ load counter LSW
1582 vld1.8 {@XMM[1]}, [ip] @ load whole counter value
1587 vst1.8 {@XMM[1]}, [sp] @ copy counter value
1590 .Lctr_enc_short_loop:
1591 add r0, sp, #0x10 @ input counter value
1592 mov r1, sp @ output on the stack
1597 vld1.8 {@XMM[0]}, [r4]! @ load input
1598 vld1.8 {@XMM[1]}, [sp] @ load encrypted counter
1602 str r0, [sp, #0x1c] @ next counter value
1604 str r8, [sp, #0x1c] @ next counter value
1606 veor @XMM[0],@XMM[0],@XMM[1]
1607 vst1.8 {@XMM[0]}, [r5]! @ store output
1609 bne .Lctr_enc_short_loop
1615 ldmia sp!, {r4-r8, pc}
1616 .size bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
1620 ######################################################################
1621 # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1622 # const AES_KEY *key1, const AES_KEY *key2,
1623 # const unsigned char iv[16]);
1625 my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
1626 my $const="r6"; # returned by _bsaes_key_convert
1631 .globl bsaes_xts_encrypt
1632 .type bsaes_xts_encrypt,%function
1636 stmdb sp!, {r4-r10, lr} @ 0x20
1638 mov r6, sp @ future $fp
1645 sub r0, sp, #0x10 @ 0x10
1646 bic r0, #0xf @ align at 16 bytes
1649 #ifdef XTS_CHAIN_TWEAK
1650 ldr r0, [ip] @ pointer to input tweak
1652 @ generate initial tweak
1653 ldr r0, [ip, #4] @ iv[]
1655 ldr r2, [ip, #0] @ key2
1657 mov r0,sp @ pointer to initial tweak
1660 ldr $rounds, [$key, #240] @ get # of rounds
1662 #ifndef BSAES_ASM_EXTENDED_KEY
1663 @ allocate the key schedule on the stack
1664 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1665 @ add r12, #`128-32` @ size of bit-sliced key schedule
1666 sub r12, #`32+16` @ place for tweak[9]
1668 @ populate the key schedule
1669 mov r4, $key @ pass key
1670 mov r5, $rounds @ pass # of rounds
1672 add r12, #0x90 @ pass key schedule
1673 bl _bsaes_key_convert
1674 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1675 vstmia r12, {@XMM[7]} @ save last round key
1677 ldr r12, [$key, #244]
1681 str r12, [$key, #244]
1682 mov r4, $key @ pass key
1683 mov r5, $rounds @ pass # of rounds
1684 add r12, $key, #248 @ pass key schedule
1685 bl _bsaes_key_convert
1686 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1687 vstmia r12, {@XMM[7]}
1690 0: sub sp, #0x90 @ place for tweak[9]
1693 vld1.8 {@XMM[8]}, [r0] @ initial tweak
1694 adr $magic, .Lxts_magic
1702 vldmia $magic, {$twmask} @ load XTS magic
1703 vshr.s64 @T[0], @XMM[8], #63
1705 vand @T[0], @T[0], $twmask
1707 for($i=9;$i<16;$i++) {
1709 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1710 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1711 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1712 vshr.s64 @T[1], @XMM[$i], #63
1713 veor @XMM[$i], @XMM[$i], @T[0]
1714 vand @T[1], @T[1], $twmask
1718 $code.=<<___ if ($i>=10);
1719 vld1.8 {@XMM[$i-10]}, [$inp]!
1721 $code.=<<___ if ($i>=11);
1722 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1726 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
1727 vst1.64 {@XMM[15]}, [r0,:128]!
1728 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1729 veor @XMM[8], @XMM[8], @T[0]
1730 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1732 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1733 veor @XMM[5], @XMM[5], @XMM[13]
1734 #ifndef BSAES_ASM_EXTENDED_KEY
1735 add r4, sp, #0x90 @ pass key schedule
1737 add r4, $key, #248 @ pass key schedule
1739 veor @XMM[6], @XMM[6], @XMM[14]
1740 mov r5, $rounds @ pass rounds
1741 veor @XMM[7], @XMM[7], @XMM[15]
1746 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1747 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1748 veor @XMM[0], @XMM[0], @XMM[ 8]
1749 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1750 veor @XMM[1], @XMM[1], @XMM[ 9]
1751 veor @XMM[8], @XMM[4], @XMM[10]
1752 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1753 veor @XMM[9], @XMM[6], @XMM[11]
1754 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
1755 veor @XMM[10], @XMM[3], @XMM[12]
1756 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1757 veor @XMM[11], @XMM[7], @XMM[13]
1758 veor @XMM[12], @XMM[2], @XMM[14]
1759 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1760 veor @XMM[13], @XMM[5], @XMM[15]
1761 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
1763 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1772 vldmia $magic, {$twmask} @ load XTS magic
1773 vshr.s64 @T[0], @XMM[8], #63
1775 vand @T[0], @T[0], $twmask
1777 for($i=9;$i<16;$i++) {
1779 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1780 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1781 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1782 vshr.s64 @T[1], @XMM[$i], #63
1783 veor @XMM[$i], @XMM[$i], @T[0]
1784 vand @T[1], @T[1], $twmask
1788 $code.=<<___ if ($i>=10);
1789 vld1.8 {@XMM[$i-10]}, [$inp]!
1791 bmi .Lxts_enc_`$i-9`
1793 $code.=<<___ if ($i>=11);
1794 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1799 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
1801 vld1.8 {@XMM[6]}, [$inp]!
1802 veor @XMM[5], @XMM[5], @XMM[13]
1803 #ifndef BSAES_ASM_EXTENDED_KEY
1804 add r4, sp, #0x90 @ pass key schedule
1806 add r4, $key, #248 @ pass key schedule
1808 veor @XMM[6], @XMM[6], @XMM[14]
1809 mov r5, $rounds @ pass rounds
1814 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1815 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1816 veor @XMM[0], @XMM[0], @XMM[ 8]
1817 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1818 veor @XMM[1], @XMM[1], @XMM[ 9]
1819 veor @XMM[8], @XMM[4], @XMM[10]
1820 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1821 veor @XMM[9], @XMM[6], @XMM[11]
1822 vld1.64 {@XMM[14]}, [r0,:128]!
1823 veor @XMM[10], @XMM[3], @XMM[12]
1824 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1825 veor @XMM[11], @XMM[7], @XMM[13]
1826 veor @XMM[12], @XMM[2], @XMM[14]
1827 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1828 vst1.8 {@XMM[12]}, [$out]!
1830 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1834 vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
1836 veor @XMM[4], @XMM[4], @XMM[12]
1837 #ifndef BSAES_ASM_EXTENDED_KEY
1838 add r4, sp, #0x90 @ pass key schedule
1840 add r4, $key, #248 @ pass key schedule
1842 veor @XMM[5], @XMM[5], @XMM[13]
1843 mov r5, $rounds @ pass rounds
1848 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1849 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1850 veor @XMM[0], @XMM[0], @XMM[ 8]
1851 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1852 veor @XMM[1], @XMM[1], @XMM[ 9]
1853 veor @XMM[8], @XMM[4], @XMM[10]
1854 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1855 veor @XMM[9], @XMM[6], @XMM[11]
1856 veor @XMM[10], @XMM[3], @XMM[12]
1857 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1858 veor @XMM[11], @XMM[7], @XMM[13]
1859 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1861 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1864 @ put this in range for both ARM and Thumb mode adr instructions
1871 vst1.64 {@XMM[13]}, [r0,:128] @ next round tweak
1873 veor @XMM[3], @XMM[3], @XMM[11]
1874 #ifndef BSAES_ASM_EXTENDED_KEY
1875 add r4, sp, #0x90 @ pass key schedule
1877 add r4, $key, #248 @ pass key schedule
1879 veor @XMM[4], @XMM[4], @XMM[12]
1880 mov r5, $rounds @ pass rounds
1885 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1886 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1887 veor @XMM[0], @XMM[0], @XMM[ 8]
1888 vld1.64 {@XMM[12]}, [r0,:128]!
1889 veor @XMM[1], @XMM[1], @XMM[ 9]
1890 veor @XMM[8], @XMM[4], @XMM[10]
1891 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1892 veor @XMM[9], @XMM[6], @XMM[11]
1893 veor @XMM[10], @XMM[3], @XMM[12]
1894 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1895 vst1.8 {@XMM[10]}, [$out]!
1897 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1901 vst1.64 {@XMM[12]}, [r0,:128] @ next round tweak
1903 veor @XMM[2], @XMM[2], @XMM[10]
1904 #ifndef BSAES_ASM_EXTENDED_KEY
1905 add r4, sp, #0x90 @ pass key schedule
1907 add r4, $key, #248 @ pass key schedule
1909 veor @XMM[3], @XMM[3], @XMM[11]
1910 mov r5, $rounds @ pass rounds
1915 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1916 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1917 veor @XMM[0], @XMM[0], @XMM[ 8]
1918 veor @XMM[1], @XMM[1], @XMM[ 9]
1919 veor @XMM[8], @XMM[4], @XMM[10]
1920 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1921 veor @XMM[9], @XMM[6], @XMM[11]
1922 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1924 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1928 vst1.64 {@XMM[11]}, [r0,:128] @ next round tweak
1930 veor @XMM[1], @XMM[1], @XMM[9]
1931 #ifndef BSAES_ASM_EXTENDED_KEY
1932 add r4, sp, #0x90 @ pass key schedule
1934 add r4, $key, #248 @ pass key schedule
1936 veor @XMM[2], @XMM[2], @XMM[10]
1937 mov r5, $rounds @ pass rounds
1942 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1943 vld1.64 {@XMM[10]}, [r0,:128]!
1944 veor @XMM[0], @XMM[0], @XMM[ 8]
1945 veor @XMM[1], @XMM[1], @XMM[ 9]
1946 veor @XMM[8], @XMM[4], @XMM[10]
1947 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1948 vst1.8 {@XMM[8]}, [$out]!
1950 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1954 vst1.64 {@XMM[10]}, [r0,:128] @ next round tweak
1956 veor @XMM[0], @XMM[0], @XMM[8]
1957 #ifndef BSAES_ASM_EXTENDED_KEY
1958 add r4, sp, #0x90 @ pass key schedule
1960 add r4, $key, #248 @ pass key schedule
1962 veor @XMM[1], @XMM[1], @XMM[9]
1963 mov r5, $rounds @ pass rounds
1968 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1969 veor @XMM[0], @XMM[0], @XMM[ 8]
1970 veor @XMM[1], @XMM[1], @XMM[ 9]
1971 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1973 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1978 veor @XMM[0], @XMM[8]
1980 vst1.8 {@XMM[0]}, [sp,:128]
1982 mov r4, $fp @ preserve fp
1986 vld1.8 {@XMM[0]}, [sp,:128]
1987 veor @XMM[0], @XMM[0], @XMM[8]
1988 vst1.8 {@XMM[0]}, [$out]!
1991 vmov @XMM[8], @XMM[9] @ next round tweak
1994 #ifndef XTS_CHAIN_TWEAK
2001 ldrb r1, [$out, #-0x10]
2002 strb r0, [$out, #-0x10]
2008 vld1.8 {@XMM[0]}, [r6]
2010 veor @XMM[0], @XMM[0], @XMM[8]
2012 vst1.8 {@XMM[0]}, [sp,:128]
2014 mov r4, $fp @ preserve fp
2018 vld1.8 {@XMM[0]}, [sp,:128]
2019 veor @XMM[0], @XMM[0], @XMM[8]
2020 vst1.8 {@XMM[0]}, [r6]
2028 #ifdef XTS_CHAIN_TWEAK
2029 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2031 .Lxts_enc_bzero: @ wipe key schedule [if any]
2037 #ifdef XTS_CHAIN_TWEAK
2038 vst1.8 {@XMM[8]}, [r1]
2041 ldmia sp!, {r4-r10, pc} @ return
2043 .size bsaes_xts_encrypt,.-bsaes_xts_encrypt
2045 .globl bsaes_xts_decrypt
2046 .type bsaes_xts_decrypt,%function
2050 stmdb sp!, {r4-r10, lr} @ 0x20
2052 mov r6, sp @ future $fp
2059 sub r0, sp, #0x10 @ 0x10
2060 bic r0, #0xf @ align at 16 bytes
2063 #ifdef XTS_CHAIN_TWEAK
2064 ldr r0, [ip] @ pointer to input tweak
2066 @ generate initial tweak
2067 ldr r0, [ip, #4] @ iv[]
2069 ldr r2, [ip, #0] @ key2
2071 mov r0, sp @ pointer to initial tweak
2074 ldr $rounds, [$key, #240] @ get # of rounds
2076 #ifndef BSAES_ASM_EXTENDED_KEY
2077 @ allocate the key schedule on the stack
2078 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
2079 @ add r12, #`128-32` @ size of bit-sliced key schedule
2080 sub r12, #`32+16` @ place for tweak[9]
2082 @ populate the key schedule
2083 mov r4, $key @ pass key
2084 mov r5, $rounds @ pass # of rounds
2086 add r12, #0x90 @ pass key schedule
2087 bl _bsaes_key_convert
2089 vldmia r4, {@XMM[6]}
2090 vstmia r12, {@XMM[15]} @ save last round key
2091 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2092 vstmia r4, {@XMM[7]}
2094 ldr r12, [$key, #244]
2098 str r12, [$key, #244]
2099 mov r4, $key @ pass key
2100 mov r5, $rounds @ pass # of rounds
2101 add r12, $key, #248 @ pass key schedule
2102 bl _bsaes_key_convert
2104 vldmia r4, {@XMM[6]}
2105 vstmia r12, {@XMM[15]} @ save last round key
2106 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2107 vstmia r4, {@XMM[7]}
2110 0: sub sp, #0x90 @ place for tweak[9]
2112 vld1.8 {@XMM[8]}, [r0] @ initial tweak
2113 adr $magic, .Lxts_magic
2115 #ifndef XTS_CHAIN_TWEAK
2116 tst $len, #0xf @ if not multiple of 16
2117 it ne @ Thumb2 thing, sanity check in ARM
2118 subne $len, #0x10 @ subtract another 16 bytes
2127 vldmia $magic, {$twmask} @ load XTS magic
2128 vshr.s64 @T[0], @XMM[8], #63
2130 vand @T[0], @T[0], $twmask
2132 for($i=9;$i<16;$i++) {
2134 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2135 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2136 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2137 vshr.s64 @T[1], @XMM[$i], #63
2138 veor @XMM[$i], @XMM[$i], @T[0]
2139 vand @T[1], @T[1], $twmask
2143 $code.=<<___ if ($i>=10);
2144 vld1.8 {@XMM[$i-10]}, [$inp]!
2146 $code.=<<___ if ($i>=11);
2147 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2151 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
2152 vst1.64 {@XMM[15]}, [r0,:128]!
2153 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2154 veor @XMM[8], @XMM[8], @T[0]
2155 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2157 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
2158 veor @XMM[5], @XMM[5], @XMM[13]
2159 #ifndef BSAES_ASM_EXTENDED_KEY
2160 add r4, sp, #0x90 @ pass key schedule
2162 add r4, $key, #248 @ pass key schedule
2164 veor @XMM[6], @XMM[6], @XMM[14]
2165 mov r5, $rounds @ pass rounds
2166 veor @XMM[7], @XMM[7], @XMM[15]
2171 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2172 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2173 veor @XMM[0], @XMM[0], @XMM[ 8]
2174 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2175 veor @XMM[1], @XMM[1], @XMM[ 9]
2176 veor @XMM[8], @XMM[6], @XMM[10]
2177 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2178 veor @XMM[9], @XMM[4], @XMM[11]
2179 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
2180 veor @XMM[10], @XMM[2], @XMM[12]
2181 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2182 veor @XMM[11], @XMM[7], @XMM[13]
2183 veor @XMM[12], @XMM[3], @XMM[14]
2184 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2185 veor @XMM[13], @XMM[5], @XMM[15]
2186 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
2188 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2197 vldmia $magic, {$twmask} @ load XTS magic
2198 vshr.s64 @T[0], @XMM[8], #63
2200 vand @T[0], @T[0], $twmask
2202 for($i=9;$i<16;$i++) {
2204 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2205 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2206 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2207 vshr.s64 @T[1], @XMM[$i], #63
2208 veor @XMM[$i], @XMM[$i], @T[0]
2209 vand @T[1], @T[1], $twmask
2213 $code.=<<___ if ($i>=10);
2214 vld1.8 {@XMM[$i-10]}, [$inp]!
2216 bmi .Lxts_dec_`$i-9`
2218 $code.=<<___ if ($i>=11);
2219 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2224 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
2226 vld1.8 {@XMM[6]}, [$inp]!
2227 veor @XMM[5], @XMM[5], @XMM[13]
2228 #ifndef BSAES_ASM_EXTENDED_KEY
2229 add r4, sp, #0x90 @ pass key schedule
2231 add r4, $key, #248 @ pass key schedule
2233 veor @XMM[6], @XMM[6], @XMM[14]
2234 mov r5, $rounds @ pass rounds
2239 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2240 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2241 veor @XMM[0], @XMM[0], @XMM[ 8]
2242 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2243 veor @XMM[1], @XMM[1], @XMM[ 9]
2244 veor @XMM[8], @XMM[6], @XMM[10]
2245 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2246 veor @XMM[9], @XMM[4], @XMM[11]
2247 vld1.64 {@XMM[14]}, [r0,:128]!
2248 veor @XMM[10], @XMM[2], @XMM[12]
2249 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2250 veor @XMM[11], @XMM[7], @XMM[13]
2251 veor @XMM[12], @XMM[3], @XMM[14]
2252 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2253 vst1.8 {@XMM[12]}, [$out]!
2255 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2259 vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
2261 veor @XMM[4], @XMM[4], @XMM[12]
2262 #ifndef BSAES_ASM_EXTENDED_KEY
2263 add r4, sp, #0x90 @ pass key schedule
2265 add r4, $key, #248 @ pass key schedule
2267 veor @XMM[5], @XMM[5], @XMM[13]
2268 mov r5, $rounds @ pass rounds
2273 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2274 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2275 veor @XMM[0], @XMM[0], @XMM[ 8]
2276 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2277 veor @XMM[1], @XMM[1], @XMM[ 9]
2278 veor @XMM[8], @XMM[6], @XMM[10]
2279 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2280 veor @XMM[9], @XMM[4], @XMM[11]
2281 veor @XMM[10], @XMM[2], @XMM[12]
2282 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2283 veor @XMM[11], @XMM[7], @XMM[13]
2284 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2286 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2290 vst1.64 {@XMM[13]}, [r0,:128] @ next round tweak
2292 veor @XMM[3], @XMM[3], @XMM[11]
2293 #ifndef BSAES_ASM_EXTENDED_KEY
2294 add r4, sp, #0x90 @ pass key schedule
2296 add r4, $key, #248 @ pass key schedule
2298 veor @XMM[4], @XMM[4], @XMM[12]
2299 mov r5, $rounds @ pass rounds
2304 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2305 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2306 veor @XMM[0], @XMM[0], @XMM[ 8]
2307 vld1.64 {@XMM[12]}, [r0,:128]!
2308 veor @XMM[1], @XMM[1], @XMM[ 9]
2309 veor @XMM[8], @XMM[6], @XMM[10]
2310 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2311 veor @XMM[9], @XMM[4], @XMM[11]
2312 veor @XMM[10], @XMM[2], @XMM[12]
2313 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2314 vst1.8 {@XMM[10]}, [$out]!
2316 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2320 vst1.64 {@XMM[12]}, [r0,:128] @ next round tweak
2322 veor @XMM[2], @XMM[2], @XMM[10]
2323 #ifndef BSAES_ASM_EXTENDED_KEY
2324 add r4, sp, #0x90 @ pass key schedule
2326 add r4, $key, #248 @ pass key schedule
2328 veor @XMM[3], @XMM[3], @XMM[11]
2329 mov r5, $rounds @ pass rounds
2334 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2335 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2336 veor @XMM[0], @XMM[0], @XMM[ 8]
2337 veor @XMM[1], @XMM[1], @XMM[ 9]
2338 veor @XMM[8], @XMM[6], @XMM[10]
2339 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2340 veor @XMM[9], @XMM[4], @XMM[11]
2341 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2343 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2347 vst1.64 {@XMM[11]}, [r0,:128] @ next round tweak
2349 veor @XMM[1], @XMM[1], @XMM[9]
2350 #ifndef BSAES_ASM_EXTENDED_KEY
2351 add r4, sp, #0x90 @ pass key schedule
2353 add r4, $key, #248 @ pass key schedule
2355 veor @XMM[2], @XMM[2], @XMM[10]
2356 mov r5, $rounds @ pass rounds
2361 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2362 vld1.64 {@XMM[10]}, [r0,:128]!
2363 veor @XMM[0], @XMM[0], @XMM[ 8]
2364 veor @XMM[1], @XMM[1], @XMM[ 9]
2365 veor @XMM[8], @XMM[6], @XMM[10]
2366 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2367 vst1.8 {@XMM[8]}, [$out]!
2369 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2373 vst1.64 {@XMM[10]}, [r0,:128] @ next round tweak
2375 veor @XMM[0], @XMM[0], @XMM[8]
2376 #ifndef BSAES_ASM_EXTENDED_KEY
2377 add r4, sp, #0x90 @ pass key schedule
2379 add r4, $key, #248 @ pass key schedule
2381 veor @XMM[1], @XMM[1], @XMM[9]
2382 mov r5, $rounds @ pass rounds
2387 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2388 veor @XMM[0], @XMM[0], @XMM[ 8]
2389 veor @XMM[1], @XMM[1], @XMM[ 9]
2390 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2392 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2397 veor @XMM[0], @XMM[8]
2399 vst1.8 {@XMM[0]}, [sp,:128]
2401 mov r4, $fp @ preserve fp
2402 mov r5, $magic @ preserve magic
2406 vld1.8 {@XMM[0]}, [sp,:128]
2407 veor @XMM[0], @XMM[0], @XMM[8]
2408 vst1.8 {@XMM[0]}, [$out]!
2412 vmov @XMM[8], @XMM[9] @ next round tweak
2415 #ifndef XTS_CHAIN_TWEAK
2419 @ calculate one round of extra tweak for the stolen ciphertext
2420 vldmia $magic, {$twmask}
2421 vshr.s64 @XMM[6], @XMM[8], #63
2422 vand @XMM[6], @XMM[6], $twmask
2423 vadd.u64 @XMM[9], @XMM[8], @XMM[8]
2424 vswp `&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
2425 veor @XMM[9], @XMM[9], @XMM[6]
2427 @ perform the final decryption with the last tweak value
2428 vld1.8 {@XMM[0]}, [$inp]!
2430 veor @XMM[0], @XMM[0], @XMM[9]
2432 vst1.8 {@XMM[0]}, [sp,:128]
2434 mov r4, $fp @ preserve fp
2438 vld1.8 {@XMM[0]}, [sp,:128]
2439 veor @XMM[0], @XMM[0], @XMM[9]
2440 vst1.8 {@XMM[0]}, [$out]
2446 strb r1, [$out, #0x10]
2452 vld1.8 {@XMM[0]}, [r6]
2454 veor @XMM[0], @XMM[8]
2456 vst1.8 {@XMM[0]}, [sp,:128]
2461 vld1.8 {@XMM[0]}, [sp,:128]
2462 veor @XMM[0], @XMM[0], @XMM[8]
2463 vst1.8 {@XMM[0]}, [r6]
2471 #ifdef XTS_CHAIN_TWEAK
2472 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2474 .Lxts_dec_bzero: @ wipe key schedule [if any]
2480 #ifdef XTS_CHAIN_TWEAK
2481 vst1.8 {@XMM[8]}, [r1]
2484 ldmia sp!, {r4-r10, pc} @ return
2486 .size bsaes_xts_decrypt,.-bsaes_xts_decrypt
2493 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
2498 last if (!s/^#/@/ and !/^$/);