2 # Copyright 2012-2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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
731 .syntax unified @ ARMv7-capable assembler is expected to handle this
732 #if defined(__thumb2__) && !defined(__APPLE__)
741 .type _bsaes_decrypt8,%function
745 vldmia $key!, {@XMM[9]} @ round 0 key
746 #if defined(__thumb2__) || defined(__APPLE__)
749 add $const,$const,#.LM0ISR-_bsaes_decrypt8
752 vldmia $const!, {@XMM[8]} @ .LM0ISR
753 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
754 veor @XMM[11], @XMM[1], @XMM[9]
755 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
756 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
757 veor @XMM[12], @XMM[2], @XMM[9]
758 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
759 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
760 veor @XMM[13], @XMM[3], @XMM[9]
761 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
762 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
763 veor @XMM[14], @XMM[4], @XMM[9]
764 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
765 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
766 veor @XMM[15], @XMM[5], @XMM[9]
767 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
768 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
769 veor @XMM[10], @XMM[6], @XMM[9]
770 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
771 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
772 veor @XMM[11], @XMM[7], @XMM[9]
773 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
774 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
775 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
776 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
778 &bitslice (@XMM[0..7, 8..11]);
780 sub $rounds,$rounds,#1
785 &ShiftRows (@XMM[0..7, 8..12]);
786 $code.=".Ldec_sbox:\n";
787 &InvSbox (@XMM[0..7, 8..15]);
789 subs $rounds,$rounds,#1
792 &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
794 vldmia $const, {@XMM[12]} @ .LISR
795 ite eq @ Thumb2 thing, sanity check in ARM
796 addeq $const,$const,#0x10
798 vldmia $const, {@XMM[12]} @ .LISRM0
803 &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
805 vldmia $key, {@XMM[8]} @ last round key
806 veor @XMM[6], @XMM[6], @XMM[8]
807 veor @XMM[4], @XMM[4], @XMM[8]
808 veor @XMM[2], @XMM[2], @XMM[8]
809 veor @XMM[7], @XMM[7], @XMM[8]
810 veor @XMM[3], @XMM[3], @XMM[8]
811 veor @XMM[5], @XMM[5], @XMM[8]
812 veor @XMM[0], @XMM[0], @XMM[8]
813 veor @XMM[1], @XMM[1], @XMM[8]
815 .size _bsaes_decrypt8,.-_bsaes_decrypt8
817 .type _bsaes_const,%object
820 .LM0ISR: @ InvShiftRows constants
821 .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
823 .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
825 .quad 0x01040b0e0205080f, 0x0306090c00070a0d
826 .LM0SR: @ ShiftRows constants
827 .quad 0x0a0e02060f03070b, 0x0004080c05090d01
829 .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
831 .quad 0x0304090e00050a0f, 0x01060b0c0207080d
833 .quad 0x02060a0e03070b0f, 0x0004080c0105090d
835 .quad 0x090d01050c000408, 0x03070b0f060a0e02
836 .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
838 .size _bsaes_const,.-_bsaes_const
840 .type _bsaes_encrypt8,%function
844 vldmia $key!, {@XMM[9]} @ round 0 key
845 #if defined(__thumb2__) || defined(__APPLE__)
848 sub $const,$const,#_bsaes_encrypt8-.LM0SR
851 vldmia $const!, {@XMM[8]} @ .LM0SR
853 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
854 veor @XMM[11], @XMM[1], @XMM[9]
855 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
856 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
857 veor @XMM[12], @XMM[2], @XMM[9]
858 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
859 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
860 veor @XMM[13], @XMM[3], @XMM[9]
861 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
862 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
863 veor @XMM[14], @XMM[4], @XMM[9]
864 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
865 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
866 veor @XMM[15], @XMM[5], @XMM[9]
867 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
868 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
869 veor @XMM[10], @XMM[6], @XMM[9]
870 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
871 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
872 veor @XMM[11], @XMM[7], @XMM[9]
873 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
874 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
875 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
876 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
877 _bsaes_encrypt8_bitslice:
879 &bitslice (@XMM[0..7, 8..11]);
881 sub $rounds,$rounds,#1
886 &ShiftRows (@XMM[0..7, 8..12]);
887 $code.=".Lenc_sbox:\n";
888 &Sbox (@XMM[0..7, 8..15]);
890 subs $rounds,$rounds,#1
893 &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
895 vldmia $const, {@XMM[12]} @ .LSR
896 ite eq @ Thumb2 thing, samity check in ARM
897 addeq $const,$const,#0x10
899 vldmia $const, {@XMM[12]} @ .LSRM0
904 # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
905 &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
907 vldmia $key, {@XMM[8]} @ last round key
908 veor @XMM[4], @XMM[4], @XMM[8]
909 veor @XMM[6], @XMM[6], @XMM[8]
910 veor @XMM[3], @XMM[3], @XMM[8]
911 veor @XMM[7], @XMM[7], @XMM[8]
912 veor @XMM[2], @XMM[2], @XMM[8]
913 veor @XMM[5], @XMM[5], @XMM[8]
914 veor @XMM[0], @XMM[0], @XMM[8]
915 veor @XMM[1], @XMM[1], @XMM[8]
917 .size _bsaes_encrypt8,.-_bsaes_encrypt8
921 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
924 my @x=reverse(@_[0..7]);
925 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
927 &swapmove (@x[0,1],1,$bs0,$t2,$t3);
929 @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
933 #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
935 &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
937 @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
943 &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
944 &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
948 .type _bsaes_key_convert,%function
952 vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
953 #if defined(__thumb2__) || defined(__APPLE__)
956 sub $const,$const,#_bsaes_key_convert-.LM0
958 vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
960 vmov.i8 @XMM[8], #0x01 @ bit masks
961 vmov.i8 @XMM[9], #0x02
962 vmov.i8 @XMM[10], #0x04
963 vmov.i8 @XMM[11], #0x08
964 vmov.i8 @XMM[12], #0x10
965 vmov.i8 @XMM[13], #0x20
966 vldmia $const, {@XMM[14]} @ .LM0
969 vrev32.8 @XMM[7], @XMM[7]
970 vrev32.8 @XMM[15], @XMM[15]
972 sub $rounds,$rounds,#1
973 vstmia $out!, {@XMM[7]} @ save round 0 key
978 vtbl.8 `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
979 vtbl.8 `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
980 vmov.i8 @XMM[6], #0x40
981 vmov.i8 @XMM[15], #0x80
983 vtst.8 @XMM[0], @XMM[7], @XMM[8]
984 vtst.8 @XMM[1], @XMM[7], @XMM[9]
985 vtst.8 @XMM[2], @XMM[7], @XMM[10]
986 vtst.8 @XMM[3], @XMM[7], @XMM[11]
987 vtst.8 @XMM[4], @XMM[7], @XMM[12]
988 vtst.8 @XMM[5], @XMM[7], @XMM[13]
989 vtst.8 @XMM[6], @XMM[7], @XMM[6]
990 vtst.8 @XMM[7], @XMM[7], @XMM[15]
991 vld1.8 {@XMM[15]}, [$inp]! @ load next round key
992 vmvn @XMM[0], @XMM[0] @ "pnot"
993 vmvn @XMM[1], @XMM[1]
994 vmvn @XMM[5], @XMM[5]
995 vmvn @XMM[6], @XMM[6]
997 vrev32.8 @XMM[15], @XMM[15]
999 subs $rounds,$rounds,#1
1000 vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
1003 vmov.i8 @XMM[7],#0x63 @ compose .L63
1004 @ don't save last round key
1006 .size _bsaes_key_convert,.-_bsaes_key_convert
1010 if (0) { # following four functions are unsupported interface
1011 # used for benchmarking...
1013 .globl bsaes_enc_key_convert
1014 .type bsaes_enc_key_convert,%function
1016 bsaes_enc_key_convert:
1017 stmdb sp!,{r4-r6,lr}
1018 vstmdb sp!,{d8-d15} @ ABI specification says so
1020 ldr r5,[$inp,#240] @ pass rounds
1021 mov r4,$inp @ pass key
1022 mov r12,$out @ pass key schedule
1023 bl _bsaes_key_convert
1024 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1025 vstmia r12, {@XMM[7]} @ save last round key
1028 ldmia sp!,{r4-r6,pc}
1029 .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
1031 .globl bsaes_encrypt_128
1032 .type bsaes_encrypt_128,%function
1035 stmdb sp!,{r4-r6,lr}
1036 vstmdb sp!,{d8-d15} @ ABI specification says so
1038 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1039 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1040 mov r4,$key @ pass the key
1041 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1042 mov r5,#10 @ pass rounds
1043 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1047 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1048 vst1.8 {@XMM[4]}, [$out]!
1049 vst1.8 {@XMM[6]}, [$out]!
1050 vst1.8 {@XMM[3]}, [$out]!
1051 vst1.8 {@XMM[7]}, [$out]!
1052 vst1.8 {@XMM[2]}, [$out]!
1053 subs $len,$len,#0x80
1054 vst1.8 {@XMM[5]}, [$out]!
1058 ldmia sp!,{r4-r6,pc}
1059 .size bsaes_encrypt_128,.-bsaes_encrypt_128
1061 .globl bsaes_dec_key_convert
1062 .type bsaes_dec_key_convert,%function
1064 bsaes_dec_key_convert:
1065 stmdb sp!,{r4-r6,lr}
1066 vstmdb sp!,{d8-d15} @ ABI specification says so
1068 ldr r5,[$inp,#240] @ pass rounds
1069 mov r4,$inp @ pass key
1070 mov r12,$out @ pass key schedule
1071 bl _bsaes_key_convert
1072 vldmia $out, {@XMM[6]}
1073 vstmia r12, {@XMM[15]} @ save last round key
1074 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1075 vstmia $out, {@XMM[7]}
1078 ldmia sp!,{r4-r6,pc}
1079 .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
1081 .globl bsaes_decrypt_128
1082 .type bsaes_decrypt_128,%function
1085 stmdb sp!,{r4-r6,lr}
1086 vstmdb sp!,{d8-d15} @ ABI specification says so
1088 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1089 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1090 mov r4,$key @ pass the key
1091 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1092 mov r5,#10 @ pass rounds
1093 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1097 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1098 vst1.8 {@XMM[6]}, [$out]!
1099 vst1.8 {@XMM[4]}, [$out]!
1100 vst1.8 {@XMM[2]}, [$out]!
1101 vst1.8 {@XMM[7]}, [$out]!
1102 vst1.8 {@XMM[3]}, [$out]!
1103 subs $len,$len,#0x80
1104 vst1.8 {@XMM[5]}, [$out]!
1108 ldmia sp!,{r4-r6,pc}
1109 .size bsaes_decrypt_128,.-bsaes_decrypt_128
1113 my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
1114 my ($keysched)=("sp");
1117 .extern AES_cbc_encrypt
1120 .global bsaes_cbc_encrypt
1121 .type bsaes_cbc_encrypt,%function
1135 @ it is up to the caller to make sure we are called with enc == 0
1138 stmdb sp!, {r4-r10, lr}
1140 ldr $ivp, [ip] @ IV is 1st arg on the stack
1141 mov $len, $len, lsr#4 @ len in 16 byte blocks
1142 sub sp, #0x10 @ scratch space to carry over the IV
1143 mov $fp, sp @ save sp
1145 ldr $rounds, [$key, #240] @ get # of rounds
1146 #ifndef BSAES_ASM_EXTENDED_KEY
1147 @ allocate the key schedule on the stack
1148 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1149 add r12, #`128-32` @ sifze of bit-slices key schedule
1151 @ populate the key schedule
1152 mov r4, $key @ pass key
1153 mov r5, $rounds @ pass # of rounds
1154 mov sp, r12 @ sp is $keysched
1155 bl _bsaes_key_convert
1156 vldmia $keysched, {@XMM[6]}
1157 vstmia r12, {@XMM[15]} @ save last round key
1158 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1159 vstmia $keysched, {@XMM[7]}
1161 ldr r12, [$key, #244]
1165 @ populate the key schedule
1166 str r12, [$key, #244]
1167 mov r4, $key @ pass key
1168 mov r5, $rounds @ pass # of rounds
1169 add r12, $key, #248 @ pass key schedule
1170 bl _bsaes_key_convert
1172 vldmia r4, {@XMM[6]}
1173 vstmia r12, {@XMM[15]} @ save last round key
1174 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1175 vstmia r4, {@XMM[7]}
1181 vld1.8 {@XMM[15]}, [$ivp] @ load IV
1186 subs $len, $len, #0x8
1187 bmi .Lcbc_dec_loop_finish
1189 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1190 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1191 #ifndef BSAES_ASM_EXTENDED_KEY
1192 mov r4, $keysched @ pass the key
1196 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1198 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]
1199 sub $inp, $inp, #0x60
1200 vstmia $fp, {@XMM[15]} @ put aside IV
1204 vldmia $fp, {@XMM[14]} @ reload IV
1205 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1206 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1207 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1208 veor @XMM[1], @XMM[1], @XMM[8]
1209 veor @XMM[6], @XMM[6], @XMM[9]
1210 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1211 veor @XMM[4], @XMM[4], @XMM[10]
1212 veor @XMM[2], @XMM[2], @XMM[11]
1213 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1214 veor @XMM[7], @XMM[7], @XMM[12]
1215 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1216 veor @XMM[3], @XMM[3], @XMM[13]
1217 vst1.8 {@XMM[6]}, [$out]!
1218 veor @XMM[5], @XMM[5], @XMM[14]
1219 vst1.8 {@XMM[4]}, [$out]!
1220 vst1.8 {@XMM[2]}, [$out]!
1221 vst1.8 {@XMM[7]}, [$out]!
1222 vst1.8 {@XMM[3]}, [$out]!
1223 vst1.8 {@XMM[5]}, [$out]!
1227 .Lcbc_dec_loop_finish:
1231 vld1.8 {@XMM[0]}, [$inp]! @ load input
1234 vld1.8 {@XMM[1]}, [$inp]!
1235 #ifndef BSAES_ASM_EXTENDED_KEY
1236 mov r4, $keysched @ pass the key
1241 vstmia $fp, {@XMM[15]} @ put aside IV
1243 vld1.8 {@XMM[2]}, [$inp]!
1246 vld1.8 {@XMM[3]}, [$inp]!
1248 vld1.8 {@XMM[4]}, [$inp]!
1251 vld1.8 {@XMM[5]}, [$inp]!
1253 vld1.8 {@XMM[6]}, [$inp]!
1254 sub $inp, $inp, #0x70
1258 vldmia $fp, {@XMM[14]} @ reload IV
1259 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1260 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1261 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1262 veor @XMM[1], @XMM[1], @XMM[8]
1263 veor @XMM[6], @XMM[6], @XMM[9]
1264 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1265 veor @XMM[4], @XMM[4], @XMM[10]
1266 veor @XMM[2], @XMM[2], @XMM[11]
1267 vld1.8 {@XMM[15]}, [$inp]!
1268 veor @XMM[7], @XMM[7], @XMM[12]
1269 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1270 veor @XMM[3], @XMM[3], @XMM[13]
1271 vst1.8 {@XMM[6]}, [$out]!
1272 vst1.8 {@XMM[4]}, [$out]!
1273 vst1.8 {@XMM[2]}, [$out]!
1274 vst1.8 {@XMM[7]}, [$out]!
1275 vst1.8 {@XMM[3]}, [$out]!
1279 sub $inp, $inp, #0x60
1281 vldmia $fp,{@XMM[14]} @ reload IV
1282 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1283 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1284 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1285 veor @XMM[1], @XMM[1], @XMM[8]
1286 veor @XMM[6], @XMM[6], @XMM[9]
1287 vld1.8 {@XMM[12]}, [$inp]!
1288 veor @XMM[4], @XMM[4], @XMM[10]
1289 veor @XMM[2], @XMM[2], @XMM[11]
1290 vld1.8 {@XMM[15]}, [$inp]!
1291 veor @XMM[7], @XMM[7], @XMM[12]
1292 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1293 vst1.8 {@XMM[6]}, [$out]!
1294 vst1.8 {@XMM[4]}, [$out]!
1295 vst1.8 {@XMM[2]}, [$out]!
1296 vst1.8 {@XMM[7]}, [$out]!
1300 sub $inp, $inp, #0x50
1302 vldmia $fp, {@XMM[14]} @ reload IV
1303 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1304 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1305 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1306 veor @XMM[1], @XMM[1], @XMM[8]
1307 veor @XMM[6], @XMM[6], @XMM[9]
1308 vld1.8 {@XMM[15]}, [$inp]!
1309 veor @XMM[4], @XMM[4], @XMM[10]
1310 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1311 veor @XMM[2], @XMM[2], @XMM[11]
1312 vst1.8 {@XMM[6]}, [$out]!
1313 vst1.8 {@XMM[4]}, [$out]!
1314 vst1.8 {@XMM[2]}, [$out]!
1318 sub $inp, $inp, #0x40
1320 vldmia $fp, {@XMM[14]} @ reload IV
1321 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1322 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1323 vld1.8 {@XMM[10]}, [$inp]!
1324 veor @XMM[1], @XMM[1], @XMM[8]
1325 veor @XMM[6], @XMM[6], @XMM[9]
1326 vld1.8 {@XMM[15]}, [$inp]!
1327 veor @XMM[4], @XMM[4], @XMM[10]
1328 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1329 vst1.8 {@XMM[6]}, [$out]!
1330 vst1.8 {@XMM[4]}, [$out]!
1334 sub $inp, $inp, #0x30
1336 vldmia $fp, {@XMM[14]} @ reload IV
1337 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1338 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1339 vld1.8 {@XMM[15]}, [$inp]!
1340 veor @XMM[1], @XMM[1], @XMM[8]
1341 veor @XMM[6], @XMM[6], @XMM[9]
1342 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1343 vst1.8 {@XMM[6]}, [$out]!
1347 sub $inp, $inp, #0x20
1349 vldmia $fp, {@XMM[14]} @ reload IV
1350 vld1.8 {@XMM[8]}, [$inp]! @ reload input
1351 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1352 vld1.8 {@XMM[15]}, [$inp]! @ reload input
1353 veor @XMM[1], @XMM[1], @XMM[8]
1354 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1358 sub $inp, $inp, #0x10
1359 mov $rounds, $out @ save original out pointer
1360 mov $out, $fp @ use the iv scratch space as out buffer
1362 vmov @XMM[4],@XMM[15] @ just in case ensure that IV
1363 vmov @XMM[5],@XMM[0] @ and input are preserved
1365 vld1.8 {@XMM[0]}, [$fp] @ load result
1366 veor @XMM[0], @XMM[0], @XMM[4] @ ^= IV
1367 vmov @XMM[15], @XMM[5] @ @XMM[5] holds input
1368 vst1.8 {@XMM[0]}, [$rounds] @ write output
1371 #ifndef BSAES_ASM_EXTENDED_KEY
1374 .Lcbc_dec_bzero: @ wipe key schedule [if any]
1375 vstmia $keysched!, {q0-q1}
1381 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1382 vst1.8 {@XMM[15]}, [$ivp] @ return IV
1384 ldmia sp!, {r4-r10, pc}
1385 .size bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
1389 my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1390 my $const = "r6"; # shared with _bsaes_encrypt8_alt
1391 my $keysched = "sp";
1395 .global bsaes_ctr32_encrypt_blocks
1396 .type bsaes_ctr32_encrypt_blocks,%function
1398 bsaes_ctr32_encrypt_blocks:
1399 cmp $len, #8 @ use plain AES for
1400 blo .Lctr_enc_short @ small sizes
1403 stmdb sp!, {r4-r10, lr}
1405 ldr $ctr, [ip] @ ctr is 1st arg on the stack
1406 sub sp, sp, #0x10 @ scratch space to carry over the ctr
1407 mov $fp, sp @ save sp
1409 ldr $rounds, [$key, #240] @ get # of rounds
1410 #ifndef BSAES_ASM_EXTENDED_KEY
1411 @ allocate the key schedule on the stack
1412 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1413 add r12, #`128-32` @ size of bit-sliced key schedule
1415 @ populate the key schedule
1416 mov r4, $key @ pass key
1417 mov r5, $rounds @ pass # of rounds
1418 mov sp, r12 @ sp is $keysched
1419 bl _bsaes_key_convert
1420 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1421 vstmia r12, {@XMM[7]} @ save last round key
1423 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1425 mov $ctr, #:lower16:(.LREVM0SR-.LM0)
1426 add $ctr, $const, $ctr
1428 add $ctr, $const, #.LREVM0SR-.LM0 @ borrow $ctr
1430 vldmia $keysched, {@XMM[4]} @ load round0 key
1432 ldr r12, [$key, #244]
1436 @ populate the key schedule
1437 str r12, [$key, #244]
1438 mov r4, $key @ pass key
1439 mov r5, $rounds @ pass # of rounds
1440 add r12, $key, #248 @ pass key schedule
1441 bl _bsaes_key_convert
1442 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1443 vstmia r12, {@XMM[7]} @ save last round key
1446 0: add r12, $key, #248
1447 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1448 adrl $ctr, .LREVM0SR @ borrow $ctr
1449 vldmia r12, {@XMM[4]} @ load round0 key
1450 sub sp, #0x10 @ place for adjusted round0 key
1453 vmov.i32 @XMM[8],#1 @ compose 1<<96
1454 veor @XMM[9],@XMM[9],@XMM[9]
1455 vrev32.8 @XMM[0],@XMM[0]
1456 vext.8 @XMM[8],@XMM[9],@XMM[8],#4
1457 vrev32.8 @XMM[4],@XMM[4]
1458 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1459 vstmia $keysched, {@XMM[4]} @ save adjusted round0 key
1464 vadd.u32 @XMM[10], @XMM[8], @XMM[9] @ compose 3<<96
1465 vadd.u32 @XMM[1], @XMM[0], @XMM[8] @ +1
1466 vadd.u32 @XMM[2], @XMM[0], @XMM[9] @ +2
1467 vadd.u32 @XMM[3], @XMM[0], @XMM[10] @ +3
1468 vadd.u32 @XMM[4], @XMM[1], @XMM[10]
1469 vadd.u32 @XMM[5], @XMM[2], @XMM[10]
1470 vadd.u32 @XMM[6], @XMM[3], @XMM[10]
1471 vadd.u32 @XMM[7], @XMM[4], @XMM[10]
1472 vadd.u32 @XMM[10], @XMM[5], @XMM[10] @ next counter
1474 @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1475 @ to flip byte order in 32-bit counter
1477 vldmia $keysched, {@XMM[9]} @ load round0 key
1478 #ifndef BSAES_ASM_EXTENDED_KEY
1479 add r4, $keysched, #0x10 @ pass next round key
1481 add r4, $key, #`248+16`
1483 vldmia $ctr, {@XMM[8]} @ .LREVM0SR
1484 mov r5, $rounds @ pass rounds
1485 vstmia $fp, {@XMM[10]} @ save next counter
1487 mov $const, #:lower16:(.LREVM0SR-.LSR)
1488 sub $const, $ctr, $const
1490 sub $const, $ctr, #.LREVM0SR-.LSR @ pass constants
1493 bl _bsaes_encrypt8_alt
1496 blo .Lctr_enc_loop_done
1498 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ load input
1499 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1500 veor @XMM[0], @XMM[8]
1501 veor @XMM[1], @XMM[9]
1502 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1503 veor @XMM[4], @XMM[10]
1504 veor @XMM[6], @XMM[11]
1505 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1506 veor @XMM[3], @XMM[12]
1507 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1508 veor @XMM[7], @XMM[13]
1509 veor @XMM[2], @XMM[14]
1510 vst1.8 {@XMM[4]}, [$out]!
1511 veor @XMM[5], @XMM[15]
1512 vst1.8 {@XMM[6]}, [$out]!
1513 vmov.i32 @XMM[8], #1 @ compose 1<<96
1514 vst1.8 {@XMM[3]}, [$out]!
1515 veor @XMM[9], @XMM[9], @XMM[9]
1516 vst1.8 {@XMM[7]}, [$out]!
1517 vext.8 @XMM[8], @XMM[9], @XMM[8], #4
1518 vst1.8 {@XMM[2]}, [$out]!
1519 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1520 vst1.8 {@XMM[5]}, [$out]!
1521 vldmia $fp, {@XMM[0]} @ load counter
1527 .Lctr_enc_loop_done:
1529 vld1.8 {@XMM[8]}, [$inp]! @ load input
1530 veor @XMM[0], @XMM[8]
1531 vst1.8 {@XMM[0]}, [$out]! @ write output
1534 vld1.8 {@XMM[9]}, [$inp]!
1535 veor @XMM[1], @XMM[9]
1536 vst1.8 {@XMM[1]}, [$out]!
1538 vld1.8 {@XMM[10]}, [$inp]!
1539 veor @XMM[4], @XMM[10]
1540 vst1.8 {@XMM[4]}, [$out]!
1543 vld1.8 {@XMM[11]}, [$inp]!
1544 veor @XMM[6], @XMM[11]
1545 vst1.8 {@XMM[6]}, [$out]!
1547 vld1.8 {@XMM[12]}, [$inp]!
1548 veor @XMM[3], @XMM[12]
1549 vst1.8 {@XMM[3]}, [$out]!
1552 vld1.8 {@XMM[13]}, [$inp]!
1553 veor @XMM[7], @XMM[13]
1554 vst1.8 {@XMM[7]}, [$out]!
1556 vld1.8 {@XMM[14]}, [$inp]
1557 veor @XMM[2], @XMM[14]
1558 vst1.8 {@XMM[2]}, [$out]!
1563 #ifndef BSAES_ASM_EXTENDED_KEY
1564 .Lctr_enc_bzero: @ wipe key schedule [if any]
1565 vstmia $keysched!, {q0-q1}
1569 vstmia $keysched, {q0-q1}
1573 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1575 ldmia sp!, {r4-r10, pc} @ return
1579 ldr ip, [sp] @ ctr pointer is passed on stack
1580 stmdb sp!, {r4-r8, lr}
1582 mov r4, $inp @ copy arguments
1586 ldr r8, [ip, #12] @ load counter LSW
1587 vld1.8 {@XMM[1]}, [ip] @ load whole counter value
1592 vst1.8 {@XMM[1]}, [sp] @ copy counter value
1595 .Lctr_enc_short_loop:
1596 add r0, sp, #0x10 @ input counter value
1597 mov r1, sp @ output on the stack
1602 vld1.8 {@XMM[0]}, [r4]! @ load input
1603 vld1.8 {@XMM[1]}, [sp] @ load encrypted counter
1607 str r0, [sp, #0x1c] @ next counter value
1609 str r8, [sp, #0x1c] @ next counter value
1611 veor @XMM[0],@XMM[0],@XMM[1]
1612 vst1.8 {@XMM[0]}, [r5]! @ store output
1614 bne .Lctr_enc_short_loop
1620 ldmia sp!, {r4-r8, pc}
1621 .size bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
1625 ######################################################################
1626 # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1627 # const AES_KEY *key1, const AES_KEY *key2,
1628 # const unsigned char iv[16]);
1630 my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
1631 my $const="r6"; # returned by _bsaes_key_convert
1636 .globl bsaes_xts_encrypt
1637 .type bsaes_xts_encrypt,%function
1641 stmdb sp!, {r4-r10, lr} @ 0x20
1643 mov r6, sp @ future $fp
1650 sub r0, sp, #0x10 @ 0x10
1651 bic r0, #0xf @ align at 16 bytes
1654 #ifdef XTS_CHAIN_TWEAK
1655 ldr r0, [ip] @ pointer to input tweak
1657 @ generate initial tweak
1658 ldr r0, [ip, #4] @ iv[]
1660 ldr r2, [ip, #0] @ key2
1662 mov r0,sp @ pointer to initial tweak
1665 ldr $rounds, [$key, #240] @ get # of rounds
1667 #ifndef BSAES_ASM_EXTENDED_KEY
1668 @ allocate the key schedule on the stack
1669 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1670 @ add r12, #`128-32` @ size of bit-sliced key schedule
1671 sub r12, #`32+16` @ place for tweak[9]
1673 @ populate the key schedule
1674 mov r4, $key @ pass key
1675 mov r5, $rounds @ pass # of rounds
1677 add r12, #0x90 @ pass key schedule
1678 bl _bsaes_key_convert
1679 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1680 vstmia r12, {@XMM[7]} @ save last round key
1682 ldr r12, [$key, #244]
1686 str r12, [$key, #244]
1687 mov r4, $key @ pass key
1688 mov r5, $rounds @ pass # of rounds
1689 add r12, $key, #248 @ pass key schedule
1690 bl _bsaes_key_convert
1691 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1692 vstmia r12, {@XMM[7]}
1695 0: sub sp, #0x90 @ place for tweak[9]
1698 vld1.8 {@XMM[8]}, [r0] @ initial tweak
1699 adr $magic, .Lxts_magic
1707 vldmia $magic, {$twmask} @ load XTS magic
1708 vshr.s64 @T[0], @XMM[8], #63
1710 vand @T[0], @T[0], $twmask
1712 for($i=9;$i<16;$i++) {
1714 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1715 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1716 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1717 vshr.s64 @T[1], @XMM[$i], #63
1718 veor @XMM[$i], @XMM[$i], @T[0]
1719 vand @T[1], @T[1], $twmask
1723 $code.=<<___ if ($i>=10);
1724 vld1.8 {@XMM[$i-10]}, [$inp]!
1726 $code.=<<___ if ($i>=11);
1727 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1731 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
1732 vst1.64 {@XMM[15]}, [r0,:128]!
1733 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1734 veor @XMM[8], @XMM[8], @T[0]
1735 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1737 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1738 veor @XMM[5], @XMM[5], @XMM[13]
1739 #ifndef BSAES_ASM_EXTENDED_KEY
1740 add r4, sp, #0x90 @ pass key schedule
1742 add r4, $key, #248 @ pass key schedule
1744 veor @XMM[6], @XMM[6], @XMM[14]
1745 mov r5, $rounds @ pass rounds
1746 veor @XMM[7], @XMM[7], @XMM[15]
1751 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1752 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1753 veor @XMM[0], @XMM[0], @XMM[ 8]
1754 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1755 veor @XMM[1], @XMM[1], @XMM[ 9]
1756 veor @XMM[8], @XMM[4], @XMM[10]
1757 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1758 veor @XMM[9], @XMM[6], @XMM[11]
1759 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
1760 veor @XMM[10], @XMM[3], @XMM[12]
1761 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1762 veor @XMM[11], @XMM[7], @XMM[13]
1763 veor @XMM[12], @XMM[2], @XMM[14]
1764 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1765 veor @XMM[13], @XMM[5], @XMM[15]
1766 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
1768 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1777 vldmia $magic, {$twmask} @ load XTS magic
1778 vshr.s64 @T[0], @XMM[8], #63
1780 vand @T[0], @T[0], $twmask
1782 for($i=9;$i<16;$i++) {
1784 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1785 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1786 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1787 vshr.s64 @T[1], @XMM[$i], #63
1788 veor @XMM[$i], @XMM[$i], @T[0]
1789 vand @T[1], @T[1], $twmask
1793 $code.=<<___ if ($i>=10);
1794 vld1.8 {@XMM[$i-10]}, [$inp]!
1796 bmi .Lxts_enc_`$i-9`
1798 $code.=<<___ if ($i>=11);
1799 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1804 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
1806 vld1.8 {@XMM[6]}, [$inp]!
1807 veor @XMM[5], @XMM[5], @XMM[13]
1808 #ifndef BSAES_ASM_EXTENDED_KEY
1809 add r4, sp, #0x90 @ pass key schedule
1811 add r4, $key, #248 @ pass key schedule
1813 veor @XMM[6], @XMM[6], @XMM[14]
1814 mov r5, $rounds @ pass rounds
1819 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1820 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1821 veor @XMM[0], @XMM[0], @XMM[ 8]
1822 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1823 veor @XMM[1], @XMM[1], @XMM[ 9]
1824 veor @XMM[8], @XMM[4], @XMM[10]
1825 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1826 veor @XMM[9], @XMM[6], @XMM[11]
1827 vld1.64 {@XMM[14]}, [r0,:128]!
1828 veor @XMM[10], @XMM[3], @XMM[12]
1829 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1830 veor @XMM[11], @XMM[7], @XMM[13]
1831 veor @XMM[12], @XMM[2], @XMM[14]
1832 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1833 vst1.8 {@XMM[12]}, [$out]!
1835 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1839 veor @XMM[4], @XMM[4], @XMM[12]
1840 #ifndef BSAES_ASM_EXTENDED_KEY
1841 add r4, sp, #0x90 @ pass key schedule
1843 add r4, $key, #248 @ pass key schedule
1845 veor @XMM[5], @XMM[5], @XMM[13]
1846 mov r5, $rounds @ pass rounds
1851 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1852 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1853 veor @XMM[0], @XMM[0], @XMM[ 8]
1854 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1855 veor @XMM[1], @XMM[1], @XMM[ 9]
1856 veor @XMM[8], @XMM[4], @XMM[10]
1857 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1858 veor @XMM[9], @XMM[6], @XMM[11]
1859 veor @XMM[10], @XMM[3], @XMM[12]
1860 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1861 veor @XMM[11], @XMM[7], @XMM[13]
1862 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1864 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1867 @ put this in range for both ARM and Thumb mode adr instructions
1874 veor @XMM[3], @XMM[3], @XMM[11]
1875 #ifndef BSAES_ASM_EXTENDED_KEY
1876 add r4, sp, #0x90 @ pass key schedule
1878 add r4, $key, #248 @ pass key schedule
1880 veor @XMM[4], @XMM[4], @XMM[12]
1881 mov r5, $rounds @ pass rounds
1886 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1887 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1888 veor @XMM[0], @XMM[0], @XMM[ 8]
1889 vld1.64 {@XMM[12]}, [r0,:128]!
1890 veor @XMM[1], @XMM[1], @XMM[ 9]
1891 veor @XMM[8], @XMM[4], @XMM[10]
1892 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1893 veor @XMM[9], @XMM[6], @XMM[11]
1894 veor @XMM[10], @XMM[3], @XMM[12]
1895 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1896 vst1.8 {@XMM[10]}, [$out]!
1898 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1902 veor @XMM[2], @XMM[2], @XMM[10]
1903 #ifndef BSAES_ASM_EXTENDED_KEY
1904 add r4, sp, #0x90 @ pass key schedule
1906 add r4, $key, #248 @ pass key schedule
1908 veor @XMM[3], @XMM[3], @XMM[11]
1909 mov r5, $rounds @ pass rounds
1914 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1915 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1916 veor @XMM[0], @XMM[0], @XMM[ 8]
1917 veor @XMM[1], @XMM[1], @XMM[ 9]
1918 veor @XMM[8], @XMM[4], @XMM[10]
1919 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1920 veor @XMM[9], @XMM[6], @XMM[11]
1921 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1923 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1927 veor @XMM[1], @XMM[1], @XMM[9]
1928 #ifndef BSAES_ASM_EXTENDED_KEY
1929 add r4, sp, #0x90 @ pass key schedule
1931 add r4, $key, #248 @ pass key schedule
1933 veor @XMM[2], @XMM[2], @XMM[10]
1934 mov r5, $rounds @ pass rounds
1939 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1940 vld1.64 {@XMM[10]}, [r0,:128]!
1941 veor @XMM[0], @XMM[0], @XMM[ 8]
1942 veor @XMM[1], @XMM[1], @XMM[ 9]
1943 veor @XMM[8], @XMM[4], @XMM[10]
1944 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1945 vst1.8 {@XMM[8]}, [$out]!
1947 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1951 veor @XMM[0], @XMM[0], @XMM[8]
1952 #ifndef BSAES_ASM_EXTENDED_KEY
1953 add r4, sp, #0x90 @ pass key schedule
1955 add r4, $key, #248 @ pass key schedule
1957 veor @XMM[1], @XMM[1], @XMM[9]
1958 mov r5, $rounds @ pass rounds
1963 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1964 veor @XMM[0], @XMM[0], @XMM[ 8]
1965 veor @XMM[1], @XMM[1], @XMM[ 9]
1966 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1968 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1973 veor @XMM[0], @XMM[0], @XMM[8]
1975 vst1.8 {@XMM[0]}, [sp,:128]
1977 mov r4, $fp @ preserve fp
1981 vld1.8 {@XMM[0]}, [sp,:128]
1982 veor @XMM[0], @XMM[0], @XMM[8]
1983 vst1.8 {@XMM[0]}, [$out]!
1986 vmov @XMM[8], @XMM[9] @ next round tweak
1989 #ifndef XTS_CHAIN_TWEAK
1996 ldrb r1, [$out, #-0x10]
1997 strb r0, [$out, #-0x10]
2003 vld1.8 {@XMM[0]}, [r6]
2005 veor @XMM[0], @XMM[0], @XMM[8]
2007 vst1.8 {@XMM[0]}, [sp,:128]
2009 mov r4, $fp @ preserve fp
2013 vld1.8 {@XMM[0]}, [sp,:128]
2014 veor @XMM[0], @XMM[0], @XMM[8]
2015 vst1.8 {@XMM[0]}, [r6]
2023 #ifdef XTS_CHAIN_TWEAK
2024 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2026 .Lxts_enc_bzero: @ wipe key schedule [if any]
2032 #ifdef XTS_CHAIN_TWEAK
2033 vst1.8 {@XMM[8]}, [r1]
2036 ldmia sp!, {r4-r10, pc} @ return
2038 .size bsaes_xts_encrypt,.-bsaes_xts_encrypt
2040 .globl bsaes_xts_decrypt
2041 .type bsaes_xts_decrypt,%function
2045 stmdb sp!, {r4-r10, lr} @ 0x20
2047 mov r6, sp @ future $fp
2054 sub r0, sp, #0x10 @ 0x10
2055 bic r0, #0xf @ align at 16 bytes
2058 #ifdef XTS_CHAIN_TWEAK
2059 ldr r0, [ip] @ pointer to input tweak
2061 @ generate initial tweak
2062 ldr r0, [ip, #4] @ iv[]
2064 ldr r2, [ip, #0] @ key2
2066 mov r0, sp @ pointer to initial tweak
2069 ldr $rounds, [$key, #240] @ get # of rounds
2071 #ifndef BSAES_ASM_EXTENDED_KEY
2072 @ allocate the key schedule on the stack
2073 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
2074 @ add r12, #`128-32` @ size of bit-sliced key schedule
2075 sub r12, #`32+16` @ place for tweak[9]
2077 @ populate the key schedule
2078 mov r4, $key @ pass key
2079 mov r5, $rounds @ pass # of rounds
2081 add r12, #0x90 @ pass key schedule
2082 bl _bsaes_key_convert
2084 vldmia r4, {@XMM[6]}
2085 vstmia r12, {@XMM[15]} @ save last round key
2086 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2087 vstmia r4, {@XMM[7]}
2089 ldr r12, [$key, #244]
2093 str r12, [$key, #244]
2094 mov r4, $key @ pass key
2095 mov r5, $rounds @ pass # of rounds
2096 add r12, $key, #248 @ pass key schedule
2097 bl _bsaes_key_convert
2099 vldmia r4, {@XMM[6]}
2100 vstmia r12, {@XMM[15]} @ save last round key
2101 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2102 vstmia r4, {@XMM[7]}
2105 0: sub sp, #0x90 @ place for tweak[9]
2107 vld1.8 {@XMM[8]}, [r0] @ initial tweak
2108 adr $magic, .Lxts_magic
2110 #ifndef XTS_CHAIN_TWEAK
2111 tst $len, #0xf @ if not multiple of 16
2112 it ne @ Thumb2 thing, sanity check in ARM
2113 subne $len, #0x10 @ subtract another 16 bytes
2122 vldmia $magic, {$twmask} @ load XTS magic
2123 vshr.s64 @T[0], @XMM[8], #63
2125 vand @T[0], @T[0], $twmask
2127 for($i=9;$i<16;$i++) {
2129 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2130 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2131 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2132 vshr.s64 @T[1], @XMM[$i], #63
2133 veor @XMM[$i], @XMM[$i], @T[0]
2134 vand @T[1], @T[1], $twmask
2138 $code.=<<___ if ($i>=10);
2139 vld1.8 {@XMM[$i-10]}, [$inp]!
2141 $code.=<<___ if ($i>=11);
2142 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2146 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
2147 vst1.64 {@XMM[15]}, [r0,:128]!
2148 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2149 veor @XMM[8], @XMM[8], @T[0]
2150 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2152 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
2153 veor @XMM[5], @XMM[5], @XMM[13]
2154 #ifndef BSAES_ASM_EXTENDED_KEY
2155 add r4, sp, #0x90 @ pass key schedule
2157 add r4, $key, #248 @ pass key schedule
2159 veor @XMM[6], @XMM[6], @XMM[14]
2160 mov r5, $rounds @ pass rounds
2161 veor @XMM[7], @XMM[7], @XMM[15]
2166 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2167 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2168 veor @XMM[0], @XMM[0], @XMM[ 8]
2169 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2170 veor @XMM[1], @XMM[1], @XMM[ 9]
2171 veor @XMM[8], @XMM[6], @XMM[10]
2172 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2173 veor @XMM[9], @XMM[4], @XMM[11]
2174 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
2175 veor @XMM[10], @XMM[2], @XMM[12]
2176 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2177 veor @XMM[11], @XMM[7], @XMM[13]
2178 veor @XMM[12], @XMM[3], @XMM[14]
2179 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2180 veor @XMM[13], @XMM[5], @XMM[15]
2181 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
2183 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2192 vldmia $magic, {$twmask} @ load XTS magic
2193 vshr.s64 @T[0], @XMM[8], #63
2195 vand @T[0], @T[0], $twmask
2197 for($i=9;$i<16;$i++) {
2199 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2200 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2201 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2202 vshr.s64 @T[1], @XMM[$i], #63
2203 veor @XMM[$i], @XMM[$i], @T[0]
2204 vand @T[1], @T[1], $twmask
2208 $code.=<<___ if ($i>=10);
2209 vld1.8 {@XMM[$i-10]}, [$inp]!
2211 bmi .Lxts_dec_`$i-9`
2213 $code.=<<___ if ($i>=11);
2214 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2219 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
2221 vld1.8 {@XMM[6]}, [$inp]!
2222 veor @XMM[5], @XMM[5], @XMM[13]
2223 #ifndef BSAES_ASM_EXTENDED_KEY
2224 add r4, sp, #0x90 @ pass key schedule
2226 add r4, $key, #248 @ pass key schedule
2228 veor @XMM[6], @XMM[6], @XMM[14]
2229 mov r5, $rounds @ pass rounds
2234 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2235 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2236 veor @XMM[0], @XMM[0], @XMM[ 8]
2237 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2238 veor @XMM[1], @XMM[1], @XMM[ 9]
2239 veor @XMM[8], @XMM[6], @XMM[10]
2240 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2241 veor @XMM[9], @XMM[4], @XMM[11]
2242 vld1.64 {@XMM[14]}, [r0,:128]!
2243 veor @XMM[10], @XMM[2], @XMM[12]
2244 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2245 veor @XMM[11], @XMM[7], @XMM[13]
2246 veor @XMM[12], @XMM[3], @XMM[14]
2247 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2248 vst1.8 {@XMM[12]}, [$out]!
2250 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2254 vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
2256 veor @XMM[4], @XMM[4], @XMM[12]
2257 #ifndef BSAES_ASM_EXTENDED_KEY
2258 add r4, sp, #0x90 @ pass key schedule
2260 add r4, $key, #248 @ pass key schedule
2262 veor @XMM[5], @XMM[5], @XMM[13]
2263 mov r5, $rounds @ pass rounds
2268 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2269 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2270 veor @XMM[0], @XMM[0], @XMM[ 8]
2271 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2272 veor @XMM[1], @XMM[1], @XMM[ 9]
2273 veor @XMM[8], @XMM[6], @XMM[10]
2274 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2275 veor @XMM[9], @XMM[4], @XMM[11]
2276 veor @XMM[10], @XMM[2], @XMM[12]
2277 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2278 veor @XMM[11], @XMM[7], @XMM[13]
2279 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2281 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2285 veor @XMM[3], @XMM[3], @XMM[11]
2286 #ifndef BSAES_ASM_EXTENDED_KEY
2287 add r4, sp, #0x90 @ pass key schedule
2289 add r4, $key, #248 @ pass key schedule
2291 veor @XMM[4], @XMM[4], @XMM[12]
2292 mov r5, $rounds @ pass rounds
2297 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2298 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2299 veor @XMM[0], @XMM[0], @XMM[ 8]
2300 vld1.64 {@XMM[12]}, [r0,:128]!
2301 veor @XMM[1], @XMM[1], @XMM[ 9]
2302 veor @XMM[8], @XMM[6], @XMM[10]
2303 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2304 veor @XMM[9], @XMM[4], @XMM[11]
2305 veor @XMM[10], @XMM[2], @XMM[12]
2306 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2307 vst1.8 {@XMM[10]}, [$out]!
2309 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2313 veor @XMM[2], @XMM[2], @XMM[10]
2314 #ifndef BSAES_ASM_EXTENDED_KEY
2315 add r4, sp, #0x90 @ pass key schedule
2317 add r4, $key, #248 @ pass key schedule
2319 veor @XMM[3], @XMM[3], @XMM[11]
2320 mov r5, $rounds @ pass rounds
2325 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2326 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2327 veor @XMM[0], @XMM[0], @XMM[ 8]
2328 veor @XMM[1], @XMM[1], @XMM[ 9]
2329 veor @XMM[8], @XMM[6], @XMM[10]
2330 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2331 veor @XMM[9], @XMM[4], @XMM[11]
2332 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2334 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2338 veor @XMM[1], @XMM[1], @XMM[9]
2339 #ifndef BSAES_ASM_EXTENDED_KEY
2340 add r4, sp, #0x90 @ pass key schedule
2342 add r4, $key, #248 @ pass key schedule
2344 veor @XMM[2], @XMM[2], @XMM[10]
2345 mov r5, $rounds @ pass rounds
2350 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2351 vld1.64 {@XMM[10]}, [r0,:128]!
2352 veor @XMM[0], @XMM[0], @XMM[ 8]
2353 veor @XMM[1], @XMM[1], @XMM[ 9]
2354 veor @XMM[8], @XMM[6], @XMM[10]
2355 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2356 vst1.8 {@XMM[8]}, [$out]!
2358 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2362 veor @XMM[0], @XMM[0], @XMM[8]
2363 #ifndef BSAES_ASM_EXTENDED_KEY
2364 add r4, sp, #0x90 @ pass key schedule
2366 add r4, $key, #248 @ pass key schedule
2368 veor @XMM[1], @XMM[1], @XMM[9]
2369 mov r5, $rounds @ pass rounds
2374 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2375 veor @XMM[0], @XMM[0], @XMM[ 8]
2376 veor @XMM[1], @XMM[1], @XMM[ 9]
2377 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2379 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2384 veor @XMM[0], @XMM[0], @XMM[8]
2386 vst1.8 {@XMM[0]}, [sp,:128]
2387 mov r5, $magic @ preserve magic
2389 mov r4, $fp @ preserve fp
2393 vld1.8 {@XMM[0]}, [sp,:128]
2394 veor @XMM[0], @XMM[0], @XMM[8]
2395 vst1.8 {@XMM[0]}, [$out]!
2399 vmov @XMM[8], @XMM[9] @ next round tweak
2402 #ifndef XTS_CHAIN_TWEAK
2406 @ calculate one round of extra tweak for the stolen ciphertext
2407 vldmia $magic, {$twmask}
2408 vshr.s64 @XMM[6], @XMM[8], #63
2409 vand @XMM[6], @XMM[6], $twmask
2410 vadd.u64 @XMM[9], @XMM[8], @XMM[8]
2411 vswp `&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
2412 veor @XMM[9], @XMM[9], @XMM[6]
2414 @ perform the final decryption with the last tweak value
2415 vld1.8 {@XMM[0]}, [$inp]!
2417 veor @XMM[0], @XMM[0], @XMM[9]
2419 vst1.8 {@XMM[0]}, [sp,:128]
2421 mov r4, $fp @ preserve fp
2425 vld1.8 {@XMM[0]}, [sp,:128]
2426 veor @XMM[0], @XMM[0], @XMM[9]
2427 vst1.8 {@XMM[0]}, [$out]
2433 strb r1, [$out, #0x10]
2439 vld1.8 {@XMM[0]}, [r6]
2441 veor @XMM[0], @XMM[8]
2443 vst1.8 {@XMM[0]}, [sp,:128]
2448 vld1.8 {@XMM[0]}, [sp,:128]
2449 veor @XMM[0], @XMM[0], @XMM[8]
2450 vst1.8 {@XMM[0]}, [r6]
2458 #ifdef XTS_CHAIN_TWEAK
2459 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2461 .Lxts_dec_bzero: @ wipe key schedule [if any]
2467 #ifdef XTS_CHAIN_TWEAK
2468 vst1.8 {@XMM[8]}, [r1]
2471 ldmia sp!, {r4-r10, pc} @ return
2473 .size bsaes_xts_decrypt,.-bsaes_xts_decrypt
2480 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
2485 last if (!s/^#/@/ and !/^$/);