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