3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
12 # Companion to x86_64-mont.pl that optimizes cache-timing attack
13 # countermeasures. The subroutines are produced by replacing bp[i]
14 # references in their x86_64-mont.pl counterparts with cache-neutral
15 # references to powers table computed in BN_mod_exp_mont_consttime.
16 # In addition subroutine that scatters elements of the powers table
17 # is implemented, so that scatter-/gathering can be tuned without
18 # bn_exp.c modifications.
22 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
24 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
26 $0 =~ m/(.*[\/\\])[^\
/\\]+$/; $dir=$1;
27 ( $xlate="${dir}x86_64-xlate.pl" and -f
$xlate ) or
28 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f
$xlate) or
29 die "can't locate x86_64-xlate.pl";
31 open OUT
,"| \"$^X\" $xlate $flavour $output";
34 # int bn_mul_mont_gather5(
35 $rp="%rdi"; # BN_ULONG *rp,
36 $ap="%rsi"; # const BN_ULONG *ap,
37 $bp="%rdx"; # const BN_ULONG *bp,
38 $np="%rcx"; # const BN_ULONG *np,
39 $n0="%r8"; # const BN_ULONG *n0,
40 $num="%r9"; # int num,
41 # int idx); # 0 to 2^5-1, "index" in $bp holding
42 # pre-computed powers of a', interlaced
43 # in such manner that b[0] is $bp[idx],
44 # b[1] is [2^5+idx], etc.
56 .globl bn_mul_mont_gather5
57 .type bn_mul_mont_gather5
,\
@function,6
69 mov
`($win64?56:8)`(%rsp),%r10d # load 7th argument
77 $code.=<<___
if ($win64);
80 movaps
%xmm7,0x10(%rsp)
87 lea
(%rsp,%r11,8),%rsp # tp=alloca(8*(num+2))
88 and \
$-1024,%rsp # minimize TLB usage
90 mov
%rax,8(%rsp,$num,8) # tp[num+1]=%rsp
92 mov
$bp,%r12 # reassign $bp
95 $STRIDE=2**5*8; # 5 is "window size"
96 $N=$STRIDE/4; # should match cache line size
99 shr \
$`log($N/8)/log(2)`,%r10
102 lea
.Lmagic_masks
(%rip),%rax
103 and \
$`2**5/($N/8)-1`,%r10 # 5 is "window size"
104 lea
96($bp,%r11,8),$bp # pointer within 1st cache line
105 movq
0(%rax,%r10,8),%xmm4 # set of masks denoting which
106 movq
8(%rax,%r10,8),%xmm5 # cache line contains element
107 movq
16(%rax,%r10,8),%xmm6 # denoted by 7th argument
108 movq
24(%rax,%r10,8),%xmm7
110 movq
`0*$STRIDE/4-96`($bp),%xmm0
111 movq
`1*$STRIDE/4-96`($bp),%xmm1
113 movq
`2*$STRIDE/4-96`($bp),%xmm2
115 movq
`3*$STRIDE/4-96`($bp),%xmm3
123 movq
%xmm0,$m0 # m0=bp[0]
125 mov
($n0),$n0 # pull n0[0] value
131 movq
`0*$STRIDE/4-96`($bp),%xmm0
132 movq
`1*$STRIDE/4-96`($bp),%xmm1
134 movq
`2*$STRIDE/4-96`($bp),%xmm2
138 mulq
$m0 # ap[0]*bp[0]
142 movq
`3*$STRIDE/4-96`($bp),%xmm3
147 imulq
$lo0,$m1 # "tp[0]"*n0
155 add
%rax,$lo0 # discarded
168 add
$hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
171 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
175 mulq
$m0 # ap[j]*bp[0]
186 movq
%xmm0,$m0 # bp[1]
189 mov
($ap),%rax # ap[0]
191 add
$hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
193 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
200 mov
$hi1,-8(%rsp,$num,8)
201 mov
%rdx,(%rsp,$num,8) # store upmost overflow bit
211 movq
`0*$STRIDE/4-96`($bp),%xmm0
212 movq
`1*$STRIDE/4-96`($bp),%xmm1
214 movq
`2*$STRIDE/4-96`($bp),%xmm2
217 mulq
$m0 # ap[0]*bp[i]
218 add
%rax,$lo0 # ap[0]*bp[i]+tp[0]
222 movq
`3*$STRIDE/4-96`($bp),%xmm3
227 imulq
$lo0,$m1 # tp[0]*n0
235 add
%rax,$lo0 # discarded
238 mov
8(%rsp),$lo0 # tp[1]
249 add
$lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
252 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
256 mulq
$m0 # ap[j]*bp[i]
260 add
$hi0,$lo0 # ap[j]*bp[i]+tp[j]
269 movq
%xmm0,$m0 # bp[i+1]
272 mov
($ap),%rax # ap[0]
274 add
$lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
277 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
283 add
$lo0,$hi1 # pull upmost overflow bit
285 mov
$hi1,-8(%rsp,$num,8)
286 mov
%rdx,(%rsp,$num,8) # store upmost overflow bit
292 xor $i,$i # i=0 and clear CF!
293 mov
(%rsp),%rax # tp[0]
294 lea
(%rsp),$ap # borrow ap for tp
298 .Lsub
: sbb
($np,$i,8),%rax
299 mov
%rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
300 mov
8($ap,$i,8),%rax # tp[i+1]
302 dec
$j # doesnn't affect CF!
305 sbb \
$0,%rax # handle upmost overflow bit
312 or $np,$ap # ap=borrow?tp:rp
314 .Lcopy
: # copy or in-place refresh
316 mov
$i,(%rsp,$i,8) # zap temporary vector
317 mov
%rax,($rp,$i,8) # rp[i]=tp[i]
322 mov
8(%rsp,$num,8),%rsi # restore %rsp
325 $code.=<<___
if ($win64);
327 movaps
0x10(%rsi),%xmm7
340 .size bn_mul_mont_gather5
,.-bn_mul_mont_gather5
343 my @A=("%r10","%r11");
344 my @N=("%r13","%rdi");
346 .type bn_mul4x_mont_gather5
,\
@function,6
348 bn_mul4x_mont_gather5
:
351 mov
`($win64?56:8)`(%rsp),%r10d # load 7th argument
359 $code.=<<___
if ($win64);
362 movaps
%xmm7,0x10(%rsp)
369 lea
(%rsp,%r11,8),%rsp # tp=alloca(8*(num+4))
370 and \
$-1024,%rsp # minimize TLB usage
372 mov
%rax,8(%rsp,$num,8) # tp[num+1]=%rsp
374 mov
$rp,16(%rsp,$num,8) # tp[num+2]=$rp
375 mov
%rdx,%r12 # reassign $bp
378 $STRIDE=2**5*8; # 5 is "window size"
379 $N=$STRIDE/4; # should match cache line size
382 shr \
$`log($N/8)/log(2)`,%r10
385 lea
.Lmagic_masks
(%rip),%rax
386 and \
$`2**5/($N/8)-1`,%r10 # 5 is "window size"
387 lea
96($bp,%r11,8),$bp # pointer within 1st cache line
388 movq
0(%rax,%r10,8),%xmm4 # set of masks denoting which
389 movq
8(%rax,%r10,8),%xmm5 # cache line contains element
390 movq
16(%rax,%r10,8),%xmm6 # denoted by 7th argument
391 movq
24(%rax,%r10,8),%xmm7
393 movq
`0*$STRIDE/4-96`($bp),%xmm0
394 movq
`1*$STRIDE/4-96`($bp),%xmm1
396 movq
`2*$STRIDE/4-96`($bp),%xmm2
398 movq
`3*$STRIDE/4-96`($bp),%xmm3
406 movq
%xmm0,$m0 # m0=bp[0]
407 mov
($n0),$n0 # pull n0[0] value
413 movq
`0*$STRIDE/4-96`($bp),%xmm0
414 movq
`1*$STRIDE/4-96`($bp),%xmm1
416 movq
`2*$STRIDE/4-96`($bp),%xmm2
420 mulq
$m0 # ap[0]*bp[0]
424 movq
`3*$STRIDE/4-96`($bp),%xmm3
429 imulq
$A[0],$m1 # "tp[0]"*n0
437 add
%rax,$A[0] # discarded
460 mulq
$m0 # ap[j]*bp[0]
462 mov
-16($np,$j,8),%rax
468 mov
-8($ap,$j,8),%rax
470 add
$A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
472 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
475 mulq
$m0 # ap[j]*bp[0]
477 mov
-8($np,$j,8),%rax
485 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
487 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
490 mulq
$m0 # ap[j]*bp[0]
500 add
$A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
502 mov
$N[0],-8(%rsp,$j,8) # tp[j-1]
505 mulq
$m0 # ap[j]*bp[0]
514 mov
-16($ap,$j,8),%rax
516 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
518 mov
$N[1],-32(%rsp,$j,8) # tp[j-1]
523 mulq
$m0 # ap[j]*bp[0]
525 mov
-16($np,$j,8),%rax
531 mov
-8($ap,$j,8),%rax
533 add
$A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
535 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
538 mulq
$m0 # ap[j]*bp[0]
540 mov
-8($np,$j,8),%rax
546 mov
($ap),%rax # ap[0]
548 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
550 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
553 movq
%xmm0,$m0 # bp[1]
558 mov
$N[0],-8(%rsp,$j,8)
559 mov
$N[1],(%rsp,$j,8) # store upmost overflow bit
565 movq
`0*$STRIDE/4-96`($bp),%xmm0
566 movq
`1*$STRIDE/4-96`($bp),%xmm1
568 movq
`2*$STRIDE/4-96`($bp),%xmm2
573 mulq
$m0 # ap[0]*bp[i]
574 add
%rax,$A[0] # ap[0]*bp[i]+tp[0]
578 movq
`3*$STRIDE/4-96`($bp),%xmm3
583 imulq
$A[0],$m1 # tp[0]*n0
591 add
%rax,$A[0] # "$N[0]", discarded
596 mulq
$m0 # ap[j]*bp[i]
600 add
8(%rsp),$A[1] # +tp[1]
608 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
615 mulq
$m0 # ap[j]*bp[i]
617 mov
-16($np,$j,8),%rax
619 add
-16(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
625 mov
-8($ap,$j,8),%rax
629 mov
$N[1],-32(%rsp,$j,8) # tp[j-1]
632 mulq
$m0 # ap[j]*bp[i]
634 mov
-8($np,$j,8),%rax
636 add
-8(%rsp,$j,8),$A[1]
646 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
649 mulq
$m0 # ap[j]*bp[i]
653 add
(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
663 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
666 mulq
$m0 # ap[j]*bp[i]
670 add
8(%rsp,$j,8),$A[1]
677 mov
-16($ap,$j,8),%rax
681 mov
$N[0],-40(%rsp,$j,8) # tp[j-1]
686 mulq
$m0 # ap[j]*bp[i]
688 mov
-16($np,$j,8),%rax
690 add
-16(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
696 mov
-8($ap,$j,8),%rax
700 mov
$N[1],-32(%rsp,$j,8) # tp[j-1]
703 mulq
$m0 # ap[j]*bp[i]
705 mov
-8($np,$j,8),%rax
707 add
-8(%rsp,$j,8),$A[1]
714 mov
($ap),%rax # ap[0]
718 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
721 movq
%xmm0,$m0 # bp[i+1]
722 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
727 add
(%rsp,$num,8),$N[0] # pull upmost overflow bit
729 mov
$N[0],-8(%rsp,$j,8)
730 mov
$N[1],(%rsp,$j,8) # store upmost overflow bit
736 my @ri=("%rax","%rdx",$m0,$m1);
738 mov
16(%rsp,$num,8),$rp # restore $rp
739 mov
0(%rsp),@ri[0] # tp[0]
741 mov
8(%rsp),@ri[1] # tp[1]
742 shr \
$2,$num # num/=4
743 lea
(%rsp),$ap # borrow ap for tp
744 xor $i,$i # i=0 and clear CF!
747 mov
16($ap),@ri[2] # tp[2]
748 mov
24($ap),@ri[3] # tp[3]
750 lea
-1($num),$j # j=num/4-1
754 mov
@ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
755 mov
@ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
756 sbb
16($np,$i,8),@ri[2]
757 mov
32($ap,$i,8),@ri[0] # tp[i+1]
758 mov
40($ap,$i,8),@ri[1]
759 sbb
24($np,$i,8),@ri[3]
760 mov
@ri[2],16($rp,$i,8) # rp[i]=tp[i]-np[i]
761 mov
@ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
762 sbb
32($np,$i,8),@ri[0]
763 mov
48($ap,$i,8),@ri[2]
764 mov
56($ap,$i,8),@ri[3]
765 sbb
40($np,$i,8),@ri[1]
767 dec
$j # doesnn't affect CF!
770 mov
@ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
771 mov
32($ap,$i,8),@ri[0] # load overflow bit
772 sbb
16($np,$i,8),@ri[2]
773 mov
@ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
774 sbb
24($np,$i,8),@ri[3]
775 mov
@ri[2],16($rp,$i,8) # rp[i]=tp[i]-np[i]
777 sbb \
$0,@ri[0] # handle upmost overflow bit
778 mov
@ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
785 or $np,$ap # ap=borrow?tp:rp
792 .Lcopy4x
: # copy or in-place refresh
793 movdqu
16($ap,$i),%xmm2
794 movdqu
32($ap,$i),%xmm1
795 movdqa
%xmm0,16(%rsp,$i)
796 movdqu
%xmm2,16($rp,$i)
797 movdqa
%xmm0,32(%rsp,$i)
798 movdqu
%xmm1,32($rp,$i)
804 movdqu
16($ap,$i),%xmm2
805 movdqa
%xmm0,16(%rsp,$i)
806 movdqu
%xmm2,16($rp,$i)
810 mov
8(%rsp,$num,8),%rsi # restore %rsp
813 $code.=<<___
if ($win64);
815 movaps
0x10(%rsi),%xmm7
828 .size bn_mul4x_mont_gather5
,.-bn_mul4x_mont_gather5
833 my ($inp,$num,$tbl,$idx)=$win64?
("%rcx","%rdx","%r8", "%r9") : # Win64 order
834 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
841 .type bn_scatter5
,\
@abi-omnipotent
845 jz
.Lscatter_epilogue
846 lea
($tbl,$idx,8),$tbl
856 .size bn_scatter5
,.-bn_scatter5
859 .type bn_gather5
,\
@abi-omnipotent
863 $code.=<<___
if ($win64);
864 .LSEH_begin_bn_gather5
:
865 # I can't trust assembler to use specific encoding:-(
866 .byte
0x48,0x83,0xec,0x28 #sub \$0x28,%rsp
867 .byte
0x0f,0x29,0x34,0x24 #movaps %xmm6,(%rsp)
868 .byte
0x0f,0x29,0x7c,0x24,0x10 #movdqa %xmm7,0x10(%rsp)
872 shr \
$`log($N/8)/log(2)`,$idx
875 lea
.Lmagic_masks
(%rip),%rax
876 and \
$`2**5/($N/8)-1`,$idx # 5 is "window size"
877 lea
96($tbl,%r11,8),$tbl # pointer within 1st cache line
878 movq
0(%rax,$idx,8),%xmm4 # set of masks denoting which
879 movq
8(%rax,$idx,8),%xmm5 # cache line contains element
880 movq
16(%rax,$idx,8),%xmm6 # denoted by 7th argument
881 movq
24(%rax,$idx,8),%xmm7
885 movq
`0*$STRIDE/4-96`($tbl),%xmm0
886 movq
`1*$STRIDE/4-96`($tbl),%xmm1
888 movq
`2*$STRIDE/4-96`($tbl),%xmm2
890 movq
`3*$STRIDE/4-96`($tbl),%xmm3
895 lea
$STRIDE($tbl),$tbl
898 movq
%xmm0,($out) # m0=bp[0]
903 $code.=<<___
if ($win64);
905 movaps
0x10(%rsp),%xmm7
910 .LSEH_end_bn_gather5
:
911 .size bn_gather5
,.-bn_gather5
917 .long
0,0, 0,0, 0,0, -1,-1
918 .long
0,0, 0,0, 0,0, 0,0
919 .asciz
"Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
922 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
923 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
931 .extern __imp_RtlVirtualUnwind
932 .type mul_handler
,\
@abi-omnipotent
946 mov
120($context),%rax # pull context->Rax
947 mov
248($context),%rbx # pull context->Rip
949 mov
8($disp),%rsi # disp->ImageBase
950 mov
56($disp),%r11 # disp->HandlerData
952 mov
0(%r11),%r10d # HandlerData[0]
953 lea
(%rsi,%r10),%r10 # end of prologue label
954 cmp %r10,%rbx # context->Rip<end of prologue label
957 lea
`40+48`(%rax),%rax
959 mov
4(%r11),%r10d # HandlerData[1]
960 lea
(%rsi,%r10),%r10 # end of alloca label
961 cmp %r10,%rbx # context->Rip<end of alloca label
964 mov
152($context),%rax # pull context->Rsp
966 mov
8(%r11),%r10d # HandlerData[2]
967 lea
(%rsi,%r10),%r10 # epilogue label
968 cmp %r10,%rbx # context->Rip>=epilogue label
969 jae
.Lcommon_seh_tail
971 mov
192($context),%r10 # pull $num
972 mov
8(%rax,%r10,8),%rax # pull saved stack pointer
975 movaps
16(%rax),%xmm1
976 lea
`40+48`(%rax),%rax
984 mov
%rbx,144($context) # restore context->Rbx
985 mov
%rbp,160($context) # restore context->Rbp
986 mov
%r12,216($context) # restore context->R12
987 mov
%r13,224($context) # restore context->R13
988 mov
%r14,232($context) # restore context->R14
989 mov
%r15,240($context) # restore context->R15
990 movups
%xmm0,512($context) # restore context->Xmm6
991 movups
%xmm1,528($context) # restore context->Xmm7
996 mov
%rax,152($context) # restore context->Rsp
997 mov
%rsi,168($context) # restore context->Rsi
998 mov
%rdi,176($context) # restore context->Rdi
1000 mov
40($disp),%rdi # disp->ContextRecord
1001 mov
$context,%rsi # context
1002 mov \
$154,%ecx # sizeof(CONTEXT)
1003 .long
0xa548f3fc # cld; rep movsq
1006 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
1007 mov
8(%rsi),%rdx # arg2, disp->ImageBase
1008 mov
0(%rsi),%r8 # arg3, disp->ControlPc
1009 mov
16(%rsi),%r9 # arg4, disp->FunctionEntry
1010 mov
40(%rsi),%r10 # disp->ContextRecord
1011 lea
56(%rsi),%r11 # &disp->HandlerData
1012 lea
24(%rsi),%r12 # &disp->EstablisherFrame
1013 mov
%r10,32(%rsp) # arg5
1014 mov
%r11,40(%rsp) # arg6
1015 mov
%r12,48(%rsp) # arg7
1016 mov
%rcx,56(%rsp) # arg8, (NULL)
1017 call
*__imp_RtlVirtualUnwind
(%rip)
1019 mov \
$1,%eax # ExceptionContinueSearch
1031 .size mul_handler
,.-mul_handler
1035 .rva
.LSEH_begin_bn_mul_mont_gather5
1036 .rva
.LSEH_end_bn_mul_mont_gather5
1037 .rva
.LSEH_info_bn_mul_mont_gather5
1039 .rva
.LSEH_begin_bn_mul4x_mont_gather5
1040 .rva
.LSEH_end_bn_mul4x_mont_gather5
1041 .rva
.LSEH_info_bn_mul4x_mont_gather5
1043 .rva
.LSEH_begin_bn_gather5
1044 .rva
.LSEH_end_bn_gather5
1045 .rva
.LSEH_info_bn_gather5
1049 .LSEH_info_bn_mul_mont_gather5
:
1052 .rva
.Lmul_alloca
,.Lmul_body
,.Lmul_epilogue
# HandlerData[]
1054 .LSEH_info_bn_mul4x_mont_gather5
:
1057 .rva
.Lmul4x_alloca
,.Lmul4x_body
,.Lmul4x_epilogue
# HandlerData[]
1059 .LSEH_info_bn_gather5
:
1060 .byte
0x01,0x0d,0x05,0x00
1061 .byte
0x0d,0x78,0x01,0x00 #movaps 0x10(rsp),xmm7
1062 .byte
0x08,0x68,0x00,0x00 #movaps (rsp),xmm6
1063 .byte
0x04,0x42,0x00,0x00 #sub rsp,0x28
1068 $code =~ s/\`([^\`]*)\`/eval($1)/gem;