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 24.0 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 22.6,
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/19.6 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>
50 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
51 open STDOUT,">$output";
53 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
54 my @XMM=map("q$_",(0..15));
57 my ($key,$rounds,$const)=("r4","r5","r6");
59 sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
60 sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
63 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
64 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
69 &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
70 &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
74 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
75 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
78 veor @b[2], @b[2], @b[1]
79 veor @b[5], @b[5], @b[6]
80 veor @b[3], @b[3], @b[0]
81 veor @b[6], @b[6], @b[2]
82 veor @b[5], @b[5], @b[0]
84 veor @b[6], @b[6], @b[3]
85 veor @b[3], @b[3], @b[7]
86 veor @b[7], @b[7], @b[5]
87 veor @b[3], @b[3], @b[4]
88 veor @b[4], @b[4], @b[5]
90 veor @b[2], @b[2], @b[7]
91 veor @b[3], @b[3], @b[1]
92 veor @b[1], @b[1], @b[5]
97 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
98 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
101 veor @b[0], @b[0], @b[6]
102 veor @b[1], @b[1], @b[4]
103 veor @b[4], @b[4], @b[6]
104 veor @b[2], @b[2], @b[0]
105 veor @b[6], @b[6], @b[1]
107 veor @b[1], @b[1], @b[5]
108 veor @b[5], @b[5], @b[3]
109 veor @b[3], @b[3], @b[7]
110 veor @b[7], @b[7], @b[5]
111 veor @b[2], @b[2], @b[5]
113 veor @b[4], @b[4], @b[7]
118 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
119 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
123 &InvInBasisChange (@b);
124 &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
125 &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
128 sub InvInBasisChange { # OutBasisChange in reverse (with twist)
129 my @b=@_[5,1,2,6,3,7,0,4];
131 veor @b[1], @b[1], @b[7]
132 veor @b[4], @b[4], @b[7]
134 veor @b[7], @b[7], @b[5]
135 veor @b[1], @b[1], @b[3]
136 veor @b[2], @b[2], @b[5]
137 veor @b[3], @b[3], @b[7]
139 veor @b[6], @b[6], @b[1]
140 veor @b[2], @b[2], @b[0]
141 veor @b[5], @b[5], @b[3]
142 veor @b[4], @b[4], @b[6]
143 veor @b[0], @b[0], @b[6]
144 veor @b[1], @b[1], @b[4]
148 sub InvOutBasisChange { # InBasisChange in reverse
149 my @b=@_[2,5,7,3,6,1,0,4];
151 veor @b[1], @b[1], @b[5]
152 veor @b[2], @b[2], @b[7]
154 veor @b[3], @b[3], @b[1]
155 veor @b[4], @b[4], @b[5]
156 veor @b[7], @b[7], @b[5]
157 veor @b[3], @b[3], @b[4]
158 veor @b[5], @b[5], @b[0]
159 veor @b[3], @b[3], @b[7]
160 veor @b[6], @b[6], @b[2]
161 veor @b[2], @b[2], @b[1]
162 veor @b[6], @b[6], @b[3]
164 veor @b[3], @b[3], @b[0]
165 veor @b[5], @b[5], @b[6]
170 #;*************************************************************
171 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
172 #;*************************************************************
173 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
185 sub Mul_GF4_N { # not used, see next subroutine
186 # multiply and scale by N
187 my ($x0,$x1,$y0,$y1,$t0)=@_;
200 # interleaved Mul_GF4_N and Mul_GF4
201 my ($x0,$x1,$y0,$y1,$t0,
202 $x2,$x3,$y2,$y3,$t1)=@_;
225 veor @t[0], @x[0], @x[2]
226 veor @t[1], @x[1], @x[3]
228 &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
230 veor @y[0], @y[0], @y[2]
231 veor @y[1], @y[1], @y[3]
233 Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
234 @x[2], @x[3], @y[2], @y[3], @t[2]);
236 veor @x[0], @x[0], @t[0]
237 veor @x[2], @x[2], @t[0]
238 veor @x[1], @x[1], @t[1]
239 veor @x[3], @x[3], @t[1]
241 veor @t[0], @x[4], @x[6]
242 veor @t[1], @x[5], @x[7]
244 &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
245 @x[6], @x[7], @y[2], @y[3], @t[2]);
247 veor @y[0], @y[0], @y[2]
248 veor @y[1], @y[1], @y[3]
250 &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
252 veor @x[4], @x[4], @t[0]
253 veor @x[6], @x[6], @t[0]
254 veor @x[5], @x[5], @t[1]
255 veor @x[7], @x[7], @t[1]
259 #;********************************************************************
260 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
261 #;********************************************************************
265 # direct optimizations from hardware
267 veor @t[3], @x[4], @x[6]
268 veor @t[2], @x[5], @x[7]
269 veor @t[1], @x[1], @x[3]
270 veor @s[1], @x[7], @x[6]
272 veor @s[0], @x[0], @x[2]
274 vorr @t[2], @t[2], @t[1]
275 veor @s[3], @t[3], @t[0]
276 vand @s[2], @t[3], @s[0]
277 vorr @t[3], @t[3], @s[0]
278 veor @s[0], @s[0], @t[1]
279 vand @t[0], @t[0], @t[1]
280 veor @t[1], @x[3], @x[2]
281 vand @s[3], @s[3], @s[0]
282 vand @s[1], @s[1], @t[1]
283 veor @t[1], @x[4], @x[5]
284 veor @s[0], @x[1], @x[0]
285 veor @t[3], @t[3], @s[1]
286 veor @t[2], @t[2], @s[1]
287 vand @s[1], @t[1], @s[0]
288 vorr @t[1], @t[1], @s[0]
289 veor @t[3], @t[3], @s[3]
290 veor @t[0], @t[0], @s[1]
291 veor @t[2], @t[2], @s[2]
292 veor @t[1], @t[1], @s[3]
293 veor @t[0], @t[0], @s[2]
294 vand @s[0], @x[7], @x[3]
295 veor @t[1], @t[1], @s[2]
296 vand @s[1], @x[6], @x[2]
297 vand @s[2], @x[5], @x[1]
298 vorr @s[3], @x[4], @x[0]
299 veor @t[3], @t[3], @s[0]
300 veor @t[1], @t[1], @s[2]
301 veor @t[0], @t[0], @s[3]
302 veor @t[2], @t[2], @s[1]
304 @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
306 @ new smaller inversion
308 vand @s[2], @t[3], @t[1]
311 veor @s[1], @t[2], @s[2]
312 veor @s[3], @t[0], @s[2]
313 veor @s[2], @t[0], @s[2] @ @s[2]=@s[3]
315 vbsl @s[1], @t[1], @t[0]
316 vbsl @s[3], @t[3], @t[2]
317 veor @t[3], @t[3], @t[2]
319 vbsl @s[0], @s[1], @s[2]
320 vbsl @t[0], @s[2], @s[1]
322 vand @s[2], @s[0], @s[3]
323 veor @t[1], @t[1], @t[0]
325 veor @s[2], @s[2], @t[3]
327 # output in s3, s2, s1, t1
329 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
331 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
332 &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
334 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
337 # AES linear components
344 vldmia $key!, {@t[0]-@t[3]}
345 veor @t[0], @t[0], @x[0]
346 veor @t[1], @t[1], @x[1]
347 vtbl.8 `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
348 vtbl.8 `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
349 vldmia $key!, {@t[0]}
350 veor @t[2], @t[2], @x[2]
351 vtbl.8 `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
352 vtbl.8 `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
353 vldmia $key!, {@t[1]}
354 veor @t[3], @t[3], @x[3]
355 vtbl.8 `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
356 vtbl.8 `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
357 vldmia $key!, {@t[2]}
358 vtbl.8 `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
359 vtbl.8 `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
360 vldmia $key!, {@t[3]}
361 veor @t[0], @t[0], @x[4]
362 veor @t[1], @t[1], @x[5]
363 vtbl.8 `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
364 vtbl.8 `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
365 veor @t[2], @t[2], @x[6]
366 vtbl.8 `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
367 vtbl.8 `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
368 veor @t[3], @t[3], @x[7]
369 vtbl.8 `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
370 vtbl.8 `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
371 vtbl.8 `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
372 vtbl.8 `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
377 # modified to emit output in order suitable for feeding back to aesenc[last]
381 vext.8 @t[0], @x[0], @x[0], #12 @ x0 <<< 32
382 vext.8 @t[1], @x[1], @x[1], #12
383 veor @x[0], @x[0], @t[0] @ x0 ^ (x0 <<< 32)
384 vext.8 @t[2], @x[2], @x[2], #12
385 veor @x[1], @x[1], @t[1]
386 vext.8 @t[3], @x[3], @x[3], #12
387 veor @x[2], @x[2], @t[2]
388 vext.8 @t[4], @x[4], @x[4], #12
389 veor @x[3], @x[3], @t[3]
390 vext.8 @t[5], @x[5], @x[5], #12
391 veor @x[4], @x[4], @t[4]
392 vext.8 @t[6], @x[6], @x[6], #12
393 veor @x[5], @x[5], @t[5]
394 vext.8 @t[7], @x[7], @x[7], #12
395 veor @x[6], @x[6], @t[6]
397 veor @t[1], @t[1], @x[0]
398 veor @x[7], @x[7], @t[7]
399 vext.8 @x[0], @x[0], @x[0], #8 @ (x0 ^ (x0 <<< 32)) <<< 64)
400 veor @t[2], @t[2], @x[1]
401 veor @t[0], @t[0], @x[7]
402 veor @t[1], @t[1], @x[7]
403 vext.8 @x[1], @x[1], @x[1], #8
404 veor @t[5], @t[5], @x[4]
405 veor @x[0], @x[0], @t[0]
406 veor @t[6], @t[6], @x[5]
407 veor @x[1], @x[1], @t[1]
408 vext.8 @t[0], @x[4], @x[4], #8
409 veor @t[4], @t[4], @x[3]
410 vext.8 @t[1], @x[5], @x[5], #8
411 veor @t[7], @t[7], @x[6]
412 vext.8 @x[4], @x[3], @x[3], #8
413 veor @t[3], @t[3], @x[2]
414 vext.8 @x[5], @x[7], @x[7], #8
415 veor @t[4], @t[4], @x[7]
416 vext.8 @x[3], @x[6], @x[6], #8
417 veor @t[3], @t[3], @x[7]
418 vext.8 @x[6], @x[2], @x[2], #8
419 veor @x[7], @t[1], @t[5]
420 veor @x[2], @t[0], @t[4]
422 veor @x[4], @x[4], @t[3]
423 veor @x[5], @x[5], @t[7]
424 veor @x[3], @x[3], @t[6]
426 veor @x[6], @x[6], @t[2]
436 @ multiplication by 0x0e
437 vext.8 @t[7], @x[7], @x[7], #12
439 veor @x[2], @x[2], @x[5] @ 2 5
440 veor @x[7], @x[7], @x[5] @ 7 5
441 vext.8 @t[0], @x[0], @x[0], #12
443 veor @x[5], @x[5], @x[0] @ 5 0 [1]
444 veor @x[0], @x[0], @x[1] @ 0 1
445 vext.8 @t[1], @x[1], @x[1], #12
446 veor @x[1], @x[1], @x[2] @ 1 25
447 veor @x[0], @x[0], @x[6] @ 01 6 [2]
448 vext.8 @t[3], @x[3], @x[3], #12
449 veor @x[1], @x[1], @x[3] @ 125 3 [4]
450 veor @x[2], @x[2], @x[0] @ 25 016 [3]
451 veor @x[3], @x[3], @x[7] @ 3 75
452 veor @x[7], @x[7], @x[6] @ 75 6 [0]
453 vext.8 @t[6], @x[6], @x[6], #12
455 veor @x[6], @x[6], @x[4] @ 6 4
456 veor @x[4], @x[4], @x[3] @ 4 375 [6]
457 veor @x[3], @x[3], @x[7] @ 375 756=36
458 veor @x[6], @x[6], @t[5] @ 64 5 [7]
459 veor @x[3], @x[3], @t[2] @ 36 2
460 vext.8 @t[5], @t[5], @t[5], #12
461 veor @x[3], @x[3], @t[4] @ 362 4 [5]
463 my @y = @x[7,5,0,2,1,3,4,6];
465 @ multiplication by 0x0b
466 veor @y[1], @y[1], @y[0]
467 veor @y[0], @y[0], @t[0]
468 vext.8 @t[2], @t[2], @t[2], #12
469 veor @y[1], @y[1], @t[1]
470 veor @y[0], @y[0], @t[5]
471 vext.8 @t[4], @t[4], @t[4], #12
472 veor @y[1], @y[1], @t[6]
473 veor @y[0], @y[0], @t[7]
474 veor @t[7], @t[7], @t[6] @ clobber t[7]
476 veor @y[3], @y[3], @t[0]
477 veor @y[1], @y[1], @y[0]
478 vext.8 @t[0], @t[0], @t[0], #12
479 veor @y[2], @y[2], @t[1]
480 veor @y[4], @y[4], @t[1]
481 vext.8 @t[1], @t[1], @t[1], #12
482 veor @y[2], @y[2], @t[2]
483 veor @y[3], @y[3], @t[2]
484 veor @y[5], @y[5], @t[2]
485 veor @y[2], @y[2], @t[7]
486 vext.8 @t[2], @t[2], @t[2], #12
487 veor @y[3], @y[3], @t[3]
488 veor @y[6], @y[6], @t[3]
489 veor @y[4], @y[4], @t[3]
490 veor @y[7], @y[7], @t[4]
491 vext.8 @t[3], @t[3], @t[3], #12
492 veor @y[5], @y[5], @t[4]
493 veor @y[7], @y[7], @t[7]
494 veor @t[7], @t[7], @t[5] @ clobber t[7] even more
495 veor @y[3], @y[3], @t[5]
496 veor @y[4], @y[4], @t[4]
498 veor @y[5], @y[5], @t[7]
499 vext.8 @t[4], @t[4], @t[4], #12
500 veor @y[6], @y[6], @t[7]
501 veor @y[4], @y[4], @t[7]
503 veor @t[7], @t[7], @t[5]
504 vext.8 @t[5], @t[5], @t[5], #12
506 @ multiplication by 0x0d
507 veor @y[4], @y[4], @y[7]
508 veor @t[7], @t[7], @t[6] @ restore t[7]
509 veor @y[7], @y[7], @t[4]
510 vext.8 @t[6], @t[6], @t[6], #12
511 veor @y[2], @y[2], @t[0]
512 veor @y[7], @y[7], @t[5]
513 vext.8 @t[7], @t[7], @t[7], #12
514 veor @y[2], @y[2], @t[2]
516 veor @y[3], @y[3], @y[1]
517 veor @y[1], @y[1], @t[1]
518 veor @y[0], @y[0], @t[0]
519 veor @y[3], @y[3], @t[0]
520 veor @y[1], @y[1], @t[5]
521 veor @y[0], @y[0], @t[5]
522 vext.8 @t[0], @t[0], @t[0], #12
523 veor @y[1], @y[1], @t[7]
524 veor @y[0], @y[0], @t[6]
525 veor @y[3], @y[3], @y[1]
526 veor @y[4], @y[4], @t[1]
527 vext.8 @t[1], @t[1], @t[1], #12
529 veor @y[7], @y[7], @t[7]
530 veor @y[4], @y[4], @t[2]
531 veor @y[5], @y[5], @t[2]
532 veor @y[2], @y[2], @t[6]
533 veor @t[6], @t[6], @t[3] @ clobber t[6]
534 vext.8 @t[2], @t[2], @t[2], #12
535 veor @y[4], @y[4], @y[7]
536 veor @y[3], @y[3], @t[6]
538 veor @y[6], @y[6], @t[6]
539 veor @y[5], @y[5], @t[5]
540 vext.8 @t[5], @t[5], @t[5], #12
541 veor @y[6], @y[6], @t[4]
542 vext.8 @t[4], @t[4], @t[4], #12
543 veor @y[5], @y[5], @t[6]
544 veor @y[6], @y[6], @t[7]
545 vext.8 @t[7], @t[7], @t[7], #12
546 veor @t[6], @t[6], @t[3] @ restore t[6]
547 vext.8 @t[3], @t[3], @t[3], #12
549 @ multiplication by 0x09
550 veor @y[4], @y[4], @y[1]
551 veor @t[1], @t[1], @y[1] @ t[1]=y[1]
552 veor @t[0], @t[0], @t[5] @ clobber t[0]
553 vext.8 @t[6], @t[6], @t[6], #12
554 veor @t[1], @t[1], @t[5]
555 veor @y[3], @y[3], @t[0]
556 veor @t[0], @t[0], @y[0] @ t[0]=y[0]
557 veor @t[1], @t[1], @t[6]
558 veor @t[6], @t[6], @t[7] @ clobber t[6]
559 veor @y[4], @y[4], @t[1]
560 veor @y[7], @y[7], @t[4]
561 veor @y[6], @y[6], @t[3]
562 veor @y[5], @y[5], @t[2]
563 veor @t[4], @t[4], @y[4] @ t[4]=y[4]
564 veor @t[3], @t[3], @y[3] @ t[3]=y[3]
565 veor @t[5], @t[5], @y[5] @ t[5]=y[5]
566 veor @t[2], @t[2], @y[2] @ t[2]=y[2]
567 veor @t[3], @t[3], @t[7]
568 veor @XMM[5], @t[5], @t[6]
569 veor @XMM[6], @t[6], @y[6] @ t[6]=y[6]
570 veor @XMM[2], @t[2], @t[6]
571 veor @XMM[7], @t[7], @y[7] @ t[7]=y[7]
575 @ vmov @XMM[2], @t[2]
578 @ vmov @XMM[5], @t[5]
579 @ vmov @XMM[6], @t[6]
580 @ vmov @XMM[7], @t[7]
585 my ($a,$b,$n,$mask,$t)=@_;
596 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
598 vshr.u64 $t0, $b0, #$n
599 vshr.u64 $t1, $b1, #$n
605 vshl.u64 $t0, $t0, #$n
607 vshl.u64 $t1, $t1, #$n
614 my @x=reverse(@_[0..7]);
615 my ($t0,$t1,$t2,$t3)=@_[8..11];
617 vmov.i8 $t0,#0x55 @ compose .LBS0
618 vmov.i8 $t1,#0x33 @ compose .LBS1
620 &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
621 &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
623 vmov.i8 $t0,#0x0f @ compose .LBS2
625 &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
626 &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
628 &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
629 &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
634 # include "arm_arch.h"
636 # define VFP_ABI_PUSH vstmdb sp!,{d8-d15}
637 # define VFP_ABI_POP vldmia sp!,{d8-d15}
638 # define VFP_ABI_FRAME 0x40
640 # define VFP_ABI_PUSH
642 # define VFP_ABI_FRAME 0
643 # define BSAES_ASM_EXTENDED_KEY
644 # define XTS_CHAIN_TWEAK
645 # define __ARM_ARCH__ __LINUX_ARM_ARCH__
654 .syntax unified @ ARMv7-capable assembler is expected to handle this
663 .type _bsaes_decrypt8,%function
666 adr $const,_bsaes_decrypt8
667 vldmia $key!, {@XMM[9]} @ round 0 key
668 add $const,$const,#.LM0ISR-_bsaes_decrypt8
670 vldmia $const!, {@XMM[8]} @ .LM0ISR
671 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
672 veor @XMM[11], @XMM[1], @XMM[9]
673 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
674 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
675 veor @XMM[12], @XMM[2], @XMM[9]
676 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
677 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
678 veor @XMM[13], @XMM[3], @XMM[9]
679 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
680 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
681 veor @XMM[14], @XMM[4], @XMM[9]
682 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
683 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
684 veor @XMM[15], @XMM[5], @XMM[9]
685 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
686 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
687 veor @XMM[10], @XMM[6], @XMM[9]
688 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
689 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
690 veor @XMM[11], @XMM[7], @XMM[9]
691 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
692 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
693 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
694 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
696 &bitslice (@XMM[0..7, 8..11]);
698 sub $rounds,$rounds,#1
703 &ShiftRows (@XMM[0..7, 8..12]);
704 $code.=".Ldec_sbox:\n";
705 &InvSbox (@XMM[0..7, 8..15]);
707 subs $rounds,$rounds,#1
710 &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
712 vldmia $const, {@XMM[12]} @ .LISR
713 ite eq @ Thumb2 thing, sanity check in ARM
714 addeq $const,$const,#0x10
716 vldmia $const, {@XMM[12]} @ .LISRM0
721 &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
723 vldmia $key, {@XMM[8]} @ last round key
724 veor @XMM[6], @XMM[6], @XMM[8]
725 veor @XMM[4], @XMM[4], @XMM[8]
726 veor @XMM[2], @XMM[2], @XMM[8]
727 veor @XMM[7], @XMM[7], @XMM[8]
728 veor @XMM[3], @XMM[3], @XMM[8]
729 veor @XMM[5], @XMM[5], @XMM[8]
730 veor @XMM[0], @XMM[0], @XMM[8]
731 veor @XMM[1], @XMM[1], @XMM[8]
733 .size _bsaes_decrypt8,.-_bsaes_decrypt8
735 .type _bsaes_const,%object
738 .LM0ISR: @ InvShiftRows constants
739 .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
741 .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
743 .quad 0x01040b0e0205080f, 0x0306090c00070a0d
744 .LM0SR: @ ShiftRows constants
745 .quad 0x0a0e02060f03070b, 0x0004080c05090d01
747 .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
749 .quad 0x0304090e00050a0f, 0x01060b0c0207080d
751 .quad 0x02060a0e03070b0f, 0x0004080c0105090d
753 .quad 0x090d01050c000408, 0x03070b0f060a0e02
754 .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
756 .size _bsaes_const,.-_bsaes_const
758 .type _bsaes_encrypt8,%function
761 adr $const,_bsaes_encrypt8
762 vldmia $key!, {@XMM[9]} @ round 0 key
763 sub $const,$const,#_bsaes_encrypt8-.LM0SR
765 vldmia $const!, {@XMM[8]} @ .LM0SR
767 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
768 veor @XMM[11], @XMM[1], @XMM[9]
769 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
770 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
771 veor @XMM[12], @XMM[2], @XMM[9]
772 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
773 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
774 veor @XMM[13], @XMM[3], @XMM[9]
775 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
776 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
777 veor @XMM[14], @XMM[4], @XMM[9]
778 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
779 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
780 veor @XMM[15], @XMM[5], @XMM[9]
781 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
782 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
783 veor @XMM[10], @XMM[6], @XMM[9]
784 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
785 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
786 veor @XMM[11], @XMM[7], @XMM[9]
787 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
788 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
789 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
790 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
791 _bsaes_encrypt8_bitslice:
793 &bitslice (@XMM[0..7, 8..11]);
795 sub $rounds,$rounds,#1
800 &ShiftRows (@XMM[0..7, 8..12]);
801 $code.=".Lenc_sbox:\n";
802 &Sbox (@XMM[0..7, 8..15]);
804 subs $rounds,$rounds,#1
807 &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
809 vldmia $const, {@XMM[12]} @ .LSR
810 ite eq @ Thumb2 thing, samity check in ARM
811 addeq $const,$const,#0x10
813 vldmia $const, {@XMM[12]} @ .LSRM0
818 # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
819 &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
821 vldmia $key, {@XMM[8]} @ last round key
822 veor @XMM[4], @XMM[4], @XMM[8]
823 veor @XMM[6], @XMM[6], @XMM[8]
824 veor @XMM[3], @XMM[3], @XMM[8]
825 veor @XMM[7], @XMM[7], @XMM[8]
826 veor @XMM[2], @XMM[2], @XMM[8]
827 veor @XMM[5], @XMM[5], @XMM[8]
828 veor @XMM[0], @XMM[0], @XMM[8]
829 veor @XMM[1], @XMM[1], @XMM[8]
831 .size _bsaes_encrypt8,.-_bsaes_encrypt8
835 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
838 my @x=reverse(@_[0..7]);
839 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
841 &swapmove (@x[0,1],1,$bs0,$t2,$t3);
843 @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
847 #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
849 &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
851 @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
857 &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
858 &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
862 .type _bsaes_key_convert,%function
865 adr $const,_bsaes_key_convert
866 vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
867 sub $const,$const,#_bsaes_key_convert-.LM0
868 vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
870 vmov.i8 @XMM[8], #0x01 @ bit masks
871 vmov.i8 @XMM[9], #0x02
872 vmov.i8 @XMM[10], #0x04
873 vmov.i8 @XMM[11], #0x08
874 vmov.i8 @XMM[12], #0x10
875 vmov.i8 @XMM[13], #0x20
876 vldmia $const, {@XMM[14]} @ .LM0
879 vrev32.8 @XMM[7], @XMM[7]
880 vrev32.8 @XMM[15], @XMM[15]
882 sub $rounds,$rounds,#1
883 vstmia $out!, {@XMM[7]} @ save round 0 key
888 vtbl.8 `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
889 vtbl.8 `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
890 vmov.i8 @XMM[6], #0x40
891 vmov.i8 @XMM[15], #0x80
893 vtst.8 @XMM[0], @XMM[7], @XMM[8]
894 vtst.8 @XMM[1], @XMM[7], @XMM[9]
895 vtst.8 @XMM[2], @XMM[7], @XMM[10]
896 vtst.8 @XMM[3], @XMM[7], @XMM[11]
897 vtst.8 @XMM[4], @XMM[7], @XMM[12]
898 vtst.8 @XMM[5], @XMM[7], @XMM[13]
899 vtst.8 @XMM[6], @XMM[7], @XMM[6]
900 vtst.8 @XMM[7], @XMM[7], @XMM[15]
901 vld1.8 {@XMM[15]}, [$inp]! @ load next round key
902 vmvn @XMM[0], @XMM[0] @ "pnot"
903 vmvn @XMM[1], @XMM[1]
904 vmvn @XMM[5], @XMM[5]
905 vmvn @XMM[6], @XMM[6]
907 vrev32.8 @XMM[15], @XMM[15]
909 subs $rounds,$rounds,#1
910 vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
913 vmov.i8 @XMM[7],#0x63 @ compose .L63
914 @ don't save last round key
916 .size _bsaes_key_convert,.-_bsaes_key_convert
920 if (0) { # following four functions are unsupported interface
921 # used for benchmarking...
923 .globl bsaes_enc_key_convert
924 .type bsaes_enc_key_convert,%function
926 bsaes_enc_key_convert:
928 vstmdb sp!,{d8-d15} @ ABI specification says so
930 ldr r5,[$inp,#240] @ pass rounds
931 mov r4,$inp @ pass key
932 mov r12,$out @ pass key schedule
933 bl _bsaes_key_convert
934 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
935 vstmia r12, {@XMM[7]} @ save last round key
939 .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
941 .globl bsaes_encrypt_128
942 .type bsaes_encrypt_128,%function
946 vstmdb sp!,{d8-d15} @ ABI specification says so
948 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
949 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
950 mov r4,$key @ pass the key
951 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
952 mov r5,#10 @ pass rounds
953 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
957 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
958 vst1.8 {@XMM[4]}, [$out]!
959 vst1.8 {@XMM[6]}, [$out]!
960 vst1.8 {@XMM[3]}, [$out]!
961 vst1.8 {@XMM[7]}, [$out]!
962 vst1.8 {@XMM[2]}, [$out]!
964 vst1.8 {@XMM[5]}, [$out]!
969 .size bsaes_encrypt_128,.-bsaes_encrypt_128
971 .globl bsaes_dec_key_convert
972 .type bsaes_dec_key_convert,%function
974 bsaes_dec_key_convert:
976 vstmdb sp!,{d8-d15} @ ABI specification says so
978 ldr r5,[$inp,#240] @ pass rounds
979 mov r4,$inp @ pass key
980 mov r12,$out @ pass key schedule
981 bl _bsaes_key_convert
982 vldmia $out, {@XMM[6]}
983 vstmia r12, {@XMM[15]} @ save last round key
984 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
985 vstmia $out, {@XMM[7]}
989 .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
991 .globl bsaes_decrypt_128
992 .type bsaes_decrypt_128,%function
996 vstmdb sp!,{d8-d15} @ ABI specification says so
998 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
999 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1000 mov r4,$key @ pass the key
1001 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1002 mov r5,#10 @ pass rounds
1003 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1007 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1008 vst1.8 {@XMM[6]}, [$out]!
1009 vst1.8 {@XMM[4]}, [$out]!
1010 vst1.8 {@XMM[2]}, [$out]!
1011 vst1.8 {@XMM[7]}, [$out]!
1012 vst1.8 {@XMM[3]}, [$out]!
1013 subs $len,$len,#0x80
1014 vst1.8 {@XMM[5]}, [$out]!
1018 ldmia sp!,{r4-r6,pc}
1019 .size bsaes_decrypt_128,.-bsaes_decrypt_128
1023 my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
1024 my ($keysched)=("sp");
1027 .extern AES_cbc_encrypt
1030 .global bsaes_cbc_encrypt
1031 .type bsaes_cbc_encrypt,%function
1045 @ it is up to the caller to make sure we are called with enc == 0
1048 stmdb sp!, {r4-r10, lr}
1050 ldr $ivp, [ip] @ IV is 1st arg on the stack
1051 mov $len, $len, lsr#4 @ len in 16 byte blocks
1052 sub sp, #0x10 @ scratch space to carry over the IV
1053 mov $fp, sp @ save sp
1055 ldr $rounds, [$key, #240] @ get # of rounds
1056 #ifndef BSAES_ASM_EXTENDED_KEY
1057 @ allocate the key schedule on the stack
1058 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1059 add r12, #`128-32` @ sifze of bit-slices key schedule
1061 @ populate the key schedule
1062 mov r4, $key @ pass key
1063 mov r5, $rounds @ pass # of rounds
1064 mov sp, r12 @ sp is $keysched
1065 bl _bsaes_key_convert
1066 vldmia $keysched, {@XMM[6]}
1067 vstmia r12, {@XMM[15]} @ save last round key
1068 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1069 vstmia $keysched, {@XMM[7]}
1071 ldr r12, [$key, #244]
1075 @ populate the key schedule
1076 str r12, [$key, #244]
1077 mov r4, $key @ pass key
1078 mov r5, $rounds @ pass # of rounds
1079 add r12, $key, #248 @ pass key schedule
1080 bl _bsaes_key_convert
1082 vldmia r4, {@XMM[6]}
1083 vstmia r12, {@XMM[15]} @ save last round key
1084 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1085 vstmia r4, {@XMM[7]}
1091 vld1.8 {@XMM[15]}, [$ivp] @ load IV
1096 subs $len, $len, #0x8
1097 bmi .Lcbc_dec_loop_finish
1099 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1100 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1101 #ifndef BSAES_ASM_EXTENDED_KEY
1102 mov r4, $keysched @ pass the key
1106 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1108 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]
1109 sub $inp, $inp, #0x60
1110 vstmia $fp, {@XMM[15]} @ put aside IV
1114 vldmia $fp, {@XMM[14]} @ reload IV
1115 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1116 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1117 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1118 veor @XMM[1], @XMM[1], @XMM[8]
1119 veor @XMM[6], @XMM[6], @XMM[9]
1120 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1121 veor @XMM[4], @XMM[4], @XMM[10]
1122 veor @XMM[2], @XMM[2], @XMM[11]
1123 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1124 veor @XMM[7], @XMM[7], @XMM[12]
1125 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1126 veor @XMM[3], @XMM[3], @XMM[13]
1127 vst1.8 {@XMM[6]}, [$out]!
1128 veor @XMM[5], @XMM[5], @XMM[14]
1129 vst1.8 {@XMM[4]}, [$out]!
1130 vst1.8 {@XMM[2]}, [$out]!
1131 vst1.8 {@XMM[7]}, [$out]!
1132 vst1.8 {@XMM[3]}, [$out]!
1133 vst1.8 {@XMM[5]}, [$out]!
1137 .Lcbc_dec_loop_finish:
1141 vld1.8 {@XMM[0]}, [$inp]! @ load input
1144 vld1.8 {@XMM[1]}, [$inp]!
1145 #ifndef BSAES_ASM_EXTENDED_KEY
1146 mov r4, $keysched @ pass the key
1151 vstmia $fp, {@XMM[15]} @ put aside IV
1153 vld1.8 {@XMM[2]}, [$inp]!
1156 vld1.8 {@XMM[3]}, [$inp]!
1158 vld1.8 {@XMM[4]}, [$inp]!
1161 vld1.8 {@XMM[5]}, [$inp]!
1163 vld1.8 {@XMM[6]}, [$inp]!
1164 sub $inp, $inp, #0x70
1168 vldmia $fp, {@XMM[14]} @ reload IV
1169 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1170 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1171 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1172 veor @XMM[1], @XMM[1], @XMM[8]
1173 veor @XMM[6], @XMM[6], @XMM[9]
1174 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1175 veor @XMM[4], @XMM[4], @XMM[10]
1176 veor @XMM[2], @XMM[2], @XMM[11]
1177 vld1.8 {@XMM[15]}, [$inp]!
1178 veor @XMM[7], @XMM[7], @XMM[12]
1179 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1180 veor @XMM[3], @XMM[3], @XMM[13]
1181 vst1.8 {@XMM[6]}, [$out]!
1182 vst1.8 {@XMM[4]}, [$out]!
1183 vst1.8 {@XMM[2]}, [$out]!
1184 vst1.8 {@XMM[7]}, [$out]!
1185 vst1.8 {@XMM[3]}, [$out]!
1189 sub $inp, $inp, #0x60
1191 vldmia $fp,{@XMM[14]} @ reload IV
1192 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1193 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1194 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1195 veor @XMM[1], @XMM[1], @XMM[8]
1196 veor @XMM[6], @XMM[6], @XMM[9]
1197 vld1.8 {@XMM[12]}, [$inp]!
1198 veor @XMM[4], @XMM[4], @XMM[10]
1199 veor @XMM[2], @XMM[2], @XMM[11]
1200 vld1.8 {@XMM[15]}, [$inp]!
1201 veor @XMM[7], @XMM[7], @XMM[12]
1202 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1203 vst1.8 {@XMM[6]}, [$out]!
1204 vst1.8 {@XMM[4]}, [$out]!
1205 vst1.8 {@XMM[2]}, [$out]!
1206 vst1.8 {@XMM[7]}, [$out]!
1210 sub $inp, $inp, #0x50
1212 vldmia $fp, {@XMM[14]} @ reload IV
1213 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1214 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1215 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1216 veor @XMM[1], @XMM[1], @XMM[8]
1217 veor @XMM[6], @XMM[6], @XMM[9]
1218 vld1.8 {@XMM[15]}, [$inp]!
1219 veor @XMM[4], @XMM[4], @XMM[10]
1220 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1221 veor @XMM[2], @XMM[2], @XMM[11]
1222 vst1.8 {@XMM[6]}, [$out]!
1223 vst1.8 {@XMM[4]}, [$out]!
1224 vst1.8 {@XMM[2]}, [$out]!
1228 sub $inp, $inp, #0x40
1230 vldmia $fp, {@XMM[14]} @ reload IV
1231 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1232 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1233 vld1.8 {@XMM[10]}, [$inp]!
1234 veor @XMM[1], @XMM[1], @XMM[8]
1235 veor @XMM[6], @XMM[6], @XMM[9]
1236 vld1.8 {@XMM[15]}, [$inp]!
1237 veor @XMM[4], @XMM[4], @XMM[10]
1238 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1239 vst1.8 {@XMM[6]}, [$out]!
1240 vst1.8 {@XMM[4]}, [$out]!
1244 sub $inp, $inp, #0x30
1246 vldmia $fp, {@XMM[14]} @ reload IV
1247 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1248 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1249 vld1.8 {@XMM[15]}, [$inp]!
1250 veor @XMM[1], @XMM[1], @XMM[8]
1251 veor @XMM[6], @XMM[6], @XMM[9]
1252 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1253 vst1.8 {@XMM[6]}, [$out]!
1257 sub $inp, $inp, #0x20
1259 vldmia $fp, {@XMM[14]} @ reload IV
1260 vld1.8 {@XMM[8]}, [$inp]! @ reload input
1261 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1262 vld1.8 {@XMM[15]}, [$inp]! @ reload input
1263 veor @XMM[1], @XMM[1], @XMM[8]
1264 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1268 sub $inp, $inp, #0x10
1269 mov $rounds, $out @ save original out pointer
1270 mov $out, $fp @ use the iv scratch space as out buffer
1272 vmov @XMM[4],@XMM[15] @ just in case ensure that IV
1273 vmov @XMM[5],@XMM[0] @ and input are preserved
1275 vld1.8 {@XMM[0]}, [$fp,:64] @ load result
1276 veor @XMM[0], @XMM[0], @XMM[4] @ ^= IV
1277 vmov @XMM[15], @XMM[5] @ @XMM[5] holds input
1278 vst1.8 {@XMM[0]}, [$rounds] @ write output
1281 #ifndef BSAES_ASM_EXTENDED_KEY
1284 .Lcbc_dec_bzero: @ wipe key schedule [if any]
1285 vstmia $keysched!, {q0-q1}
1291 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1292 vst1.8 {@XMM[15]}, [$ivp] @ return IV
1294 ldmia sp!, {r4-r10, pc}
1295 .size bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
1299 my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1300 my $const = "r6"; # shared with _bsaes_encrypt8_alt
1301 my $keysched = "sp";
1305 .global bsaes_ctr32_encrypt_blocks
1306 .type bsaes_ctr32_encrypt_blocks,%function
1308 bsaes_ctr32_encrypt_blocks:
1309 cmp $len, #8 @ use plain AES for
1310 blo .Lctr_enc_short @ small sizes
1313 stmdb sp!, {r4-r10, lr}
1315 ldr $ctr, [ip] @ ctr is 1st arg on the stack
1316 sub sp, sp, #0x10 @ scratch space to carry over the ctr
1317 mov $fp, sp @ save sp
1319 ldr $rounds, [$key, #240] @ get # of rounds
1320 #ifndef BSAES_ASM_EXTENDED_KEY
1321 @ allocate the key schedule on the stack
1322 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1323 add r12, #`128-32` @ size of bit-sliced key schedule
1325 @ populate the key schedule
1326 mov r4, $key @ pass key
1327 mov r5, $rounds @ pass # of rounds
1328 mov sp, r12 @ sp is $keysched
1329 bl _bsaes_key_convert
1330 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1331 vstmia r12, {@XMM[7]} @ save last round key
1333 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1334 add $ctr, $const, #.LREVM0SR-.LM0 @ borrow $ctr
1335 vldmia $keysched, {@XMM[4]} @ load round0 key
1337 ldr r12, [$key, #244]
1341 @ populate the key schedule
1342 str r12, [$key, #244]
1343 mov r4, $key @ pass key
1344 mov r5, $rounds @ pass # of rounds
1345 add r12, $key, #248 @ pass key schedule
1346 bl _bsaes_key_convert
1347 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1348 vstmia r12, {@XMM[7]} @ save last round key
1351 0: add r12, $key, #248
1352 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1353 adrl $ctr, .LREVM0SR @ borrow $ctr
1354 vldmia r12, {@XMM[4]} @ load round0 key
1355 sub sp, #0x10 @ place for adjusted round0 key
1358 vmov.i32 @XMM[8],#1 @ compose 1<<96
1359 veor @XMM[9],@XMM[9],@XMM[9]
1360 vrev32.8 @XMM[0],@XMM[0]
1361 vext.8 @XMM[8],@XMM[9],@XMM[8],#4
1362 vrev32.8 @XMM[4],@XMM[4]
1363 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1364 vstmia $keysched, {@XMM[4]} @ save adjusted round0 key
1369 vadd.u32 @XMM[10], @XMM[8], @XMM[9] @ compose 3<<96
1370 vadd.u32 @XMM[1], @XMM[0], @XMM[8] @ +1
1371 vadd.u32 @XMM[2], @XMM[0], @XMM[9] @ +2
1372 vadd.u32 @XMM[3], @XMM[0], @XMM[10] @ +3
1373 vadd.u32 @XMM[4], @XMM[1], @XMM[10]
1374 vadd.u32 @XMM[5], @XMM[2], @XMM[10]
1375 vadd.u32 @XMM[6], @XMM[3], @XMM[10]
1376 vadd.u32 @XMM[7], @XMM[4], @XMM[10]
1377 vadd.u32 @XMM[10], @XMM[5], @XMM[10] @ next counter
1379 @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1380 @ to flip byte order in 32-bit counter
1382 vldmia $keysched, {@XMM[9]} @ load round0 key
1383 #ifndef BSAES_ASM_EXTENDED_KEY
1384 add r4, $keysched, #0x10 @ pass next round key
1386 add r4, $key, #`248+16`
1388 vldmia $ctr, {@XMM[8]} @ .LREVM0SR
1389 mov r5, $rounds @ pass rounds
1390 vstmia $fp, {@XMM[10]} @ save next counter
1391 sub $const, $ctr, #.LREVM0SR-.LSR @ pass constants
1393 bl _bsaes_encrypt8_alt
1396 blo .Lctr_enc_loop_done
1398 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ load input
1399 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1400 veor @XMM[0], @XMM[8]
1401 veor @XMM[1], @XMM[9]
1402 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1403 veor @XMM[4], @XMM[10]
1404 veor @XMM[6], @XMM[11]
1405 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1406 veor @XMM[3], @XMM[12]
1407 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1408 veor @XMM[7], @XMM[13]
1409 veor @XMM[2], @XMM[14]
1410 vst1.8 {@XMM[4]}, [$out]!
1411 veor @XMM[5], @XMM[15]
1412 vst1.8 {@XMM[6]}, [$out]!
1413 vmov.i32 @XMM[8], #1 @ compose 1<<96
1414 vst1.8 {@XMM[3]}, [$out]!
1415 veor @XMM[9], @XMM[9], @XMM[9]
1416 vst1.8 {@XMM[7]}, [$out]!
1417 vext.8 @XMM[8], @XMM[9], @XMM[8], #4
1418 vst1.8 {@XMM[2]}, [$out]!
1419 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1420 vst1.8 {@XMM[5]}, [$out]!
1421 vldmia $fp, {@XMM[0]} @ load counter
1427 .Lctr_enc_loop_done:
1429 vld1.8 {@XMM[8]}, [$inp]! @ load input
1430 veor @XMM[0], @XMM[8]
1431 vst1.8 {@XMM[0]}, [$out]! @ write output
1434 vld1.8 {@XMM[9]}, [$inp]!
1435 veor @XMM[1], @XMM[9]
1436 vst1.8 {@XMM[1]}, [$out]!
1438 vld1.8 {@XMM[10]}, [$inp]!
1439 veor @XMM[4], @XMM[10]
1440 vst1.8 {@XMM[4]}, [$out]!
1443 vld1.8 {@XMM[11]}, [$inp]!
1444 veor @XMM[6], @XMM[11]
1445 vst1.8 {@XMM[6]}, [$out]!
1447 vld1.8 {@XMM[12]}, [$inp]!
1448 veor @XMM[3], @XMM[12]
1449 vst1.8 {@XMM[3]}, [$out]!
1452 vld1.8 {@XMM[13]}, [$inp]!
1453 veor @XMM[7], @XMM[13]
1454 vst1.8 {@XMM[7]}, [$out]!
1456 vld1.8 {@XMM[14]}, [$inp]
1457 veor @XMM[2], @XMM[14]
1458 vst1.8 {@XMM[2]}, [$out]!
1463 #ifndef BSAES_ASM_EXTENDED_KEY
1464 .Lctr_enc_bzero: @ wipe key schedule [if any]
1465 vstmia $keysched!, {q0-q1}
1469 vstmia $keysched, {q0-q1}
1473 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1475 ldmia sp!, {r4-r10, pc} @ return
1479 ldr ip, [sp] @ ctr pointer is passed on stack
1480 stmdb sp!, {r4-r8, lr}
1482 mov r4, $inp @ copy arguments
1486 ldr r8, [ip, #12] @ load counter LSW
1487 vld1.8 {@XMM[1]}, [ip] @ load whole counter value
1492 vst1.8 {@XMM[1]}, [sp,:64] @ copy counter value
1495 .Lctr_enc_short_loop:
1496 add r0, sp, #0x10 @ input counter value
1497 mov r1, sp @ output on the stack
1502 vld1.8 {@XMM[0]}, [r4]! @ load input
1503 vld1.8 {@XMM[1]}, [sp,:64] @ load encrypted counter
1507 str r0, [sp, #0x1c] @ next counter value
1509 str r8, [sp, #0x1c] @ next counter value
1511 veor @XMM[0],@XMM[0],@XMM[1]
1512 vst1.8 {@XMM[0]}, [r5]! @ store output
1514 bne .Lctr_enc_short_loop
1520 ldmia sp!, {r4-r8, pc}
1521 .size bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
1525 ######################################################################
1526 # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1527 # const AES_KEY *key1, const AES_KEY *key2,
1528 # const unsigned char iv[16]);
1530 my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
1531 my $const="r6"; # returned by _bsaes_key_convert
1536 .globl bsaes_xts_encrypt
1537 .type bsaes_xts_encrypt,%function
1541 stmdb sp!, {r4-r10, lr} @ 0x20
1543 mov r6, sp @ future $fp
1550 sub r0, sp, #0x10 @ 0x10
1551 bic r0, #0xf @ align at 16 bytes
1554 #ifdef XTS_CHAIN_TWEAK
1555 ldr r0, [ip] @ pointer to input tweak
1557 @ generate initial tweak
1558 ldr r0, [ip, #4] @ iv[]
1560 ldr r2, [ip, #0] @ key2
1562 mov r0,sp @ pointer to initial tweak
1565 ldr $rounds, [$key, #240] @ get # of rounds
1567 #ifndef BSAES_ASM_EXTENDED_KEY
1568 @ allocate the key schedule on the stack
1569 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1570 @ add r12, #`128-32` @ size of bit-sliced key schedule
1571 sub r12, #`32+16` @ place for tweak[9]
1573 @ populate the key schedule
1574 mov r4, $key @ pass key
1575 mov r5, $rounds @ pass # of rounds
1577 add r12, #0x90 @ pass key schedule
1578 bl _bsaes_key_convert
1579 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1580 vstmia r12, {@XMM[7]} @ save last round key
1582 ldr r12, [$key, #244]
1586 str r12, [$key, #244]
1587 mov r4, $key @ pass key
1588 mov r5, $rounds @ pass # of rounds
1589 add r12, $key, #248 @ pass key schedule
1590 bl _bsaes_key_convert
1591 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1592 vstmia r12, {@XMM[7]}
1595 0: sub sp, #0x90 @ place for tweak[9]
1598 vld1.8 {@XMM[8]}, [r0] @ initial tweak
1599 adr $magic, .Lxts_magic
1607 vldmia $magic, {$twmask} @ load XTS magic
1608 vshr.s64 @T[0], @XMM[8], #63
1610 vand @T[0], @T[0], $twmask
1612 for($i=9;$i<16;$i++) {
1614 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1615 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1616 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1617 vshr.s64 @T[1], @XMM[$i], #63
1618 veor @XMM[$i], @XMM[$i], @T[0]
1619 vand @T[1], @T[1], $twmask
1623 $code.=<<___ if ($i>=10);
1624 vld1.8 {@XMM[$i-10]}, [$inp]!
1626 $code.=<<___ if ($i>=11);
1627 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1631 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
1632 vst1.64 {@XMM[15]}, [r0,:128]!
1633 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1634 veor @XMM[8], @XMM[8], @T[0]
1635 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1637 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1638 veor @XMM[5], @XMM[5], @XMM[13]
1639 #ifndef BSAES_ASM_EXTENDED_KEY
1640 add r4, sp, #0x90 @ pass key schedule
1642 add r4, $key, #248 @ pass key schedule
1644 veor @XMM[6], @XMM[6], @XMM[14]
1645 mov r5, $rounds @ pass rounds
1646 veor @XMM[7], @XMM[7], @XMM[15]
1651 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1652 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1653 veor @XMM[0], @XMM[0], @XMM[ 8]
1654 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1655 veor @XMM[1], @XMM[1], @XMM[ 9]
1656 veor @XMM[8], @XMM[4], @XMM[10]
1657 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1658 veor @XMM[9], @XMM[6], @XMM[11]
1659 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
1660 veor @XMM[10], @XMM[3], @XMM[12]
1661 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1662 veor @XMM[11], @XMM[7], @XMM[13]
1663 veor @XMM[12], @XMM[2], @XMM[14]
1664 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1665 veor @XMM[13], @XMM[5], @XMM[15]
1666 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
1668 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1677 vldmia $magic, {$twmask} @ load XTS magic
1678 vshr.s64 @T[0], @XMM[8], #63
1680 vand @T[0], @T[0], $twmask
1682 for($i=9;$i<16;$i++) {
1684 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1685 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1686 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1687 vshr.s64 @T[1], @XMM[$i], #63
1688 veor @XMM[$i], @XMM[$i], @T[0]
1689 vand @T[1], @T[1], $twmask
1693 $code.=<<___ if ($i>=10);
1694 vld1.8 {@XMM[$i-10]}, [$inp]!
1696 bmi .Lxts_enc_`$i-9`
1698 $code.=<<___ if ($i>=11);
1699 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1704 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
1706 vld1.8 {@XMM[6]}, [$inp]!
1707 veor @XMM[5], @XMM[5], @XMM[13]
1708 #ifndef BSAES_ASM_EXTENDED_KEY
1709 add r4, sp, #0x90 @ pass key schedule
1711 add r4, $key, #248 @ pass key schedule
1713 veor @XMM[6], @XMM[6], @XMM[14]
1714 mov r5, $rounds @ pass rounds
1719 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1720 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1721 veor @XMM[0], @XMM[0], @XMM[ 8]
1722 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1723 veor @XMM[1], @XMM[1], @XMM[ 9]
1724 veor @XMM[8], @XMM[4], @XMM[10]
1725 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1726 veor @XMM[9], @XMM[6], @XMM[11]
1727 vld1.64 {@XMM[14]}, [r0,:128]!
1728 veor @XMM[10], @XMM[3], @XMM[12]
1729 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1730 veor @XMM[11], @XMM[7], @XMM[13]
1731 veor @XMM[12], @XMM[2], @XMM[14]
1732 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1733 vst1.8 {@XMM[12]}, [$out]!
1735 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1739 vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
1741 veor @XMM[4], @XMM[4], @XMM[12]
1742 #ifndef BSAES_ASM_EXTENDED_KEY
1743 add r4, sp, #0x90 @ pass key schedule
1745 add r4, $key, #248 @ pass key schedule
1747 veor @XMM[5], @XMM[5], @XMM[13]
1748 mov r5, $rounds @ pass rounds
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 veor @XMM[10], @XMM[3], @XMM[12]
1762 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1763 veor @XMM[11], @XMM[7], @XMM[13]
1764 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1766 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1769 @ put this in range for both ARM and Thumb mode adr instructions
1776 vst1.64 {@XMM[13]}, [r0,:128] @ next round tweak
1778 veor @XMM[3], @XMM[3], @XMM[11]
1779 #ifndef BSAES_ASM_EXTENDED_KEY
1780 add r4, sp, #0x90 @ pass key schedule
1782 add r4, $key, #248 @ pass key schedule
1784 veor @XMM[4], @XMM[4], @XMM[12]
1785 mov r5, $rounds @ pass rounds
1790 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1791 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1792 veor @XMM[0], @XMM[0], @XMM[ 8]
1793 vld1.64 {@XMM[12]}, [r0,:128]!
1794 veor @XMM[1], @XMM[1], @XMM[ 9]
1795 veor @XMM[8], @XMM[4], @XMM[10]
1796 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1797 veor @XMM[9], @XMM[6], @XMM[11]
1798 veor @XMM[10], @XMM[3], @XMM[12]
1799 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1800 vst1.8 {@XMM[10]}, [$out]!
1802 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1806 vst1.64 {@XMM[12]}, [r0,:128] @ next round tweak
1808 veor @XMM[2], @XMM[2], @XMM[10]
1809 #ifndef BSAES_ASM_EXTENDED_KEY
1810 add r4, sp, #0x90 @ pass key schedule
1812 add r4, $key, #248 @ pass key schedule
1814 veor @XMM[3], @XMM[3], @XMM[11]
1815 mov r5, $rounds @ pass rounds
1820 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1821 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1822 veor @XMM[0], @XMM[0], @XMM[ 8]
1823 veor @XMM[1], @XMM[1], @XMM[ 9]
1824 veor @XMM[8], @XMM[4], @XMM[10]
1825 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1826 veor @XMM[9], @XMM[6], @XMM[11]
1827 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1829 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1833 vst1.64 {@XMM[11]}, [r0,:128] @ next round tweak
1835 veor @XMM[1], @XMM[1], @XMM[9]
1836 #ifndef BSAES_ASM_EXTENDED_KEY
1837 add r4, sp, #0x90 @ pass key schedule
1839 add r4, $key, #248 @ pass key schedule
1841 veor @XMM[2], @XMM[2], @XMM[10]
1842 mov r5, $rounds @ pass rounds
1847 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1848 vld1.64 {@XMM[10]}, [r0,:128]!
1849 veor @XMM[0], @XMM[0], @XMM[ 8]
1850 veor @XMM[1], @XMM[1], @XMM[ 9]
1851 veor @XMM[8], @XMM[4], @XMM[10]
1852 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1853 vst1.8 {@XMM[8]}, [$out]!
1855 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1859 vst1.64 {@XMM[10]}, [r0,:128] @ next round tweak
1861 veor @XMM[0], @XMM[0], @XMM[8]
1862 #ifndef BSAES_ASM_EXTENDED_KEY
1863 add r4, sp, #0x90 @ pass key schedule
1865 add r4, $key, #248 @ pass key schedule
1867 veor @XMM[1], @XMM[1], @XMM[9]
1868 mov r5, $rounds @ pass rounds
1873 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1874 veor @XMM[0], @XMM[0], @XMM[ 8]
1875 veor @XMM[1], @XMM[1], @XMM[ 9]
1876 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1878 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1883 veor @XMM[0], @XMM[8]
1885 vst1.8 {@XMM[0]}, [sp,:128]
1887 mov r4, $fp @ preserve fp
1891 vld1.8 {@XMM[0]}, [sp,:128]
1892 veor @XMM[0], @XMM[0], @XMM[8]
1893 vst1.8 {@XMM[0]}, [$out]!
1896 vmov @XMM[8], @XMM[9] @ next round tweak
1899 #ifndef XTS_CHAIN_TWEAK
1906 ldrb r1, [$out, #-0x10]
1907 strb r0, [$out, #-0x10]
1913 vld1.8 {@XMM[0]}, [r6]
1915 veor @XMM[0], @XMM[0], @XMM[8]
1917 vst1.8 {@XMM[0]}, [sp,:128]
1919 mov r4, $fp @ preserve fp
1923 vld1.8 {@XMM[0]}, [sp,:128]
1924 veor @XMM[0], @XMM[0], @XMM[8]
1925 vst1.8 {@XMM[0]}, [r6]
1933 #ifdef XTS_CHAIN_TWEAK
1934 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
1936 .Lxts_enc_bzero: @ wipe key schedule [if any]
1942 #ifdef XTS_CHAIN_TWEAK
1943 vst1.8 {@XMM[8]}, [r1]
1946 ldmia sp!, {r4-r10, pc} @ return
1948 .size bsaes_xts_encrypt,.-bsaes_xts_encrypt
1950 .globl bsaes_xts_decrypt
1951 .type bsaes_xts_decrypt,%function
1955 stmdb sp!, {r4-r10, lr} @ 0x20
1957 mov r6, sp @ future $fp
1964 sub r0, sp, #0x10 @ 0x10
1965 bic r0, #0xf @ align at 16 bytes
1968 #ifdef XTS_CHAIN_TWEAK
1969 ldr r0, [ip] @ pointer to input tweak
1971 @ generate initial tweak
1972 ldr r0, [ip, #4] @ iv[]
1974 ldr r2, [ip, #0] @ key2
1976 mov r0, sp @ pointer to initial tweak
1979 ldr $rounds, [$key, #240] @ get # of rounds
1981 #ifndef BSAES_ASM_EXTENDED_KEY
1982 @ allocate the key schedule on the stack
1983 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1984 @ add r12, #`128-32` @ size of bit-sliced key schedule
1985 sub r12, #`32+16` @ place for tweak[9]
1987 @ populate the key schedule
1988 mov r4, $key @ pass key
1989 mov r5, $rounds @ pass # of rounds
1991 add r12, #0x90 @ pass key schedule
1992 bl _bsaes_key_convert
1994 vldmia r4, {@XMM[6]}
1995 vstmia r12, {@XMM[15]} @ save last round key
1996 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1997 vstmia r4, {@XMM[7]}
1999 ldr r12, [$key, #244]
2003 str r12, [$key, #244]
2004 mov r4, $key @ pass key
2005 mov r5, $rounds @ pass # of rounds
2006 add r12, $key, #248 @ pass key schedule
2007 bl _bsaes_key_convert
2009 vldmia r4, {@XMM[6]}
2010 vstmia r12, {@XMM[15]} @ save last round key
2011 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2012 vstmia r4, {@XMM[7]}
2015 0: sub sp, #0x90 @ place for tweak[9]
2017 vld1.8 {@XMM[8]}, [r0] @ initial tweak
2018 adr $magic, .Lxts_magic
2020 tst $len, #0xf @ if not multiple of 16
2021 it ne @ Thumb2 thing, sanity check in ARM
2022 subne $len, #0x10 @ subtract another 16 bytes
2030 vldmia $magic, {$twmask} @ load XTS magic
2031 vshr.s64 @T[0], @XMM[8], #63
2033 vand @T[0], @T[0], $twmask
2035 for($i=9;$i<16;$i++) {
2037 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2038 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2039 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2040 vshr.s64 @T[1], @XMM[$i], #63
2041 veor @XMM[$i], @XMM[$i], @T[0]
2042 vand @T[1], @T[1], $twmask
2046 $code.=<<___ if ($i>=10);
2047 vld1.8 {@XMM[$i-10]}, [$inp]!
2049 $code.=<<___ if ($i>=11);
2050 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2054 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
2055 vst1.64 {@XMM[15]}, [r0,:128]!
2056 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2057 veor @XMM[8], @XMM[8], @T[0]
2058 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2060 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
2061 veor @XMM[5], @XMM[5], @XMM[13]
2062 #ifndef BSAES_ASM_EXTENDED_KEY
2063 add r4, sp, #0x90 @ pass key schedule
2065 add r4, $key, #248 @ pass key schedule
2067 veor @XMM[6], @XMM[6], @XMM[14]
2068 mov r5, $rounds @ pass rounds
2069 veor @XMM[7], @XMM[7], @XMM[15]
2074 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2075 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2076 veor @XMM[0], @XMM[0], @XMM[ 8]
2077 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2078 veor @XMM[1], @XMM[1], @XMM[ 9]
2079 veor @XMM[8], @XMM[6], @XMM[10]
2080 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2081 veor @XMM[9], @XMM[4], @XMM[11]
2082 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
2083 veor @XMM[10], @XMM[2], @XMM[12]
2084 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2085 veor @XMM[11], @XMM[7], @XMM[13]
2086 veor @XMM[12], @XMM[3], @XMM[14]
2087 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2088 veor @XMM[13], @XMM[5], @XMM[15]
2089 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
2091 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2100 vldmia $magic, {$twmask} @ load XTS magic
2101 vshr.s64 @T[0], @XMM[8], #63
2103 vand @T[0], @T[0], $twmask
2105 for($i=9;$i<16;$i++) {
2107 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2108 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2109 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2110 vshr.s64 @T[1], @XMM[$i], #63
2111 veor @XMM[$i], @XMM[$i], @T[0]
2112 vand @T[1], @T[1], $twmask
2116 $code.=<<___ if ($i>=10);
2117 vld1.8 {@XMM[$i-10]}, [$inp]!
2119 bmi .Lxts_dec_`$i-9`
2121 $code.=<<___ if ($i>=11);
2122 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2127 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
2129 vld1.8 {@XMM[6]}, [$inp]!
2130 veor @XMM[5], @XMM[5], @XMM[13]
2131 #ifndef BSAES_ASM_EXTENDED_KEY
2132 add r4, sp, #0x90 @ pass key schedule
2134 add r4, $key, #248 @ pass key schedule
2136 veor @XMM[6], @XMM[6], @XMM[14]
2137 mov r5, $rounds @ pass rounds
2142 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2143 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2144 veor @XMM[0], @XMM[0], @XMM[ 8]
2145 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2146 veor @XMM[1], @XMM[1], @XMM[ 9]
2147 veor @XMM[8], @XMM[6], @XMM[10]
2148 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2149 veor @XMM[9], @XMM[4], @XMM[11]
2150 vld1.64 {@XMM[14]}, [r0,:128]!
2151 veor @XMM[10], @XMM[2], @XMM[12]
2152 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2153 veor @XMM[11], @XMM[7], @XMM[13]
2154 veor @XMM[12], @XMM[3], @XMM[14]
2155 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2156 vst1.8 {@XMM[12]}, [$out]!
2158 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2162 vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
2164 veor @XMM[4], @XMM[4], @XMM[12]
2165 #ifndef BSAES_ASM_EXTENDED_KEY
2166 add r4, sp, #0x90 @ pass key schedule
2168 add r4, $key, #248 @ pass key schedule
2170 veor @XMM[5], @XMM[5], @XMM[13]
2171 mov r5, $rounds @ pass rounds
2176 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2177 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2178 veor @XMM[0], @XMM[0], @XMM[ 8]
2179 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2180 veor @XMM[1], @XMM[1], @XMM[ 9]
2181 veor @XMM[8], @XMM[6], @XMM[10]
2182 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2183 veor @XMM[9], @XMM[4], @XMM[11]
2184 veor @XMM[10], @XMM[2], @XMM[12]
2185 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2186 veor @XMM[11], @XMM[7], @XMM[13]
2187 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2189 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2193 vst1.64 {@XMM[13]}, [r0,:128] @ next round tweak
2195 veor @XMM[3], @XMM[3], @XMM[11]
2196 #ifndef BSAES_ASM_EXTENDED_KEY
2197 add r4, sp, #0x90 @ pass key schedule
2199 add r4, $key, #248 @ pass key schedule
2201 veor @XMM[4], @XMM[4], @XMM[12]
2202 mov r5, $rounds @ pass rounds
2207 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2208 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2209 veor @XMM[0], @XMM[0], @XMM[ 8]
2210 vld1.64 {@XMM[12]}, [r0,:128]!
2211 veor @XMM[1], @XMM[1], @XMM[ 9]
2212 veor @XMM[8], @XMM[6], @XMM[10]
2213 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2214 veor @XMM[9], @XMM[4], @XMM[11]
2215 veor @XMM[10], @XMM[2], @XMM[12]
2216 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2217 vst1.8 {@XMM[10]}, [$out]!
2219 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2223 vst1.64 {@XMM[12]}, [r0,:128] @ next round tweak
2225 veor @XMM[2], @XMM[2], @XMM[10]
2226 #ifndef BSAES_ASM_EXTENDED_KEY
2227 add r4, sp, #0x90 @ pass key schedule
2229 add r4, $key, #248 @ pass key schedule
2231 veor @XMM[3], @XMM[3], @XMM[11]
2232 mov r5, $rounds @ pass rounds
2237 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2238 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2239 veor @XMM[0], @XMM[0], @XMM[ 8]
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 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2246 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2250 vst1.64 {@XMM[11]}, [r0,:128] @ next round tweak
2252 veor @XMM[1], @XMM[1], @XMM[9]
2253 #ifndef BSAES_ASM_EXTENDED_KEY
2254 add r4, sp, #0x90 @ pass key schedule
2256 add r4, $key, #248 @ pass key schedule
2258 veor @XMM[2], @XMM[2], @XMM[10]
2259 mov r5, $rounds @ pass rounds
2264 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2265 vld1.64 {@XMM[10]}, [r0,:128]!
2266 veor @XMM[0], @XMM[0], @XMM[ 8]
2267 veor @XMM[1], @XMM[1], @XMM[ 9]
2268 veor @XMM[8], @XMM[6], @XMM[10]
2269 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2270 vst1.8 {@XMM[8]}, [$out]!
2272 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2276 vst1.64 {@XMM[10]}, [r0,:128] @ next round tweak
2278 veor @XMM[0], @XMM[0], @XMM[8]
2279 #ifndef BSAES_ASM_EXTENDED_KEY
2280 add r4, sp, #0x90 @ pass key schedule
2282 add r4, $key, #248 @ pass key schedule
2284 veor @XMM[1], @XMM[1], @XMM[9]
2285 mov r5, $rounds @ pass rounds
2290 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2291 veor @XMM[0], @XMM[0], @XMM[ 8]
2292 veor @XMM[1], @XMM[1], @XMM[ 9]
2293 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2295 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2300 veor @XMM[0], @XMM[8]
2302 vst1.8 {@XMM[0]}, [sp,:128]
2304 mov r4, $fp @ preserve fp
2305 mov r5, $magic @ preserve magic
2309 vld1.8 {@XMM[0]}, [sp,:128]
2310 veor @XMM[0], @XMM[0], @XMM[8]
2311 vst1.8 {@XMM[0]}, [$out]!
2315 vmov @XMM[8], @XMM[9] @ next round tweak
2318 #ifndef XTS_CHAIN_TWEAK
2322 @ calculate one round of extra tweak for the stolen ciphertext
2323 vldmia $magic, {$twmask}
2324 vshr.s64 @XMM[6], @XMM[8], #63
2325 vand @XMM[6], @XMM[6], $twmask
2326 vadd.u64 @XMM[9], @XMM[8], @XMM[8]
2327 vswp `&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
2328 veor @XMM[9], @XMM[9], @XMM[6]
2330 @ perform the final decryption with the last tweak value
2331 vld1.8 {@XMM[0]}, [$inp]!
2333 veor @XMM[0], @XMM[0], @XMM[9]
2335 vst1.8 {@XMM[0]}, [sp,:128]
2337 mov r4, $fp @ preserve fp
2341 vld1.8 {@XMM[0]}, [sp,:128]
2342 veor @XMM[0], @XMM[0], @XMM[9]
2343 vst1.8 {@XMM[0]}, [$out]
2349 strb r1, [$out, #0x10]
2355 vld1.8 {@XMM[0]}, [r6]
2357 veor @XMM[0], @XMM[8]
2359 vst1.8 {@XMM[0]}, [sp,:128]
2364 vld1.8 {@XMM[0]}, [sp,:128]
2365 veor @XMM[0], @XMM[0], @XMM[8]
2366 vst1.8 {@XMM[0]}, [r6]
2374 #ifdef XTS_CHAIN_TWEAK
2375 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2377 .Lxts_dec_bzero: @ wipe key schedule [if any]
2383 #ifdef XTS_CHAIN_TWEAK
2384 vst1.8 {@XMM[8]}, [r1]
2387 ldmia sp!, {r4-r10, pc} @ return
2389 .size bsaes_xts_decrypt,.-bsaes_xts_decrypt
2396 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
2401 last if (!s/^#/@/ and !/^$/);