2005-07-06 13:55:00 -07:00
/ * AES ( R i j n d a e l ) i m p l e m e n t a t i o n ( F I P S P U B 1 9 7 ) f o r x86 _ 6 4
*
* Copyright ( C ) 2 0 0 5 A n d r e a s S t e i n m e t z , < a s t @domdv.de>
*
* License :
* This c o d e c a n 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
* License ( G P L ) V e r s i o n 2 p r o v i d e d t h a t t h e a b o v e h e a d e r d o w n t o a n d
* including t h i s s e n t e n c e i s r e t a i n e d i n f u l l .
* /
2007-11-08 21:25:04 +08:00
.extern crypto_ft_tab
.extern crypto_it_tab
.extern crypto_fl_tab
.extern crypto_il_tab
2005-07-06 13:55:00 -07:00
.text
2013-01-19 13:38:50 +02:00
# include < l i n u x / l i n k a g e . h >
2006-05-16 22:09:29 +10:00
# include < a s m / a s m - o f f s e t s . h >
2005-07-06 13:55:00 -07:00
# define R 1 % r a x
# define R 1 E % e a x
# define R 1 X % a x
# define R 1 H % a h
# define R 1 L % a l
# define R 2 % r b x
# define R 2 E % e b x
# define R 2 X % b x
# define R 2 H % b h
# define R 2 L % b l
# define R 3 % r c x
# define R 3 E % e c x
# define R 3 X % c x
# define R 3 H % c h
# define R 3 L % c l
# define R 4 % r d x
# define R 4 E % e d x
# define R 4 X % d x
# define R 4 H % d h
# define R 4 L % d l
# define R 5 % r s i
# define R 5 E % e s i
# define R 6 % r d i
# define R 6 E % e d i
2017-05-16 21:03:08 -07:00
# define R 7 % r9 / * d o n ' t u s e % r b p ; it breaks stack traces */
# define R 7 E % r9 d
2005-07-06 13:55:00 -07:00
# define R 8 % r8
# define R 1 0 % r10
# define R 1 1 % r11
2017-05-16 21:03:08 -07:00
# define p r o l o g u e ( F U N C ,K E Y ,B 1 2 8 ,B 1 9 2 ,r1 ,r2 ,r5 ,r6 ,r7 ,r8 ,r9 ,r10 ,r11 ) \
2013-01-19 13:38:50 +02:00
ENTRY( F U N C ) ; \
movq r1 ,r2 ; \
2009-01-09 17:25:50 +11:00
leaq K E Y + 4 8 ( r8 ) ,r9 ; \
2005-07-06 13:55:00 -07:00
movq r10 ,r11 ; \
movl ( r7 ) ,r5 ## E ; \
movl 4 ( r7 ) ,r1 ## E ; \
movl 8 ( r7 ) ,r6 ## E ; \
movl 1 2 ( r7 ) ,r7 ## E ; \
2009-01-09 17:25:50 +11:00
movl 4 8 0 ( r8 ) ,r10 ## E ; \
2005-07-06 13:55:00 -07:00
xorl - 4 8 ( r9 ) ,r5 ## E ; \
xorl - 4 4 ( r9 ) ,r1 ## E ; \
xorl - 4 0 ( r9 ) ,r6 ## E ; \
xorl - 3 6 ( r9 ) ,r7 ## E ; \
cmpl $ 2 4 ,r10 ## E ; \
jb B 1 2 8 ; \
leaq 3 2 ( r9 ) ,r9 ; \
je B 1 9 2 ; \
leaq 3 2 ( r9 ) ,r9 ;
2017-05-16 21:03:08 -07:00
# define e p i l o g u e ( F U N C ,r1 ,r2 ,r5 ,r6 ,r7 ,r8 ,r9 ) \
2005-07-06 13:55:00 -07:00
movq r1 ,r2 ; \
movl r5 ## E , ( r 9 ) ; \
movl r6 ## E , 4 ( r9 ) ; \
movl r7 ## E , 8 ( r9 ) ; \
movl r8 ## E , 12 ( r9 ) ; \
2013-01-19 13:38:50 +02:00
ret; \
ENDPROC( F U N C ) ;
2005-07-06 13:55:00 -07:00
# define r o u n d ( T A B ,O F F S E T ,r1 ,r2 ,r3 ,r4 ,r5 ,r6 ,r7 ,r8 ,r a ,r b ,r c ,r d ) \
movzbl r2 ## H , r 5 ## E ; \
movzbl r2 ## L , r 6 ## E ; \
movl T A B + 1 0 2 4 ( ,r5 ,4 ) ,r5 ## E ; \
movw r4 ## X , r 2 ## X ; \
movl T A B ( ,r6 ,4 ) ,r6 ## E ; \
roll $ 1 6 ,r2 ## E ; \
shrl $ 1 6 ,r4 ## E ; \
2017-05-16 21:03:08 -07:00
movzbl r4 ## L , r 7 ## E ; \
movzbl r4 ## H , r 4 ## E ; \
2005-07-06 13:55:00 -07:00
xorl O F F S E T ( r8 ) ,r a ## E ; \
xorl O F F S E T + 4 ( r8 ) ,r b ## E ; \
2017-05-16 21:03:08 -07:00
xorl T A B + 3 0 7 2 ( ,r4 ,4 ) ,r5 ## E ; \
xorl T A B + 2 0 4 8 ( ,r7 ,4 ) ,r6 ## E ; \
2005-07-06 13:55:00 -07:00
movzbl r1 ## L , r 7 ## E ; \
movzbl r1 ## H , r 4 ## E ; \
movl T A B + 1 0 2 4 ( ,r4 ,4 ) ,r4 ## E ; \
movw r3 ## X , r 1 ## X ; \
roll $ 1 6 ,r1 ## E ; \
shrl $ 1 6 ,r3 ## E ; \
xorl T A B ( ,r7 ,4 ) ,r5 ## E ; \
2017-05-16 21:03:08 -07:00
movzbl r3 ## L , r 7 ## E ; \
movzbl r3 ## H , r 3 ## E ; \
xorl T A B + 3 0 7 2 ( ,r3 ,4 ) ,r4 ## E ; \
xorl T A B + 2 0 4 8 ( ,r7 ,4 ) ,r5 ## E ; \
movzbl r1 ## L , r 7 ## E ; \
movzbl r1 ## H , r 3 ## E ; \
2005-07-06 13:55:00 -07:00
shrl $ 1 6 ,r1 ## E ; \
2017-05-16 21:03:08 -07:00
xorl T A B + 3 0 7 2 ( ,r3 ,4 ) ,r6 ## E ; \
movl T A B + 2 0 4 8 ( ,r7 ,4 ) ,r3 ## E ; \
movzbl r1 ## L , r 7 ## E ; \
movzbl r1 ## H , r 1 ## E ; \
xorl T A B + 1 0 2 4 ( ,r1 ,4 ) ,r6 ## E ; \
xorl T A B ( ,r7 ,4 ) ,r3 ## E ; \
2005-07-06 13:55:00 -07:00
movzbl r2 ## H , r 1 ## E ; \
movzbl r2 ## L , r 7 ## E ; \
shrl $ 1 6 ,r2 ## E ; \
xorl T A B + 3 0 7 2 ( ,r1 ,4 ) ,r3 ## E ; \
xorl T A B + 2 0 4 8 ( ,r7 ,4 ) ,r4 ## E ; \
movzbl r2 ## H , r 1 ## E ; \
movzbl r2 ## L , r 2 ## E ; \
xorl O F F S E T + 8 ( r8 ) ,r c ## E ; \
xorl O F F S E T + 1 2 ( r8 ) ,r d ## E ; \
xorl T A B + 1 0 2 4 ( ,r1 ,4 ) ,r3 ## E ; \
xorl T A B ( ,r2 ,4 ) ,r4 ## E ;
# define m o v e _ r e g s ( r1 ,r2 ,r3 ,r4 ) \
movl r3 ## E , r 1 ## E ; \
movl r4 ## E , r 2 ## E ;
2006-05-16 22:09:29 +10:00
# define e n t r y ( F U N C ,K E Y ,B 1 2 8 ,B 1 9 2 ) \
2017-05-16 21:03:08 -07:00
prologue( F U N C ,K E Y ,B 1 2 8 ,B 1 9 2 ,R 2 ,R 8 ,R 1 ,R 3 ,R 4 ,R 6 ,R 1 0 ,R 5 ,R 1 1 )
2005-07-06 13:55:00 -07:00
2017-05-16 21:03:08 -07:00
# define r e t u r n ( F U N C ) e p i l o g u e ( F U N C ,R 8 ,R 2 ,R 5 ,R 6 ,R 3 ,R 4 ,R 1 1 )
2005-07-06 13:55:00 -07:00
# define e n c r y p t _ r o u n d ( T A B ,O F F S E T ) \
round( T A B ,O F F S E T ,R 1 ,R 2 ,R 3 ,R 4 ,R 5 ,R 6 ,R 7 ,R 1 0 ,R 5 ,R 6 ,R 3 ,R 4 ) \
move_ r e g s ( R 1 ,R 2 ,R 5 ,R 6 )
# define e n c r y p t _ f i n a l ( T A B ,O F F S E T ) \
round( T A B ,O F F S E T ,R 1 ,R 2 ,R 3 ,R 4 ,R 5 ,R 6 ,R 7 ,R 1 0 ,R 5 ,R 6 ,R 3 ,R 4 )
# define d e c r y p t _ r o u n d ( T A B ,O F F S E T ) \
round( T A B ,O F F S E T ,R 2 ,R 1 ,R 4 ,R 3 ,R 6 ,R 5 ,R 7 ,R 1 0 ,R 5 ,R 6 ,R 3 ,R 4 ) \
move_ r e g s ( R 1 ,R 2 ,R 5 ,R 6 )
# define d e c r y p t _ f i n a l ( T A B ,O F F S E T ) \
round( T A B ,O F F S E T ,R 2 ,R 1 ,R 4 ,R 3 ,R 6 ,R 5 ,R 7 ,R 1 0 ,R 5 ,R 6 ,R 3 ,R 4 )
2006-06-02 08:42:25 +10:00
/* void aes_enc_blk(stuct crypto_tfm *tfm, u8 *out, const u8 *in) */
2005-07-06 13:55:00 -07:00
2013-01-19 13:38:50 +02:00
entry( a e s _ e n c _ b l k ,0 ,. L e 1 2 8 ,. L e 1 9 2 )
2007-11-08 21:25:04 +08:00
encrypt_ r o u n d ( c r y p t o _ f t _ t a b ,- 9 6 )
encrypt_ r o u n d ( c r y p t o _ f t _ t a b ,- 8 0 )
2013-01-19 13:38:50 +02:00
.Le192 : encrypt_ r o u n d ( c r y p t o _ f t _ t a b ,- 6 4 )
2007-11-08 21:25:04 +08:00
encrypt_ r o u n d ( c r y p t o _ f t _ t a b ,- 4 8 )
2013-01-19 13:38:50 +02:00
.Le128 : encrypt_ r o u n d ( c r y p t o _ f t _ t a b ,- 3 2 )
2007-11-08 21:25:04 +08:00
encrypt_ r o u n d ( c r y p t o _ f t _ t a b ,- 1 6 )
encrypt_ r o u n d ( c r y p t o _ f t _ t a b , 0 )
encrypt_ r o u n d ( c r y p t o _ f t _ t a b , 1 6 )
encrypt_ r o u n d ( c r y p t o _ f t _ t a b , 3 2 )
encrypt_ r o u n d ( c r y p t o _ f t _ t a b , 4 8 )
encrypt_ r o u n d ( c r y p t o _ f t _ t a b , 6 4 )
encrypt_ r o u n d ( c r y p t o _ f t _ t a b , 8 0 )
encrypt_ r o u n d ( c r y p t o _ f t _ t a b , 9 6 )
encrypt_ f i n a l ( c r y p t o _ f l _ t a b ,1 1 2 )
2013-01-19 13:38:50 +02:00
return( a e s _ e n c _ b l k )
2005-07-06 13:55:00 -07:00
2006-06-02 08:42:25 +10:00
/* void aes_dec_blk(struct crypto_tfm *tfm, u8 *out, const u8 *in) */
2005-07-06 13:55:00 -07:00
2013-01-19 13:38:50 +02:00
entry( a e s _ d e c _ b l k ,2 4 0 ,. L d12 8 ,. L d19 2 )
2007-11-08 21:25:04 +08:00
decrypt_ r o u n d ( c r y p t o _ i t _ t a b ,- 9 6 )
decrypt_ r o u n d ( c r y p t o _ i t _ t a b ,- 8 0 )
2013-01-19 13:38:50 +02:00
.Ld192 : decrypt_ r o u n d ( c r y p t o _ i t _ t a b ,- 6 4 )
2007-11-08 21:25:04 +08:00
decrypt_ r o u n d ( c r y p t o _ i t _ t a b ,- 4 8 )
2013-01-19 13:38:50 +02:00
.Ld128 : decrypt_ r o u n d ( c r y p t o _ i t _ t a b ,- 3 2 )
2007-11-08 21:25:04 +08:00
decrypt_ r o u n d ( c r y p t o _ i t _ t a b ,- 1 6 )
decrypt_ r o u n d ( c r y p t o _ i t _ t a b , 0 )
decrypt_ r o u n d ( c r y p t o _ i t _ t a b , 1 6 )
decrypt_ r o u n d ( c r y p t o _ i t _ t a b , 3 2 )
decrypt_ r o u n d ( c r y p t o _ i t _ t a b , 4 8 )
decrypt_ r o u n d ( c r y p t o _ i t _ t a b , 6 4 )
decrypt_ r o u n d ( c r y p t o _ i t _ t a b , 8 0 )
decrypt_ r o u n d ( c r y p t o _ i t _ t a b , 9 6 )
decrypt_ f i n a l ( c r y p t o _ i l _ t a b ,1 1 2 )
2013-01-19 13:38:50 +02:00
return( a e s _ d e c _ b l k )