2019-05-27 09:55:05 +03:00
/* SPDX-License-Identifier: GPL-2.0-or-later */
2006-06-20 14:59:16 +04:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright ( C ) 2 0 0 6 b y J o a c h i m F r i t s c h i , < j f r i t s c h i @freenet.de> *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
.file " twofish- i 5 8 6 - a s m . S "
.text
2013-01-19 15:39:46 +04:00
# include < l i n u x / l i n k a g e . h >
2006-06-20 14:59:16 +04:00
# include < a s m / a s m - o f f s e t s . h >
tree-wide: Assorted spelling fixes
In particular, several occurances of funny versions of 'success',
'unknown', 'therefore', 'acknowledge', 'argument', 'achieve', 'address',
'beginning', 'desirable', 'separate' and 'necessary' are fixed.
Signed-off-by: Daniel Mack <daniel@caiaq.de>
Cc: Joe Perches <joe@perches.com>
Cc: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2010-02-03 03:01:28 +03:00
/* return address at 0 */
2006-06-20 14:59:16 +04:00
# define i n _ b l k 1 2 / * i n p u t b y t e a r r a y a d d r e s s p a r a m e t e r * /
# define o u t _ b l k 8 / * o u t p u t b y t e a r r a y a d d r e s s p a r a m e t e r * /
2011-09-26 17:47:20 +04:00
# define c t x 4 / * T w o f i s h c o n t e x t s t r u c t u r e * /
2006-06-20 14:59:16 +04:00
# define a _ o f f s e t 0
# define b _ o f f s e t 4
# define c _ o f f s e t 8
# define d _ o f f s e t 1 2
/* Structure of the crypto context struct*/
# define s0 0 / * S 0 A r r a y 2 5 6 W o r d s e a c h * /
# define s1 1 0 2 4 / * S 1 A r r a y * /
# define s2 2 0 4 8 / * S 2 A r r a y * /
# define s3 3 0 7 2 / * S 3 A r r a y * /
# define w 4 0 9 6 / * 8 w h i t e n i n g k e y s ( w o r d ) * /
# define k 4 1 2 8 / * k e y 1 - 3 2 ( w o r d ) * /
/* define a few register aliases to allow macro substitution */
# define R 0 D % e a x
# define R 0 B % a l
# define R 0 H % a h
# define R 1 D % e b x
# define R 1 B % b l
# define R 1 H % b h
# define R 2 D % e c x
# define R 2 B % c l
# define R 2 H % c h
# define R 3 D % e d x
# define R 3 B % d l
# define R 3 H % d h
/* performs input whitening */
# define i n p u t _ w h i t e n i n g ( s r c ,c o n t e x t ,o f f s e t ) \
xor w + o f f s e t ( c o n t e x t ) , s r c ;
/* performs input whitening */
# define o u t p u t _ w h i t e n i n g ( s r c ,c o n t e x t ,o f f s e t ) \
xor w + 1 6 + o f f s e t ( c o n t e x t ) , s r c ;
/ *
* a i n p u t r e g i s t e r c o n t a i n i n g a ( r o t a t e d 1 6 )
* b i n p u t r e g i s t e r c o n t a i n i n g b
* c i n p u t r e g i s t e r c o n t a i n i n g c
* d i n p u t r e g i s t e r c o n t a i n i n g d ( a l r e a d y r o l $ 1 )
* operations o n a a n d b a r e i n t e r l e a v e d t o i n c r e a s e p e r f o r m a n c e
* /
# define e n c r y p t _ r o u n d ( a ,b ,c ,d ,r o u n d ) \
push d ## D ; \
movzx b ## B , % e d i ; \
mov s1 ( % e b p ,% e d i ,4 ) ,d ## D ; \
movzx a ## B , % e d i ; \
mov s2 ( % e b p ,% e d i ,4 ) ,% e s i ;\
movzx b ## H , % e d i ; \
ror $ 1 6 , b ## D ; \
xor s2 ( % e b p ,% e d i ,4 ) ,d ## D ; \
movzx a ## H , % e d i ; \
ror $ 1 6 , a ## D ; \
xor s3 ( % e b p ,% e d i ,4 ) ,% e s i ;\
movzx b ## B , % e d i ; \
xor s3 ( % e b p ,% e d i ,4 ) ,d ## D ; \
movzx a ## B , % e d i ; \
xor ( % e b p ,% e d i ,4 ) , % e s i ;\
movzx b ## H , % e d i ; \
ror $ 1 5 , b ## D ; \
xor ( % e b p ,% e d i ,4 ) , d ## D ; \
movzx a ## H , % e d i ; \
xor s1 ( % e b p ,% e d i ,4 ) ,% e s i ;\
pop % e d i ;\
add d ## D , % e s i ; \
add % e s i , d ## D ; \
add k + r o u n d ( % e b p ) , % e s i ;\
xor % e s i , c ## D ; \
rol $ 1 5 , c ## D ; \
add k + 4 + r o u n d ( % e b p ) ,d ## D ; \
xor % e d i , d ## D ;
/ *
* a i n p u t r e g i s t e r c o n t a i n i n g a ( r o t a t e d 1 6 )
* b i n p u t r e g i s t e r c o n t a i n i n g b
* c i n p u t r e g i s t e r c o n t a i n i n g c
* d i n p u t r e g i s t e r c o n t a i n i n g d ( a l r e a d y r o l $ 1 )
* operations o n a a n d b a r e i n t e r l e a v e d t o i n c r e a s e p e r f o r m a n c e
* last r o u n d h a s d i f f e r e n t r o t a t i o n s f o r t h e o u t p u t p r e p a r a t i o n
* /
# define e n c r y p t _ l a s t _ r o u n d ( a ,b ,c ,d ,r o u n d ) \
push d ## D ; \
movzx b ## B , % e d i ; \
mov s1 ( % e b p ,% e d i ,4 ) ,d ## D ; \
movzx a ## B , % e d i ; \
mov s2 ( % e b p ,% e d i ,4 ) ,% e s i ;\
movzx b ## H , % e d i ; \
ror $ 1 6 , b ## D ; \
xor s2 ( % e b p ,% e d i ,4 ) ,d ## D ; \
movzx a ## H , % e d i ; \
ror $ 1 6 , a ## D ; \
xor s3 ( % e b p ,% e d i ,4 ) ,% e s i ;\
movzx b ## B , % e d i ; \
xor s3 ( % e b p ,% e d i ,4 ) ,d ## D ; \
movzx a ## B , % e d i ; \
xor ( % e b p ,% e d i ,4 ) , % e s i ;\
movzx b ## H , % e d i ; \
ror $ 1 6 , b ## D ; \
xor ( % e b p ,% e d i ,4 ) , d ## D ; \
movzx a ## H , % e d i ; \
xor s1 ( % e b p ,% e d i ,4 ) ,% e s i ;\
pop % e d i ;\
add d ## D , % e s i ; \
add % e s i , d ## D ; \
add k + r o u n d ( % e b p ) , % e s i ;\
xor % e s i , c ## D ; \
ror $ 1 , c ## D ; \
add k + 4 + r o u n d ( % e b p ) ,d ## D ; \
xor % e d i , d ## D ;
/ *
* a i n p u t r e g i s t e r c o n t a i n i n g a
* b i n p u t r e g i s t e r c o n t a i n i n g b ( r o t a t e d 1 6 )
* c i n p u t r e g i s t e r c o n t a i n i n g c
* d i n p u t r e g i s t e r c o n t a i n i n g d ( a l r e a d y r o l $ 1 )
* operations o n a a n d b a r e i n t e r l e a v e d t o i n c r e a s e p e r f o r m a n c e
* /
# define d e c r y p t _ r o u n d ( a ,b ,c ,d ,r o u n d ) \
push c ## D ; \
movzx a ## B , % e d i ; \
mov ( % e b p ,% e d i ,4 ) , c ## D ; \
movzx b ## B , % e d i ; \
mov s3 ( % e b p ,% e d i ,4 ) ,% e s i ;\
movzx a ## H , % e d i ; \
ror $ 1 6 , a ## D ; \
xor s1 ( % e b p ,% e d i ,4 ) ,c ## D ; \
movzx b ## H , % e d i ; \
ror $ 1 6 , b ## D ; \
xor ( % e b p ,% e d i ,4 ) , % e s i ;\
movzx a ## B , % e d i ; \
xor s2 ( % e b p ,% e d i ,4 ) ,c ## D ; \
movzx b ## B , % e d i ; \
xor s1 ( % e b p ,% e d i ,4 ) ,% e s i ;\
movzx a ## H , % e d i ; \
ror $ 1 5 , a ## D ; \
xor s3 ( % e b p ,% e d i ,4 ) ,c ## D ; \
movzx b ## H , % e d i ; \
xor s2 ( % e b p ,% e d i ,4 ) ,% e s i ;\
pop % e d i ;\
add % e s i , c ## D ; \
add c ## D , % e s i ; \
add k + r o u n d ( % e b p ) , c ## D ; \
xor % e d i , c ## D ; \
add k + 4 + r o u n d ( % e b p ) ,% e s i ;\
xor % e s i , d ## D ; \
rol $ 1 5 , d ## D ;
/ *
* a i n p u t r e g i s t e r c o n t a i n i n g a
* b i n p u t r e g i s t e r c o n t a i n i n g b ( r o t a t e d 1 6 )
* c i n p u t r e g i s t e r c o n t a i n i n g c
* d i n p u t r e g i s t e r c o n t a i n i n g d ( a l r e a d y r o l $ 1 )
* operations o n a a n d b a r e i n t e r l e a v e d t o i n c r e a s e p e r f o r m a n c e
* last r o u n d h a s d i f f e r e n t r o t a t i o n s f o r t h e o u t p u t p r e p a r a t i o n
* /
# define d e c r y p t _ l a s t _ r o u n d ( a ,b ,c ,d ,r o u n d ) \
push c ## D ; \
movzx a ## B , % e d i ; \
mov ( % e b p ,% e d i ,4 ) , c ## D ; \
movzx b ## B , % e d i ; \
mov s3 ( % e b p ,% e d i ,4 ) ,% e s i ;\
movzx a ## H , % e d i ; \
ror $ 1 6 , a ## D ; \
xor s1 ( % e b p ,% e d i ,4 ) ,c ## D ; \
movzx b ## H , % e d i ; \
ror $ 1 6 , b ## D ; \
xor ( % e b p ,% e d i ,4 ) , % e s i ;\
movzx a ## B , % e d i ; \
xor s2 ( % e b p ,% e d i ,4 ) ,c ## D ; \
movzx b ## B , % e d i ; \
xor s1 ( % e b p ,% e d i ,4 ) ,% e s i ;\
movzx a ## H , % e d i ; \
ror $ 1 6 , a ## D ; \
xor s3 ( % e b p ,% e d i ,4 ) ,c ## D ; \
movzx b ## H , % e d i ; \
xor s2 ( % e b p ,% e d i ,4 ) ,% e s i ;\
pop % e d i ;\
add % e s i , c ## D ; \
add c ## D , % e s i ; \
add k + r o u n d ( % e b p ) , c ## D ; \
xor % e d i , c ## D ; \
add k + 4 + r o u n d ( % e b p ) ,% e s i ;\
xor % e s i , d ## D ; \
ror $ 1 , d ## D ;
2013-01-19 15:39:46 +04:00
ENTRY( t w o f i s h _ e n c _ b l k )
2006-06-20 14:59:16 +04:00
push % e b p / * s a v e r e g i s t e r s a c c o r d i n g t o c a l l i n g c o n v e n t i o n * /
push % e b x
push % e s i
push % e d i
2011-09-26 17:47:20 +04:00
mov c t x + 1 6 ( % e s p ) , % e b p / * a b u s e t h e b a s e p o i n t e r : s e t n e w b a s e
* pointer t o t h e c t x a d d r e s s * /
tree-wide: Assorted spelling fixes
In particular, several occurances of funny versions of 'success',
'unknown', 'therefore', 'acknowledge', 'argument', 'achieve', 'address',
'beginning', 'desirable', 'separate' and 'necessary' are fixed.
Signed-off-by: Daniel Mack <daniel@caiaq.de>
Cc: Joe Perches <joe@perches.com>
Cc: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2010-02-03 03:01:28 +03:00
mov i n _ b l k + 1 6 ( % e s p ) ,% e d i / * i n p u t a d d r e s s i n e d i * /
2006-06-20 14:59:16 +04:00
mov ( % e d i ) , % e a x
mov b _ o f f s e t ( % e d i ) , % e b x
mov c _ o f f s e t ( % e d i ) , % e c x
mov d _ o f f s e t ( % e d i ) , % e d x
input_ w h i t e n i n g ( % e a x ,% e b p ,a _ o f f s e t )
ror $ 1 6 , % e a x
input_ w h i t e n i n g ( % e b x ,% e b p ,b _ o f f s e t )
input_ w h i t e n i n g ( % e c x ,% e b p ,c _ o f f s e t )
input_ w h i t e n i n g ( % e d x ,% e b p ,d _ o f f s e t )
rol $ 1 , % e d x
encrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,0 ) ;
encrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,8 ) ;
encrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,2 * 8 ) ;
encrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,3 * 8 ) ;
encrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,4 * 8 ) ;
encrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,5 * 8 ) ;
encrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,6 * 8 ) ;
encrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,7 * 8 ) ;
encrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,8 * 8 ) ;
encrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,9 * 8 ) ;
encrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,1 0 * 8 ) ;
encrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,1 1 * 8 ) ;
encrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,1 2 * 8 ) ;
encrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,1 3 * 8 ) ;
encrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,1 4 * 8 ) ;
encrypt_ l a s t _ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,1 5 * 8 ) ;
output_ w h i t e n i n g ( % e a x ,% e b p ,c _ o f f s e t )
output_ w h i t e n i n g ( % e b x ,% e b p ,d _ o f f s e t )
output_ w h i t e n i n g ( % e c x ,% e b p ,a _ o f f s e t )
output_ w h i t e n i n g ( % e d x ,% e b p ,b _ o f f s e t )
mov o u t _ b l k + 1 6 ( % e s p ) ,% e d i ;
mov % e a x , c _ o f f s e t ( % e d i )
mov % e b x , d _ o f f s e t ( % e d i )
mov % e c x , ( % e d i )
mov % e d x , b _ o f f s e t ( % e d i )
pop % e d i
pop % e s i
pop % e b x
pop % e b p
mov $ 1 , % e a x
ret
2013-01-19 15:39:46 +04:00
ENDPROC( t w o f i s h _ e n c _ b l k )
2006-06-20 14:59:16 +04:00
2013-01-19 15:39:46 +04:00
ENTRY( t w o f i s h _ d e c _ b l k )
2006-06-20 14:59:16 +04:00
push % e b p / * s a v e r e g i s t e r s a c c o r d i n g t o c a l l i n g c o n v e n t i o n * /
push % e b x
push % e s i
push % e d i
2011-09-26 17:47:20 +04:00
mov c t x + 1 6 ( % e s p ) , % e b p / * a b u s e t h e b a s e p o i n t e r : s e t n e w b a s e
* pointer t o t h e c t x a d d r e s s * /
tree-wide: Assorted spelling fixes
In particular, several occurances of funny versions of 'success',
'unknown', 'therefore', 'acknowledge', 'argument', 'achieve', 'address',
'beginning', 'desirable', 'separate' and 'necessary' are fixed.
Signed-off-by: Daniel Mack <daniel@caiaq.de>
Cc: Joe Perches <joe@perches.com>
Cc: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2010-02-03 03:01:28 +03:00
mov i n _ b l k + 1 6 ( % e s p ) ,% e d i / * i n p u t a d d r e s s i n e d i * /
2006-06-20 14:59:16 +04:00
mov ( % e d i ) , % e a x
mov b _ o f f s e t ( % e d i ) , % e b x
mov c _ o f f s e t ( % e d i ) , % e c x
mov d _ o f f s e t ( % e d i ) , % e d x
output_ w h i t e n i n g ( % e a x ,% e b p ,a _ o f f s e t )
output_ w h i t e n i n g ( % e b x ,% e b p ,b _ o f f s e t )
ror $ 1 6 , % e b x
output_ w h i t e n i n g ( % e c x ,% e b p ,c _ o f f s e t )
output_ w h i t e n i n g ( % e d x ,% e b p ,d _ o f f s e t )
rol $ 1 , % e c x
decrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,1 5 * 8 ) ;
decrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,1 4 * 8 ) ;
decrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,1 3 * 8 ) ;
decrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,1 2 * 8 ) ;
decrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,1 1 * 8 ) ;
decrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,1 0 * 8 ) ;
decrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,9 * 8 ) ;
decrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,8 * 8 ) ;
decrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,7 * 8 ) ;
decrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,6 * 8 ) ;
decrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,5 * 8 ) ;
decrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,4 * 8 ) ;
decrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,3 * 8 ) ;
decrypt_ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,2 * 8 ) ;
decrypt_ r o u n d ( R 0 ,R 1 ,R 2 ,R 3 ,1 * 8 ) ;
decrypt_ l a s t _ r o u n d ( R 2 ,R 3 ,R 0 ,R 1 ,0 ) ;
input_ w h i t e n i n g ( % e a x ,% e b p ,c _ o f f s e t )
input_ w h i t e n i n g ( % e b x ,% e b p ,d _ o f f s e t )
input_ w h i t e n i n g ( % e c x ,% e b p ,a _ o f f s e t )
input_ w h i t e n i n g ( % e d x ,% e b p ,b _ o f f s e t )
mov o u t _ b l k + 1 6 ( % e s p ) ,% e d i ;
mov % e a x , c _ o f f s e t ( % e d i )
mov % e b x , d _ o f f s e t ( % e d i )
mov % e c x , ( % e d i )
mov % e d x , b _ o f f s e t ( % e d i )
pop % e d i
pop % e s i
pop % e b x
pop % e b p
mov $ 1 , % e a x
ret
2013-01-19 15:39:46 +04:00
ENDPROC( t w o f i s h _ d e c _ b l k )