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 STDOUT
,"| $^X $xlate $flavour $output";
33 # int bn_mul_mont_gather5(
34 $rp="%rdi"; # BN_ULONG *rp,
35 $ap="%rsi"; # const BN_ULONG *ap,
36 $bp="%rdx"; # const BN_ULONG *bp,
37 $np="%rcx"; # const BN_ULONG *np,
38 $n0="%r8"; # const BN_ULONG *n0,
39 $num="%r9"; # int num,
40 # int idx); # 0 to 2^5-1, "index" in $bp holding
41 # pre-computed powers of a', interlaced
42 # in such manner that b[0] is $bp[idx],
43 # b[1] is [2^5+idx], etc.
55 .globl bn_mul_mont_gather5
56 .type bn_mul_mont_gather5
,\
@function,6
68 mov
`($win64?56:8)`(%rsp),%r10d # load 7th argument
76 $code.=<<___
if ($win64);
79 movaps
%xmm7,0x10(%rsp)
86 lea
(%rsp,%r11,8),%rsp # tp=alloca(8*(num+2))
87 and \
$-1024,%rsp # minimize TLB usage
89 mov
%rax,8(%rsp,$num,8) # tp[num+1]=%rsp
91 mov
$bp,%r12 # reassign $bp
94 $STRIDE=2**5*8; # 5 is "window size"
95 $N=$STRIDE/4; # should match cache line size
98 shr \
$`log($N/8)/log(2)`,%r10
101 lea
.Lmagic_masks
(%rip),%rax
102 and \
$`2**5/($N/8)-1`,%r10 # 5 is "window size"
103 lea
96($bp,%r11,8),$bp # pointer within 1st cache line
104 movq
0(%rax,%r10,8),%xmm4 # set of masks denoting which
105 movq
8(%rax,%r10,8),%xmm5 # cache line contains element
106 movq
16(%rax,%r10,8),%xmm6 # denoted by 7th argument
107 movq
24(%rax,%r10,8),%xmm7
109 movq
`0*$STRIDE/4-96`($bp),%xmm0
110 movq
`1*$STRIDE/4-96`($bp),%xmm1
112 movq
`2*$STRIDE/4-96`($bp),%xmm2
114 movq
`3*$STRIDE/4-96`($bp),%xmm3
122 movq
%xmm0,$m0 # m0=bp[0]
124 mov
($n0),$n0 # pull n0[0] value
130 movq
`0*$STRIDE/4-96`($bp),%xmm0
131 movq
`1*$STRIDE/4-96`($bp),%xmm1
133 movq
`2*$STRIDE/4-96`($bp),%xmm2
137 mulq
$m0 # ap[0]*bp[0]
141 movq
`3*$STRIDE/4-96`($bp),%xmm3
146 imulq
$lo0,$m1 # "tp[0]"*n0
154 add
%rax,$lo0 # discarded
167 add
$hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
170 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
174 mulq
$m0 # ap[j]*bp[0]
185 movq
%xmm0,$m0 # bp[1]
188 mov
($ap),%rax # ap[0]
190 add
$hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
192 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
199 mov
$hi1,-8(%rsp,$num,8)
200 mov
%rdx,(%rsp,$num,8) # store upmost overflow bit
210 movq
`0*$STRIDE/4-96`($bp),%xmm0
211 movq
`1*$STRIDE/4-96`($bp),%xmm1
213 movq
`2*$STRIDE/4-96`($bp),%xmm2
216 mulq
$m0 # ap[0]*bp[i]
217 add
%rax,$lo0 # ap[0]*bp[i]+tp[0]
221 movq
`3*$STRIDE/4-96`($bp),%xmm3
226 imulq
$lo0,$m1 # tp[0]*n0
234 add
%rax,$lo0 # discarded
237 mov
8(%rsp),$lo0 # tp[1]
248 add
$lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
251 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
255 mulq
$m0 # ap[j]*bp[i]
259 add
$hi0,$lo0 # ap[j]*bp[i]+tp[j]
268 movq
%xmm0,$m0 # bp[i+1]
271 mov
($ap),%rax # ap[0]
273 add
$lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
276 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
282 add
$lo0,$hi1 # pull upmost overflow bit
284 mov
$hi1,-8(%rsp,$num,8)
285 mov
%rdx,(%rsp,$num,8) # store upmost overflow bit
291 xor $i,$i # i=0 and clear CF!
292 mov
(%rsp),%rax # tp[0]
293 lea
(%rsp),$ap # borrow ap for tp
297 .Lsub
: sbb
($np,$i,8),%rax
298 mov
%rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
299 mov
8($ap,$i,8),%rax # tp[i+1]
301 dec
$j # doesnn't affect CF!
304 sbb \
$0,%rax # handle upmost overflow bit
311 or $np,$ap # ap=borrow?tp:rp
313 .Lcopy
: # copy or in-place refresh
315 mov
$i,(%rsp,$i,8) # zap temporary vector
316 mov
%rax,($rp,$i,8) # rp[i]=tp[i]
321 mov
8(%rsp,$num,8),%rsi # restore %rsp
324 $code.=<<___
if ($win64);
326 movaps
0x10(%rsi),%xmm7
339 .size bn_mul_mont_gather5
,.-bn_mul_mont_gather5
342 my @A=("%r10","%r11");
343 my @N=("%r13","%rdi");
345 .type bn_mul4x_mont_gather5
,\
@function,6
347 bn_mul4x_mont_gather5
:
350 mov
`($win64?56:8)`(%rsp),%r10d # load 7th argument
358 $code.=<<___
if ($win64);
361 movaps
%xmm7,0x10(%rsp)
368 lea
(%rsp,%r11,8),%rsp # tp=alloca(8*(num+4))
369 and \
$-1024,%rsp # minimize TLB usage
371 mov
%rax,8(%rsp,$num,8) # tp[num+1]=%rsp
373 mov
$rp,16(%rsp,$num,8) # tp[num+2]=$rp
374 mov
%rdx,%r12 # reassign $bp
377 $STRIDE=2**5*8; # 5 is "window size"
378 $N=$STRIDE/4; # should match cache line size
381 shr \
$`log($N/8)/log(2)`,%r10
384 lea
.Lmagic_masks
(%rip),%rax
385 and \
$`2**5/($N/8)-1`,%r10 # 5 is "window size"
386 lea
96($bp,%r11,8),$bp # pointer within 1st cache line
387 movq
0(%rax,%r10,8),%xmm4 # set of masks denoting which
388 movq
8(%rax,%r10,8),%xmm5 # cache line contains element
389 movq
16(%rax,%r10,8),%xmm6 # denoted by 7th argument
390 movq
24(%rax,%r10,8),%xmm7
392 movq
`0*$STRIDE/4-96`($bp),%xmm0
393 movq
`1*$STRIDE/4-96`($bp),%xmm1
395 movq
`2*$STRIDE/4-96`($bp),%xmm2
397 movq
`3*$STRIDE/4-96`($bp),%xmm3
405 movq
%xmm0,$m0 # m0=bp[0]
406 mov
($n0),$n0 # pull n0[0] value
412 movq
`0*$STRIDE/4-96`($bp),%xmm0
413 movq
`1*$STRIDE/4-96`($bp),%xmm1
415 movq
`2*$STRIDE/4-96`($bp),%xmm2
419 mulq
$m0 # ap[0]*bp[0]
423 movq
`3*$STRIDE/4-96`($bp),%xmm3
428 imulq
$A[0],$m1 # "tp[0]"*n0
436 add
%rax,$A[0] # discarded
459 mulq
$m0 # ap[j]*bp[0]
461 mov
-16($np,$j,8),%rax
467 mov
-8($ap,$j,8),%rax
469 add
$A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
471 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
474 mulq
$m0 # ap[j]*bp[0]
476 mov
-8($np,$j,8),%rax
484 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
486 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
489 mulq
$m0 # ap[j]*bp[0]
499 add
$A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
501 mov
$N[0],-8(%rsp,$j,8) # tp[j-1]
504 mulq
$m0 # ap[j]*bp[0]
513 mov
-16($ap,$j,8),%rax
515 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
517 mov
$N[1],-32(%rsp,$j,8) # tp[j-1]
522 mulq
$m0 # ap[j]*bp[0]
524 mov
-16($np,$j,8),%rax
530 mov
-8($ap,$j,8),%rax
532 add
$A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
534 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
537 mulq
$m0 # ap[j]*bp[0]
539 mov
-8($np,$j,8),%rax
545 mov
($ap),%rax # ap[0]
547 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
549 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
552 movq
%xmm0,$m0 # bp[1]
557 mov
$N[0],-8(%rsp,$j,8)
558 mov
$N[1],(%rsp,$j,8) # store upmost overflow bit
564 movq
`0*$STRIDE/4-96`($bp),%xmm0
565 movq
`1*$STRIDE/4-96`($bp),%xmm1
567 movq
`2*$STRIDE/4-96`($bp),%xmm2
572 mulq
$m0 # ap[0]*bp[i]
573 add
%rax,$A[0] # ap[0]*bp[i]+tp[0]
577 movq
`3*$STRIDE/4-96`($bp),%xmm3
582 imulq
$A[0],$m1 # tp[0]*n0
590 add
%rax,$A[0] # "$N[0]", discarded
595 mulq
$m0 # ap[j]*bp[i]
599 add
8(%rsp),$A[1] # +tp[1]
607 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
614 mulq
$m0 # ap[j]*bp[i]
616 mov
-16($np,$j,8),%rax
618 add
-16(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
624 mov
-8($ap,$j,8),%rax
628 mov
$N[1],-32(%rsp,$j,8) # tp[j-1]
631 mulq
$m0 # ap[j]*bp[i]
633 mov
-8($np,$j,8),%rax
635 add
-8(%rsp,$j,8),$A[1]
645 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
648 mulq
$m0 # ap[j]*bp[i]
652 add
(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
662 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
665 mulq
$m0 # ap[j]*bp[i]
669 add
8(%rsp,$j,8),$A[1]
676 mov
-16($ap,$j,8),%rax
680 mov
$N[0],-40(%rsp,$j,8) # tp[j-1]
685 mulq
$m0 # ap[j]*bp[i]
687 mov
-16($np,$j,8),%rax
689 add
-16(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
695 mov
-8($ap,$j,8),%rax
699 mov
$N[1],-32(%rsp,$j,8) # tp[j-1]
702 mulq
$m0 # ap[j]*bp[i]
704 mov
-8($np,$j,8),%rax
706 add
-8(%rsp,$j,8),$A[1]
713 mov
($ap),%rax # ap[0]
717 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
720 movq
%xmm0,$m0 # bp[i+1]
721 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
726 add
(%rsp,$num,8),$N[0] # pull upmost overflow bit
728 mov
$N[0],-8(%rsp,$j,8)
729 mov
$N[1],(%rsp,$j,8) # store upmost overflow bit
735 my @ri=("%rax","%rdx",$m0,$m1);
737 mov
16(%rsp,$num,8),$rp # restore $rp
738 mov
0(%rsp),@ri[0] # tp[0]
740 mov
8(%rsp),@ri[1] # tp[1]
741 shr \
$2,$num # num/=4
742 lea
(%rsp),$ap # borrow ap for tp
743 xor $i,$i # i=0 and clear CF!
746 mov
16($ap),@ri[2] # tp[2]
747 mov
24($ap),@ri[3] # tp[3]
749 lea
-1($num),$j # j=num/4-1
753 mov
@ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
754 mov
@ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
755 sbb
16($np,$i,8),@ri[2]
756 mov
32($ap,$i,8),@ri[0] # tp[i+1]
757 mov
40($ap,$i,8),@ri[1]
758 sbb
24($np,$i,8),@ri[3]
759 mov
@ri[2],16($rp,$i,8) # rp[i]=tp[i]-np[i]
760 mov
@ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
761 sbb
32($np,$i,8),@ri[0]
762 mov
48($ap,$i,8),@ri[2]
763 mov
56($ap,$i,8),@ri[3]
764 sbb
40($np,$i,8),@ri[1]
766 dec
$j # doesnn't affect CF!
769 mov
@ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
770 mov
32($ap,$i,8),@ri[0] # load overflow bit
771 sbb
16($np,$i,8),@ri[2]
772 mov
@ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
773 sbb
24($np,$i,8),@ri[3]
774 mov
@ri[2],16($rp,$i,8) # rp[i]=tp[i]-np[i]
776 sbb \
$0,@ri[0] # handle upmost overflow bit
777 mov
@ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
784 or $np,$ap # ap=borrow?tp:rp
791 .Lcopy4x
: # copy or in-place refresh
792 movdqu
16($ap,$i),%xmm2
793 movdqu
32($ap,$i),%xmm1
794 movdqa
%xmm0,16(%rsp,$i)
795 movdqu
%xmm2,16($rp,$i)
796 movdqa
%xmm0,32(%rsp,$i)
797 movdqu
%xmm1,32($rp,$i)
803 movdqu
16($ap,$i),%xmm2
804 movdqa
%xmm0,16(%rsp,$i)
805 movdqu
%xmm2,16($rp,$i)
809 mov
8(%rsp,$num,8),%rsi # restore %rsp
812 $code.=<<___
if ($win64);
814 movaps
0x10(%rsi),%xmm7
827 .size bn_mul4x_mont_gather5
,.-bn_mul4x_mont_gather5
832 my ($inp,$num,$tbl,$idx)=$win64?
("%rcx","%rdx","%r8", "%r9") : # Win64 order
833 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
840 .type bn_scatter5
,\
@abi-omnipotent
844 jz
.Lscatter_epilogue
845 lea
($tbl,$idx,8),$tbl
855 .size bn_scatter5
,.-bn_scatter5
858 .type bn_gather5
,\
@abi-omnipotent
862 $code.=<<___
if ($win64);
863 .LSEH_begin_bn_gather5
:
864 # I can't trust assembler to use specific encoding:-(
865 .byte
0x48,0x83,0xec,0x28 #sub \$0x28,%rsp
866 .byte
0x0f,0x29,0x34,0x24 #movaps %xmm6,(%rsp)
867 .byte
0x0f,0x29,0x7c,0x24,0x10 #movdqa %xmm7,0x10(%rsp)
871 shr \
$`log($N/8)/log(2)`,$idx
874 lea
.Lmagic_masks
(%rip),%rax
875 and \
$`2**5/($N/8)-1`,$idx # 5 is "window size"
876 lea
96($tbl,%r11,8),$tbl # pointer within 1st cache line
877 movq
0(%rax,$idx,8),%xmm4 # set of masks denoting which
878 movq
8(%rax,$idx,8),%xmm5 # cache line contains element
879 movq
16(%rax,$idx,8),%xmm6 # denoted by 7th argument
880 movq
24(%rax,$idx,8),%xmm7
884 movq
`0*$STRIDE/4-96`($tbl),%xmm0
885 movq
`1*$STRIDE/4-96`($tbl),%xmm1
887 movq
`2*$STRIDE/4-96`($tbl),%xmm2
889 movq
`3*$STRIDE/4-96`($tbl),%xmm3
894 lea
$STRIDE($tbl),$tbl
897 movq
%xmm0,($out) # m0=bp[0]
902 $code.=<<___
if ($win64);
904 movaps
%xmm7,0x10(%rsp)
909 .LSEH_end_bn_gather5
:
910 .size bn_gather5
,.-bn_gather5
916 .long
0,0, 0,0, 0,0, -1,-1
917 .long
0,0, 0,0, 0,0, 0,0
918 .asciz
"Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
921 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
922 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
930 .extern __imp_RtlVirtualUnwind
931 .type mul_handler
,\
@abi-omnipotent
945 mov
120($context),%rax # pull context->Rax
946 mov
248($context),%rbx # pull context->Rip
948 mov
8($disp),%rsi # disp->ImageBase
949 mov
56($disp),%r11 # disp->HandlerData
951 mov
0(%r11),%r10d # HandlerData[0]
952 lea
(%rsi,%r10),%r10 # end of prologue label
953 cmp %r10,%rbx # context->Rip<end of prologue label
956 lea
`40+48`(%rax),%rax
958 mov
4(%r11),%r10d # HandlerData[1]
959 lea
(%rsi,%r10),%r10 # end of alloca label
960 cmp %r10,%rbx # context->Rip<end of alloca label
963 mov
152($context),%rax # pull context->Rsp
965 mov
8(%r11),%r10d # HandlerData[2]
966 lea
(%rsi,%r10),%r10 # epilogue label
967 cmp %r10,%rbx # context->Rip>=epilogue label
968 jae
.Lcommon_seh_tail
970 mov
192($context),%r10 # pull $num
971 mov
8(%rax,%r10,8),%rax # pull saved stack pointer
974 movaps
16(%rax),%xmm1
975 lea
`40+48`(%rax),%rax
983 mov
%rbx,144($context) # restore context->Rbx
984 mov
%rbp,160($context) # restore context->Rbp
985 mov
%r12,216($context) # restore context->R12
986 mov
%r13,224($context) # restore context->R13
987 mov
%r14,232($context) # restore context->R14
988 mov
%r15,240($context) # restore context->R15
989 movups
%xmm0,512($context) # restore context->Xmm6
990 movups
%xmm1,528($context) # restore context->Xmm7
995 mov
%rax,152($context) # restore context->Rsp
996 mov
%rsi,168($context) # restore context->Rsi
997 mov
%rdi,176($context) # restore context->Rdi
999 mov
40($disp),%rdi # disp->ContextRecord
1000 mov
$context,%rsi # context
1001 mov \
$154,%ecx # sizeof(CONTEXT)
1002 .long
0xa548f3fc # cld; rep movsq
1005 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
1006 mov
8(%rsi),%rdx # arg2, disp->ImageBase
1007 mov
0(%rsi),%r8 # arg3, disp->ControlPc
1008 mov
16(%rsi),%r9 # arg4, disp->FunctionEntry
1009 mov
40(%rsi),%r10 # disp->ContextRecord
1010 lea
56(%rsi),%r11 # &disp->HandlerData
1011 lea
24(%rsi),%r12 # &disp->EstablisherFrame
1012 mov
%r10,32(%rsp) # arg5
1013 mov
%r11,40(%rsp) # arg6
1014 mov
%r12,48(%rsp) # arg7
1015 mov
%rcx,56(%rsp) # arg8, (NULL)
1016 call
*__imp_RtlVirtualUnwind
(%rip)
1018 mov \
$1,%eax # ExceptionContinueSearch
1030 .size mul_handler
,.-mul_handler
1034 .rva
.LSEH_begin_bn_mul_mont_gather5
1035 .rva
.LSEH_end_bn_mul_mont_gather5
1036 .rva
.LSEH_info_bn_mul_mont_gather5
1038 .rva
.LSEH_begin_bn_mul4x_mont_gather5
1039 .rva
.LSEH_end_bn_mul4x_mont_gather5
1040 .rva
.LSEH_info_bn_mul4x_mont_gather5
1042 .rva
.LSEH_begin_bn_gather5
1043 .rva
.LSEH_end_bn_gather5
1044 .rva
.LSEH_info_bn_gather5
1048 .LSEH_info_bn_mul_mont_gather5
:
1051 .rva
.Lmul_alloca
,.Lmul_body
,.Lmul_epilogue
# HandlerData[]
1053 .LSEH_info_bn_mul4x_mont_gather5
:
1056 .rva
.Lmul4x_alloca
,.Lmul4x_body
,.Lmul4x_epilogue
# HandlerData[]
1058 .LSEH_info_bn_gather5
:
1059 .byte
0x01,0x0d,0x05,0x00
1060 .byte
0x0d,0x78,0x01,0x00 #movaps 0x10(rsp),xmm7
1061 .byte
0x08,0x68,0x00,0x00 #movaps (rsp),xmm6
1062 .byte
0x04,0x42,0x00,0x00 #sub rsp,0x28
1067 $code =~ s/\`([^\`]*)\`/eval($1)/gem;