Unified - adapt the generation of rc5 assembler to use GENERATE
[openssl.git] / crypto / rc5 / asm / rc5-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],"rc5-586.pl");
12
13 $RC5_MAX_ROUNDS=16;
14 $RC5_32_OFF=($RC5_MAX_ROUNDS+2)*4;
15 $A="edi";
16 $B="esi";
17 $S="ebp";
18 $tmp1="eax";
19 $r="ebx";
20 $tmpc="ecx";
21 $tmp4="edx";
22
23 &RC5_32_encrypt("RC5_32_encrypt",1);
24 &RC5_32_encrypt("RC5_32_decrypt",0);
25 &cbc("RC5_32_cbc_encrypt","RC5_32_encrypt","RC5_32_decrypt",0,4,5,3,-1,-1);
26 &asm_finish();
27
28 close STDOUT;
29
30 sub RC5_32_encrypt
31         {
32         local($name,$enc)=@_;
33
34         &function_begin_B($name,"");
35
36         &comment("");
37
38         &push("ebp");
39          &push("esi");
40         &push("edi");
41          &mov($tmp4,&wparam(0));
42         &mov($S,&wparam(1));
43
44         &comment("Load the 2 words");
45          &mov($A,&DWP(0,$tmp4,"",0));
46         &mov($B,&DWP(4,$tmp4,"",0));
47
48         &push($r);
49          &mov($r,       &DWP(0,$S,"",0));
50
51         # encrypting part
52
53         if ($enc)
54                 {
55                  &add($A,       &DWP(4+0,$S,"",0));
56                 &add($B,        &DWP(4+4,$S,"",0));
57
58                 for ($i=0; $i<$RC5_MAX_ROUNDS; $i++)
59                         {
60                          &xor($A,       $B);
61                         &mov($tmp1,     &DWP(12+$i*8,$S,"",0));
62                          &mov($tmpc,    $B);
63                         &rotl($A,       &LB("ecx"));
64                         &add($A,        $tmp1);
65
66                          &xor($B,       $A);
67                         &mov($tmp1,     &DWP(16+$i*8,$S,"",0));
68                          &mov($tmpc,    $A);
69                         &rotl($B,       &LB("ecx"));
70                         &add($B,        $tmp1);
71                         if (($i == 7) || ($i == 11))
72                                 {
73                          &cmp($r,       $i+1);
74                         &je(&label("rc5_exit"));
75                                 }
76                         }
77                 }
78         else
79                 {
80                  &cmp($r,       12);
81                 &je(&label("rc5_dec_12"));
82                  &cmp($r,       8);
83                 &je(&label("rc5_dec_8"));
84                 for ($i=$RC5_MAX_ROUNDS; $i > 0; $i--)
85                         {
86                         &set_label("rc5_dec_$i") if ($i == 12) || ($i == 8);
87                          &mov($tmp1,    &DWP($i*8+8,$S,"",0));
88                         &sub($B,        $tmp1);
89                          &mov($tmpc,    $A);
90                         &rotr($B,       &LB("ecx"));
91                         &xor($B,        $A);
92
93                          &mov($tmp1,    &DWP($i*8+4,$S,"",0));
94                         &sub($A,        $tmp1);
95                          &mov($tmpc,    $B);
96                         &rotr($A,       &LB("ecx"));
97                         &xor($A,        $B);
98                         }
99                  &sub($B,       &DWP(4+4,$S,"",0));
100                 &sub($A,        &DWP(4+0,$S,"",0));
101                 }
102
103         &set_label("rc5_exit");
104          &mov(&DWP(0,$tmp4,"",0),$A);
105         &mov(&DWP(4,$tmp4,"",0),$B);
106
107          &pop("ebx");
108         &pop("edi");
109          &pop("esi");
110         &pop("ebp");
111          &ret();
112         &function_end_B($name);
113         }
114
115