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