3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
9 # Specific modes and adaptation for Linux kernel by Ard Biesheuvel
10 # <ard.biesheuvel@linaro.org>. Permission to use under GPL terms is
12 # ====================================================================
14 # Bit-sliced AES for ARM NEON
18 # This implementation is direct adaptation of bsaes-x86_64 module for
19 # ARM NEON. Except that this module is endian-neutral [in sense that
20 # it can be compiled for either endianness] by courtesy of vld1.8's
21 # neutrality. Initial version doesn't implement interface to OpenSSL,
22 # only low-level primitives and unsupported entry points, just enough
23 # to collect performance results, which for Cortex-A8 core are:
25 # encrypt 19.5 cycles per byte processed with 128-bit key
26 # decrypt 22.1 cycles per byte processed with 128-bit key
27 # key conv. 440 cycles per 128-bit key/0.18 of 8x block
29 # Snapdragon S4 encrypts byte in 17.6 cycles and decrypts in 19.7,
30 # which is [much] worse than anticipated (for further details see
31 # http://www.openssl.org/~appro/Snapdragon-S4.html).
33 # Cortex-A15 manages in 14.2/16.1 cycles [when integer-only code
34 # manages in 20.0 cycles].
36 # When comparing to x86_64 results keep in mind that NEON unit is
37 # [mostly] single-issue and thus can't [fully] benefit from
38 # instruction-level parallelism. And when comparing to aes-armv4
39 # results keep in mind key schedule conversion overhead (see
40 # bsaes-x86_64.pl for further details)...
46 # Add CBC, CTR and XTS subroutines, adapt for kernel use.
48 # <ard.biesheuvel@linaro.org>
51 if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
52 else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
54 if ($flavour && $flavour ne "void") {
55 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
56 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
57 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
58 die "can't locate arm-xlate.pl";
60 open STDOUT,"| \"$^X\" $xlate $flavour $output";
62 open STDOUT,">$output";
65 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
66 my @XMM=map("q$_",(0..15));
69 my ($key,$rounds,$const)=("r4","r5","r6");
71 sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
72 sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
75 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
76 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
81 &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
82 &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
86 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
87 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
90 veor @b[2], @b[2], @b[1]
91 veor @b[5], @b[5], @b[6]
92 veor @b[3], @b[3], @b[0]
93 veor @b[6], @b[6], @b[2]
94 veor @b[5], @b[5], @b[0]
96 veor @b[6], @b[6], @b[3]
97 veor @b[3], @b[3], @b[7]
98 veor @b[7], @b[7], @b[5]
99 veor @b[3], @b[3], @b[4]
100 veor @b[4], @b[4], @b[5]
102 veor @b[2], @b[2], @b[7]
103 veor @b[3], @b[3], @b[1]
104 veor @b[1], @b[1], @b[5]
109 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
110 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
113 veor @b[0], @b[0], @b[6]
114 veor @b[1], @b[1], @b[4]
115 veor @b[4], @b[4], @b[6]
116 veor @b[2], @b[2], @b[0]
117 veor @b[6], @b[6], @b[1]
119 veor @b[1], @b[1], @b[5]
120 veor @b[5], @b[5], @b[3]
121 veor @b[3], @b[3], @b[7]
122 veor @b[7], @b[7], @b[5]
123 veor @b[2], @b[2], @b[5]
125 veor @b[4], @b[4], @b[7]
130 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
131 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
135 &InvInBasisChange (@b);
136 &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
137 &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
140 sub InvInBasisChange { # OutBasisChange in reverse (with twist)
141 my @b=@_[5,1,2,6,3,7,0,4];
143 veor @b[1], @b[1], @b[7]
144 veor @b[4], @b[4], @b[7]
146 veor @b[7], @b[7], @b[5]
147 veor @b[1], @b[1], @b[3]
148 veor @b[2], @b[2], @b[5]
149 veor @b[3], @b[3], @b[7]
151 veor @b[6], @b[6], @b[1]
152 veor @b[2], @b[2], @b[0]
153 veor @b[5], @b[5], @b[3]
154 veor @b[4], @b[4], @b[6]
155 veor @b[0], @b[0], @b[6]
156 veor @b[1], @b[1], @b[4]
160 sub InvOutBasisChange { # InBasisChange in reverse
161 my @b=@_[2,5,7,3,6,1,0,4];
163 veor @b[1], @b[1], @b[5]
164 veor @b[2], @b[2], @b[7]
166 veor @b[3], @b[3], @b[1]
167 veor @b[4], @b[4], @b[5]
168 veor @b[7], @b[7], @b[5]
169 veor @b[3], @b[3], @b[4]
170 veor @b[5], @b[5], @b[0]
171 veor @b[3], @b[3], @b[7]
172 veor @b[6], @b[6], @b[2]
173 veor @b[2], @b[2], @b[1]
174 veor @b[6], @b[6], @b[3]
176 veor @b[3], @b[3], @b[0]
177 veor @b[5], @b[5], @b[6]
182 #;*************************************************************
183 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
184 #;*************************************************************
185 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
197 sub Mul_GF4_N { # not used, see next subroutine
198 # multiply and scale by N
199 my ($x0,$x1,$y0,$y1,$t0)=@_;
212 # interleaved Mul_GF4_N and Mul_GF4
213 my ($x0,$x1,$y0,$y1,$t0,
214 $x2,$x3,$y2,$y3,$t1)=@_;
237 veor @t[0], @x[0], @x[2]
238 veor @t[1], @x[1], @x[3]
240 &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
242 veor @y[0], @y[0], @y[2]
243 veor @y[1], @y[1], @y[3]
245 Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
246 @x[2], @x[3], @y[2], @y[3], @t[2]);
248 veor @x[0], @x[0], @t[0]
249 veor @x[2], @x[2], @t[0]
250 veor @x[1], @x[1], @t[1]
251 veor @x[3], @x[3], @t[1]
253 veor @t[0], @x[4], @x[6]
254 veor @t[1], @x[5], @x[7]
256 &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
257 @x[6], @x[7], @y[2], @y[3], @t[2]);
259 veor @y[0], @y[0], @y[2]
260 veor @y[1], @y[1], @y[3]
262 &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
264 veor @x[4], @x[4], @t[0]
265 veor @x[6], @x[6], @t[0]
266 veor @x[5], @x[5], @t[1]
267 veor @x[7], @x[7], @t[1]
271 #;********************************************************************
272 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
273 #;********************************************************************
277 # direct optimizations from hardware
279 veor @t[3], @x[4], @x[6]
280 veor @t[2], @x[5], @x[7]
281 veor @t[1], @x[1], @x[3]
282 veor @s[1], @x[7], @x[6]
284 veor @s[0], @x[0], @x[2]
286 vorr @t[2], @t[2], @t[1]
287 veor @s[3], @t[3], @t[0]
288 vand @s[2], @t[3], @s[0]
289 vorr @t[3], @t[3], @s[0]
290 veor @s[0], @s[0], @t[1]
291 vand @t[0], @t[0], @t[1]
292 veor @t[1], @x[3], @x[2]
293 vand @s[3], @s[3], @s[0]
294 vand @s[1], @s[1], @t[1]
295 veor @t[1], @x[4], @x[5]
296 veor @s[0], @x[1], @x[0]
297 veor @t[3], @t[3], @s[1]
298 veor @t[2], @t[2], @s[1]
299 vand @s[1], @t[1], @s[0]
300 vorr @t[1], @t[1], @s[0]
301 veor @t[3], @t[3], @s[3]
302 veor @t[0], @t[0], @s[1]
303 veor @t[2], @t[2], @s[2]
304 veor @t[1], @t[1], @s[3]
305 veor @t[0], @t[0], @s[2]
306 vand @s[0], @x[7], @x[3]
307 veor @t[1], @t[1], @s[2]
308 vand @s[1], @x[6], @x[2]
309 vand @s[2], @x[5], @x[1]
310 vorr @s[3], @x[4], @x[0]
311 veor @t[3], @t[3], @s[0]
312 veor @t[1], @t[1], @s[2]
313 veor @t[0], @t[0], @s[3]
314 veor @t[2], @t[2], @s[1]
316 @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
318 @ new smaller inversion
320 vand @s[2], @t[3], @t[1]
323 veor @s[1], @t[2], @s[2]
324 veor @s[3], @t[0], @s[2]
325 veor @s[2], @t[0], @s[2] @ @s[2]=@s[3]
327 vbsl @s[1], @t[1], @t[0]
328 vbsl @s[3], @t[3], @t[2]
329 veor @t[3], @t[3], @t[2]
331 vbsl @s[0], @s[1], @s[2]
332 vbsl @t[0], @s[2], @s[1]
334 vand @s[2], @s[0], @s[3]
335 veor @t[1], @t[1], @t[0]
337 veor @s[2], @s[2], @t[3]
339 # output in s3, s2, s1, t1
341 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
343 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
344 &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
346 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
349 # AES linear components
356 vldmia $key!, {@t[0]-@t[3]}
357 veor @t[0], @t[0], @x[0]
358 veor @t[1], @t[1], @x[1]
359 vtbl.8 `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
360 vtbl.8 `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
361 vldmia $key!, {@t[0]}
362 veor @t[2], @t[2], @x[2]
363 vtbl.8 `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
364 vtbl.8 `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
365 vldmia $key!, {@t[1]}
366 veor @t[3], @t[3], @x[3]
367 vtbl.8 `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
368 vtbl.8 `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
369 vldmia $key!, {@t[2]}
370 vtbl.8 `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
371 vtbl.8 `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
372 vldmia $key!, {@t[3]}
373 veor @t[0], @t[0], @x[4]
374 veor @t[1], @t[1], @x[5]
375 vtbl.8 `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
376 vtbl.8 `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
377 veor @t[2], @t[2], @x[6]
378 vtbl.8 `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
379 vtbl.8 `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
380 veor @t[3], @t[3], @x[7]
381 vtbl.8 `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
382 vtbl.8 `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
383 vtbl.8 `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
384 vtbl.8 `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
389 # modified to emit output in order suitable for feeding back to aesenc[last]
392 my $inv=@_[16]; # optional
394 vext.8 @t[0], @x[0], @x[0], #12 @ x0 <<< 32
395 vext.8 @t[1], @x[1], @x[1], #12
396 veor @x[0], @x[0], @t[0] @ x0 ^ (x0 <<< 32)
397 vext.8 @t[2], @x[2], @x[2], #12
398 veor @x[1], @x[1], @t[1]
399 vext.8 @t[3], @x[3], @x[3], #12
400 veor @x[2], @x[2], @t[2]
401 vext.8 @t[4], @x[4], @x[4], #12
402 veor @x[3], @x[3], @t[3]
403 vext.8 @t[5], @x[5], @x[5], #12
404 veor @x[4], @x[4], @t[4]
405 vext.8 @t[6], @x[6], @x[6], #12
406 veor @x[5], @x[5], @t[5]
407 vext.8 @t[7], @x[7], @x[7], #12
408 veor @x[6], @x[6], @t[6]
410 veor @t[1], @t[1], @x[0]
411 veor @x[7], @x[7], @t[7]
412 vext.8 @x[0], @x[0], @x[0], #8 @ (x0 ^ (x0 <<< 32)) <<< 64)
413 veor @t[2], @t[2], @x[1]
414 veor @t[0], @t[0], @x[7]
415 veor @t[1], @t[1], @x[7]
416 vext.8 @x[1], @x[1], @x[1], #8
417 veor @t[5], @t[5], @x[4]
418 veor @x[0], @x[0], @t[0]
419 veor @t[6], @t[6], @x[5]
420 veor @x[1], @x[1], @t[1]
421 vext.8 @t[0], @x[4], @x[4], #8
422 veor @t[4], @t[4], @x[3]
423 vext.8 @t[1], @x[5], @x[5], #8
424 veor @t[7], @t[7], @x[6]
425 vext.8 @x[4], @x[3], @x[3], #8
426 veor @t[3], @t[3], @x[2]
427 vext.8 @x[5], @x[7], @x[7], #8
428 veor @t[4], @t[4], @x[7]
429 vext.8 @x[3], @x[6], @x[6], #8
430 veor @t[3], @t[3], @x[7]
431 vext.8 @x[6], @x[2], @x[2], #8
432 veor @x[7], @t[1], @t[5]
434 $code.=<<___ if (!$inv);
435 veor @x[2], @t[0], @t[4]
436 veor @x[4], @x[4], @t[3]
437 veor @x[5], @x[5], @t[7]
438 veor @x[3], @x[3], @t[6]
440 veor @x[6], @x[6], @t[2]
443 $code.=<<___ if ($inv);
444 veor @t[3], @t[3], @x[4]
445 veor @x[5], @x[5], @t[7]
446 veor @x[2], @x[3], @t[6]
447 veor @x[3], @t[0], @t[4]
448 veor @x[4], @x[6], @t[2]
454 sub InvMixColumns_orig {
459 @ multiplication by 0x0e
460 vext.8 @t[7], @x[7], @x[7], #12
462 veor @x[2], @x[2], @x[5] @ 2 5
463 veor @x[7], @x[7], @x[5] @ 7 5
464 vext.8 @t[0], @x[0], @x[0], #12
466 veor @x[5], @x[5], @x[0] @ 5 0 [1]
467 veor @x[0], @x[0], @x[1] @ 0 1
468 vext.8 @t[1], @x[1], @x[1], #12
469 veor @x[1], @x[1], @x[2] @ 1 25
470 veor @x[0], @x[0], @x[6] @ 01 6 [2]
471 vext.8 @t[3], @x[3], @x[3], #12
472 veor @x[1], @x[1], @x[3] @ 125 3 [4]
473 veor @x[2], @x[2], @x[0] @ 25 016 [3]
474 veor @x[3], @x[3], @x[7] @ 3 75
475 veor @x[7], @x[7], @x[6] @ 75 6 [0]
476 vext.8 @t[6], @x[6], @x[6], #12
478 veor @x[6], @x[6], @x[4] @ 6 4
479 veor @x[4], @x[4], @x[3] @ 4 375 [6]
480 veor @x[3], @x[3], @x[7] @ 375 756=36
481 veor @x[6], @x[6], @t[5] @ 64 5 [7]
482 veor @x[3], @x[3], @t[2] @ 36 2
483 vext.8 @t[5], @t[5], @t[5], #12
484 veor @x[3], @x[3], @t[4] @ 362 4 [5]
486 my @y = @x[7,5,0,2,1,3,4,6];
488 @ multiplication by 0x0b
489 veor @y[1], @y[1], @y[0]
490 veor @y[0], @y[0], @t[0]
491 vext.8 @t[2], @t[2], @t[2], #12
492 veor @y[1], @y[1], @t[1]
493 veor @y[0], @y[0], @t[5]
494 vext.8 @t[4], @t[4], @t[4], #12
495 veor @y[1], @y[1], @t[6]
496 veor @y[0], @y[0], @t[7]
497 veor @t[7], @t[7], @t[6] @ clobber t[7]
499 veor @y[3], @y[3], @t[0]
500 veor @y[1], @y[1], @y[0]
501 vext.8 @t[0], @t[0], @t[0], #12
502 veor @y[2], @y[2], @t[1]
503 veor @y[4], @y[4], @t[1]
504 vext.8 @t[1], @t[1], @t[1], #12
505 veor @y[2], @y[2], @t[2]
506 veor @y[3], @y[3], @t[2]
507 veor @y[5], @y[5], @t[2]
508 veor @y[2], @y[2], @t[7]
509 vext.8 @t[2], @t[2], @t[2], #12
510 veor @y[3], @y[3], @t[3]
511 veor @y[6], @y[6], @t[3]
512 veor @y[4], @y[4], @t[3]
513 veor @y[7], @y[7], @t[4]
514 vext.8 @t[3], @t[3], @t[3], #12
515 veor @y[5], @y[5], @t[4]
516 veor @y[7], @y[7], @t[7]
517 veor @t[7], @t[7], @t[5] @ clobber t[7] even more
518 veor @y[3], @y[3], @t[5]
519 veor @y[4], @y[4], @t[4]
521 veor @y[5], @y[5], @t[7]
522 vext.8 @t[4], @t[4], @t[4], #12
523 veor @y[6], @y[6], @t[7]
524 veor @y[4], @y[4], @t[7]
526 veor @t[7], @t[7], @t[5]
527 vext.8 @t[5], @t[5], @t[5], #12
529 @ multiplication by 0x0d
530 veor @y[4], @y[4], @y[7]
531 veor @t[7], @t[7], @t[6] @ restore t[7]
532 veor @y[7], @y[7], @t[4]
533 vext.8 @t[6], @t[6], @t[6], #12
534 veor @y[2], @y[2], @t[0]
535 veor @y[7], @y[7], @t[5]
536 vext.8 @t[7], @t[7], @t[7], #12
537 veor @y[2], @y[2], @t[2]
539 veor @y[3], @y[3], @y[1]
540 veor @y[1], @y[1], @t[1]
541 veor @y[0], @y[0], @t[0]
542 veor @y[3], @y[3], @t[0]
543 veor @y[1], @y[1], @t[5]
544 veor @y[0], @y[0], @t[5]
545 vext.8 @t[0], @t[0], @t[0], #12
546 veor @y[1], @y[1], @t[7]
547 veor @y[0], @y[0], @t[6]
548 veor @y[3], @y[3], @y[1]
549 veor @y[4], @y[4], @t[1]
550 vext.8 @t[1], @t[1], @t[1], #12
552 veor @y[7], @y[7], @t[7]
553 veor @y[4], @y[4], @t[2]
554 veor @y[5], @y[5], @t[2]
555 veor @y[2], @y[2], @t[6]
556 veor @t[6], @t[6], @t[3] @ clobber t[6]
557 vext.8 @t[2], @t[2], @t[2], #12
558 veor @y[4], @y[4], @y[7]
559 veor @y[3], @y[3], @t[6]
561 veor @y[6], @y[6], @t[6]
562 veor @y[5], @y[5], @t[5]
563 vext.8 @t[5], @t[5], @t[5], #12
564 veor @y[6], @y[6], @t[4]
565 vext.8 @t[4], @t[4], @t[4], #12
566 veor @y[5], @y[5], @t[6]
567 veor @y[6], @y[6], @t[7]
568 vext.8 @t[7], @t[7], @t[7], #12
569 veor @t[6], @t[6], @t[3] @ restore t[6]
570 vext.8 @t[3], @t[3], @t[3], #12
572 @ multiplication by 0x09
573 veor @y[4], @y[4], @y[1]
574 veor @t[1], @t[1], @y[1] @ t[1]=y[1]
575 veor @t[0], @t[0], @t[5] @ clobber t[0]
576 vext.8 @t[6], @t[6], @t[6], #12
577 veor @t[1], @t[1], @t[5]
578 veor @y[3], @y[3], @t[0]
579 veor @t[0], @t[0], @y[0] @ t[0]=y[0]
580 veor @t[1], @t[1], @t[6]
581 veor @t[6], @t[6], @t[7] @ clobber t[6]
582 veor @y[4], @y[4], @t[1]
583 veor @y[7], @y[7], @t[4]
584 veor @y[6], @y[6], @t[3]
585 veor @y[5], @y[5], @t[2]
586 veor @t[4], @t[4], @y[4] @ t[4]=y[4]
587 veor @t[3], @t[3], @y[3] @ t[3]=y[3]
588 veor @t[5], @t[5], @y[5] @ t[5]=y[5]
589 veor @t[2], @t[2], @y[2] @ t[2]=y[2]
590 veor @t[3], @t[3], @t[7]
591 veor @XMM[5], @t[5], @t[6]
592 veor @XMM[6], @t[6], @y[6] @ t[6]=y[6]
593 veor @XMM[2], @t[2], @t[6]
594 veor @XMM[7], @t[7], @y[7] @ t[7]=y[7]
598 @ vmov @XMM[2], @t[2]
601 @ vmov @XMM[5], @t[5]
602 @ vmov @XMM[6], @t[6]
603 @ vmov @XMM[7], @t[7]
611 # Thanks to Jussi Kivilinna for providing pointer to
613 # | 0e 0b 0d 09 | | 02 03 01 01 | | 05 00 04 00 |
614 # | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
615 # | 0d 09 0e 0b | | 01 01 02 03 | | 04 00 05 00 |
616 # | 0b 0d 09 0e | | 03 01 01 02 | | 00 04 00 05 |
619 @ multiplication by 0x05-0x00-0x04-0x00
620 vext.8 @t[0], @x[0], @x[0], #8
621 vext.8 @t[6], @x[6], @x[6], #8
622 vext.8 @t[7], @x[7], @x[7], #8
623 veor @t[0], @t[0], @x[0]
624 vext.8 @t[1], @x[1], @x[1], #8
625 veor @t[6], @t[6], @x[6]
626 vext.8 @t[2], @x[2], @x[2], #8
627 veor @t[7], @t[7], @x[7]
628 vext.8 @t[3], @x[3], @x[3], #8
629 veor @t[1], @t[1], @x[1]
630 vext.8 @t[4], @x[4], @x[4], #8
631 veor @t[2], @t[2], @x[2]
632 vext.8 @t[5], @x[5], @x[5], #8
633 veor @t[3], @t[3], @x[3]
634 veor @t[4], @t[4], @x[4]
635 veor @t[5], @t[5], @x[5]
637 veor @x[0], @x[0], @t[6]
638 veor @x[1], @x[1], @t[6]
639 veor @x[2], @x[2], @t[0]
640 veor @x[4], @x[4], @t[2]
641 veor @x[3], @x[3], @t[1]
642 veor @x[1], @x[1], @t[7]
643 veor @x[2], @x[2], @t[7]
644 veor @x[4], @x[4], @t[6]
645 veor @x[5], @x[5], @t[3]
646 veor @x[3], @x[3], @t[6]
647 veor @x[6], @x[6], @t[4]
648 veor @x[4], @x[4], @t[7]
649 veor @x[5], @x[5], @t[7]
650 veor @x[7], @x[7], @t[5]
652 &MixColumns (@x,@t,1); # flipped 2<->3 and 4<->6
656 my ($a,$b,$n,$mask,$t)=@_;
667 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
669 vshr.u64 $t0, $b0, #$n
670 vshr.u64 $t1, $b1, #$n
676 vshl.u64 $t0, $t0, #$n
678 vshl.u64 $t1, $t1, #$n
685 my @x=reverse(@_[0..7]);
686 my ($t0,$t1,$t2,$t3)=@_[8..11];
688 vmov.i8 $t0,#0x55 @ compose .LBS0
689 vmov.i8 $t1,#0x33 @ compose .LBS1
691 &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
692 &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
694 vmov.i8 $t0,#0x0f @ compose .LBS2
696 &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
697 &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
699 &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
700 &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
705 # include "arm_arch.h"
707 # define VFP_ABI_PUSH vstmdb sp!,{d8-d15}
708 # define VFP_ABI_POP vldmia sp!,{d8-d15}
709 # define VFP_ABI_FRAME 0x40
711 # define VFP_ABI_PUSH
713 # define VFP_ABI_FRAME 0
714 # define BSAES_ASM_EXTENDED_KEY
715 # define XTS_CHAIN_TWEAK
716 # define __ARM_ARCH__ __LINUX_ARM_ARCH__
717 # define __ARM_MAX_ARCH__ 7
724 #if __ARM_MAX_ARCH__>=7
729 .syntax unified @ ARMv7-capable assembler is expected to handle this
730 #if defined(__thumb2__) && !defined(__APPLE__)
737 .type _bsaes_decrypt8,%function
740 adr $const,_bsaes_decrypt8
741 vldmia $key!, {@XMM[9]} @ round 0 key
745 add $const,$const,#.LM0ISR-_bsaes_decrypt8
748 vldmia $const!, {@XMM[8]} @ .LM0ISR
749 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
750 veor @XMM[11], @XMM[1], @XMM[9]
751 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
752 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
753 veor @XMM[12], @XMM[2], @XMM[9]
754 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
755 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
756 veor @XMM[13], @XMM[3], @XMM[9]
757 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
758 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
759 veor @XMM[14], @XMM[4], @XMM[9]
760 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
761 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
762 veor @XMM[15], @XMM[5], @XMM[9]
763 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
764 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
765 veor @XMM[10], @XMM[6], @XMM[9]
766 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
767 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
768 veor @XMM[11], @XMM[7], @XMM[9]
769 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
770 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
771 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
772 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
774 &bitslice (@XMM[0..7, 8..11]);
776 sub $rounds,$rounds,#1
781 &ShiftRows (@XMM[0..7, 8..12]);
782 $code.=".Ldec_sbox:\n";
783 &InvSbox (@XMM[0..7, 8..15]);
785 subs $rounds,$rounds,#1
788 &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
790 vldmia $const, {@XMM[12]} @ .LISR
791 ite eq @ Thumb2 thing, sanity check in ARM
792 addeq $const,$const,#0x10
794 vldmia $const, {@XMM[12]} @ .LISRM0
799 &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
801 vldmia $key, {@XMM[8]} @ last round key
802 veor @XMM[6], @XMM[6], @XMM[8]
803 veor @XMM[4], @XMM[4], @XMM[8]
804 veor @XMM[2], @XMM[2], @XMM[8]
805 veor @XMM[7], @XMM[7], @XMM[8]
806 veor @XMM[3], @XMM[3], @XMM[8]
807 veor @XMM[5], @XMM[5], @XMM[8]
808 veor @XMM[0], @XMM[0], @XMM[8]
809 veor @XMM[1], @XMM[1], @XMM[8]
811 .size _bsaes_decrypt8,.-_bsaes_decrypt8
813 .type _bsaes_const,%object
816 .LM0ISR: @ InvShiftRows constants
817 .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
819 .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
821 .quad 0x01040b0e0205080f, 0x0306090c00070a0d
822 .LM0SR: @ ShiftRows constants
823 .quad 0x0a0e02060f03070b, 0x0004080c05090d01
825 .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
827 .quad 0x0304090e00050a0f, 0x01060b0c0207080d
829 .quad 0x02060a0e03070b0f, 0x0004080c0105090d
831 .quad 0x090d01050c000408, 0x03070b0f060a0e02
832 .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
834 .size _bsaes_const,.-_bsaes_const
836 .type _bsaes_encrypt8,%function
839 adr $const,_bsaes_encrypt8
840 vldmia $key!, {@XMM[9]} @ round 0 key
844 sub $const,$const,#_bsaes_encrypt8-.LM0SR
847 vldmia $const!, {@XMM[8]} @ .LM0SR
849 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
850 veor @XMM[11], @XMM[1], @XMM[9]
851 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
852 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
853 veor @XMM[12], @XMM[2], @XMM[9]
854 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
855 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
856 veor @XMM[13], @XMM[3], @XMM[9]
857 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
858 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
859 veor @XMM[14], @XMM[4], @XMM[9]
860 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
861 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
862 veor @XMM[15], @XMM[5], @XMM[9]
863 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
864 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
865 veor @XMM[10], @XMM[6], @XMM[9]
866 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
867 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
868 veor @XMM[11], @XMM[7], @XMM[9]
869 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
870 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
871 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
872 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
873 _bsaes_encrypt8_bitslice:
875 &bitslice (@XMM[0..7, 8..11]);
877 sub $rounds,$rounds,#1
882 &ShiftRows (@XMM[0..7, 8..12]);
883 $code.=".Lenc_sbox:\n";
884 &Sbox (@XMM[0..7, 8..15]);
886 subs $rounds,$rounds,#1
889 &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
891 vldmia $const, {@XMM[12]} @ .LSR
892 ite eq @ Thumb2 thing, samity check in ARM
893 addeq $const,$const,#0x10
895 vldmia $const, {@XMM[12]} @ .LSRM0
900 # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
901 &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
903 vldmia $key, {@XMM[8]} @ last round key
904 veor @XMM[4], @XMM[4], @XMM[8]
905 veor @XMM[6], @XMM[6], @XMM[8]
906 veor @XMM[3], @XMM[3], @XMM[8]
907 veor @XMM[7], @XMM[7], @XMM[8]
908 veor @XMM[2], @XMM[2], @XMM[8]
909 veor @XMM[5], @XMM[5], @XMM[8]
910 veor @XMM[0], @XMM[0], @XMM[8]
911 veor @XMM[1], @XMM[1], @XMM[8]
913 .size _bsaes_encrypt8,.-_bsaes_encrypt8
917 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
920 my @x=reverse(@_[0..7]);
921 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
923 &swapmove (@x[0,1],1,$bs0,$t2,$t3);
925 @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
929 #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
931 &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
933 @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
939 &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
940 &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
944 .type _bsaes_key_convert,%function
947 adr $const,_bsaes_key_convert
948 vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
952 sub $const,$const,#_bsaes_key_convert-.LM0
954 vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
956 vmov.i8 @XMM[8], #0x01 @ bit masks
957 vmov.i8 @XMM[9], #0x02
958 vmov.i8 @XMM[10], #0x04
959 vmov.i8 @XMM[11], #0x08
960 vmov.i8 @XMM[12], #0x10
961 vmov.i8 @XMM[13], #0x20
962 vldmia $const, {@XMM[14]} @ .LM0
965 vrev32.8 @XMM[7], @XMM[7]
966 vrev32.8 @XMM[15], @XMM[15]
968 sub $rounds,$rounds,#1
969 vstmia $out!, {@XMM[7]} @ save round 0 key
974 vtbl.8 `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
975 vtbl.8 `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
976 vmov.i8 @XMM[6], #0x40
977 vmov.i8 @XMM[15], #0x80
979 vtst.8 @XMM[0], @XMM[7], @XMM[8]
980 vtst.8 @XMM[1], @XMM[7], @XMM[9]
981 vtst.8 @XMM[2], @XMM[7], @XMM[10]
982 vtst.8 @XMM[3], @XMM[7], @XMM[11]
983 vtst.8 @XMM[4], @XMM[7], @XMM[12]
984 vtst.8 @XMM[5], @XMM[7], @XMM[13]
985 vtst.8 @XMM[6], @XMM[7], @XMM[6]
986 vtst.8 @XMM[7], @XMM[7], @XMM[15]
987 vld1.8 {@XMM[15]}, [$inp]! @ load next round key
988 vmvn @XMM[0], @XMM[0] @ "pnot"
989 vmvn @XMM[1], @XMM[1]
990 vmvn @XMM[5], @XMM[5]
991 vmvn @XMM[6], @XMM[6]
993 vrev32.8 @XMM[15], @XMM[15]
995 subs $rounds,$rounds,#1
996 vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
999 vmov.i8 @XMM[7],#0x63 @ compose .L63
1000 @ don't save last round key
1002 .size _bsaes_key_convert,.-_bsaes_key_convert
1006 if (0) { # following four functions are unsupported interface
1007 # used for benchmarking...
1009 .globl bsaes_enc_key_convert
1010 .type bsaes_enc_key_convert,%function
1012 bsaes_enc_key_convert:
1013 stmdb sp!,{r4-r6,lr}
1014 vstmdb sp!,{d8-d15} @ ABI specification says so
1016 ldr r5,[$inp,#240] @ pass rounds
1017 mov r4,$inp @ pass key
1018 mov r12,$out @ pass key schedule
1019 bl _bsaes_key_convert
1020 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1021 vstmia r12, {@XMM[7]} @ save last round key
1024 ldmia sp!,{r4-r6,pc}
1025 .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
1027 .globl bsaes_encrypt_128
1028 .type bsaes_encrypt_128,%function
1031 stmdb sp!,{r4-r6,lr}
1032 vstmdb sp!,{d8-d15} @ ABI specification says so
1034 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1035 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1036 mov r4,$key @ pass the key
1037 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1038 mov r5,#10 @ pass rounds
1039 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1043 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1044 vst1.8 {@XMM[4]}, [$out]!
1045 vst1.8 {@XMM[6]}, [$out]!
1046 vst1.8 {@XMM[3]}, [$out]!
1047 vst1.8 {@XMM[7]}, [$out]!
1048 vst1.8 {@XMM[2]}, [$out]!
1049 subs $len,$len,#0x80
1050 vst1.8 {@XMM[5]}, [$out]!
1054 ldmia sp!,{r4-r6,pc}
1055 .size bsaes_encrypt_128,.-bsaes_encrypt_128
1057 .globl bsaes_dec_key_convert
1058 .type bsaes_dec_key_convert,%function
1060 bsaes_dec_key_convert:
1061 stmdb sp!,{r4-r6,lr}
1062 vstmdb sp!,{d8-d15} @ ABI specification says so
1064 ldr r5,[$inp,#240] @ pass rounds
1065 mov r4,$inp @ pass key
1066 mov r12,$out @ pass key schedule
1067 bl _bsaes_key_convert
1068 vldmia $out, {@XMM[6]}
1069 vstmia r12, {@XMM[15]} @ save last round key
1070 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1071 vstmia $out, {@XMM[7]}
1074 ldmia sp!,{r4-r6,pc}
1075 .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
1077 .globl bsaes_decrypt_128
1078 .type bsaes_decrypt_128,%function
1081 stmdb sp!,{r4-r6,lr}
1082 vstmdb sp!,{d8-d15} @ ABI specification says so
1084 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1085 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1086 mov r4,$key @ pass the key
1087 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1088 mov r5,#10 @ pass rounds
1089 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1093 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1094 vst1.8 {@XMM[6]}, [$out]!
1095 vst1.8 {@XMM[4]}, [$out]!
1096 vst1.8 {@XMM[2]}, [$out]!
1097 vst1.8 {@XMM[7]}, [$out]!
1098 vst1.8 {@XMM[3]}, [$out]!
1099 subs $len,$len,#0x80
1100 vst1.8 {@XMM[5]}, [$out]!
1104 ldmia sp!,{r4-r6,pc}
1105 .size bsaes_decrypt_128,.-bsaes_decrypt_128
1109 my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
1110 my ($keysched)=("sp");
1113 .extern AES_cbc_encrypt
1116 .global bsaes_cbc_encrypt
1117 .type bsaes_cbc_encrypt,%function
1131 @ it is up to the caller to make sure we are called with enc == 0
1134 stmdb sp!, {r4-r10, lr}
1136 ldr $ivp, [ip] @ IV is 1st arg on the stack
1137 mov $len, $len, lsr#4 @ len in 16 byte blocks
1138 sub sp, #0x10 @ scratch space to carry over the IV
1139 mov $fp, sp @ save sp
1141 ldr $rounds, [$key, #240] @ get # of rounds
1142 #ifndef BSAES_ASM_EXTENDED_KEY
1143 @ allocate the key schedule on the stack
1144 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1145 add r12, #`128-32` @ sifze of bit-slices key schedule
1147 @ populate the key schedule
1148 mov r4, $key @ pass key
1149 mov r5, $rounds @ pass # of rounds
1150 mov sp, r12 @ sp is $keysched
1151 bl _bsaes_key_convert
1152 vldmia $keysched, {@XMM[6]}
1153 vstmia r12, {@XMM[15]} @ save last round key
1154 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1155 vstmia $keysched, {@XMM[7]}
1157 ldr r12, [$key, #244]
1161 @ populate the key schedule
1162 str r12, [$key, #244]
1163 mov r4, $key @ pass key
1164 mov r5, $rounds @ pass # of rounds
1165 add r12, $key, #248 @ pass key schedule
1166 bl _bsaes_key_convert
1168 vldmia r4, {@XMM[6]}
1169 vstmia r12, {@XMM[15]} @ save last round key
1170 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1171 vstmia r4, {@XMM[7]}
1177 vld1.8 {@XMM[15]}, [$ivp] @ load IV
1182 subs $len, $len, #0x8
1183 bmi .Lcbc_dec_loop_finish
1185 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1186 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1187 #ifndef BSAES_ASM_EXTENDED_KEY
1188 mov r4, $keysched @ pass the key
1192 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1194 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]
1195 sub $inp, $inp, #0x60
1196 vstmia $fp, {@XMM[15]} @ put aside IV
1200 vldmia $fp, {@XMM[14]} @ reload IV
1201 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1202 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1203 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1204 veor @XMM[1], @XMM[1], @XMM[8]
1205 veor @XMM[6], @XMM[6], @XMM[9]
1206 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1207 veor @XMM[4], @XMM[4], @XMM[10]
1208 veor @XMM[2], @XMM[2], @XMM[11]
1209 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1210 veor @XMM[7], @XMM[7], @XMM[12]
1211 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1212 veor @XMM[3], @XMM[3], @XMM[13]
1213 vst1.8 {@XMM[6]}, [$out]!
1214 veor @XMM[5], @XMM[5], @XMM[14]
1215 vst1.8 {@XMM[4]}, [$out]!
1216 vst1.8 {@XMM[2]}, [$out]!
1217 vst1.8 {@XMM[7]}, [$out]!
1218 vst1.8 {@XMM[3]}, [$out]!
1219 vst1.8 {@XMM[5]}, [$out]!
1223 .Lcbc_dec_loop_finish:
1227 vld1.8 {@XMM[0]}, [$inp]! @ load input
1230 vld1.8 {@XMM[1]}, [$inp]!
1231 #ifndef BSAES_ASM_EXTENDED_KEY
1232 mov r4, $keysched @ pass the key
1237 vstmia $fp, {@XMM[15]} @ put aside IV
1239 vld1.8 {@XMM[2]}, [$inp]!
1242 vld1.8 {@XMM[3]}, [$inp]!
1244 vld1.8 {@XMM[4]}, [$inp]!
1247 vld1.8 {@XMM[5]}, [$inp]!
1249 vld1.8 {@XMM[6]}, [$inp]!
1250 sub $inp, $inp, #0x70
1254 vldmia $fp, {@XMM[14]} @ reload IV
1255 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1256 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1257 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1258 veor @XMM[1], @XMM[1], @XMM[8]
1259 veor @XMM[6], @XMM[6], @XMM[9]
1260 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1261 veor @XMM[4], @XMM[4], @XMM[10]
1262 veor @XMM[2], @XMM[2], @XMM[11]
1263 vld1.8 {@XMM[15]}, [$inp]!
1264 veor @XMM[7], @XMM[7], @XMM[12]
1265 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1266 veor @XMM[3], @XMM[3], @XMM[13]
1267 vst1.8 {@XMM[6]}, [$out]!
1268 vst1.8 {@XMM[4]}, [$out]!
1269 vst1.8 {@XMM[2]}, [$out]!
1270 vst1.8 {@XMM[7]}, [$out]!
1271 vst1.8 {@XMM[3]}, [$out]!
1275 sub $inp, $inp, #0x60
1277 vldmia $fp,{@XMM[14]} @ reload IV
1278 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1279 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1280 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1281 veor @XMM[1], @XMM[1], @XMM[8]
1282 veor @XMM[6], @XMM[6], @XMM[9]
1283 vld1.8 {@XMM[12]}, [$inp]!
1284 veor @XMM[4], @XMM[4], @XMM[10]
1285 veor @XMM[2], @XMM[2], @XMM[11]
1286 vld1.8 {@XMM[15]}, [$inp]!
1287 veor @XMM[7], @XMM[7], @XMM[12]
1288 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1289 vst1.8 {@XMM[6]}, [$out]!
1290 vst1.8 {@XMM[4]}, [$out]!
1291 vst1.8 {@XMM[2]}, [$out]!
1292 vst1.8 {@XMM[7]}, [$out]!
1296 sub $inp, $inp, #0x50
1298 vldmia $fp, {@XMM[14]} @ reload IV
1299 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1300 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1301 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1302 veor @XMM[1], @XMM[1], @XMM[8]
1303 veor @XMM[6], @XMM[6], @XMM[9]
1304 vld1.8 {@XMM[15]}, [$inp]!
1305 veor @XMM[4], @XMM[4], @XMM[10]
1306 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1307 veor @XMM[2], @XMM[2], @XMM[11]
1308 vst1.8 {@XMM[6]}, [$out]!
1309 vst1.8 {@XMM[4]}, [$out]!
1310 vst1.8 {@XMM[2]}, [$out]!
1314 sub $inp, $inp, #0x40
1316 vldmia $fp, {@XMM[14]} @ reload IV
1317 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1318 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1319 vld1.8 {@XMM[10]}, [$inp]!
1320 veor @XMM[1], @XMM[1], @XMM[8]
1321 veor @XMM[6], @XMM[6], @XMM[9]
1322 vld1.8 {@XMM[15]}, [$inp]!
1323 veor @XMM[4], @XMM[4], @XMM[10]
1324 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1325 vst1.8 {@XMM[6]}, [$out]!
1326 vst1.8 {@XMM[4]}, [$out]!
1330 sub $inp, $inp, #0x30
1332 vldmia $fp, {@XMM[14]} @ reload IV
1333 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1334 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1335 vld1.8 {@XMM[15]}, [$inp]!
1336 veor @XMM[1], @XMM[1], @XMM[8]
1337 veor @XMM[6], @XMM[6], @XMM[9]
1338 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1339 vst1.8 {@XMM[6]}, [$out]!
1343 sub $inp, $inp, #0x20
1345 vldmia $fp, {@XMM[14]} @ reload IV
1346 vld1.8 {@XMM[8]}, [$inp]! @ reload input
1347 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1348 vld1.8 {@XMM[15]}, [$inp]! @ reload input
1349 veor @XMM[1], @XMM[1], @XMM[8]
1350 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1354 sub $inp, $inp, #0x10
1355 mov $rounds, $out @ save original out pointer
1356 mov $out, $fp @ use the iv scratch space as out buffer
1358 vmov @XMM[4],@XMM[15] @ just in case ensure that IV
1359 vmov @XMM[5],@XMM[0] @ and input are preserved
1361 vld1.8 {@XMM[0]}, [$fp,:64] @ load result
1362 veor @XMM[0], @XMM[0], @XMM[4] @ ^= IV
1363 vmov @XMM[15], @XMM[5] @ @XMM[5] holds input
1364 vst1.8 {@XMM[0]}, [$rounds] @ write output
1367 #ifndef BSAES_ASM_EXTENDED_KEY
1370 .Lcbc_dec_bzero: @ wipe key schedule [if any]
1371 vstmia $keysched!, {q0-q1}
1377 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1378 vst1.8 {@XMM[15]}, [$ivp] @ return IV
1380 ldmia sp!, {r4-r10, pc}
1381 .size bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
1385 my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1386 my $const = "r6"; # shared with _bsaes_encrypt8_alt
1387 my $keysched = "sp";
1391 .global bsaes_ctr32_encrypt_blocks
1392 .type bsaes_ctr32_encrypt_blocks,%function
1394 bsaes_ctr32_encrypt_blocks:
1395 cmp $len, #8 @ use plain AES for
1396 blo .Lctr_enc_short @ small sizes
1399 stmdb sp!, {r4-r10, lr}
1401 ldr $ctr, [ip] @ ctr is 1st arg on the stack
1402 sub sp, sp, #0x10 @ scratch space to carry over the ctr
1403 mov $fp, sp @ save sp
1405 ldr $rounds, [$key, #240] @ get # of rounds
1406 #ifndef BSAES_ASM_EXTENDED_KEY
1407 @ allocate the key schedule on the stack
1408 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1409 add r12, #`128-32` @ size of bit-sliced key schedule
1411 @ populate the key schedule
1412 mov r4, $key @ pass key
1413 mov r5, $rounds @ pass # of rounds
1414 mov sp, r12 @ sp is $keysched
1415 bl _bsaes_key_convert
1416 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1417 vstmia r12, {@XMM[7]} @ save last round key
1419 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1421 mov $ctr, #:lower16:(.LREVM0SR-.LM0)
1422 add $ctr, $const, $ctr
1424 add $ctr, $const, #.LREVM0SR-.LM0 @ borrow $ctr
1426 vldmia $keysched, {@XMM[4]} @ load round0 key
1428 ldr r12, [$key, #244]
1432 @ populate the key schedule
1433 str r12, [$key, #244]
1434 mov r4, $key @ pass key
1435 mov r5, $rounds @ pass # of rounds
1436 add r12, $key, #248 @ pass key schedule
1437 bl _bsaes_key_convert
1438 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1439 vstmia r12, {@XMM[7]} @ save last round key
1442 0: add r12, $key, #248
1443 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1444 adrl $ctr, .LREVM0SR @ borrow $ctr
1445 vldmia r12, {@XMM[4]} @ load round0 key
1446 sub sp, #0x10 @ place for adjusted round0 key
1449 vmov.i32 @XMM[8],#1 @ compose 1<<96
1450 veor @XMM[9],@XMM[9],@XMM[9]
1451 vrev32.8 @XMM[0],@XMM[0]
1452 vext.8 @XMM[8],@XMM[9],@XMM[8],#4
1453 vrev32.8 @XMM[4],@XMM[4]
1454 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1455 vstmia $keysched, {@XMM[4]} @ save adjusted round0 key
1460 vadd.u32 @XMM[10], @XMM[8], @XMM[9] @ compose 3<<96
1461 vadd.u32 @XMM[1], @XMM[0], @XMM[8] @ +1
1462 vadd.u32 @XMM[2], @XMM[0], @XMM[9] @ +2
1463 vadd.u32 @XMM[3], @XMM[0], @XMM[10] @ +3
1464 vadd.u32 @XMM[4], @XMM[1], @XMM[10]
1465 vadd.u32 @XMM[5], @XMM[2], @XMM[10]
1466 vadd.u32 @XMM[6], @XMM[3], @XMM[10]
1467 vadd.u32 @XMM[7], @XMM[4], @XMM[10]
1468 vadd.u32 @XMM[10], @XMM[5], @XMM[10] @ next counter
1470 @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1471 @ to flip byte order in 32-bit counter
1473 vldmia $keysched, {@XMM[9]} @ load round0 key
1474 #ifndef BSAES_ASM_EXTENDED_KEY
1475 add r4, $keysched, #0x10 @ pass next round key
1477 add r4, $key, #`248+16`
1479 vldmia $ctr, {@XMM[8]} @ .LREVM0SR
1480 mov r5, $rounds @ pass rounds
1481 vstmia $fp, {@XMM[10]} @ save next counter
1483 mov $const, #:lower16:(.LREVM0SR-.LSR)
1484 sub $const, $ctr, $const
1486 sub $const, $ctr, #.LREVM0SR-.LSR @ pass constants
1489 bl _bsaes_encrypt8_alt
1492 blo .Lctr_enc_loop_done
1494 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ load input
1495 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1496 veor @XMM[0], @XMM[8]
1497 veor @XMM[1], @XMM[9]
1498 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1499 veor @XMM[4], @XMM[10]
1500 veor @XMM[6], @XMM[11]
1501 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1502 veor @XMM[3], @XMM[12]
1503 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1504 veor @XMM[7], @XMM[13]
1505 veor @XMM[2], @XMM[14]
1506 vst1.8 {@XMM[4]}, [$out]!
1507 veor @XMM[5], @XMM[15]
1508 vst1.8 {@XMM[6]}, [$out]!
1509 vmov.i32 @XMM[8], #1 @ compose 1<<96
1510 vst1.8 {@XMM[3]}, [$out]!
1511 veor @XMM[9], @XMM[9], @XMM[9]
1512 vst1.8 {@XMM[7]}, [$out]!
1513 vext.8 @XMM[8], @XMM[9], @XMM[8], #4
1514 vst1.8 {@XMM[2]}, [$out]!
1515 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1516 vst1.8 {@XMM[5]}, [$out]!
1517 vldmia $fp, {@XMM[0]} @ load counter
1523 .Lctr_enc_loop_done:
1525 vld1.8 {@XMM[8]}, [$inp]! @ load input
1526 veor @XMM[0], @XMM[8]
1527 vst1.8 {@XMM[0]}, [$out]! @ write output
1530 vld1.8 {@XMM[9]}, [$inp]!
1531 veor @XMM[1], @XMM[9]
1532 vst1.8 {@XMM[1]}, [$out]!
1534 vld1.8 {@XMM[10]}, [$inp]!
1535 veor @XMM[4], @XMM[10]
1536 vst1.8 {@XMM[4]}, [$out]!
1539 vld1.8 {@XMM[11]}, [$inp]!
1540 veor @XMM[6], @XMM[11]
1541 vst1.8 {@XMM[6]}, [$out]!
1543 vld1.8 {@XMM[12]}, [$inp]!
1544 veor @XMM[3], @XMM[12]
1545 vst1.8 {@XMM[3]}, [$out]!
1548 vld1.8 {@XMM[13]}, [$inp]!
1549 veor @XMM[7], @XMM[13]
1550 vst1.8 {@XMM[7]}, [$out]!
1552 vld1.8 {@XMM[14]}, [$inp]
1553 veor @XMM[2], @XMM[14]
1554 vst1.8 {@XMM[2]}, [$out]!
1559 #ifndef BSAES_ASM_EXTENDED_KEY
1560 .Lctr_enc_bzero: @ wipe key schedule [if any]
1561 vstmia $keysched!, {q0-q1}
1565 vstmia $keysched, {q0-q1}
1569 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1571 ldmia sp!, {r4-r10, pc} @ return
1575 ldr ip, [sp] @ ctr pointer is passed on stack
1576 stmdb sp!, {r4-r8, lr}
1578 mov r4, $inp @ copy arguments
1582 ldr r8, [ip, #12] @ load counter LSW
1583 vld1.8 {@XMM[1]}, [ip] @ load whole counter value
1588 vst1.8 {@XMM[1]}, [sp] @ copy counter value
1591 .Lctr_enc_short_loop:
1592 add r0, sp, #0x10 @ input counter value
1593 mov r1, sp @ output on the stack
1598 vld1.8 {@XMM[0]}, [r4]! @ load input
1599 vld1.8 {@XMM[1]}, [sp] @ load encrypted counter
1603 str r0, [sp, #0x1c] @ next counter value
1605 str r8, [sp, #0x1c] @ next counter value
1607 veor @XMM[0],@XMM[0],@XMM[1]
1608 vst1.8 {@XMM[0]}, [r5]! @ store output
1610 bne .Lctr_enc_short_loop
1616 ldmia sp!, {r4-r8, pc}
1617 .size bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
1621 ######################################################################
1622 # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1623 # const AES_KEY *key1, const AES_KEY *key2,
1624 # const unsigned char iv[16]);
1626 my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
1627 my $const="r6"; # returned by _bsaes_key_convert
1632 .globl bsaes_xts_encrypt
1633 .type bsaes_xts_encrypt,%function
1637 stmdb sp!, {r4-r10, lr} @ 0x20
1639 mov r6, sp @ future $fp
1646 sub r0, sp, #0x10 @ 0x10
1647 bic r0, #0xf @ align at 16 bytes
1650 #ifdef XTS_CHAIN_TWEAK
1651 ldr r0, [ip] @ pointer to input tweak
1653 @ generate initial tweak
1654 ldr r0, [ip, #4] @ iv[]
1656 ldr r2, [ip, #0] @ key2
1658 mov r0,sp @ pointer to initial tweak
1661 ldr $rounds, [$key, #240] @ get # of rounds
1663 #ifndef BSAES_ASM_EXTENDED_KEY
1664 @ allocate the key schedule on the stack
1665 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1666 @ add r12, #`128-32` @ size of bit-sliced key schedule
1667 sub r12, #`32+16` @ place for tweak[9]
1669 @ populate the key schedule
1670 mov r4, $key @ pass key
1671 mov r5, $rounds @ pass # of rounds
1673 add r12, #0x90 @ pass key schedule
1674 bl _bsaes_key_convert
1675 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1676 vstmia r12, {@XMM[7]} @ save last round key
1678 ldr r12, [$key, #244]
1682 str r12, [$key, #244]
1683 mov r4, $key @ pass key
1684 mov r5, $rounds @ pass # of rounds
1685 add r12, $key, #248 @ pass key schedule
1686 bl _bsaes_key_convert
1687 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1688 vstmia r12, {@XMM[7]}
1691 0: sub sp, #0x90 @ place for tweak[9]
1694 vld1.8 {@XMM[8]}, [r0] @ initial tweak
1695 adr $magic, .Lxts_magic
1703 vldmia $magic, {$twmask} @ load XTS magic
1704 vshr.s64 @T[0], @XMM[8], #63
1706 vand @T[0], @T[0], $twmask
1708 for($i=9;$i<16;$i++) {
1710 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1711 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1712 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1713 vshr.s64 @T[1], @XMM[$i], #63
1714 veor @XMM[$i], @XMM[$i], @T[0]
1715 vand @T[1], @T[1], $twmask
1719 $code.=<<___ if ($i>=10);
1720 vld1.8 {@XMM[$i-10]}, [$inp]!
1722 $code.=<<___ if ($i>=11);
1723 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1727 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
1728 vst1.64 {@XMM[15]}, [r0,:128]!
1729 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1730 veor @XMM[8], @XMM[8], @T[0]
1731 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1733 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1734 veor @XMM[5], @XMM[5], @XMM[13]
1735 #ifndef BSAES_ASM_EXTENDED_KEY
1736 add r4, sp, #0x90 @ pass key schedule
1738 add r4, $key, #248 @ pass key schedule
1740 veor @XMM[6], @XMM[6], @XMM[14]
1741 mov r5, $rounds @ pass rounds
1742 veor @XMM[7], @XMM[7], @XMM[15]
1747 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1748 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1749 veor @XMM[0], @XMM[0], @XMM[ 8]
1750 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1751 veor @XMM[1], @XMM[1], @XMM[ 9]
1752 veor @XMM[8], @XMM[4], @XMM[10]
1753 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1754 veor @XMM[9], @XMM[6], @XMM[11]
1755 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
1756 veor @XMM[10], @XMM[3], @XMM[12]
1757 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1758 veor @XMM[11], @XMM[7], @XMM[13]
1759 veor @XMM[12], @XMM[2], @XMM[14]
1760 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1761 veor @XMM[13], @XMM[5], @XMM[15]
1762 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
1764 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1773 vldmia $magic, {$twmask} @ load XTS magic
1774 vshr.s64 @T[0], @XMM[8], #63
1776 vand @T[0], @T[0], $twmask
1778 for($i=9;$i<16;$i++) {
1780 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1781 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1782 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1783 vshr.s64 @T[1], @XMM[$i], #63
1784 veor @XMM[$i], @XMM[$i], @T[0]
1785 vand @T[1], @T[1], $twmask
1789 $code.=<<___ if ($i>=10);
1790 vld1.8 {@XMM[$i-10]}, [$inp]!
1792 bmi .Lxts_enc_`$i-9`
1794 $code.=<<___ if ($i>=11);
1795 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1800 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
1802 vld1.8 {@XMM[6]}, [$inp]!
1803 veor @XMM[5], @XMM[5], @XMM[13]
1804 #ifndef BSAES_ASM_EXTENDED_KEY
1805 add r4, sp, #0x90 @ pass key schedule
1807 add r4, $key, #248 @ pass key schedule
1809 veor @XMM[6], @XMM[6], @XMM[14]
1810 mov r5, $rounds @ pass rounds
1815 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1816 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1817 veor @XMM[0], @XMM[0], @XMM[ 8]
1818 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1819 veor @XMM[1], @XMM[1], @XMM[ 9]
1820 veor @XMM[8], @XMM[4], @XMM[10]
1821 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1822 veor @XMM[9], @XMM[6], @XMM[11]
1823 vld1.64 {@XMM[14]}, [r0,:128]!
1824 veor @XMM[10], @XMM[3], @XMM[12]
1825 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1826 veor @XMM[11], @XMM[7], @XMM[13]
1827 veor @XMM[12], @XMM[2], @XMM[14]
1828 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1829 vst1.8 {@XMM[12]}, [$out]!
1831 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1835 vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
1837 veor @XMM[4], @XMM[4], @XMM[12]
1838 #ifndef BSAES_ASM_EXTENDED_KEY
1839 add r4, sp, #0x90 @ pass key schedule
1841 add r4, $key, #248 @ pass key schedule
1843 veor @XMM[5], @XMM[5], @XMM[13]
1844 mov r5, $rounds @ pass rounds
1849 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1850 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1851 veor @XMM[0], @XMM[0], @XMM[ 8]
1852 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1853 veor @XMM[1], @XMM[1], @XMM[ 9]
1854 veor @XMM[8], @XMM[4], @XMM[10]
1855 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1856 veor @XMM[9], @XMM[6], @XMM[11]
1857 veor @XMM[10], @XMM[3], @XMM[12]
1858 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1859 veor @XMM[11], @XMM[7], @XMM[13]
1860 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1862 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1865 @ put this in range for both ARM and Thumb mode adr instructions
1872 vst1.64 {@XMM[13]}, [r0,:128] @ next round tweak
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 vst1.64 {@XMM[12]}, [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 vst1.64 {@XMM[11]}, [r0,:128] @ next round tweak
1931 veor @XMM[1], @XMM[1], @XMM[9]
1932 #ifndef BSAES_ASM_EXTENDED_KEY
1933 add r4, sp, #0x90 @ pass key schedule
1935 add r4, $key, #248 @ pass key schedule
1937 veor @XMM[2], @XMM[2], @XMM[10]
1938 mov r5, $rounds @ pass rounds
1943 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1944 vld1.64 {@XMM[10]}, [r0,:128]!
1945 veor @XMM[0], @XMM[0], @XMM[ 8]
1946 veor @XMM[1], @XMM[1], @XMM[ 9]
1947 veor @XMM[8], @XMM[4], @XMM[10]
1948 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1949 vst1.8 {@XMM[8]}, [$out]!
1951 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1955 vst1.64 {@XMM[10]}, [r0,:128] @ next round tweak
1957 veor @XMM[0], @XMM[0], @XMM[8]
1958 #ifndef BSAES_ASM_EXTENDED_KEY
1959 add r4, sp, #0x90 @ pass key schedule
1961 add r4, $key, #248 @ pass key schedule
1963 veor @XMM[1], @XMM[1], @XMM[9]
1964 mov r5, $rounds @ pass rounds
1969 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1970 veor @XMM[0], @XMM[0], @XMM[ 8]
1971 veor @XMM[1], @XMM[1], @XMM[ 9]
1972 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1974 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1979 veor @XMM[0], @XMM[8]
1981 vst1.8 {@XMM[0]}, [sp,:128]
1983 mov r4, $fp @ preserve fp
1987 vld1.8 {@XMM[0]}, [sp,:128]
1988 veor @XMM[0], @XMM[0], @XMM[8]
1989 vst1.8 {@XMM[0]}, [$out]!
1992 vmov @XMM[8], @XMM[9] @ next round tweak
1995 #ifndef XTS_CHAIN_TWEAK
2002 ldrb r1, [$out, #-0x10]
2003 strb r0, [$out, #-0x10]
2009 vld1.8 {@XMM[0]}, [r6]
2011 veor @XMM[0], @XMM[0], @XMM[8]
2013 vst1.8 {@XMM[0]}, [sp,:128]
2015 mov r4, $fp @ preserve fp
2019 vld1.8 {@XMM[0]}, [sp,:128]
2020 veor @XMM[0], @XMM[0], @XMM[8]
2021 vst1.8 {@XMM[0]}, [r6]
2029 #ifdef XTS_CHAIN_TWEAK
2030 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2032 .Lxts_enc_bzero: @ wipe key schedule [if any]
2038 #ifdef XTS_CHAIN_TWEAK
2039 vst1.8 {@XMM[8]}, [r1]
2042 ldmia sp!, {r4-r10, pc} @ return
2044 .size bsaes_xts_encrypt,.-bsaes_xts_encrypt
2046 .globl bsaes_xts_decrypt
2047 .type bsaes_xts_decrypt,%function
2051 stmdb sp!, {r4-r10, lr} @ 0x20
2053 mov r6, sp @ future $fp
2060 sub r0, sp, #0x10 @ 0x10
2061 bic r0, #0xf @ align at 16 bytes
2064 #ifdef XTS_CHAIN_TWEAK
2065 ldr r0, [ip] @ pointer to input tweak
2067 @ generate initial tweak
2068 ldr r0, [ip, #4] @ iv[]
2070 ldr r2, [ip, #0] @ key2
2072 mov r0, sp @ pointer to initial tweak
2075 ldr $rounds, [$key, #240] @ get # of rounds
2077 #ifndef BSAES_ASM_EXTENDED_KEY
2078 @ allocate the key schedule on the stack
2079 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
2080 @ add r12, #`128-32` @ size of bit-sliced key schedule
2081 sub r12, #`32+16` @ place for tweak[9]
2083 @ populate the key schedule
2084 mov r4, $key @ pass key
2085 mov r5, $rounds @ pass # of rounds
2087 add r12, #0x90 @ pass key schedule
2088 bl _bsaes_key_convert
2090 vldmia r4, {@XMM[6]}
2091 vstmia r12, {@XMM[15]} @ save last round key
2092 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2093 vstmia r4, {@XMM[7]}
2095 ldr r12, [$key, #244]
2099 str r12, [$key, #244]
2100 mov r4, $key @ pass key
2101 mov r5, $rounds @ pass # of rounds
2102 add r12, $key, #248 @ pass key schedule
2103 bl _bsaes_key_convert
2105 vldmia r4, {@XMM[6]}
2106 vstmia r12, {@XMM[15]} @ save last round key
2107 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2108 vstmia r4, {@XMM[7]}
2111 0: sub sp, #0x90 @ place for tweak[9]
2113 vld1.8 {@XMM[8]}, [r0] @ initial tweak
2114 adr $magic, .Lxts_magic
2116 #ifndef XTS_CHAIN_TWEAK
2117 tst $len, #0xf @ if not multiple of 16
2118 it ne @ Thumb2 thing, sanity check in ARM
2119 subne $len, #0x10 @ subtract another 16 bytes
2128 vldmia $magic, {$twmask} @ load XTS magic
2129 vshr.s64 @T[0], @XMM[8], #63
2131 vand @T[0], @T[0], $twmask
2133 for($i=9;$i<16;$i++) {
2135 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2136 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2137 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2138 vshr.s64 @T[1], @XMM[$i], #63
2139 veor @XMM[$i], @XMM[$i], @T[0]
2140 vand @T[1], @T[1], $twmask
2144 $code.=<<___ if ($i>=10);
2145 vld1.8 {@XMM[$i-10]}, [$inp]!
2147 $code.=<<___ if ($i>=11);
2148 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2152 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
2153 vst1.64 {@XMM[15]}, [r0,:128]!
2154 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2155 veor @XMM[8], @XMM[8], @T[0]
2156 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2158 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
2159 veor @XMM[5], @XMM[5], @XMM[13]
2160 #ifndef BSAES_ASM_EXTENDED_KEY
2161 add r4, sp, #0x90 @ pass key schedule
2163 add r4, $key, #248 @ pass key schedule
2165 veor @XMM[6], @XMM[6], @XMM[14]
2166 mov r5, $rounds @ pass rounds
2167 veor @XMM[7], @XMM[7], @XMM[15]
2172 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2173 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2174 veor @XMM[0], @XMM[0], @XMM[ 8]
2175 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2176 veor @XMM[1], @XMM[1], @XMM[ 9]
2177 veor @XMM[8], @XMM[6], @XMM[10]
2178 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2179 veor @XMM[9], @XMM[4], @XMM[11]
2180 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
2181 veor @XMM[10], @XMM[2], @XMM[12]
2182 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2183 veor @XMM[11], @XMM[7], @XMM[13]
2184 veor @XMM[12], @XMM[3], @XMM[14]
2185 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2186 veor @XMM[13], @XMM[5], @XMM[15]
2187 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
2189 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2198 vldmia $magic, {$twmask} @ load XTS magic
2199 vshr.s64 @T[0], @XMM[8], #63
2201 vand @T[0], @T[0], $twmask
2203 for($i=9;$i<16;$i++) {
2205 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2206 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2207 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2208 vshr.s64 @T[1], @XMM[$i], #63
2209 veor @XMM[$i], @XMM[$i], @T[0]
2210 vand @T[1], @T[1], $twmask
2214 $code.=<<___ if ($i>=10);
2215 vld1.8 {@XMM[$i-10]}, [$inp]!
2217 bmi .Lxts_dec_`$i-9`
2219 $code.=<<___ if ($i>=11);
2220 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2225 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
2227 vld1.8 {@XMM[6]}, [$inp]!
2228 veor @XMM[5], @XMM[5], @XMM[13]
2229 #ifndef BSAES_ASM_EXTENDED_KEY
2230 add r4, sp, #0x90 @ pass key schedule
2232 add r4, $key, #248 @ pass key schedule
2234 veor @XMM[6], @XMM[6], @XMM[14]
2235 mov r5, $rounds @ pass rounds
2240 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2241 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2242 veor @XMM[0], @XMM[0], @XMM[ 8]
2243 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2244 veor @XMM[1], @XMM[1], @XMM[ 9]
2245 veor @XMM[8], @XMM[6], @XMM[10]
2246 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2247 veor @XMM[9], @XMM[4], @XMM[11]
2248 vld1.64 {@XMM[14]}, [r0,:128]!
2249 veor @XMM[10], @XMM[2], @XMM[12]
2250 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2251 veor @XMM[11], @XMM[7], @XMM[13]
2252 veor @XMM[12], @XMM[3], @XMM[14]
2253 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2254 vst1.8 {@XMM[12]}, [$out]!
2256 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2260 vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
2262 veor @XMM[4], @XMM[4], @XMM[12]
2263 #ifndef BSAES_ASM_EXTENDED_KEY
2264 add r4, sp, #0x90 @ pass key schedule
2266 add r4, $key, #248 @ pass key schedule
2268 veor @XMM[5], @XMM[5], @XMM[13]
2269 mov r5, $rounds @ pass rounds
2274 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2275 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2276 veor @XMM[0], @XMM[0], @XMM[ 8]
2277 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2278 veor @XMM[1], @XMM[1], @XMM[ 9]
2279 veor @XMM[8], @XMM[6], @XMM[10]
2280 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2281 veor @XMM[9], @XMM[4], @XMM[11]
2282 veor @XMM[10], @XMM[2], @XMM[12]
2283 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2284 veor @XMM[11], @XMM[7], @XMM[13]
2285 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2287 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2291 vst1.64 {@XMM[13]}, [r0,:128] @ next round tweak
2293 veor @XMM[3], @XMM[3], @XMM[11]
2294 #ifndef BSAES_ASM_EXTENDED_KEY
2295 add r4, sp, #0x90 @ pass key schedule
2297 add r4, $key, #248 @ pass key schedule
2299 veor @XMM[4], @XMM[4], @XMM[12]
2300 mov r5, $rounds @ pass rounds
2305 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2306 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2307 veor @XMM[0], @XMM[0], @XMM[ 8]
2308 vld1.64 {@XMM[12]}, [r0,:128]!
2309 veor @XMM[1], @XMM[1], @XMM[ 9]
2310 veor @XMM[8], @XMM[6], @XMM[10]
2311 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2312 veor @XMM[9], @XMM[4], @XMM[11]
2313 veor @XMM[10], @XMM[2], @XMM[12]
2314 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2315 vst1.8 {@XMM[10]}, [$out]!
2317 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2321 vst1.64 {@XMM[12]}, [r0,:128] @ next round tweak
2323 veor @XMM[2], @XMM[2], @XMM[10]
2324 #ifndef BSAES_ASM_EXTENDED_KEY
2325 add r4, sp, #0x90 @ pass key schedule
2327 add r4, $key, #248 @ pass key schedule
2329 veor @XMM[3], @XMM[3], @XMM[11]
2330 mov r5, $rounds @ pass rounds
2335 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2336 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2337 veor @XMM[0], @XMM[0], @XMM[ 8]
2338 veor @XMM[1], @XMM[1], @XMM[ 9]
2339 veor @XMM[8], @XMM[6], @XMM[10]
2340 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2341 veor @XMM[9], @XMM[4], @XMM[11]
2342 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2344 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2348 vst1.64 {@XMM[11]}, [r0,:128] @ next round tweak
2350 veor @XMM[1], @XMM[1], @XMM[9]
2351 #ifndef BSAES_ASM_EXTENDED_KEY
2352 add r4, sp, #0x90 @ pass key schedule
2354 add r4, $key, #248 @ pass key schedule
2356 veor @XMM[2], @XMM[2], @XMM[10]
2357 mov r5, $rounds @ pass rounds
2362 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2363 vld1.64 {@XMM[10]}, [r0,:128]!
2364 veor @XMM[0], @XMM[0], @XMM[ 8]
2365 veor @XMM[1], @XMM[1], @XMM[ 9]
2366 veor @XMM[8], @XMM[6], @XMM[10]
2367 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2368 vst1.8 {@XMM[8]}, [$out]!
2370 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2374 vst1.64 {@XMM[10]}, [r0,:128] @ next round tweak
2376 veor @XMM[0], @XMM[0], @XMM[8]
2377 #ifndef BSAES_ASM_EXTENDED_KEY
2378 add r4, sp, #0x90 @ pass key schedule
2380 add r4, $key, #248 @ pass key schedule
2382 veor @XMM[1], @XMM[1], @XMM[9]
2383 mov r5, $rounds @ pass rounds
2388 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2389 veor @XMM[0], @XMM[0], @XMM[ 8]
2390 veor @XMM[1], @XMM[1], @XMM[ 9]
2391 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2393 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2398 veor @XMM[0], @XMM[8]
2400 vst1.8 {@XMM[0]}, [sp,:128]
2402 mov r4, $fp @ preserve fp
2403 mov r5, $magic @ preserve magic
2407 vld1.8 {@XMM[0]}, [sp,:128]
2408 veor @XMM[0], @XMM[0], @XMM[8]
2409 vst1.8 {@XMM[0]}, [$out]!
2413 vmov @XMM[8], @XMM[9] @ next round tweak
2416 #ifndef XTS_CHAIN_TWEAK
2420 @ calculate one round of extra tweak for the stolen ciphertext
2421 vldmia $magic, {$twmask}
2422 vshr.s64 @XMM[6], @XMM[8], #63
2423 vand @XMM[6], @XMM[6], $twmask
2424 vadd.u64 @XMM[9], @XMM[8], @XMM[8]
2425 vswp `&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
2426 veor @XMM[9], @XMM[9], @XMM[6]
2428 @ perform the final decryption with the last tweak value
2429 vld1.8 {@XMM[0]}, [$inp]!
2431 veor @XMM[0], @XMM[0], @XMM[9]
2433 vst1.8 {@XMM[0]}, [sp,:128]
2435 mov r4, $fp @ preserve fp
2439 vld1.8 {@XMM[0]}, [sp,:128]
2440 veor @XMM[0], @XMM[0], @XMM[9]
2441 vst1.8 {@XMM[0]}, [$out]
2447 strb r1, [$out, #0x10]
2453 vld1.8 {@XMM[0]}, [r6]
2455 veor @XMM[0], @XMM[8]
2457 vst1.8 {@XMM[0]}, [sp,:128]
2462 vld1.8 {@XMM[0]}, [sp,:128]
2463 veor @XMM[0], @XMM[0], @XMM[8]
2464 vst1.8 {@XMM[0]}, [r6]
2472 #ifdef XTS_CHAIN_TWEAK
2473 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2475 .Lxts_dec_bzero: @ wipe key schedule [if any]
2481 #ifdef XTS_CHAIN_TWEAK
2482 vst1.8 {@XMM[8]}, [r1]
2485 ldmia sp!, {r4-r10, pc} @ return
2487 .size bsaes_xts_decrypt,.-bsaes_xts_decrypt
2494 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
2499 last if (!s/^#/@/ and !/^$/);