Unified - adapt the generation of blowfish assembler to use GENERATE
[openssl.git] / crypto / bf / asm / bf-586.pl
1 #!/usr/local/bin/perl
2
3 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
4 push(@INC,"${dir}","${dir}../../perlasm");
5 require "x86asm.pl";
6 require "cbc.pl";
7
8 $output = pop;
9 open STDOUT,">$output";
10
11 &asm_init($ARGV[0],"bf-586.pl",$ARGV[$#ARGV] eq "386");
12
13 $BF_ROUNDS=16;
14 $BF_OFF=($BF_ROUNDS+2)*4;
15 $L="edi";
16 $R="esi";
17 $P="ebp";
18 $tmp1="eax";
19 $tmp2="ebx";
20 $tmp3="ecx";
21 $tmp4="edx";
22
23 &BF_encrypt("BF_encrypt",1);
24 &BF_encrypt("BF_decrypt",0);
25 &cbc("BF_cbc_encrypt","BF_encrypt","BF_decrypt",1,4,5,3,-1,-1);
26 &asm_finish();
27
28 close STDOUT;
29
30 sub BF_encrypt
31         {
32         local($name,$enc)=@_;
33
34         &function_begin_B($name,"");
35
36         &comment("");
37
38         &push("ebp");
39         &push("ebx");
40         &mov($tmp2,&wparam(0));
41         &mov($P,&wparam(1));
42         &push("esi");
43         &push("edi");
44
45         &comment("Load the 2 words");
46         &mov($L,&DWP(0,$tmp2,"",0));
47         &mov($R,&DWP(4,$tmp2,"",0));
48
49         &xor(   $tmp1,  $tmp1);
50
51         # encrypting part
52
53         if ($enc)
54                 {
55                  &mov($tmp2,&DWP(0,$P,"",0));
56                 &xor(   $tmp3,  $tmp3);
57
58                 &xor($L,$tmp2);
59                 for ($i=0; $i<$BF_ROUNDS; $i+=2)
60                         {
61                         &comment("");
62                         &comment("Round $i");
63                         &BF_ENCRYPT($i+1,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,1);
64
65                         &comment("");
66                         &comment("Round ".sprintf("%d",$i+1));
67                         &BF_ENCRYPT($i+2,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,1);
68                         }
69                 # &mov($tmp1,&wparam(0)); In last loop
70                 &mov($tmp4,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
71                 }
72         else
73                 {
74                  &mov($tmp2,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
75                 &xor(   $tmp3,  $tmp3);
76
77                 &xor($L,$tmp2);
78                 for ($i=$BF_ROUNDS; $i>0; $i-=2)
79                         {
80                         &comment("");
81                         &comment("Round $i");
82                         &BF_ENCRYPT($i,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,0);
83                         &comment("");
84                         &comment("Round ".sprintf("%d",$i-1));
85                         &BF_ENCRYPT($i-1,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,0);
86                         }
87                 # &mov($tmp1,&wparam(0)); In last loop
88                 &mov($tmp4,&DWP(0,$P,"",0));
89                 }
90
91         &xor($R,$tmp4);
92         &mov(&DWP(4,$tmp1,"",0),$L);
93
94         &mov(&DWP(0,$tmp1,"",0),$R);
95         &function_end($name);
96         }
97
98 sub BF_ENCRYPT
99         {
100         local($i,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,$enc)=@_;
101
102         &mov(   $tmp4,          &DWP(&n2a($i*4),$P,"",0)); # for next round
103
104         &mov(   $tmp2,          $R);
105         &xor(   $L,             $tmp4);
106
107         &shr(   $tmp2,          16);
108         &mov(   $tmp4,          $R);
109
110         &movb(  &LB($tmp1),     &HB($tmp2));    # A
111         &and(   $tmp2,          0xff);          # B
112
113         &movb(  &LB($tmp3),     &HB($tmp4));    # C
114         &and(   $tmp4,          0xff);          # D
115
116         &mov(   $tmp1,          &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));
117         &mov(   $tmp2,          &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));
118
119         &add(   $tmp2,          $tmp1);
120         &mov(   $tmp1,          &DWP(&n2a($BF_OFF+0x0800),$P,$tmp3,4));
121
122         &xor(   $tmp2,          $tmp1);
123         &mov(   $tmp4,          &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp4,4));
124
125         &add(   $tmp2,          $tmp4);
126         if (($enc && ($i != 16)) || ((!$enc) && ($i != 1)))
127                 { &xor( $tmp1,          $tmp1); }
128         else
129                 {
130                 &comment("Load parameter 0 ($i) enc=$enc");
131                 &mov($tmp1,&wparam(0));
132                 } # In last loop
133
134         &xor(   $L,             $tmp2);
135         # delay
136         }
137
138 sub n2a
139         {
140         sprintf("%d",$_[0]);
141         }
142