2019-06-04 11:11:33 +03:00
/* SPDX-License-Identifier: GPL-2.0-only */
2017-01-11 19:41:53 +03:00
/ *
* Scalar A E S c o r e t r a n s f o r m
*
* Copyright ( C ) 2 0 1 7 L i n a r o L t d .
* Author : Ard B i e s h e u v e l < a r d . b i e s h e u v e l @linaro.org>
* /
# include < l i n u x / l i n k a g e . h >
2018-10-18 07:37:59 +03:00
# include < a s m / a s s e m b l e r . h >
2017-07-24 13:28:19 +03:00
# include < a s m / c a c h e . h >
2017-01-11 19:41:53 +03:00
.text
.align 5
rk . r e q r0
rounds . r e q r1
in . r e q r2
out . r e q r3
2017-01-13 11:33:26 +03:00
ttab . r e q i p
2017-01-11 19:41:53 +03:00
t0 . r e q l r
t1 . r e q r2
t2 . r e q r3
.macro _ _ select, o u t , i n , i d x
.if __LINUX_ARM_ARCH__ < 7
and \ o u t , \ i n , #0xff < < ( 8 * \ i d x )
.else
ubfx \ o u t , \ i n , #( 8 * \ i d x ) , #8
.endif
.endm
2017-07-24 13:28:19 +03:00
.macro _ _ load, o u t , i n , i d x , s z , o p
2017-01-11 19:41:53 +03:00
.if __LINUX_ARM_ARCH__ < 7 & & \ idx > 0
2017-07-24 13:28:19 +03:00
ldr\ o p \ o u t , [ t t a b , \ i n , l s r #( 8 * \ i d x ) - \ s z ]
2017-01-11 19:41:53 +03:00
.else
2017-07-24 13:28:19 +03:00
ldr\ o p \ o u t , [ t t a b , \ i n , l s l #\ s z ]
2017-01-11 19:41:53 +03:00
.endif
.endm
2018-10-18 07:37:59 +03:00
.macro _ _ hround, o u t 0 , o u t 1 , i n 0 , i n 1 , i n 2 , i n 3 , t 3 , t 4 , e n c , s z , o p , o l d c p s r
2017-01-11 19:41:53 +03:00
_ _ select \ o u t 0 , \ i n 0 , 0
_ _ select t 0 , \ i n 1 , 1
2017-07-24 13:28:19 +03:00
_ _ load \ o u t 0 , \ o u t 0 , 0 , \ s z , \ o p
_ _ load t 0 , t 0 , 1 , \ s z , \ o p
2017-01-11 19:41:53 +03:00
.if \ enc
_ _ select \ o u t 1 , \ i n 1 , 0
_ _ select t 1 , \ i n 2 , 1
.else
_ _ select \ o u t 1 , \ i n 3 , 0
_ _ select t 1 , \ i n 0 , 1
.endif
2017-07-24 13:28:19 +03:00
_ _ load \ o u t 1 , \ o u t 1 , 0 , \ s z , \ o p
2017-01-11 19:41:53 +03:00
_ _ select t 2 , \ i n 2 , 2
2017-07-24 13:28:19 +03:00
_ _ load t 1 , t 1 , 1 , \ s z , \ o p
_ _ load t 2 , t 2 , 2 , \ s z , \ o p
2017-01-11 19:41:53 +03:00
eor \ o u t 0 , \ o u t 0 , t 0 , r o r #24
_ _ select t 0 , \ i n 3 , 3
.if \ enc
_ _ select \ t 3 , \ i n 3 , 2
_ _ select \ t 4 , \ i n 0 , 3
.else
_ _ select \ t 3 , \ i n 1 , 2
_ _ select \ t 4 , \ i n 2 , 3
.endif
2017-07-24 13:28:19 +03:00
_ _ load \ t 3 , \ t 3 , 2 , \ s z , \ o p
_ _ load t 0 , t 0 , 3 , \ s z , \ o p
_ _ load \ t 4 , \ t 4 , 3 , \ s z , \ o p
2017-01-11 19:41:53 +03:00
2018-10-18 07:37:59 +03:00
.ifnb \ oldcpsr
/ *
* This i s t h e f i n a l r o u n d a n d w e ' r e d o n e w i t h a l l d a t a - d e p e n d e n t t a b l e
* lookups, s o w e c a n s a f e l y r e - e n a b l e i n t e r r u p t s .
* /
restore_ i r q s \ o l d c p s r
.endif
2017-01-11 19:41:53 +03:00
eor \ o u t 1 , \ o u t 1 , t 1 , r o r #24
eor \ o u t 0 , \ o u t 0 , t 2 , r o r #16
ldm r k ! , { t 1 , t 2 }
eor \ o u t 1 , \ o u t 1 , \ t 3 , r o r #16
eor \ o u t 0 , \ o u t 0 , t 0 , r o r #8
eor \ o u t 1 , \ o u t 1 , \ t 4 , r o r #8
eor \ o u t 0 , \ o u t 0 , t 1
eor \ o u t 1 , \ o u t 1 , t 2
.endm
2018-10-18 07:37:59 +03:00
.macro fround, o u t 0 , o u t 1 , o u t 2 , o u t 3 , i n 0 , i n 1 , i n 2 , i n 3 , s z =2 , o p , o l d c p s r
2017-07-24 13:28:19 +03:00
_ _ hround \ o u t 0 , \ o u t 1 , \ i n 0 , \ i n 1 , \ i n 2 , \ i n 3 , \ o u t 2 , \ o u t 3 , 1 , \ s z , \ o p
2018-10-18 07:37:59 +03:00
_ _ hround \ o u t 2 , \ o u t 3 , \ i n 2 , \ i n 3 , \ i n 0 , \ i n 1 , \ i n 1 , \ i n 2 , 1 , \ s z , \ o p , \ o l d c p s r
2017-01-11 19:41:53 +03:00
.endm
2018-10-18 07:37:59 +03:00
.macro iround, o u t 0 , o u t 1 , o u t 2 , o u t 3 , i n 0 , i n 1 , i n 2 , i n 3 , s z =2 , o p , o l d c p s r
2017-07-24 13:28:19 +03:00
_ _ hround \ o u t 0 , \ o u t 1 , \ i n 0 , \ i n 3 , \ i n 2 , \ i n 1 , \ o u t 2 , \ o u t 3 , 0 , \ s z , \ o p
2018-10-18 07:37:59 +03:00
_ _ hround \ o u t 2 , \ o u t 3 , \ i n 2 , \ i n 1 , \ i n 0 , \ i n 3 , \ i n 1 , \ i n 0 , 0 , \ s z , \ o p , \ o l d c p s r
2017-01-11 19:41:53 +03:00
.endm
2017-07-24 13:28:19 +03:00
.macro do_ c r y p t , r o u n d , t t a b , l t a b , b s z
2017-01-11 19:41:53 +03:00
push { r3 - r11 , l r }
2018-10-18 07:37:59 +03:00
/ / Load k e y s f i r s t , t o r e d u c e l a t e n c y i n c a s e t h e y ' r e n o t c a c h e d y e t .
ldm r k ! , { r8 - r11 }
2017-01-11 19:41:53 +03:00
ldr r4 , [ i n ]
ldr r5 , [ i n , #4 ]
ldr r6 , [ i n , #8 ]
ldr r7 , [ i n , #12 ]
# ifdef C O N F I G _ C P U _ B I G _ E N D I A N
2021-03-10 13:14:20 +03:00
rev_ l r4 , t 0
rev_ l r5 , t 0
rev_ l r6 , t 0
rev_ l r7 , t 0
2017-01-11 19:41:53 +03:00
# endif
eor r4 , r4 , r8
eor r5 , r5 , r9
eor r6 , r6 , r10
eor r7 , r7 , r11
2021-03-10 13:14:20 +03:00
mov_ l t t a b , \ t t a b
2018-10-18 07:37:59 +03:00
/ *
* Disable i n t e r r u p t s a n d p r e f e t c h t h e 1 0 2 4 - b y t e ' f t ' o r ' i t ' t a b l e i n t o
* L1 c a c h e , a s s u m i n g c a c h e l i n e s i z e > = 3 2 . T h i s i s a h a r d e n i n g m e a s u r e
* intended t o m a k e c a c h e - t i m i n g a t t a c k s m o r e d i f f i c u l t . T h e y m a y n o t
* be f u l l y p r e v e n t e d , h o w e v e r ; see the paper
* https : / / cr. y p . t o / a n t i f o r g e r y / c a c h e t i m i n g - 2 0 0 5 0 4 1 4 . p d f
* ( " Cache- t i m i n g a t t a c k s o n A E S " ) f o r a d i s c u s s i o n o f t h e m a n y
* difficulties i n v o l v e d i n w r i t i n g t r u l y c o n s t a n t - t i m e A E S s o f t w a r e .
* /
save_ a n d _ d i s a b l e _ i r q s t 0
.set i, 0
.rept 1024 / 1 2 8
ldr r8 , [ t t a b , #i + 0 ]
ldr r9 , [ t t a b , #i + 32 ]
ldr r10 , [ t t a b , #i + 64 ]
ldr r11 , [ t t a b , #i + 96 ]
.set i, i + 1 2 8
.endr
push { t 0 } / / o l d c p s r
2017-01-11 19:41:53 +03:00
tst r o u n d s , #2
bne 1 f
0 : \ round r8 , r9 , r10 , r11 , r4 , r5 , r6 , r7
\ round r4 , r5 , r6 , r7 , r8 , r9 , r10 , r11
1 : subs r o u n d s , r o u n d s , #4
\ round r8 , r9 , r10 , r11 , r4 , r5 , r6 , r7
2017-07-24 13:28:19 +03:00
bls 2 f
2017-01-11 19:41:53 +03:00
\ round r4 , r5 , r6 , r7 , r8 , r9 , r10 , r11
2017-07-24 13:28:19 +03:00
b 0 b
2018-10-18 07:37:59 +03:00
2 : .ifb \ l t a b
add t t a b , t t a b , #1
.else
2021-03-10 13:14:20 +03:00
mov_ l t t a b , \ l t a b
2018-10-18 07:37:59 +03:00
/ / Prefetch i n v e r s e S - b o x f o r f i n a l r o u n d ; see explanation above
.set i, 0
.rept 256 / 6 4
ldr t 0 , [ t t a b , #i + 0 ]
ldr t 1 , [ t t a b , #i + 32 ]
.set i, i + 6 4
.endr
.endif
pop { r o u n d s } / / o l d c p s r
\ round r4 , r5 , r6 , r7 , r8 , r9 , r10 , r11 , \ b s z , b , r o u n d s
2017-01-11 19:41:53 +03:00
# ifdef C O N F I G _ C P U _ B I G _ E N D I A N
2021-03-10 13:14:20 +03:00
rev_ l r4 , t 0
rev_ l r5 , t 0
rev_ l r6 , t 0
rev_ l r7 , t 0
2017-01-11 19:41:53 +03:00
# endif
ldr o u t , [ s p ]
str r4 , [ o u t ]
str r5 , [ o u t , #4 ]
str r6 , [ o u t , #8 ]
str r7 , [ o u t , #12 ]
pop { r3 - r11 , p c }
.align 3
.ltorg
.endm
2018-02-12 16:52:37 +03:00
ENTRY( _ _ a e s _ a r m _ e n c r y p t )
2018-10-18 07:37:59 +03:00
do_ c r y p t f r o u n d , c r y p t o _ f t _ t a b ,, 2
2018-02-12 16:52:37 +03:00
ENDPROC( _ _ a e s _ a r m _ e n c r y p t )
.align 5
ENTRY( _ _ a e s _ a r m _ d e c r y p t )
2019-07-02 22:41:48 +03:00
do_ c r y p t i r o u n d , c r y p t o _ i t _ t a b , c r y p t o _ a e s _ i n v _ s b o x , 0
2018-02-12 16:52:37 +03:00
ENDPROC( _ _ a e s _ a r m _ d e c r y p t )