2 # Copyright 2012-2020 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.
53 # $output is the last argument if it looks like a file (it has an extension)
54 # $flavour is the first argument if it doesn't look like a file
55 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
56 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
58 if ($flavour && $flavour ne "void") {
59 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
60 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
61 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
62 die "can't locate arm-xlate.pl";
64 open STDOUT,"| \"$^X\" $xlate $flavour \"$output\""
65 or die "can't call $xlate: $!";
67 $output and open STDOUT,">$output";
70 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
71 my @XMM=map("q$_",(0..15));
74 my ($key,$rounds,$const)=("r4","r5","r6");
76 sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
77 sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
80 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
81 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
86 &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
87 &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
91 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
92 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
95 veor @b[2], @b[2], @b[1]
96 veor @b[5], @b[5], @b[6]
97 veor @b[3], @b[3], @b[0]
98 veor @b[6], @b[6], @b[2]
99 veor @b[5], @b[5], @b[0]
101 veor @b[6], @b[6], @b[3]
102 veor @b[3], @b[3], @b[7]
103 veor @b[7], @b[7], @b[5]
104 veor @b[3], @b[3], @b[4]
105 veor @b[4], @b[4], @b[5]
107 veor @b[2], @b[2], @b[7]
108 veor @b[3], @b[3], @b[1]
109 veor @b[1], @b[1], @b[5]
114 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
115 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
118 veor @b[0], @b[0], @b[6]
119 veor @b[1], @b[1], @b[4]
120 veor @b[4], @b[4], @b[6]
121 veor @b[2], @b[2], @b[0]
122 veor @b[6], @b[6], @b[1]
124 veor @b[1], @b[1], @b[5]
125 veor @b[5], @b[5], @b[3]
126 veor @b[3], @b[3], @b[7]
127 veor @b[7], @b[7], @b[5]
128 veor @b[2], @b[2], @b[5]
130 veor @b[4], @b[4], @b[7]
135 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
136 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
140 &InvInBasisChange (@b);
141 &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
142 &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
145 sub InvInBasisChange { # OutBasisChange in reverse (with twist)
146 my @b=@_[5,1,2,6,3,7,0,4];
148 veor @b[1], @b[1], @b[7]
149 veor @b[4], @b[4], @b[7]
151 veor @b[7], @b[7], @b[5]
152 veor @b[1], @b[1], @b[3]
153 veor @b[2], @b[2], @b[5]
154 veor @b[3], @b[3], @b[7]
156 veor @b[6], @b[6], @b[1]
157 veor @b[2], @b[2], @b[0]
158 veor @b[5], @b[5], @b[3]
159 veor @b[4], @b[4], @b[6]
160 veor @b[0], @b[0], @b[6]
161 veor @b[1], @b[1], @b[4]
165 sub InvOutBasisChange { # InBasisChange in reverse
166 my @b=@_[2,5,7,3,6,1,0,4];
168 veor @b[1], @b[1], @b[5]
169 veor @b[2], @b[2], @b[7]
171 veor @b[3], @b[3], @b[1]
172 veor @b[4], @b[4], @b[5]
173 veor @b[7], @b[7], @b[5]
174 veor @b[3], @b[3], @b[4]
175 veor @b[5], @b[5], @b[0]
176 veor @b[3], @b[3], @b[7]
177 veor @b[6], @b[6], @b[2]
178 veor @b[2], @b[2], @b[1]
179 veor @b[6], @b[6], @b[3]
181 veor @b[3], @b[3], @b[0]
182 veor @b[5], @b[5], @b[6]
187 #;*************************************************************
188 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
189 #;*************************************************************
190 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
202 sub Mul_GF4_N { # not used, see next subroutine
203 # multiply and scale by N
204 my ($x0,$x1,$y0,$y1,$t0)=@_;
217 # interleaved Mul_GF4_N and Mul_GF4
218 my ($x0,$x1,$y0,$y1,$t0,
219 $x2,$x3,$y2,$y3,$t1)=@_;
242 veor @t[0], @x[0], @x[2]
243 veor @t[1], @x[1], @x[3]
245 &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
247 veor @y[0], @y[0], @y[2]
248 veor @y[1], @y[1], @y[3]
250 Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
251 @x[2], @x[3], @y[2], @y[3], @t[2]);
253 veor @x[0], @x[0], @t[0]
254 veor @x[2], @x[2], @t[0]
255 veor @x[1], @x[1], @t[1]
256 veor @x[3], @x[3], @t[1]
258 veor @t[0], @x[4], @x[6]
259 veor @t[1], @x[5], @x[7]
261 &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
262 @x[6], @x[7], @y[2], @y[3], @t[2]);
264 veor @y[0], @y[0], @y[2]
265 veor @y[1], @y[1], @y[3]
267 &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
269 veor @x[4], @x[4], @t[0]
270 veor @x[6], @x[6], @t[0]
271 veor @x[5], @x[5], @t[1]
272 veor @x[7], @x[7], @t[1]
276 #;********************************************************************
277 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
278 #;********************************************************************
282 # direct optimizations from hardware
284 veor @t[3], @x[4], @x[6]
285 veor @t[2], @x[5], @x[7]
286 veor @t[1], @x[1], @x[3]
287 veor @s[1], @x[7], @x[6]
289 veor @s[0], @x[0], @x[2]
291 vorr @t[2], @t[2], @t[1]
292 veor @s[3], @t[3], @t[0]
293 vand @s[2], @t[3], @s[0]
294 vorr @t[3], @t[3], @s[0]
295 veor @s[0], @s[0], @t[1]
296 vand @t[0], @t[0], @t[1]
297 veor @t[1], @x[3], @x[2]
298 vand @s[3], @s[3], @s[0]
299 vand @s[1], @s[1], @t[1]
300 veor @t[1], @x[4], @x[5]
301 veor @s[0], @x[1], @x[0]
302 veor @t[3], @t[3], @s[1]
303 veor @t[2], @t[2], @s[1]
304 vand @s[1], @t[1], @s[0]
305 vorr @t[1], @t[1], @s[0]
306 veor @t[3], @t[3], @s[3]
307 veor @t[0], @t[0], @s[1]
308 veor @t[2], @t[2], @s[2]
309 veor @t[1], @t[1], @s[3]
310 veor @t[0], @t[0], @s[2]
311 vand @s[0], @x[7], @x[3]
312 veor @t[1], @t[1], @s[2]
313 vand @s[1], @x[6], @x[2]
314 vand @s[2], @x[5], @x[1]
315 vorr @s[3], @x[4], @x[0]
316 veor @t[3], @t[3], @s[0]
317 veor @t[1], @t[1], @s[2]
318 veor @t[0], @t[0], @s[3]
319 veor @t[2], @t[2], @s[1]
321 @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
323 @ new smaller inversion
325 vand @s[2], @t[3], @t[1]
328 veor @s[1], @t[2], @s[2]
329 veor @s[3], @t[0], @s[2]
330 veor @s[2], @t[0], @s[2] @ @s[2]=@s[3]
332 vbsl @s[1], @t[1], @t[0]
333 vbsl @s[3], @t[3], @t[2]
334 veor @t[3], @t[3], @t[2]
336 vbsl @s[0], @s[1], @s[2]
337 vbsl @t[0], @s[2], @s[1]
339 vand @s[2], @s[0], @s[3]
340 veor @t[1], @t[1], @t[0]
342 veor @s[2], @s[2], @t[3]
344 # output in s3, s2, s1, t1
346 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
348 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
349 &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
351 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
354 # AES linear components
361 vldmia $key!, {@t[0]-@t[3]}
362 veor @t[0], @t[0], @x[0]
363 veor @t[1], @t[1], @x[1]
364 vtbl.8 `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
365 vtbl.8 `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
366 vldmia $key!, {@t[0]}
367 veor @t[2], @t[2], @x[2]
368 vtbl.8 `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
369 vtbl.8 `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
370 vldmia $key!, {@t[1]}
371 veor @t[3], @t[3], @x[3]
372 vtbl.8 `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
373 vtbl.8 `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
374 vldmia $key!, {@t[2]}
375 vtbl.8 `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
376 vtbl.8 `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
377 vldmia $key!, {@t[3]}
378 veor @t[0], @t[0], @x[4]
379 veor @t[1], @t[1], @x[5]
380 vtbl.8 `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
381 vtbl.8 `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
382 veor @t[2], @t[2], @x[6]
383 vtbl.8 `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
384 vtbl.8 `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
385 veor @t[3], @t[3], @x[7]
386 vtbl.8 `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
387 vtbl.8 `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
388 vtbl.8 `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
389 vtbl.8 `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
394 # modified to emit output in order suitable for feeding back to aesenc[last]
397 my $inv=@_[16]; # optional
399 vext.8 @t[0], @x[0], @x[0], #12 @ x0 <<< 32
400 vext.8 @t[1], @x[1], @x[1], #12
401 veor @x[0], @x[0], @t[0] @ x0 ^ (x0 <<< 32)
402 vext.8 @t[2], @x[2], @x[2], #12
403 veor @x[1], @x[1], @t[1]
404 vext.8 @t[3], @x[3], @x[3], #12
405 veor @x[2], @x[2], @t[2]
406 vext.8 @t[4], @x[4], @x[4], #12
407 veor @x[3], @x[3], @t[3]
408 vext.8 @t[5], @x[5], @x[5], #12
409 veor @x[4], @x[4], @t[4]
410 vext.8 @t[6], @x[6], @x[6], #12
411 veor @x[5], @x[5], @t[5]
412 vext.8 @t[7], @x[7], @x[7], #12
413 veor @x[6], @x[6], @t[6]
415 veor @t[1], @t[1], @x[0]
416 veor @x[7], @x[7], @t[7]
417 vext.8 @x[0], @x[0], @x[0], #8 @ (x0 ^ (x0 <<< 32)) <<< 64)
418 veor @t[2], @t[2], @x[1]
419 veor @t[0], @t[0], @x[7]
420 veor @t[1], @t[1], @x[7]
421 vext.8 @x[1], @x[1], @x[1], #8
422 veor @t[5], @t[5], @x[4]
423 veor @x[0], @x[0], @t[0]
424 veor @t[6], @t[6], @x[5]
425 veor @x[1], @x[1], @t[1]
426 vext.8 @t[0], @x[4], @x[4], #8
427 veor @t[4], @t[4], @x[3]
428 vext.8 @t[1], @x[5], @x[5], #8
429 veor @t[7], @t[7], @x[6]
430 vext.8 @x[4], @x[3], @x[3], #8
431 veor @t[3], @t[3], @x[2]
432 vext.8 @x[5], @x[7], @x[7], #8
433 veor @t[4], @t[4], @x[7]
434 vext.8 @x[3], @x[6], @x[6], #8
435 veor @t[3], @t[3], @x[7]
436 vext.8 @x[6], @x[2], @x[2], #8
437 veor @x[7], @t[1], @t[5]
439 $code.=<<___ if (!$inv);
440 veor @x[2], @t[0], @t[4]
441 veor @x[4], @x[4], @t[3]
442 veor @x[5], @x[5], @t[7]
443 veor @x[3], @x[3], @t[6]
445 veor @x[6], @x[6], @t[2]
448 $code.=<<___ if ($inv);
449 veor @t[3], @t[3], @x[4]
450 veor @x[5], @x[5], @t[7]
451 veor @x[2], @x[3], @t[6]
452 veor @x[3], @t[0], @t[4]
453 veor @x[4], @x[6], @t[2]
459 sub InvMixColumns_orig {
464 @ multiplication by 0x0e
465 vext.8 @t[7], @x[7], @x[7], #12
467 veor @x[2], @x[2], @x[5] @ 2 5
468 veor @x[7], @x[7], @x[5] @ 7 5
469 vext.8 @t[0], @x[0], @x[0], #12
471 veor @x[5], @x[5], @x[0] @ 5 0 [1]
472 veor @x[0], @x[0], @x[1] @ 0 1
473 vext.8 @t[1], @x[1], @x[1], #12
474 veor @x[1], @x[1], @x[2] @ 1 25
475 veor @x[0], @x[0], @x[6] @ 01 6 [2]
476 vext.8 @t[3], @x[3], @x[3], #12
477 veor @x[1], @x[1], @x[3] @ 125 3 [4]
478 veor @x[2], @x[2], @x[0] @ 25 016 [3]
479 veor @x[3], @x[3], @x[7] @ 3 75
480 veor @x[7], @x[7], @x[6] @ 75 6 [0]
481 vext.8 @t[6], @x[6], @x[6], #12
483 veor @x[6], @x[6], @x[4] @ 6 4
484 veor @x[4], @x[4], @x[3] @ 4 375 [6]
485 veor @x[3], @x[3], @x[7] @ 375 756=36
486 veor @x[6], @x[6], @t[5] @ 64 5 [7]
487 veor @x[3], @x[3], @t[2] @ 36 2
488 vext.8 @t[5], @t[5], @t[5], #12
489 veor @x[3], @x[3], @t[4] @ 362 4 [5]
491 my @y = @x[7,5,0,2,1,3,4,6];
493 @ multiplication by 0x0b
494 veor @y[1], @y[1], @y[0]
495 veor @y[0], @y[0], @t[0]
496 vext.8 @t[2], @t[2], @t[2], #12
497 veor @y[1], @y[1], @t[1]
498 veor @y[0], @y[0], @t[5]
499 vext.8 @t[4], @t[4], @t[4], #12
500 veor @y[1], @y[1], @t[6]
501 veor @y[0], @y[0], @t[7]
502 veor @t[7], @t[7], @t[6] @ clobber t[7]
504 veor @y[3], @y[3], @t[0]
505 veor @y[1], @y[1], @y[0]
506 vext.8 @t[0], @t[0], @t[0], #12
507 veor @y[2], @y[2], @t[1]
508 veor @y[4], @y[4], @t[1]
509 vext.8 @t[1], @t[1], @t[1], #12
510 veor @y[2], @y[2], @t[2]
511 veor @y[3], @y[3], @t[2]
512 veor @y[5], @y[5], @t[2]
513 veor @y[2], @y[2], @t[7]
514 vext.8 @t[2], @t[2], @t[2], #12
515 veor @y[3], @y[3], @t[3]
516 veor @y[6], @y[6], @t[3]
517 veor @y[4], @y[4], @t[3]
518 veor @y[7], @y[7], @t[4]
519 vext.8 @t[3], @t[3], @t[3], #12
520 veor @y[5], @y[5], @t[4]
521 veor @y[7], @y[7], @t[7]
522 veor @t[7], @t[7], @t[5] @ clobber t[7] even more
523 veor @y[3], @y[3], @t[5]
524 veor @y[4], @y[4], @t[4]
526 veor @y[5], @y[5], @t[7]
527 vext.8 @t[4], @t[4], @t[4], #12
528 veor @y[6], @y[6], @t[7]
529 veor @y[4], @y[4], @t[7]
531 veor @t[7], @t[7], @t[5]
532 vext.8 @t[5], @t[5], @t[5], #12
534 @ multiplication by 0x0d
535 veor @y[4], @y[4], @y[7]
536 veor @t[7], @t[7], @t[6] @ restore t[7]
537 veor @y[7], @y[7], @t[4]
538 vext.8 @t[6], @t[6], @t[6], #12
539 veor @y[2], @y[2], @t[0]
540 veor @y[7], @y[7], @t[5]
541 vext.8 @t[7], @t[7], @t[7], #12
542 veor @y[2], @y[2], @t[2]
544 veor @y[3], @y[3], @y[1]
545 veor @y[1], @y[1], @t[1]
546 veor @y[0], @y[0], @t[0]
547 veor @y[3], @y[3], @t[0]
548 veor @y[1], @y[1], @t[5]
549 veor @y[0], @y[0], @t[5]
550 vext.8 @t[0], @t[0], @t[0], #12
551 veor @y[1], @y[1], @t[7]
552 veor @y[0], @y[0], @t[6]
553 veor @y[3], @y[3], @y[1]
554 veor @y[4], @y[4], @t[1]
555 vext.8 @t[1], @t[1], @t[1], #12
557 veor @y[7], @y[7], @t[7]
558 veor @y[4], @y[4], @t[2]
559 veor @y[5], @y[5], @t[2]
560 veor @y[2], @y[2], @t[6]
561 veor @t[6], @t[6], @t[3] @ clobber t[6]
562 vext.8 @t[2], @t[2], @t[2], #12
563 veor @y[4], @y[4], @y[7]
564 veor @y[3], @y[3], @t[6]
566 veor @y[6], @y[6], @t[6]
567 veor @y[5], @y[5], @t[5]
568 vext.8 @t[5], @t[5], @t[5], #12
569 veor @y[6], @y[6], @t[4]
570 vext.8 @t[4], @t[4], @t[4], #12
571 veor @y[5], @y[5], @t[6]
572 veor @y[6], @y[6], @t[7]
573 vext.8 @t[7], @t[7], @t[7], #12
574 veor @t[6], @t[6], @t[3] @ restore t[6]
575 vext.8 @t[3], @t[3], @t[3], #12
577 @ multiplication by 0x09
578 veor @y[4], @y[4], @y[1]
579 veor @t[1], @t[1], @y[1] @ t[1]=y[1]
580 veor @t[0], @t[0], @t[5] @ clobber t[0]
581 vext.8 @t[6], @t[6], @t[6], #12
582 veor @t[1], @t[1], @t[5]
583 veor @y[3], @y[3], @t[0]
584 veor @t[0], @t[0], @y[0] @ t[0]=y[0]
585 veor @t[1], @t[1], @t[6]
586 veor @t[6], @t[6], @t[7] @ clobber t[6]
587 veor @y[4], @y[4], @t[1]
588 veor @y[7], @y[7], @t[4]
589 veor @y[6], @y[6], @t[3]
590 veor @y[5], @y[5], @t[2]
591 veor @t[4], @t[4], @y[4] @ t[4]=y[4]
592 veor @t[3], @t[3], @y[3] @ t[3]=y[3]
593 veor @t[5], @t[5], @y[5] @ t[5]=y[5]
594 veor @t[2], @t[2], @y[2] @ t[2]=y[2]
595 veor @t[3], @t[3], @t[7]
596 veor @XMM[5], @t[5], @t[6]
597 veor @XMM[6], @t[6], @y[6] @ t[6]=y[6]
598 veor @XMM[2], @t[2], @t[6]
599 veor @XMM[7], @t[7], @y[7] @ t[7]=y[7]
603 @ vmov @XMM[2], @t[2]
606 @ vmov @XMM[5], @t[5]
607 @ vmov @XMM[6], @t[6]
608 @ vmov @XMM[7], @t[7]
616 # Thanks to Jussi Kivilinna for providing pointer to
618 # | 0e 0b 0d 09 | | 02 03 01 01 | | 05 00 04 00 |
619 # | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
620 # | 0d 09 0e 0b | | 01 01 02 03 | | 04 00 05 00 |
621 # | 0b 0d 09 0e | | 03 01 01 02 | | 00 04 00 05 |
624 @ multiplication by 0x05-0x00-0x04-0x00
625 vext.8 @t[0], @x[0], @x[0], #8
626 vext.8 @t[6], @x[6], @x[6], #8
627 vext.8 @t[7], @x[7], @x[7], #8
628 veor @t[0], @t[0], @x[0]
629 vext.8 @t[1], @x[1], @x[1], #8
630 veor @t[6], @t[6], @x[6]
631 vext.8 @t[2], @x[2], @x[2], #8
632 veor @t[7], @t[7], @x[7]
633 vext.8 @t[3], @x[3], @x[3], #8
634 veor @t[1], @t[1], @x[1]
635 vext.8 @t[4], @x[4], @x[4], #8
636 veor @t[2], @t[2], @x[2]
637 vext.8 @t[5], @x[5], @x[5], #8
638 veor @t[3], @t[3], @x[3]
639 veor @t[4], @t[4], @x[4]
640 veor @t[5], @t[5], @x[5]
642 veor @x[0], @x[0], @t[6]
643 veor @x[1], @x[1], @t[6]
644 veor @x[2], @x[2], @t[0]
645 veor @x[4], @x[4], @t[2]
646 veor @x[3], @x[3], @t[1]
647 veor @x[1], @x[1], @t[7]
648 veor @x[2], @x[2], @t[7]
649 veor @x[4], @x[4], @t[6]
650 veor @x[5], @x[5], @t[3]
651 veor @x[3], @x[3], @t[6]
652 veor @x[6], @x[6], @t[4]
653 veor @x[4], @x[4], @t[7]
654 veor @x[5], @x[5], @t[7]
655 veor @x[7], @x[7], @t[5]
657 &MixColumns (@x,@t,1); # flipped 2<->3 and 4<->6
661 my ($a,$b,$n,$mask,$t)=@_;
672 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
674 vshr.u64 $t0, $b0, #$n
675 vshr.u64 $t1, $b1, #$n
681 vshl.u64 $t0, $t0, #$n
683 vshl.u64 $t1, $t1, #$n
690 my @x=reverse(@_[0..7]);
691 my ($t0,$t1,$t2,$t3)=@_[8..11];
693 vmov.i8 $t0,#0x55 @ compose .LBS0
694 vmov.i8 $t1,#0x33 @ compose .LBS1
696 &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
697 &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
699 vmov.i8 $t0,#0x0f @ compose .LBS2
701 &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
702 &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
704 &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
705 &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
710 # include "arm_arch.h"
712 # define VFP_ABI_PUSH vstmdb sp!,{d8-d15}
713 # define VFP_ABI_POP vldmia sp!,{d8-d15}
714 # define VFP_ABI_FRAME 0x40
716 # define VFP_ABI_PUSH
718 # define VFP_ABI_FRAME 0
719 # define BSAES_ASM_EXTENDED_KEY
720 # define XTS_CHAIN_TWEAK
721 # define __ARM_ARCH__ __LINUX_ARM_ARCH__
722 # define __ARM_MAX_ARCH__ 7
729 #if __ARM_MAX_ARCH__>=7
733 .syntax unified @ ARMv7-capable assembler is expected to handle this
734 #if defined(__thumb2__) && !defined(__APPLE__)
743 .type _bsaes_decrypt8,%function
747 vldmia $key!, {@XMM[9]} @ round 0 key
748 #if defined(__thumb2__) || defined(__APPLE__)
751 add $const,$const,#.LM0ISR-_bsaes_decrypt8
754 vldmia $const!, {@XMM[8]} @ .LM0ISR
755 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
756 veor @XMM[11], @XMM[1], @XMM[9]
757 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
758 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
759 veor @XMM[12], @XMM[2], @XMM[9]
760 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
761 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
762 veor @XMM[13], @XMM[3], @XMM[9]
763 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
764 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
765 veor @XMM[14], @XMM[4], @XMM[9]
766 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
767 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
768 veor @XMM[15], @XMM[5], @XMM[9]
769 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
770 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
771 veor @XMM[10], @XMM[6], @XMM[9]
772 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
773 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
774 veor @XMM[11], @XMM[7], @XMM[9]
775 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
776 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
777 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
778 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
780 &bitslice (@XMM[0..7, 8..11]);
782 sub $rounds,$rounds,#1
787 &ShiftRows (@XMM[0..7, 8..12]);
788 $code.=".Ldec_sbox:\n";
789 &InvSbox (@XMM[0..7, 8..15]);
791 subs $rounds,$rounds,#1
794 &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
796 vldmia $const, {@XMM[12]} @ .LISR
797 ite eq @ Thumb2 thing, sanity check in ARM
798 addeq $const,$const,#0x10
800 vldmia $const, {@XMM[12]} @ .LISRM0
805 &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
807 vldmia $key, {@XMM[8]} @ last round key
808 veor @XMM[6], @XMM[6], @XMM[8]
809 veor @XMM[4], @XMM[4], @XMM[8]
810 veor @XMM[2], @XMM[2], @XMM[8]
811 veor @XMM[7], @XMM[7], @XMM[8]
812 veor @XMM[3], @XMM[3], @XMM[8]
813 veor @XMM[5], @XMM[5], @XMM[8]
814 veor @XMM[0], @XMM[0], @XMM[8]
815 veor @XMM[1], @XMM[1], @XMM[8]
817 .size _bsaes_decrypt8,.-_bsaes_decrypt8
819 .type _bsaes_const,%object
822 .LM0ISR: @ InvShiftRows constants
823 .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
825 .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
827 .quad 0x01040b0e0205080f, 0x0306090c00070a0d
828 .LM0SR: @ ShiftRows constants
829 .quad 0x0a0e02060f03070b, 0x0004080c05090d01
831 .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
833 .quad 0x0304090e00050a0f, 0x01060b0c0207080d
835 .quad 0x02060a0e03070b0f, 0x0004080c0105090d
837 .quad 0x090d01050c000408, 0x03070b0f060a0e02
838 .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
840 .size _bsaes_const,.-_bsaes_const
842 .type _bsaes_encrypt8,%function
846 vldmia $key!, {@XMM[9]} @ round 0 key
847 #if defined(__thumb2__) || defined(__APPLE__)
850 sub $const,$const,#_bsaes_encrypt8-.LM0SR
853 vldmia $const!, {@XMM[8]} @ .LM0SR
855 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
856 veor @XMM[11], @XMM[1], @XMM[9]
857 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
858 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
859 veor @XMM[12], @XMM[2], @XMM[9]
860 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
861 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
862 veor @XMM[13], @XMM[3], @XMM[9]
863 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
864 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
865 veor @XMM[14], @XMM[4], @XMM[9]
866 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
867 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
868 veor @XMM[15], @XMM[5], @XMM[9]
869 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
870 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
871 veor @XMM[10], @XMM[6], @XMM[9]
872 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
873 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
874 veor @XMM[11], @XMM[7], @XMM[9]
875 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
876 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
877 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
878 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
879 _bsaes_encrypt8_bitslice:
881 &bitslice (@XMM[0..7, 8..11]);
883 sub $rounds,$rounds,#1
888 &ShiftRows (@XMM[0..7, 8..12]);
889 $code.=".Lenc_sbox:\n";
890 &Sbox (@XMM[0..7, 8..15]);
892 subs $rounds,$rounds,#1
895 &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
897 vldmia $const, {@XMM[12]} @ .LSR
898 ite eq @ Thumb2 thing, samity check in ARM
899 addeq $const,$const,#0x10
901 vldmia $const, {@XMM[12]} @ .LSRM0
906 # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
907 &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
909 vldmia $key, {@XMM[8]} @ last round key
910 veor @XMM[4], @XMM[4], @XMM[8]
911 veor @XMM[6], @XMM[6], @XMM[8]
912 veor @XMM[3], @XMM[3], @XMM[8]
913 veor @XMM[7], @XMM[7], @XMM[8]
914 veor @XMM[2], @XMM[2], @XMM[8]
915 veor @XMM[5], @XMM[5], @XMM[8]
916 veor @XMM[0], @XMM[0], @XMM[8]
917 veor @XMM[1], @XMM[1], @XMM[8]
919 .size _bsaes_encrypt8,.-_bsaes_encrypt8
923 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
926 my @x=reverse(@_[0..7]);
927 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
929 &swapmove (@x[0,1],1,$bs0,$t2,$t3);
931 @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
935 #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
937 &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
939 @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
945 &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
946 &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
950 .type _bsaes_key_convert,%function
954 vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
955 #if defined(__thumb2__) || defined(__APPLE__)
958 sub $const,$const,#_bsaes_key_convert-.LM0
960 vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
962 vmov.i8 @XMM[8], #0x01 @ bit masks
963 vmov.i8 @XMM[9], #0x02
964 vmov.i8 @XMM[10], #0x04
965 vmov.i8 @XMM[11], #0x08
966 vmov.i8 @XMM[12], #0x10
967 vmov.i8 @XMM[13], #0x20
968 vldmia $const, {@XMM[14]} @ .LM0
971 vrev32.8 @XMM[7], @XMM[7]
972 vrev32.8 @XMM[15], @XMM[15]
974 sub $rounds,$rounds,#1
975 vstmia $out!, {@XMM[7]} @ save round 0 key
980 vtbl.8 `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
981 vtbl.8 `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
982 vmov.i8 @XMM[6], #0x40
983 vmov.i8 @XMM[15], #0x80
985 vtst.8 @XMM[0], @XMM[7], @XMM[8]
986 vtst.8 @XMM[1], @XMM[7], @XMM[9]
987 vtst.8 @XMM[2], @XMM[7], @XMM[10]
988 vtst.8 @XMM[3], @XMM[7], @XMM[11]
989 vtst.8 @XMM[4], @XMM[7], @XMM[12]
990 vtst.8 @XMM[5], @XMM[7], @XMM[13]
991 vtst.8 @XMM[6], @XMM[7], @XMM[6]
992 vtst.8 @XMM[7], @XMM[7], @XMM[15]
993 vld1.8 {@XMM[15]}, [$inp]! @ load next round key
994 vmvn @XMM[0], @XMM[0] @ "pnot"
995 vmvn @XMM[1], @XMM[1]
996 vmvn @XMM[5], @XMM[5]
997 vmvn @XMM[6], @XMM[6]
999 vrev32.8 @XMM[15], @XMM[15]
1001 subs $rounds,$rounds,#1
1002 vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
1005 vmov.i8 @XMM[7],#0x63 @ compose .L63
1006 @ don't save last round key
1008 .size _bsaes_key_convert,.-_bsaes_key_convert
1012 if (0) { # following four functions are unsupported interface
1013 # used for benchmarking...
1015 .globl bsaes_enc_key_convert
1016 .type bsaes_enc_key_convert,%function
1018 bsaes_enc_key_convert:
1019 stmdb sp!,{r4-r6,lr}
1020 vstmdb sp!,{d8-d15} @ ABI specification says so
1022 ldr r5,[$inp,#240] @ pass rounds
1023 mov r4,$inp @ pass key
1024 mov r12,$out @ pass key schedule
1025 bl _bsaes_key_convert
1026 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1027 vstmia r12, {@XMM[7]} @ save last round key
1030 ldmia sp!,{r4-r6,pc}
1031 .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
1033 .globl bsaes_encrypt_128
1034 .type bsaes_encrypt_128,%function
1037 stmdb sp!,{r4-r6,lr}
1038 vstmdb sp!,{d8-d15} @ ABI specification says so
1040 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1041 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1042 mov r4,$key @ pass the key
1043 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1044 mov r5,#10 @ pass rounds
1045 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1049 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1050 vst1.8 {@XMM[4]}, [$out]!
1051 vst1.8 {@XMM[6]}, [$out]!
1052 vst1.8 {@XMM[3]}, [$out]!
1053 vst1.8 {@XMM[7]}, [$out]!
1054 vst1.8 {@XMM[2]}, [$out]!
1055 subs $len,$len,#0x80
1056 vst1.8 {@XMM[5]}, [$out]!
1060 ldmia sp!,{r4-r6,pc}
1061 .size bsaes_encrypt_128,.-bsaes_encrypt_128
1063 .globl bsaes_dec_key_convert
1064 .type bsaes_dec_key_convert,%function
1066 bsaes_dec_key_convert:
1067 stmdb sp!,{r4-r6,lr}
1068 vstmdb sp!,{d8-d15} @ ABI specification says so
1070 ldr r5,[$inp,#240] @ pass rounds
1071 mov r4,$inp @ pass key
1072 mov r12,$out @ pass key schedule
1073 bl _bsaes_key_convert
1074 vldmia $out, {@XMM[6]}
1075 vstmia r12, {@XMM[15]} @ save last round key
1076 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1077 vstmia $out, {@XMM[7]}
1080 ldmia sp!,{r4-r6,pc}
1081 .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
1083 .globl bsaes_decrypt_128
1084 .type bsaes_decrypt_128,%function
1087 stmdb sp!,{r4-r6,lr}
1088 vstmdb sp!,{d8-d15} @ ABI specification says so
1090 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1091 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1092 mov r4,$key @ pass the key
1093 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1094 mov r5,#10 @ pass rounds
1095 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1099 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1100 vst1.8 {@XMM[6]}, [$out]!
1101 vst1.8 {@XMM[4]}, [$out]!
1102 vst1.8 {@XMM[2]}, [$out]!
1103 vst1.8 {@XMM[7]}, [$out]!
1104 vst1.8 {@XMM[3]}, [$out]!
1105 subs $len,$len,#0x80
1106 vst1.8 {@XMM[5]}, [$out]!
1110 ldmia sp!,{r4-r6,pc}
1111 .size bsaes_decrypt_128,.-bsaes_decrypt_128
1115 my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
1116 my ($keysched)=("sp");
1119 .extern AES_cbc_encrypt
1122 .global bsaes_cbc_encrypt
1123 .type bsaes_cbc_encrypt,%function
1137 @ it is up to the caller to make sure we are called with enc == 0
1140 stmdb sp!, {r4-r10, lr}
1142 ldr $ivp, [ip] @ IV is 1st arg on the stack
1143 mov $len, $len, lsr#4 @ len in 16 byte blocks
1144 sub sp, #0x10 @ scratch space to carry over the IV
1145 mov $fp, sp @ save sp
1147 ldr $rounds, [$key, #240] @ get # of rounds
1148 #ifndef BSAES_ASM_EXTENDED_KEY
1149 @ allocate the key schedule on the stack
1150 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1151 add r12, #`128-32` @ sifze of bit-slices key schedule
1153 @ populate the key schedule
1154 mov r4, $key @ pass key
1155 mov r5, $rounds @ pass # of rounds
1156 mov sp, r12 @ sp is $keysched
1157 bl _bsaes_key_convert
1158 vldmia $keysched, {@XMM[6]}
1159 vstmia r12, {@XMM[15]} @ save last round key
1160 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1161 vstmia $keysched, {@XMM[7]}
1163 ldr r12, [$key, #244]
1167 @ populate the key schedule
1168 str r12, [$key, #244]
1169 mov r4, $key @ pass key
1170 mov r5, $rounds @ pass # of rounds
1171 add r12, $key, #248 @ pass key schedule
1172 bl _bsaes_key_convert
1174 vldmia r4, {@XMM[6]}
1175 vstmia r12, {@XMM[15]} @ save last round key
1176 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1177 vstmia r4, {@XMM[7]}
1183 vld1.8 {@XMM[15]}, [$ivp] @ load IV
1188 subs $len, $len, #0x8
1189 bmi .Lcbc_dec_loop_finish
1191 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1192 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1193 #ifndef BSAES_ASM_EXTENDED_KEY
1194 mov r4, $keysched @ pass the key
1198 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1200 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]
1201 sub $inp, $inp, #0x60
1202 vstmia $fp, {@XMM[15]} @ put aside IV
1206 vldmia $fp, {@XMM[14]} @ reload IV
1207 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1208 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1209 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1210 veor @XMM[1], @XMM[1], @XMM[8]
1211 veor @XMM[6], @XMM[6], @XMM[9]
1212 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1213 veor @XMM[4], @XMM[4], @XMM[10]
1214 veor @XMM[2], @XMM[2], @XMM[11]
1215 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1216 veor @XMM[7], @XMM[7], @XMM[12]
1217 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1218 veor @XMM[3], @XMM[3], @XMM[13]
1219 vst1.8 {@XMM[6]}, [$out]!
1220 veor @XMM[5], @XMM[5], @XMM[14]
1221 vst1.8 {@XMM[4]}, [$out]!
1222 vst1.8 {@XMM[2]}, [$out]!
1223 vst1.8 {@XMM[7]}, [$out]!
1224 vst1.8 {@XMM[3]}, [$out]!
1225 vst1.8 {@XMM[5]}, [$out]!
1229 .Lcbc_dec_loop_finish:
1233 vld1.8 {@XMM[0]}, [$inp]! @ load input
1236 vld1.8 {@XMM[1]}, [$inp]!
1237 #ifndef BSAES_ASM_EXTENDED_KEY
1238 mov r4, $keysched @ pass the key
1243 vstmia $fp, {@XMM[15]} @ put aside IV
1245 vld1.8 {@XMM[2]}, [$inp]!
1248 vld1.8 {@XMM[3]}, [$inp]!
1250 vld1.8 {@XMM[4]}, [$inp]!
1253 vld1.8 {@XMM[5]}, [$inp]!
1255 vld1.8 {@XMM[6]}, [$inp]!
1256 sub $inp, $inp, #0x70
1260 vldmia $fp, {@XMM[14]} @ reload IV
1261 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1262 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1263 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1264 veor @XMM[1], @XMM[1], @XMM[8]
1265 veor @XMM[6], @XMM[6], @XMM[9]
1266 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1267 veor @XMM[4], @XMM[4], @XMM[10]
1268 veor @XMM[2], @XMM[2], @XMM[11]
1269 vld1.8 {@XMM[15]}, [$inp]!
1270 veor @XMM[7], @XMM[7], @XMM[12]
1271 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1272 veor @XMM[3], @XMM[3], @XMM[13]
1273 vst1.8 {@XMM[6]}, [$out]!
1274 vst1.8 {@XMM[4]}, [$out]!
1275 vst1.8 {@XMM[2]}, [$out]!
1276 vst1.8 {@XMM[7]}, [$out]!
1277 vst1.8 {@XMM[3]}, [$out]!
1281 sub $inp, $inp, #0x60
1283 vldmia $fp,{@XMM[14]} @ reload IV
1284 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1285 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1286 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1287 veor @XMM[1], @XMM[1], @XMM[8]
1288 veor @XMM[6], @XMM[6], @XMM[9]
1289 vld1.8 {@XMM[12]}, [$inp]!
1290 veor @XMM[4], @XMM[4], @XMM[10]
1291 veor @XMM[2], @XMM[2], @XMM[11]
1292 vld1.8 {@XMM[15]}, [$inp]!
1293 veor @XMM[7], @XMM[7], @XMM[12]
1294 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1295 vst1.8 {@XMM[6]}, [$out]!
1296 vst1.8 {@XMM[4]}, [$out]!
1297 vst1.8 {@XMM[2]}, [$out]!
1298 vst1.8 {@XMM[7]}, [$out]!
1302 sub $inp, $inp, #0x50
1304 vldmia $fp, {@XMM[14]} @ reload IV
1305 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1306 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1307 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1308 veor @XMM[1], @XMM[1], @XMM[8]
1309 veor @XMM[6], @XMM[6], @XMM[9]
1310 vld1.8 {@XMM[15]}, [$inp]!
1311 veor @XMM[4], @XMM[4], @XMM[10]
1312 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1313 veor @XMM[2], @XMM[2], @XMM[11]
1314 vst1.8 {@XMM[6]}, [$out]!
1315 vst1.8 {@XMM[4]}, [$out]!
1316 vst1.8 {@XMM[2]}, [$out]!
1320 sub $inp, $inp, #0x40
1322 vldmia $fp, {@XMM[14]} @ reload IV
1323 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1324 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1325 vld1.8 {@XMM[10]}, [$inp]!
1326 veor @XMM[1], @XMM[1], @XMM[8]
1327 veor @XMM[6], @XMM[6], @XMM[9]
1328 vld1.8 {@XMM[15]}, [$inp]!
1329 veor @XMM[4], @XMM[4], @XMM[10]
1330 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1331 vst1.8 {@XMM[6]}, [$out]!
1332 vst1.8 {@XMM[4]}, [$out]!
1336 sub $inp, $inp, #0x30
1338 vldmia $fp, {@XMM[14]} @ reload IV
1339 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1340 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1341 vld1.8 {@XMM[15]}, [$inp]!
1342 veor @XMM[1], @XMM[1], @XMM[8]
1343 veor @XMM[6], @XMM[6], @XMM[9]
1344 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1345 vst1.8 {@XMM[6]}, [$out]!
1349 sub $inp, $inp, #0x20
1351 vldmia $fp, {@XMM[14]} @ reload IV
1352 vld1.8 {@XMM[8]}, [$inp]! @ reload input
1353 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1354 vld1.8 {@XMM[15]}, [$inp]! @ reload input
1355 veor @XMM[1], @XMM[1], @XMM[8]
1356 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1360 sub $inp, $inp, #0x10
1361 mov $rounds, $out @ save original out pointer
1362 mov $out, $fp @ use the iv scratch space as out buffer
1364 vmov @XMM[4],@XMM[15] @ just in case ensure that IV
1365 vmov @XMM[5],@XMM[0] @ and input are preserved
1367 vld1.8 {@XMM[0]}, [$fp] @ load result
1368 veor @XMM[0], @XMM[0], @XMM[4] @ ^= IV
1369 vmov @XMM[15], @XMM[5] @ @XMM[5] holds input
1370 vst1.8 {@XMM[0]}, [$rounds] @ write output
1373 #ifndef BSAES_ASM_EXTENDED_KEY
1376 .Lcbc_dec_bzero: @ wipe key schedule [if any]
1377 vstmia $keysched!, {q0-q1}
1383 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1384 vst1.8 {@XMM[15]}, [$ivp] @ return IV
1386 ldmia sp!, {r4-r10, pc}
1387 .size bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
1391 my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1392 my $const = "r6"; # shared with _bsaes_encrypt8_alt
1393 my $keysched = "sp";
1397 .global bsaes_ctr32_encrypt_blocks
1398 .type bsaes_ctr32_encrypt_blocks,%function
1400 bsaes_ctr32_encrypt_blocks:
1401 cmp $len, #8 @ use plain AES for
1402 blo .Lctr_enc_short @ small sizes
1405 stmdb sp!, {r4-r10, lr}
1407 ldr $ctr, [ip] @ ctr is 1st arg on the stack
1408 sub sp, sp, #0x10 @ scratch space to carry over the ctr
1409 mov $fp, sp @ save sp
1411 ldr $rounds, [$key, #240] @ get # of rounds
1412 #ifndef BSAES_ASM_EXTENDED_KEY
1413 @ allocate the key schedule on the stack
1414 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1415 add r12, #`128-32` @ size of bit-sliced key schedule
1417 @ populate the key schedule
1418 mov r4, $key @ pass key
1419 mov r5, $rounds @ pass # of rounds
1420 mov sp, r12 @ sp is $keysched
1421 bl _bsaes_key_convert
1422 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1423 vstmia r12, {@XMM[7]} @ save last round key
1425 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1427 mov $ctr, #:lower16:(.LREVM0SR-.LM0)
1428 add $ctr, $const, $ctr
1430 add $ctr, $const, #.LREVM0SR-.LM0 @ borrow $ctr
1432 vldmia $keysched, {@XMM[4]} @ load round0 key
1434 ldr r12, [$key, #244]
1438 @ populate the key schedule
1439 str r12, [$key, #244]
1440 mov r4, $key @ pass key
1441 mov r5, $rounds @ pass # of rounds
1442 add r12, $key, #248 @ pass key schedule
1443 bl _bsaes_key_convert
1444 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1445 vstmia r12, {@XMM[7]} @ save last round key
1448 0: add r12, $key, #248
1449 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1450 adrl $ctr, .LREVM0SR @ borrow $ctr
1451 vldmia r12, {@XMM[4]} @ load round0 key
1452 sub sp, #0x10 @ place for adjusted round0 key
1455 vmov.i32 @XMM[8],#1 @ compose 1<<96
1456 veor @XMM[9],@XMM[9],@XMM[9]
1457 vrev32.8 @XMM[0],@XMM[0]
1458 vext.8 @XMM[8],@XMM[9],@XMM[8],#4
1459 vrev32.8 @XMM[4],@XMM[4]
1460 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1461 vstmia $keysched, {@XMM[4]} @ save adjusted round0 key
1466 vadd.u32 @XMM[10], @XMM[8], @XMM[9] @ compose 3<<96
1467 vadd.u32 @XMM[1], @XMM[0], @XMM[8] @ +1
1468 vadd.u32 @XMM[2], @XMM[0], @XMM[9] @ +2
1469 vadd.u32 @XMM[3], @XMM[0], @XMM[10] @ +3
1470 vadd.u32 @XMM[4], @XMM[1], @XMM[10]
1471 vadd.u32 @XMM[5], @XMM[2], @XMM[10]
1472 vadd.u32 @XMM[6], @XMM[3], @XMM[10]
1473 vadd.u32 @XMM[7], @XMM[4], @XMM[10]
1474 vadd.u32 @XMM[10], @XMM[5], @XMM[10] @ next counter
1476 @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1477 @ to flip byte order in 32-bit counter
1479 vldmia $keysched, {@XMM[9]} @ load round0 key
1480 #ifndef BSAES_ASM_EXTENDED_KEY
1481 add r4, $keysched, #0x10 @ pass next round key
1483 add r4, $key, #`248+16`
1485 vldmia $ctr, {@XMM[8]} @ .LREVM0SR
1486 mov r5, $rounds @ pass rounds
1487 vstmia $fp, {@XMM[10]} @ save next counter
1489 mov $const, #:lower16:(.LREVM0SR-.LSR)
1490 sub $const, $ctr, $const
1492 sub $const, $ctr, #.LREVM0SR-.LSR @ pass constants
1495 bl _bsaes_encrypt8_alt
1498 blo .Lctr_enc_loop_done
1500 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ load input
1501 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1502 veor @XMM[0], @XMM[8]
1503 veor @XMM[1], @XMM[9]
1504 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1505 veor @XMM[4], @XMM[10]
1506 veor @XMM[6], @XMM[11]
1507 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1508 veor @XMM[3], @XMM[12]
1509 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1510 veor @XMM[7], @XMM[13]
1511 veor @XMM[2], @XMM[14]
1512 vst1.8 {@XMM[4]}, [$out]!
1513 veor @XMM[5], @XMM[15]
1514 vst1.8 {@XMM[6]}, [$out]!
1515 vmov.i32 @XMM[8], #1 @ compose 1<<96
1516 vst1.8 {@XMM[3]}, [$out]!
1517 veor @XMM[9], @XMM[9], @XMM[9]
1518 vst1.8 {@XMM[7]}, [$out]!
1519 vext.8 @XMM[8], @XMM[9], @XMM[8], #4
1520 vst1.8 {@XMM[2]}, [$out]!
1521 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1522 vst1.8 {@XMM[5]}, [$out]!
1523 vldmia $fp, {@XMM[0]} @ load counter
1529 .Lctr_enc_loop_done:
1531 vld1.8 {@XMM[8]}, [$inp]! @ load input
1532 veor @XMM[0], @XMM[8]
1533 vst1.8 {@XMM[0]}, [$out]! @ write output
1536 vld1.8 {@XMM[9]}, [$inp]!
1537 veor @XMM[1], @XMM[9]
1538 vst1.8 {@XMM[1]}, [$out]!
1540 vld1.8 {@XMM[10]}, [$inp]!
1541 veor @XMM[4], @XMM[10]
1542 vst1.8 {@XMM[4]}, [$out]!
1545 vld1.8 {@XMM[11]}, [$inp]!
1546 veor @XMM[6], @XMM[11]
1547 vst1.8 {@XMM[6]}, [$out]!
1549 vld1.8 {@XMM[12]}, [$inp]!
1550 veor @XMM[3], @XMM[12]
1551 vst1.8 {@XMM[3]}, [$out]!
1554 vld1.8 {@XMM[13]}, [$inp]!
1555 veor @XMM[7], @XMM[13]
1556 vst1.8 {@XMM[7]}, [$out]!
1558 vld1.8 {@XMM[14]}, [$inp]
1559 veor @XMM[2], @XMM[14]
1560 vst1.8 {@XMM[2]}, [$out]!
1565 #ifndef BSAES_ASM_EXTENDED_KEY
1566 .Lctr_enc_bzero: @ wipe key schedule [if any]
1567 vstmia $keysched!, {q0-q1}
1571 vstmia $keysched, {q0-q1}
1575 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1577 ldmia sp!, {r4-r10, pc} @ return
1581 ldr ip, [sp] @ ctr pointer is passed on stack
1582 stmdb sp!, {r4-r8, lr}
1584 mov r4, $inp @ copy arguments
1588 ldr r8, [ip, #12] @ load counter LSW
1589 vld1.8 {@XMM[1]}, [ip] @ load whole counter value
1594 vst1.8 {@XMM[1]}, [sp] @ copy counter value
1597 .Lctr_enc_short_loop:
1598 add r0, sp, #0x10 @ input counter value
1599 mov r1, sp @ output on the stack
1604 vld1.8 {@XMM[0]}, [r4]! @ load input
1605 vld1.8 {@XMM[1]}, [sp] @ load encrypted counter
1609 str r0, [sp, #0x1c] @ next counter value
1611 str r8, [sp, #0x1c] @ next counter value
1613 veor @XMM[0],@XMM[0],@XMM[1]
1614 vst1.8 {@XMM[0]}, [r5]! @ store output
1616 bne .Lctr_enc_short_loop
1622 ldmia sp!, {r4-r8, pc}
1623 .size bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
1627 ######################################################################
1628 # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1629 # const AES_KEY *key1, const AES_KEY *key2,
1630 # const unsigned char iv[16]);
1632 my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
1633 my $const="r6"; # returned by _bsaes_key_convert
1638 .globl bsaes_xts_encrypt
1639 .type bsaes_xts_encrypt,%function
1643 stmdb sp!, {r4-r10, lr} @ 0x20
1645 mov r6, sp @ future $fp
1652 sub r0, sp, #0x10 @ 0x10
1653 bic r0, #0xf @ align at 16 bytes
1656 #ifdef XTS_CHAIN_TWEAK
1657 ldr r0, [ip] @ pointer to input tweak
1659 @ generate initial tweak
1660 ldr r0, [ip, #4] @ iv[]
1662 ldr r2, [ip, #0] @ key2
1664 mov r0,sp @ pointer to initial tweak
1667 ldr $rounds, [$key, #240] @ get # of rounds
1669 #ifndef BSAES_ASM_EXTENDED_KEY
1670 @ allocate the key schedule on the stack
1671 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1672 @ add r12, #`128-32` @ size of bit-sliced key schedule
1673 sub r12, #`32+16` @ place for tweak[9]
1675 @ populate the key schedule
1676 mov r4, $key @ pass key
1677 mov r5, $rounds @ pass # of rounds
1679 add r12, #0x90 @ pass key schedule
1680 bl _bsaes_key_convert
1681 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1682 vstmia r12, {@XMM[7]} @ save last round key
1684 ldr r12, [$key, #244]
1688 str r12, [$key, #244]
1689 mov r4, $key @ pass key
1690 mov r5, $rounds @ pass # of rounds
1691 add r12, $key, #248 @ pass key schedule
1692 bl _bsaes_key_convert
1693 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1694 vstmia r12, {@XMM[7]}
1697 0: sub sp, #0x90 @ place for tweak[9]
1700 vld1.8 {@XMM[8]}, [r0] @ initial tweak
1701 adr $magic, .Lxts_magic
1709 vldmia $magic, {$twmask} @ load XTS magic
1710 vshr.s64 @T[0], @XMM[8], #63
1712 vand @T[0], @T[0], $twmask
1714 for($i=9;$i<16;$i++) {
1716 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1717 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1718 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1719 vshr.s64 @T[1], @XMM[$i], #63
1720 veor @XMM[$i], @XMM[$i], @T[0]
1721 vand @T[1], @T[1], $twmask
1725 $code.=<<___ if ($i>=10);
1726 vld1.8 {@XMM[$i-10]}, [$inp]!
1728 $code.=<<___ if ($i>=11);
1729 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1733 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
1734 vst1.64 {@XMM[15]}, [r0,:128]!
1735 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1736 veor @XMM[8], @XMM[8], @T[0]
1737 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1739 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1740 veor @XMM[5], @XMM[5], @XMM[13]
1741 #ifndef BSAES_ASM_EXTENDED_KEY
1742 add r4, sp, #0x90 @ pass key schedule
1744 add r4, $key, #248 @ pass key schedule
1746 veor @XMM[6], @XMM[6], @XMM[14]
1747 mov r5, $rounds @ pass rounds
1748 veor @XMM[7], @XMM[7], @XMM[15]
1753 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1754 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1755 veor @XMM[0], @XMM[0], @XMM[ 8]
1756 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1757 veor @XMM[1], @XMM[1], @XMM[ 9]
1758 veor @XMM[8], @XMM[4], @XMM[10]
1759 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1760 veor @XMM[9], @XMM[6], @XMM[11]
1761 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
1762 veor @XMM[10], @XMM[3], @XMM[12]
1763 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1764 veor @XMM[11], @XMM[7], @XMM[13]
1765 veor @XMM[12], @XMM[2], @XMM[14]
1766 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1767 veor @XMM[13], @XMM[5], @XMM[15]
1768 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
1770 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1779 vldmia $magic, {$twmask} @ load XTS magic
1780 vshr.s64 @T[0], @XMM[8], #63
1782 vand @T[0], @T[0], $twmask
1784 for($i=9;$i<16;$i++) {
1786 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1787 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1788 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1789 vshr.s64 @T[1], @XMM[$i], #63
1790 veor @XMM[$i], @XMM[$i], @T[0]
1791 vand @T[1], @T[1], $twmask
1795 $code.=<<___ if ($i>=10);
1796 vld1.8 {@XMM[$i-10]}, [$inp]!
1798 bmi .Lxts_enc_`$i-9`
1800 $code.=<<___ if ($i>=11);
1801 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1806 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
1808 vld1.8 {@XMM[6]}, [$inp]!
1809 veor @XMM[5], @XMM[5], @XMM[13]
1810 #ifndef BSAES_ASM_EXTENDED_KEY
1811 add r4, sp, #0x90 @ pass key schedule
1813 add r4, $key, #248 @ pass key schedule
1815 veor @XMM[6], @XMM[6], @XMM[14]
1816 mov r5, $rounds @ pass rounds
1821 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1822 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1823 veor @XMM[0], @XMM[0], @XMM[ 8]
1824 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1825 veor @XMM[1], @XMM[1], @XMM[ 9]
1826 veor @XMM[8], @XMM[4], @XMM[10]
1827 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1828 veor @XMM[9], @XMM[6], @XMM[11]
1829 vld1.64 {@XMM[14]}, [r0,:128]!
1830 veor @XMM[10], @XMM[3], @XMM[12]
1831 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1832 veor @XMM[11], @XMM[7], @XMM[13]
1833 veor @XMM[12], @XMM[2], @XMM[14]
1834 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1835 vst1.8 {@XMM[12]}, [$out]!
1837 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1841 veor @XMM[4], @XMM[4], @XMM[12]
1842 #ifndef BSAES_ASM_EXTENDED_KEY
1843 add r4, sp, #0x90 @ pass key schedule
1845 add r4, $key, #248 @ pass key schedule
1847 veor @XMM[5], @XMM[5], @XMM[13]
1848 mov r5, $rounds @ pass rounds
1853 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1854 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1855 veor @XMM[0], @XMM[0], @XMM[ 8]
1856 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1857 veor @XMM[1], @XMM[1], @XMM[ 9]
1858 veor @XMM[8], @XMM[4], @XMM[10]
1859 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1860 veor @XMM[9], @XMM[6], @XMM[11]
1861 veor @XMM[10], @XMM[3], @XMM[12]
1862 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1863 veor @XMM[11], @XMM[7], @XMM[13]
1864 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1866 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1869 @ put this in range for both ARM and Thumb mode adr instructions
1876 veor @XMM[3], @XMM[3], @XMM[11]
1877 #ifndef BSAES_ASM_EXTENDED_KEY
1878 add r4, sp, #0x90 @ pass key schedule
1880 add r4, $key, #248 @ pass key schedule
1882 veor @XMM[4], @XMM[4], @XMM[12]
1883 mov r5, $rounds @ pass rounds
1888 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1889 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1890 veor @XMM[0], @XMM[0], @XMM[ 8]
1891 vld1.64 {@XMM[12]}, [r0,:128]!
1892 veor @XMM[1], @XMM[1], @XMM[ 9]
1893 veor @XMM[8], @XMM[4], @XMM[10]
1894 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1895 veor @XMM[9], @XMM[6], @XMM[11]
1896 veor @XMM[10], @XMM[3], @XMM[12]
1897 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1898 vst1.8 {@XMM[10]}, [$out]!
1900 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1904 veor @XMM[2], @XMM[2], @XMM[10]
1905 #ifndef BSAES_ASM_EXTENDED_KEY
1906 add r4, sp, #0x90 @ pass key schedule
1908 add r4, $key, #248 @ pass key schedule
1910 veor @XMM[3], @XMM[3], @XMM[11]
1911 mov r5, $rounds @ pass rounds
1916 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1917 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1918 veor @XMM[0], @XMM[0], @XMM[ 8]
1919 veor @XMM[1], @XMM[1], @XMM[ 9]
1920 veor @XMM[8], @XMM[4], @XMM[10]
1921 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1922 veor @XMM[9], @XMM[6], @XMM[11]
1923 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1925 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1929 veor @XMM[1], @XMM[1], @XMM[9]
1930 #ifndef BSAES_ASM_EXTENDED_KEY
1931 add r4, sp, #0x90 @ pass key schedule
1933 add r4, $key, #248 @ pass key schedule
1935 veor @XMM[2], @XMM[2], @XMM[10]
1936 mov r5, $rounds @ pass rounds
1941 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1942 vld1.64 {@XMM[10]}, [r0,:128]!
1943 veor @XMM[0], @XMM[0], @XMM[ 8]
1944 veor @XMM[1], @XMM[1], @XMM[ 9]
1945 veor @XMM[8], @XMM[4], @XMM[10]
1946 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1947 vst1.8 {@XMM[8]}, [$out]!
1949 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1953 veor @XMM[0], @XMM[0], @XMM[8]
1954 #ifndef BSAES_ASM_EXTENDED_KEY
1955 add r4, sp, #0x90 @ pass key schedule
1957 add r4, $key, #248 @ pass key schedule
1959 veor @XMM[1], @XMM[1], @XMM[9]
1960 mov r5, $rounds @ pass rounds
1965 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1966 veor @XMM[0], @XMM[0], @XMM[ 8]
1967 veor @XMM[1], @XMM[1], @XMM[ 9]
1968 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1970 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1975 veor @XMM[0], @XMM[0], @XMM[8]
1977 vst1.8 {@XMM[0]}, [sp,:128]
1979 mov r4, $fp @ preserve fp
1983 vld1.8 {@XMM[0]}, [sp,:128]
1984 veor @XMM[0], @XMM[0], @XMM[8]
1985 vst1.8 {@XMM[0]}, [$out]!
1988 vmov @XMM[8], @XMM[9] @ next round tweak
1991 #ifndef XTS_CHAIN_TWEAK
1998 ldrb r1, [$out, #-0x10]
1999 strb r0, [$out, #-0x10]
2005 vld1.8 {@XMM[0]}, [r6]
2007 veor @XMM[0], @XMM[0], @XMM[8]
2009 vst1.8 {@XMM[0]}, [sp,:128]
2011 mov r4, $fp @ preserve fp
2015 vld1.8 {@XMM[0]}, [sp,:128]
2016 veor @XMM[0], @XMM[0], @XMM[8]
2017 vst1.8 {@XMM[0]}, [r6]
2025 #ifdef XTS_CHAIN_TWEAK
2026 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2028 .Lxts_enc_bzero: @ wipe key schedule [if any]
2034 #ifdef XTS_CHAIN_TWEAK
2035 vst1.8 {@XMM[8]}, [r1]
2038 ldmia sp!, {r4-r10, pc} @ return
2040 .size bsaes_xts_encrypt,.-bsaes_xts_encrypt
2042 .globl bsaes_xts_decrypt
2043 .type bsaes_xts_decrypt,%function
2047 stmdb sp!, {r4-r10, lr} @ 0x20
2049 mov r6, sp @ future $fp
2056 sub r0, sp, #0x10 @ 0x10
2057 bic r0, #0xf @ align at 16 bytes
2060 #ifdef XTS_CHAIN_TWEAK
2061 ldr r0, [ip] @ pointer to input tweak
2063 @ generate initial tweak
2064 ldr r0, [ip, #4] @ iv[]
2066 ldr r2, [ip, #0] @ key2
2068 mov r0, sp @ pointer to initial tweak
2071 ldr $rounds, [$key, #240] @ get # of rounds
2073 #ifndef BSAES_ASM_EXTENDED_KEY
2074 @ allocate the key schedule on the stack
2075 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
2076 @ add r12, #`128-32` @ size of bit-sliced key schedule
2077 sub r12, #`32+16` @ place for tweak[9]
2079 @ populate the key schedule
2080 mov r4, $key @ pass key
2081 mov r5, $rounds @ pass # of rounds
2083 add r12, #0x90 @ pass key schedule
2084 bl _bsaes_key_convert
2086 vldmia r4, {@XMM[6]}
2087 vstmia r12, {@XMM[15]} @ save last round key
2088 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2089 vstmia r4, {@XMM[7]}
2091 ldr r12, [$key, #244]
2095 str r12, [$key, #244]
2096 mov r4, $key @ pass key
2097 mov r5, $rounds @ pass # of rounds
2098 add r12, $key, #248 @ pass key schedule
2099 bl _bsaes_key_convert
2101 vldmia r4, {@XMM[6]}
2102 vstmia r12, {@XMM[15]} @ save last round key
2103 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2104 vstmia r4, {@XMM[7]}
2107 0: sub sp, #0x90 @ place for tweak[9]
2109 vld1.8 {@XMM[8]}, [r0] @ initial tweak
2110 adr $magic, .Lxts_magic
2112 #ifndef XTS_CHAIN_TWEAK
2113 tst $len, #0xf @ if not multiple of 16
2114 it ne @ Thumb2 thing, sanity check in ARM
2115 subne $len, #0x10 @ subtract another 16 bytes
2124 vldmia $magic, {$twmask} @ load XTS magic
2125 vshr.s64 @T[0], @XMM[8], #63
2127 vand @T[0], @T[0], $twmask
2129 for($i=9;$i<16;$i++) {
2131 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2132 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2133 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2134 vshr.s64 @T[1], @XMM[$i], #63
2135 veor @XMM[$i], @XMM[$i], @T[0]
2136 vand @T[1], @T[1], $twmask
2140 $code.=<<___ if ($i>=10);
2141 vld1.8 {@XMM[$i-10]}, [$inp]!
2143 $code.=<<___ if ($i>=11);
2144 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2148 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
2149 vst1.64 {@XMM[15]}, [r0,:128]!
2150 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2151 veor @XMM[8], @XMM[8], @T[0]
2152 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2154 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
2155 veor @XMM[5], @XMM[5], @XMM[13]
2156 #ifndef BSAES_ASM_EXTENDED_KEY
2157 add r4, sp, #0x90 @ pass key schedule
2159 add r4, $key, #248 @ pass key schedule
2161 veor @XMM[6], @XMM[6], @XMM[14]
2162 mov r5, $rounds @ pass rounds
2163 veor @XMM[7], @XMM[7], @XMM[15]
2168 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2169 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2170 veor @XMM[0], @XMM[0], @XMM[ 8]
2171 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2172 veor @XMM[1], @XMM[1], @XMM[ 9]
2173 veor @XMM[8], @XMM[6], @XMM[10]
2174 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2175 veor @XMM[9], @XMM[4], @XMM[11]
2176 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
2177 veor @XMM[10], @XMM[2], @XMM[12]
2178 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2179 veor @XMM[11], @XMM[7], @XMM[13]
2180 veor @XMM[12], @XMM[3], @XMM[14]
2181 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2182 veor @XMM[13], @XMM[5], @XMM[15]
2183 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
2185 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2194 vldmia $magic, {$twmask} @ load XTS magic
2195 vshr.s64 @T[0], @XMM[8], #63
2197 vand @T[0], @T[0], $twmask
2199 for($i=9;$i<16;$i++) {
2201 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2202 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2203 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2204 vshr.s64 @T[1], @XMM[$i], #63
2205 veor @XMM[$i], @XMM[$i], @T[0]
2206 vand @T[1], @T[1], $twmask
2210 $code.=<<___ if ($i>=10);
2211 vld1.8 {@XMM[$i-10]}, [$inp]!
2213 bmi .Lxts_dec_`$i-9`
2215 $code.=<<___ if ($i>=11);
2216 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2221 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
2223 vld1.8 {@XMM[6]}, [$inp]!
2224 veor @XMM[5], @XMM[5], @XMM[13]
2225 #ifndef BSAES_ASM_EXTENDED_KEY
2226 add r4, sp, #0x90 @ pass key schedule
2228 add r4, $key, #248 @ pass key schedule
2230 veor @XMM[6], @XMM[6], @XMM[14]
2231 mov r5, $rounds @ pass rounds
2236 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2237 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2238 veor @XMM[0], @XMM[0], @XMM[ 8]
2239 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2240 veor @XMM[1], @XMM[1], @XMM[ 9]
2241 veor @XMM[8], @XMM[6], @XMM[10]
2242 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2243 veor @XMM[9], @XMM[4], @XMM[11]
2244 vld1.64 {@XMM[14]}, [r0,:128]!
2245 veor @XMM[10], @XMM[2], @XMM[12]
2246 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2247 veor @XMM[11], @XMM[7], @XMM[13]
2248 veor @XMM[12], @XMM[3], @XMM[14]
2249 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2250 vst1.8 {@XMM[12]}, [$out]!
2252 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2256 vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
2258 veor @XMM[4], @XMM[4], @XMM[12]
2259 #ifndef BSAES_ASM_EXTENDED_KEY
2260 add r4, sp, #0x90 @ pass key schedule
2262 add r4, $key, #248 @ pass key schedule
2264 veor @XMM[5], @XMM[5], @XMM[13]
2265 mov r5, $rounds @ pass rounds
2270 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2271 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2272 veor @XMM[0], @XMM[0], @XMM[ 8]
2273 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2274 veor @XMM[1], @XMM[1], @XMM[ 9]
2275 veor @XMM[8], @XMM[6], @XMM[10]
2276 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2277 veor @XMM[9], @XMM[4], @XMM[11]
2278 veor @XMM[10], @XMM[2], @XMM[12]
2279 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2280 veor @XMM[11], @XMM[7], @XMM[13]
2281 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2283 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2287 veor @XMM[3], @XMM[3], @XMM[11]
2288 #ifndef BSAES_ASM_EXTENDED_KEY
2289 add r4, sp, #0x90 @ pass key schedule
2291 add r4, $key, #248 @ pass key schedule
2293 veor @XMM[4], @XMM[4], @XMM[12]
2294 mov r5, $rounds @ pass rounds
2299 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2300 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2301 veor @XMM[0], @XMM[0], @XMM[ 8]
2302 vld1.64 {@XMM[12]}, [r0,:128]!
2303 veor @XMM[1], @XMM[1], @XMM[ 9]
2304 veor @XMM[8], @XMM[6], @XMM[10]
2305 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2306 veor @XMM[9], @XMM[4], @XMM[11]
2307 veor @XMM[10], @XMM[2], @XMM[12]
2308 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2309 vst1.8 {@XMM[10]}, [$out]!
2311 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2315 veor @XMM[2], @XMM[2], @XMM[10]
2316 #ifndef BSAES_ASM_EXTENDED_KEY
2317 add r4, sp, #0x90 @ pass key schedule
2319 add r4, $key, #248 @ pass key schedule
2321 veor @XMM[3], @XMM[3], @XMM[11]
2322 mov r5, $rounds @ pass rounds
2327 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2328 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2329 veor @XMM[0], @XMM[0], @XMM[ 8]
2330 veor @XMM[1], @XMM[1], @XMM[ 9]
2331 veor @XMM[8], @XMM[6], @XMM[10]
2332 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2333 veor @XMM[9], @XMM[4], @XMM[11]
2334 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2336 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2340 veor @XMM[1], @XMM[1], @XMM[9]
2341 #ifndef BSAES_ASM_EXTENDED_KEY
2342 add r4, sp, #0x90 @ pass key schedule
2344 add r4, $key, #248 @ pass key schedule
2346 veor @XMM[2], @XMM[2], @XMM[10]
2347 mov r5, $rounds @ pass rounds
2352 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2353 vld1.64 {@XMM[10]}, [r0,:128]!
2354 veor @XMM[0], @XMM[0], @XMM[ 8]
2355 veor @XMM[1], @XMM[1], @XMM[ 9]
2356 veor @XMM[8], @XMM[6], @XMM[10]
2357 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2358 vst1.8 {@XMM[8]}, [$out]!
2360 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2364 veor @XMM[0], @XMM[0], @XMM[8]
2365 #ifndef BSAES_ASM_EXTENDED_KEY
2366 add r4, sp, #0x90 @ pass key schedule
2368 add r4, $key, #248 @ pass key schedule
2370 veor @XMM[1], @XMM[1], @XMM[9]
2371 mov r5, $rounds @ pass rounds
2376 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2377 veor @XMM[0], @XMM[0], @XMM[ 8]
2378 veor @XMM[1], @XMM[1], @XMM[ 9]
2379 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2381 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2386 veor @XMM[0], @XMM[0], @XMM[8]
2388 vst1.8 {@XMM[0]}, [sp,:128]
2389 mov r5, $magic @ preserve magic
2391 mov r4, $fp @ preserve fp
2395 vld1.8 {@XMM[0]}, [sp,:128]
2396 veor @XMM[0], @XMM[0], @XMM[8]
2397 vst1.8 {@XMM[0]}, [$out]!
2401 vmov @XMM[8], @XMM[9] @ next round tweak
2404 #ifndef XTS_CHAIN_TWEAK
2408 @ calculate one round of extra tweak for the stolen ciphertext
2409 vldmia $magic, {$twmask}
2410 vshr.s64 @XMM[6], @XMM[8], #63
2411 vand @XMM[6], @XMM[6], $twmask
2412 vadd.u64 @XMM[9], @XMM[8], @XMM[8]
2413 vswp `&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
2414 veor @XMM[9], @XMM[9], @XMM[6]
2416 @ perform the final decryption with the last tweak value
2417 vld1.8 {@XMM[0]}, [$inp]!
2419 veor @XMM[0], @XMM[0], @XMM[9]
2421 vst1.8 {@XMM[0]}, [sp,:128]
2423 mov r4, $fp @ preserve fp
2427 vld1.8 {@XMM[0]}, [sp,:128]
2428 veor @XMM[0], @XMM[0], @XMM[9]
2429 vst1.8 {@XMM[0]}, [$out]
2435 strb r1, [$out, #0x10]
2441 vld1.8 {@XMM[0]}, [r6]
2443 veor @XMM[0], @XMM[8]
2445 vst1.8 {@XMM[0]}, [sp,:128]
2450 vld1.8 {@XMM[0]}, [sp,:128]
2451 veor @XMM[0], @XMM[0], @XMM[8]
2452 vst1.8 {@XMM[0]}, [r6]
2460 #ifdef XTS_CHAIN_TWEAK
2461 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2463 .Lxts_dec_bzero: @ wipe key schedule [if any]
2469 #ifdef XTS_CHAIN_TWEAK
2470 vst1.8 {@XMM[8]}, [r1]
2473 ldmia sp!, {r4-r10, pc} @ return
2475 .size bsaes_xts_decrypt,.-bsaes_xts_decrypt
2482 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
2487 last if (!s/^#/@/ and !/^$/);
2494 close STDOUT or die "error closing STDOUT: $!";