xref: /titanic_50/usr/src/common/crypto/md5/amd64/md5_amd64.pl (revision fdf4286765e129590dce97b37d12188bf7000b58)
1#!/usr/bin/perl -w
2use strict;
3my $code .= <<EOF;
4#if !defined(lint) && !defined(__lint)
5/
6/ MD5 optimized for AMD64.
7/
8/ Author: Marc Bevand <bevand_m (at) epita.fr>
9/ Licence: I hereby disclaim the copyright on this code and place it
10/ in the public domain.
11/
12
13	.ident	"%Z%%M%	%I%	%E% SMI"
14
15/
16/ The following is Marc Bevand's MD5 implementation optimized for
17/ AMD64.  It has been lifted intact, except for changing the comment
18/ character, adding comments, and a kludge to generate valid "lea"
19/ instructions that are not generated by the Solaris "as" assembler
20/ (CR 6628627).
21/
22/ typedef struct {
23/	uint32_t state[4];	/* state (ABCD) */
24/	uint32_t count[2];	/* number of bits, modulo 2^64 (lsb first) */
25/	union	{
26/		uint8_t		buf8[64];	/* undigested input */
27/		uint32_t	buf32[16];	/* realigned input */
28/		} buf_un;
29/	} MD5_CTX;
30/
31/ void md5_block_asm_host_order(MD5_CTX *ctx, const void *inpp,
32/        unsigned int input_length_in_blocks);
33/
34/ Registers used:
35/	rax  A		r8  old A
36/	rbx  B		r9  old B
37/	rcx  C		r10 tmp
38/	rdx  D		r11 tmp
39/	rsi  ptr	r12 tmp
40/	rdi  end	r13 -
41/	rbp  -		r14 old C
42/	rsp  stack	r15 old D
43/
44
45EOF
46
47
48sub lea_offset_register_r10d_ebx
49# Workaround for a Solaris "gas" assembler bug where compiling the source
50# errors out and does not generate a valid "lea" instruction.  Specifically,
51#	lea OFFSET(REGISTER,%r10d),REGISTER
52#
53# For Solaris as, "as -a32" must be used to compile this.
54# For Solaris gas 2.15, this errors out with this message:
55# Error: `0xf57c0faf(%eax,%r10d)' is not a valid 64 bit base/index expression
56# This should be fixed in Solaris gas 2.16.
57# It assembles with the Linux "as --64" gas 2.17 assembler and runs OK.
58#
59# For the ONBLD NV tools, the aw wrapper script fails when -a32 is used:
60# /ws/onnv-tools/onbld/bin/i386/aw -xarch=amd64 -P -a32 -o lea.o lea.s
61# aw: as->gas mapping failed at or near arg '-a32'
62#
63# For more information, see CRs 6644870 and 6628627.
64# Note2: Solaris "as" uses "/" for comments; Linux "as" uses "#" for comments.
65{
66	use Switch;
67	my ($offset, $register, $comment) = @_;
68
69	# Failed "lea" instruction.
70	# This instruction errors out from the Solaris as assembler.
71	# It assembles with the Linux "as --64" assembler and runs OK.
72	$code .= "	/ lea $offset($register,%r10d),$register"
73						. "	$comment\n";
74
75	# Workaround #1 (not used)
76	# One workaround is to generate two "add" instructions that are
77	# functionally equivalent to "lea."  The problem is this workaround
78	# is about 4.5% slower than a lea, so is not used.
79	#$code .= "	add	%r10d,$register\n";
80	#$code .= "	add	\&0x%0x,$offset\n";
81
82	# Workaround #2 (used)
83	# This workaround hand-generates hex machine code for lea.
84	$code .= "	/ Solaris as assembly bug CR 6628627 errors out for\n";
85	$code .= "	/ the above, so we specify the machine code in hex:\n";
86	$code .= "	.byte	0x67,0x42,0x8d	/ lea offset(reg,%r10d),reg\n";
87
88	switch ($register) {
89	case "%eax"	{ $code .= "	.byte	0x84,0x10	/ reg=%eax\n"; }
90	case "%ebx"	{ $code .= "	.byte	0x9c,0x13	/ reg=%ebx\n"; }
91	case "%ecx"	{ $code .= "	.byte	0x8c,0x11	/ reg=%ecx\n"; }
92	case "%edx"	{ $code .= "	.byte	0x94,0x12	/ reg=%edx\n"; }
93	else		{ $code .= "ERROR: unknown register $register\n"; }
94	}
95
96	$code .= "	.long	$offset	/ offset\n";
97}
98
99
100
101# round1_step() does:
102#   dst = x + ((dst + F(x,y,z) + X[k] + T_i) <<< s)
103#   %r10d = X[k_next]
104#   %r11d = z' (copy of z for the next step)
105# Each round1_step() takes about 5.3 clocks (9 instructions, 1.7 IPC)
106sub round1_step
107{
108    my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
109    $code .= "	mov	0*4(%rsi),	%r10d		/* (NEXT STEP) X[0] */\n" if ($pos == -1);
110    $code .= "	mov	%edx,		%r11d		/* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
111    $code .= "	xor	$y,		%r11d		/* y ^ ... */\n";
112	#lea	$T_i($dst,%r10d),$dst		/* Const + dst + ... */
113	lea_offset_register_r10d_ebx($T_i, $dst, "/* Const + dst + ... */");
114    $code .= <<EOF;
115	and	$x,		%r11d		/* x & ... */
116	xor	$z,		%r11d		/* z ^ ... */
117	mov	$k_next*4(%rsi),%r10d		/* (NEXT STEP) X[$k_next] */
118	add	%r11d,		$dst		/* dst += ... */
119	rol	\$$s,		$dst		/* dst <<< s */
120	mov	$y,		%r11d		/* (NEXT STEP) z' = $y */
121	add	$x,		$dst		/* dst += x */
122EOF
123}
124
125# round2_step() does:
126#   dst = x + ((dst + G(x,y,z) + X[k] + T_i) <<< s)
127#   %r10d = X[k_next]
128#   %r11d = z' (copy of z for the next step)
129#   %r12d = z' (copy of z for the next step)
130# Each round2_step() takes about 5.4 clocks (11 instructions, 2.0 IPC)
131sub round2_step
132{
133    my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
134    $code .= "	mov	1*4(%rsi),	%r10d		/* (NEXT STEP) X[1] */\n" if ($pos == -1);
135    $code .= "	mov	%edx,		%r11d		/* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
136    $code .= "	mov	%edx,		%r12d		/* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
137    $code .= "	not	%r11d				/* not z */\n";
138	#lea	$T_i($dst,%r10d),$dst		/* Const + dst + ... */
139	lea_offset_register_r10d_ebx($T_i, $dst, "/* Const + dst + ... */");
140    $code .= <<EOF;
141	and	$x,		%r12d		/* x & z */
142	and	$y,		%r11d		/* y & (not z) */
143	mov	$k_next*4(%rsi),%r10d		/* (NEXT STEP) X[$k_next] */
144	or	%r11d,		%r12d		/* (y & (not z)) | (x & z) */
145	mov	$y,		%r11d		/* (NEXT STEP) z' = $y */
146	add	%r12d,		$dst		/* dst += ... */
147	mov	$y,		%r12d		/* (NEXT STEP) z' = $y */
148	rol	\$$s,		$dst		/* dst <<< s */
149	add	$x,		$dst		/* dst += x */
150EOF
151}
152
153# round3_step() does:
154#   dst = x + ((dst + H(x,y,z) + X[k] + T_i) <<< s)
155#   %r10d = X[k_next]
156#   %r11d = y' (copy of y for the next step)
157# Each round3_step() takes about 4.2 clocks (8 instructions, 1.9 IPC)
158sub round3_step
159{
160    my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
161    $code .= "	mov	5*4(%rsi),	%r10d		/* (NEXT STEP) X[5] */\n" if ($pos == -1);
162    $code .= "	mov	%ecx,		%r11d		/* (NEXT STEP) y' = %ecx */\n" if ($pos == -1);
163	#lea	$T_i($dst,%r10d),$dst		/* Const + dst + ... */
164	lea_offset_register_r10d_ebx($T_i, $dst, "/* Const + dst + ... */");
165    $code .= <<EOF;
166	mov	$k_next*4(%rsi),%r10d		/* (NEXT STEP) X[$k_next] */
167	xor	$z,		%r11d		/* z ^ ... */
168	xor	$x,		%r11d		/* x ^ ... */
169	add	%r11d,		$dst		/* dst += ... */
170	rol	\$$s,		$dst		/* dst <<< s */
171	mov	$x,		%r11d		/* (NEXT STEP) y' = $x */
172	add	$x,		$dst		/* dst += x */
173EOF
174}
175
176# round4_step() does:
177#   dst = x + ((dst + I(x,y,z) + X[k] + T_i) <<< s)
178#   %r10d = X[k_next]
179#   %r11d = not z' (copy of not z for the next step)
180# Each round4_step() takes about 5.2 clocks (9 instructions, 1.7 IPC)
181sub round4_step
182{
183    my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
184    $code .= "	mov	0*4(%rsi),	%r10d		/* (NEXT STEP) X[0] */\n" if ($pos == -1);
185    $code .= "	mov	\$0xffffffff,	%r11d\n" if ($pos == -1);
186    $code .= "	xor	%edx,		%r11d		/* (NEXT STEP) not z' = not %edx*/\n"
187    if ($pos == -1);
188	#lea	$T_i($dst,%r10d),$dst		/* Const + dst + ... */
189	lea_offset_register_r10d_ebx($T_i, $dst, "/* Const + dst + ... */");
190    $code .= <<EOF;
191	or	$x,		%r11d		/* x | ... */
192	xor	$y,		%r11d		/* y ^ ... */
193	add	%r11d,		$dst		/* dst += ... */
194	mov	$k_next*4(%rsi),%r10d		/* (NEXT STEP) X[$k_next] */
195	mov	\$0xffffffff,	%r11d
196	rol	\$$s,		$dst		/* dst <<< s */
197	xor	$y,		%r11d		/* (NEXT STEP) not z' = not $y */
198	add	$x,		$dst		/* dst += x */
199EOF
200}
201
202
203#
204# Execution begins here.
205#
206
207my $output = shift;
208open STDOUT,">$output" or die "can't open $output: $!";
209
210$code .= <<EOF;
211
212#include <sys/asm_linkage.h>
213
214	ENTRY_NP(md5_block_asm_host_order)
215	push	%rbp
216	push	%rbx
217	push	%r12
218	push	%r13
219	push	%r14
220	push	%r15
221
222	/ rdi = arg #1 (ctx, MD5_CTX pointer)
223	/ rsi = arg #2 (ptr, data pointer)
224	/ rdx = arg #3 (nbr, number of 64-byte blocks to process)
225	mov	%rdi,		%rbp	/ rbp = ctx
226	shl	\$6,		%rdx	/ rdx = nbr in bytes
227	lea	(%rsi,%rdx),	%rdi	/ rdi = end
228	mov	0*4(%rbp),	%eax	/ eax = ctx->A
229	mov	1*4(%rbp),	%ebx	/ ebx = ctx->B
230	mov	2*4(%rbp),	%ecx	/ ecx = ctx->C
231	mov	3*4(%rbp),	%edx	/ edx = ctx->D
232	push	%rbp			/ save ctx
233	/ end is 'rdi'
234	/ ptr is 'rsi'
235	/ A is 'eax'
236	/ B is 'ebx'
237	/ C is 'ecx'
238	/ D is 'edx'
239
240	cmp	%rdi,		%rsi		/ cmp end with ptr
241	je	1f				/ jmp if ptr == end
242
243	/ BEGIN of loop over 64-byte blocks
2442:	/ save old values of A, B, C, D
245	mov	%eax,		%r8d
246	mov	%ebx,		%r9d
247	mov	%ecx,		%r14d
248	mov	%edx,		%r15d
249EOF
250round1_step(-1,'%eax','%ebx','%ecx','%edx', '1','0xd76aa478', '7');
251round1_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xe8c7b756','12');
252round1_step( 0,'%ecx','%edx','%eax','%ebx', '3','0x242070db','17');
253round1_step( 0,'%ebx','%ecx','%edx','%eax', '4','0xc1bdceee','22');
254round1_step( 0,'%eax','%ebx','%ecx','%edx', '5','0xf57c0faf', '7');
255round1_step( 0,'%edx','%eax','%ebx','%ecx', '6','0x4787c62a','12');
256round1_step( 0,'%ecx','%edx','%eax','%ebx', '7','0xa8304613','17');
257round1_step( 0,'%ebx','%ecx','%edx','%eax', '8','0xfd469501','22');
258round1_step( 0,'%eax','%ebx','%ecx','%edx', '9','0x698098d8', '7');
259round1_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8b44f7af','12');
260round1_step( 0,'%ecx','%edx','%eax','%ebx','11','0xffff5bb1','17');
261round1_step( 0,'%ebx','%ecx','%edx','%eax','12','0x895cd7be','22');
262round1_step( 0,'%eax','%ebx','%ecx','%edx','13','0x6b901122', '7');
263round1_step( 0,'%edx','%eax','%ebx','%ecx','14','0xfd987193','12');
264round1_step( 0,'%ecx','%edx','%eax','%ebx','15','0xa679438e','17');
265round1_step( 1,'%ebx','%ecx','%edx','%eax', '0','0x49b40821','22');
266
267round2_step(-1,'%eax','%ebx','%ecx','%edx', '6','0xf61e2562', '5');
268round2_step( 0,'%edx','%eax','%ebx','%ecx','11','0xc040b340', '9');
269round2_step( 0,'%ecx','%edx','%eax','%ebx', '0','0x265e5a51','14');
270round2_step( 0,'%ebx','%ecx','%edx','%eax', '5','0xe9b6c7aa','20');
271round2_step( 0,'%eax','%ebx','%ecx','%edx','10','0xd62f105d', '5');
272round2_step( 0,'%edx','%eax','%ebx','%ecx','15', '0x2441453', '9');
273round2_step( 0,'%ecx','%edx','%eax','%ebx', '4','0xd8a1e681','14');
274round2_step( 0,'%ebx','%ecx','%edx','%eax', '9','0xe7d3fbc8','20');
275round2_step( 0,'%eax','%ebx','%ecx','%edx','14','0x21e1cde6', '5');
276round2_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xc33707d6', '9');
277round2_step( 0,'%ecx','%edx','%eax','%ebx', '8','0xf4d50d87','14');
278round2_step( 0,'%ebx','%ecx','%edx','%eax','13','0x455a14ed','20');
279round2_step( 0,'%eax','%ebx','%ecx','%edx', '2','0xa9e3e905', '5');
280round2_step( 0,'%edx','%eax','%ebx','%ecx', '7','0xfcefa3f8', '9');
281round2_step( 0,'%ecx','%edx','%eax','%ebx','12','0x676f02d9','14');
282round2_step( 1,'%ebx','%ecx','%edx','%eax', '0','0x8d2a4c8a','20');
283
284round3_step(-1,'%eax','%ebx','%ecx','%edx', '8','0xfffa3942', '4');
285round3_step( 0,'%edx','%eax','%ebx','%ecx','11','0x8771f681','11');
286round3_step( 0,'%ecx','%edx','%eax','%ebx','14','0x6d9d6122','16');
287round3_step( 0,'%ebx','%ecx','%edx','%eax', '1','0xfde5380c','23');
288round3_step( 0,'%eax','%ebx','%ecx','%edx', '4','0xa4beea44', '4');
289round3_step( 0,'%edx','%eax','%ebx','%ecx', '7','0x4bdecfa9','11');
290round3_step( 0,'%ecx','%edx','%eax','%ebx','10','0xf6bb4b60','16');
291round3_step( 0,'%ebx','%ecx','%edx','%eax','13','0xbebfbc70','23');
292round3_step( 0,'%eax','%ebx','%ecx','%edx', '0','0x289b7ec6', '4');
293round3_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xeaa127fa','11');
294round3_step( 0,'%ecx','%edx','%eax','%ebx', '6','0xd4ef3085','16');
295round3_step( 0,'%ebx','%ecx','%edx','%eax', '9', '0x4881d05','23');
296round3_step( 0,'%eax','%ebx','%ecx','%edx','12','0xd9d4d039', '4');
297round3_step( 0,'%edx','%eax','%ebx','%ecx','15','0xe6db99e5','11');
298round3_step( 0,'%ecx','%edx','%eax','%ebx', '2','0x1fa27cf8','16');
299round3_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xc4ac5665','23');
300
301round4_step(-1,'%eax','%ebx','%ecx','%edx', '7','0xf4292244', '6');
302round4_step( 0,'%edx','%eax','%ebx','%ecx','14','0x432aff97','10');
303round4_step( 0,'%ecx','%edx','%eax','%ebx', '5','0xab9423a7','15');
304round4_step( 0,'%ebx','%ecx','%edx','%eax','12','0xfc93a039','21');
305round4_step( 0,'%eax','%ebx','%ecx','%edx', '3','0x655b59c3', '6');
306round4_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8f0ccc92','10');
307round4_step( 0,'%ecx','%edx','%eax','%ebx', '1','0xffeff47d','15');
308round4_step( 0,'%ebx','%ecx','%edx','%eax', '8','0x85845dd1','21');
309round4_step( 0,'%eax','%ebx','%ecx','%edx','15','0x6fa87e4f', '6');
310round4_step( 0,'%edx','%eax','%ebx','%ecx', '6','0xfe2ce6e0','10');
311round4_step( 0,'%ecx','%edx','%eax','%ebx','13','0xa3014314','15');
312round4_step( 0,'%ebx','%ecx','%edx','%eax', '4','0x4e0811a1','21');
313round4_step( 0,'%eax','%ebx','%ecx','%edx','11','0xf7537e82', '6');
314round4_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xbd3af235','10');
315round4_step( 0,'%ecx','%edx','%eax','%ebx', '9','0x2ad7d2bb','15');
316round4_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xeb86d391','21');
317$code .= <<EOF;
318	/ add old values of A, B, C, D
319	add	%r8d,	%eax
320	add	%r9d,	%ebx
321	add	%r14d,	%ecx
322	add	%r15d,	%edx
323
324	/ loop control
325	add	\$64,		%rsi		/ ptr += 64
326	cmp	%rdi,		%rsi		/ cmp end with ptr
327	jb	2b				/ jmp if ptr < end
328	/ END of loop over 64-byte blocks
329
3301:	pop	%rbp				/ restore ctx
331	mov	%eax,		0*4(%rbp)	/ ctx->A = A
332	mov	%ebx,		1*4(%rbp)	/ ctx->B = B
333	mov	%ecx,		2*4(%rbp)	/ ctx->C = C
334	mov	%edx,		3*4(%rbp)	/ ctx->D = D
335
336	pop	%r15
337	pop	%r14
338	pop	%r13
339	pop	%r12
340	pop	%rbx
341	pop	%rbp
342	ret
343	SET_SIZE(md5_block_asm_host_order)
344
345
346#else
347	/* LINTED */
348	/* Nothing to be linted in this file--it's pure assembly source. */
349#endif /* !lint && !__lint */
350EOF
351
352print $code;
353