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