import less(1)
[unleashed/tickless.git] / usr / src / common / crypto / md5 / amd64 / md5_amd64.pl
blob51567635350d952effbd8d8f7145e99328187d57
1 #!/usr/bin/perl -w
3 # MD5 optimized for AMD64.
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.
11 # The following is Marc Bevand's MD5 implementation optimized for
12 # AMD64. It has been lifted intact, except for changing the comment
13 # character and adding comments.
15 # typedef struct {
16 # uint32_t state[4]; /* state (ABCD) */
17 # uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
18 # union {
19 # uint8_t buf8[64]; /* undigested input */
20 # uint32_t buf32[16]; /* realigned input */
21 # } buf_un;
22 # } MD5_CTX;
24 # void md5_block_asm_host_order(MD5_CTX *ctx, const void *inpp,
25 # unsigned int input_length_in_blocks);
27 # Registers used:
28 # rax A r8 old A
29 # rbx B r9 old B
30 # rcx C r10 tmp
31 # rdx D r11 tmp
32 # rsi ptr r12 tmp
33 # rdi end r13 -
34 # rbp - r14 old C
35 # rsp stack r15 old D
38 use strict;
39 my $code;
42 # round1_step() does:
43 # dst = x + ((dst + F(x,y,z) + X[k] + T_i) <<< s)
44 # %r10d = X[k_next]
45 # %r11d = z' (copy of z for the next step)
46 # Each round1_step() takes about 5.3 clocks (9 instructions, 1.7 IPC)
47 sub round1_step
49 my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
50 $code .= " mov 0*4(%rsi), %r10d /* (NEXT STEP) X[0] */\n" if ($pos == -1);
51 $code .= " mov %edx, %r11d /* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
52 $T_i = sprintf("-0x%08x", (0xffffffff ^ hex($T_i))+1)
53 if (hex($T_i) >= 0x80000000);
55 $code .= <<EOF;
56 xor $y, %r11d /* y ^ ... */
57 lea $T_i($dst,%r10d),$dst /* Const + dst + ... r1 */
58 and $x, %r11d /* x & ... */
59 xor $z, %r11d /* z ^ ... */
60 mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */
61 add %r11d, $dst /* dst += ... */
62 rol \$$s, $dst /* dst <<< s */
63 mov $y, %r11d /* (NEXT STEP) z' = $y */
64 add $x, $dst /* dst += x */
65 EOF
68 # round2_step() does:
69 # dst = x + ((dst + G(x,y,z) + X[k] + T_i) <<< s)
70 # %r10d = X[k_next]
71 # %r11d = z' (copy of z for the next step)
72 # %r12d = z' (copy of z for the next step)
73 # Each round2_step() takes about 5.4 clocks (11 instructions, 2.0 IPC)
74 sub round2_step
76 my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
77 $code .= " mov 1*4(%rsi), %r10d /* (NEXT STEP) X[1] */\n" if ($pos == -1);
78 $code .= " mov %edx, %r11d /* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
79 $code .= " mov %edx, %r12d /* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
80 $T_i = sprintf("-0x%08x", (0xffffffff ^ hex($T_i))+1)
81 if (hex($T_i) >= 0x80000000);
83 $code .= <<EOF;
84 not %r11d /* not z */
85 lea $T_i($dst,%r10d),$dst /* Const + dst + ... r2 */
86 and $x, %r12d /* x & z */
87 and $y, %r11d /* y & (not z) */
88 mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */
89 or %r11d, %r12d /* (y & (not z)) | (x & z) */
90 mov $y, %r11d /* (NEXT STEP) z' = $y */
91 add %r12d, $dst /* dst += ... */
92 mov $y, %r12d /* (NEXT STEP) z' = $y */
93 rol \$$s, $dst /* dst <<< s */
94 add $x, $dst /* dst += x */
95 EOF
98 # round3_step() does:
99 # dst = x + ((dst + H(x,y,z) + X[k] + T_i) <<< s)
100 # %r10d = X[k_next]
101 # %r11d = y' (copy of y for the next step)
102 # Each round3_step() takes about 4.2 clocks (8 instructions, 1.9 IPC)
103 sub round3_step
105 my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
106 $code .= " mov 5*4(%rsi), %r10d /* (NEXT STEP) X[5] */\n" if ($pos == -1);
107 $code .= " mov %ecx, %r11d /* (NEXT STEP) y' = %ecx */\n" if ($pos == -1);
108 $T_i = sprintf("-0x%08x", (0xffffffff ^ hex($T_i))+1)
109 if (hex($T_i) >= 0x80000000);
111 $code .= <<EOF;
112 lea $T_i($dst,%r10d),$dst /* Const + dst + ... r3 */
113 mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */
114 xor $z, %r11d /* z ^ ... */
115 xor $x, %r11d /* x ^ ... */
116 add %r11d, $dst /* dst += ... */
117 rol \$$s, $dst /* dst <<< s */
118 mov $x, %r11d /* (NEXT STEP) y' = $x */
119 add $x, $dst /* dst += x */
123 # round4_step() does:
124 # dst = x + ((dst + I(x,y,z) + X[k] + T_i) <<< s)
125 # %r10d = X[k_next]
126 # %r11d = not z' (copy of not z for the next step)
127 # Each round4_step() takes about 5.2 clocks (9 instructions, 1.7 IPC)
128 sub round4_step
130 my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
131 $code .= " mov 0*4(%rsi), %r10d /* (NEXT STEP) X[0] */\n" if ($pos == -1);
132 $code .= " mov \$0xffffffff, %r11d\n" if ($pos == -1);
133 $code .= " xor %edx, %r11d /* (NEXT STEP) not z' = not %edx*/\n"
134 if ($pos == -1);
135 $T_i = sprintf("-0x%08x", (0xffffffff ^ hex($T_i))+1)
136 if (hex($T_i) >= 0x80000000);
138 $code .= <<EOF;
139 lea $T_i($dst,%r10d),$dst /* Const + dst + ... r4 */
140 or $x, %r11d /* x | ... */
141 xor $y, %r11d /* y ^ ... */
142 add %r11d, $dst /* dst += ... */
143 mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */
144 mov \$0xffffffff, %r11d
145 rol \$$s, $dst /* dst <<< s */
146 xor $y, %r11d /* (NEXT STEP) not z' = not $y */
147 add $x, $dst /* dst += x */
153 # Execution begins here.
156 my $output = shift;
157 open STDOUT,">$output" or die "can't open $output: $!";
159 $code .= <<EOF;
160 #include <sys/asm_linkage.h>
162 ENTRY_NP(md5_block_asm_host_order)
163 push %rbp
164 push %rbx
165 push %r12
166 push %r13
167 push %r14
168 push %r15
170 / rdi = arg #1 (ctx, MD5_CTX pointer)
171 / rsi = arg #2 (ptr, data pointer)
172 / rdx = arg #3 (nbr, number of 64-byte blocks to process)
173 mov %rdi, %rbp / rbp = ctx
174 shl \$6, %rdx / rdx = nbr in bytes
175 lea (%rsi,%rdx), %rdi / rdi = end
176 mov 0*4(%rbp), %eax / eax = ctx->A
177 mov 1*4(%rbp), %ebx / ebx = ctx->B
178 mov 2*4(%rbp), %ecx / ecx = ctx->C
179 mov 3*4(%rbp), %edx / edx = ctx->D
180 push %rbp / save ctx
181 / end is 'rdi'
182 / ptr is 'rsi'
183 / A is 'eax'
184 / B is 'ebx'
185 / C is 'ecx'
186 / D is 'edx'
188 cmp %rdi, %rsi / cmp end with ptr
189 je 1f / jmp if ptr == end
191 / BEGIN of loop over 64-byte blocks
192 2: / save old values of A, B, C, D
193 mov %eax, %r8d
194 mov %ebx, %r9d
195 mov %ecx, %r14d
196 mov %edx, %r15d
198 round1_step(-1,'%eax','%ebx','%ecx','%edx', '1','0xd76aa478', '7');
199 round1_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xe8c7b756','12');
200 round1_step( 0,'%ecx','%edx','%eax','%ebx', '3','0x242070db','17');
201 round1_step( 0,'%ebx','%ecx','%edx','%eax', '4','0xc1bdceee','22');
202 round1_step( 0,'%eax','%ebx','%ecx','%edx', '5','0xf57c0faf', '7');
203 round1_step( 0,'%edx','%eax','%ebx','%ecx', '6','0x4787c62a','12');
204 round1_step( 0,'%ecx','%edx','%eax','%ebx', '7','0xa8304613','17');
205 round1_step( 0,'%ebx','%ecx','%edx','%eax', '8','0xfd469501','22');
206 round1_step( 0,'%eax','%ebx','%ecx','%edx', '9','0x698098d8', '7');
207 round1_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8b44f7af','12');
208 round1_step( 0,'%ecx','%edx','%eax','%ebx','11','0xffff5bb1','17');
209 round1_step( 0,'%ebx','%ecx','%edx','%eax','12','0x895cd7be','22');
210 round1_step( 0,'%eax','%ebx','%ecx','%edx','13','0x6b901122', '7');
211 round1_step( 0,'%edx','%eax','%ebx','%ecx','14','0xfd987193','12');
212 round1_step( 0,'%ecx','%edx','%eax','%ebx','15','0xa679438e','17');
213 round1_step( 1,'%ebx','%ecx','%edx','%eax', '0','0x49b40821','22');
215 round2_step(-1,'%eax','%ebx','%ecx','%edx', '6','0xf61e2562', '5');
216 round2_step( 0,'%edx','%eax','%ebx','%ecx','11','0xc040b340', '9');
217 round2_step( 0,'%ecx','%edx','%eax','%ebx', '0','0x265e5a51','14');
218 round2_step( 0,'%ebx','%ecx','%edx','%eax', '5','0xe9b6c7aa','20');
219 round2_step( 0,'%eax','%ebx','%ecx','%edx','10','0xd62f105d', '5');
220 round2_step( 0,'%edx','%eax','%ebx','%ecx','15', '0x2441453', '9');
221 round2_step( 0,'%ecx','%edx','%eax','%ebx', '4','0xd8a1e681','14');
222 round2_step( 0,'%ebx','%ecx','%edx','%eax', '9','0xe7d3fbc8','20');
223 round2_step( 0,'%eax','%ebx','%ecx','%edx','14','0x21e1cde6', '5');
224 round2_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xc33707d6', '9');
225 round2_step( 0,'%ecx','%edx','%eax','%ebx', '8','0xf4d50d87','14');
226 round2_step( 0,'%ebx','%ecx','%edx','%eax','13','0x455a14ed','20');
227 round2_step( 0,'%eax','%ebx','%ecx','%edx', '2','0xa9e3e905', '5');
228 round2_step( 0,'%edx','%eax','%ebx','%ecx', '7','0xfcefa3f8', '9');
229 round2_step( 0,'%ecx','%edx','%eax','%ebx','12','0x676f02d9','14');
230 round2_step( 1,'%ebx','%ecx','%edx','%eax', '0','0x8d2a4c8a','20');
232 round3_step(-1,'%eax','%ebx','%ecx','%edx', '8','0xfffa3942', '4');
233 round3_step( 0,'%edx','%eax','%ebx','%ecx','11','0x8771f681','11');
234 round3_step( 0,'%ecx','%edx','%eax','%ebx','14','0x6d9d6122','16');
235 round3_step( 0,'%ebx','%ecx','%edx','%eax', '1','0xfde5380c','23');
236 round3_step( 0,'%eax','%ebx','%ecx','%edx', '4','0xa4beea44', '4');
237 round3_step( 0,'%edx','%eax','%ebx','%ecx', '7','0x4bdecfa9','11');
238 round3_step( 0,'%ecx','%edx','%eax','%ebx','10','0xf6bb4b60','16');
239 round3_step( 0,'%ebx','%ecx','%edx','%eax','13','0xbebfbc70','23');
240 round3_step( 0,'%eax','%ebx','%ecx','%edx', '0','0x289b7ec6', '4');
241 round3_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xeaa127fa','11');
242 round3_step( 0,'%ecx','%edx','%eax','%ebx', '6','0xd4ef3085','16');
243 round3_step( 0,'%ebx','%ecx','%edx','%eax', '9', '0x4881d05','23');
244 round3_step( 0,'%eax','%ebx','%ecx','%edx','12','0xd9d4d039', '4');
245 round3_step( 0,'%edx','%eax','%ebx','%ecx','15','0xe6db99e5','11');
246 round3_step( 0,'%ecx','%edx','%eax','%ebx', '2','0x1fa27cf8','16');
247 round3_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xc4ac5665','23');
249 round4_step(-1,'%eax','%ebx','%ecx','%edx', '7','0xf4292244', '6');
250 round4_step( 0,'%edx','%eax','%ebx','%ecx','14','0x432aff97','10');
251 round4_step( 0,'%ecx','%edx','%eax','%ebx', '5','0xab9423a7','15');
252 round4_step( 0,'%ebx','%ecx','%edx','%eax','12','0xfc93a039','21');
253 round4_step( 0,'%eax','%ebx','%ecx','%edx', '3','0x655b59c3', '6');
254 round4_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8f0ccc92','10');
255 round4_step( 0,'%ecx','%edx','%eax','%ebx', '1','0xffeff47d','15');
256 round4_step( 0,'%ebx','%ecx','%edx','%eax', '8','0x85845dd1','21');
257 round4_step( 0,'%eax','%ebx','%ecx','%edx','15','0x6fa87e4f', '6');
258 round4_step( 0,'%edx','%eax','%ebx','%ecx', '6','0xfe2ce6e0','10');
259 round4_step( 0,'%ecx','%edx','%eax','%ebx','13','0xa3014314','15');
260 round4_step( 0,'%ebx','%ecx','%edx','%eax', '4','0x4e0811a1','21');
261 round4_step( 0,'%eax','%ebx','%ecx','%edx','11','0xf7537e82', '6');
262 round4_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xbd3af235','10');
263 round4_step( 0,'%ecx','%edx','%eax','%ebx', '9','0x2ad7d2bb','15');
264 round4_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xeb86d391','21');
265 $code .= <<EOF;
266 / add old values of A, B, C, D
267 add %r8d, %eax
268 add %r9d, %ebx
269 add %r14d, %ecx
270 add %r15d, %edx
272 / loop control
273 add \$64, %rsi / ptr += 64
274 cmp %rdi, %rsi / cmp end with ptr
275 jb 2b / jmp if ptr < end
276 / END of loop over 64-byte blocks
278 1: pop %rbp / restore ctx
279 mov %eax, 0*4(%rbp) / ctx->A = A
280 mov %ebx, 1*4(%rbp) / ctx->B = B
281 mov %ecx, 2*4(%rbp) / ctx->C = C
282 mov %edx, 3*4(%rbp) / ctx->D = D
284 pop %r15
285 pop %r14
286 pop %r13
287 pop %r12
288 pop %rbx
289 pop %rbp
291 SET_SIZE(md5_block_asm_host_order)
295 print $code;