2014-03-21 13:19:17 +04:00
/ *
* linux/ a r c h / a r m 6 4 / c r y p t o / a e s - c e . S - A E S c i p h e r f o r A R M v8 w i t h
* Crypto E x t e n s i o n s
*
2017-07-24 13:28:10 +03:00
* Copyright ( C ) 2 0 1 3 - 2 0 1 7 L i n a r o L t d < a r d . b i e s h e u v e l @linaro.org>
2014-03-21 13:19:17 +04:00
*
* This p r o g r a m i s f r e e s o f t w a r e ; you can redistribute it and/or modify
* it u n d e r t h e t e r m s o f t h e G N U G e n e r a l P u b l i c L i c e n s e v e r s i o n 2 a s
* published b y t h e F r e e S o f t w a r e F o u n d a t i o n .
* /
# include < l i n u x / l i n k a g e . h >
2016-10-11 21:15:19 +03:00
# include < a s m / a s s e m b l e r . h >
2014-03-21 13:19:17 +04:00
# define A E S _ E N T R Y ( f u n c ) E N T R Y ( c e _ ## f u n c )
# define A E S _ E N D P R O C ( f u n c ) E N D P R O C ( c e _ ## f u n c )
.arch armv8 - a + c r y p t o
/* preload all round keys */
.macro load_ r o u n d _ k e y s , r o u n d s , r k
cmp \ r o u n d s , #12
blo 2 2 2 2 f / * 1 2 8 b i t s * /
beq 1 1 1 1 f / * 1 9 2 b i t s * /
2017-07-24 13:28:10 +03:00
ld1 { v17 . 4 s - v18 . 4 s } , [ \ r k ] , #32
1111 : ld1 { v19 . 4 s - v20 . 4 s } , [ \ r k ] , #32
2222 : ld1 { v21 . 4 s - v24 . 4 s } , [ \ r k ] , #64
ld1 { v25 . 4 s - v28 . 4 s } , [ \ r k ] , #64
ld1 { v29 . 4 s - v31 . 4 s } , [ \ r k ]
2014-03-21 13:19:17 +04:00
.endm
/* prepare for encryption with key in rk[] */
2018-04-30 19:18:24 +03:00
.macro enc_ p r e p a r e , r o u n d s , r k , t e m p
mov \ t e m p , \ r k
load_ r o u n d _ k e y s \ r o u n d s , \ t e m p
2014-03-21 13:19:17 +04:00
.endm
/* prepare for encryption (again) but with new key in rk[] */
2018-04-30 19:18:24 +03:00
.macro enc_ s w i t c h _ k e y , r o u n d s , r k , t e m p
mov \ t e m p , \ r k
load_ r o u n d _ k e y s \ r o u n d s , \ t e m p
2014-03-21 13:19:17 +04:00
.endm
/* prepare for decryption with key in rk[] */
2018-04-30 19:18:24 +03:00
.macro dec_ p r e p a r e , r o u n d s , r k , t e m p
mov \ t e m p , \ r k
load_ r o u n d _ k e y s \ r o u n d s , \ t e m p
2014-03-21 13:19:17 +04:00
.endm
.macro do_ e n c _ N x , d e , m c , k , i 0 , i 1 , i 2 , i 3
aes\ d e \ i 0 \ ( ) . 1 6 b , \ k \ ( ) . 1 6 b
aes\ m c \ i 0 \ ( ) . 1 6 b , \ i 0 \ ( ) . 1 6 b
.ifnb \ i1
2015-03-17 21:05:13 +03:00
aes\ d e \ i 1 \ ( ) . 1 6 b , \ k \ ( ) . 1 6 b
2014-03-21 13:19:17 +04:00
aes\ m c \ i 1 \ ( ) . 1 6 b , \ i 1 \ ( ) . 1 6 b
.ifnb \ i3
2015-03-17 21:05:13 +03:00
aes\ d e \ i 2 \ ( ) . 1 6 b , \ k \ ( ) . 1 6 b
2014-03-21 13:19:17 +04:00
aes\ m c \ i 2 \ ( ) . 1 6 b , \ i 2 \ ( ) . 1 6 b
2015-03-17 21:05:13 +03:00
aes\ d e \ i 3 \ ( ) . 1 6 b , \ k \ ( ) . 1 6 b
2014-03-21 13:19:17 +04:00
aes\ m c \ i 3 \ ( ) . 1 6 b , \ i 3 \ ( ) . 1 6 b
.endif
.endif
.endm
/* up to 4 interleaved encryption rounds with the same round key */
.macro round_ N x , e n c , k , i 0 , i 1 , i 2 , i 3
.ifc \ enc, e
do_ e n c _ N x e , m c , \ k , \ i 0 , \ i 1 , \ i 2 , \ i 3
.else
do_ e n c _ N x d , i m c , \ k , \ i 0 , \ i 1 , \ i 2 , \ i 3
.endif
.endm
/* up to 4 interleaved final rounds */
.macro fin_ r o u n d _ N x , d e , k , k 2 , i 0 , i 1 , i 2 , i 3
aes\ d e \ i 0 \ ( ) . 1 6 b , \ k \ ( ) . 1 6 b
.ifnb \ i1
aes\ d e \ i 1 \ ( ) . 1 6 b , \ k \ ( ) . 1 6 b
.ifnb \ i3
aes\ d e \ i 2 \ ( ) . 1 6 b , \ k \ ( ) . 1 6 b
aes\ d e \ i 3 \ ( ) . 1 6 b , \ k \ ( ) . 1 6 b
.endif
.endif
eor \ i 0 \ ( ) . 1 6 b , \ i 0 \ ( ) . 1 6 b , \ k 2 \ ( ) . 1 6 b
.ifnb \ i1
eor \ i 1 \ ( ) . 1 6 b , \ i 1 \ ( ) . 1 6 b , \ k 2 \ ( ) . 1 6 b
.ifnb \ i3
eor \ i 2 \ ( ) . 1 6 b , \ i 2 \ ( ) . 1 6 b , \ k 2 \ ( ) . 1 6 b
eor \ i 3 \ ( ) . 1 6 b , \ i 3 \ ( ) . 1 6 b , \ k 2 \ ( ) . 1 6 b
.endif
.endif
.endm
/* up to 4 interleaved blocks */
.macro do_ b l o c k _ N x , e n c , r o u n d s , i 0 , i 1 , i 2 , i 3
cmp \ r o u n d s , #12
blo 2 2 2 2 f / * 1 2 8 b i t s * /
beq 1 1 1 1 f / * 1 9 2 b i t s * /
round_ N x \ e n c , v17 , \ i 0 , \ i 1 , \ i 2 , \ i 3
round_ N x \ e n c , v18 , \ i 0 , \ i 1 , \ i 2 , \ i 3
1111 : round_ N x \ e n c , v19 , \ i 0 , \ i 1 , \ i 2 , \ i 3
round_ N x \ e n c , v20 , \ i 0 , \ i 1 , \ i 2 , \ i 3
2222 : .irp k e y , v 21 , v2 2 , v23 , v24 , v25 , v26 , v27 , v28 , v29
round_ N x \ e n c , \ k e y , \ i 0 , \ i 1 , \ i 2 , \ i 3
.endr
fin_ r o u n d _ N x \ e n c , v30 , v31 , \ i 0 , \ i 1 , \ i 2 , \ i 3
.endm
.macro encrypt_ b l o c k , i n , r o u n d s , t 0 , t 1 , t 2
do_ b l o c k _ N x e , \ r o u n d s , \ i n
.endm
.macro encrypt_ b l o c k 2 x , i 0 , i 1 , r o u n d s , t 0 , t 1 , t 2
do_ b l o c k _ N x e , \ r o u n d s , \ i 0 , \ i 1
.endm
.macro encrypt_ b l o c k 4 x , i 0 , i 1 , i 2 , i 3 , r o u n d s , t 0 , t 1 , t 2
do_ b l o c k _ N x e , \ r o u n d s , \ i 0 , \ i 1 , \ i 2 , \ i 3
.endm
.macro decrypt_ b l o c k , i n , r o u n d s , t 0 , t 1 , t 2
do_ b l o c k _ N x d , \ r o u n d s , \ i n
.endm
.macro decrypt_ b l o c k 2 x , i 0 , i 1 , r o u n d s , t 0 , t 1 , t 2
do_ b l o c k _ N x d , \ r o u n d s , \ i 0 , \ i 1
.endm
.macro decrypt_ b l o c k 4 x , i 0 , i 1 , i 2 , i 3 , r o u n d s , t 0 , t 1 , t 2
do_ b l o c k _ N x d , \ r o u n d s , \ i 0 , \ i 1 , \ i 2 , \ i 3
.endm
# include " a e s - m o d e s . S "