• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/local/bin/perl
2
3# Normal is the
4# md5_block_x86(MD5_CTX *c, ULONG *X);
5# version, non-normal is the
6# md5_block_x86(MD5_CTX *c, ULONG *X,int blocks);
7
8$normal=0;
9
10$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
11push(@INC,"${dir}","${dir}../../../perlasm");
12require "x86asm.pl";
13
14$output=pop;
15open STDOUT,">$output";
16
17&asm_init($ARGV[0]);
18
19$A="eax";
20$B="ebx";
21$C="ecx";
22$D="edx";
23$tmp1="edi";
24$tmp2="ebp";
25$X="esi";
26
27# What we need to load into $tmp for the next round
28%Ltmp1=("R0",&Np($C), "R1",&Np($C), "R2",&Np($C), "R3",&Np($D));
29@xo=(
30 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,	# R0
31 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12,	# R1
32 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2,	# R2
33 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9,	# R3
34 );
35
36&md5_block("md5_block_asm_data_order");
37&asm_finish();
38
39close STDOUT or die "error closing STDOUT";
40
41sub Np
42	{
43	local($p)=@_;
44	local(%n)=($A,$D,$B,$A,$C,$B,$D,$C);
45	return($n{$p});
46	}
47
48sub R0
49	{
50	local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
51
52	&mov($tmp1,$C)  if $pos < 0;
53	&mov($tmp2,&DWP($xo[$ki]*4,$K,"",0)) if $pos < 0; # very first one
54
55	# body proper
56
57	&comment("R0 $ki");
58	&xor($tmp1,$d); # F function - part 2
59
60	&and($tmp1,$b); # F function - part 3
61	&lea($a,&DWP($t,$a,$tmp2,1));
62
63	&xor($tmp1,$d); # F function - part 4
64
65	&add($a,$tmp1);
66	&mov($tmp1,&Np($c)) if $pos < 1;	# next tmp1 for R0
67	&mov($tmp1,&Np($c)) if $pos == 1;	# next tmp1 for R1
68
69	&rotl($a,$s);
70
71	&mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2);
72
73	&add($a,$b);
74	}
75
76sub R1
77	{
78	local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
79
80	&comment("R1 $ki");
81
82	&lea($a,&DWP($t,$a,$tmp2,1));
83
84	&xor($tmp1,$b); # G function - part 2
85	&and($tmp1,$d); # G function - part 3
86
87	&mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2);
88	&xor($tmp1,$c);			# G function - part 4
89
90	&add($a,$tmp1);
91	&mov($tmp1,&Np($c)) if $pos < 1;	# G function - part 1
92	&mov($tmp1,&Np($c)) if $pos == 1;	# G function - part 1
93
94	&rotl($a,$s);
95
96	&add($a,$b);
97	}
98
99sub R2
100	{
101	local($n,$pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
102	# This one is different, only 3 logical operations
103
104if (($n & 1) == 0)
105	{
106	&comment("R2 $ki");
107	# make sure to do 'D' first, not 'B', else we clash with
108	# the last add from the previous round.
109
110	&xor($tmp1,$d); # H function - part 2
111
112	&xor($tmp1,$b); # H function - part 3
113	&lea($a,&DWP($t,$a,$tmp2,1));
114
115	&add($a,$tmp1);
116
117	&rotl($a,$s);
118
119	&mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0));
120	&mov($tmp1,&Np($c));
121	}
122else
123	{
124	&comment("R2 $ki");
125	# make sure to do 'D' first, not 'B', else we clash with
126	# the last add from the previous round.
127
128	&lea($a,&DWP($t,$a,$tmp2,1));
129
130	&add($b,$c);			# MOVED FORWARD
131	&xor($tmp1,$d); # H function - part 2
132
133	&xor($tmp1,$b); # H function - part 3
134	&mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2);
135
136	&add($a,$tmp1);
137	&mov($tmp1,&Np($c)) if $pos < 1;	# H function - part 1
138	&mov($tmp1,-1) if $pos == 1;		# I function - part 1
139
140	&rotl($a,$s);
141
142	&add($a,$b);
143	}
144	}
145
146sub R3
147	{
148	local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
149
150	&comment("R3 $ki");
151
152	# &not($tmp1)
153	&xor($tmp1,$d) if $pos < 0; 	# I function - part 2
154
155	&or($tmp1,$b);				# I function - part 3
156	&lea($a,&DWP($t,$a,$tmp2,1));
157
158	&xor($tmp1,$c); 			# I function - part 4
159	&mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0))	if $pos != 2; # load X/k value
160	&mov($tmp2,&wparam(0)) if $pos == 2;
161
162	&add($a,$tmp1);
163	&mov($tmp1,-1) if $pos < 1;	# H function - part 1
164	&add($K,64) if $pos >=1 && !$normal;
165
166	&rotl($a,$s);
167
168	&xor($tmp1,&Np($d)) if $pos <= 0; 	# I function - part = first time
169	&mov($tmp1,&DWP( 0,$tmp2,"",0)) if $pos > 0;
170	&add($a,$b);
171	}
172
173
174sub md5_block
175	{
176	local($name)=@_;
177
178	&function_begin_B($name,"",3);
179
180	# parameter 1 is the MD5_CTX structure.
181	# A	0
182	# B	4
183	# C	8
184	# D 	12
185
186	&push("esi");
187	 &push("edi");
188	&mov($tmp1,	&wparam(0)); # edi
189	 &mov($X,	&wparam(1)); # esi
190	&mov($C,	&wparam(2));
191	 &push("ebp");
192	&shl($C,	6);
193	&push("ebx");
194	 &add($C,	$X); # offset we end at
195	&sub($C,	64);
196	 &mov($A,	&DWP( 0,$tmp1,"",0));
197	&push($C);	# Put on the TOS
198	 &mov($B,	&DWP( 4,$tmp1,"",0));
199	&mov($C,	&DWP( 8,$tmp1,"",0));
200	 &mov($D,	&DWP(12,$tmp1,"",0));
201
202	&set_label("start") unless $normal;
203	&comment("");
204	&comment("R0 section");
205
206	&R0(-2,$A,$B,$C,$D,$X, 0, 7,0xd76aa478);
207	&R0( 0,$D,$A,$B,$C,$X, 1,12,0xe8c7b756);
208	&R0( 0,$C,$D,$A,$B,$X, 2,17,0x242070db);
209	&R0( 0,$B,$C,$D,$A,$X, 3,22,0xc1bdceee);
210	&R0( 0,$A,$B,$C,$D,$X, 4, 7,0xf57c0faf);
211	&R0( 0,$D,$A,$B,$C,$X, 5,12,0x4787c62a);
212	&R0( 0,$C,$D,$A,$B,$X, 6,17,0xa8304613);
213	&R0( 0,$B,$C,$D,$A,$X, 7,22,0xfd469501);
214	&R0( 0,$A,$B,$C,$D,$X, 8, 7,0x698098d8);
215	&R0( 0,$D,$A,$B,$C,$X, 9,12,0x8b44f7af);
216	&R0( 0,$C,$D,$A,$B,$X,10,17,0xffff5bb1);
217	&R0( 0,$B,$C,$D,$A,$X,11,22,0x895cd7be);
218	&R0( 0,$A,$B,$C,$D,$X,12, 7,0x6b901122);
219	&R0( 0,$D,$A,$B,$C,$X,13,12,0xfd987193);
220	&R0( 0,$C,$D,$A,$B,$X,14,17,0xa679438e);
221	&R0( 1,$B,$C,$D,$A,$X,15,22,0x49b40821);
222
223	&comment("");
224	&comment("R1 section");
225	&R1(-1,$A,$B,$C,$D,$X,16, 5,0xf61e2562);
226	&R1( 0,$D,$A,$B,$C,$X,17, 9,0xc040b340);
227	&R1( 0,$C,$D,$A,$B,$X,18,14,0x265e5a51);
228	&R1( 0,$B,$C,$D,$A,$X,19,20,0xe9b6c7aa);
229	&R1( 0,$A,$B,$C,$D,$X,20, 5,0xd62f105d);
230	&R1( 0,$D,$A,$B,$C,$X,21, 9,0x02441453);
231	&R1( 0,$C,$D,$A,$B,$X,22,14,0xd8a1e681);
232	&R1( 0,$B,$C,$D,$A,$X,23,20,0xe7d3fbc8);
233	&R1( 0,$A,$B,$C,$D,$X,24, 5,0x21e1cde6);
234	&R1( 0,$D,$A,$B,$C,$X,25, 9,0xc33707d6);
235	&R1( 0,$C,$D,$A,$B,$X,26,14,0xf4d50d87);
236	&R1( 0,$B,$C,$D,$A,$X,27,20,0x455a14ed);
237	&R1( 0,$A,$B,$C,$D,$X,28, 5,0xa9e3e905);
238	&R1( 0,$D,$A,$B,$C,$X,29, 9,0xfcefa3f8);
239	&R1( 0,$C,$D,$A,$B,$X,30,14,0x676f02d9);
240	&R1( 1,$B,$C,$D,$A,$X,31,20,0x8d2a4c8a);
241
242	&comment("");
243	&comment("R2 section");
244	&R2( 0,-1,$A,$B,$C,$D,$X,32, 4,0xfffa3942);
245	&R2( 1, 0,$D,$A,$B,$C,$X,33,11,0x8771f681);
246	&R2( 2, 0,$C,$D,$A,$B,$X,34,16,0x6d9d6122);
247	&R2( 3, 0,$B,$C,$D,$A,$X,35,23,0xfde5380c);
248	&R2( 4, 0,$A,$B,$C,$D,$X,36, 4,0xa4beea44);
249	&R2( 5, 0,$D,$A,$B,$C,$X,37,11,0x4bdecfa9);
250	&R2( 6, 0,$C,$D,$A,$B,$X,38,16,0xf6bb4b60);
251	&R2( 7, 0,$B,$C,$D,$A,$X,39,23,0xbebfbc70);
252	&R2( 8, 0,$A,$B,$C,$D,$X,40, 4,0x289b7ec6);
253	&R2( 9, 0,$D,$A,$B,$C,$X,41,11,0xeaa127fa);
254	&R2(10, 0,$C,$D,$A,$B,$X,42,16,0xd4ef3085);
255	&R2(11, 0,$B,$C,$D,$A,$X,43,23,0x04881d05);
256	&R2(12, 0,$A,$B,$C,$D,$X,44, 4,0xd9d4d039);
257	&R2(13, 0,$D,$A,$B,$C,$X,45,11,0xe6db99e5);
258	&R2(14, 0,$C,$D,$A,$B,$X,46,16,0x1fa27cf8);
259	&R2(15, 1,$B,$C,$D,$A,$X,47,23,0xc4ac5665);
260
261	&comment("");
262	&comment("R3 section");
263	&R3(-1,$A,$B,$C,$D,$X,48, 6,0xf4292244);
264	&R3( 0,$D,$A,$B,$C,$X,49,10,0x432aff97);
265	&R3( 0,$C,$D,$A,$B,$X,50,15,0xab9423a7);
266	&R3( 0,$B,$C,$D,$A,$X,51,21,0xfc93a039);
267	&R3( 0,$A,$B,$C,$D,$X,52, 6,0x655b59c3);
268	&R3( 0,$D,$A,$B,$C,$X,53,10,0x8f0ccc92);
269	&R3( 0,$C,$D,$A,$B,$X,54,15,0xffeff47d);
270	&R3( 0,$B,$C,$D,$A,$X,55,21,0x85845dd1);
271	&R3( 0,$A,$B,$C,$D,$X,56, 6,0x6fa87e4f);
272	&R3( 0,$D,$A,$B,$C,$X,57,10,0xfe2ce6e0);
273	&R3( 0,$C,$D,$A,$B,$X,58,15,0xa3014314);
274	&R3( 0,$B,$C,$D,$A,$X,59,21,0x4e0811a1);
275	&R3( 0,$A,$B,$C,$D,$X,60, 6,0xf7537e82);
276	&R3( 0,$D,$A,$B,$C,$X,61,10,0xbd3af235);
277	&R3( 0,$C,$D,$A,$B,$X,62,15,0x2ad7d2bb);
278	&R3( 2,$B,$C,$D,$A,$X,63,21,0xeb86d391);
279
280	# &mov($tmp2,&wparam(0));	# done in the last R3
281	# &mov($tmp1,	&DWP( 0,$tmp2,"",0)); # done is the last R3
282
283	&add($A,$tmp1);
284	 &mov($tmp1,	&DWP( 4,$tmp2,"",0));
285
286	&add($B,$tmp1);
287	&mov($tmp1,	&DWP( 8,$tmp2,"",0));
288
289	&add($C,$tmp1);
290	&mov($tmp1,	&DWP(12,$tmp2,"",0));
291
292	&add($D,$tmp1);
293	&mov(&DWP( 0,$tmp2,"",0),$A);
294
295	&mov(&DWP( 4,$tmp2,"",0),$B);
296	&mov($tmp1,&swtmp(0)) unless $normal;
297
298	&mov(&DWP( 8,$tmp2,"",0),$C);
299	 &mov(&DWP(12,$tmp2,"",0),$D);
300
301	&cmp($tmp1,$X) unless $normal;			# check count
302	 &jae(&label("start")) unless $normal;
303
304	&pop("eax"); # pop the temp variable off the stack
305	 &pop("ebx");
306	&pop("ebp");
307	 &pop("edi");
308	&pop("esi");
309	 &ret();
310	&function_end_B($name);
311	}
312
313