• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/perl -w
2#
3# MD5 optimized for AMD64.
4#
5# Author: Marc Bevand <bevand_m (at) epita.fr>
6# Licence: I hereby disclaim the copyright on this code and place it
7# in the public domain.
8#
9
10use strict;
11
12my $code;
13
14# round1_step() does:
15#   dst = x + ((dst + F(x,y,z) + X[k] + T_i) <<< s)
16#   %r10d = X[k_next]
17#   %r11d = z' (copy of z for the next step)
18# Each round1_step() takes about 5.3 clocks (9 instructions, 1.7 IPC)
19sub round1_step
20{
21    my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
22    $code .= " mov	0*4(%rsi),	%r10d		/* (NEXT STEP) X[0] */\n" if ($pos == -1);
23    $code .= " mov	%edx,		%r11d		/* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
24    $code .= <<EOF;
25	xor	$y,		%r11d		/* y ^ ... */
26	lea	$T_i($dst,%r10d),$dst		/* Const + dst + ... */
27	and	$x,		%r11d		/* x & ... */
28	xor	$z,		%r11d		/* z ^ ... */
29	mov	$k_next*4(%rsi),%r10d		/* (NEXT STEP) X[$k_next] */
30	add	%r11d,		$dst		/* dst += ... */
31	rol	\$$s,		$dst		/* dst <<< s */
32	mov	$y,		%r11d		/* (NEXT STEP) z' = $y */
33	add	$x,		$dst		/* dst += x */
34EOF
35}
36
37# round2_step() does:
38#   dst = x + ((dst + G(x,y,z) + X[k] + T_i) <<< s)
39#   %r10d = X[k_next]
40#   %r11d = z' (copy of z for the next step)
41#   %r12d = z' (copy of z for the next step)
42# Each round2_step() takes about 5.4 clocks (11 instructions, 2.0 IPC)
43sub round2_step
44{
45    my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
46    $code .= " mov	1*4(%rsi),	%r10d		/* (NEXT STEP) X[1] */\n" if ($pos == -1);
47    $code .= " mov	%edx,		%r11d		/* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
48    $code .= " mov	%edx,		%r12d		/* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
49    $code .= <<EOF;
50	not	%r11d				/* not z */
51	lea	$T_i($dst,%r10d),$dst		/* Const + dst + ... */
52	and	$x,		%r12d		/* x & z */
53	and	$y,		%r11d		/* y & (not z) */
54	mov	$k_next*4(%rsi),%r10d		/* (NEXT STEP) X[$k_next] */
55	or	%r11d,		%r12d		/* (y & (not z)) | (x & z) */
56	mov	$y,		%r11d		/* (NEXT STEP) z' = $y */
57	add	%r12d,		$dst		/* dst += ... */
58	mov	$y,		%r12d		/* (NEXT STEP) z' = $y */
59	rol	\$$s,		$dst		/* dst <<< s */
60	add	$x,		$dst		/* dst += x */
61EOF
62}
63
64# round3_step() does:
65#   dst = x + ((dst + H(x,y,z) + X[k] + T_i) <<< s)
66#   %r10d = X[k_next]
67#   %r11d = y' (copy of y for the next step)
68# Each round3_step() takes about 4.2 clocks (8 instructions, 1.9 IPC)
69sub round3_step
70{
71    my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
72    $code .= " mov	5*4(%rsi),	%r10d		/* (NEXT STEP) X[5] */\n" if ($pos == -1);
73    $code .= " mov	%ecx,		%r11d		/* (NEXT STEP) y' = %ecx */\n" if ($pos == -1);
74    $code .= <<EOF;
75	lea	$T_i($dst,%r10d),$dst		/* Const + dst + ... */
76	mov	$k_next*4(%rsi),%r10d		/* (NEXT STEP) X[$k_next] */
77	xor	$z,		%r11d		/* z ^ ... */
78	xor	$x,		%r11d		/* x ^ ... */
79	add	%r11d,		$dst		/* dst += ... */
80	rol	\$$s,		$dst		/* dst <<< s */
81	mov	$x,		%r11d		/* (NEXT STEP) y' = $x */
82	add	$x,		$dst		/* dst += x */
83EOF
84}
85
86# round4_step() does:
87#   dst = x + ((dst + I(x,y,z) + X[k] + T_i) <<< s)
88#   %r10d = X[k_next]
89#   %r11d = not z' (copy of not z for the next step)
90# Each round4_step() takes about 5.2 clocks (9 instructions, 1.7 IPC)
91sub round4_step
92{
93    my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
94    $code .= " mov	0*4(%rsi),	%r10d		/* (NEXT STEP) X[0] */\n" if ($pos == -1);
95    $code .= " mov	\$0xffffffff,	%r11d\n" if ($pos == -1);
96    $code .= " xor	%edx,		%r11d		/* (NEXT STEP) not z' = not %edx*/\n"
97    if ($pos == -1);
98    $code .= <<EOF;
99	lea	$T_i($dst,%r10d),$dst		/* Const + dst + ... */
100	or	$x,		%r11d		/* x | ... */
101	xor	$y,		%r11d		/* y ^ ... */
102	add	%r11d,		$dst		/* dst += ... */
103	mov	$k_next*4(%rsi),%r10d		/* (NEXT STEP) X[$k_next] */
104	mov	\$0xffffffff,	%r11d
105	rol	\$$s,		$dst		/* dst <<< s */
106	xor	$y,		%r11d		/* (NEXT STEP) not z' = not $y */
107	add	$x,		$dst		/* dst += x */
108EOF
109}
110
111no warnings qw(uninitialized);
112my $flavour = shift;
113my $output  = shift;
114if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
115
116my $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
117
118$0 =~ m/(.*[\/\\])[^\/\\]+$/; my $dir=$1; my $xlate;
119( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
120( $xlate="${dir}../../../perlasm/x86_64-xlate.pl" and -f $xlate) or
121die "can't locate x86_64-xlate.pl";
122
123open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
124*STDOUT=*OUT;
125
126$code .= <<EOF;
127.text
128.align 16
129
130.globl md5_block_asm_data_order
131.type md5_block_asm_data_order,\@function,3
132md5_block_asm_data_order:
133.cfi_startproc
134	_CET_ENDBR
135	push	%rbp
136.cfi_push	rbp
137	push	%rbx
138.cfi_push	rbx
139	push	%r12
140.cfi_push	r12
141	push	%r14
142.cfi_push	r14
143	push	%r15
144.cfi_push	r15
145.Lprologue:
146
147	# rdi = arg #1 (ctx, MD5_CTX pointer)
148	# rsi = arg #2 (ptr, data pointer)
149	# rdx = arg #3 (nbr, number of 16-word blocks to process)
150	mov	%rdi,		%rbp	# rbp = ctx
151	shl	\$6,		%rdx	# rdx = nbr in bytes
152	lea	(%rsi,%rdx),	%rdi	# rdi = end
153	mov	0*4(%rbp),	%eax	# eax = ctx->A
154	mov	1*4(%rbp),	%ebx	# ebx = ctx->B
155	mov	2*4(%rbp),	%ecx	# ecx = ctx->C
156	mov	3*4(%rbp),	%edx	# edx = ctx->D
157	# end is 'rdi'
158	# ptr is 'rsi'
159	# A is 'eax'
160	# B is 'ebx'
161	# C is 'ecx'
162	# D is 'edx'
163
164	cmp	%rdi,		%rsi		# cmp end with ptr
165	je	.Lend				# jmp if ptr == end
166
167	# BEGIN of loop over 16-word blocks
168.Lloop:	# save old values of A, B, C, D
169	mov	%eax,		%r8d
170	mov	%ebx,		%r9d
171	mov	%ecx,		%r14d
172	mov	%edx,		%r15d
173EOF
174round1_step(-1,'%eax','%ebx','%ecx','%edx', '1','0xd76aa478', '7');
175round1_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xe8c7b756','12');
176round1_step( 0,'%ecx','%edx','%eax','%ebx', '3','0x242070db','17');
177round1_step( 0,'%ebx','%ecx','%edx','%eax', '4','0xc1bdceee','22');
178round1_step( 0,'%eax','%ebx','%ecx','%edx', '5','0xf57c0faf', '7');
179round1_step( 0,'%edx','%eax','%ebx','%ecx', '6','0x4787c62a','12');
180round1_step( 0,'%ecx','%edx','%eax','%ebx', '7','0xa8304613','17');
181round1_step( 0,'%ebx','%ecx','%edx','%eax', '8','0xfd469501','22');
182round1_step( 0,'%eax','%ebx','%ecx','%edx', '9','0x698098d8', '7');
183round1_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8b44f7af','12');
184round1_step( 0,'%ecx','%edx','%eax','%ebx','11','0xffff5bb1','17');
185round1_step( 0,'%ebx','%ecx','%edx','%eax','12','0x895cd7be','22');
186round1_step( 0,'%eax','%ebx','%ecx','%edx','13','0x6b901122', '7');
187round1_step( 0,'%edx','%eax','%ebx','%ecx','14','0xfd987193','12');
188round1_step( 0,'%ecx','%edx','%eax','%ebx','15','0xa679438e','17');
189round1_step( 1,'%ebx','%ecx','%edx','%eax', '0','0x49b40821','22');
190
191round2_step(-1,'%eax','%ebx','%ecx','%edx', '6','0xf61e2562', '5');
192round2_step( 0,'%edx','%eax','%ebx','%ecx','11','0xc040b340', '9');
193round2_step( 0,'%ecx','%edx','%eax','%ebx', '0','0x265e5a51','14');
194round2_step( 0,'%ebx','%ecx','%edx','%eax', '5','0xe9b6c7aa','20');
195round2_step( 0,'%eax','%ebx','%ecx','%edx','10','0xd62f105d', '5');
196round2_step( 0,'%edx','%eax','%ebx','%ecx','15', '0x2441453', '9');
197round2_step( 0,'%ecx','%edx','%eax','%ebx', '4','0xd8a1e681','14');
198round2_step( 0,'%ebx','%ecx','%edx','%eax', '9','0xe7d3fbc8','20');
199round2_step( 0,'%eax','%ebx','%ecx','%edx','14','0x21e1cde6', '5');
200round2_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xc33707d6', '9');
201round2_step( 0,'%ecx','%edx','%eax','%ebx', '8','0xf4d50d87','14');
202round2_step( 0,'%ebx','%ecx','%edx','%eax','13','0x455a14ed','20');
203round2_step( 0,'%eax','%ebx','%ecx','%edx', '2','0xa9e3e905', '5');
204round2_step( 0,'%edx','%eax','%ebx','%ecx', '7','0xfcefa3f8', '9');
205round2_step( 0,'%ecx','%edx','%eax','%ebx','12','0x676f02d9','14');
206round2_step( 1,'%ebx','%ecx','%edx','%eax', '0','0x8d2a4c8a','20');
207
208round3_step(-1,'%eax','%ebx','%ecx','%edx', '8','0xfffa3942', '4');
209round3_step( 0,'%edx','%eax','%ebx','%ecx','11','0x8771f681','11');
210round3_step( 0,'%ecx','%edx','%eax','%ebx','14','0x6d9d6122','16');
211round3_step( 0,'%ebx','%ecx','%edx','%eax', '1','0xfde5380c','23');
212round3_step( 0,'%eax','%ebx','%ecx','%edx', '4','0xa4beea44', '4');
213round3_step( 0,'%edx','%eax','%ebx','%ecx', '7','0x4bdecfa9','11');
214round3_step( 0,'%ecx','%edx','%eax','%ebx','10','0xf6bb4b60','16');
215round3_step( 0,'%ebx','%ecx','%edx','%eax','13','0xbebfbc70','23');
216round3_step( 0,'%eax','%ebx','%ecx','%edx', '0','0x289b7ec6', '4');
217round3_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xeaa127fa','11');
218round3_step( 0,'%ecx','%edx','%eax','%ebx', '6','0xd4ef3085','16');
219round3_step( 0,'%ebx','%ecx','%edx','%eax', '9', '0x4881d05','23');
220round3_step( 0,'%eax','%ebx','%ecx','%edx','12','0xd9d4d039', '4');
221round3_step( 0,'%edx','%eax','%ebx','%ecx','15','0xe6db99e5','11');
222round3_step( 0,'%ecx','%edx','%eax','%ebx', '2','0x1fa27cf8','16');
223round3_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xc4ac5665','23');
224
225round4_step(-1,'%eax','%ebx','%ecx','%edx', '7','0xf4292244', '6');
226round4_step( 0,'%edx','%eax','%ebx','%ecx','14','0x432aff97','10');
227round4_step( 0,'%ecx','%edx','%eax','%ebx', '5','0xab9423a7','15');
228round4_step( 0,'%ebx','%ecx','%edx','%eax','12','0xfc93a039','21');
229round4_step( 0,'%eax','%ebx','%ecx','%edx', '3','0x655b59c3', '6');
230round4_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8f0ccc92','10');
231round4_step( 0,'%ecx','%edx','%eax','%ebx', '1','0xffeff47d','15');
232round4_step( 0,'%ebx','%ecx','%edx','%eax', '8','0x85845dd1','21');
233round4_step( 0,'%eax','%ebx','%ecx','%edx','15','0x6fa87e4f', '6');
234round4_step( 0,'%edx','%eax','%ebx','%ecx', '6','0xfe2ce6e0','10');
235round4_step( 0,'%ecx','%edx','%eax','%ebx','13','0xa3014314','15');
236round4_step( 0,'%ebx','%ecx','%edx','%eax', '4','0x4e0811a1','21');
237round4_step( 0,'%eax','%ebx','%ecx','%edx','11','0xf7537e82', '6');
238round4_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xbd3af235','10');
239round4_step( 0,'%ecx','%edx','%eax','%ebx', '9','0x2ad7d2bb','15');
240round4_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xeb86d391','21');
241$code .= <<EOF;
242	# add old values of A, B, C, D
243	add	%r8d,	%eax
244	add	%r9d,	%ebx
245	add	%r14d,	%ecx
246	add	%r15d,	%edx
247
248	# loop control
249	add	\$64,		%rsi		# ptr += 64
250	cmp	%rdi,		%rsi		# cmp end with ptr
251	jb	.Lloop				# jmp if ptr < end
252	# END of loop over 16-word blocks
253
254.Lend:
255	mov	%eax,		0*4(%rbp)	# ctx->A = A
256	mov	%ebx,		1*4(%rbp)	# ctx->B = B
257	mov	%ecx,		2*4(%rbp)	# ctx->C = C
258	mov	%edx,		3*4(%rbp)	# ctx->D = D
259
260	mov	(%rsp),%r15
261.cfi_restore r15
262	mov	8(%rsp),%r14
263.cfi_restore r14
264	mov	16(%rsp),%r12
265.cfi_restore r12
266	mov	24(%rsp),%rbx
267.cfi_restore rbx
268	mov	32(%rsp),%rbp
269.cfi_restore rbp
270	add	\$40,%rsp
271.cfi_adjust_cfa_offset	-40
272.Lepilogue:
273	ret
274.cfi_endproc
275.size md5_block_asm_data_order,.-md5_block_asm_data_order
276EOF
277
278# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
279#		CONTEXT *context,DISPATCHER_CONTEXT *disp)
280if ($win64) {
281my $rec="%rcx";
282my $frame="%rdx";
283my $context="%r8";
284my $disp="%r9";
285
286$code.=<<___;
287.extern	__imp_RtlVirtualUnwind
288.type	se_handler,\@abi-omnipotent
289.align	16
290se_handler:
291	push	%rsi
292	push	%rdi
293	push	%rbx
294	push	%rbp
295	push	%r12
296	push	%r13
297	push	%r14
298	push	%r15
299	pushfq
300	sub	\$64,%rsp
301
302	mov	120($context),%rax	# pull context->Rax
303	mov	248($context),%rbx	# pull context->Rip
304
305	lea	.Lprologue(%rip),%r10
306	cmp	%r10,%rbx		# context->Rip<.Lprologue
307	jb	.Lin_prologue
308
309	mov	152($context),%rax	# pull context->Rsp
310
311	lea	.Lepilogue(%rip),%r10
312	cmp	%r10,%rbx		# context->Rip>=.Lepilogue
313	jae	.Lin_prologue
314
315	lea	40(%rax),%rax
316
317	mov	-8(%rax),%rbp
318	mov	-16(%rax),%rbx
319	mov	-24(%rax),%r12
320	mov	-32(%rax),%r14
321	mov	-40(%rax),%r15
322	mov	%rbx,144($context)	# restore context->Rbx
323	mov	%rbp,160($context)	# restore context->Rbp
324	mov	%r12,216($context)	# restore context->R12
325	mov	%r14,232($context)	# restore context->R14
326	mov	%r15,240($context)	# restore context->R15
327
328.Lin_prologue:
329	mov	8(%rax),%rdi
330	mov	16(%rax),%rsi
331	mov	%rax,152($context)	# restore context->Rsp
332	mov	%rsi,168($context)	# restore context->Rsi
333	mov	%rdi,176($context)	# restore context->Rdi
334
335	mov	40($disp),%rdi		# disp->ContextRecord
336	mov	$context,%rsi		# context
337	mov	\$154,%ecx		# sizeof(CONTEXT)
338	.long	0xa548f3fc		# cld; rep movsq
339
340	mov	$disp,%rsi
341	xor	%rcx,%rcx		# arg1, UNW_FLAG_NHANDLER
342	mov	8(%rsi),%rdx		# arg2, disp->ImageBase
343	mov	0(%rsi),%r8		# arg3, disp->ControlPc
344	mov	16(%rsi),%r9		# arg4, disp->FunctionEntry
345	mov	40(%rsi),%r10		# disp->ContextRecord
346	lea	56(%rsi),%r11		# &disp->HandlerData
347	lea	24(%rsi),%r12		# &disp->EstablisherFrame
348	mov	%r10,32(%rsp)		# arg5
349	mov	%r11,40(%rsp)		# arg6
350	mov	%r12,48(%rsp)		# arg7
351	mov	%rcx,56(%rsp)		# arg8, (NULL)
352	call	*__imp_RtlVirtualUnwind(%rip)
353
354	mov	\$1,%eax		# ExceptionContinueSearch
355	add	\$64,%rsp
356	popfq
357	pop	%r15
358	pop	%r14
359	pop	%r13
360	pop	%r12
361	pop	%rbp
362	pop	%rbx
363	pop	%rdi
364	pop	%rsi
365	ret
366.size	se_handler,.-se_handler
367
368.section	.pdata
369.align	4
370	.rva	.LSEH_begin_md5_block_asm_data_order
371	.rva	.LSEH_end_md5_block_asm_data_order
372	.rva	.LSEH_info_md5_block_asm_data_order
373
374.section	.xdata
375.align	8
376.LSEH_info_md5_block_asm_data_order:
377	.byte	9,0,0,0
378	.rva	se_handler
379___
380}
381
382print $code;
383
384close STDOUT or die "error closing STDOUT: $!";
385