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 # Cortex-A15 manages in 14.2/19.6 cycles [when integer-only code
30 # manages in 20.0 cycles].
32 # When comparing to x86_64 results keep in mind that NEON unit is
33 # [mostly] single-issue and thus can't [fully] benefit from
34 # instruction-level parallelism. And when comparing to aes-armv4
35 # results keep in mind key schedule conversion overhead (see
36 # bsaes-x86_64.pl for further details)...
40 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
41 open STDOUT,">$output";
43 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
44 my @XMM=map("q$_",(0..15));
47 my ($key,$rounds,$const)=("r4","r5","r6");
49 sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
50 sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
53 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
54 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
59 &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
60 &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
64 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
65 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
68 veor @b[2], @b[2], @b[1]
69 veor @b[5], @b[5], @b[6]
70 veor @b[3], @b[3], @b[0]
71 veor @b[6], @b[6], @b[2]
72 veor @b[5], @b[5], @b[0]
74 veor @b[6], @b[6], @b[3]
75 veor @b[3], @b[3], @b[7]
76 veor @b[7], @b[7], @b[5]
77 veor @b[3], @b[3], @b[4]
78 veor @b[4], @b[4], @b[5]
80 veor @b[2], @b[2], @b[7]
81 veor @b[3], @b[3], @b[1]
82 veor @b[1], @b[1], @b[5]
87 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
88 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
91 veor @b[0], @b[0], @b[6]
92 veor @b[1], @b[1], @b[4]
93 veor @b[4], @b[4], @b[6]
94 veor @b[2], @b[2], @b[0]
95 veor @b[6], @b[6], @b[1]
97 veor @b[1], @b[1], @b[5]
98 veor @b[5], @b[5], @b[3]
99 veor @b[3], @b[3], @b[7]
100 veor @b[7], @b[7], @b[5]
101 veor @b[2], @b[2], @b[5]
103 veor @b[4], @b[4], @b[7]
108 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
109 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
113 &InvInBasisChange (@b);
114 &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
115 &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
118 sub InvInBasisChange { # OutBasisChange in reverse (with twist)
119 my @b=@_[5,1,2,6,3,7,0,4];
121 veor @b[1], @b[1], @b[7]
122 veor @b[4], @b[4], @b[7]
124 veor @b[7], @b[7], @b[5]
125 veor @b[1], @b[1], @b[3]
126 veor @b[2], @b[2], @b[5]
127 veor @b[3], @b[3], @b[7]
129 veor @b[6], @b[6], @b[1]
130 veor @b[2], @b[2], @b[0]
131 veor @b[5], @b[5], @b[3]
132 veor @b[4], @b[4], @b[6]
133 veor @b[0], @b[0], @b[6]
134 veor @b[1], @b[1], @b[4]
138 sub InvOutBasisChange { # InBasisChange in reverse
139 my @b=@_[2,5,7,3,6,1,0,4];
141 veor @b[1], @b[1], @b[5]
142 veor @b[2], @b[2], @b[7]
144 veor @b[3], @b[3], @b[1]
145 veor @b[4], @b[4], @b[5]
146 veor @b[7], @b[7], @b[5]
147 veor @b[3], @b[3], @b[4]
148 veor @b[5], @b[5], @b[0]
149 veor @b[3], @b[3], @b[7]
150 veor @b[6], @b[6], @b[2]
151 veor @b[2], @b[2], @b[1]
152 veor @b[6], @b[6], @b[3]
154 veor @b[3], @b[3], @b[0]
155 veor @b[5], @b[5], @b[6]
160 #;*************************************************************
161 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
162 #;*************************************************************
163 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
175 sub Mul_GF4_N { # not used, see next subroutine
176 # multiply and scale by N
177 my ($x0,$x1,$y0,$y1,$t0)=@_;
190 # interleaved Mul_GF4_N and Mul_GF4
191 my ($x0,$x1,$y0,$y1,$t0,
192 $x2,$x3,$y2,$y3,$t1)=@_;
215 veor @t[0], @x[0], @x[2]
216 veor @t[1], @x[1], @x[3]
218 &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
220 veor @y[0], @y[0], @y[2]
221 veor @y[1], @y[1], @y[3]
223 Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
224 @x[2], @x[3], @y[2], @y[3], @t[2]);
226 veor @x[0], @x[0], @t[0]
227 veor @x[2], @x[2], @t[0]
228 veor @x[1], @x[1], @t[1]
229 veor @x[3], @x[3], @t[1]
231 veor @t[0], @x[4], @x[6]
232 veor @t[1], @x[5], @x[7]
234 &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
235 @x[6], @x[7], @y[2], @y[3], @t[2]);
237 veor @y[0], @y[0], @y[2]
238 veor @y[1], @y[1], @y[3]
240 &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
242 veor @x[4], @x[4], @t[0]
243 veor @x[6], @x[6], @t[0]
244 veor @x[5], @x[5], @t[1]
245 veor @x[7], @x[7], @t[1]
249 #;********************************************************************
250 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
251 #;********************************************************************
255 # direct optimizations from hardware
257 veor @t[3], @x[4], @x[6]
258 veor @t[2], @x[5], @x[7]
259 veor @t[1], @x[1], @x[3]
260 veor @s[1], @x[7], @x[6]
262 veor @s[0], @x[0], @x[2]
264 vorr @t[2], @t[2], @t[1]
265 veor @s[3], @t[3], @t[0]
266 vand @s[2], @t[3], @s[0]
267 vorr @t[3], @t[3], @s[0]
268 veor @s[0], @s[0], @t[1]
269 vand @t[0], @t[0], @t[1]
270 veor @t[1], @x[3], @x[2]
271 vand @s[3], @s[3], @s[0]
272 vand @s[1], @s[1], @t[1]
273 veor @t[1], @x[4], @x[5]
274 veor @s[0], @x[1], @x[0]
275 veor @t[3], @t[3], @s[1]
276 veor @t[2], @t[2], @s[1]
277 vand @s[1], @t[1], @s[0]
278 vorr @t[1], @t[1], @s[0]
279 veor @t[3], @t[3], @s[3]
280 veor @t[0], @t[0], @s[1]
281 veor @t[2], @t[2], @s[2]
282 veor @t[1], @t[1], @s[3]
283 veor @t[0], @t[0], @s[2]
284 vand @s[0], @x[7], @x[3]
285 veor @t[1], @t[1], @s[2]
286 vand @s[1], @x[6], @x[2]
287 vand @s[2], @x[5], @x[1]
288 vorr @s[3], @x[4], @x[0]
289 veor @t[3], @t[3], @s[0]
290 veor @t[1], @t[1], @s[2]
291 veor @t[0], @t[0], @s[3]
292 veor @t[2], @t[2], @s[1]
294 @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
296 @ new smaller inversion
298 vand @s[2], @t[3], @t[1]
301 veor @s[1], @t[2], @s[2]
302 veor @s[3], @t[0], @s[2]
303 veor @s[2], @t[0], @s[2] @ @s[2]=@s[3]
305 vbsl @s[1], @t[1], @t[0]
306 vbsl @s[3], @t[3], @t[2]
307 veor @t[3], @t[3], @t[2]
309 vbsl @s[0], @s[1], @s[2]
310 vbsl @t[0], @s[2], @s[1]
312 vand @s[2], @s[0], @s[3]
313 veor @t[1], @t[1], @t[0]
315 veor @s[2], @s[2], @t[3]
317 # output in s3, s2, s1, t1
319 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
321 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
322 &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
324 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
327 # AES linear components
334 vldmia $key!, {@t[0]-@t[3]}
335 veor @t[0], @t[0], @x[0]
336 veor @t[1], @t[1], @x[1]
337 vtbl.8 `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
338 vtbl.8 `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
339 vldmia $key!, {@t[0]}
340 veor @t[2], @t[2], @x[2]
341 vtbl.8 `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
342 vtbl.8 `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
343 vldmia $key!, {@t[1]}
344 veor @t[3], @t[3], @x[3]
345 vtbl.8 `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
346 vtbl.8 `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
347 vldmia $key!, {@t[2]}
348 vtbl.8 `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
349 vtbl.8 `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
350 vldmia $key!, {@t[3]}
351 veor @t[0], @t[0], @x[4]
352 veor @t[1], @t[1], @x[5]
353 vtbl.8 `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
354 vtbl.8 `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
355 veor @t[2], @t[2], @x[6]
356 vtbl.8 `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
357 vtbl.8 `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
358 veor @t[3], @t[3], @x[7]
359 vtbl.8 `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
360 vtbl.8 `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
361 vtbl.8 `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
362 vtbl.8 `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
367 # modified to emit output in order suitable for feeding back to aesenc[last]
371 vext.8 @t[0], @x[0], @x[0], #12 @ x0 <<< 32
372 vext.8 @t[1], @x[1], @x[1], #12
373 veor @x[0], @x[0], @t[0] @ x0 ^ (x0 <<< 32)
374 vext.8 @t[2], @x[2], @x[2], #12
375 veor @x[1], @x[1], @t[1]
376 vext.8 @t[3], @x[3], @x[3], #12
377 veor @x[2], @x[2], @t[2]
378 vext.8 @t[4], @x[4], @x[4], #12
379 veor @x[3], @x[3], @t[3]
380 vext.8 @t[5], @x[5], @x[5], #12
381 veor @x[4], @x[4], @t[4]
382 vext.8 @t[6], @x[6], @x[6], #12
383 veor @x[5], @x[5], @t[5]
384 vext.8 @t[7], @x[7], @x[7], #12
385 veor @x[6], @x[6], @t[6]
387 veor @t[1], @t[1], @x[0]
388 veor @x[7], @x[7], @t[7]
389 vext.8 @x[0], @x[0], @x[0], #8 @ (x0 ^ (x0 <<< 32)) <<< 64)
390 veor @t[2], @t[2], @x[1]
391 veor @t[0], @t[0], @x[7]
392 veor @t[1], @t[1], @x[7]
393 vext.8 @x[1], @x[1], @x[1], #8
394 veor @t[5], @t[5], @x[4]
395 veor @x[0], @x[0], @t[0]
396 veor @t[6], @t[6], @x[5]
397 veor @x[1], @x[1], @t[1]
398 vext.8 @t[0], @x[4], @x[4], #8
399 veor @t[4], @t[4], @x[3]
400 vext.8 @t[1], @x[5], @x[5], #8
401 veor @t[7], @t[7], @x[6]
402 vext.8 @x[4], @x[3], @x[3], #8
403 veor @t[3], @t[3], @x[2]
404 vext.8 @x[5], @x[7], @x[7], #8
405 veor @t[4], @t[4], @x[7]
406 vext.8 @x[3], @x[6], @x[6], #8
407 veor @t[3], @t[3], @x[7]
408 vext.8 @x[6], @x[2], @x[2], #8
409 veor @x[7], @t[1], @t[5]
410 veor @x[2], @t[0], @t[4]
412 veor @x[4], @x[4], @t[3]
413 veor @x[5], @x[5], @t[7]
414 veor @x[3], @x[3], @t[6]
416 veor @x[6], @x[6], @t[2]
426 @ multiplication by 0x0e
427 vext.8 @t[7], @x[7], @x[7], #12
429 veor @x[2], @x[2], @x[5] @ 2 5
430 veor @x[7], @x[7], @x[5] @ 7 5
431 vext.8 @t[0], @x[0], @x[0], #12
433 veor @x[5], @x[5], @x[0] @ 5 0 [1]
434 veor @x[0], @x[0], @x[1] @ 0 1
435 vext.8 @t[1], @x[1], @x[1], #12
436 veor @x[1], @x[1], @x[2] @ 1 25
437 veor @x[0], @x[0], @x[6] @ 01 6 [2]
438 vext.8 @t[3], @x[3], @x[3], #12
439 veor @x[1], @x[1], @x[3] @ 125 3 [4]
440 veor @x[2], @x[2], @x[0] @ 25 016 [3]
441 veor @x[3], @x[3], @x[7] @ 3 75
442 veor @x[7], @x[7], @x[6] @ 75 6 [0]
443 vext.8 @t[6], @x[6], @x[6], #12
445 veor @x[6], @x[6], @x[4] @ 6 4
446 veor @x[4], @x[4], @x[3] @ 4 375 [6]
447 veor @x[3], @x[3], @x[7] @ 375 756=36
448 veor @x[6], @x[6], @t[5] @ 64 5 [7]
449 veor @x[3], @x[3], @t[2] @ 36 2
450 vext.8 @t[5], @t[5], @t[5], #12
451 veor @x[3], @x[3], @t[4] @ 362 4 [5]
453 my @y = @x[7,5,0,2,1,3,4,6];
455 @ multiplication by 0x0b
456 veor @y[1], @y[1], @y[0]
457 veor @y[0], @y[0], @t[0]
458 vext.8 @t[2], @t[2], @t[2], #12
459 veor @y[1], @y[1], @t[1]
460 veor @y[0], @y[0], @t[5]
461 vext.8 @t[4], @t[4], @t[4], #12
462 veor @y[1], @y[1], @t[6]
463 veor @y[0], @y[0], @t[7]
464 veor @t[7], @t[7], @t[6] @ clobber t[7]
466 veor @y[3], @y[3], @t[0]
467 veor @y[1], @y[1], @y[0]
468 vext.8 @t[0], @t[0], @t[0], #12
469 veor @y[2], @y[2], @t[1]
470 veor @y[4], @y[4], @t[1]
471 vext.8 @t[1], @t[1], @t[1], #12
472 veor @y[2], @y[2], @t[2]
473 veor @y[3], @y[3], @t[2]
474 veor @y[5], @y[5], @t[2]
475 veor @y[2], @y[2], @t[7]
476 vext.8 @t[2], @t[2], @t[2], #12
477 veor @y[3], @y[3], @t[3]
478 veor @y[6], @y[6], @t[3]
479 veor @y[4], @y[4], @t[3]
480 veor @y[7], @y[7], @t[4]
481 vext.8 @t[3], @t[3], @t[3], #12
482 veor @y[5], @y[5], @t[4]
483 veor @y[7], @y[7], @t[7]
484 veor @t[7], @t[7], @t[5] @ clobber t[7] even more
485 veor @y[3], @y[3], @t[5]
486 veor @y[4], @y[4], @t[4]
488 veor @y[5], @y[5], @t[7]
489 vext.8 @t[4], @t[4], @t[4], #12
490 veor @y[6], @y[6], @t[7]
491 veor @y[4], @y[4], @t[7]
493 veor @t[7], @t[7], @t[5]
494 vext.8 @t[5], @t[5], @t[5], #12
496 @ multiplication by 0x0d
497 veor @y[4], @y[4], @y[7]
498 veor @t[7], @t[7], @t[6] @ restore t[7]
499 veor @y[7], @y[7], @t[4]
500 vext.8 @t[6], @t[6], @t[6], #12
501 veor @y[2], @y[2], @t[0]
502 veor @y[7], @y[7], @t[5]
503 vext.8 @t[7], @t[7], @t[7], #12
504 veor @y[2], @y[2], @t[2]
506 veor @y[3], @y[3], @y[1]
507 veor @y[1], @y[1], @t[1]
508 veor @y[0], @y[0], @t[0]
509 veor @y[3], @y[3], @t[0]
510 veor @y[1], @y[1], @t[5]
511 veor @y[0], @y[0], @t[5]
512 vext.8 @t[0], @t[0], @t[0], #12
513 veor @y[1], @y[1], @t[7]
514 veor @y[0], @y[0], @t[6]
515 veor @y[3], @y[3], @y[1]
516 veor @y[4], @y[4], @t[1]
517 vext.8 @t[1], @t[1], @t[1], #12
519 veor @y[7], @y[7], @t[7]
520 veor @y[4], @y[4], @t[2]
521 veor @y[5], @y[5], @t[2]
522 veor @y[2], @y[2], @t[6]
523 veor @t[6], @t[6], @t[3] @ clobber t[6]
524 vext.8 @t[2], @t[2], @t[2], #12
525 veor @y[4], @y[4], @y[7]
526 veor @y[3], @y[3], @t[6]
528 veor @y[6], @y[6], @t[6]
529 veor @y[5], @y[5], @t[5]
530 vext.8 @t[5], @t[5], @t[5], #12
531 veor @y[6], @y[6], @t[4]
532 vext.8 @t[4], @t[4], @t[4], #12
533 veor @y[5], @y[5], @t[6]
534 veor @y[6], @y[6], @t[7]
535 vext.8 @t[7], @t[7], @t[7], #12
536 veor @t[6], @t[6], @t[3] @ restore t[6]
537 vext.8 @t[3], @t[3], @t[3], #12
539 @ multiplication by 0x09
540 veor @y[4], @y[4], @y[1]
541 veor @t[1], @t[1], @y[1] @ t[1]=y[1]
542 veor @t[0], @t[0], @t[5] @ clobber t[0]
543 vext.8 @t[6], @t[6], @t[6], #12
544 veor @t[1], @t[1], @t[5]
545 veor @y[3], @y[3], @t[0]
546 veor @t[0], @t[0], @y[0] @ t[0]=y[0]
547 veor @t[1], @t[1], @t[6]
548 veor @t[6], @t[6], @t[7] @ clobber t[6]
549 veor @y[4], @y[4], @t[1]
550 veor @y[7], @y[7], @t[4]
551 veor @y[6], @y[6], @t[3]
552 veor @y[5], @y[5], @t[2]
553 veor @t[4], @t[4], @y[4] @ t[4]=y[4]
554 veor @t[3], @t[3], @y[3] @ t[3]=y[3]
555 veor @t[5], @t[5], @y[5] @ t[5]=y[5]
556 veor @t[2], @t[2], @y[2] @ t[2]=y[2]
557 veor @t[3], @t[3], @t[7]
558 veor @XMM[5], @t[5], @t[6]
559 veor @XMM[6], @t[6], @y[6] @ t[6]=y[6]
560 veor @XMM[2], @t[2], @t[6]
561 veor @XMM[7], @t[7], @y[7] @ t[7]=y[7]
565 @ vmov @XMM[2], @t[2]
568 @ vmov @XMM[5], @t[5]
569 @ vmov @XMM[6], @t[6]
570 @ vmov @XMM[7], @t[7]
575 my ($a,$b,$n,$mask,$t)=@_;
586 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
588 vshr.u64 $t0, $b0, #$n
589 vshr.u64 $t1, $b1, #$n
595 vshl.u64 $t0, $t0, #$n
597 vshl.u64 $t1, $t1, #$n
604 my @x=reverse(@_[0..7]);
605 my ($t0,$t1,$t2,$t3)=@_[8..11];
607 vmov.i8 $t0,#0x55 @ compose .LBS0
608 vmov.i8 $t1,#0x33 @ compose .LBS1
610 &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
611 &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
613 vmov.i8 $t0,#0x0f @ compose .LBS2
615 &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
616 &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
618 &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
619 &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
623 #include "arm_arch.h"
630 .type _bsaes_decrypt8,%function
633 sub $const,pc,#8 @ _bsaes_decrypt8
634 vldmia $key!, {@XMM[9]} @ round 0 key
635 add $const,$const,#.LM0ISR-_bsaes_decrypt8
637 vldmia $const!, {@XMM[8]} @ .LM0ISR
638 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
639 veor @XMM[11], @XMM[1], @XMM[9]
640 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
641 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
642 veor @XMM[12], @XMM[2], @XMM[9]
643 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
644 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
645 veor @XMM[13], @XMM[3], @XMM[9]
646 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
647 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
648 veor @XMM[14], @XMM[4], @XMM[9]
649 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
650 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
651 veor @XMM[15], @XMM[5], @XMM[9]
652 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
653 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
654 veor @XMM[10], @XMM[6], @XMM[9]
655 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
656 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
657 veor @XMM[11], @XMM[7], @XMM[9]
658 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
659 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
660 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
661 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
663 &bitslice (@XMM[0..7, 8..11]);
665 sub $rounds,$rounds,#1
670 &ShiftRows (@XMM[0..7, 8..12]);
671 $code.=".Ldec_sbox:\n";
672 &InvSbox (@XMM[0..7, 8..15]);
674 subs $rounds,$rounds,#1
677 &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
679 vldmia $const, {@XMM[12]} @ .LISR
680 addeq $const,$const,#0x10
682 vldmia $const, {@XMM[12]} @ .LISRM0
687 &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
689 vldmia $key, {@XMM[8]} @ last round key
690 veor @XMM[6], @XMM[6], @XMM[8]
691 veor @XMM[4], @XMM[4], @XMM[8]
692 veor @XMM[2], @XMM[2], @XMM[8]
693 veor @XMM[7], @XMM[7], @XMM[8]
694 veor @XMM[3], @XMM[3], @XMM[8]
695 veor @XMM[5], @XMM[5], @XMM[8]
696 veor @XMM[0], @XMM[0], @XMM[8]
697 veor @XMM[1], @XMM[1], @XMM[8]
699 .size _bsaes_decrypt8,.-_bsaes_decrypt8
701 .type _bsaes_const,%object
704 .LM0ISR: @ InvShiftRows constants
705 .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
707 .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
709 .quad 0x01040b0e0205080f, 0x0306090c00070a0d
710 .LM0SR: @ ShiftRows constants
711 .quad 0x0a0e02060f03070b, 0x0004080c05090d01
713 .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
715 .quad 0x0304090e00050a0f, 0x01060b0c0207080d
717 .quad 0x02060a0e03070b0f, 0x0004080c0105090d
718 .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
720 .size _bsaes_const,.-_bsaes_const
722 .type _bsaes_encrypt8,%function
725 sub $const,pc,#8 @ _bsaes_encrypt8
726 vldmia $key!, {@XMM[9]} @ round 0 key
727 sub $const,$const,#_bsaes_encrypt8-.LM0SR
729 vldmia $const!, {@XMM[8]} @ .LM0SR
730 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
731 veor @XMM[11], @XMM[1], @XMM[9]
732 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
733 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
734 veor @XMM[12], @XMM[2], @XMM[9]
735 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
736 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
737 veor @XMM[13], @XMM[3], @XMM[9]
738 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
739 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
740 veor @XMM[14], @XMM[4], @XMM[9]
741 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
742 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
743 veor @XMM[15], @XMM[5], @XMM[9]
744 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
745 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
746 veor @XMM[10], @XMM[6], @XMM[9]
747 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
748 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
749 veor @XMM[11], @XMM[7], @XMM[9]
750 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
751 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
752 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
753 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
754 _bsaes_encrypt8_bitslice:
756 &bitslice (@XMM[0..7, 8..11]);
758 sub $rounds,$rounds,#1
763 &ShiftRows (@XMM[0..7, 8..12]);
764 $code.=".Lenc_sbox:\n";
765 &Sbox (@XMM[0..7, 8..15]);
767 subs $rounds,$rounds,#1
770 &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
772 vldmia $const, {@XMM[12]} @ .LSR
773 addeq $const,$const,#0x10
775 vldmia $const, {@XMM[12]} @ .LSRM0
780 # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
781 &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
783 vldmia $key, {@XMM[8]} @ last round key
784 veor @XMM[4], @XMM[4], @XMM[8]
785 veor @XMM[6], @XMM[6], @XMM[8]
786 veor @XMM[3], @XMM[3], @XMM[8]
787 veor @XMM[7], @XMM[7], @XMM[8]
788 veor @XMM[2], @XMM[2], @XMM[8]
789 veor @XMM[5], @XMM[5], @XMM[8]
790 veor @XMM[0], @XMM[0], @XMM[8]
791 veor @XMM[1], @XMM[1], @XMM[8]
793 .size _bsaes_encrypt8,.-_bsaes_encrypt8
797 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
800 my @x=reverse(@_[0..7]);
801 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
803 &swapmove (@x[0,1],1,$bs0,$t2,$t3);
805 @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
809 #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
811 &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
813 @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
819 &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
820 &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
824 .type _bsaes_key_convert,%function
827 sub $const,pc,#8 @ _bsaes_key_convert
828 vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
829 sub $const,$const,#_bsaes_key_convert-.LM0
830 vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
832 vmov.i8 @XMM[8], #0x01 @ bit masks
833 vmov.i8 @XMM[9], #0x02
834 vmov.i8 @XMM[10], #0x04
835 vmov.i8 @XMM[11], #0x08
836 vmov.i8 @XMM[12], #0x10
837 vmov.i8 @XMM[13], #0x20
838 vldmia $const, {@XMM[14]} @ .LM0
841 vrev32.8 @XMM[7], @XMM[7]
842 vrev32.8 @XMM[15], @XMM[15]
844 sub $rounds,$rounds,#1
845 vstmia $out!, {@XMM[7]} @ save round 0 key
850 vtbl.8 `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
851 vtbl.8 `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
852 vmov.i8 @XMM[6], #0x40
853 vmov.i8 @XMM[15], #0x80
855 vtst.8 @XMM[0], @XMM[7], @XMM[8]
856 vtst.8 @XMM[1], @XMM[7], @XMM[9]
857 vtst.8 @XMM[2], @XMM[7], @XMM[10]
858 vtst.8 @XMM[3], @XMM[7], @XMM[11]
859 vtst.8 @XMM[4], @XMM[7], @XMM[12]
860 vtst.8 @XMM[5], @XMM[7], @XMM[13]
861 vtst.8 @XMM[6], @XMM[7], @XMM[6]
862 vtst.8 @XMM[7], @XMM[7], @XMM[15]
863 vld1.8 {@XMM[15]}, [$inp]! @ load next round key
864 vmvn @XMM[0], @XMM[0] @ "pnot"
865 vmvn @XMM[1], @XMM[1]
866 vmvn @XMM[5], @XMM[5]
867 vmvn @XMM[6], @XMM[6]
869 vrev32.8 @XMM[15], @XMM[15]
871 subs $rounds,$rounds,#1
872 vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
875 vmov.i8 @XMM[7],#0x63 @ compose .L63
876 @ don't save last round key
878 .size _bsaes_key_convert,.-_bsaes_key_convert
882 if (1) { # following four functions are unsupported interface
883 # used for benchmarking...
885 .globl bsaes_enc_key_convert
886 .type bsaes_enc_key_convert,%function
888 bsaes_enc_key_convert:
890 vstmdb sp!,{d8-d15} @ ABI specification says so
892 ldr r5,[$inp,#240] @ pass rounds
893 mov r4,$inp @ pass key
894 mov r12,$out @ pass key schedule
895 bl _bsaes_key_convert
896 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
897 vstmia r12, {@XMM[7]} @ save last round key
901 .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
903 .globl bsaes_encrypt_128
904 .type bsaes_encrypt_128,%function
908 vstmdb sp!,{d8-d15} @ ABI specification says so
910 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
911 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
912 mov r4,$key @ pass the key
913 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
914 mov r5,#10 @ pass rounds
915 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
919 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
920 vst1.8 {@XMM[4]}, [$out]!
921 vst1.8 {@XMM[6]}, [$out]!
922 vst1.8 {@XMM[3]}, [$out]!
923 vst1.8 {@XMM[7]}, [$out]!
924 vst1.8 {@XMM[2]}, [$out]!
926 vst1.8 {@XMM[5]}, [$out]!
931 .size bsaes_encrypt_128,.-bsaes_encrypt_128
933 .globl bsaes_dec_key_convert
934 .type bsaes_dec_key_convert,%function
936 bsaes_dec_key_convert:
938 vstmdb sp!,{d8-d15} @ ABI specification says so
940 ldr r5,[$inp,#240] @ pass rounds
941 mov r4,$inp @ pass key
942 mov r12,$out @ pass key schedule
943 bl _bsaes_key_convert
944 vldmia $out, {@XMM[6]}
945 vstmia r12, {@XMM[15]} @ save last round key
946 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
947 vstmia $out, {@XMM[7]}
951 .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
953 .globl bsaes_decrypt_128
954 .type bsaes_decrypt_128,%function
958 vstmdb sp!,{d8-d15} @ ABI specification says so
960 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
961 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
962 mov r4,$key @ pass the key
963 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
964 mov r5,#10 @ pass rounds
965 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
969 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
970 vst1.8 {@XMM[6]}, [$out]!
971 vst1.8 {@XMM[4]}, [$out]!
972 vst1.8 {@XMM[2]}, [$out]!
973 vst1.8 {@XMM[7]}, [$out]!
974 vst1.8 {@XMM[3]}, [$out]!
976 vst1.8 {@XMM[5]}, [$out]!
981 .size bsaes_decrypt_128,.-bsaes_decrypt_128
988 $code =~ s/\`([^\`]*)\`/eval($1)/gem;