592cbd7c76708d490377354b843151940080f4bd
[openssl.git] / crypto / rc5 / asm / rc5-586.pl
1 #! /usr/bin/env perl
2 # Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9
10 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
11 push(@INC,"${dir}","${dir}../../perlasm");
12 require "x86asm.pl";
13 require "cbc.pl";
14
15 $output = pop;
16 open STDOUT,">$output";
17
18 &asm_init($ARGV[0]);
19
20 $RC5_MAX_ROUNDS=16;
21 $RC5_32_OFF=($RC5_MAX_ROUNDS+2)*4;
22 $A="edi";
23 $B="esi";
24 $S="ebp";
25 $tmp1="eax";
26 $r="ebx";
27 $tmpc="ecx";
28 $tmp4="edx";
29
30 &RC5_32_encrypt("RC5_32_encrypt",1);
31 &RC5_32_encrypt("RC5_32_decrypt",0);
32 &cbc("RC5_32_cbc_encrypt","RC5_32_encrypt","RC5_32_decrypt",0,4,5,3,-1,-1);
33 &asm_finish();
34
35 close STDOUT;
36
37 sub RC5_32_encrypt
38         {
39         local($name,$enc)=@_;
40
41         &function_begin_B($name,"");
42
43         &comment("");
44
45         &push("ebp");
46          &push("esi");
47         &push("edi");
48          &mov($tmp4,&wparam(0));
49         &mov($S,&wparam(1));
50
51         &comment("Load the 2 words");
52          &mov($A,&DWP(0,$tmp4,"",0));
53         &mov($B,&DWP(4,$tmp4,"",0));
54
55         &push($r);
56          &mov($r,       &DWP(0,$S,"",0));
57
58         # encrypting part
59
60         if ($enc)
61                 {
62                  &add($A,       &DWP(4+0,$S,"",0));
63                 &add($B,        &DWP(4+4,$S,"",0));
64
65                 for ($i=0; $i<$RC5_MAX_ROUNDS; $i++)
66                         {
67                          &xor($A,       $B);
68                         &mov($tmp1,     &DWP(12+$i*8,$S,"",0));
69                          &mov($tmpc,    $B);
70                         &rotl($A,       &LB("ecx"));
71                         &add($A,        $tmp1);
72
73                          &xor($B,       $A);
74                         &mov($tmp1,     &DWP(16+$i*8,$S,"",0));
75                          &mov($tmpc,    $A);
76                         &rotl($B,       &LB("ecx"));
77                         &add($B,        $tmp1);
78                         if (($i == 7) || ($i == 11))
79                                 {
80                          &cmp($r,       $i+1);
81                         &je(&label("rc5_exit"));
82                                 }
83                         }
84                 }
85         else
86                 {
87                  &cmp($r,       12);
88                 &je(&label("rc5_dec_12"));
89                  &cmp($r,       8);
90                 &je(&label("rc5_dec_8"));
91                 for ($i=$RC5_MAX_ROUNDS; $i > 0; $i--)
92                         {
93                         &set_label("rc5_dec_$i") if ($i == 12) || ($i == 8);
94                          &mov($tmp1,    &DWP($i*8+8,$S,"",0));
95                         &sub($B,        $tmp1);
96                          &mov($tmpc,    $A);
97                         &rotr($B,       &LB("ecx"));
98                         &xor($B,        $A);
99
100                          &mov($tmp1,    &DWP($i*8+4,$S,"",0));
101                         &sub($A,        $tmp1);
102                          &mov($tmpc,    $B);
103                         &rotr($A,       &LB("ecx"));
104                         &xor($A,        $B);
105                         }
106                  &sub($B,       &DWP(4+4,$S,"",0));
107                 &sub($A,        &DWP(4+0,$S,"",0));
108                 }
109
110         &set_label("rc5_exit");
111          &mov(&DWP(0,$tmp4,"",0),$A);
112         &mov(&DWP(4,$tmp4,"",0),$B);
113
114          &pop("ebx");
115         &pop("edi");
116          &pop("esi");
117         &pop("ebp");
118          &ret();
119         &function_end_B($name);
120         }
121
122