2019-05-27 08:55:05 +02:00
/* SPDX-License-Identifier: GPL-2.0-or-later */
2006-06-20 21:12:02 +10: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- x86 _ 6 4 - a s m . S "
.text
2013-01-19 13:39:46 +02:00
# include < l i n u x / l i n k a g e . h >
2006-06-20 21:12:02 +10:00
# include < a s m / a s m - o f f s e t s . h >
# 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 % r a x
# define R 0 D % e a x
# define R 0 B % a l
# define R 0 H % a h
# define R 1 % r b x
# define R 1 D % e b x
# define R 1 B % b l
# define R 1 H % b h
# define R 2 % r c x
# define R 2 D % e c x
# define R 2 B % c l
# define R 2 H % c h
# define R 3 % r d x
# 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 ) \
movzx b ## B , % e d i ; \
mov s1 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx a ## B , % e d i ; \
mov s2 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
movzx b ## H , % e d i ; \
ror $ 1 6 , b ## D ; \
xor s2 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx a ## H , % e d i ; \
ror $ 1 6 , a ## D ; \
xor s3 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
movzx b ## B , % e d i ; \
xor s3 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx a ## B , % e d i ; \
xor ( % r11 ,% r d i ,4 ) , % r9 d ;\
movzx b ## H , % e d i ; \
ror $ 1 5 , b ## D ; \
xor ( % r11 ,% r d i ,4 ) , % r8 d ;\
movzx a ## H , % e d i ; \
xor s1 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
add % r8 d , % r9 d ;\
add % r9 d , % r8 d ;\
add k + r o u n d ( % r11 ) , % r9 d ;\
xor % r9 d , c ## D ; \
rol $ 1 5 , c ## D ; \
add k + 4 + r o u n d ( % r11 ) ,% r8 d ;\
xor % r8 d , 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
* during t h e r o u n d a a n d b a r e p r e p a r e d f o r t h e o u t p u t w h i t e n i n g
* /
# 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 ) \
mov b ## D , % r 10 d ;\
shl $ 3 2 , % r10 ;\
movzx b ## B , % e d i ; \
mov s1 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx a ## B , % e d i ; \
mov s2 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
movzx b ## H , % e d i ; \
ror $ 1 6 , b ## D ; \
xor s2 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx a ## H , % e d i ; \
ror $ 1 6 , a ## D ; \
xor s3 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
movzx b ## B , % e d i ; \
xor s3 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx a ## B , % e d i ; \
xor ( % r11 ,% r d i ,4 ) , % r9 d ;\
xor a , % r10 ;\
movzx b ## H , % e d i ; \
xor ( % r11 ,% r d i ,4 ) , % r8 d ;\
movzx a ## H , % e d i ; \
xor s1 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
add % r8 d , % r9 d ;\
add % r9 d , % r8 d ;\
add k + r o u n d ( % r11 ) , % r9 d ;\
xor % r9 d , c ## D ; \
ror $ 1 , c ## D ; \
add k + 4 + r o u n d ( % r11 ) ,% r8 d ;\
xor % r8 d , 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 ( a l r e a d y r o l $ 1 )
* 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
* 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 ) \
movzx a ## B , % e d i ; \
mov ( % r11 ,% r d i ,4 ) , % r9 d ;\
movzx b ## B , % e d i ; \
mov s3 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx a ## H , % e d i ; \
ror $ 1 6 , a ## D ; \
xor s1 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
movzx b ## H , % e d i ; \
ror $ 1 6 , b ## D ; \
xor ( % r11 ,% r d i ,4 ) , % r8 d ;\
movzx a ## B , % e d i ; \
xor s2 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
movzx b ## B , % e d i ; \
xor s1 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx a ## H , % e d i ; \
ror $ 1 5 , a ## D ; \
xor s3 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
movzx b ## H , % e d i ; \
xor s2 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
add % r8 d , % r9 d ;\
add % r9 d , % r8 d ;\
add k + r o u n d ( % r11 ) , % r9 d ;\
xor % r9 d , c ## D ; \
add k + 4 + r o u n d ( % r11 ) ,% r8 d ;\
xor % r8 d , 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
* 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 ( a l r e a d y r o l $ 1 )
* 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
* 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
* during t h e r o u n d a a n d b a r e p r e p a r e d f o r t h e o u t p u t w h i t e n i n g
* /
# 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 ) \
movzx a ## B , % e d i ; \
mov ( % r11 ,% r d i ,4 ) , % r9 d ;\
movzx b ## B , % e d i ; \
mov s3 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx b ## H , % e d i ; \
ror $ 1 6 , b ## D ; \
xor ( % r11 ,% r d i ,4 ) , % r8 d ;\
movzx a ## H , % e d i ; \
mov b ## D , % r 10 d ;\
shl $ 3 2 , % r10 ;\
xor a , % r10 ;\
ror $ 1 6 , a ## D ; \
xor s1 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
movzx b ## B , % e d i ; \
xor s1 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx a ## B , % e d i ; \
xor s2 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
movzx b ## H , % e d i ; \
xor s2 ( % r11 ,% r d i ,4 ) ,% r8 d ;\
movzx a ## H , % e d i ; \
xor s3 ( % r11 ,% r d i ,4 ) ,% r9 d ;\
add % r8 d , % r9 d ;\
add % r9 d , % r8 d ;\
add k + r o u n d ( % r11 ) , % r9 d ;\
xor % r9 d , c ## D ; \
add k + 4 + r o u n d ( % r11 ) ,% r8 d ;\
xor % r8 d , d ## D ; \
ror $ 1 , d ## D ;
2019-10-11 13:51:04 +02:00
SYM_ F U N C _ S T A R T ( t w o f i s h _ e n c _ b l k )
2006-06-20 21:12:02 +10:00
pushq R 1
2011-09-26 16:47:20 +03:00
/* %rdi contains the ctx address */
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 08:01:28 +08:00
/* %rsi contains the output address */
/* %rdx contains the input address */
/ * ctx a d d r e s s i s m o v e d t o f r e e o n e n o n - r e x r e g i s t e r
2006-06-20 21:12:02 +10:00
as t a r g e t f o r t h e 8 b i t h i g h o p e r a t i o n s * /
mov % r d i , % r11
movq ( R 3 ) , R 1
movq 8 ( R 3 ) , R 3
input_ w h i t e n i n g ( R 1 ,% r11 ,a _ o f f s e t )
input_ w h i t e n i n g ( R 3 ,% r11 ,c _ o f f s e t )
mov R 1 D , R 0 D
rol $ 1 6 , R 0 D
shr $ 3 2 , R 1
mov R 3 D , R 2 D
shr $ 3 2 , R 3
rol $ 1 , R 3 D
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 ( % r10 ,% r11 ,a _ o f f s e t )
movq % r10 , ( % r s i )
shl $ 3 2 , R 1
xor R 0 , R 1
output_ w h i t e n i n g ( R 1 ,% r11 ,c _ o f f s e t )
movq R 1 , 8 ( % r s i )
popq R 1
2015-03-31 19:00:10 +02:00
movl $ 1 ,% e a x
2021-12-04 14:43:40 +01:00
RET
2019-10-11 13:51:04 +02:00
SYM_ F U N C _ E N D ( t w o f i s h _ e n c _ b l k )
2006-06-20 21:12:02 +10:00
2019-10-11 13:51:04 +02:00
SYM_ F U N C _ S T A R T ( t w o f i s h _ d e c _ b l k )
2006-06-20 21:12:02 +10:00
pushq R 1
2011-09-26 16:47:20 +03:00
/* %rdi contains the ctx address */
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 08:01:28 +08:00
/* %rsi contains the output address */
/* %rdx contains the input address */
/ * ctx a d d r e s s i s m o v e d t o f r e e o n e n o n - r e x r e g i s t e r
2006-06-20 21:12:02 +10:00
as t a r g e t f o r t h e 8 b i t h i g h o p e r a t i o n s * /
mov % r d i , % r11
movq ( R 3 ) , R 1
movq 8 ( R 3 ) , R 3
output_ w h i t e n i n g ( R 1 ,% r11 ,a _ o f f s e t )
output_ w h i t e n i n g ( R 3 ,% r11 ,c _ o f f s e t )
mov R 1 D , R 0 D
shr $ 3 2 , R 1
rol $ 1 6 , R 1 D
mov R 3 D , R 2 D
shr $ 3 2 , R 3
rol $ 1 , R 2 D
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 ( % r10 ,% r11 ,a _ o f f s e t )
movq % r10 , ( % r s i )
shl $ 3 2 , R 1
xor R 0 , R 1
input_ w h i t e n i n g ( R 1 ,% r11 ,c _ o f f s e t )
movq R 1 , 8 ( % r s i )
popq R 1
2015-03-31 19:00:10 +02:00
movl $ 1 ,% e a x
2021-12-04 14:43:40 +01:00
RET
2019-10-11 13:51:04 +02:00
SYM_ F U N C _ E N D ( t w o f i s h _ d e c _ b l k )