2014-02-10 14:26:29 +04:00
/ *
* aesce- c c m - c o r e . S - A E S - C C M t r a n s f o r m f o r A R M v8 w i t h C r y p t o 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-02-10 14:26:29 +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:17 +03:00
# include < a s m / a s s e m b l e r . h >
2014-02-10 14:26:29 +04:00
.text
.arch armv8 - a + c r y p t o
/ *
* void c e _ a e s _ c c m _ a u t h _ d a t a ( u 8 m a c [ ] , u 8 c o n s t i n [ ] , u 3 2 a b y t e s ,
* u3 2 * m a c p , u 8 c o n s t r k [ ] , u 3 2 r o u n d s ) ;
* /
ENTRY( c e _ a e s _ c c m _ a u t h _ d a t a )
2018-07-29 17:52:30 +03:00
ldr w8 , [ x3 ] / * l e f t o v e r f r o m p r e v r o u n d ? * /
2016-10-11 21:15:17 +03:00
ld1 { v0 . 1 6 b } , [ x0 ] / * l o a d m a c * /
2018-07-29 17:52:30 +03:00
cbz w8 , 1 f
sub w8 , w8 , #16
2014-02-10 14:26:29 +04:00
eor v1 . 1 6 b , v1 . 1 6 b , v1 . 1 6 b
2018-07-29 17:52:30 +03:00
0 : ldrb w7 , [ x1 ] , #1 / * g e t 1 b y t e o f i n p u t * /
subs w2 , w2 , #1
add w8 , w8 , #1
2014-02-10 14:26:29 +04:00
ins v1 . b [ 0 ] , w7
ext v1 . 1 6 b , v1 . 1 6 b , v1 . 1 6 b , #1 / * r o t a t e i n t h e i n p u t b y t e s * /
beq 8 f / * o u t o f i n p u t ? * /
2018-07-29 17:52:30 +03:00
cbnz w8 , 0 b
2014-02-10 14:26:29 +04:00
eor v0 . 1 6 b , v0 . 1 6 b , v1 . 1 6 b
2018-07-29 17:52:30 +03:00
1 : ld1 { v3 . 4 s } , [ x4 ] / * l o a d f i r s t r o u n d k e y * /
prfm p l d l 1 s t r m , [ x1 ]
cmp w5 , #12 / * w h i c h k e y s i z e ? * /
add x6 , x4 , #16
sub w7 , w5 , #2 / * m o d i f i e d # o f r o u n d s * /
2014-02-10 14:26:29 +04:00
bmi 2 f
bne 5 f
mov v5 . 1 6 b , v3 . 1 6 b
b 4 f
2 : mov v4 . 1 6 b , v3 . 1 6 b
2017-07-24 13:28:10 +03:00
ld1 { v5 . 4 s } , [ x6 ] , #16 / * l o a d 2 n d r o u n d k e y * /
2014-02-10 14:26:29 +04:00
3 : aese v0 . 1 6 b , v4 . 1 6 b
aesmc v0 . 1 6 b , v0 . 1 6 b
2017-07-24 13:28:10 +03:00
4 : ld1 { v3 . 4 s } , [ x6 ] , #16 / * l o a d n e x t r o u n d k e y * /
2014-02-10 14:26:29 +04:00
aese v0 . 1 6 b , v5 . 1 6 b
aesmc v0 . 1 6 b , v0 . 1 6 b
2017-07-24 13:28:10 +03:00
5 : ld1 { v4 . 4 s } , [ x6 ] , #16 / * l o a d n e x t r o u n d k e y * /
2014-02-10 14:26:29 +04:00
subs w7 , w7 , #3
aese v0 . 1 6 b , v3 . 1 6 b
aesmc v0 . 1 6 b , v0 . 1 6 b
2017-07-24 13:28:10 +03:00
ld1 { v5 . 4 s } , [ x6 ] , #16 / * l o a d n e x t r o u n d k e y * /
2014-02-10 14:26:29 +04:00
bpl 3 b
aese v0 . 1 6 b , v4 . 1 6 b
2018-07-29 17:52:30 +03:00
subs w2 , w2 , #16 / * l a s t d a t a ? * /
2014-02-10 14:26:29 +04:00
eor v0 . 1 6 b , v0 . 1 6 b , v5 . 1 6 b / * f i n a l r o u n d * /
bmi 6 f
2018-07-29 17:52:30 +03:00
ld1 { v1 . 1 6 b } , [ x1 ] , #16 / * l o a d n e x t i n p u t b l o c k * /
2014-02-10 14:26:29 +04:00
eor v0 . 1 6 b , v0 . 1 6 b , v1 . 1 6 b / * x o r w i t h m a c * /
2018-07-29 17:52:30 +03:00
bne 1 b
6 : st1 { v0 . 1 6 b } , [ x0 ] / * s t o r e m a c * /
2014-02-10 14:26:29 +04:00
beq 1 0 f
2018-07-29 17:52:30 +03:00
adds w2 , w2 , #16
2014-02-10 14:26:29 +04:00
beq 1 0 f
2018-07-29 17:52:30 +03:00
mov w8 , w2
7 : ldrb w7 , [ x1 ] , #1
2014-02-10 14:26:29 +04:00
umov w6 , v0 . b [ 0 ]
eor w6 , w6 , w7
2018-07-29 17:52:30 +03:00
strb w6 , [ x0 ] , #1
subs w2 , w2 , #1
2014-02-10 14:26:29 +04:00
beq 1 0 f
ext v0 . 1 6 b , v0 . 1 6 b , v0 . 1 6 b , #1 / * r o t a t e o u t t h e m a c b y t e s * /
b 7 b
2019-01-24 19:33:45 +03:00
8 : cbz w8 , 9 1 f
mov w7 , w8
2018-07-29 17:52:30 +03:00
add w8 , w8 , #16
2014-02-10 14:26:29 +04:00
9 : ext v1 . 1 6 b , v1 . 1 6 b , v1 . 1 6 b , #1
adds w7 , w7 , #1
bne 9 b
2019-01-24 19:33:45 +03:00
91 : eor v0 . 1 6 b , v0 . 1 6 b , v1 . 1 6 b
2018-07-29 17:52:30 +03:00
st1 { v0 . 1 6 b } , [ x0 ]
10 : str w8 , [ x3 ]
2014-02-10 14:26:29 +04:00
ret
ENDPROC( c e _ a e s _ c c m _ a u t h _ d a t a )
/ *
* void c e _ a e s _ c c m _ f i n a l ( u 8 m a c [ ] , u 8 c o n s t c t r [ ] , u 8 c o n s t r k [ ] ,
* u3 2 r o u n d s ) ;
* /
ENTRY( c e _ a e s _ c c m _ f i n a l )
2017-07-24 13:28:10 +03:00
ld1 { v3 . 4 s } , [ x2 ] , #16 / * l o a d f i r s t r o u n d k e y * /
2016-10-11 21:15:17 +03:00
ld1 { v0 . 1 6 b } , [ x0 ] / * l o a d m a c * /
2014-02-10 14:26:29 +04:00
cmp w3 , #12 / * w h i c h k e y s i z e ? * /
sub w3 , w3 , #2 / * m o d i f i e d # o f r o u n d s * /
2016-10-11 21:15:17 +03:00
ld1 { v1 . 1 6 b } , [ x1 ] / * l o a d 1 s t c t r i v * /
2014-02-10 14:26:29 +04:00
bmi 0 f
bne 3 f
mov v5 . 1 6 b , v3 . 1 6 b
b 2 f
0 : mov v4 . 1 6 b , v3 . 1 6 b
2017-07-24 13:28:10 +03:00
1 : ld1 { v5 . 4 s } , [ x2 ] , #16 / * l o a d n e x t r o u n d k e y * /
2014-02-10 14:26:29 +04:00
aese v0 . 1 6 b , v4 . 1 6 b
aesmc v0 . 1 6 b , v0 . 1 6 b
2015-03-17 21:05:13 +03:00
aese v1 . 1 6 b , v4 . 1 6 b
2014-02-10 14:26:29 +04:00
aesmc v1 . 1 6 b , v1 . 1 6 b
2017-07-24 13:28:10 +03:00
2 : ld1 { v3 . 4 s } , [ x2 ] , #16 / * l o a d n e x t r o u n d k e y * /
2014-02-10 14:26:29 +04:00
aese v0 . 1 6 b , v5 . 1 6 b
aesmc v0 . 1 6 b , v0 . 1 6 b
2015-03-17 21:05:13 +03:00
aese v1 . 1 6 b , v5 . 1 6 b
2014-02-10 14:26:29 +04:00
aesmc v1 . 1 6 b , v1 . 1 6 b
2017-07-24 13:28:10 +03:00
3 : ld1 { v4 . 4 s } , [ x2 ] , #16 / * l o a d n e x t r o u n d k e y * /
2014-02-10 14:26:29 +04:00
subs w3 , w3 , #3
aese v0 . 1 6 b , v3 . 1 6 b
aesmc v0 . 1 6 b , v0 . 1 6 b
2015-03-17 21:05:13 +03:00
aese v1 . 1 6 b , v3 . 1 6 b
2014-02-10 14:26:29 +04:00
aesmc v1 . 1 6 b , v1 . 1 6 b
bpl 1 b
aese v0 . 1 6 b , v4 . 1 6 b
aese v1 . 1 6 b , v4 . 1 6 b
/* final round key cancels out */
eor v0 . 1 6 b , v0 . 1 6 b , v1 . 1 6 b / * e n - / d e c r y p t t h e m a c * /
2016-10-11 21:15:17 +03:00
st1 { v0 . 1 6 b } , [ x0 ] / * s t o r e r e s u l t * /
2014-02-10 14:26:29 +04:00
ret
ENDPROC( c e _ a e s _ c c m _ f i n a l )
.macro aes_ c c m _ d o _ c r y p t ,e n c
2018-07-29 17:52:30 +03:00
ldr x8 , [ x6 , #8 ] / * l o a d l o w e r c t r * /
ld1 { v0 . 1 6 b } , [ x5 ] / * l o a d m a c * /
CPU_ L E ( r e v x8 , x8 ) / * k e e p s w a b b e d c t r i n r e g * /
2014-02-10 14:26:29 +04:00
0 : /* outer loop */
2018-07-29 17:52:30 +03:00
ld1 { v1 . 8 b } , [ x6 ] / * l o a d u p p e r c t r * /
prfm p l d l 1 s t r m , [ x1 ]
add x8 , x8 , #1
rev x9 , x8
cmp w4 , #12 / * w h i c h k e y s i z e ? * /
sub w7 , w4 , #2 / * g e t m o d i f i e d # o f r o u n d s * /
2014-02-10 14:26:29 +04:00
ins v1 . d [ 1 ] , x9 / * n o c a r r y i n l o w e r c t r * /
2018-07-29 17:52:30 +03:00
ld1 { v3 . 4 s } , [ x3 ] / * l o a d f i r s t r o u n d k e y * /
add x10 , x3 , #16
2014-02-10 14:26:29 +04:00
bmi 1 f
bne 4 f
mov v5 . 1 6 b , v3 . 1 6 b
b 3 f
1 : mov v4 . 1 6 b , v3 . 1 6 b
2017-07-24 13:28:10 +03:00
ld1 { v5 . 4 s } , [ x10 ] , #16 / * l o a d 2 n d r o u n d k e y * /
2014-02-10 14:26:29 +04:00
2 : /* inner loop: 3 rounds, 2x interleaved */
aese v0 . 1 6 b , v4 . 1 6 b
aesmc v0 . 1 6 b , v0 . 1 6 b
2015-03-17 21:05:13 +03:00
aese v1 . 1 6 b , v4 . 1 6 b
2014-02-10 14:26:29 +04:00
aesmc v1 . 1 6 b , v1 . 1 6 b
2017-07-24 13:28:10 +03:00
3 : ld1 { v3 . 4 s } , [ x10 ] , #16 / * l o a d n e x t r o u n d k e y * /
2014-02-10 14:26:29 +04:00
aese v0 . 1 6 b , v5 . 1 6 b
aesmc v0 . 1 6 b , v0 . 1 6 b
2015-03-17 21:05:13 +03:00
aese v1 . 1 6 b , v5 . 1 6 b
2014-02-10 14:26:29 +04:00
aesmc v1 . 1 6 b , v1 . 1 6 b
2017-07-24 13:28:10 +03:00
4 : ld1 { v4 . 4 s } , [ x10 ] , #16 / * l o a d n e x t r o u n d k e y * /
2014-02-10 14:26:29 +04:00
subs w7 , w7 , #3
aese v0 . 1 6 b , v3 . 1 6 b
aesmc v0 . 1 6 b , v0 . 1 6 b
2015-03-17 21:05:13 +03:00
aese v1 . 1 6 b , v3 . 1 6 b
2014-02-10 14:26:29 +04:00
aesmc v1 . 1 6 b , v1 . 1 6 b
2017-07-24 13:28:10 +03:00
ld1 { v5 . 4 s } , [ x10 ] , #16 / * l o a d n e x t r o u n d k e y * /
2014-02-10 14:26:29 +04:00
bpl 2 b
aese v0 . 1 6 b , v4 . 1 6 b
aese v1 . 1 6 b , v4 . 1 6 b
2018-07-29 17:52:30 +03:00
subs w2 , w2 , #16
bmi 6 f / * p a r t i a l b l o c k ? * /
ld1 { v2 . 1 6 b } , [ x1 ] , #16 / * l o a d n e x t i n p u t b l o c k * /
2014-02-10 14:26:29 +04:00
.if \ enc = = 1
eor v2 . 1 6 b , v2 . 1 6 b , v5 . 1 6 b / * f i n a l r o u n d e n c + m a c * /
eor v1 . 1 6 b , v1 . 1 6 b , v2 . 1 6 b / * x o r w i t h c r y p t e d c t r * /
.else
eor v2 . 1 6 b , v2 . 1 6 b , v1 . 1 6 b / * x o r w i t h c r y p t e d c t r * /
eor v1 . 1 6 b , v2 . 1 6 b , v5 . 1 6 b / * f i n a l r o u n d e n c * /
.endif
eor v0 . 1 6 b , v0 . 1 6 b , v2 . 1 6 b / * x o r m a c w i t h p t ^ r k [ l a s t ] * /
2018-07-29 17:52:30 +03:00
st1 { v1 . 1 6 b } , [ x0 ] , #16 / * w r i t e o u t p u t b l o c k * /
bne 0 b
CPU_ L E ( r e v x8 , x8 )
st1 { v0 . 1 6 b } , [ x5 ] / * s t o r e m a c * /
str x8 , [ x6 , #8 ] / * s t o r e l s b e n d o f c t r ( B E ) * /
5 : ret
6 : eor v0 . 1 6 b , v0 . 1 6 b , v5 . 1 6 b / * f i n a l r o u n d m a c * /
2014-02-10 14:26:29 +04:00
eor v1 . 1 6 b , v1 . 1 6 b , v5 . 1 6 b / * f i n a l r o u n d e n c * /
2018-07-29 17:52:30 +03:00
st1 { v0 . 1 6 b } , [ x5 ] / * s t o r e m a c * /
add w2 , w2 , #16 / * p r o c e s s p a r t i a l t a i l b l o c k * /
7 : ldrb w9 , [ x1 ] , #1 / * g e t 1 b y t e o f i n p u t * /
2014-02-10 14:26:29 +04:00
umov w6 , v1 . b [ 0 ] / * g e t t o p c r y p t e d c t r b y t e * /
umov w7 , v0 . b [ 0 ] / * g e t t o p m a c b y t e * /
.if \ enc = = 1
eor w7 , w7 , w9
eor w9 , w9 , w6
.else
eor w9 , w9 , w6
eor w7 , w7 , w9
.endif
2018-07-29 17:52:30 +03:00
strb w9 , [ x0 ] , #1 / * s t o r e o u t b y t e * /
strb w7 , [ x5 ] , #1 / * s t o r e m a c b y t e * /
subs w2 , w2 , #1
beq 5 b
2014-02-10 14:26:29 +04:00
ext v0 . 1 6 b , v0 . 1 6 b , v0 . 1 6 b , #1 / * s h i f t o u t m a c b y t e * /
ext v1 . 1 6 b , v1 . 1 6 b , v1 . 1 6 b , #1 / * s h i f t o u t c t r b y t e * /
2018-07-29 17:52:30 +03:00
b 7 b
2014-02-10 14:26:29 +04:00
.endm
/ *
* void c e _ a e s _ c c m _ e n c r y p t ( u 8 o u t [ ] , u 8 c o n s t i n [ ] , u 3 2 c b y t e s ,
* u8 c o n s t r k [ ] , u 3 2 r o u n d s , u 8 m a c [ ] ,
* u8 c t r [ ] ) ;
* void c e _ a e s _ c c m _ d e c r y p t ( u 8 o u t [ ] , u 8 c o n s t i n [ ] , u 3 2 c b y t e s ,
* u8 c o n s t r k [ ] , u 3 2 r o u n d s , u 8 m a c [ ] ,
* u8 c t r [ ] ) ;
* /
ENTRY( c e _ a e s _ c c m _ e n c r y p t )
aes_ c c m _ d o _ c r y p t 1
ENDPROC( c e _ a e s _ c c m _ e n c r y p t )
ENTRY( c e _ a e s _ c c m _ d e c r y p t )
aes_ c c m _ d o _ c r y p t 0
ENDPROC( c e _ a e s _ c c m _ d e c r y p t )