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/.
8 # ====================================================================
10 # Bit-sliced AES for ARM NEON
14 # This implementation is direct adaptation of bsaes-x86_64 module for
15 # ARM NEON. Except that this module is endian-neutral [in sense that
16 # it can be compiled for either endianness] by courtesy of vld1.8's
17 # neutrality. Initial version doesn't implement interface to OpenSSL,
18 # only low-level primitives and unsupported entry points, just enough
19 # to collect performance results, which for Cortex-A8 core are:
21 # encrypt 20.9 cycles per byte processed with 128-bit key
22 # decrypt 25.6 cycles per byte processed with 128-bit key
23 # key conv. 900 cycles per 128-bit key/0.34 of 8x block
25 # When comparing to x86_64 results keep in mind that NEON unit is
26 # [mostly] single-issue and thus can't benefit from parallelism. And
27 # when comparing to aes-armv4 results keep in mind key schedule
28 # conversion overhead (see bsaes-x86_64.pl for details)...
32 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
33 open STDOUT,">$output";
35 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
36 my @XMM=map("q$_",(0..15));
39 my ($key,$rounds,$const)=("r4","r5","r6");
41 sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
42 sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
45 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
46 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
51 &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
52 &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
56 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
57 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
60 veor @b[5], @b[5], @b[6]
61 veor @b[2], @b[2], @b[1]
62 veor @b[3], @b[3], @b[0]
63 veor @b[6], @b[6], @b[2]
64 veor @b[5], @b[5], @b[0]
66 veor @b[6], @b[6], @b[3]
67 veor @b[3], @b[3], @b[7]
68 veor @b[7], @b[7], @b[5]
69 veor @b[3], @b[3], @b[4]
70 veor @b[4], @b[4], @b[5]
71 veor @b[3], @b[3], @b[1]
73 veor @b[2], @b[2], @b[7]
74 veor @b[1], @b[1], @b[5]
79 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
80 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
83 veor @b[0], @b[0], @b[6]
84 veor @b[1], @b[1], @b[4]
85 veor @b[2], @b[2], @b[0]
86 veor @b[4], @b[4], @b[6]
87 veor @b[6], @b[6], @b[1]
89 veor @b[1], @b[1], @b[5]
90 veor @b[5], @b[5], @b[3]
91 veor @b[3], @b[3], @b[7]
92 veor @b[7], @b[7], @b[5]
93 veor @b[2], @b[2], @b[5]
95 veor @b[4], @b[4], @b[7]
100 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
101 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
105 &InvInBasisChange (@b);
106 &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
107 &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
110 sub InvInBasisChange { # OutBasisChange in reverse
111 my @b=@_[5,1,2,6,3,7,0,4];
113 veor @b[4], @b[4], @b[7]
115 veor @b[7], @b[7], @b[5]
116 veor @b[2], @b[2], @b[5]
117 veor @b[3], @b[3], @b[7]
118 veor @b[5], @b[5], @b[3]
119 veor @b[1], @b[1], @b[5]
121 veor @b[6], @b[6], @b[1]
122 veor @b[2], @b[2], @b[0]
123 veor @b[4], @b[4], @b[6]
124 veor @b[0], @b[0], @b[6]
125 veor @b[1], @b[1], @b[4]
129 sub InvOutBasisChange { # InBasisChange in reverse
130 my @b=@_[2,5,7,3,6,1,0,4];
132 veor @b[1], @b[1], @b[5]
133 veor @b[2], @b[2], @b[7]
135 veor @b[3], @b[3], @b[1]
136 veor @b[4], @b[4], @b[5]
137 veor @b[7], @b[7], @b[5]
138 veor @b[3], @b[3], @b[4]
139 veor @b[5], @b[5], @b[0]
140 veor @b[3], @b[3], @b[7]
141 veor @b[6], @b[6], @b[2]
142 veor @b[2], @b[2], @b[1]
143 veor @b[6], @b[6], @b[3]
145 veor @b[3], @b[3], @b[0]
146 veor @b[5], @b[5], @b[6]
151 #;*************************************************************
152 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
153 #;*************************************************************
154 my ($x0,$x1,$y0,$y1,$t0)=@_;
166 sub Mul_GF4_N { # not used, see next subroutine
167 # multiply and scale by N
168 my ($x0,$x1,$y0,$y1,$t0)=@_;
181 # interleaved Mul_GF4_N and Mul_GF4
182 my ($x0,$x1,$y0,$y1,$t0,
183 $x2,$x3,$y2,$y3,$t1)=@_;
209 &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2]);
211 veor @t[0], @t[0], @x[2]
212 veor @t[1], @t[1], @x[3]
213 veor @y[0], @y[0], @y[2]
214 veor @y[1], @y[1], @y[3]
216 Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
217 @x[2], @x[3], @y[2], @y[3], @t[2]);
219 veor @x[0], @x[0], @t[0]
220 veor @x[2], @x[2], @t[0]
221 veor @x[1], @x[1], @t[1]
222 veor @x[3], @x[3], @t[1]
224 veor @t[0], @x[4], @x[6]
225 veor @t[1], @x[5], @x[7]
227 &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
228 @x[6], @x[7], @y[2], @y[3], @t[2]);
230 veor @y[0], @y[0], @y[2]
231 veor @y[1], @y[1], @y[3]
233 &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[3]);
235 veor @x[4], @x[4], @t[0]
236 veor @x[6], @x[6], @t[0]
237 veor @x[5], @x[5], @t[1]
238 veor @x[7], @x[7], @t[1]
242 #;********************************************************************
243 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
244 #;********************************************************************
248 # direct optimizations from hardware
250 veor @t[3], @x[4], @x[6]
251 veor @t[2], @x[5], @x[7]
252 veor @t[1], @x[1], @x[3]
253 veor @s[1], @x[7], @x[6]
255 veor @s[0], @x[0], @x[2]
257 vorr @t[2], @t[2], @t[1]
258 veor @s[3], @t[3], @t[0]
259 vand @s[2], @t[3], @s[0]
260 vorr @t[3], @t[3], @s[0]
261 veor @s[0], @s[0], @t[1]
262 vand @t[0], @t[0], @t[1]
263 vand @s[3], @s[3], @s[0]
264 veor @s[0], @x[3], @x[2]
265 vand @s[1], @s[1], @s[0]
266 veor @t[3], @t[3], @s[1]
267 veor @t[2], @t[2], @s[1]
268 veor @s[1], @x[4], @x[5]
269 veor @s[0], @x[1], @x[0]
270 vorr @t[1], @s[1], @s[0]
271 vand @s[1], @s[1], @s[0]
272 veor @t[0], @t[0], @s[1]
273 veor @t[3], @t[3], @s[3]
274 veor @t[2], @t[2], @s[2]
275 veor @t[1], @t[1], @s[3]
276 veor @t[0], @t[0], @s[2]
277 veor @t[1], @t[1], @s[2]
278 vand @s[0], @x[7], @x[3]
279 vand @s[1], @x[6], @x[2]
280 vand @s[2], @x[5], @x[1]
281 vorr @s[3], @x[4], @x[0]
282 veor @t[3], @t[3], @s[0]
283 veor @t[2], @t[2], @s[1]
284 veor @t[1], @t[1], @s[2]
285 veor @t[0], @t[0], @s[3]
287 @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
289 @ new smaller inversion
291 veor @s[0], @t[3], @t[2]
292 vand @t[3], @t[3], @t[1]
294 veor @s[2], @t[0], @t[3]
295 vand @s[3], @s[0], @s[2]
297 veor @s[3], @s[3], @t[2]
298 veor @s[1], @t[1], @t[0]
300 veor @t[3], @t[3], @t[2]
302 vand @s[1], @s[1], @t[3]
304 veor @s[1], @s[1], @t[0]
306 veor @t[2], @s[2], @s[1]
307 veor @t[1], @t[1], @s[1]
309 vand @t[2], @t[2], @t[0]
311 veor @s[2], @s[2], @t[2]
312 veor @t[1], @t[1], @t[2]
314 vand @s[2], @s[2], @s[3]
316 veor @s[2], @s[2], @s[0]
318 # output in s3, s2, s1, t1
320 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
322 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
323 &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
325 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
328 # AES linear components
335 vldmia $key!, {@t[0]-@t[3]}
336 veor @t[0], @t[0], @x[0]
337 veor @t[1], @t[1], @x[1]
338 vtbl.8 `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
339 vtbl.8 `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
340 vldmia $key!, {@t[0]}
341 veor @t[2], @t[2], @x[2]
342 vtbl.8 `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
343 vtbl.8 `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
344 vldmia $key!, {@t[1]}
345 veor @t[3], @t[3], @x[3]
346 vtbl.8 `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
347 vtbl.8 `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
348 vldmia $key!, {@t[2]}
349 vtbl.8 `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
350 vtbl.8 `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
351 vldmia $key!, {@t[3]}
352 veor @t[0], @t[0], @x[4]
353 veor @t[1], @t[1], @x[5]
354 vtbl.8 `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
355 vtbl.8 `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
356 veor @t[2], @t[2], @x[6]
357 vtbl.8 `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
358 vtbl.8 `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
359 veor @t[3], @t[3], @x[7]
360 vtbl.8 `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
361 vtbl.8 `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
362 vtbl.8 `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
363 vtbl.8 `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
368 # modified to emit output in order suitable for feeding back to aesenc[last]
372 vext.8 @t[0], @x[0], @x[0], #12 @ x0 <<< 32
373 vext.8 @t[1], @x[1], @x[1], #12
374 veor @x[0], @x[0], @t[0] @ x0 ^ (x0 <<< 32)
375 vext.8 @t[2], @x[2], @x[2], #12
376 veor @x[1], @x[1], @t[1]
377 vext.8 @t[3], @x[3], @x[3], #12
378 veor @x[2], @x[2], @t[2]
379 vext.8 @t[4], @x[4], @x[4], #12
380 veor @x[3], @x[3], @t[3]
381 vext.8 @t[5], @x[5], @x[5], #12
382 veor @x[4], @x[4], @t[4]
383 vext.8 @t[6], @x[6], @x[6], #12
384 veor @x[5], @x[5], @t[5]
385 vext.8 @t[7], @x[7], @x[7], #12
386 veor @x[6], @x[6], @t[6]
387 veor @x[7], @x[7], @t[7]
389 veor @t[1], @t[1], @x[0]
390 vext.8 @x[0], @x[0], @x[0], #8 @ (x0 ^ (x0 <<< 32)) <<< 64)
391 veor @t[0], @t[0], @x[7]
392 veor @t[1], @t[1], @x[7]
393 veor @t[2], @t[2], @x[1]
394 vext.8 @x[1], @x[1], @x[1], #8
395 veor @t[5], @t[5], @x[4]
396 veor @x[0], @x[0], @t[0]
397 veor @t[6], @t[6], @x[5]
398 veor @x[1], @x[1], @t[1]
399 vext.8 @t[0], @x[4], @x[4], #8
400 veor @t[4], @t[4], @x[3]
401 vext.8 @t[1], @x[5], @x[5], #8
402 veor @t[7], @t[7], @x[6]
403 vext.8 @x[4], @x[3], @x[3], #8
404 veor @t[3], @t[3], @x[2]
405 vext.8 @x[5], @x[7], @x[7], #8
406 veor @t[3], @t[3], @x[7]
407 vext.8 @x[3], @x[6], @x[6], #8
408 veor @t[4], @t[4], @x[7]
409 vext.8 @x[6], @x[2], @x[2], #8
410 veor @x[2], @t[0], @t[4]
411 veor @x[7], @t[1], @t[5]
413 veor @x[4], @x[4], @t[3]
414 veor @x[5], @x[5], @t[7]
415 veor @x[3], @x[3], @t[6]
417 veor @x[6], @x[6], @t[2]
427 @ multiplication by 0x0e
428 vext.8 @t[7], @x[7], @x[7], #12
430 veor @x[7], @x[7], @x[5] @ 7 5
431 veor @x[2], @x[2], @x[5] @ 2 5
432 vext.8 @t[0], @x[0], @x[0], #12
434 veor @x[5], @x[5], @x[0] @ 5 0 [1]
435 veor @x[0], @x[0], @x[1] @ 0 1
436 vext.8 @t[1], @x[1], @x[1], #12
437 veor @x[1], @x[1], @x[2] @ 1 25
438 veor @x[0], @x[0], @x[6] @ 01 6 [2]
439 veor @x[1], @x[1], @x[3] @ 125 3 [4]
440 vext.8 @t[3], @x[3], @x[3], #12
441 veor @x[2], @x[2], @x[0] @ 25 016 [3]
442 veor @x[3], @x[3], @x[7] @ 3 75
443 veor @x[7], @x[7], @x[6] @ 75 6 [0]
444 vext.8 @t[6], @x[6], @x[6], #12
446 veor @x[6], @x[6], @x[4] @ 6 4
447 veor @x[4], @x[4], @x[3] @ 4 375 [6]
448 veor @x[3], @x[3], @x[7] @ 375 756=36
449 veor @x[6], @x[6], @t[5] @ 64 5 [7]
450 veor @x[3], @x[3], @t[2] @ 36 2
451 vext.8 @t[5], @t[5], @t[5], #12
452 veor @x[3], @x[3], @t[4] @ 362 4 [5]
454 my @y = @x[7,5,0,2,1,3,4,6];
456 @ multiplication by 0x0b
457 veor @y[1], @y[1], @y[0]
458 veor @y[0], @y[0], @t[0]
459 veor @y[1], @y[1], @t[1]
460 vext.8 @t[2], @t[2], @t[2], #12
461 veor @y[0], @y[0], @t[5]
462 veor @y[1], @y[1], @t[6]
463 veor @y[0], @y[0], @t[7]
464 vext.8 @t[4], @t[4], @t[4], #12
465 veor @t[7], @t[7], @t[6] @ clobber t[7]
466 veor @y[1], @y[1], @y[0]
468 veor @y[3], @y[3], @t[0]
469 vext.8 @t[0], @t[0], @t[0], #12
470 veor @y[2], @y[2], @t[1]
471 veor @y[4], @y[4], @t[1]
472 veor @y[2], @y[2], @t[2]
473 vext.8 @t[1], @t[1], @t[1], #12
474 veor @y[3], @y[3], @t[2]
475 veor @y[5], @y[5], @t[2]
476 veor @y[2], @y[2], @t[7]
477 vext.8 @t[2], @t[2], @t[2], #12
478 veor @y[3], @y[3], @t[3]
479 veor @y[6], @y[6], @t[3]
480 veor @y[4], @y[4], @t[3]
481 vext.8 @t[3], @t[3], @t[3], #12
482 veor @y[7], @y[7], @t[4]
483 veor @y[5], @y[5], @t[4]
484 veor @y[7], @y[7], @t[7]
485 veor @y[3], @y[3], @t[5]
486 veor @y[4], @y[4], @t[4]
487 veor @t[7], @t[7], @t[5] @ clobber t[7] even more
489 veor @y[5], @y[5], @t[7]
490 vext.8 @t[4], @t[4], @t[4], #12
491 veor @y[6], @y[6], @t[7]
492 veor @y[4], @y[4], @t[7]
494 veor @t[7], @t[7], @t[5]
495 vext.8 @t[5], @t[5], @t[5], #12
496 veor @t[7], @t[7], @t[6] @ restore t[7]
498 @ multiplication by 0x0d
499 veor @y[4], @y[4], @y[7]
500 veor @y[7], @y[7], @t[4]
501 vext.8 @t[6], @t[6], @t[6], #12
502 veor @y[2], @y[2], @t[0]
503 veor @y[7], @y[7], @t[5]
504 veor @y[2], @y[2], @t[2]
505 vext.8 @t[7], @t[7], @t[7], #12
507 veor @y[3], @y[3], @y[1]
508 veor @y[1], @y[1], @t[1]
509 veor @y[0], @y[0], @t[0]
510 veor @y[3], @y[3], @t[0]
511 veor @y[1], @y[1], @t[5]
512 veor @y[0], @y[0], @t[5]
513 veor @y[1], @y[1], @t[7]
514 vext.8 @t[0], @t[0], @t[0], #12
515 veor @y[0], @y[0], @t[6]
516 veor @y[3], @y[3], @y[1]
517 veor @y[4], @y[4], @t[1]
518 vext.8 @t[1], @t[1], @t[1], #12
520 veor @y[7], @y[7], @t[7]
521 veor @y[4], @y[4], @t[2]
522 veor @y[5], @y[5], @t[2]
523 vext.8 @t[2], @t[2], @t[2], #12
524 veor @y[2], @y[2], @t[6]
525 veor @t[6], @t[6], @t[3] @ clobber t[6]
526 veor @y[4], @y[4], @y[7]
527 veor @y[3], @y[3], @t[6]
529 veor @y[6], @y[6], @t[6]
530 veor @y[5], @y[5], @t[5]
531 vext.8 @t[5], @t[5], @t[5], #12
532 veor @y[6], @y[6], @t[4]
533 vext.8 @t[4], @t[4], @t[4], #12
534 veor @y[5], @y[5], @t[6]
535 veor @y[6], @y[6], @t[7]
536 vext.8 @t[7], @t[7], @t[7], #12
537 veor @t[6], @t[6], @t[3] @ restore t[6]
538 vext.8 @t[3], @t[3], @t[3], #12
540 @ multiplication by 0x09
541 veor @y[4], @y[4], @y[1]
542 veor @t[1], @t[1], @y[1] @ t[1]=y[1]
543 veor @t[0], @t[0], @t[5] @ clobber t[0]
544 vext.8 @t[6], @t[6], @t[6], #12
545 veor @t[1], @t[1], @t[5]
546 veor @y[3], @y[3], @t[0]
547 veor @t[0], @t[0], @y[0] @ t[0]=y[0]
548 veor @t[1], @t[1], @t[6]
549 veor @t[6], @t[6], @t[7] @ clobber t[6]
550 veor @y[4], @y[4], @t[1]
551 veor @y[7], @y[7], @t[4]
552 veor @t[4], @t[4], @y[4] @ t[4]=y[4]
553 veor @y[6], @y[6], @t[3]
554 veor @t[3], @t[3], @y[3] @ t[3]=y[3]
555 veor @y[5], @y[5], @t[2]
556 veor @t[2], @t[2], @y[2] @ t[2]=y[2]
557 veor @t[3], @t[3], @t[7]
558 veor @t[5], @t[5], @y[5] @ t[5]=y[5]
559 veor @XMM[5], @t[5], @t[6]
560 veor @XMM[6], @t[6], @y[6] @ t[6]=y[6]
561 veor @XMM[2], @t[2], @t[6]
562 veor @XMM[7], @t[7], @y[7] @ t[7]=y[7]
566 @ vmov @XMM[2], @t[2]
569 @ vmov @XMM[5], @t[5]
570 @ vmov @XMM[6], @t[6]
571 @ vmov @XMM[7], @t[7]
576 my ($a,$b,$n,$mask,$t)=@_;
587 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
589 vshr.u64 $t0, $b0, #$n
590 vshr.u64 $t1, $b1, #$n
596 vshl.u64 $t0, $t0, #$n
598 vshl.u64 $t1, $t1, #$n
605 my @x=reverse(@_[0..7]);
606 my ($t0,$t1,$t2,$t3)=@_[8..11];
608 vmov.i8 $t0,#0x55 @ compose .LBS0
609 vmov.i8 $t1,#0x33 @ compose .LBS1
611 &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
612 &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
614 vmov.i8 $t0,#0x0f @ compose .LBS2
616 &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
617 &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
619 &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
620 &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
628 .type _bsaes_decrypt8,%function
631 sub $const,pc,#8 @ _bsaes_decrypt8
632 vldmia $key!, {@XMM[9]} @ round 0 key
633 add $const,$const,#.LM0ISR-_bsaes_decrypt8
635 vldmia $const!, {@XMM[8]} @ .LM0ISR
636 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
637 veor @XMM[11], @XMM[1], @XMM[9]
638 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
639 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
640 veor @XMM[12], @XMM[2], @XMM[9]
641 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
642 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
643 veor @XMM[13], @XMM[3], @XMM[9]
644 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
645 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
646 veor @XMM[14], @XMM[4], @XMM[9]
647 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
648 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
649 veor @XMM[15], @XMM[5], @XMM[9]
650 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
651 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
652 veor @XMM[10], @XMM[6], @XMM[9]
653 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
654 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
655 veor @XMM[11], @XMM[7], @XMM[9]
656 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
657 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
658 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
659 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
661 &bitslice (@XMM[0..7, 8..11]);
663 sub $rounds,$rounds,#1
668 &ShiftRows (@XMM[0..7, 8..12]);
669 $code.=".Ldec_sbox:\n";
670 &InvSbox (@XMM[0..7, 8..15]);
672 subs $rounds,$rounds,#1
675 &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
677 vldmia $const, {@XMM[12]} @ .LISR
678 addeq $const,$const,#0x10
680 vldmia $const, {@XMM[12]} @ .LISRM0
685 &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
687 vldmia $key, {@XMM[8]} @ last round key
688 veor @XMM[6], @XMM[6], @XMM[8]
689 veor @XMM[4], @XMM[4], @XMM[8]
690 veor @XMM[2], @XMM[2], @XMM[8]
691 veor @XMM[7], @XMM[7], @XMM[8]
692 veor @XMM[3], @XMM[3], @XMM[8]
693 veor @XMM[5], @XMM[5], @XMM[8]
694 veor @XMM[0], @XMM[0], @XMM[8]
695 veor @XMM[1], @XMM[1], @XMM[8]
697 .size _bsaes_decrypt8,.-_bsaes_decrypt8
699 .type _bsaes_const,%object
702 .LM0ISR: @ InvShiftRows constants
703 .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
705 .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
707 .quad 0x01040b0e0205080f, 0x0306090c00070a0d
708 .LM0SR: @ ShiftRows constants
709 .quad 0x0a0e02060f03070b, 0x0004080c05090d01
711 .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
713 .quad 0x0304090e00050a0f, 0x01060b0c0207080d
715 .quad 0x02060a0e03070b0f, 0x0004080c0105090d
716 .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
718 .size _bsaes_const,.-_bsaes_const
720 .type _bsaes_encrypt8,%function
723 sub $const,pc,#8 @ _bsaes_encrypt8
724 vldmia $key!, {@XMM[9]} @ round 0 key
725 sub $const,$const,#_bsaes_encrypt8-.LM0SR
727 vldmia $const!, {@XMM[8]} @ .LM0SR
728 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
729 veor @XMM[11], @XMM[1], @XMM[9]
730 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
731 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
732 veor @XMM[12], @XMM[2], @XMM[9]
733 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
734 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
735 veor @XMM[13], @XMM[3], @XMM[9]
736 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
737 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
738 veor @XMM[14], @XMM[4], @XMM[9]
739 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
740 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
741 veor @XMM[15], @XMM[5], @XMM[9]
742 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
743 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
744 veor @XMM[10], @XMM[6], @XMM[9]
745 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
746 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
747 veor @XMM[11], @XMM[7], @XMM[9]
748 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
749 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
750 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
751 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
752 _bsaes_encrypt8_bitslice:
754 &bitslice (@XMM[0..7, 8..11]);
756 sub $rounds,$rounds,#1
761 &ShiftRows (@XMM[0..7, 8..12]);
762 $code.=".Lenc_sbox:\n";
763 &Sbox (@XMM[0..7, 8..15]);
765 subs $rounds,$rounds,#1
768 &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
770 vldmia $const, {@XMM[12]} @ .LSR
771 addeq $const,$const,#0x10
773 vldmia $const, {@XMM[12]} @ .LSRM0
778 # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
779 &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
781 vldmia $key, {@XMM[8]} @ last round key
782 veor @XMM[4], @XMM[4], @XMM[8]
783 veor @XMM[6], @XMM[6], @XMM[8]
784 veor @XMM[3], @XMM[3], @XMM[8]
785 veor @XMM[7], @XMM[7], @XMM[8]
786 veor @XMM[2], @XMM[2], @XMM[8]
787 veor @XMM[5], @XMM[5], @XMM[8]
788 veor @XMM[0], @XMM[0], @XMM[8]
789 veor @XMM[1], @XMM[1], @XMM[8]
791 .size _bsaes_encrypt8,.-_bsaes_encrypt8
795 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
798 my @x=reverse(@_[0..7]);
799 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
801 &swapmove (@x[0,1],1,$bs0,$t2,$t3);
803 @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
807 #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
809 &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
811 @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
817 &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
818 &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
822 .type _bsaes_key_convert,%function
825 sub $const,pc,#8 @ _bsaes_key_convert
826 vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
827 sub $const,$const,#_bsaes_key_convert-.LM0
828 vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
830 vmov.i8 @XMM[8], #0x55 @ compose .LBS0
831 vmov.i8 @XMM[9], #0x33 @ compose .LBS1
832 vmov.i8 @XMM[10],#0x0f @ compose .LBS2
833 vldmia $const, {@XMM[13]} @ .LM0
836 vrev32.8 @XMM[7], @XMM[7]
837 vrev32.8 @XMM[15], @XMM[15]
839 sub $rounds,$rounds,#1
840 vstmia $out!, {@XMM[7]} @ save round 0 key
845 vtbl.8 `&Dlo(@XMM[6])`,{@XMM[15]},`&Dlo(@XMM[13])`
846 vtbl.8 `&Dhi(@XMM[6])`,{@XMM[15]},`&Dhi(@XMM[13])`
847 vmov @XMM[7], @XMM[6]
849 &bitslice_key (@XMM[0..7, 8..12]);
851 vld1.8 {@XMM[15]}, [$inp]! @ load next round key
852 vmvn @XMM[5], @XMM[5] @ "pnot"
853 vmvn @XMM[6], @XMM[6]
854 vmvn @XMM[0], @XMM[0]
855 vmvn @XMM[1], @XMM[1]
857 vrev32.8 @XMM[15], @XMM[15]
859 subs $rounds,$rounds,#1
860 vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
863 vmov.i8 @XMM[7],#0x63 @ compose .L63
864 @ don't save last round key
866 .size _bsaes_key_convert,.-_bsaes_key_convert
870 if (1) { # following four functions are unsupported interface
871 # used for benchmarking...
873 .globl bsaes_enc_key_convert
874 .type bsaes_enc_key_convert,%function
876 bsaes_enc_key_convert:
878 vstmdb sp!,{d8-d15} @ ABI specification says so
880 ldr r5,[$inp,#240] @ pass rounds
881 mov r4,$inp @ pass key
882 mov r12,$out @ pass key schedule
883 bl _bsaes_key_convert
884 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
885 vstmia r12, {@XMM[7]} @ save last round key
889 .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
891 .globl bsaes_encrypt_128
892 .type bsaes_encrypt_128,%function
896 vstmdb sp!,{d8-d15} @ ABI specification says so
898 vld1.8 {@XMM[0]}, [$inp]! @ load input
899 vld1.8 {@XMM[1]}, [$inp]!
900 vld1.8 {@XMM[2]}, [$inp]!
901 vld1.8 {@XMM[3]}, [$inp]!
902 vld1.8 {@XMM[4]}, [$inp]!
903 vld1.8 {@XMM[5]}, [$inp]!
904 mov r4,$key @ pass the key
905 vld1.8 {@XMM[6]}, [$inp]!
906 mov r5,#10 @ pass rounds
907 vld1.8 {@XMM[7]}, [$inp]!
911 vst1.8 {@XMM[0]}, [$out]! @ write output
912 vst1.8 {@XMM[1]}, [$out]!
913 vst1.8 {@XMM[4]}, [$out]!
914 vst1.8 {@XMM[6]}, [$out]!
915 vst1.8 {@XMM[3]}, [$out]!
916 vst1.8 {@XMM[7]}, [$out]!
917 vst1.8 {@XMM[2]}, [$out]!
919 vst1.8 {@XMM[5]}, [$out]!
924 .size bsaes_encrypt_128,.-bsaes_encrypt_128
926 .globl bsaes_dec_key_convert
927 .type bsaes_dec_key_convert,%function
929 bsaes_dec_key_convert:
931 vstmdb sp!,{d8-d15} @ ABI specification says so
933 ldr r5,[$inp,#240] @ pass rounds
934 mov r4,$inp @ pass key
935 mov r12,$out @ pass key schedule
936 bl _bsaes_key_convert
937 vldmia $out, {@XMM[6]}
938 vstmia r12, {@XMM[15]} @ save last round key
939 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
940 vstmia $out, {@XMM[7]}
944 .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
946 .globl bsaes_decrypt_128
947 .type bsaes_decrypt_128,%function
951 vstmdb sp!,{d8-d15} @ ABI specification says so
953 vld1.8 {@XMM[0]}, [$inp]! @ load input
954 vld1.8 {@XMM[1]}, [$inp]!
955 vld1.8 {@XMM[2]}, [$inp]!
956 vld1.8 {@XMM[3]}, [$inp]!
957 vld1.8 {@XMM[4]}, [$inp]!
958 vld1.8 {@XMM[5]}, [$inp]!
959 mov r4,$key @ pass the key
960 vld1.8 {@XMM[6]}, [$inp]!
961 mov r5,#10 @ pass rounds
962 vld1.8 {@XMM[7]}, [$inp]!
966 vst1.8 {@XMM[0]}, [$out]! @ write output
967 vst1.8 {@XMM[1]}, [$out]!
968 vst1.8 {@XMM[6]}, [$out]!
969 vst1.8 {@XMM[4]}, [$out]!
970 vst1.8 {@XMM[2]}, [$out]!
971 vst1.8 {@XMM[7]}, [$out]!
972 vst1.8 {@XMM[3]}, [$out]!
974 vst1.8 {@XMM[5]}, [$out]!
979 .size bsaes_decrypt_128,.-bsaes_decrypt_128
983 $code =~ s/\`([^\`]*)\`/eval($1)/gem;