2 # Copyright 2012-2018 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
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
16 # Specific modes and adaptation for Linux kernel by Ard Biesheuvel
17 # of Linaro. Permission to use under GPL terms is granted.
18 # ====================================================================
20 # Bit-sliced AES for ARM NEON
24 # This implementation is direct adaptation of bsaes-x86_64 module for
25 # ARM NEON. Except that this module is endian-neutral [in sense that
26 # it can be compiled for either endianness] by courtesy of vld1.8's
27 # neutrality. Initial version doesn't implement interface to OpenSSL,
28 # only low-level primitives and unsupported entry points, just enough
29 # to collect performance results, which for Cortex-A8 core are:
31 # encrypt 19.5 cycles per byte processed with 128-bit key
32 # decrypt 22.1 cycles per byte processed with 128-bit key
33 # key conv. 440 cycles per 128-bit key/0.18 of 8x block
35 # Snapdragon S4 encrypts byte in 17.6 cycles and decrypts in 19.7,
36 # which is [much] worse than anticipated (for further details see
37 # http://www.openssl.org/~appro/Snapdragon-S4.html).
39 # Cortex-A15 manages in 14.2/16.1 cycles [when integer-only code
40 # manages in 20.0 cycles].
42 # When comparing to x86_64 results keep in mind that NEON unit is
43 # [mostly] single-issue and thus can't [fully] benefit from
44 # instruction-level parallelism. And when comparing to aes-armv4
45 # results keep in mind key schedule conversion overhead (see
46 # bsaes-x86_64.pl for further details)...
51 # Add CBC, CTR and XTS subroutines and adapt for kernel use; courtesy of Ard.
54 if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
55 else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
57 if ($flavour && $flavour ne "void") {
58 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
59 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
60 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
61 die "can't locate arm-xlate.pl";
63 open STDOUT,"| \"$^X\" $xlate $flavour $output";
65 open STDOUT,">$output";
68 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
69 my @XMM=map("q$_",(0..15));
72 my ($key,$rounds,$const)=("r4","r5","r6");
74 sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
75 sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
78 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
79 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
84 &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
85 &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
89 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
90 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
93 veor @b[2], @b[2], @b[1]
94 veor @b[5], @b[5], @b[6]
95 veor @b[3], @b[3], @b[0]
96 veor @b[6], @b[6], @b[2]
97 veor @b[5], @b[5], @b[0]
99 veor @b[6], @b[6], @b[3]
100 veor @b[3], @b[3], @b[7]
101 veor @b[7], @b[7], @b[5]
102 veor @b[3], @b[3], @b[4]
103 veor @b[4], @b[4], @b[5]
105 veor @b[2], @b[2], @b[7]
106 veor @b[3], @b[3], @b[1]
107 veor @b[1], @b[1], @b[5]
112 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
113 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
116 veor @b[0], @b[0], @b[6]
117 veor @b[1], @b[1], @b[4]
118 veor @b[4], @b[4], @b[6]
119 veor @b[2], @b[2], @b[0]
120 veor @b[6], @b[6], @b[1]
122 veor @b[1], @b[1], @b[5]
123 veor @b[5], @b[5], @b[3]
124 veor @b[3], @b[3], @b[7]
125 veor @b[7], @b[7], @b[5]
126 veor @b[2], @b[2], @b[5]
128 veor @b[4], @b[4], @b[7]
133 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
134 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
138 &InvInBasisChange (@b);
139 &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
140 &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
143 sub InvInBasisChange { # OutBasisChange in reverse (with twist)
144 my @b=@_[5,1,2,6,3,7,0,4];
146 veor @b[1], @b[1], @b[7]
147 veor @b[4], @b[4], @b[7]
149 veor @b[7], @b[7], @b[5]
150 veor @b[1], @b[1], @b[3]
151 veor @b[2], @b[2], @b[5]
152 veor @b[3], @b[3], @b[7]
154 veor @b[6], @b[6], @b[1]
155 veor @b[2], @b[2], @b[0]
156 veor @b[5], @b[5], @b[3]
157 veor @b[4], @b[4], @b[6]
158 veor @b[0], @b[0], @b[6]
159 veor @b[1], @b[1], @b[4]
163 sub InvOutBasisChange { # InBasisChange in reverse
164 my @b=@_[2,5,7,3,6,1,0,4];
166 veor @b[1], @b[1], @b[5]
167 veor @b[2], @b[2], @b[7]
169 veor @b[3], @b[3], @b[1]
170 veor @b[4], @b[4], @b[5]
171 veor @b[7], @b[7], @b[5]
172 veor @b[3], @b[3], @b[4]
173 veor @b[5], @b[5], @b[0]
174 veor @b[3], @b[3], @b[7]
175 veor @b[6], @b[6], @b[2]
176 veor @b[2], @b[2], @b[1]
177 veor @b[6], @b[6], @b[3]
179 veor @b[3], @b[3], @b[0]
180 veor @b[5], @b[5], @b[6]
185 #;*************************************************************
186 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
187 #;*************************************************************
188 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
200 sub Mul_GF4_N { # not used, see next subroutine
201 # multiply and scale by N
202 my ($x0,$x1,$y0,$y1,$t0)=@_;
215 # interleaved Mul_GF4_N and Mul_GF4
216 my ($x0,$x1,$y0,$y1,$t0,
217 $x2,$x3,$y2,$y3,$t1)=@_;
240 veor @t[0], @x[0], @x[2]
241 veor @t[1], @x[1], @x[3]
243 &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
245 veor @y[0], @y[0], @y[2]
246 veor @y[1], @y[1], @y[3]
248 Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
249 @x[2], @x[3], @y[2], @y[3], @t[2]);
251 veor @x[0], @x[0], @t[0]
252 veor @x[2], @x[2], @t[0]
253 veor @x[1], @x[1], @t[1]
254 veor @x[3], @x[3], @t[1]
256 veor @t[0], @x[4], @x[6]
257 veor @t[1], @x[5], @x[7]
259 &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
260 @x[6], @x[7], @y[2], @y[3], @t[2]);
262 veor @y[0], @y[0], @y[2]
263 veor @y[1], @y[1], @y[3]
265 &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
267 veor @x[4], @x[4], @t[0]
268 veor @x[6], @x[6], @t[0]
269 veor @x[5], @x[5], @t[1]
270 veor @x[7], @x[7], @t[1]
274 #;********************************************************************
275 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
276 #;********************************************************************
280 # direct optimizations from hardware
282 veor @t[3], @x[4], @x[6]
283 veor @t[2], @x[5], @x[7]
284 veor @t[1], @x[1], @x[3]
285 veor @s[1], @x[7], @x[6]
287 veor @s[0], @x[0], @x[2]
289 vorr @t[2], @t[2], @t[1]
290 veor @s[3], @t[3], @t[0]
291 vand @s[2], @t[3], @s[0]
292 vorr @t[3], @t[3], @s[0]
293 veor @s[0], @s[0], @t[1]
294 vand @t[0], @t[0], @t[1]
295 veor @t[1], @x[3], @x[2]
296 vand @s[3], @s[3], @s[0]
297 vand @s[1], @s[1], @t[1]
298 veor @t[1], @x[4], @x[5]
299 veor @s[0], @x[1], @x[0]
300 veor @t[3], @t[3], @s[1]
301 veor @t[2], @t[2], @s[1]
302 vand @s[1], @t[1], @s[0]
303 vorr @t[1], @t[1], @s[0]
304 veor @t[3], @t[3], @s[3]
305 veor @t[0], @t[0], @s[1]
306 veor @t[2], @t[2], @s[2]
307 veor @t[1], @t[1], @s[3]
308 veor @t[0], @t[0], @s[2]
309 vand @s[0], @x[7], @x[3]
310 veor @t[1], @t[1], @s[2]
311 vand @s[1], @x[6], @x[2]
312 vand @s[2], @x[5], @x[1]
313 vorr @s[3], @x[4], @x[0]
314 veor @t[3], @t[3], @s[0]
315 veor @t[1], @t[1], @s[2]
316 veor @t[0], @t[0], @s[3]
317 veor @t[2], @t[2], @s[1]
319 @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
321 @ new smaller inversion
323 vand @s[2], @t[3], @t[1]
326 veor @s[1], @t[2], @s[2]
327 veor @s[3], @t[0], @s[2]
328 veor @s[2], @t[0], @s[2] @ @s[2]=@s[3]
330 vbsl @s[1], @t[1], @t[0]
331 vbsl @s[3], @t[3], @t[2]
332 veor @t[3], @t[3], @t[2]
334 vbsl @s[0], @s[1], @s[2]
335 vbsl @t[0], @s[2], @s[1]
337 vand @s[2], @s[0], @s[3]
338 veor @t[1], @t[1], @t[0]
340 veor @s[2], @s[2], @t[3]
342 # output in s3, s2, s1, t1
344 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
346 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
347 &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
349 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
352 # AES linear components
359 vldmia $key!, {@t[0]-@t[3]}
360 veor @t[0], @t[0], @x[0]
361 veor @t[1], @t[1], @x[1]
362 vtbl.8 `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
363 vtbl.8 `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
364 vldmia $key!, {@t[0]}
365 veor @t[2], @t[2], @x[2]
366 vtbl.8 `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
367 vtbl.8 `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
368 vldmia $key!, {@t[1]}
369 veor @t[3], @t[3], @x[3]
370 vtbl.8 `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
371 vtbl.8 `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
372 vldmia $key!, {@t[2]}
373 vtbl.8 `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
374 vtbl.8 `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
375 vldmia $key!, {@t[3]}
376 veor @t[0], @t[0], @x[4]
377 veor @t[1], @t[1], @x[5]
378 vtbl.8 `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
379 vtbl.8 `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
380 veor @t[2], @t[2], @x[6]
381 vtbl.8 `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
382 vtbl.8 `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
383 veor @t[3], @t[3], @x[7]
384 vtbl.8 `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
385 vtbl.8 `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
386 vtbl.8 `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
387 vtbl.8 `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
392 # modified to emit output in order suitable for feeding back to aesenc[last]
395 my $inv=@_[16]; # optional
397 vext.8 @t[0], @x[0], @x[0], #12 @ x0 <<< 32
398 vext.8 @t[1], @x[1], @x[1], #12
399 veor @x[0], @x[0], @t[0] @ x0 ^ (x0 <<< 32)
400 vext.8 @t[2], @x[2], @x[2], #12
401 veor @x[1], @x[1], @t[1]
402 vext.8 @t[3], @x[3], @x[3], #12
403 veor @x[2], @x[2], @t[2]
404 vext.8 @t[4], @x[4], @x[4], #12
405 veor @x[3], @x[3], @t[3]
406 vext.8 @t[5], @x[5], @x[5], #12
407 veor @x[4], @x[4], @t[4]
408 vext.8 @t[6], @x[6], @x[6], #12
409 veor @x[5], @x[5], @t[5]
410 vext.8 @t[7], @x[7], @x[7], #12
411 veor @x[6], @x[6], @t[6]
413 veor @t[1], @t[1], @x[0]
414 veor @x[7], @x[7], @t[7]
415 vext.8 @x[0], @x[0], @x[0], #8 @ (x0 ^ (x0 <<< 32)) <<< 64)
416 veor @t[2], @t[2], @x[1]
417 veor @t[0], @t[0], @x[7]
418 veor @t[1], @t[1], @x[7]
419 vext.8 @x[1], @x[1], @x[1], #8
420 veor @t[5], @t[5], @x[4]
421 veor @x[0], @x[0], @t[0]
422 veor @t[6], @t[6], @x[5]
423 veor @x[1], @x[1], @t[1]
424 vext.8 @t[0], @x[4], @x[4], #8
425 veor @t[4], @t[4], @x[3]
426 vext.8 @t[1], @x[5], @x[5], #8
427 veor @t[7], @t[7], @x[6]
428 vext.8 @x[4], @x[3], @x[3], #8
429 veor @t[3], @t[3], @x[2]
430 vext.8 @x[5], @x[7], @x[7], #8
431 veor @t[4], @t[4], @x[7]
432 vext.8 @x[3], @x[6], @x[6], #8
433 veor @t[3], @t[3], @x[7]
434 vext.8 @x[6], @x[2], @x[2], #8
435 veor @x[7], @t[1], @t[5]
437 $code.=<<___ if (!$inv);
438 veor @x[2], @t[0], @t[4]
439 veor @x[4], @x[4], @t[3]
440 veor @x[5], @x[5], @t[7]
441 veor @x[3], @x[3], @t[6]
443 veor @x[6], @x[6], @t[2]
446 $code.=<<___ if ($inv);
447 veor @t[3], @t[3], @x[4]
448 veor @x[5], @x[5], @t[7]
449 veor @x[2], @x[3], @t[6]
450 veor @x[3], @t[0], @t[4]
451 veor @x[4], @x[6], @t[2]
457 sub InvMixColumns_orig {
462 @ multiplication by 0x0e
463 vext.8 @t[7], @x[7], @x[7], #12
465 veor @x[2], @x[2], @x[5] @ 2 5
466 veor @x[7], @x[7], @x[5] @ 7 5
467 vext.8 @t[0], @x[0], @x[0], #12
469 veor @x[5], @x[5], @x[0] @ 5 0 [1]
470 veor @x[0], @x[0], @x[1] @ 0 1
471 vext.8 @t[1], @x[1], @x[1], #12
472 veor @x[1], @x[1], @x[2] @ 1 25
473 veor @x[0], @x[0], @x[6] @ 01 6 [2]
474 vext.8 @t[3], @x[3], @x[3], #12
475 veor @x[1], @x[1], @x[3] @ 125 3 [4]
476 veor @x[2], @x[2], @x[0] @ 25 016 [3]
477 veor @x[3], @x[3], @x[7] @ 3 75
478 veor @x[7], @x[7], @x[6] @ 75 6 [0]
479 vext.8 @t[6], @x[6], @x[6], #12
481 veor @x[6], @x[6], @x[4] @ 6 4
482 veor @x[4], @x[4], @x[3] @ 4 375 [6]
483 veor @x[3], @x[3], @x[7] @ 375 756=36
484 veor @x[6], @x[6], @t[5] @ 64 5 [7]
485 veor @x[3], @x[3], @t[2] @ 36 2
486 vext.8 @t[5], @t[5], @t[5], #12
487 veor @x[3], @x[3], @t[4] @ 362 4 [5]
489 my @y = @x[7,5,0,2,1,3,4,6];
491 @ multiplication by 0x0b
492 veor @y[1], @y[1], @y[0]
493 veor @y[0], @y[0], @t[0]
494 vext.8 @t[2], @t[2], @t[2], #12
495 veor @y[1], @y[1], @t[1]
496 veor @y[0], @y[0], @t[5]
497 vext.8 @t[4], @t[4], @t[4], #12
498 veor @y[1], @y[1], @t[6]
499 veor @y[0], @y[0], @t[7]
500 veor @t[7], @t[7], @t[6] @ clobber t[7]
502 veor @y[3], @y[3], @t[0]
503 veor @y[1], @y[1], @y[0]
504 vext.8 @t[0], @t[0], @t[0], #12
505 veor @y[2], @y[2], @t[1]
506 veor @y[4], @y[4], @t[1]
507 vext.8 @t[1], @t[1], @t[1], #12
508 veor @y[2], @y[2], @t[2]
509 veor @y[3], @y[3], @t[2]
510 veor @y[5], @y[5], @t[2]
511 veor @y[2], @y[2], @t[7]
512 vext.8 @t[2], @t[2], @t[2], #12
513 veor @y[3], @y[3], @t[3]
514 veor @y[6], @y[6], @t[3]
515 veor @y[4], @y[4], @t[3]
516 veor @y[7], @y[7], @t[4]
517 vext.8 @t[3], @t[3], @t[3], #12
518 veor @y[5], @y[5], @t[4]
519 veor @y[7], @y[7], @t[7]
520 veor @t[7], @t[7], @t[5] @ clobber t[7] even more
521 veor @y[3], @y[3], @t[5]
522 veor @y[4], @y[4], @t[4]
524 veor @y[5], @y[5], @t[7]
525 vext.8 @t[4], @t[4], @t[4], #12
526 veor @y[6], @y[6], @t[7]
527 veor @y[4], @y[4], @t[7]
529 veor @t[7], @t[7], @t[5]
530 vext.8 @t[5], @t[5], @t[5], #12
532 @ multiplication by 0x0d
533 veor @y[4], @y[4], @y[7]
534 veor @t[7], @t[7], @t[6] @ restore t[7]
535 veor @y[7], @y[7], @t[4]
536 vext.8 @t[6], @t[6], @t[6], #12
537 veor @y[2], @y[2], @t[0]
538 veor @y[7], @y[7], @t[5]
539 vext.8 @t[7], @t[7], @t[7], #12
540 veor @y[2], @y[2], @t[2]
542 veor @y[3], @y[3], @y[1]
543 veor @y[1], @y[1], @t[1]
544 veor @y[0], @y[0], @t[0]
545 veor @y[3], @y[3], @t[0]
546 veor @y[1], @y[1], @t[5]
547 veor @y[0], @y[0], @t[5]
548 vext.8 @t[0], @t[0], @t[0], #12
549 veor @y[1], @y[1], @t[7]
550 veor @y[0], @y[0], @t[6]
551 veor @y[3], @y[3], @y[1]
552 veor @y[4], @y[4], @t[1]
553 vext.8 @t[1], @t[1], @t[1], #12
555 veor @y[7], @y[7], @t[7]
556 veor @y[4], @y[4], @t[2]
557 veor @y[5], @y[5], @t[2]
558 veor @y[2], @y[2], @t[6]
559 veor @t[6], @t[6], @t[3] @ clobber t[6]
560 vext.8 @t[2], @t[2], @t[2], #12
561 veor @y[4], @y[4], @y[7]
562 veor @y[3], @y[3], @t[6]
564 veor @y[6], @y[6], @t[6]
565 veor @y[5], @y[5], @t[5]
566 vext.8 @t[5], @t[5], @t[5], #12
567 veor @y[6], @y[6], @t[4]
568 vext.8 @t[4], @t[4], @t[4], #12
569 veor @y[5], @y[5], @t[6]
570 veor @y[6], @y[6], @t[7]
571 vext.8 @t[7], @t[7], @t[7], #12
572 veor @t[6], @t[6], @t[3] @ restore t[6]
573 vext.8 @t[3], @t[3], @t[3], #12
575 @ multiplication by 0x09
576 veor @y[4], @y[4], @y[1]
577 veor @t[1], @t[1], @y[1] @ t[1]=y[1]
578 veor @t[0], @t[0], @t[5] @ clobber t[0]
579 vext.8 @t[6], @t[6], @t[6], #12
580 veor @t[1], @t[1], @t[5]
581 veor @y[3], @y[3], @t[0]
582 veor @t[0], @t[0], @y[0] @ t[0]=y[0]
583 veor @t[1], @t[1], @t[6]
584 veor @t[6], @t[6], @t[7] @ clobber t[6]
585 veor @y[4], @y[4], @t[1]
586 veor @y[7], @y[7], @t[4]
587 veor @y[6], @y[6], @t[3]
588 veor @y[5], @y[5], @t[2]
589 veor @t[4], @t[4], @y[4] @ t[4]=y[4]
590 veor @t[3], @t[3], @y[3] @ t[3]=y[3]
591 veor @t[5], @t[5], @y[5] @ t[5]=y[5]
592 veor @t[2], @t[2], @y[2] @ t[2]=y[2]
593 veor @t[3], @t[3], @t[7]
594 veor @XMM[5], @t[5], @t[6]
595 veor @XMM[6], @t[6], @y[6] @ t[6]=y[6]
596 veor @XMM[2], @t[2], @t[6]
597 veor @XMM[7], @t[7], @y[7] @ t[7]=y[7]
601 @ vmov @XMM[2], @t[2]
604 @ vmov @XMM[5], @t[5]
605 @ vmov @XMM[6], @t[6]
606 @ vmov @XMM[7], @t[7]
614 # Thanks to Jussi Kivilinna for providing pointer to
616 # | 0e 0b 0d 09 | | 02 03 01 01 | | 05 00 04 00 |
617 # | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
618 # | 0d 09 0e 0b | | 01 01 02 03 | | 04 00 05 00 |
619 # | 0b 0d 09 0e | | 03 01 01 02 | | 00 04 00 05 |
622 @ multiplication by 0x05-0x00-0x04-0x00
623 vext.8 @t[0], @x[0], @x[0], #8
624 vext.8 @t[6], @x[6], @x[6], #8
625 vext.8 @t[7], @x[7], @x[7], #8
626 veor @t[0], @t[0], @x[0]
627 vext.8 @t[1], @x[1], @x[1], #8
628 veor @t[6], @t[6], @x[6]
629 vext.8 @t[2], @x[2], @x[2], #8
630 veor @t[7], @t[7], @x[7]
631 vext.8 @t[3], @x[3], @x[3], #8
632 veor @t[1], @t[1], @x[1]
633 vext.8 @t[4], @x[4], @x[4], #8
634 veor @t[2], @t[2], @x[2]
635 vext.8 @t[5], @x[5], @x[5], #8
636 veor @t[3], @t[3], @x[3]
637 veor @t[4], @t[4], @x[4]
638 veor @t[5], @t[5], @x[5]
640 veor @x[0], @x[0], @t[6]
641 veor @x[1], @x[1], @t[6]
642 veor @x[2], @x[2], @t[0]
643 veor @x[4], @x[4], @t[2]
644 veor @x[3], @x[3], @t[1]
645 veor @x[1], @x[1], @t[7]
646 veor @x[2], @x[2], @t[7]
647 veor @x[4], @x[4], @t[6]
648 veor @x[5], @x[5], @t[3]
649 veor @x[3], @x[3], @t[6]
650 veor @x[6], @x[6], @t[4]
651 veor @x[4], @x[4], @t[7]
652 veor @x[5], @x[5], @t[7]
653 veor @x[7], @x[7], @t[5]
655 &MixColumns (@x,@t,1); # flipped 2<->3 and 4<->6
659 my ($a,$b,$n,$mask,$t)=@_;
670 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
672 vshr.u64 $t0, $b0, #$n
673 vshr.u64 $t1, $b1, #$n
679 vshl.u64 $t0, $t0, #$n
681 vshl.u64 $t1, $t1, #$n
688 my @x=reverse(@_[0..7]);
689 my ($t0,$t1,$t2,$t3)=@_[8..11];
691 vmov.i8 $t0,#0x55 @ compose .LBS0
692 vmov.i8 $t1,#0x33 @ compose .LBS1
694 &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
695 &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
697 vmov.i8 $t0,#0x0f @ compose .LBS2
699 &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
700 &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
702 &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
703 &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
708 # include "arm_arch.h"
710 # define VFP_ABI_PUSH vstmdb sp!,{d8-d15}
711 # define VFP_ABI_POP vldmia sp!,{d8-d15}
712 # define VFP_ABI_FRAME 0x40
714 # define VFP_ABI_PUSH
716 # define VFP_ABI_FRAME 0
717 # define BSAES_ASM_EXTENDED_KEY
718 # define XTS_CHAIN_TWEAK
719 # define __ARM_ARCH__ __LINUX_ARM_ARCH__
720 # define __ARM_MAX_ARCH__ 7
727 #if __ARM_MAX_ARCH__>=7
732 .syntax unified @ ARMv7-capable assembler is expected to handle this
733 #if defined(__thumb2__) && !defined(__APPLE__)
740 .type _bsaes_decrypt8,%function
744 vldmia $key!, {@XMM[9]} @ round 0 key
745 #if defined(__thumb2__) || defined(__APPLE__)
748 add $const,$const,#.LM0ISR-_bsaes_decrypt8
751 vldmia $const!, {@XMM[8]} @ .LM0ISR
752 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
753 veor @XMM[11], @XMM[1], @XMM[9]
754 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
755 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
756 veor @XMM[12], @XMM[2], @XMM[9]
757 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
758 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
759 veor @XMM[13], @XMM[3], @XMM[9]
760 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
761 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
762 veor @XMM[14], @XMM[4], @XMM[9]
763 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
764 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
765 veor @XMM[15], @XMM[5], @XMM[9]
766 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
767 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
768 veor @XMM[10], @XMM[6], @XMM[9]
769 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
770 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
771 veor @XMM[11], @XMM[7], @XMM[9]
772 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
773 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
774 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
775 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
777 &bitslice (@XMM[0..7, 8..11]);
779 sub $rounds,$rounds,#1
784 &ShiftRows (@XMM[0..7, 8..12]);
785 $code.=".Ldec_sbox:\n";
786 &InvSbox (@XMM[0..7, 8..15]);
788 subs $rounds,$rounds,#1
791 &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
793 vldmia $const, {@XMM[12]} @ .LISR
794 ite eq @ Thumb2 thing, sanity check in ARM
795 addeq $const,$const,#0x10
797 vldmia $const, {@XMM[12]} @ .LISRM0
802 &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
804 vldmia $key, {@XMM[8]} @ last round key
805 veor @XMM[6], @XMM[6], @XMM[8]
806 veor @XMM[4], @XMM[4], @XMM[8]
807 veor @XMM[2], @XMM[2], @XMM[8]
808 veor @XMM[7], @XMM[7], @XMM[8]
809 veor @XMM[3], @XMM[3], @XMM[8]
810 veor @XMM[5], @XMM[5], @XMM[8]
811 veor @XMM[0], @XMM[0], @XMM[8]
812 veor @XMM[1], @XMM[1], @XMM[8]
814 .size _bsaes_decrypt8,.-_bsaes_decrypt8
816 .type _bsaes_const,%object
819 .LM0ISR: @ InvShiftRows constants
820 .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
822 .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
824 .quad 0x01040b0e0205080f, 0x0306090c00070a0d
825 .LM0SR: @ ShiftRows constants
826 .quad 0x0a0e02060f03070b, 0x0004080c05090d01
828 .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
830 .quad 0x0304090e00050a0f, 0x01060b0c0207080d
832 .quad 0x02060a0e03070b0f, 0x0004080c0105090d
834 .quad 0x090d01050c000408, 0x03070b0f060a0e02
835 .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
837 .size _bsaes_const,.-_bsaes_const
839 .type _bsaes_encrypt8,%function
843 vldmia $key!, {@XMM[9]} @ round 0 key
844 #if defined(__thumb2__) || defined(__APPLE__)
847 sub $const,$const,#_bsaes_encrypt8-.LM0SR
850 vldmia $const!, {@XMM[8]} @ .LM0SR
852 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
853 veor @XMM[11], @XMM[1], @XMM[9]
854 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
855 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
856 veor @XMM[12], @XMM[2], @XMM[9]
857 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
858 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
859 veor @XMM[13], @XMM[3], @XMM[9]
860 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
861 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
862 veor @XMM[14], @XMM[4], @XMM[9]
863 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
864 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
865 veor @XMM[15], @XMM[5], @XMM[9]
866 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
867 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
868 veor @XMM[10], @XMM[6], @XMM[9]
869 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
870 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
871 veor @XMM[11], @XMM[7], @XMM[9]
872 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
873 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
874 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
875 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
876 _bsaes_encrypt8_bitslice:
878 &bitslice (@XMM[0..7, 8..11]);
880 sub $rounds,$rounds,#1
885 &ShiftRows (@XMM[0..7, 8..12]);
886 $code.=".Lenc_sbox:\n";
887 &Sbox (@XMM[0..7, 8..15]);
889 subs $rounds,$rounds,#1
892 &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
894 vldmia $const, {@XMM[12]} @ .LSR
895 ite eq @ Thumb2 thing, samity check in ARM
896 addeq $const,$const,#0x10
898 vldmia $const, {@XMM[12]} @ .LSRM0
903 # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
904 &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
906 vldmia $key, {@XMM[8]} @ last round key
907 veor @XMM[4], @XMM[4], @XMM[8]
908 veor @XMM[6], @XMM[6], @XMM[8]
909 veor @XMM[3], @XMM[3], @XMM[8]
910 veor @XMM[7], @XMM[7], @XMM[8]
911 veor @XMM[2], @XMM[2], @XMM[8]
912 veor @XMM[5], @XMM[5], @XMM[8]
913 veor @XMM[0], @XMM[0], @XMM[8]
914 veor @XMM[1], @XMM[1], @XMM[8]
916 .size _bsaes_encrypt8,.-_bsaes_encrypt8
920 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
923 my @x=reverse(@_[0..7]);
924 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
926 &swapmove (@x[0,1],1,$bs0,$t2,$t3);
928 @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
932 #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
934 &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
936 @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
942 &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
943 &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
947 .type _bsaes_key_convert,%function
951 vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
952 #if defined(__thumb2__) || defined(__APPLE__)
955 sub $const,$const,#_bsaes_key_convert-.LM0
957 vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
959 vmov.i8 @XMM[8], #0x01 @ bit masks
960 vmov.i8 @XMM[9], #0x02
961 vmov.i8 @XMM[10], #0x04
962 vmov.i8 @XMM[11], #0x08
963 vmov.i8 @XMM[12], #0x10
964 vmov.i8 @XMM[13], #0x20
965 vldmia $const, {@XMM[14]} @ .LM0
968 vrev32.8 @XMM[7], @XMM[7]
969 vrev32.8 @XMM[15], @XMM[15]
971 sub $rounds,$rounds,#1
972 vstmia $out!, {@XMM[7]} @ save round 0 key
977 vtbl.8 `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
978 vtbl.8 `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
979 vmov.i8 @XMM[6], #0x40
980 vmov.i8 @XMM[15], #0x80
982 vtst.8 @XMM[0], @XMM[7], @XMM[8]
983 vtst.8 @XMM[1], @XMM[7], @XMM[9]
984 vtst.8 @XMM[2], @XMM[7], @XMM[10]
985 vtst.8 @XMM[3], @XMM[7], @XMM[11]
986 vtst.8 @XMM[4], @XMM[7], @XMM[12]
987 vtst.8 @XMM[5], @XMM[7], @XMM[13]
988 vtst.8 @XMM[6], @XMM[7], @XMM[6]
989 vtst.8 @XMM[7], @XMM[7], @XMM[15]
990 vld1.8 {@XMM[15]}, [$inp]! @ load next round key
991 vmvn @XMM[0], @XMM[0] @ "pnot"
992 vmvn @XMM[1], @XMM[1]
993 vmvn @XMM[5], @XMM[5]
994 vmvn @XMM[6], @XMM[6]
996 vrev32.8 @XMM[15], @XMM[15]
998 subs $rounds,$rounds,#1
999 vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
1002 vmov.i8 @XMM[7],#0x63 @ compose .L63
1003 @ don't save last round key
1005 .size _bsaes_key_convert,.-_bsaes_key_convert
1009 if (0) { # following four functions are unsupported interface
1010 # used for benchmarking...
1012 .globl bsaes_enc_key_convert
1013 .type bsaes_enc_key_convert,%function
1015 bsaes_enc_key_convert:
1016 stmdb sp!,{r4-r6,lr}
1017 vstmdb sp!,{d8-d15} @ ABI specification says so
1019 ldr r5,[$inp,#240] @ pass rounds
1020 mov r4,$inp @ pass key
1021 mov r12,$out @ pass key schedule
1022 bl _bsaes_key_convert
1023 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1024 vstmia r12, {@XMM[7]} @ save last round key
1027 ldmia sp!,{r4-r6,pc}
1028 .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
1030 .globl bsaes_encrypt_128
1031 .type bsaes_encrypt_128,%function
1034 stmdb sp!,{r4-r6,lr}
1035 vstmdb sp!,{d8-d15} @ ABI specification says so
1037 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1038 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1039 mov r4,$key @ pass the key
1040 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1041 mov r5,#10 @ pass rounds
1042 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1046 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1047 vst1.8 {@XMM[4]}, [$out]!
1048 vst1.8 {@XMM[6]}, [$out]!
1049 vst1.8 {@XMM[3]}, [$out]!
1050 vst1.8 {@XMM[7]}, [$out]!
1051 vst1.8 {@XMM[2]}, [$out]!
1052 subs $len,$len,#0x80
1053 vst1.8 {@XMM[5]}, [$out]!
1057 ldmia sp!,{r4-r6,pc}
1058 .size bsaes_encrypt_128,.-bsaes_encrypt_128
1060 .globl bsaes_dec_key_convert
1061 .type bsaes_dec_key_convert,%function
1063 bsaes_dec_key_convert:
1064 stmdb sp!,{r4-r6,lr}
1065 vstmdb sp!,{d8-d15} @ ABI specification says so
1067 ldr r5,[$inp,#240] @ pass rounds
1068 mov r4,$inp @ pass key
1069 mov r12,$out @ pass key schedule
1070 bl _bsaes_key_convert
1071 vldmia $out, {@XMM[6]}
1072 vstmia r12, {@XMM[15]} @ save last round key
1073 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1074 vstmia $out, {@XMM[7]}
1077 ldmia sp!,{r4-r6,pc}
1078 .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
1080 .globl bsaes_decrypt_128
1081 .type bsaes_decrypt_128,%function
1084 stmdb sp!,{r4-r6,lr}
1085 vstmdb sp!,{d8-d15} @ ABI specification says so
1087 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1088 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1089 mov r4,$key @ pass the key
1090 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1091 mov r5,#10 @ pass rounds
1092 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1096 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1097 vst1.8 {@XMM[6]}, [$out]!
1098 vst1.8 {@XMM[4]}, [$out]!
1099 vst1.8 {@XMM[2]}, [$out]!
1100 vst1.8 {@XMM[7]}, [$out]!
1101 vst1.8 {@XMM[3]}, [$out]!
1102 subs $len,$len,#0x80
1103 vst1.8 {@XMM[5]}, [$out]!
1107 ldmia sp!,{r4-r6,pc}
1108 .size bsaes_decrypt_128,.-bsaes_decrypt_128
1112 my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
1113 my ($keysched)=("sp");
1116 .extern AES_cbc_encrypt
1119 .global bsaes_cbc_encrypt
1120 .type bsaes_cbc_encrypt,%function
1134 @ it is up to the caller to make sure we are called with enc == 0
1137 stmdb sp!, {r4-r10, lr}
1139 ldr $ivp, [ip] @ IV is 1st arg on the stack
1140 mov $len, $len, lsr#4 @ len in 16 byte blocks
1141 sub sp, #0x10 @ scratch space to carry over the IV
1142 mov $fp, sp @ save sp
1144 ldr $rounds, [$key, #240] @ get # of rounds
1145 #ifndef BSAES_ASM_EXTENDED_KEY
1146 @ allocate the key schedule on the stack
1147 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1148 add r12, #`128-32` @ sifze of bit-slices key schedule
1150 @ populate the key schedule
1151 mov r4, $key @ pass key
1152 mov r5, $rounds @ pass # of rounds
1153 mov sp, r12 @ sp is $keysched
1154 bl _bsaes_key_convert
1155 vldmia $keysched, {@XMM[6]}
1156 vstmia r12, {@XMM[15]} @ save last round key
1157 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1158 vstmia $keysched, {@XMM[7]}
1160 ldr r12, [$key, #244]
1164 @ populate the key schedule
1165 str r12, [$key, #244]
1166 mov r4, $key @ pass key
1167 mov r5, $rounds @ pass # of rounds
1168 add r12, $key, #248 @ pass key schedule
1169 bl _bsaes_key_convert
1171 vldmia r4, {@XMM[6]}
1172 vstmia r12, {@XMM[15]} @ save last round key
1173 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1174 vstmia r4, {@XMM[7]}
1180 vld1.8 {@XMM[15]}, [$ivp] @ load IV
1185 subs $len, $len, #0x8
1186 bmi .Lcbc_dec_loop_finish
1188 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1189 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1190 #ifndef BSAES_ASM_EXTENDED_KEY
1191 mov r4, $keysched @ pass the key
1195 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1197 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]
1198 sub $inp, $inp, #0x60
1199 vstmia $fp, {@XMM[15]} @ put aside IV
1203 vldmia $fp, {@XMM[14]} @ reload IV
1204 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1205 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1206 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1207 veor @XMM[1], @XMM[1], @XMM[8]
1208 veor @XMM[6], @XMM[6], @XMM[9]
1209 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1210 veor @XMM[4], @XMM[4], @XMM[10]
1211 veor @XMM[2], @XMM[2], @XMM[11]
1212 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1213 veor @XMM[7], @XMM[7], @XMM[12]
1214 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1215 veor @XMM[3], @XMM[3], @XMM[13]
1216 vst1.8 {@XMM[6]}, [$out]!
1217 veor @XMM[5], @XMM[5], @XMM[14]
1218 vst1.8 {@XMM[4]}, [$out]!
1219 vst1.8 {@XMM[2]}, [$out]!
1220 vst1.8 {@XMM[7]}, [$out]!
1221 vst1.8 {@XMM[3]}, [$out]!
1222 vst1.8 {@XMM[5]}, [$out]!
1226 .Lcbc_dec_loop_finish:
1230 vld1.8 {@XMM[0]}, [$inp]! @ load input
1233 vld1.8 {@XMM[1]}, [$inp]!
1234 #ifndef BSAES_ASM_EXTENDED_KEY
1235 mov r4, $keysched @ pass the key
1240 vstmia $fp, {@XMM[15]} @ put aside IV
1242 vld1.8 {@XMM[2]}, [$inp]!
1245 vld1.8 {@XMM[3]}, [$inp]!
1247 vld1.8 {@XMM[4]}, [$inp]!
1250 vld1.8 {@XMM[5]}, [$inp]!
1252 vld1.8 {@XMM[6]}, [$inp]!
1253 sub $inp, $inp, #0x70
1257 vldmia $fp, {@XMM[14]} @ reload IV
1258 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1259 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1260 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1261 veor @XMM[1], @XMM[1], @XMM[8]
1262 veor @XMM[6], @XMM[6], @XMM[9]
1263 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1264 veor @XMM[4], @XMM[4], @XMM[10]
1265 veor @XMM[2], @XMM[2], @XMM[11]
1266 vld1.8 {@XMM[15]}, [$inp]!
1267 veor @XMM[7], @XMM[7], @XMM[12]
1268 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1269 veor @XMM[3], @XMM[3], @XMM[13]
1270 vst1.8 {@XMM[6]}, [$out]!
1271 vst1.8 {@XMM[4]}, [$out]!
1272 vst1.8 {@XMM[2]}, [$out]!
1273 vst1.8 {@XMM[7]}, [$out]!
1274 vst1.8 {@XMM[3]}, [$out]!
1278 sub $inp, $inp, #0x60
1280 vldmia $fp,{@XMM[14]} @ reload IV
1281 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1282 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1283 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1284 veor @XMM[1], @XMM[1], @XMM[8]
1285 veor @XMM[6], @XMM[6], @XMM[9]
1286 vld1.8 {@XMM[12]}, [$inp]!
1287 veor @XMM[4], @XMM[4], @XMM[10]
1288 veor @XMM[2], @XMM[2], @XMM[11]
1289 vld1.8 {@XMM[15]}, [$inp]!
1290 veor @XMM[7], @XMM[7], @XMM[12]
1291 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1292 vst1.8 {@XMM[6]}, [$out]!
1293 vst1.8 {@XMM[4]}, [$out]!
1294 vst1.8 {@XMM[2]}, [$out]!
1295 vst1.8 {@XMM[7]}, [$out]!
1299 sub $inp, $inp, #0x50
1301 vldmia $fp, {@XMM[14]} @ reload IV
1302 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1303 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1304 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1305 veor @XMM[1], @XMM[1], @XMM[8]
1306 veor @XMM[6], @XMM[6], @XMM[9]
1307 vld1.8 {@XMM[15]}, [$inp]!
1308 veor @XMM[4], @XMM[4], @XMM[10]
1309 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1310 veor @XMM[2], @XMM[2], @XMM[11]
1311 vst1.8 {@XMM[6]}, [$out]!
1312 vst1.8 {@XMM[4]}, [$out]!
1313 vst1.8 {@XMM[2]}, [$out]!
1317 sub $inp, $inp, #0x40
1319 vldmia $fp, {@XMM[14]} @ reload IV
1320 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1321 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1322 vld1.8 {@XMM[10]}, [$inp]!
1323 veor @XMM[1], @XMM[1], @XMM[8]
1324 veor @XMM[6], @XMM[6], @XMM[9]
1325 vld1.8 {@XMM[15]}, [$inp]!
1326 veor @XMM[4], @XMM[4], @XMM[10]
1327 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1328 vst1.8 {@XMM[6]}, [$out]!
1329 vst1.8 {@XMM[4]}, [$out]!
1333 sub $inp, $inp, #0x30
1335 vldmia $fp, {@XMM[14]} @ reload IV
1336 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1337 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1338 vld1.8 {@XMM[15]}, [$inp]!
1339 veor @XMM[1], @XMM[1], @XMM[8]
1340 veor @XMM[6], @XMM[6], @XMM[9]
1341 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1342 vst1.8 {@XMM[6]}, [$out]!
1346 sub $inp, $inp, #0x20
1348 vldmia $fp, {@XMM[14]} @ reload IV
1349 vld1.8 {@XMM[8]}, [$inp]! @ reload input
1350 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1351 vld1.8 {@XMM[15]}, [$inp]! @ reload input
1352 veor @XMM[1], @XMM[1], @XMM[8]
1353 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1357 sub $inp, $inp, #0x10
1358 mov $rounds, $out @ save original out pointer
1359 mov $out, $fp @ use the iv scratch space as out buffer
1361 vmov @XMM[4],@XMM[15] @ just in case ensure that IV
1362 vmov @XMM[5],@XMM[0] @ and input are preserved
1364 vld1.8 {@XMM[0]}, [$fp] @ load result
1365 veor @XMM[0], @XMM[0], @XMM[4] @ ^= IV
1366 vmov @XMM[15], @XMM[5] @ @XMM[5] holds input
1367 vst1.8 {@XMM[0]}, [$rounds] @ write output
1370 #ifndef BSAES_ASM_EXTENDED_KEY
1373 .Lcbc_dec_bzero: @ wipe key schedule [if any]
1374 vstmia $keysched!, {q0-q1}
1380 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1381 vst1.8 {@XMM[15]}, [$ivp] @ return IV
1383 ldmia sp!, {r4-r10, pc}
1384 .size bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
1388 my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1389 my $const = "r6"; # shared with _bsaes_encrypt8_alt
1390 my $keysched = "sp";
1394 .global bsaes_ctr32_encrypt_blocks
1395 .type bsaes_ctr32_encrypt_blocks,%function
1397 bsaes_ctr32_encrypt_blocks:
1398 cmp $len, #8 @ use plain AES for
1399 blo .Lctr_enc_short @ small sizes
1402 stmdb sp!, {r4-r10, lr}
1404 ldr $ctr, [ip] @ ctr is 1st arg on the stack
1405 sub sp, sp, #0x10 @ scratch space to carry over the ctr
1406 mov $fp, sp @ save sp
1408 ldr $rounds, [$key, #240] @ get # of rounds
1409 #ifndef BSAES_ASM_EXTENDED_KEY
1410 @ allocate the key schedule on the stack
1411 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1412 add r12, #`128-32` @ size of bit-sliced key schedule
1414 @ populate the key schedule
1415 mov r4, $key @ pass key
1416 mov r5, $rounds @ pass # of rounds
1417 mov sp, r12 @ sp is $keysched
1418 bl _bsaes_key_convert
1419 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1420 vstmia r12, {@XMM[7]} @ save last round key
1422 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1424 mov $ctr, #:lower16:(.LREVM0SR-.LM0)
1425 add $ctr, $const, $ctr
1427 add $ctr, $const, #.LREVM0SR-.LM0 @ borrow $ctr
1429 vldmia $keysched, {@XMM[4]} @ load round0 key
1431 ldr r12, [$key, #244]
1435 @ populate the key schedule
1436 str r12, [$key, #244]
1437 mov r4, $key @ pass key
1438 mov r5, $rounds @ pass # of rounds
1439 add r12, $key, #248 @ pass key schedule
1440 bl _bsaes_key_convert
1441 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1442 vstmia r12, {@XMM[7]} @ save last round key
1445 0: add r12, $key, #248
1446 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1447 adrl $ctr, .LREVM0SR @ borrow $ctr
1448 vldmia r12, {@XMM[4]} @ load round0 key
1449 sub sp, #0x10 @ place for adjusted round0 key
1452 vmov.i32 @XMM[8],#1 @ compose 1<<96
1453 veor @XMM[9],@XMM[9],@XMM[9]
1454 vrev32.8 @XMM[0],@XMM[0]
1455 vext.8 @XMM[8],@XMM[9],@XMM[8],#4
1456 vrev32.8 @XMM[4],@XMM[4]
1457 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1458 vstmia $keysched, {@XMM[4]} @ save adjusted round0 key
1463 vadd.u32 @XMM[10], @XMM[8], @XMM[9] @ compose 3<<96
1464 vadd.u32 @XMM[1], @XMM[0], @XMM[8] @ +1
1465 vadd.u32 @XMM[2], @XMM[0], @XMM[9] @ +2
1466 vadd.u32 @XMM[3], @XMM[0], @XMM[10] @ +3
1467 vadd.u32 @XMM[4], @XMM[1], @XMM[10]
1468 vadd.u32 @XMM[5], @XMM[2], @XMM[10]
1469 vadd.u32 @XMM[6], @XMM[3], @XMM[10]
1470 vadd.u32 @XMM[7], @XMM[4], @XMM[10]
1471 vadd.u32 @XMM[10], @XMM[5], @XMM[10] @ next counter
1473 @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1474 @ to flip byte order in 32-bit counter
1476 vldmia $keysched, {@XMM[9]} @ load round0 key
1477 #ifndef BSAES_ASM_EXTENDED_KEY
1478 add r4, $keysched, #0x10 @ pass next round key
1480 add r4, $key, #`248+16`
1482 vldmia $ctr, {@XMM[8]} @ .LREVM0SR
1483 mov r5, $rounds @ pass rounds
1484 vstmia $fp, {@XMM[10]} @ save next counter
1486 mov $const, #:lower16:(.LREVM0SR-.LSR)
1487 sub $const, $ctr, $const
1489 sub $const, $ctr, #.LREVM0SR-.LSR @ pass constants
1492 bl _bsaes_encrypt8_alt
1495 blo .Lctr_enc_loop_done
1497 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ load input
1498 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1499 veor @XMM[0], @XMM[8]
1500 veor @XMM[1], @XMM[9]
1501 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1502 veor @XMM[4], @XMM[10]
1503 veor @XMM[6], @XMM[11]
1504 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1505 veor @XMM[3], @XMM[12]
1506 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1507 veor @XMM[7], @XMM[13]
1508 veor @XMM[2], @XMM[14]
1509 vst1.8 {@XMM[4]}, [$out]!
1510 veor @XMM[5], @XMM[15]
1511 vst1.8 {@XMM[6]}, [$out]!
1512 vmov.i32 @XMM[8], #1 @ compose 1<<96
1513 vst1.8 {@XMM[3]}, [$out]!
1514 veor @XMM[9], @XMM[9], @XMM[9]
1515 vst1.8 {@XMM[7]}, [$out]!
1516 vext.8 @XMM[8], @XMM[9], @XMM[8], #4
1517 vst1.8 {@XMM[2]}, [$out]!
1518 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1519 vst1.8 {@XMM[5]}, [$out]!
1520 vldmia $fp, {@XMM[0]} @ load counter
1526 .Lctr_enc_loop_done:
1528 vld1.8 {@XMM[8]}, [$inp]! @ load input
1529 veor @XMM[0], @XMM[8]
1530 vst1.8 {@XMM[0]}, [$out]! @ write output
1533 vld1.8 {@XMM[9]}, [$inp]!
1534 veor @XMM[1], @XMM[9]
1535 vst1.8 {@XMM[1]}, [$out]!
1537 vld1.8 {@XMM[10]}, [$inp]!
1538 veor @XMM[4], @XMM[10]
1539 vst1.8 {@XMM[4]}, [$out]!
1542 vld1.8 {@XMM[11]}, [$inp]!
1543 veor @XMM[6], @XMM[11]
1544 vst1.8 {@XMM[6]}, [$out]!
1546 vld1.8 {@XMM[12]}, [$inp]!
1547 veor @XMM[3], @XMM[12]
1548 vst1.8 {@XMM[3]}, [$out]!
1551 vld1.8 {@XMM[13]}, [$inp]!
1552 veor @XMM[7], @XMM[13]
1553 vst1.8 {@XMM[7]}, [$out]!
1555 vld1.8 {@XMM[14]}, [$inp]
1556 veor @XMM[2], @XMM[14]
1557 vst1.8 {@XMM[2]}, [$out]!
1562 #ifndef BSAES_ASM_EXTENDED_KEY
1563 .Lctr_enc_bzero: @ wipe key schedule [if any]
1564 vstmia $keysched!, {q0-q1}
1568 vstmia $keysched, {q0-q1}
1572 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1574 ldmia sp!, {r4-r10, pc} @ return
1578 ldr ip, [sp] @ ctr pointer is passed on stack
1579 stmdb sp!, {r4-r8, lr}
1581 mov r4, $inp @ copy arguments
1585 ldr r8, [ip, #12] @ load counter LSW
1586 vld1.8 {@XMM[1]}, [ip] @ load whole counter value
1591 vst1.8 {@XMM[1]}, [sp] @ copy counter value
1594 .Lctr_enc_short_loop:
1595 add r0, sp, #0x10 @ input counter value
1596 mov r1, sp @ output on the stack
1601 vld1.8 {@XMM[0]}, [r4]! @ load input
1602 vld1.8 {@XMM[1]}, [sp] @ load encrypted counter
1606 str r0, [sp, #0x1c] @ next counter value
1608 str r8, [sp, #0x1c] @ next counter value
1610 veor @XMM[0],@XMM[0],@XMM[1]
1611 vst1.8 {@XMM[0]}, [r5]! @ store output
1613 bne .Lctr_enc_short_loop
1619 ldmia sp!, {r4-r8, pc}
1620 .size bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
1624 ######################################################################
1625 # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1626 # const AES_KEY *key1, const AES_KEY *key2,
1627 # const unsigned char iv[16]);
1629 my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
1630 my $const="r6"; # returned by _bsaes_key_convert
1635 .globl bsaes_xts_encrypt
1636 .type bsaes_xts_encrypt,%function
1640 stmdb sp!, {r4-r10, lr} @ 0x20
1642 mov r6, sp @ future $fp
1649 sub r0, sp, #0x10 @ 0x10
1650 bic r0, #0xf @ align at 16 bytes
1653 #ifdef XTS_CHAIN_TWEAK
1654 ldr r0, [ip] @ pointer to input tweak
1656 @ generate initial tweak
1657 ldr r0, [ip, #4] @ iv[]
1659 ldr r2, [ip, #0] @ key2
1661 mov r0,sp @ pointer to initial tweak
1664 ldr $rounds, [$key, #240] @ get # of rounds
1666 #ifndef BSAES_ASM_EXTENDED_KEY
1667 @ allocate the key schedule on the stack
1668 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1669 @ add r12, #`128-32` @ size of bit-sliced key schedule
1670 sub r12, #`32+16` @ place for tweak[9]
1672 @ populate the key schedule
1673 mov r4, $key @ pass key
1674 mov r5, $rounds @ pass # of rounds
1676 add r12, #0x90 @ pass key schedule
1677 bl _bsaes_key_convert
1678 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1679 vstmia r12, {@XMM[7]} @ save last round key
1681 ldr r12, [$key, #244]
1685 str r12, [$key, #244]
1686 mov r4, $key @ pass key
1687 mov r5, $rounds @ pass # of rounds
1688 add r12, $key, #248 @ pass key schedule
1689 bl _bsaes_key_convert
1690 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1691 vstmia r12, {@XMM[7]}
1694 0: sub sp, #0x90 @ place for tweak[9]
1697 vld1.8 {@XMM[8]}, [r0] @ initial tweak
1698 adr $magic, .Lxts_magic
1706 vldmia $magic, {$twmask} @ load XTS magic
1707 vshr.s64 @T[0], @XMM[8], #63
1709 vand @T[0], @T[0], $twmask
1711 for($i=9;$i<16;$i++) {
1713 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1714 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1715 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1716 vshr.s64 @T[1], @XMM[$i], #63
1717 veor @XMM[$i], @XMM[$i], @T[0]
1718 vand @T[1], @T[1], $twmask
1722 $code.=<<___ if ($i>=10);
1723 vld1.8 {@XMM[$i-10]}, [$inp]!
1725 $code.=<<___ if ($i>=11);
1726 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1730 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
1731 vst1.64 {@XMM[15]}, [r0,:128]!
1732 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1733 veor @XMM[8], @XMM[8], @T[0]
1734 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1736 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1737 veor @XMM[5], @XMM[5], @XMM[13]
1738 #ifndef BSAES_ASM_EXTENDED_KEY
1739 add r4, sp, #0x90 @ pass key schedule
1741 add r4, $key, #248 @ pass key schedule
1743 veor @XMM[6], @XMM[6], @XMM[14]
1744 mov r5, $rounds @ pass rounds
1745 veor @XMM[7], @XMM[7], @XMM[15]
1750 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1751 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1752 veor @XMM[0], @XMM[0], @XMM[ 8]
1753 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1754 veor @XMM[1], @XMM[1], @XMM[ 9]
1755 veor @XMM[8], @XMM[4], @XMM[10]
1756 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1757 veor @XMM[9], @XMM[6], @XMM[11]
1758 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
1759 veor @XMM[10], @XMM[3], @XMM[12]
1760 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1761 veor @XMM[11], @XMM[7], @XMM[13]
1762 veor @XMM[12], @XMM[2], @XMM[14]
1763 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1764 veor @XMM[13], @XMM[5], @XMM[15]
1765 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
1767 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1776 vldmia $magic, {$twmask} @ load XTS magic
1777 vshr.s64 @T[0], @XMM[8], #63
1779 vand @T[0], @T[0], $twmask
1781 for($i=9;$i<16;$i++) {
1783 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1784 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1785 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1786 vshr.s64 @T[1], @XMM[$i], #63
1787 veor @XMM[$i], @XMM[$i], @T[0]
1788 vand @T[1], @T[1], $twmask
1792 $code.=<<___ if ($i>=10);
1793 vld1.8 {@XMM[$i-10]}, [$inp]!
1795 bmi .Lxts_enc_`$i-9`
1797 $code.=<<___ if ($i>=11);
1798 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1803 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
1805 vld1.8 {@XMM[6]}, [$inp]!
1806 veor @XMM[5], @XMM[5], @XMM[13]
1807 #ifndef BSAES_ASM_EXTENDED_KEY
1808 add r4, sp, #0x90 @ pass key schedule
1810 add r4, $key, #248 @ pass key schedule
1812 veor @XMM[6], @XMM[6], @XMM[14]
1813 mov r5, $rounds @ pass rounds
1818 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1819 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1820 veor @XMM[0], @XMM[0], @XMM[ 8]
1821 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1822 veor @XMM[1], @XMM[1], @XMM[ 9]
1823 veor @XMM[8], @XMM[4], @XMM[10]
1824 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1825 veor @XMM[9], @XMM[6], @XMM[11]
1826 vld1.64 {@XMM[14]}, [r0,:128]!
1827 veor @XMM[10], @XMM[3], @XMM[12]
1828 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1829 veor @XMM[11], @XMM[7], @XMM[13]
1830 veor @XMM[12], @XMM[2], @XMM[14]
1831 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1832 vst1.8 {@XMM[12]}, [$out]!
1834 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1838 veor @XMM[4], @XMM[4], @XMM[12]
1839 #ifndef BSAES_ASM_EXTENDED_KEY
1840 add r4, sp, #0x90 @ pass key schedule
1842 add r4, $key, #248 @ pass key schedule
1844 veor @XMM[5], @XMM[5], @XMM[13]
1845 mov r5, $rounds @ pass rounds
1850 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1851 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1852 veor @XMM[0], @XMM[0], @XMM[ 8]
1853 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1854 veor @XMM[1], @XMM[1], @XMM[ 9]
1855 veor @XMM[8], @XMM[4], @XMM[10]
1856 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1857 veor @XMM[9], @XMM[6], @XMM[11]
1858 veor @XMM[10], @XMM[3], @XMM[12]
1859 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1860 veor @XMM[11], @XMM[7], @XMM[13]
1861 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1863 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1866 @ put this in range for both ARM and Thumb mode adr instructions
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 veor @XMM[2], @XMM[2], @XMM[10]
1902 #ifndef BSAES_ASM_EXTENDED_KEY
1903 add r4, sp, #0x90 @ pass key schedule
1905 add r4, $key, #248 @ pass key schedule
1907 veor @XMM[3], @XMM[3], @XMM[11]
1908 mov r5, $rounds @ pass rounds
1913 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1914 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1915 veor @XMM[0], @XMM[0], @XMM[ 8]
1916 veor @XMM[1], @XMM[1], @XMM[ 9]
1917 veor @XMM[8], @XMM[4], @XMM[10]
1918 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1919 veor @XMM[9], @XMM[6], @XMM[11]
1920 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1922 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1926 veor @XMM[1], @XMM[1], @XMM[9]
1927 #ifndef BSAES_ASM_EXTENDED_KEY
1928 add r4, sp, #0x90 @ pass key schedule
1930 add r4, $key, #248 @ pass key schedule
1932 veor @XMM[2], @XMM[2], @XMM[10]
1933 mov r5, $rounds @ pass rounds
1938 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1939 vld1.64 {@XMM[10]}, [r0,:128]!
1940 veor @XMM[0], @XMM[0], @XMM[ 8]
1941 veor @XMM[1], @XMM[1], @XMM[ 9]
1942 veor @XMM[8], @XMM[4], @XMM[10]
1943 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1944 vst1.8 {@XMM[8]}, [$out]!
1946 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1950 veor @XMM[0], @XMM[0], @XMM[8]
1951 #ifndef BSAES_ASM_EXTENDED_KEY
1952 add r4, sp, #0x90 @ pass key schedule
1954 add r4, $key, #248 @ pass key schedule
1956 veor @XMM[1], @XMM[1], @XMM[9]
1957 mov r5, $rounds @ pass rounds
1962 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1963 veor @XMM[0], @XMM[0], @XMM[ 8]
1964 veor @XMM[1], @XMM[1], @XMM[ 9]
1965 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1967 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1972 veor @XMM[0], @XMM[0], @XMM[8]
1974 vst1.8 {@XMM[0]}, [sp,:128]
1976 mov r4, $fp @ preserve fp
1980 vld1.8 {@XMM[0]}, [sp,:128]
1981 veor @XMM[0], @XMM[0], @XMM[8]
1982 vst1.8 {@XMM[0]}, [$out]!
1985 vmov @XMM[8], @XMM[9] @ next round tweak
1988 #ifndef XTS_CHAIN_TWEAK
1995 ldrb r1, [$out, #-0x10]
1996 strb r0, [$out, #-0x10]
2002 vld1.8 {@XMM[0]}, [r6]
2004 veor @XMM[0], @XMM[0], @XMM[8]
2006 vst1.8 {@XMM[0]}, [sp,:128]
2008 mov r4, $fp @ preserve fp
2012 vld1.8 {@XMM[0]}, [sp,:128]
2013 veor @XMM[0], @XMM[0], @XMM[8]
2014 vst1.8 {@XMM[0]}, [r6]
2022 #ifdef XTS_CHAIN_TWEAK
2023 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2025 .Lxts_enc_bzero: @ wipe key schedule [if any]
2031 #ifdef XTS_CHAIN_TWEAK
2032 vst1.8 {@XMM[8]}, [r1]
2035 ldmia sp!, {r4-r10, pc} @ return
2037 .size bsaes_xts_encrypt,.-bsaes_xts_encrypt
2039 .globl bsaes_xts_decrypt
2040 .type bsaes_xts_decrypt,%function
2044 stmdb sp!, {r4-r10, lr} @ 0x20
2046 mov r6, sp @ future $fp
2053 sub r0, sp, #0x10 @ 0x10
2054 bic r0, #0xf @ align at 16 bytes
2057 #ifdef XTS_CHAIN_TWEAK
2058 ldr r0, [ip] @ pointer to input tweak
2060 @ generate initial tweak
2061 ldr r0, [ip, #4] @ iv[]
2063 ldr r2, [ip, #0] @ key2
2065 mov r0, sp @ pointer to initial tweak
2068 ldr $rounds, [$key, #240] @ get # of rounds
2070 #ifndef BSAES_ASM_EXTENDED_KEY
2071 @ allocate the key schedule on the stack
2072 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
2073 @ add r12, #`128-32` @ size of bit-sliced key schedule
2074 sub r12, #`32+16` @ place for tweak[9]
2076 @ populate the key schedule
2077 mov r4, $key @ pass key
2078 mov r5, $rounds @ pass # of rounds
2080 add r12, #0x90 @ pass key schedule
2081 bl _bsaes_key_convert
2083 vldmia r4, {@XMM[6]}
2084 vstmia r12, {@XMM[15]} @ save last round key
2085 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2086 vstmia r4, {@XMM[7]}
2088 ldr r12, [$key, #244]
2092 str r12, [$key, #244]
2093 mov r4, $key @ pass key
2094 mov r5, $rounds @ pass # of rounds
2095 add r12, $key, #248 @ pass key schedule
2096 bl _bsaes_key_convert
2098 vldmia r4, {@XMM[6]}
2099 vstmia r12, {@XMM[15]} @ save last round key
2100 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2101 vstmia r4, {@XMM[7]}
2104 0: sub sp, #0x90 @ place for tweak[9]
2106 vld1.8 {@XMM[8]}, [r0] @ initial tweak
2107 adr $magic, .Lxts_magic
2109 #ifndef XTS_CHAIN_TWEAK
2110 tst $len, #0xf @ if not multiple of 16
2111 it ne @ Thumb2 thing, sanity check in ARM
2112 subne $len, #0x10 @ subtract another 16 bytes
2121 vldmia $magic, {$twmask} @ load XTS magic
2122 vshr.s64 @T[0], @XMM[8], #63
2124 vand @T[0], @T[0], $twmask
2126 for($i=9;$i<16;$i++) {
2128 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2129 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2130 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2131 vshr.s64 @T[1], @XMM[$i], #63
2132 veor @XMM[$i], @XMM[$i], @T[0]
2133 vand @T[1], @T[1], $twmask
2137 $code.=<<___ if ($i>=10);
2138 vld1.8 {@XMM[$i-10]}, [$inp]!
2140 $code.=<<___ if ($i>=11);
2141 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2145 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
2146 vst1.64 {@XMM[15]}, [r0,:128]!
2147 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2148 veor @XMM[8], @XMM[8], @T[0]
2149 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2151 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
2152 veor @XMM[5], @XMM[5], @XMM[13]
2153 #ifndef BSAES_ASM_EXTENDED_KEY
2154 add r4, sp, #0x90 @ pass key schedule
2156 add r4, $key, #248 @ pass key schedule
2158 veor @XMM[6], @XMM[6], @XMM[14]
2159 mov r5, $rounds @ pass rounds
2160 veor @XMM[7], @XMM[7], @XMM[15]
2165 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2166 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2167 veor @XMM[0], @XMM[0], @XMM[ 8]
2168 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2169 veor @XMM[1], @XMM[1], @XMM[ 9]
2170 veor @XMM[8], @XMM[6], @XMM[10]
2171 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2172 veor @XMM[9], @XMM[4], @XMM[11]
2173 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
2174 veor @XMM[10], @XMM[2], @XMM[12]
2175 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2176 veor @XMM[11], @XMM[7], @XMM[13]
2177 veor @XMM[12], @XMM[3], @XMM[14]
2178 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2179 veor @XMM[13], @XMM[5], @XMM[15]
2180 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
2182 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2191 vldmia $magic, {$twmask} @ load XTS magic
2192 vshr.s64 @T[0], @XMM[8], #63
2194 vand @T[0], @T[0], $twmask
2196 for($i=9;$i<16;$i++) {
2198 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2199 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2200 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2201 vshr.s64 @T[1], @XMM[$i], #63
2202 veor @XMM[$i], @XMM[$i], @T[0]
2203 vand @T[1], @T[1], $twmask
2207 $code.=<<___ if ($i>=10);
2208 vld1.8 {@XMM[$i-10]}, [$inp]!
2210 bmi .Lxts_dec_`$i-9`
2212 $code.=<<___ if ($i>=11);
2213 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2218 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
2220 vld1.8 {@XMM[6]}, [$inp]!
2221 veor @XMM[5], @XMM[5], @XMM[13]
2222 #ifndef BSAES_ASM_EXTENDED_KEY
2223 add r4, sp, #0x90 @ pass key schedule
2225 add r4, $key, #248 @ pass key schedule
2227 veor @XMM[6], @XMM[6], @XMM[14]
2228 mov r5, $rounds @ pass rounds
2233 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2234 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2235 veor @XMM[0], @XMM[0], @XMM[ 8]
2236 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2237 veor @XMM[1], @XMM[1], @XMM[ 9]
2238 veor @XMM[8], @XMM[6], @XMM[10]
2239 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2240 veor @XMM[9], @XMM[4], @XMM[11]
2241 vld1.64 {@XMM[14]}, [r0,:128]!
2242 veor @XMM[10], @XMM[2], @XMM[12]
2243 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2244 veor @XMM[11], @XMM[7], @XMM[13]
2245 veor @XMM[12], @XMM[3], @XMM[14]
2246 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2247 vst1.8 {@XMM[12]}, [$out]!
2249 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2253 vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
2255 veor @XMM[4], @XMM[4], @XMM[12]
2256 #ifndef BSAES_ASM_EXTENDED_KEY
2257 add r4, sp, #0x90 @ pass key schedule
2259 add r4, $key, #248 @ pass key schedule
2261 veor @XMM[5], @XMM[5], @XMM[13]
2262 mov r5, $rounds @ pass rounds
2267 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2268 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2269 veor @XMM[0], @XMM[0], @XMM[ 8]
2270 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2271 veor @XMM[1], @XMM[1], @XMM[ 9]
2272 veor @XMM[8], @XMM[6], @XMM[10]
2273 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2274 veor @XMM[9], @XMM[4], @XMM[11]
2275 veor @XMM[10], @XMM[2], @XMM[12]
2276 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2277 veor @XMM[11], @XMM[7], @XMM[13]
2278 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2280 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2284 veor @XMM[3], @XMM[3], @XMM[11]
2285 #ifndef BSAES_ASM_EXTENDED_KEY
2286 add r4, sp, #0x90 @ pass key schedule
2288 add r4, $key, #248 @ pass key schedule
2290 veor @XMM[4], @XMM[4], @XMM[12]
2291 mov r5, $rounds @ pass rounds
2296 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2297 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2298 veor @XMM[0], @XMM[0], @XMM[ 8]
2299 vld1.64 {@XMM[12]}, [r0,:128]!
2300 veor @XMM[1], @XMM[1], @XMM[ 9]
2301 veor @XMM[8], @XMM[6], @XMM[10]
2302 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2303 veor @XMM[9], @XMM[4], @XMM[11]
2304 veor @XMM[10], @XMM[2], @XMM[12]
2305 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2306 vst1.8 {@XMM[10]}, [$out]!
2308 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2312 veor @XMM[2], @XMM[2], @XMM[10]
2313 #ifndef BSAES_ASM_EXTENDED_KEY
2314 add r4, sp, #0x90 @ pass key schedule
2316 add r4, $key, #248 @ pass key schedule
2318 veor @XMM[3], @XMM[3], @XMM[11]
2319 mov r5, $rounds @ pass rounds
2324 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2325 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2326 veor @XMM[0], @XMM[0], @XMM[ 8]
2327 veor @XMM[1], @XMM[1], @XMM[ 9]
2328 veor @XMM[8], @XMM[6], @XMM[10]
2329 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2330 veor @XMM[9], @XMM[4], @XMM[11]
2331 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2333 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2337 veor @XMM[1], @XMM[1], @XMM[9]
2338 #ifndef BSAES_ASM_EXTENDED_KEY
2339 add r4, sp, #0x90 @ pass key schedule
2341 add r4, $key, #248 @ pass key schedule
2343 veor @XMM[2], @XMM[2], @XMM[10]
2344 mov r5, $rounds @ pass rounds
2349 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2350 vld1.64 {@XMM[10]}, [r0,:128]!
2351 veor @XMM[0], @XMM[0], @XMM[ 8]
2352 veor @XMM[1], @XMM[1], @XMM[ 9]
2353 veor @XMM[8], @XMM[6], @XMM[10]
2354 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2355 vst1.8 {@XMM[8]}, [$out]!
2357 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2361 veor @XMM[0], @XMM[0], @XMM[8]
2362 #ifndef BSAES_ASM_EXTENDED_KEY
2363 add r4, sp, #0x90 @ pass key schedule
2365 add r4, $key, #248 @ pass key schedule
2367 veor @XMM[1], @XMM[1], @XMM[9]
2368 mov r5, $rounds @ pass rounds
2373 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2374 veor @XMM[0], @XMM[0], @XMM[ 8]
2375 veor @XMM[1], @XMM[1], @XMM[ 9]
2376 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2378 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2383 veor @XMM[0], @XMM[0], @XMM[8]
2385 vst1.8 {@XMM[0]}, [sp,:128]
2386 mov r5, $magic @ preserve magic
2388 mov r4, $fp @ preserve fp
2392 vld1.8 {@XMM[0]}, [sp,:128]
2393 veor @XMM[0], @XMM[0], @XMM[8]
2394 vst1.8 {@XMM[0]}, [$out]!
2398 vmov @XMM[8], @XMM[9] @ next round tweak
2401 #ifndef XTS_CHAIN_TWEAK
2405 @ calculate one round of extra tweak for the stolen ciphertext
2406 vldmia $magic, {$twmask}
2407 vshr.s64 @XMM[6], @XMM[8], #63
2408 vand @XMM[6], @XMM[6], $twmask
2409 vadd.u64 @XMM[9], @XMM[8], @XMM[8]
2410 vswp `&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
2411 veor @XMM[9], @XMM[9], @XMM[6]
2413 @ perform the final decryption with the last tweak value
2414 vld1.8 {@XMM[0]}, [$inp]!
2416 veor @XMM[0], @XMM[0], @XMM[9]
2418 vst1.8 {@XMM[0]}, [sp,:128]
2420 mov r4, $fp @ preserve fp
2424 vld1.8 {@XMM[0]}, [sp,:128]
2425 veor @XMM[0], @XMM[0], @XMM[9]
2426 vst1.8 {@XMM[0]}, [$out]
2432 strb r1, [$out, #0x10]
2438 vld1.8 {@XMM[0]}, [r6]
2440 veor @XMM[0], @XMM[8]
2442 vst1.8 {@XMM[0]}, [sp,:128]
2447 vld1.8 {@XMM[0]}, [sp,:128]
2448 veor @XMM[0], @XMM[0], @XMM[8]
2449 vst1.8 {@XMM[0]}, [r6]
2457 #ifdef XTS_CHAIN_TWEAK
2458 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2460 .Lxts_dec_bzero: @ wipe key schedule [if any]
2466 #ifdef XTS_CHAIN_TWEAK
2467 vst1.8 {@XMM[8]}, [r1]
2470 ldmia sp!, {r4-r10, pc} @ return
2472 .size bsaes_xts_decrypt,.-bsaes_xts_decrypt
2479 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
2484 last if (!s/^#/@/ and !/^$/);