147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#!/usr/local/bin/perl
247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrompush(@INC,"${dir}","${dir}../../perlasm");
547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromrequire "x86asm.pl";
647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromrequire "cbc.pl";
747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom&asm_init($ARGV[0],"bf-586.pl",$ARGV[$#ARGV] eq "386");
947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
1047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom$BF_ROUNDS=16;
1147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom$BF_OFF=($BF_ROUNDS+2)*4;
1247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom$L="edi";
1347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom$R="esi";
1447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom$P="ebp";
1547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom$tmp1="eax";
1647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom$tmp2="ebx";
1747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom$tmp3="ecx";
1847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom$tmp4="edx";
1947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
2047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom&BF_encrypt("BF_encrypt",1);
2147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom&BF_encrypt("BF_decrypt",0);
2247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom&cbc("BF_cbc_encrypt","BF_encrypt","BF_decrypt",1,4,5,3,-1,-1);
2347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom&asm_finish();
2447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
2547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromsub BF_encrypt
2647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	{
2747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	local($name,$enc)=@_;
2847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
2947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&function_begin_B($name,"");
3047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
3147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&comment("");
3247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
3347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&push("ebp");
3447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&push("ebx");
3547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov($tmp2,&wparam(0));
3647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov($P,&wparam(1));
3747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&push("esi");
3847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&push("edi");
3947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
4047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&comment("Load the 2 words");
4147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov($L,&DWP(0,$tmp2,"",0));
4247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov($R,&DWP(4,$tmp2,"",0));
4347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
4447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&xor(	$tmp1,	$tmp1);
4547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
4647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	# encrypting part
4747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
4847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	if ($enc)
4947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		{
5047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		 &mov($tmp2,&DWP(0,$P,"",0));
5147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		&xor(	$tmp3,	$tmp3);
5247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
5347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		&xor($L,$tmp2);
5447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		for ($i=0; $i<$BF_ROUNDS; $i+=2)
5547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			{
5647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&comment("");
5747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&comment("Round $i");
5847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&BF_ENCRYPT($i+1,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,1);
5947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
6047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&comment("");
6147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&comment("Round ".sprintf("%d",$i+1));
6247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&BF_ENCRYPT($i+2,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,1);
6347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			}
6447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		# &mov($tmp1,&wparam(0)); In last loop
6547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		&mov($tmp4,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
6647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		}
6747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	else
6847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		{
6947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		 &mov($tmp2,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
7047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		&xor(	$tmp3,	$tmp3);
7147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
7247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		&xor($L,$tmp2);
7347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		for ($i=$BF_ROUNDS; $i>0; $i-=2)
7447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			{
7547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&comment("");
7647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&comment("Round $i");
7747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&BF_ENCRYPT($i,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,0);
7847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&comment("");
7947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&comment("Round ".sprintf("%d",$i-1));
8047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			&BF_ENCRYPT($i-1,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,0);
8147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			}
8247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		# &mov($tmp1,&wparam(0)); In last loop
8347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		&mov($tmp4,&DWP(0,$P,"",0));
8447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		}
8547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
8647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&xor($R,$tmp4);
8747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov(&DWP(4,$tmp1,"",0),$L);
8847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
8947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov(&DWP(0,$tmp1,"",0),$R);
9047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&function_end($name);
9147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	}
9247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
9347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromsub BF_ENCRYPT
9447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	{
9547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	local($i,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,$enc)=@_;
9647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
9747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov(	$tmp4,		&DWP(&n2a($i*4),$P,"",0)); # for next round
9847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
9947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov(	$tmp2,		$R);
10047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&xor(	$L,		$tmp4);
10147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
10247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&shr(	$tmp2,		16);
10347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov(	$tmp4,		$R);
10447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
10547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&movb(	&LB($tmp1),	&HB($tmp2));	# A
10647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&and(	$tmp2,		0xff);		# B
10747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
10847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&movb(	&LB($tmp3),	&HB($tmp4));	# C
10947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&and(	$tmp4,		0xff);		# D
11047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
11147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov(	$tmp1,		&DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));
11247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov(	$tmp2,		&DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));
11347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
11447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&add(	$tmp2,		$tmp1);
11547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov(	$tmp1,		&DWP(&n2a($BF_OFF+0x0800),$P,$tmp3,4));
11647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
11747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&xor(	$tmp2,		$tmp1);
11847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&mov(	$tmp4,		&DWP(&n2a($BF_OFF+0x0C00),$P,$tmp4,4));
11947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
12047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&add(	$tmp2,		$tmp4);
12147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	if (($enc && ($i != 16)) || ((!$enc) && ($i != 1)))
12247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		{ &xor(	$tmp1,		$tmp1); }
12347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	else
12447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		{
12547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		&comment("Load parameter 0 ($i) enc=$enc");
12647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		&mov($tmp1,&wparam(0));
12747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		} # In last loop
12847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
12947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	&xor(	$L,		$tmp2);
13047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	# delay
13147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	}
13247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
13347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromsub n2a
13447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	{
13547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	sprintf("%d",$_[0]);
13647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	}
13747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
138