2005-04-16 15:20:36 -07:00
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/ / Copyright ( c ) 2 0 0 1 , D r B r i a n G l a d m a n < > , W o r c e s t e r , U K .
/ / All r i g h t s r e s e r v e d .
/ /
/ / LICENSE T E R M S
/ /
/ / The f r e e d i s t r i b u t i o n a n d u s e o f t h i s s o f t w a r e i n b o t h s o u r c e a n d b i n a r y
/ / form i s a l l o w e d ( w i t h o r w i t h o u t c h a n g e s ) p r o v i d e d t h a t :
/ /
/ / 1 . distributions o f t h i s s o u r c e c o d e i n c l u d e t h e a b o v e c o p y r i g h t
/ / notice, t h i s l i s t o f c o n d i t i o n s a n d t h e f o l l o w i n g d i s c l a i m e r / /
/ /
/ / 2 . distributions i n b i n a r y f o r m i n c l u d e t h e a b o v e c o p y r i g h t
/ / notice, t h i s l i s t o f c o n d i t i o n s a n d t h e f o l l o w i n g d i s c l a i m e r
/ / in t h e d o c u m e n t a t i o n a n d / o r o t h e r a s s o c i a t e d m a t e r i a l s / /
/ /
/ / 3 . the c o p y r i g h t h o l d e r ' s n a m e i s n o t u s e d t o e n d o r s e p r o d u c t s
/ / built u s i n g t h i s s o f t w a r e w i t h o u t s p e c i f i c w r i t t e n p e r m i s s i o n .
/ /
/ /
/ / ALTERNATIVELY, p r o v i d e d t h a t t h i s n o t i c e i s r e t a i n e d i n f u l l , t h i s p r o d u c t
/ / may b e d i s t r i b u t e d 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 ( G P L ) ,
/ / in w h i c h c a s e t h e p r o v i s i o n s o f t h e G P L a p p l y I N S T E A D O F t h o s e g i v e n a b o v e .
/ /
/ / Copyright ( c ) 2 0 0 4 L i n u s T o r v a l d s < t o r v a l d s @osdl.org>
/ / Copyright ( c ) 2 0 0 4 R e d H a t , I n c . , J a m e s M o r r i s < j m o r r i s @redhat.com>
/ / DISCLAIMER
/ /
/ / This s o f t w a r e i s p r o v i d e d ' a s i s ' w i t h n o e x p l i c i t o r i m p l i e d w a r r a n t i e s
/ / in r e s p e c t o f i t s p r o p e r t i e s i n c l u d i n g , b u t n o t l i m i t e d t o , c o r r e c t n e s s
/ / and f i t n e s s f o r p u r p o s e .
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/ / Issue D a t e : 2 9 / 0 7 / 2 0 0 2
.file " aes- i 5 8 6 - a s m . S "
.text
2006-05-16 22:09:29 +10:00
# include < a s m / a s m - o f f s e t s . h >
2005-04-16 15:20:36 -07:00
2006-05-16 22:09:29 +10:00
# define t l e n 1 0 2 4 / / l e n g t h o f e a c h o f 4 ' x o r ' a r r a y s ( 2 5 6 3 2 - b i t w o r d s )
2005-04-16 15:20:36 -07:00
2006-05-16 22:09:29 +10:00
/* offsets to parameters with one register pushed onto stack */
# define t f m 8
# define o u t _ b l k 1 2
# define i n _ b l k 1 6
2005-04-16 15:20:36 -07:00
2006-05-16 22:09:29 +10:00
/* offsets in crypto_tfm structure */
2007-11-10 19:07:16 +08:00
# define k l e n ( c r y p t o _ t f m _ c t x _ o f f s e t + 0 )
# define e k e y ( c r y p t o _ t f m _ c t x _ o f f s e t + 4 )
# define d k e y ( c r y p t o _ t f m _ c t x _ o f f s e t + 2 4 4 )
2005-04-16 15:20:36 -07:00
/ / register m a p p i n g f o r e n c r y p t a n d d e c r y p t s u b r o u t i n e s
# define r0 e a x
# define r1 e b x
# define r2 e c x
# define r3 e d x
# define r4 e s i
# define r5 e d i
# define e a x l a l
# define e a x h a h
# define e b x l b l
# define e b x h b h
# define e c x l c l
# define e c x h c h
# define e d x l d l
# define e d x h d h
# define _ h ( r e g ) r e g ## h
# define h ( r e g ) _ h ( r e g )
# define _ l ( r e g ) r e g ## l
# define l ( r e g ) _ l ( r e g )
/ / This m a c r o t a k e s a 3 2 - b i t w o r d r e p r e s e n t i n g a c o l u m n a n d u s e s
/ / each o f i t s f o u r b y t e s t o i n d e x i n t o f o u r t a b l e s o f 2 5 6 3 2 - b i t
/ / words t o o b t a i n v a l u e s t h a t a r e t h e n x o r e d i n t o t h e a p p r o p r i a t e
/ / output r e g i s t e r s r0 , r1 , r4 o r r5 .
/ / Parameters :
/ / table t a b l e b a s e a d d r e s s
/ / % 1 out_ s t a t e [ 0 ]
/ / % 2 out_ s t a t e [ 1 ]
/ / % 3 out_ s t a t e [ 2 ]
/ / % 4 out_ s t a t e [ 3 ]
/ / idx i n p u t r e g i s t e r f o r t h e r o u n d ( d e s t r o y e d )
/ / tmp s c r a t c h r e g i s t e r f o r t h e r o u n d
/ / sched k e y s c h e d u l e
# define d o _ c o l ( t a b l e , a1 ,a2 ,a3 ,a4 , i d x , t m p ) \
movzx % l ( i d x ) ,% t m p ; \
xor t a b l e ( ,% t m p ,4 ) ,% a1 ; \
movzx % h ( i d x ) ,% t m p ; \
shr $ 1 6 ,% i d x ; \
xor t a b l e + t l e n ( ,% t m p ,4 ) ,% a2 ; \
movzx % l ( i d x ) ,% t m p ; \
movzx % h ( i d x ) ,% i d x ; \
xor t a b l e + 2 * t l e n ( ,% t m p ,4 ) ,% a3 ; \
xor t a b l e + 3 * t l e n ( ,% i d x ,4 ) ,% a4 ;
/ / initialise o u t p u t r e g i s t e r s f r o m t h e k e y s c h e d u l e
/ / NB1 : original v a l u e o f a3 i s i n i d x o n e x i t
/ / NB2 : original v a l u e s o f a1 ,a2 ,a4 a r e n ' t u s e d
# define d o _ f c o l ( t a b l e , a1 ,a2 ,a3 ,a4 , i d x , t m p , s c h e d ) \
mov 0 s c h e d ,% a1 ; \
movzx % l ( i d x ) ,% t m p ; \
mov 1 2 s c h e d ,% a2 ; \
xor t a b l e ( ,% t m p ,4 ) ,% a1 ; \
mov 4 s c h e d ,% a4 ; \
movzx % h ( i d x ) ,% t m p ; \
shr $ 1 6 ,% i d x ; \
xor t a b l e + t l e n ( ,% t m p ,4 ) ,% a2 ; \
movzx % l ( i d x ) ,% t m p ; \
movzx % h ( i d x ) ,% i d x ; \
xor t a b l e + 3 * t l e n ( ,% i d x ,4 ) ,% a4 ; \
mov % a3 ,% i d x ; \
mov 8 s c h e d ,% a3 ; \
xor t a b l e + 2 * t l e n ( ,% t m p ,4 ) ,% a3 ;
/ / initialise o u t p u t r e g i s t e r s f r o m t h e k e y s c h e d u l e
/ / NB1 : original v a l u e o f a3 i s i n i d x o n e x i t
/ / NB2 : original v a l u e s o f a1 ,a2 ,a4 a r e n ' t u s e d
# define d o _ i c o l ( t a b l e , a1 ,a2 ,a3 ,a4 , i d x , t m p , s c h e d ) \
mov 0 s c h e d ,% a1 ; \
movzx % l ( i d x ) ,% t m p ; \
mov 4 s c h e d ,% a2 ; \
xor t a b l e ( ,% t m p ,4 ) ,% a1 ; \
mov 1 2 s c h e d ,% a4 ; \
movzx % h ( i d x ) ,% t m p ; \
shr $ 1 6 ,% i d x ; \
xor t a b l e + t l e n ( ,% t m p ,4 ) ,% a2 ; \
movzx % l ( i d x ) ,% t m p ; \
movzx % h ( i d x ) ,% i d x ; \
xor t a b l e + 3 * t l e n ( ,% i d x ,4 ) ,% a4 ; \
mov % a3 ,% i d x ; \
mov 8 s c h e d ,% a3 ; \
xor t a b l e + 2 * t l e n ( ,% t m p ,4 ) ,% a3 ;
/ / original G l a d m a n h a d c o n d i t i o n a l s a v e s t o M M X r e g s .
# define s a v e ( a1 , a2 ) \
mov % a2 ,4 * a1 ( % e s p )
# define r e s t o r e ( a1 , a2 ) \
mov 4 * a2 ( % e s p ) ,% a1
/ / These m a c r o s p e r f o r m a f o r w a r d e n c r y p t i o n c y c l e . T h e y a r e e n t e r e d w i t h
/ / the f i r s t p r e v i o u s r o u n d c o l u m n v a l u e s i n r0 ,r1 ,r4 ,r5 a n d
/ / exit w i t h t h e f i n a l v a l u e s i n t h e s a m e r e g i s t e r s , u s i n g s t a c k
/ / for t e m p o r a r y s t o r a g e .
/ / round c o l u m n v a l u e s
/ / on e n t r y : r0 ,r1 ,r4 ,r5
/ / on e x i t : r2 ,r1 ,r4 ,r5
# define f w d _ r n d1 ( a r g , t a b l e ) \
save ( 0 ,r1 ) ; \
save ( 1 ,r5 ) ; \
\
/* compute new column values */ \
do_ f c o l ( t a b l e , r2 ,r5 ,r4 ,r1 , r0 ,r3 , a r g ) ; /* idx=r0 */ \
do_ c o l ( t a b l e , r4 ,r1 ,r2 ,r5 , r0 ,r3 ) ; /* idx=r4 */ \
restore( r0 ,0 ) ; \
do_ c o l ( t a b l e , r1 ,r2 ,r5 ,r4 , r0 ,r3 ) ; /* idx=r1 */ \
restore( r0 ,1 ) ; \
do_ c o l ( t a b l e , r5 ,r4 ,r1 ,r2 , r0 ,r3 ) ; /* idx=r5 */
/ / round c o l u m n v a l u e s
/ / on e n t r y : r2 ,r1 ,r4 ,r5
/ / on e x i t : r0 ,r1 ,r4 ,r5
# define f w d _ r n d2 ( a r g , t a b l e ) \
save ( 0 ,r1 ) ; \
save ( 1 ,r5 ) ; \
\
/* compute new column values */ \
do_ f c o l ( t a b l e , r0 ,r5 ,r4 ,r1 , r2 ,r3 , a r g ) ; /* idx=r2 */ \
do_ c o l ( t a b l e , r4 ,r1 ,r0 ,r5 , r2 ,r3 ) ; /* idx=r4 */ \
restore( r2 ,0 ) ; \
do_ c o l ( t a b l e , r1 ,r0 ,r5 ,r4 , r2 ,r3 ) ; /* idx=r1 */ \
restore( r2 ,1 ) ; \
do_ c o l ( t a b l e , r5 ,r4 ,r1 ,r0 , r2 ,r3 ) ; /* idx=r5 */
/ / These m a c r o s p e r f o r m s a n i n v e r s e e n c r y p t i o n c y c l e . T h e y a r e e n t e r e d w i t h
/ / the f i r s t p r e v i o u s r o u n d c o l u m n v a l u e s i n r0 ,r1 ,r4 ,r5 a n d
/ / exit w i t h t h e f i n a l v a l u e s i n t h e s a m e r e g i s t e r s , u s i n g s t a c k
/ / for t e m p o r a r y s t o r a g e
/ / round c o l u m n v a l u e s
/ / on e n t r y : r0 ,r1 ,r4 ,r5
/ / on e x i t : r2 ,r1 ,r4 ,r5
# define i n v _ r n d1 ( a r g , t a b l e ) \
save ( 0 ,r1 ) ; \
save ( 1 ,r5 ) ; \
\
/* compute new column values */ \
do_ i c o l ( t a b l e , r2 ,r1 ,r4 ,r5 , r0 ,r3 , a r g ) ; /* idx=r0 */ \
do_ c o l ( t a b l e , r4 ,r5 ,r2 ,r1 , r0 ,r3 ) ; /* idx=r4 */ \
restore( r0 ,0 ) ; \
do_ c o l ( t a b l e , r1 ,r4 ,r5 ,r2 , r0 ,r3 ) ; /* idx=r1 */ \
restore( r0 ,1 ) ; \
do_ c o l ( t a b l e , r5 ,r2 ,r1 ,r4 , r0 ,r3 ) ; /* idx=r5 */
/ / round c o l u m n v a l u e s
/ / on e n t r y : r2 ,r1 ,r4 ,r5
/ / on e x i t : r0 ,r1 ,r4 ,r5
# define i n v _ r n d2 ( a r g , t a b l e ) \
save ( 0 ,r1 ) ; \
save ( 1 ,r5 ) ; \
\
/* compute new column values */ \
do_ i c o l ( t a b l e , r0 ,r1 ,r4 ,r5 , r2 ,r3 , a r g ) ; /* idx=r2 */ \
do_ c o l ( t a b l e , r4 ,r5 ,r0 ,r1 , r2 ,r3 ) ; /* idx=r4 */ \
restore( r2 ,0 ) ; \
do_ c o l ( t a b l e , r1 ,r4 ,r5 ,r0 , r2 ,r3 ) ; /* idx=r1 */ \
restore( r2 ,1 ) ; \
do_ c o l ( t a b l e , r5 ,r0 ,r1 ,r4 , r2 ,r3 ) ; /* idx=r5 */
/ / AES ( R i j n d a e l ) E n c r y p t i o n S u b r o u t i n e
2006-05-16 22:09:29 +10:00
/* void aes_enc_blk(struct crypto_tfm *tfm, u8 *out_blk, const u8 *in_blk) */
2005-04-16 15:20:36 -07:00
.global aes_enc_blk
2007-11-10 19:07:16 +08:00
.extern crypto_ft_tab
.extern crypto_fl_tab
2005-04-16 15:20:36 -07:00
.align 4
aes_enc_blk :
push % e b p
2006-05-16 22:09:29 +10:00
mov t f m ( % e s p ) ,% e b p
2005-04-16 15:20:36 -07:00
/ / CAUTION : the o r d e r a n d t h e v a l u e s u s e d i n t h e s e a s s i g n s
/ / rely o n t h e r e g i s t e r m a p p i n g s
1 : push % e b x
mov i n _ b l k + 4 ( % e s p ) ,% r2
push % e s i
2007-11-10 19:07:16 +08:00
mov k l e n ( % e b p ) ,% r3 / / k e y s i z e
2005-04-16 15:20:36 -07:00
push % e d i
# if e k e y ! = 0
lea e k e y ( % e b p ) ,% e b p / / k e y p o i n t e r
# endif
/ / input f o u r c o l u m n s a n d x o r i n f i r s t r o u n d k e y
mov ( % r2 ) ,% r0
mov 4 ( % r2 ) ,% r1
mov 8 ( % r2 ) ,% r4
mov 1 2 ( % r2 ) ,% r5
xor ( % e b p ) ,% r0
xor 4 ( % e b p ) ,% r1
xor 8 ( % e b p ) ,% r4
xor 1 2 ( % e b p ) ,% r5
2005-11-29 22:23:20 +11:00
sub $ 8 ,% e s p / / s p a c e f o r r e g i s t e r s a v e s o n s t a c k
add $ 1 6 ,% e b p / / i n c r e m e n t t o n e x t r o u n d k e y
2007-11-10 19:07:16 +08:00
cmp $ 2 4 ,% r3
2005-11-29 22:23:20 +11:00
jb 4 f / / 1 0 r o u n d s f o r 1 2 8 - b i t k e y
lea 3 2 ( % e b p ) ,% e b p
je 3 f / / 1 2 r o u n d s f o r 1 9 2 - b i t k e y
lea 3 2 ( % e b p ) ,% e b p
2007-11-10 19:07:16 +08:00
2 : fwd_ r n d1 ( - 6 4 ( % e b p ) , c r y p t o _ f t _ t a b ) / / 1 4 r o u n d s f o r 2 5 6 - b i t k e y
fwd_ r n d2 ( - 4 8 ( % e b p ) , c r y p t o _ f t _ t a b )
3 : fwd_ r n d1 ( - 3 2 ( % e b p ) , c r y p t o _ f t _ t a b ) / / 1 2 r o u n d s f o r 1 9 2 - b i t k e y
fwd_ r n d2 ( - 1 6 ( % e b p ) , c r y p t o _ f t _ t a b )
4 : fwd_ r n d1 ( ( % e b p ) , c r y p t o _ f t _ t a b ) / / 1 0 r o u n d s f o r 1 2 8 - b i t k e y
fwd_ r n d2 ( + 1 6 ( % e b p ) , c r y p t o _ f t _ t a b )
fwd_ r n d1 ( + 3 2 ( % e b p ) , c r y p t o _ f t _ t a b )
fwd_ r n d2 ( + 4 8 ( % e b p ) , c r y p t o _ f t _ t a b )
fwd_ r n d1 ( + 6 4 ( % e b p ) , c r y p t o _ f t _ t a b )
fwd_ r n d2 ( + 8 0 ( % e b p ) , c r y p t o _ f t _ t a b )
fwd_ r n d1 ( + 9 6 ( % e b p ) , c r y p t o _ f t _ t a b )
fwd_ r n d2 ( + 1 1 2 ( % e b p ) , c r y p t o _ f t _ t a b )
fwd_ r n d1 ( + 1 2 8 ( % e b p ) , c r y p t o _ f t _ t a b )
fwd_ r n d2 ( + 1 4 4 ( % e b p ) , c r y p t o _ f l _ t a b ) / / l a s t r o u n d u s e s a d i f f e r e n t t a b l e
2005-04-16 15:20:36 -07:00
/ / move f i n a l v a l u e s t o t h e o u t p u t a r r a y . C A U T I O N : t h e
/ / order o f t h e s e a s s i g n s r e l y o n t h e r e g i s t e r m a p p i n g s
add $ 8 ,% e s p
mov o u t _ b l k + 1 2 ( % e s p ) ,% e b p
mov % r5 ,1 2 ( % e b p )
pop % e d i
mov % r4 ,8 ( % e b p )
pop % e s i
mov % r1 ,4 ( % e b p )
pop % e b x
mov % r0 ,( % e b p )
pop % e b p
mov $ 1 ,% e a x
ret
/ / AES ( R i j n d a e l ) D e c r y p t i o n S u b r o u t i n e
2006-05-16 22:09:29 +10:00
/* void aes_dec_blk(struct crypto_tfm *tfm, u8 *out_blk, const u8 *in_blk) */
2005-04-16 15:20:36 -07:00
.global aes_dec_blk
2007-11-10 19:07:16 +08:00
.extern crypto_it_tab
.extern crypto_il_tab
2005-04-16 15:20:36 -07:00
.align 4
aes_dec_blk :
push % e b p
2006-05-16 22:09:29 +10:00
mov t f m ( % e s p ) ,% e b p
2005-04-16 15:20:36 -07:00
/ / CAUTION : the o r d e r a n d t h e v a l u e s u s e d i n t h e s e a s s i g n s
/ / rely o n t h e r e g i s t e r m a p p i n g s
1 : push % e b x
mov i n _ b l k + 4 ( % e s p ) ,% r2
push % e s i
2007-11-10 19:07:16 +08:00
mov k l e n ( % e b p ) ,% r3 / / k e y s i z e
2005-04-16 15:20:36 -07:00
push % e d i
# if d k e y ! = 0
lea d k e y ( % e b p ) ,% e b p / / k e y p o i n t e r
# endif
/ / input f o u r c o l u m n s a n d x o r i n f i r s t r o u n d k e y
mov ( % r2 ) ,% r0
mov 4 ( % r2 ) ,% r1
mov 8 ( % r2 ) ,% r4
mov 1 2 ( % r2 ) ,% r5
xor ( % e b p ) ,% r0
xor 4 ( % e b p ) ,% r1
xor 8 ( % e b p ) ,% r4
xor 1 2 ( % e b p ) ,% r5
2005-11-29 22:23:20 +11:00
sub $ 8 ,% e s p / / s p a c e f o r r e g i s t e r s a v e s o n s t a c k
2007-11-10 19:07:16 +08:00
add $ 1 6 ,% e b p / / i n c r e m e n t t o n e x t r o u n d k e y
cmp $ 2 4 ,% r3
2005-11-29 22:23:20 +11:00
jb 4 f / / 1 0 r o u n d s f o r 1 2 8 - b i t k e y
2007-11-10 19:07:16 +08:00
lea 3 2 ( % e b p ) ,% e b p
2005-11-29 22:23:20 +11:00
je 3 f / / 1 2 r o u n d s f o r 1 9 2 - b i t k e y
2007-11-10 19:07:16 +08:00
lea 3 2 ( % e b p ) ,% e b p
2 : inv_ r n d1 ( - 6 4 ( % e b p ) , c r y p t o _ i t _ t a b ) / / 1 4 r o u n d s f o r 2 5 6 - b i t k e y
inv_ r n d2 ( - 4 8 ( % e b p ) , c r y p t o _ i t _ t a b )
3 : inv_ r n d1 ( - 3 2 ( % e b p ) , c r y p t o _ i t _ t a b ) / / 1 2 r o u n d s f o r 1 9 2 - b i t k e y
inv_ r n d2 ( - 1 6 ( % e b p ) , c r y p t o _ i t _ t a b )
4 : inv_ r n d1 ( ( % e b p ) , c r y p t o _ i t _ t a b ) / / 1 0 r o u n d s f o r 1 2 8 - b i t k e y
inv_ r n d2 ( + 1 6 ( % e b p ) , c r y p t o _ i t _ t a b )
inv_ r n d1 ( + 3 2 ( % e b p ) , c r y p t o _ i t _ t a b )
inv_ r n d2 ( + 4 8 ( % e b p ) , c r y p t o _ i t _ t a b )
inv_ r n d1 ( + 6 4 ( % e b p ) , c r y p t o _ i t _ t a b )
inv_ r n d2 ( + 8 0 ( % e b p ) , c r y p t o _ i t _ t a b )
inv_ r n d1 ( + 9 6 ( % e b p ) , c r y p t o _ i t _ t a b )
inv_ r n d2 ( + 1 1 2 ( % e b p ) , c r y p t o _ i t _ t a b )
inv_ r n d1 ( + 1 2 8 ( % e b p ) , c r y p t o _ i t _ t a b )
inv_ r n d2 ( + 1 4 4 ( % e b p ) , c r y p t o _ i l _ t a b ) / / l a s t r o u n d u s e s a d i f f e r e n t t a b l e
2005-04-16 15:20:36 -07:00
/ / move f i n a l v a l u e s t o t h e o u t p u t a r r a y . C A U T I O N : t h e
/ / order o f t h e s e a s s i g n s r e l y o n t h e r e g i s t e r m a p p i n g s
add $ 8 ,% e s p
mov o u t _ b l k + 1 2 ( % e s p ) ,% e b p
mov % r5 ,1 2 ( % e b p )
pop % e d i
mov % r4 ,8 ( % e b p )
pop % e s i
mov % r1 ,4 ( % e b p )
pop % e b x
mov % r0 ,( % e b p )
pop % e b p
mov $ 1 ,% e a x
ret