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 19.5 cycles per byte processed with 128-bit key
22 # decrypt 24.0 cycles per byte processed with 128-bit key
23 # key conv. 440 cycles per 128-bit key/0.18 of 8x block
25 # Snapdragon S4 encrypts byte in 17.6 cycles and decrypts in 22.6,
26 # which is [much] worse than anticipated (for further details see
27 # http://www.openssl.org/~appro/Snapdragon-S4.html).
29 # When comparing to x86_64 results keep in mind that NEON unit is
30 # [mostly] single-issue and thus can't [fully] benefit from
31 # instruction-level parallelism. And when comparing to aes-armv4
32 # results keep in mind key schedule conversion overhead (see
33 # bsaes-x86_64.pl for further details)...
37 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
38 open STDOUT,">$output";
40 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
41 my @XMM=map("q$_",(0..15));
44 my ($key,$rounds,$const)=("r4","r5","r6");
46 sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
47 sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
50 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
51 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
56 &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
57 &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
61 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
62 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
65 veor @b[2], @b[2], @b[1]
66 veor @b[5], @b[5], @b[6]
67 veor @b[3], @b[3], @b[0]
68 veor @b[6], @b[6], @b[2]
69 veor @b[5], @b[5], @b[0]
71 veor @b[6], @b[6], @b[3]
72 veor @b[3], @b[3], @b[7]
73 veor @b[7], @b[7], @b[5]
74 veor @b[3], @b[3], @b[4]
75 veor @b[4], @b[4], @b[5]
77 veor @b[2], @b[2], @b[7]
78 veor @b[3], @b[3], @b[1]
79 veor @b[1], @b[1], @b[5]
84 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
85 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
88 veor @b[0], @b[0], @b[6]
89 veor @b[1], @b[1], @b[4]
90 veor @b[4], @b[4], @b[6]
91 veor @b[2], @b[2], @b[0]
92 veor @b[6], @b[6], @b[1]
94 veor @b[1], @b[1], @b[5]
95 veor @b[5], @b[5], @b[3]
96 veor @b[3], @b[3], @b[7]
97 veor @b[7], @b[7], @b[5]
98 veor @b[2], @b[2], @b[5]
100 veor @b[4], @b[4], @b[7]
105 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
106 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
110 &InvInBasisChange (@b);
111 &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
112 &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
115 sub InvInBasisChange { # OutBasisChange in reverse (with twist)
116 my @b=@_[5,1,2,6,3,7,0,4];
118 veor @b[1], @b[1], @b[7]
119 veor @b[4], @b[4], @b[7]
121 veor @b[7], @b[7], @b[5]
122 veor @b[1], @b[1], @b[3]
123 veor @b[2], @b[2], @b[5]
124 veor @b[3], @b[3], @b[7]
126 veor @b[6], @b[6], @b[1]
127 veor @b[2], @b[2], @b[0]
128 veor @b[5], @b[5], @b[3]
129 veor @b[4], @b[4], @b[6]
130 veor @b[0], @b[0], @b[6]
131 veor @b[1], @b[1], @b[4]
135 sub InvOutBasisChange { # InBasisChange in reverse
136 my @b=@_[2,5,7,3,6,1,0,4];
138 veor @b[1], @b[1], @b[5]
139 veor @b[2], @b[2], @b[7]
141 veor @b[3], @b[3], @b[1]
142 veor @b[4], @b[4], @b[5]
143 veor @b[7], @b[7], @b[5]
144 veor @b[3], @b[3], @b[4]
145 veor @b[5], @b[5], @b[0]
146 veor @b[3], @b[3], @b[7]
147 veor @b[6], @b[6], @b[2]
148 veor @b[2], @b[2], @b[1]
149 veor @b[6], @b[6], @b[3]
151 veor @b[3], @b[3], @b[0]
152 veor @b[5], @b[5], @b[6]
157 #;*************************************************************
158 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
159 #;*************************************************************
160 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
172 sub Mul_GF4_N { # not used, see next subroutine
173 # multiply and scale by N
174 my ($x0,$x1,$y0,$y1,$t0)=@_;
187 # interleaved Mul_GF4_N and Mul_GF4
188 my ($x0,$x1,$y0,$y1,$t0,
189 $x2,$x3,$y2,$y3,$t1)=@_;
212 veor @t[0], @x[0], @x[2]
213 veor @t[1], @x[1], @x[3]
215 &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
217 veor @y[0], @y[0], @y[2]
218 veor @y[1], @y[1], @y[3]
220 Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
221 @x[2], @x[3], @y[2], @y[3], @t[2]);
223 veor @x[0], @x[0], @t[0]
224 veor @x[2], @x[2], @t[0]
225 veor @x[1], @x[1], @t[1]
226 veor @x[3], @x[3], @t[1]
228 veor @t[0], @x[4], @x[6]
229 veor @t[1], @x[5], @x[7]
231 &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
232 @x[6], @x[7], @y[2], @y[3], @t[2]);
234 veor @y[0], @y[0], @y[2]
235 veor @y[1], @y[1], @y[3]
237 &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
239 veor @x[4], @x[4], @t[0]
240 veor @x[6], @x[6], @t[0]
241 veor @x[5], @x[5], @t[1]
242 veor @x[7], @x[7], @t[1]
246 #;********************************************************************
247 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
248 #;********************************************************************
252 # direct optimizations from hardware
254 veor @t[3], @x[4], @x[6]
255 veor @t[2], @x[5], @x[7]
256 veor @t[1], @x[1], @x[3]
257 veor @s[1], @x[7], @x[6]
259 veor @s[0], @x[0], @x[2]
261 vorr @t[2], @t[2], @t[1]
262 veor @s[3], @t[3], @t[0]
263 vand @s[2], @t[3], @s[0]
264 vorr @t[3], @t[3], @s[0]
265 veor @s[0], @s[0], @t[1]
266 vand @t[0], @t[0], @t[1]
267 veor @t[1], @x[3], @x[2]
268 vand @s[3], @s[3], @s[0]
269 vand @s[1], @s[1], @t[1]
270 veor @t[1], @x[4], @x[5]
271 veor @s[0], @x[1], @x[0]
272 veor @t[3], @t[3], @s[1]
273 veor @t[2], @t[2], @s[1]
274 vand @s[1], @t[1], @s[0]
275 vorr @t[1], @t[1], @s[0]
276 veor @t[3], @t[3], @s[3]
277 veor @t[0], @t[0], @s[1]
278 veor @t[2], @t[2], @s[2]
279 veor @t[1], @t[1], @s[3]
280 veor @t[0], @t[0], @s[2]
281 vand @s[0], @x[7], @x[3]
282 veor @t[1], @t[1], @s[2]
283 vand @s[1], @x[6], @x[2]
284 vand @s[2], @x[5], @x[1]
285 vorr @s[3], @x[4], @x[0]
286 veor @t[3], @t[3], @s[0]
287 veor @t[1], @t[1], @s[2]
288 veor @t[0], @t[0], @s[3]
289 veor @t[2], @t[2], @s[1]
291 @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
293 @ new smaller inversion
295 vand @s[2], @t[3], @t[1]
298 veor @s[1], @t[2], @s[2]
299 veor @s[3], @t[0], @s[2]
300 veor @s[2], @t[0], @s[2] @ @s[2]=@s[3]
302 vbsl @s[1], @t[1], @t[0]
303 vbsl @s[3], @t[3], @t[2]
304 veor @t[3], @t[3], @t[2]
306 vbsl @s[0], @s[1], @s[2]
307 vbsl @t[0], @s[2], @s[1]
309 vand @s[2], @s[0], @s[3]
310 veor @t[1], @t[1], @t[0]
312 veor @s[2], @s[2], @t[3]
314 # output in s3, s2, s1, t1
316 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
318 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
319 &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
321 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
324 # AES linear components
331 vldmia $key!, {@t[0]-@t[3]}
332 veor @t[0], @t[0], @x[0]
333 veor @t[1], @t[1], @x[1]
334 vtbl.8 `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
335 vtbl.8 `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
336 vldmia $key!, {@t[0]}
337 veor @t[2], @t[2], @x[2]
338 vtbl.8 `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
339 vtbl.8 `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
340 vldmia $key!, {@t[1]}
341 veor @t[3], @t[3], @x[3]
342 vtbl.8 `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
343 vtbl.8 `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
344 vldmia $key!, {@t[2]}
345 vtbl.8 `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
346 vtbl.8 `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
347 vldmia $key!, {@t[3]}
348 veor @t[0], @t[0], @x[4]
349 veor @t[1], @t[1], @x[5]
350 vtbl.8 `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
351 vtbl.8 `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
352 veor @t[2], @t[2], @x[6]
353 vtbl.8 `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
354 vtbl.8 `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
355 veor @t[3], @t[3], @x[7]
356 vtbl.8 `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
357 vtbl.8 `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
358 vtbl.8 `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
359 vtbl.8 `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
364 # modified to emit output in order suitable for feeding back to aesenc[last]
368 vext.8 @t[0], @x[0], @x[0], #12 @ x0 <<< 32
369 vext.8 @t[1], @x[1], @x[1], #12
370 veor @x[0], @x[0], @t[0] @ x0 ^ (x0 <<< 32)
371 vext.8 @t[2], @x[2], @x[2], #12
372 veor @x[1], @x[1], @t[1]
373 vext.8 @t[3], @x[3], @x[3], #12
374 veor @x[2], @x[2], @t[2]
375 vext.8 @t[4], @x[4], @x[4], #12
376 veor @x[3], @x[3], @t[3]
377 vext.8 @t[5], @x[5], @x[5], #12
378 veor @x[4], @x[4], @t[4]
379 vext.8 @t[6], @x[6], @x[6], #12
380 veor @x[5], @x[5], @t[5]
381 vext.8 @t[7], @x[7], @x[7], #12
382 veor @x[6], @x[6], @t[6]
384 veor @t[1], @t[1], @x[0]
385 veor @x[7], @x[7], @t[7]
386 vext.8 @x[0], @x[0], @x[0], #8 @ (x0 ^ (x0 <<< 32)) <<< 64)
387 veor @t[2], @t[2], @x[1]
388 veor @t[0], @t[0], @x[7]
389 veor @t[1], @t[1], @x[7]
390 vext.8 @x[1], @x[1], @x[1], #8
391 veor @t[5], @t[5], @x[4]
392 veor @x[0], @x[0], @t[0]
393 veor @t[6], @t[6], @x[5]
394 veor @x[1], @x[1], @t[1]
395 vext.8 @t[0], @x[4], @x[4], #8
396 veor @t[4], @t[4], @x[3]
397 vext.8 @t[1], @x[5], @x[5], #8
398 veor @t[7], @t[7], @x[6]
399 vext.8 @x[4], @x[3], @x[3], #8
400 veor @t[3], @t[3], @x[2]
401 vext.8 @x[5], @x[7], @x[7], #8
402 veor @t[4], @t[4], @x[7]
403 vext.8 @x[3], @x[6], @x[6], #8
404 veor @t[3], @t[3], @x[7]
405 vext.8 @x[6], @x[2], @x[2], #8
406 veor @x[7], @t[1], @t[5]
407 veor @x[2], @t[0], @t[4]
409 veor @x[4], @x[4], @t[3]
410 veor @x[5], @x[5], @t[7]
411 veor @x[3], @x[3], @t[6]
413 veor @x[6], @x[6], @t[2]
423 @ multiplication by 0x0e
424 vext.8 @t[7], @x[7], @x[7], #12
426 veor @x[2], @x[2], @x[5] @ 2 5
427 veor @x[7], @x[7], @x[5] @ 7 5
428 vext.8 @t[0], @x[0], @x[0], #12
430 veor @x[5], @x[5], @x[0] @ 5 0 [1]
431 veor @x[0], @x[0], @x[1] @ 0 1
432 vext.8 @t[1], @x[1], @x[1], #12
433 veor @x[1], @x[1], @x[2] @ 1 25
434 veor @x[0], @x[0], @x[6] @ 01 6 [2]
435 vext.8 @t[3], @x[3], @x[3], #12
436 veor @x[1], @x[1], @x[3] @ 125 3 [4]
437 veor @x[2], @x[2], @x[0] @ 25 016 [3]
438 veor @x[3], @x[3], @x[7] @ 3 75
439 veor @x[7], @x[7], @x[6] @ 75 6 [0]
440 vext.8 @t[6], @x[6], @x[6], #12
442 veor @x[6], @x[6], @x[4] @ 6 4
443 veor @x[4], @x[4], @x[3] @ 4 375 [6]
444 veor @x[3], @x[3], @x[7] @ 375 756=36
445 veor @x[6], @x[6], @t[5] @ 64 5 [7]
446 veor @x[3], @x[3], @t[2] @ 36 2
447 vext.8 @t[5], @t[5], @t[5], #12
448 veor @x[3], @x[3], @t[4] @ 362 4 [5]
450 my @y = @x[7,5,0,2,1,3,4,6];
452 @ multiplication by 0x0b
453 veor @y[1], @y[1], @y[0]
454 veor @y[0], @y[0], @t[0]
455 vext.8 @t[2], @t[2], @t[2], #12
456 veor @y[1], @y[1], @t[1]
457 veor @y[0], @y[0], @t[5]
458 vext.8 @t[4], @t[4], @t[4], #12
459 veor @y[1], @y[1], @t[6]
460 veor @y[0], @y[0], @t[7]
461 veor @t[7], @t[7], @t[6] @ clobber t[7]
463 veor @y[3], @y[3], @t[0]
464 veor @y[1], @y[1], @y[0]
465 vext.8 @t[0], @t[0], @t[0], #12
466 veor @y[2], @y[2], @t[1]
467 veor @y[4], @y[4], @t[1]
468 vext.8 @t[1], @t[1], @t[1], #12
469 veor @y[2], @y[2], @t[2]
470 veor @y[3], @y[3], @t[2]
471 veor @y[5], @y[5], @t[2]
472 veor @y[2], @y[2], @t[7]
473 vext.8 @t[2], @t[2], @t[2], #12
474 veor @y[3], @y[3], @t[3]
475 veor @y[6], @y[6], @t[3]
476 veor @y[4], @y[4], @t[3]
477 veor @y[7], @y[7], @t[4]
478 vext.8 @t[3], @t[3], @t[3], #12
479 veor @y[5], @y[5], @t[4]
480 veor @y[7], @y[7], @t[7]
481 veor @t[7], @t[7], @t[5] @ clobber t[7] even more
482 veor @y[3], @y[3], @t[5]
483 veor @y[4], @y[4], @t[4]
485 veor @y[5], @y[5], @t[7]
486 vext.8 @t[4], @t[4], @t[4], #12
487 veor @y[6], @y[6], @t[7]
488 veor @y[4], @y[4], @t[7]
490 veor @t[7], @t[7], @t[5]
491 vext.8 @t[5], @t[5], @t[5], #12
493 @ multiplication by 0x0d
494 veor @y[4], @y[4], @y[7]
495 veor @t[7], @t[7], @t[6] @ restore t[7]
496 veor @y[7], @y[7], @t[4]
497 vext.8 @t[6], @t[6], @t[6], #12
498 veor @y[2], @y[2], @t[0]
499 veor @y[7], @y[7], @t[5]
500 vext.8 @t[7], @t[7], @t[7], #12
501 veor @y[2], @y[2], @t[2]
503 veor @y[3], @y[3], @y[1]
504 veor @y[1], @y[1], @t[1]
505 veor @y[0], @y[0], @t[0]
506 veor @y[3], @y[3], @t[0]
507 veor @y[1], @y[1], @t[5]
508 veor @y[0], @y[0], @t[5]
509 vext.8 @t[0], @t[0], @t[0], #12
510 veor @y[1], @y[1], @t[7]
511 veor @y[0], @y[0], @t[6]
512 veor @y[3], @y[3], @y[1]
513 veor @y[4], @y[4], @t[1]
514 vext.8 @t[1], @t[1], @t[1], #12
516 veor @y[7], @y[7], @t[7]
517 veor @y[4], @y[4], @t[2]
518 veor @y[5], @y[5], @t[2]
519 veor @y[2], @y[2], @t[6]
520 veor @t[6], @t[6], @t[3] @ clobber t[6]
521 vext.8 @t[2], @t[2], @t[2], #12
522 veor @y[4], @y[4], @y[7]
523 veor @y[3], @y[3], @t[6]
525 veor @y[6], @y[6], @t[6]
526 veor @y[5], @y[5], @t[5]
527 vext.8 @t[5], @t[5], @t[5], #12
528 veor @y[6], @y[6], @t[4]
529 vext.8 @t[4], @t[4], @t[4], #12
530 veor @y[5], @y[5], @t[6]
531 veor @y[6], @y[6], @t[7]
532 vext.8 @t[7], @t[7], @t[7], #12
533 veor @t[6], @t[6], @t[3] @ restore t[6]
534 vext.8 @t[3], @t[3], @t[3], #12
536 @ multiplication by 0x09
537 veor @y[4], @y[4], @y[1]
538 veor @t[1], @t[1], @y[1] @ t[1]=y[1]
539 veor @t[0], @t[0], @t[5] @ clobber t[0]
540 vext.8 @t[6], @t[6], @t[6], #12
541 veor @t[1], @t[1], @t[5]
542 veor @y[3], @y[3], @t[0]
543 veor @t[0], @t[0], @y[0] @ t[0]=y[0]
544 veor @t[1], @t[1], @t[6]
545 veor @t[6], @t[6], @t[7] @ clobber t[6]
546 veor @y[4], @y[4], @t[1]
547 veor @y[7], @y[7], @t[4]
548 veor @y[6], @y[6], @t[3]
549 veor @y[5], @y[5], @t[2]
550 veor @t[4], @t[4], @y[4] @ t[4]=y[4]
551 veor @t[3], @t[3], @y[3] @ t[3]=y[3]
552 veor @t[5], @t[5], @y[5] @ t[5]=y[5]
553 veor @t[2], @t[2], @y[2] @ t[2]=y[2]
554 veor @t[3], @t[3], @t[7]
555 veor @XMM[5], @t[5], @t[6]
556 veor @XMM[6], @t[6], @y[6] @ t[6]=y[6]
557 veor @XMM[2], @t[2], @t[6]
558 veor @XMM[7], @t[7], @y[7] @ t[7]=y[7]
562 @ vmov @XMM[2], @t[2]
565 @ vmov @XMM[5], @t[5]
566 @ vmov @XMM[6], @t[6]
567 @ vmov @XMM[7], @t[7]
572 my ($a,$b,$n,$mask,$t)=@_;
583 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
585 vshr.u64 $t0, $b0, #$n
586 vshr.u64 $t1, $b1, #$n
592 vshl.u64 $t0, $t0, #$n
594 vshl.u64 $t1, $t1, #$n
601 my @x=reverse(@_[0..7]);
602 my ($t0,$t1,$t2,$t3)=@_[8..11];
604 vmov.i8 $t0,#0x55 @ compose .LBS0
605 vmov.i8 $t1,#0x33 @ compose .LBS1
607 &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
608 &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
610 vmov.i8 $t0,#0x0f @ compose .LBS2
612 &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
613 &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
615 &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
616 &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
624 .type _bsaes_decrypt8,%function
627 sub $const,pc,#8 @ _bsaes_decrypt8
628 vldmia $key!, {@XMM[9]} @ round 0 key
629 add $const,$const,#.LM0ISR-_bsaes_decrypt8
631 vldmia $const!, {@XMM[8]} @ .LM0ISR
632 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
633 veor @XMM[11], @XMM[1], @XMM[9]
634 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
635 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
636 veor @XMM[12], @XMM[2], @XMM[9]
637 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
638 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
639 veor @XMM[13], @XMM[3], @XMM[9]
640 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
641 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
642 veor @XMM[14], @XMM[4], @XMM[9]
643 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
644 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
645 veor @XMM[15], @XMM[5], @XMM[9]
646 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
647 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
648 veor @XMM[10], @XMM[6], @XMM[9]
649 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
650 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
651 veor @XMM[11], @XMM[7], @XMM[9]
652 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
653 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
654 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
655 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
657 &bitslice (@XMM[0..7, 8..11]);
659 sub $rounds,$rounds,#1
664 &ShiftRows (@XMM[0..7, 8..12]);
665 $code.=".Ldec_sbox:\n";
666 &InvSbox (@XMM[0..7, 8..15]);
668 subs $rounds,$rounds,#1
671 &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
673 vldmia $const, {@XMM[12]} @ .LISR
674 addeq $const,$const,#0x10
676 vldmia $const, {@XMM[12]} @ .LISRM0
681 &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
683 vldmia $key, {@XMM[8]} @ last round key
684 veor @XMM[6], @XMM[6], @XMM[8]
685 veor @XMM[4], @XMM[4], @XMM[8]
686 veor @XMM[2], @XMM[2], @XMM[8]
687 veor @XMM[7], @XMM[7], @XMM[8]
688 veor @XMM[3], @XMM[3], @XMM[8]
689 veor @XMM[5], @XMM[5], @XMM[8]
690 veor @XMM[0], @XMM[0], @XMM[8]
691 veor @XMM[1], @XMM[1], @XMM[8]
693 .size _bsaes_decrypt8,.-_bsaes_decrypt8
695 .type _bsaes_const,%object
698 .LM0ISR: @ InvShiftRows constants
699 .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
701 .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
703 .quad 0x01040b0e0205080f, 0x0306090c00070a0d
704 .LM0SR: @ ShiftRows constants
705 .quad 0x0a0e02060f03070b, 0x0004080c05090d01
707 .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
709 .quad 0x0304090e00050a0f, 0x01060b0c0207080d
711 .quad 0x02060a0e03070b0f, 0x0004080c0105090d
712 .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
714 .size _bsaes_const,.-_bsaes_const
716 .type _bsaes_encrypt8,%function
719 sub $const,pc,#8 @ _bsaes_encrypt8
720 vldmia $key!, {@XMM[9]} @ round 0 key
721 sub $const,$const,#_bsaes_encrypt8-.LM0SR
723 vldmia $const!, {@XMM[8]} @ .LM0SR
724 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
725 veor @XMM[11], @XMM[1], @XMM[9]
726 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
727 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
728 veor @XMM[12], @XMM[2], @XMM[9]
729 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
730 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
731 veor @XMM[13], @XMM[3], @XMM[9]
732 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
733 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
734 veor @XMM[14], @XMM[4], @XMM[9]
735 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
736 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
737 veor @XMM[15], @XMM[5], @XMM[9]
738 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
739 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
740 veor @XMM[10], @XMM[6], @XMM[9]
741 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
742 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
743 veor @XMM[11], @XMM[7], @XMM[9]
744 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
745 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
746 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
747 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
748 _bsaes_encrypt8_bitslice:
750 &bitslice (@XMM[0..7, 8..11]);
752 sub $rounds,$rounds,#1
757 &ShiftRows (@XMM[0..7, 8..12]);
758 $code.=".Lenc_sbox:\n";
759 &Sbox (@XMM[0..7, 8..15]);
761 subs $rounds,$rounds,#1
764 &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
766 vldmia $const, {@XMM[12]} @ .LSR
767 addeq $const,$const,#0x10
769 vldmia $const, {@XMM[12]} @ .LSRM0
774 # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
775 &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
777 vldmia $key, {@XMM[8]} @ last round key
778 veor @XMM[4], @XMM[4], @XMM[8]
779 veor @XMM[6], @XMM[6], @XMM[8]
780 veor @XMM[3], @XMM[3], @XMM[8]
781 veor @XMM[7], @XMM[7], @XMM[8]
782 veor @XMM[2], @XMM[2], @XMM[8]
783 veor @XMM[5], @XMM[5], @XMM[8]
784 veor @XMM[0], @XMM[0], @XMM[8]
785 veor @XMM[1], @XMM[1], @XMM[8]
787 .size _bsaes_encrypt8,.-_bsaes_encrypt8
791 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
794 my @x=reverse(@_[0..7]);
795 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
797 &swapmove (@x[0,1],1,$bs0,$t2,$t3);
799 @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
803 #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
805 &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
807 @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
813 &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
814 &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
818 .type _bsaes_key_convert,%function
821 sub $const,pc,#8 @ _bsaes_key_convert
822 vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
823 sub $const,$const,#_bsaes_key_convert-.LM0
824 vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
826 vmov.i8 @XMM[8], #0x01 @ bit masks
827 vmov.i8 @XMM[9], #0x02
828 vmov.i8 @XMM[10], #0x04
829 vmov.i8 @XMM[11], #0x08
830 vmov.i8 @XMM[12], #0x10
831 vmov.i8 @XMM[13], #0x20
832 vldmia $const, {@XMM[14]} @ .LM0
835 vrev32.8 @XMM[7], @XMM[7]
836 vrev32.8 @XMM[15], @XMM[15]
838 sub $rounds,$rounds,#1
839 vstmia $out!, {@XMM[7]} @ save round 0 key
844 vtbl.8 `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
845 vtbl.8 `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
846 vmov.i8 @XMM[6], #0x40
847 vmov.i8 @XMM[15], #0x80
849 vtst.8 @XMM[0], @XMM[7], @XMM[8]
850 vtst.8 @XMM[1], @XMM[7], @XMM[9]
851 vtst.8 @XMM[2], @XMM[7], @XMM[10]
852 vtst.8 @XMM[3], @XMM[7], @XMM[11]
853 vtst.8 @XMM[4], @XMM[7], @XMM[12]
854 vtst.8 @XMM[5], @XMM[7], @XMM[13]
855 vtst.8 @XMM[6], @XMM[7], @XMM[6]
856 vtst.8 @XMM[7], @XMM[7], @XMM[15]
857 vld1.8 {@XMM[15]}, [$inp]! @ load next round key
858 vmvn @XMM[0], @XMM[0] @ "pnot"
859 vmvn @XMM[1], @XMM[1]
860 vmvn @XMM[5], @XMM[5]
861 vmvn @XMM[6], @XMM[6]
863 vrev32.8 @XMM[15], @XMM[15]
865 subs $rounds,$rounds,#1
866 vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
869 vmov.i8 @XMM[7],#0x63 @ compose .L63
870 @ don't save last round key
872 .size _bsaes_key_convert,.-_bsaes_key_convert
876 if (1) { # following four functions are unsupported interface
877 # used for benchmarking...
879 .globl bsaes_enc_key_convert
880 .type bsaes_enc_key_convert,%function
882 bsaes_enc_key_convert:
884 vstmdb sp!,{d8-d15} @ ABI specification says so
886 ldr r5,[$inp,#240] @ pass rounds
887 mov r4,$inp @ pass key
888 mov r12,$out @ pass key schedule
889 bl _bsaes_key_convert
890 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
891 vstmia r12, {@XMM[7]} @ save last round key
895 .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
897 .globl bsaes_encrypt_128
898 .type bsaes_encrypt_128,%function
902 vstmdb sp!,{d8-d15} @ ABI specification says so
904 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
905 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
906 mov r4,$key @ pass the key
907 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
908 mov r5,#10 @ pass rounds
909 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
913 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
914 vst1.8 {@XMM[4]}, [$out]!
915 vst1.8 {@XMM[6]}, [$out]!
916 vst1.8 {@XMM[3]}, [$out]!
917 vst1.8 {@XMM[7]}, [$out]!
918 vst1.8 {@XMM[2]}, [$out]!
920 vst1.8 {@XMM[5]}, [$out]!
925 .size bsaes_encrypt_128,.-bsaes_encrypt_128
927 .globl bsaes_dec_key_convert
928 .type bsaes_dec_key_convert,%function
930 bsaes_dec_key_convert:
932 vstmdb sp!,{d8-d15} @ ABI specification says so
934 ldr r5,[$inp,#240] @ pass rounds
935 mov r4,$inp @ pass key
936 mov r12,$out @ pass key schedule
937 bl _bsaes_key_convert
938 vldmia $out, {@XMM[6]}
939 vstmia r12, {@XMM[15]} @ save last round key
940 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
941 vstmia $out, {@XMM[7]}
945 .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
947 .globl bsaes_decrypt_128
948 .type bsaes_decrypt_128,%function
952 vstmdb sp!,{d8-d15} @ ABI specification says so
954 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
955 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
956 mov r4,$key @ pass the key
957 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
958 mov r5,#10 @ pass rounds
959 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
963 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
964 vst1.8 {@XMM[6]}, [$out]!
965 vst1.8 {@XMM[4]}, [$out]!
966 vst1.8 {@XMM[2]}, [$out]!
967 vst1.8 {@XMM[7]}, [$out]!
968 vst1.8 {@XMM[3]}, [$out]!
970 vst1.8 {@XMM[5]}, [$out]!
975 .size bsaes_decrypt_128,.-bsaes_decrypt_128
979 $code =~ s/\`([^\`]*)\`/eval($1)/gem;