1 | #! /usr/bin/env perl
|
---|
2 | # Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | #
|
---|
4 | # Licensed under the OpenSSL license (the "License"). You may not use
|
---|
5 | # this file except in compliance with the License. You can obtain a copy
|
---|
6 | # in the file LICENSE in the source distribution or at
|
---|
7 | # https://www.openssl.org/source/license.html
|
---|
8 |
|
---|
9 | #
|
---|
10 | # ====================================================================
|
---|
11 | # Written by Andy Polyakov <[email protected]> for the OpenSSL
|
---|
12 | # project. The module is, however, dual licensed under OpenSSL and
|
---|
13 | # CRYPTOGAMS licenses depending on where you obtain it. For further
|
---|
14 | # details see http://www.openssl.org/~appro/cryptogams/.
|
---|
15 | # ====================================================================
|
---|
16 | #
|
---|
17 | # May 2011
|
---|
18 | #
|
---|
19 | # The module implements bn_GF2m_mul_2x2 polynomial multiplication used
|
---|
20 | # in bn_gf2m.c. It's kind of low-hanging mechanical port from C for
|
---|
21 | # the time being... Except that it has two code paths: code suitable
|
---|
22 | # for any x86_64 CPU and PCLMULQDQ one suitable for Westmere and
|
---|
23 | # later. Improvement varies from one benchmark and µ-arch to another.
|
---|
24 | # Vanilla code path is at most 20% faster than compiler-generated code
|
---|
25 | # [not very impressive], while PCLMULQDQ - whole 85%-160% better on
|
---|
26 | # 163- and 571-bit ECDH benchmarks on Intel CPUs. Keep in mind that
|
---|
27 | # these coefficients are not ones for bn_GF2m_mul_2x2 itself, as not
|
---|
28 | # all CPU time is burnt in it...
|
---|
29 |
|
---|
30 | $flavour = shift;
|
---|
31 | $output = shift;
|
---|
32 | if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
|
---|
33 |
|
---|
34 | $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
|
---|
35 |
|
---|
36 | $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
|
---|
37 | ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
|
---|
38 | ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
|
---|
39 | die "can't locate x86_64-xlate.pl";
|
---|
40 |
|
---|
41 | open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
|
---|
42 | *STDOUT=*OUT;
|
---|
43 |
|
---|
44 | ($lo,$hi)=("%rax","%rdx"); $a=$lo;
|
---|
45 | ($i0,$i1)=("%rsi","%rdi");
|
---|
46 | ($t0,$t1)=("%rbx","%rcx");
|
---|
47 | ($b,$mask)=("%rbp","%r8");
|
---|
48 | ($a1,$a2,$a4,$a8,$a12,$a48)=map("%r$_",(9..15));
|
---|
49 | ($R,$Tx)=("%xmm0","%xmm1");
|
---|
50 |
|
---|
51 | $code.=<<___;
|
---|
52 | .text
|
---|
53 |
|
---|
54 | .type _mul_1x1,\@abi-omnipotent
|
---|
55 | .align 16
|
---|
56 | _mul_1x1:
|
---|
57 | sub \$128+8,%rsp
|
---|
58 | mov \$-1,$a1
|
---|
59 | lea ($a,$a),$i0
|
---|
60 | shr \$3,$a1
|
---|
61 | lea (,$a,4),$i1
|
---|
62 | and $a,$a1 # a1=a&0x1fffffffffffffff
|
---|
63 | lea (,$a,8),$a8
|
---|
64 | sar \$63,$a # broadcast 63rd bit
|
---|
65 | lea ($a1,$a1),$a2
|
---|
66 | sar \$63,$i0 # broadcast 62nd bit
|
---|
67 | lea (,$a1,4),$a4
|
---|
68 | and $b,$a
|
---|
69 | sar \$63,$i1 # boardcast 61st bit
|
---|
70 | mov $a,$hi # $a is $lo
|
---|
71 | shl \$63,$lo
|
---|
72 | and $b,$i0
|
---|
73 | shr \$1,$hi
|
---|
74 | mov $i0,$t1
|
---|
75 | shl \$62,$i0
|
---|
76 | and $b,$i1
|
---|
77 | shr \$2,$t1
|
---|
78 | xor $i0,$lo
|
---|
79 | mov $i1,$t0
|
---|
80 | shl \$61,$i1
|
---|
81 | xor $t1,$hi
|
---|
82 | shr \$3,$t0
|
---|
83 | xor $i1,$lo
|
---|
84 | xor $t0,$hi
|
---|
85 |
|
---|
86 | mov $a1,$a12
|
---|
87 | movq \$0,0(%rsp) # tab[0]=0
|
---|
88 | xor $a2,$a12 # a1^a2
|
---|
89 | mov $a1,8(%rsp) # tab[1]=a1
|
---|
90 | mov $a4,$a48
|
---|
91 | mov $a2,16(%rsp) # tab[2]=a2
|
---|
92 | xor $a8,$a48 # a4^a8
|
---|
93 | mov $a12,24(%rsp) # tab[3]=a1^a2
|
---|
94 |
|
---|
95 | xor $a4,$a1
|
---|
96 | mov $a4,32(%rsp) # tab[4]=a4
|
---|
97 | xor $a4,$a2
|
---|
98 | mov $a1,40(%rsp) # tab[5]=a1^a4
|
---|
99 | xor $a4,$a12
|
---|
100 | mov $a2,48(%rsp) # tab[6]=a2^a4
|
---|
101 | xor $a48,$a1 # a1^a4^a4^a8=a1^a8
|
---|
102 | mov $a12,56(%rsp) # tab[7]=a1^a2^a4
|
---|
103 | xor $a48,$a2 # a2^a4^a4^a8=a1^a8
|
---|
104 |
|
---|
105 | mov $a8,64(%rsp) # tab[8]=a8
|
---|
106 | xor $a48,$a12 # a1^a2^a4^a4^a8=a1^a2^a8
|
---|
107 | mov $a1,72(%rsp) # tab[9]=a1^a8
|
---|
108 | xor $a4,$a1 # a1^a8^a4
|
---|
109 | mov $a2,80(%rsp) # tab[10]=a2^a8
|
---|
110 | xor $a4,$a2 # a2^a8^a4
|
---|
111 | mov $a12,88(%rsp) # tab[11]=a1^a2^a8
|
---|
112 |
|
---|
113 | xor $a4,$a12 # a1^a2^a8^a4
|
---|
114 | mov $a48,96(%rsp) # tab[12]=a4^a8
|
---|
115 | mov $mask,$i0
|
---|
116 | mov $a1,104(%rsp) # tab[13]=a1^a4^a8
|
---|
117 | and $b,$i0
|
---|
118 | mov $a2,112(%rsp) # tab[14]=a2^a4^a8
|
---|
119 | shr \$4,$b
|
---|
120 | mov $a12,120(%rsp) # tab[15]=a1^a2^a4^a8
|
---|
121 | mov $mask,$i1
|
---|
122 | and $b,$i1
|
---|
123 | shr \$4,$b
|
---|
124 |
|
---|
125 | movq (%rsp,$i0,8),$R # half of calculations is done in SSE2
|
---|
126 | mov $mask,$i0
|
---|
127 | and $b,$i0
|
---|
128 | shr \$4,$b
|
---|
129 | ___
|
---|
130 | for ($n=1;$n<8;$n++) {
|
---|
131 | $code.=<<___;
|
---|
132 | mov (%rsp,$i1,8),$t1
|
---|
133 | mov $mask,$i1
|
---|
134 | mov $t1,$t0
|
---|
135 | shl \$`8*$n-4`,$t1
|
---|
136 | and $b,$i1
|
---|
137 | movq (%rsp,$i0,8),$Tx
|
---|
138 | shr \$`64-(8*$n-4)`,$t0
|
---|
139 | xor $t1,$lo
|
---|
140 | pslldq \$$n,$Tx
|
---|
141 | mov $mask,$i0
|
---|
142 | shr \$4,$b
|
---|
143 | xor $t0,$hi
|
---|
144 | and $b,$i0
|
---|
145 | shr \$4,$b
|
---|
146 | pxor $Tx,$R
|
---|
147 | ___
|
---|
148 | }
|
---|
149 | $code.=<<___;
|
---|
150 | mov (%rsp,$i1,8),$t1
|
---|
151 | mov $t1,$t0
|
---|
152 | shl \$`8*$n-4`,$t1
|
---|
153 | movq $R,$i0
|
---|
154 | shr \$`64-(8*$n-4)`,$t0
|
---|
155 | xor $t1,$lo
|
---|
156 | psrldq \$8,$R
|
---|
157 | xor $t0,$hi
|
---|
158 | movq $R,$i1
|
---|
159 | xor $i0,$lo
|
---|
160 | xor $i1,$hi
|
---|
161 |
|
---|
162 | add \$128+8,%rsp
|
---|
163 | ret
|
---|
164 | .Lend_mul_1x1:
|
---|
165 | .size _mul_1x1,.-_mul_1x1
|
---|
166 | ___
|
---|
167 |
|
---|
168 | ($rp,$a1,$a0,$b1,$b0) = $win64? ("%rcx","%rdx","%r8", "%r9","%r10") : # Win64 order
|
---|
169 | ("%rdi","%rsi","%rdx","%rcx","%r8"); # Unix order
|
---|
170 |
|
---|
171 | $code.=<<___;
|
---|
172 | .extern OPENSSL_ia32cap_P
|
---|
173 | .globl bn_GF2m_mul_2x2
|
---|
174 | .type bn_GF2m_mul_2x2,\@abi-omnipotent
|
---|
175 | .align 16
|
---|
176 | bn_GF2m_mul_2x2:
|
---|
177 | mov OPENSSL_ia32cap_P(%rip),%rax
|
---|
178 | bt \$33,%rax
|
---|
179 | jnc .Lvanilla_mul_2x2
|
---|
180 |
|
---|
181 | movq $a1,%xmm0
|
---|
182 | movq $b1,%xmm1
|
---|
183 | movq $a0,%xmm2
|
---|
184 | ___
|
---|
185 | $code.=<<___ if ($win64);
|
---|
186 | movq 40(%rsp),%xmm3
|
---|
187 | ___
|
---|
188 | $code.=<<___ if (!$win64);
|
---|
189 | movq $b0,%xmm3
|
---|
190 | ___
|
---|
191 | $code.=<<___;
|
---|
192 | movdqa %xmm0,%xmm4
|
---|
193 | movdqa %xmm1,%xmm5
|
---|
194 | pclmulqdq \$0,%xmm1,%xmm0 # a1·b1
|
---|
195 | pxor %xmm2,%xmm4
|
---|
196 | pxor %xmm3,%xmm5
|
---|
197 | pclmulqdq \$0,%xmm3,%xmm2 # a0·b0
|
---|
198 | pclmulqdq \$0,%xmm5,%xmm4 # (a0+a1)·(b0+b1)
|
---|
199 | xorps %xmm0,%xmm4
|
---|
200 | xorps %xmm2,%xmm4 # (a0+a1)·(b0+b1)-a0·b0-a1·b1
|
---|
201 | movdqa %xmm4,%xmm5
|
---|
202 | pslldq \$8,%xmm4
|
---|
203 | psrldq \$8,%xmm5
|
---|
204 | pxor %xmm4,%xmm2
|
---|
205 | pxor %xmm5,%xmm0
|
---|
206 | movdqu %xmm2,0($rp)
|
---|
207 | movdqu %xmm0,16($rp)
|
---|
208 | ret
|
---|
209 |
|
---|
210 | .align 16
|
---|
211 | .Lvanilla_mul_2x2:
|
---|
212 | lea -8*17(%rsp),%rsp
|
---|
213 | ___
|
---|
214 | $code.=<<___ if ($win64);
|
---|
215 | mov `8*17+40`(%rsp),$b0
|
---|
216 | mov %rdi,8*15(%rsp)
|
---|
217 | mov %rsi,8*16(%rsp)
|
---|
218 | ___
|
---|
219 | $code.=<<___;
|
---|
220 | mov %r14,8*10(%rsp)
|
---|
221 | mov %r13,8*11(%rsp)
|
---|
222 | mov %r12,8*12(%rsp)
|
---|
223 | mov %rbp,8*13(%rsp)
|
---|
224 | mov %rbx,8*14(%rsp)
|
---|
225 | .Lbody_mul_2x2:
|
---|
226 | mov $rp,32(%rsp) # save the arguments
|
---|
227 | mov $a1,40(%rsp)
|
---|
228 | mov $a0,48(%rsp)
|
---|
229 | mov $b1,56(%rsp)
|
---|
230 | mov $b0,64(%rsp)
|
---|
231 |
|
---|
232 | mov \$0xf,$mask
|
---|
233 | mov $a1,$a
|
---|
234 | mov $b1,$b
|
---|
235 | call _mul_1x1 # a1·b1
|
---|
236 | mov $lo,16(%rsp)
|
---|
237 | mov $hi,24(%rsp)
|
---|
238 |
|
---|
239 | mov 48(%rsp),$a
|
---|
240 | mov 64(%rsp),$b
|
---|
241 | call _mul_1x1 # a0·b0
|
---|
242 | mov $lo,0(%rsp)
|
---|
243 | mov $hi,8(%rsp)
|
---|
244 |
|
---|
245 | mov 40(%rsp),$a
|
---|
246 | mov 56(%rsp),$b
|
---|
247 | xor 48(%rsp),$a
|
---|
248 | xor 64(%rsp),$b
|
---|
249 | call _mul_1x1 # (a0+a1)·(b0+b1)
|
---|
250 | ___
|
---|
251 | @r=("%rbx","%rcx","%rdi","%rsi");
|
---|
252 | $code.=<<___;
|
---|
253 | mov 0(%rsp),@r[0]
|
---|
254 | mov 8(%rsp),@r[1]
|
---|
255 | mov 16(%rsp),@r[2]
|
---|
256 | mov 24(%rsp),@r[3]
|
---|
257 | mov 32(%rsp),%rbp
|
---|
258 |
|
---|
259 | xor $hi,$lo
|
---|
260 | xor @r[1],$hi
|
---|
261 | xor @r[0],$lo
|
---|
262 | mov @r[0],0(%rbp)
|
---|
263 | xor @r[2],$hi
|
---|
264 | mov @r[3],24(%rbp)
|
---|
265 | xor @r[3],$lo
|
---|
266 | xor @r[3],$hi
|
---|
267 | xor $hi,$lo
|
---|
268 | mov $hi,16(%rbp)
|
---|
269 | mov $lo,8(%rbp)
|
---|
270 |
|
---|
271 | mov 8*10(%rsp),%r14
|
---|
272 | mov 8*11(%rsp),%r13
|
---|
273 | mov 8*12(%rsp),%r12
|
---|
274 | mov 8*13(%rsp),%rbp
|
---|
275 | mov 8*14(%rsp),%rbx
|
---|
276 | ___
|
---|
277 | $code.=<<___ if ($win64);
|
---|
278 | mov 8*15(%rsp),%rdi
|
---|
279 | mov 8*16(%rsp),%rsi
|
---|
280 | ___
|
---|
281 | $code.=<<___;
|
---|
282 | lea 8*17(%rsp),%rsp
|
---|
283 | ret
|
---|
284 | .Lend_mul_2x2:
|
---|
285 | .size bn_GF2m_mul_2x2,.-bn_GF2m_mul_2x2
|
---|
286 | .asciz "GF(2^m) Multiplication for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
|
---|
287 | .align 16
|
---|
288 | ___
|
---|
289 |
|
---|
290 | # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
|
---|
291 | # CONTEXT *context,DISPATCHER_CONTEXT *disp)
|
---|
292 | if ($win64) {
|
---|
293 | $rec="%rcx";
|
---|
294 | $frame="%rdx";
|
---|
295 | $context="%r8";
|
---|
296 | $disp="%r9";
|
---|
297 |
|
---|
298 | $code.=<<___;
|
---|
299 | .extern __imp_RtlVirtualUnwind
|
---|
300 |
|
---|
301 | .type se_handler,\@abi-omnipotent
|
---|
302 | .align 16
|
---|
303 | se_handler:
|
---|
304 | push %rsi
|
---|
305 | push %rdi
|
---|
306 | push %rbx
|
---|
307 | push %rbp
|
---|
308 | push %r12
|
---|
309 | push %r13
|
---|
310 | push %r14
|
---|
311 | push %r15
|
---|
312 | pushfq
|
---|
313 | sub \$64,%rsp
|
---|
314 |
|
---|
315 | mov 152($context),%rax # pull context->Rsp
|
---|
316 | mov 248($context),%rbx # pull context->Rip
|
---|
317 |
|
---|
318 | lea .Lbody_mul_2x2(%rip),%r10
|
---|
319 | cmp %r10,%rbx # context->Rip<"prologue" label
|
---|
320 | jb .Lin_prologue
|
---|
321 |
|
---|
322 | mov 8*10(%rax),%r14 # mimic epilogue
|
---|
323 | mov 8*11(%rax),%r13
|
---|
324 | mov 8*12(%rax),%r12
|
---|
325 | mov 8*13(%rax),%rbp
|
---|
326 | mov 8*14(%rax),%rbx
|
---|
327 | mov 8*15(%rax),%rdi
|
---|
328 | mov 8*16(%rax),%rsi
|
---|
329 |
|
---|
330 | mov %rbx,144($context) # restore context->Rbx
|
---|
331 | mov %rbp,160($context) # restore context->Rbp
|
---|
332 | mov %rsi,168($context) # restore context->Rsi
|
---|
333 | mov %rdi,176($context) # restore context->Rdi
|
---|
334 | mov %r12,216($context) # restore context->R12
|
---|
335 | mov %r13,224($context) # restore context->R13
|
---|
336 | mov %r14,232($context) # restore context->R14
|
---|
337 |
|
---|
338 | .Lin_prologue:
|
---|
339 | lea 8*17(%rax),%rax
|
---|
340 | mov %rax,152($context) # restore context->Rsp
|
---|
341 |
|
---|
342 | mov 40($disp),%rdi # disp->ContextRecord
|
---|
343 | mov $context,%rsi # context
|
---|
344 | mov \$154,%ecx # sizeof(CONTEXT)
|
---|
345 | .long 0xa548f3fc # cld; rep movsq
|
---|
346 |
|
---|
347 | mov $disp,%rsi
|
---|
348 | xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
|
---|
349 | mov 8(%rsi),%rdx # arg2, disp->ImageBase
|
---|
350 | mov 0(%rsi),%r8 # arg3, disp->ControlPc
|
---|
351 | mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
|
---|
352 | mov 40(%rsi),%r10 # disp->ContextRecord
|
---|
353 | lea 56(%rsi),%r11 # &disp->HandlerData
|
---|
354 | lea 24(%rsi),%r12 # &disp->EstablisherFrame
|
---|
355 | mov %r10,32(%rsp) # arg5
|
---|
356 | mov %r11,40(%rsp) # arg6
|
---|
357 | mov %r12,48(%rsp) # arg7
|
---|
358 | mov %rcx,56(%rsp) # arg8, (NULL)
|
---|
359 | call *__imp_RtlVirtualUnwind(%rip)
|
---|
360 |
|
---|
361 | mov \$1,%eax # ExceptionContinueSearch
|
---|
362 | add \$64,%rsp
|
---|
363 | popfq
|
---|
364 | pop %r15
|
---|
365 | pop %r14
|
---|
366 | pop %r13
|
---|
367 | pop %r12
|
---|
368 | pop %rbp
|
---|
369 | pop %rbx
|
---|
370 | pop %rdi
|
---|
371 | pop %rsi
|
---|
372 | ret
|
---|
373 | .size se_handler,.-se_handler
|
---|
374 |
|
---|
375 | .section .pdata
|
---|
376 | .align 4
|
---|
377 | .rva _mul_1x1
|
---|
378 | .rva .Lend_mul_1x1
|
---|
379 | .rva .LSEH_info_1x1
|
---|
380 |
|
---|
381 | .rva .Lvanilla_mul_2x2
|
---|
382 | .rva .Lend_mul_2x2
|
---|
383 | .rva .LSEH_info_2x2
|
---|
384 | .section .xdata
|
---|
385 | .align 8
|
---|
386 | .LSEH_info_1x1:
|
---|
387 | .byte 0x01,0x07,0x02,0x00
|
---|
388 | .byte 0x07,0x01,0x11,0x00 # sub rsp,128+8
|
---|
389 | .LSEH_info_2x2:
|
---|
390 | .byte 9,0,0,0
|
---|
391 | .rva se_handler
|
---|
392 | ___
|
---|
393 | }
|
---|
394 |
|
---|
395 | $code =~ s/\`([^\`]*)\`/eval($1)/gem;
|
---|
396 | print $code;
|
---|
397 | close STDOUT;
|
---|