7a62f671617c5849e2c6fb9ac501a8b62a1ba74a
[openssl.git] / crypto / bf / asm / bf-686.pl
1 #!/usr/local/bin/perl
2 #!/usr/local/bin/perl
3
4 push(@INC,"perlasm","../../perlasm");
5 require "x86asm.pl";
6 require "cbc.pl";
7
8 &asm_init($ARGV[0],"bf-686.pl");
9
10 $BF_ROUNDS=16;
11 $BF_OFF=($BF_ROUNDS+2)*4;
12 $L="ecx";
13 $R="edx";
14 $P="edi";
15 $tot="esi";
16 $tmp1="eax";
17 $tmp2="ebx";
18 $tmp3="ebp";
19
20 &des_encrypt("BF_encrypt",1);
21 &des_encrypt("BF_decrypt",0);
22 &cbc("BF_cbc_encrypt","BF_encrypt","BF_decrypt",1,4,5,3,-1,-1);
23
24 &asm_finish();
25
26 &file_end();
27
28 sub des_encrypt
29         {
30         local($name,$enc)=@_;
31
32         &function_begin($name,"");
33
34         &comment("");
35         &comment("Load the 2 words");
36         &mov("eax",&wparam(0));
37         &mov($L,&DWP(0,"eax","",0));
38         &mov($R,&DWP(4,"eax","",0));
39
40         &comment("");
41         &comment("P pointer, s and enc flag");
42         &mov($P,&wparam(1));
43
44         &xor(   $tmp1,  $tmp1);
45         &xor(   $tmp2,  $tmp2);
46
47         # encrypting part
48
49         if ($enc)
50                 {
51                 &xor($L,&DWP(0,$P,"",0));
52                 for ($i=0; $i<$BF_ROUNDS; $i+=2)
53                         {
54                         &comment("");
55                         &comment("Round $i");
56                         &BF_ENCRYPT($i+1,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);
57
58                         &comment("");
59                         &comment("Round ".sprintf("%d",$i+1));
60                         &BF_ENCRYPT($i+2,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);
61                         }
62                 &xor($R,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
63
64                 &mov("eax",&wparam(0));
65                 &mov(&DWP(0,"eax","",0),$R);
66                 &mov(&DWP(4,"eax","",0),$L);
67                 &function_end_A($name);
68                 }
69         else
70                 {
71                 &xor($L,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
72                 for ($i=$BF_ROUNDS; $i>0; $i-=2)
73                         {
74                         &comment("");
75                         &comment("Round $i");
76                         &BF_ENCRYPT($i,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);
77                         &comment("");
78                         &comment("Round ".sprintf("%d",$i-1));
79                         &BF_ENCRYPT($i-1,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);
80                         }
81                 &xor($R,&DWP(0,$P,"",0));
82
83                 &mov("eax",&wparam(0));
84                 &mov(&DWP(0,"eax","",0),$R);
85                 &mov(&DWP(4,"eax","",0),$L);
86                 &function_end_A($name);
87                 }
88
89         &function_end_B($name);
90         }
91
92 sub BF_ENCRYPT
93         {
94         local($i,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3)=@_;
95
96         &rotr(  $R,             16);
97         &mov(   $tot,           &DWP(&n2a($i*4),$P,"",0));
98
99         &movb(  &LB($tmp1),     &HB($R));
100         &movb(  &LB($tmp2),     &LB($R));
101
102         &rotr(  $R,             16);
103         &xor(   $L,             $tot);
104
105         &mov(   $tot,           &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));
106         &mov(   $tmp3,          &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));
107
108         &movb(  &LB($tmp1),     &HB($R));
109         &movb(  &LB($tmp2),     &LB($R));
110
111         &add(   $tot,           $tmp3);
112         &mov(   $tmp1,          &DWP(&n2a($BF_OFF+0x0800),$P,$tmp1,4)); # delay
113
114         &xor(   $tot,           $tmp1);
115         &mov(   $tmp3,          &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp2,4));
116
117         &add(   $tot,           $tmp3);
118         &xor(   $tmp1,          $tmp1);
119
120         &xor(   $L,             $tot);                                  
121         # delay
122         }
123
124 sub n2a
125         {
126         sprintf("%d",$_[0]);
127         }
128