Update - OpenSSL 1.1.1-pre7-dev
This commit is contained in:
@@ -297,15 +297,12 @@ bn_mul_mont:
|
||||
mov sp,$tp
|
||||
mov $bp,$rp # restore rp
|
||||
|
||||
and sp,$hi0,$ap
|
||||
bic $bp,$hi0,$bp
|
||||
bis $bp,$ap,$ap # ap=borrow?tp:rp
|
||||
|
||||
.align 4
|
||||
.Lcopy: ldq $aj,0($ap) # copy or in-place refresh
|
||||
.Lcopy: ldq $aj,0($tp) # conditional copy
|
||||
ldq $nj,0($rp)
|
||||
lda $tp,8($tp)
|
||||
lda $rp,8($rp)
|
||||
lda $ap,8($ap)
|
||||
cmoveq $hi0,$nj,$aj
|
||||
stq zero,-8($tp) # zap tp
|
||||
cmpult $tp,$tj,AT
|
||||
stq $aj,-8($rp)
|
||||
|
||||
@@ -262,14 +262,15 @@ bn_mul_mont:
|
||||
mov $tp,sp @ "rewind" $tp
|
||||
sub $rp,$rp,$aj @ "rewind" $rp
|
||||
|
||||
and $ap,$tp,$nhi
|
||||
bic $np,$rp,$nhi
|
||||
orr $ap,$ap,$np @ ap=borrow?tp:rp
|
||||
|
||||
.Lcopy: ldr $tj,[$ap],#4 @ copy or in-place refresh
|
||||
.Lcopy: ldr $tj,[$tp] @ conditional copy
|
||||
ldr $aj,[$rp]
|
||||
str sp,[$tp],#4 @ zap tp
|
||||
str $tj,[$rp],#4
|
||||
cmp $tp,$num
|
||||
#ifdef __thumb2__
|
||||
it cc
|
||||
#endif
|
||||
movcc $aj,$tj
|
||||
str $aj,[$rp],#4
|
||||
teq $tp,$num @ preserve carry
|
||||
bne .Lcopy
|
||||
|
||||
mov sp,$num
|
||||
|
||||
@@ -341,19 +341,19 @@ bn_mul_mont_general:
|
||||
{ .mmb; sub rptr=rptr,len // rewind
|
||||
sub tptr=tptr,len
|
||||
clrrrb.pr };;
|
||||
{ .mmi; and aptr=tptr,topbit
|
||||
andcm bptr=rptr,topbit
|
||||
{ .mmi; mov aptr=rptr
|
||||
mov bptr=tptr
|
||||
mov pr.rot=1<<16 };;
|
||||
{ .mii; or nptr=aptr,bptr
|
||||
{ .mii; cmp.eq p0,p6=topbit,r0
|
||||
mov ar.lc=lc
|
||||
mov ar.ec=3 };;
|
||||
mov ar.ec=2 };;
|
||||
|
||||
.Lcopy_ctop:
|
||||
{ .mmb; (p16) ld8 n[0]=[nptr],8
|
||||
(p18) st8 [tptr]=r0,8
|
||||
(p16) nop.b 0 }
|
||||
{ .mmb; (p16) nop.m 0
|
||||
(p18) st8 [rptr]=n[2],8
|
||||
{ .mmi; (p16) ld8 a[0]=[aptr],8
|
||||
(p16) ld8 t[0]=[bptr],8
|
||||
(p6) mov a[1]=t[1] };; // (p17)
|
||||
{ .mmb; (p17) st8 [rptr]=a[1],8
|
||||
(p17) st8 [tptr]=r0,8
|
||||
br.ctop.sptk .Lcopy_ctop };;
|
||||
.Lcopy_cend:
|
||||
|
||||
|
||||
@@ -386,15 +386,13 @@ $code.=<<___;
|
||||
$PTR_SUB $rp,$num # restore rp
|
||||
not $hi1,$hi0
|
||||
|
||||
and $ap,$hi0,$sp
|
||||
and $bp,$hi1,$rp
|
||||
or $ap,$ap,$bp # ap=borrow?tp:rp
|
||||
|
||||
.align 4
|
||||
.Lcopy: $LD $aj,($ap)
|
||||
$PTR_ADD $ap,$BNSZ
|
||||
.Lcopy: $LD $nj,($tp) # conditional move
|
||||
$LD $aj,($rp)
|
||||
$ST $zero,($tp)
|
||||
$PTR_ADD $tp,$BNSZ
|
||||
and $nj,$hi0
|
||||
and $aj,$hi1
|
||||
or $aj,$nj
|
||||
sltu $at,$tp,$tj
|
||||
$ST $aj,($rp)
|
||||
bnez $at,.Lcopy
|
||||
|
||||
@@ -517,7 +517,6 @@ L\$sub
|
||||
stws,ma $hi1,4($rp)
|
||||
|
||||
subb $ti0,%r0,$hi1
|
||||
ldo -4($tp),$tp
|
||||
___
|
||||
$code.=<<___ if ($BN_SZ==8);
|
||||
ldd,ma 8($tp),$ti0
|
||||
@@ -532,20 +531,18 @@ L\$sub
|
||||
|
||||
extrd,u $ti0,31,32,$ti0 ; carry in flipped word order
|
||||
sub,db $ti0,%r0,$hi1
|
||||
ldo -8($tp),$tp
|
||||
___
|
||||
$code.=<<___;
|
||||
and $tp,$hi1,$ap
|
||||
andcm $rp,$hi1,$bp
|
||||
or $ap,$bp,$np
|
||||
|
||||
ldo `$LOCALS+32`($fp),$tp
|
||||
sub $rp,$arrsz,$rp ; rewind rp
|
||||
subi 0,$arrsz,$idx
|
||||
ldo `$LOCALS+32`($fp),$tp
|
||||
L\$copy
|
||||
ldd $idx($np),$hi0
|
||||
ldd 0($tp),$ti0
|
||||
ldd 0($rp),$hi0
|
||||
std,ma %r0,8($tp)
|
||||
addib,<> 8,$idx,.-8 ; L\$copy
|
||||
comiclr,= 0,$hi1,%r0
|
||||
copy $ti0,$hi0
|
||||
addib,<> 8,$idx,L\$copy
|
||||
std,ma $hi0,8($rp)
|
||||
___
|
||||
|
||||
@@ -856,17 +853,16 @@ L\$sub_pa11
|
||||
stws,ma $hi1,4($rp)
|
||||
|
||||
subb $ti0,%r0,$hi1
|
||||
ldo -4($tp),$tp
|
||||
and $tp,$hi1,$ap
|
||||
andcm $rp,$hi1,$bp
|
||||
or $ap,$bp,$np
|
||||
|
||||
ldo `$LOCALS+32`($fp),$tp
|
||||
sub $rp,$arrsz,$rp ; rewind rp
|
||||
subi 0,$arrsz,$idx
|
||||
ldo `$LOCALS+32`($fp),$tp
|
||||
L\$copy_pa11
|
||||
ldwx $idx($np),$hi0
|
||||
ldw 0($tp),$ti0
|
||||
ldw 0($rp),$hi0
|
||||
stws,ma %r0,4($tp)
|
||||
comiclr,= 0,$hi1,%r0
|
||||
copy $ti0,$hi0
|
||||
addib,<> 4,$idx,L\$copy_pa11
|
||||
stws,ma $hi0,4($rp)
|
||||
|
||||
|
||||
@@ -314,15 +314,16 @@ Lsub: $LDX $tj,$tp,$j
|
||||
li $j,0
|
||||
mtctr $num
|
||||
subfe $ovf,$j,$ovf ; handle upmost overflow bit
|
||||
and $ap,$tp,$ovf
|
||||
andc $np,$rp,$ovf
|
||||
or $ap,$ap,$np ; ap=borrow?tp:rp
|
||||
|
||||
.align 4
|
||||
Lcopy: ; copy or in-place refresh
|
||||
$LDX $tj,$ap,$j
|
||||
$STX $tj,$rp,$j
|
||||
Lcopy: ; conditional copy
|
||||
$LDX $tj,$tp,$j
|
||||
$LDX $aj,$rp,$j
|
||||
and $tj,$tj,$ovf
|
||||
andc $aj,$aj,$ovf
|
||||
$STX $j,$tp,$j ; zap at once
|
||||
or $aj,$aj,$tj
|
||||
$STX $aj,$rp,$j
|
||||
addi $j,$j,$BNSZ
|
||||
bdnz Lcopy
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#! /usr/bin/env perl
|
||||
# Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
# Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the OpenSSL license (the "License"). You may not use
|
||||
# this file except in compliance with the License. You can obtain a copy
|
||||
@@ -40,7 +40,7 @@
|
||||
#dsa 512 bits 0.0087s 0.0106s 114.3 94.5
|
||||
#dsa 1024 bits 0.0256s 0.0313s 39.0 32.0
|
||||
#
|
||||
# Same bechmark with this assembler code:
|
||||
# Same benchmark with this assembler code:
|
||||
#
|
||||
#rsa 512 bits 0.0056s 0.0005s 178.6 2049.2
|
||||
#rsa 1024 bits 0.0283s 0.0015s 35.3 674.1
|
||||
|
||||
+29
-12
@@ -1501,16 +1501,14 @@ Lsub: ldx $t0,$tp,$i
|
||||
|
||||
li $i,0
|
||||
subfe $ovf,$i,$ovf ; handle upmost overflow bit
|
||||
and $ap,$tp,$ovf
|
||||
andc $np,$rp,$ovf
|
||||
or $ap,$ap,$np ; ap=borrow?tp:rp
|
||||
addi $t7,$ap,8
|
||||
mtctr $j
|
||||
|
||||
.align 4
|
||||
Lcopy: ; copy or in-place refresh
|
||||
ldx $t0,$ap,$i
|
||||
ldx $t1,$t7,$i
|
||||
Lcopy: ; conditional copy
|
||||
ldx $t0,$tp,$i
|
||||
ldx $t1,$t4,$i
|
||||
ldx $t2,$rp,$i
|
||||
ldx $t3,$t6,$i
|
||||
std $i,8($nap_d) ; zap nap_d
|
||||
std $i,16($nap_d)
|
||||
std $i,24($nap_d)
|
||||
@@ -1519,6 +1517,12 @@ Lcopy: ; copy or in-place refresh
|
||||
std $i,48($nap_d)
|
||||
std $i,56($nap_d)
|
||||
stdu $i,64($nap_d)
|
||||
and $t0,$t0,$ovf
|
||||
and $t1,$t1,$ovf
|
||||
andc $t2,$t2,$ovf
|
||||
andc $t3,$t3,$ovf
|
||||
or $t0,$t0,$t2
|
||||
or $t1,$t1,$t3
|
||||
stdx $t0,$rp,$i
|
||||
stdx $t1,$t6,$i
|
||||
stdx $i,$tp,$i ; zap tp at once
|
||||
@@ -1561,20 +1565,21 @@ Lsub: lwz $t0,12($tp) ; load tp[j..j+3] in 64-bit word order
|
||||
|
||||
li $i,0
|
||||
subfe $ovf,$i,$ovf ; handle upmost overflow bit
|
||||
addi $tp,$sp,`$FRAME+$TRANSFER+4`
|
||||
addi $ap,$sp,`$FRAME+$TRANSFER+4`
|
||||
subf $rp,$num,$rp ; rewind rp
|
||||
and $ap,$tp,$ovf
|
||||
andc $np,$rp,$ovf
|
||||
or $ap,$ap,$np ; ap=borrow?tp:rp
|
||||
addi $tp,$sp,`$FRAME+$TRANSFER`
|
||||
mtctr $j
|
||||
|
||||
.align 4
|
||||
Lcopy: ; copy or in-place refresh
|
||||
Lcopy: ; conditional copy
|
||||
lwz $t0,4($ap)
|
||||
lwz $t1,8($ap)
|
||||
lwz $t2,12($ap)
|
||||
lwzu $t3,16($ap)
|
||||
lwz $t4,4($rp)
|
||||
lwz $t5,8($rp)
|
||||
lwz $t6,12($rp)
|
||||
lwz $t7,16($rp)
|
||||
std $i,8($nap_d) ; zap nap_d
|
||||
std $i,16($nap_d)
|
||||
std $i,24($nap_d)
|
||||
@@ -1583,6 +1588,18 @@ Lcopy: ; copy or in-place refresh
|
||||
std $i,48($nap_d)
|
||||
std $i,56($nap_d)
|
||||
stdu $i,64($nap_d)
|
||||
and $t0,$t0,$ovf
|
||||
and $t1,$t1,$ovf
|
||||
and $t2,$t2,$ovf
|
||||
and $t3,$t3,$ovf
|
||||
andc $t4,$t4,$ovf
|
||||
andc $t5,$t5,$ovf
|
||||
andc $t6,$t6,$ovf
|
||||
andc $t7,$t7,$ovf
|
||||
or $t0,$t0,$t4
|
||||
or $t1,$t1,$t5
|
||||
or $t2,$t2,$t6
|
||||
or $t3,$t3,$t7
|
||||
stw $t0,4($rp)
|
||||
stw $t1,8($rp)
|
||||
stw $t2,12($rp)
|
||||
|
||||
@@ -252,16 +252,16 @@ $code.=<<___;
|
||||
brct $count,.Lsub
|
||||
lghi $ahi,0
|
||||
slbgr $AHI,$ahi # handle upmost carry
|
||||
|
||||
ngr $ap,$AHI
|
||||
lghi $np,-1
|
||||
xgr $np,$AHI
|
||||
ngr $np,$rp
|
||||
ogr $ap,$np # ap=borrow?tp:rp
|
||||
lghi $NHI,-1
|
||||
xgr $NHI,$AHI
|
||||
|
||||
la $j,0(%r0)
|
||||
lgr $count,$num
|
||||
.Lcopy: lg $alo,0($j,$ap) # copy or in-place refresh
|
||||
.Lcopy: lg $ahi,$stdframe($j,$sp) # conditional copy
|
||||
lg $alo,0($j,$rp)
|
||||
ngr $ahi,$AHI
|
||||
ngr $alo,$NHI
|
||||
ogr $alo,$ahi
|
||||
_dswap $alo
|
||||
stg $j,$stdframe($j,$sp) # zap tp
|
||||
stg $alo,0($j,$rp)
|
||||
|
||||
@@ -888,19 +888,17 @@ $code.=<<___;
|
||||
sub $tp, $num, $tp
|
||||
sub $rp, $num, $rp
|
||||
|
||||
subc $ovf, %g0, $ovf ! handle upmost overflow bit
|
||||
and $tp, $ovf, $ap
|
||||
andn $rp, $ovf, $np
|
||||
or $np, $ap, $ap ! ap=borrow?tp:rp
|
||||
subccc $ovf, %g0, $ovf ! handle upmost overflow bit
|
||||
ba .Lcopy
|
||||
sub $num, 8, $cnt
|
||||
|
||||
.align 16
|
||||
.Lcopy: ! copy or in-place refresh
|
||||
ldx [$ap+0], $t2
|
||||
add $ap, 8, $ap
|
||||
.Lcopy: ! conditional copy
|
||||
ldx [$tp], $tj
|
||||
ldx [$rp+0], $t2
|
||||
stx %g0, [$tp] ! zap
|
||||
add $tp, 8, $tp
|
||||
movcs %icc, $tj, $t2
|
||||
stx $t2, [$rp+0]
|
||||
add $rp, 8, $rp
|
||||
brnz $cnt, .Lcopy
|
||||
@@ -1136,19 +1134,17 @@ $code.=<<___;
|
||||
sub $tp, $num, $tp
|
||||
sub $rp, $num, $rp
|
||||
|
||||
subc $ovf, %g0, $ovf ! handle upmost overflow bit
|
||||
and $tp, $ovf, $ap
|
||||
andn $rp, $ovf, $np
|
||||
or $np, $ap, $ap ! ap=borrow?tp:rp
|
||||
subccc $ovf, %g0, $ovf ! handle upmost overflow bit
|
||||
ba .Lcopy_g5
|
||||
sub $num, 8, $cnt
|
||||
|
||||
.align 16
|
||||
.Lcopy_g5: ! copy or in-place refresh
|
||||
ldx [$ap+0], $t2
|
||||
add $ap, 8, $ap
|
||||
.Lcopy_g5: ! conditional copy
|
||||
ldx [$tp], $tj
|
||||
ldx [$rp+0], $t2
|
||||
stx %g0, [$tp] ! zap
|
||||
add $tp, 8, $tp
|
||||
movcs %icc, $tj, $t2
|
||||
stx $t2, [$rp+0]
|
||||
add $rp, 8, $rp
|
||||
brnz $cnt, .Lcopy_g5
|
||||
|
||||
@@ -265,7 +265,6 @@ $fname:
|
||||
.Ltail:
|
||||
add $np,$num,$np
|
||||
add $rp,$num,$rp
|
||||
mov $tp,$ap
|
||||
sub %g0,$num,%o7 ! k=-num
|
||||
ba .Lsub
|
||||
subcc %g0,%g0,%g0 ! clear %icc.c
|
||||
@@ -278,15 +277,14 @@ $fname:
|
||||
add %o7,4,%o7
|
||||
brnz %o7,.Lsub
|
||||
st %o1,[$i]
|
||||
subc $car2,0,$car2 ! handle upmost overflow bit
|
||||
and $tp,$car2,$ap
|
||||
andn $rp,$car2,$np
|
||||
or $ap,$np,$ap
|
||||
subccc $car2,0,$car2 ! handle upmost overflow bit
|
||||
sub %g0,$num,%o7
|
||||
|
||||
.Lcopy:
|
||||
ld [$ap+%o7],%o0 ! copy or in-place refresh
|
||||
ld [$tp+%o7],%o1 ! conditional copy
|
||||
ld [$rp+%o7],%o0
|
||||
st %g0,[$tp+%o7] ! zap tp
|
||||
movcs %icc,%o1,%o0
|
||||
st %o0,[$rp+%o7]
|
||||
add %o7,4,%o7
|
||||
brnz %o7,.Lcopy
|
||||
|
||||
@@ -213,18 +213,15 @@ $sp=&DWP(28,"esp");
|
||||
|
||||
&mov ("eax",&DWP(0,"esi","edx",4)); # upmost overflow bit
|
||||
&sbb ("eax",0);
|
||||
&and ("esi","eax");
|
||||
¬ ("eax");
|
||||
&mov ("ebp","edi");
|
||||
&and ("ebp","eax");
|
||||
&or ("esi","ebp"); # tp=carry?tp:rp
|
||||
|
||||
&mov ("ecx","edx"); # num
|
||||
&xor ("edx","edx"); # i=0
|
||||
&mov ("edx",0); # i=0
|
||||
|
||||
&set_label("copy",8);
|
||||
&mov ("eax",&DWP(0,"esi","edx",4));
|
||||
&mov (&DWP(64,"esp","edx",4),"ecx"); # zap tp
|
||||
&mov ("ebx",&DWP(0,"esi","edx",4));
|
||||
&mov ("eax",&DWP(0,"edi","edx",4));
|
||||
&mov (&DWP(0,"esi","edx",4),"ecx"); # zap tp
|
||||
&cmovc ("eax","ebx");
|
||||
&mov (&DWP(0,"edi","edx",4),"eax");
|
||||
&lea ("edx",&DWP(1,"edx")); # i++
|
||||
&loop (&label("copy"));
|
||||
|
||||
@@ -310,23 +310,23 @@ $code.=<<___;
|
||||
sub $anp, $num, $anp
|
||||
sub $rp, $num, $rp
|
||||
|
||||
subc $ovf, %g0, $ovf ! handle upmost overflow bit
|
||||
and $tp, $ovf, $ap
|
||||
andn $rp, $ovf, $np
|
||||
or $np, $ap, $ap ! ap=borrow?tp:rp
|
||||
subccc $ovf, %g0, $ovf ! handle upmost overflow bit
|
||||
ba .Lcopy
|
||||
sub $num, 8, $cnt
|
||||
|
||||
.align 16
|
||||
.Lcopy: ! copy or in-place refresh
|
||||
ld [$ap+0], $t2
|
||||
ld [$ap+4], $t3
|
||||
add $ap, 8, $ap
|
||||
.Lcopy: ! conditional copy
|
||||
ld [$tp+0], $t0
|
||||
ld [$tp+4], $t1
|
||||
ld [$rp+0], $t2
|
||||
ld [$rp+4], $t3
|
||||
stx %g0, [$tp] ! zap
|
||||
add $tp, 8, $tp
|
||||
stx %g0, [$anp] ! zap
|
||||
stx %g0, [$anp+8]
|
||||
add $anp, 16, $anp
|
||||
movcs %icc, $t0, $t2
|
||||
movcs %icc, $t1, $t3
|
||||
st $t3, [$rp+0] ! flip order
|
||||
st $t2, [$rp+4]
|
||||
add $rp, 8, $rp
|
||||
|
||||
@@ -604,16 +604,18 @@ $sbit=$num;
|
||||
&jge (&label("sub"));
|
||||
|
||||
&sbb ("eax",0); # handle upmost overflow bit
|
||||
&and ($tp,"eax");
|
||||
¬ ("eax");
|
||||
&mov ($np,$rp);
|
||||
&and ($np,"eax");
|
||||
&or ($tp,$np); # tp=carry?tp:rp
|
||||
&mov ("edx",-1);
|
||||
&xor ("edx","eax");
|
||||
&jmp (&label("copy"));
|
||||
|
||||
&set_label("copy",16); # copy or in-place refresh
|
||||
&mov ("eax",&DWP(0,$tp,$num,4));
|
||||
&mov (&DWP(0,$rp,$num,4),"eax"); # rp[i]=tp[i]
|
||||
&set_label("copy",16); # conditional copy
|
||||
&mov ($tp,&DWP($frame,"esp",$num,4));
|
||||
&mov ($np,&DWP(0,$rp,$num,4));
|
||||
&mov (&DWP($frame,"esp",$num,4),$j); # zap temporary vector
|
||||
&and ($tp,"eax");
|
||||
&and ($np,"edx");
|
||||
&or ($np,$tp);
|
||||
&mov (&DWP(0,$rp,$num,4),$np);
|
||||
&dec ($num);
|
||||
&jge (&label("copy"));
|
||||
|
||||
|
||||
@@ -311,30 +311,30 @@ $code.=<<___;
|
||||
|
||||
xor $i,$i # i=0 and clear CF!
|
||||
mov (%rsp),%rax # tp[0]
|
||||
lea (%rsp),$ap # borrow ap for tp
|
||||
mov $num,$j # j=num
|
||||
jmp .Lsub
|
||||
|
||||
.align 16
|
||||
.Lsub: sbb ($np,$i,8),%rax
|
||||
mov %rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
|
||||
mov 8($ap,$i,8),%rax # tp[i+1]
|
||||
mov 8(%rsp,$i,8),%rax # tp[i+1]
|
||||
lea 1($i),$i # i++
|
||||
dec $j # doesn't affect CF!
|
||||
jnz .Lsub
|
||||
|
||||
sbb \$0,%rax # handle upmost overflow bit
|
||||
mov \$-1,%rbx
|
||||
xor %rax,%rbx # not %rax
|
||||
xor $i,$i
|
||||
and %rax,$ap
|
||||
not %rax
|
||||
mov $rp,$np
|
||||
and %rax,$np
|
||||
mov $num,$j # j=num
|
||||
or $np,$ap # ap=borrow?tp:rp
|
||||
.align 16
|
||||
.Lcopy: # copy or in-place refresh
|
||||
mov ($ap,$i,8),%rax
|
||||
mov $i,(%rsp,$i,8) # zap temporary vector
|
||||
mov %rax,($rp,$i,8) # rp[i]=tp[i]
|
||||
|
||||
.Lcopy: # conditional copy
|
||||
mov ($rp,$i,8),%rcx
|
||||
mov (%rsp,$i,8),%rdx
|
||||
and %rbx,%rcx
|
||||
and %rax,%rdx
|
||||
mov $num,(%rsp,$i,8) # zap temporary vector
|
||||
or %rcx,%rdx
|
||||
mov %rdx,($rp,$i,8) # rp[i]=tp[i]
|
||||
lea 1($i),$i
|
||||
sub \$1,$j
|
||||
jnz .Lcopy
|
||||
@@ -724,7 +724,6 @@ $code.=<<___;
|
||||
mov 16(%rsp,$num,8),$rp # restore $rp
|
||||
lea -4($num),$j
|
||||
mov 0(%rsp),@ri[0] # tp[0]
|
||||
pxor %xmm0,%xmm0
|
||||
mov 8(%rsp),@ri[1] # tp[1]
|
||||
shr \$2,$j # j=num/4-1
|
||||
lea (%rsp),$ap # borrow ap for tp
|
||||
@@ -734,8 +733,7 @@ $code.=<<___;
|
||||
mov 16($ap),@ri[2] # tp[2]
|
||||
mov 24($ap),@ri[3] # tp[3]
|
||||
sbb 8($np),@ri[1]
|
||||
jmp .Lsub4x
|
||||
.align 16
|
||||
|
||||
.Lsub4x:
|
||||
mov @ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
|
||||
mov @ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
|
||||
@@ -762,34 +760,35 @@ $code.=<<___;
|
||||
|
||||
sbb \$0,@ri[0] # handle upmost overflow bit
|
||||
mov @ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
|
||||
xor $i,$i # i=0
|
||||
and @ri[0],$ap
|
||||
not @ri[0]
|
||||
mov $rp,$np
|
||||
and @ri[0],$np
|
||||
lea -4($num),$j
|
||||
or $np,$ap # ap=borrow?tp:rp
|
||||
shr \$2,$j # j=num/4-1
|
||||
pxor %xmm0,%xmm0
|
||||
movq @ri[0],%xmm4
|
||||
pcmpeqd %xmm5,%xmm5
|
||||
pshufd \$0,%xmm4,%xmm4
|
||||
mov $num,$j
|
||||
pxor %xmm4,%xmm5
|
||||
shr \$2,$j # j=num/4
|
||||
xor %eax,%eax # i=0
|
||||
|
||||
movdqu ($ap),%xmm1
|
||||
movdqa %xmm0,(%rsp)
|
||||
movdqu %xmm1,($rp)
|
||||
jmp .Lcopy4x
|
||||
.align 16
|
||||
.Lcopy4x: # copy or in-place refresh
|
||||
movdqu 16($ap,$i),%xmm2
|
||||
movdqu 32($ap,$i),%xmm1
|
||||
movdqa %xmm0,16(%rsp,$i)
|
||||
movdqu %xmm2,16($rp,$i)
|
||||
movdqa %xmm0,32(%rsp,$i)
|
||||
movdqu %xmm1,32($rp,$i)
|
||||
lea 32($i),$i
|
||||
.Lcopy4x: # conditional copy
|
||||
movdqa (%rsp,%rax),%xmm1
|
||||
movdqu ($rp,%rax),%xmm2
|
||||
pand %xmm4,%xmm1
|
||||
pand %xmm5,%xmm2
|
||||
movdqa 16(%rsp,%rax),%xmm3
|
||||
movdqa %xmm0,(%rsp,%rax)
|
||||
por %xmm2,%xmm1
|
||||
movdqu 16($rp,%rax),%xmm2
|
||||
movdqu %xmm1,($rp,%rax)
|
||||
pand %xmm4,%xmm3
|
||||
pand %xmm5,%xmm2
|
||||
movdqa %xmm0,16(%rsp,%rax)
|
||||
por %xmm2,%xmm3
|
||||
movdqu %xmm3,16($rp,%rax)
|
||||
lea 32(%rax),%rax
|
||||
dec $j
|
||||
jnz .Lcopy4x
|
||||
|
||||
movdqu 16($ap,$i),%xmm2
|
||||
movdqa %xmm0,16(%rsp,$i)
|
||||
movdqu %xmm2,16($rp,$i)
|
||||
___
|
||||
}
|
||||
$code.=<<___;
|
||||
|
||||
@@ -423,18 +423,19 @@ $code.=<<___;
|
||||
jnz .Lsub
|
||||
|
||||
sbb \$0,%rax # handle upmost overflow bit
|
||||
mov \$-1,%rbx
|
||||
xor %rax,%rbx
|
||||
xor $i,$i
|
||||
and %rax,$ap
|
||||
not %rax
|
||||
mov $rp,$np
|
||||
and %rax,$np
|
||||
mov $num,$j # j=num
|
||||
or $np,$ap # ap=borrow?tp:rp
|
||||
.align 16
|
||||
.Lcopy: # copy or in-place refresh
|
||||
mov ($ap,$i,8),%rax
|
||||
|
||||
.Lcopy: # conditional copy
|
||||
mov ($rp,$i,8),%rcx
|
||||
mov (%rsp,$i,8),%rdx
|
||||
and %rbx,%rcx
|
||||
and %rax,%rdx
|
||||
mov $i,(%rsp,$i,8) # zap temporary vector
|
||||
mov %rax,($rp,$i,8) # rp[i]=tp[i]
|
||||
or %rcx,%rdx
|
||||
mov %rdx,($rp,$i,8) # rp[i]=tp[i]
|
||||
lea 1($i),$i
|
||||
sub \$1,$j
|
||||
jnz .Lcopy
|
||||
|
||||
+50
-88
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -10,51 +10,69 @@
|
||||
#include "internal/cryptlib.h"
|
||||
#include "bn_lcl.h"
|
||||
|
||||
/* r can == a or b */
|
||||
/* signed add of b to a. */
|
||||
int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
|
||||
{
|
||||
int a_neg = a->neg, ret;
|
||||
int ret, r_neg, cmp_res;
|
||||
|
||||
bn_check_top(a);
|
||||
bn_check_top(b);
|
||||
|
||||
/*-
|
||||
* a + b a+b
|
||||
* a + -b a-b
|
||||
* -a + b b-a
|
||||
* -a + -b -(a+b)
|
||||
*/
|
||||
if (a_neg ^ b->neg) {
|
||||
/* only one is negative */
|
||||
if (a_neg) {
|
||||
const BIGNUM *tmp;
|
||||
|
||||
tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
}
|
||||
|
||||
/* we are now a - b */
|
||||
|
||||
if (BN_ucmp(a, b) < 0) {
|
||||
if (!BN_usub(r, b, a))
|
||||
return 0;
|
||||
r->neg = 1;
|
||||
if (a->neg == b->neg) {
|
||||
r_neg = a->neg;
|
||||
ret = BN_uadd(r, a, b);
|
||||
} else {
|
||||
cmp_res = BN_ucmp(a, b);
|
||||
if (cmp_res > 0) {
|
||||
r_neg = a->neg;
|
||||
ret = BN_usub(r, a, b);
|
||||
} else if (cmp_res < 0) {
|
||||
r_neg = b->neg;
|
||||
ret = BN_usub(r, b, a);
|
||||
} else {
|
||||
if (!BN_usub(r, a, b))
|
||||
return 0;
|
||||
r->neg = 0;
|
||||
r_neg = 0;
|
||||
BN_zero(r);
|
||||
ret = 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
ret = BN_uadd(r, a, b);
|
||||
r->neg = a_neg;
|
||||
r->neg = r_neg;
|
||||
bn_check_top(r);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* unsigned add of b to a */
|
||||
/* signed sub of b from a. */
|
||||
int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
|
||||
{
|
||||
int ret, r_neg, cmp_res;
|
||||
|
||||
bn_check_top(a);
|
||||
bn_check_top(b);
|
||||
|
||||
if (a->neg != b->neg) {
|
||||
r_neg = a->neg;
|
||||
ret = BN_uadd(r, a, b);
|
||||
} else {
|
||||
cmp_res = BN_ucmp(a, b);
|
||||
if (cmp_res > 0) {
|
||||
r_neg = a->neg;
|
||||
ret = BN_usub(r, a, b);
|
||||
} else if (cmp_res < 0) {
|
||||
r_neg = !b->neg;
|
||||
ret = BN_usub(r, b, a);
|
||||
} else {
|
||||
r_neg = 0;
|
||||
BN_zero(r);
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
|
||||
r->neg = r_neg;
|
||||
bn_check_top(r);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* unsigned add of b to a, r can be equal to a or b. */
|
||||
int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
|
||||
{
|
||||
int max, min, dif;
|
||||
@@ -151,59 +169,3 @@ int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
|
||||
{
|
||||
int max;
|
||||
int add = 0, neg = 0;
|
||||
|
||||
bn_check_top(a);
|
||||
bn_check_top(b);
|
||||
|
||||
/*-
|
||||
* a - b a-b
|
||||
* a - -b a+b
|
||||
* -a - b -(a+b)
|
||||
* -a - -b b-a
|
||||
*/
|
||||
if (a->neg) {
|
||||
if (b->neg) {
|
||||
const BIGNUM *tmp;
|
||||
|
||||
tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
} else {
|
||||
add = 1;
|
||||
neg = 1;
|
||||
}
|
||||
} else {
|
||||
if (b->neg) {
|
||||
add = 1;
|
||||
neg = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (add) {
|
||||
if (!BN_uadd(r, a, b))
|
||||
return 0;
|
||||
r->neg = neg;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* We are actually doing a - b :-) */
|
||||
|
||||
max = (a->top > b->top) ? a->top : b->top;
|
||||
if (bn_wexpand(r, max) == NULL)
|
||||
return 0;
|
||||
if (BN_ucmp(a, b) < 0) {
|
||||
if (!BN_usub(r, b, a))
|
||||
return 0;
|
||||
r->neg = 1;
|
||||
} else {
|
||||
if (!BN_usub(r, a, b))
|
||||
return 0;
|
||||
r->neg = 0;
|
||||
}
|
||||
bn_check_top(r);
|
||||
return 1;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -82,7 +82,6 @@ void BN_BLINDING_free(BN_BLINDING *r)
|
||||
{
|
||||
if (r == NULL)
|
||||
return;
|
||||
|
||||
BN_free(r->A);
|
||||
BN_free(r->Ai);
|
||||
BN_free(r->e);
|
||||
@@ -251,7 +250,7 @@ BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
|
||||
|
||||
do {
|
||||
int rv;
|
||||
if (!BN_rand_range(ret->A, ret->mod))
|
||||
if (!BN_priv_rand_range(ret->A, ret->mod))
|
||||
goto err;
|
||||
if (!int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv)) {
|
||||
/*
|
||||
|
||||
+11
-5
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -255,9 +255,12 @@ static int BN_STACK_push(BN_STACK *st, unsigned int idx)
|
||||
/* Need to expand */
|
||||
unsigned int newsize =
|
||||
st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;
|
||||
unsigned int *newitems = OPENSSL_malloc(sizeof(*newitems) * newsize);
|
||||
if (newitems == NULL)
|
||||
unsigned int *newitems;
|
||||
|
||||
if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL) {
|
||||
BNerr(BN_F_BN_STACK_PUSH, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
if (st->depth)
|
||||
memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);
|
||||
OPENSSL_free(st->indexes);
|
||||
@@ -306,9 +309,12 @@ static BIGNUM *BN_POOL_get(BN_POOL *p, int flag)
|
||||
|
||||
/* Full; allocate a new pool item and link it in. */
|
||||
if (p->used == p->size) {
|
||||
BN_POOL_ITEM *item = OPENSSL_malloc(sizeof(*item));
|
||||
if (item == NULL)
|
||||
BN_POOL_ITEM *item;
|
||||
|
||||
if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) {
|
||||
BNerr(BN_F_BN_POOL_GET, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
for (loop = 0, bn = item->vals; loop++ < BN_CTX_POOL_SIZE; bn++) {
|
||||
bn_init(bn);
|
||||
if ((flag & BN_FLG_SECURE) != 0)
|
||||
|
||||
+5
-1
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Generated by util/mkerr.pl DO NOT EDIT
|
||||
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -62,12 +62,16 @@ static const ERR_STRING_DATA BN_str_functs[] = {
|
||||
"BN_mod_inverse_no_branch"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_MOD_LSHIFT_QUICK, 0), "BN_mod_lshift_quick"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_MOD_SQRT, 0), "BN_mod_sqrt"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_MONT_CTX_NEW, 0), "BN_MONT_CTX_new"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_MPI2BN, 0), "BN_mpi2bn"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_NEW, 0), "BN_new"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_POOL_GET, 0), "BN_POOL_get"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_RAND, 0), "BN_rand"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_RAND_RANGE, 0), "BN_rand_range"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_RECP_CTX_NEW, 0), "BN_RECP_CTX_new"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_RSHIFT, 0), "BN_rshift"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_SET_WORDS, 0), "bn_set_words"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_STACK_PUSH, 0), "BN_STACK_push"},
|
||||
{ERR_PACK(ERR_LIB_BN, BN_F_BN_USUB, 0), "BN_usub"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
+8
-1
@@ -140,7 +140,14 @@ BIGNUM *int_bn_mod_inverse(BIGNUM *in,
|
||||
BIGNUM *ret = NULL;
|
||||
int sign;
|
||||
|
||||
if (pnoinv)
|
||||
/* This is invalid input so we don't worry about constant time here */
|
||||
if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {
|
||||
if (pnoinv != NULL)
|
||||
*pnoinv = 1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pnoinv != NULL)
|
||||
*pnoinv = 0;
|
||||
|
||||
if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
|
||||
|
||||
+59
-105
@@ -22,30 +22,32 @@
|
||||
*/
|
||||
# define MAX_ITERATIONS 50
|
||||
|
||||
static const BN_ULONG SQR_tb[16] = { 0, 1, 4, 5, 16, 17, 20, 21,
|
||||
64, 65, 68, 69, 80, 81, 84, 85
|
||||
};
|
||||
# define SQR_nibble(w) ((((w) & 8) << 3) \
|
||||
| (((w) & 4) << 2) \
|
||||
| (((w) & 2) << 1) \
|
||||
| ((w) & 1))
|
||||
|
||||
|
||||
/* Platform-specific macros to accelerate squaring. */
|
||||
# if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
|
||||
# define SQR1(w) \
|
||||
SQR_tb[(w) >> 60 & 0xF] << 56 | SQR_tb[(w) >> 56 & 0xF] << 48 | \
|
||||
SQR_tb[(w) >> 52 & 0xF] << 40 | SQR_tb[(w) >> 48 & 0xF] << 32 | \
|
||||
SQR_tb[(w) >> 44 & 0xF] << 24 | SQR_tb[(w) >> 40 & 0xF] << 16 | \
|
||||
SQR_tb[(w) >> 36 & 0xF] << 8 | SQR_tb[(w) >> 32 & 0xF]
|
||||
SQR_nibble((w) >> 60) << 56 | SQR_nibble((w) >> 56) << 48 | \
|
||||
SQR_nibble((w) >> 52) << 40 | SQR_nibble((w) >> 48) << 32 | \
|
||||
SQR_nibble((w) >> 44) << 24 | SQR_nibble((w) >> 40) << 16 | \
|
||||
SQR_nibble((w) >> 36) << 8 | SQR_nibble((w) >> 32)
|
||||
# define SQR0(w) \
|
||||
SQR_tb[(w) >> 28 & 0xF] << 56 | SQR_tb[(w) >> 24 & 0xF] << 48 | \
|
||||
SQR_tb[(w) >> 20 & 0xF] << 40 | SQR_tb[(w) >> 16 & 0xF] << 32 | \
|
||||
SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
|
||||
SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
|
||||
SQR_nibble((w) >> 28) << 56 | SQR_nibble((w) >> 24) << 48 | \
|
||||
SQR_nibble((w) >> 20) << 40 | SQR_nibble((w) >> 16) << 32 | \
|
||||
SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | \
|
||||
SQR_nibble((w) >> 4) << 8 | SQR_nibble((w) )
|
||||
# endif
|
||||
# ifdef THIRTY_TWO_BIT
|
||||
# define SQR1(w) \
|
||||
SQR_tb[(w) >> 28 & 0xF] << 24 | SQR_tb[(w) >> 24 & 0xF] << 16 | \
|
||||
SQR_tb[(w) >> 20 & 0xF] << 8 | SQR_tb[(w) >> 16 & 0xF]
|
||||
SQR_nibble((w) >> 28) << 24 | SQR_nibble((w) >> 24) << 16 | \
|
||||
SQR_nibble((w) >> 20) << 8 | SQR_nibble((w) >> 16)
|
||||
# define SQR0(w) \
|
||||
SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
|
||||
SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
|
||||
SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | \
|
||||
SQR_nibble((w) >> 4) << 8 | SQR_nibble((w) )
|
||||
# endif
|
||||
|
||||
# if !defined(OPENSSL_BN_ASM_GF2m)
|
||||
@@ -547,7 +549,8 @@ int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
|
||||
* Hernandez, J.L., and Menezes, A. "Software Implementation of Elliptic
|
||||
* Curve Cryptography Over Binary Fields".
|
||||
*/
|
||||
int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
|
||||
static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,
|
||||
const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
|
||||
int ret = 0;
|
||||
@@ -713,6 +716,46 @@ int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*-
|
||||
* Wrapper for BN_GF2m_mod_inv_vartime that blinds the input before calling.
|
||||
* This is not constant time.
|
||||
* But it does eliminate first order deduction on the input.
|
||||
*/
|
||||
int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *b = NULL;
|
||||
int ret = 0;
|
||||
|
||||
BN_CTX_start(ctx);
|
||||
if ((b = BN_CTX_get(ctx)) == NULL)
|
||||
goto err;
|
||||
|
||||
/* generate blinding value */
|
||||
do {
|
||||
if (!BN_priv_rand(b, BN_num_bits(p) - 1,
|
||||
BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
|
||||
goto err;
|
||||
} while (BN_is_zero(b));
|
||||
|
||||
/* r := a * b */
|
||||
if (!BN_GF2m_mod_mul(r, a, b, p, ctx))
|
||||
goto err;
|
||||
|
||||
/* r := 1/(a * b) */
|
||||
if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))
|
||||
goto err;
|
||||
|
||||
/* r := b/(a * b) = 1/a */
|
||||
if (!BN_GF2m_mod_mul(r, r, b, p, ctx))
|
||||
goto err;
|
||||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
BN_CTX_end(ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Invert xx, reduce modulo p, and store the result in r. r could be xx.
|
||||
* This function calls down to the BN_GF2m_mod_inv implementation; this
|
||||
@@ -740,7 +783,6 @@ int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],
|
||||
return ret;
|
||||
}
|
||||
|
||||
# ifndef OPENSSL_SUN_GF2M_DIV
|
||||
/*
|
||||
* Divide y by x, reduce modulo p, and store the result in r. r could be x
|
||||
* or y, x could equal y.
|
||||
@@ -771,94 +813,6 @@ int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
|
||||
BN_CTX_end(ctx);
|
||||
return ret;
|
||||
}
|
||||
# else
|
||||
/*
|
||||
* Divide y by x, reduce modulo p, and store the result in r. r could be x
|
||||
* or y, x could equal y. Uses algorithm Modular_Division_GF(2^m) from
|
||||
* Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to the
|
||||
* Great Divide".
|
||||
*/
|
||||
int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
|
||||
const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *a, *b, *u, *v;
|
||||
int ret = 0;
|
||||
|
||||
bn_check_top(y);
|
||||
bn_check_top(x);
|
||||
bn_check_top(p);
|
||||
|
||||
BN_CTX_start(ctx);
|
||||
|
||||
a = BN_CTX_get(ctx);
|
||||
b = BN_CTX_get(ctx);
|
||||
u = BN_CTX_get(ctx);
|
||||
v = BN_CTX_get(ctx);
|
||||
if (v == NULL)
|
||||
goto err;
|
||||
|
||||
/* reduce x and y mod p */
|
||||
if (!BN_GF2m_mod(u, y, p))
|
||||
goto err;
|
||||
if (!BN_GF2m_mod(a, x, p))
|
||||
goto err;
|
||||
if (!BN_copy(b, p))
|
||||
goto err;
|
||||
|
||||
while (!BN_is_odd(a)) {
|
||||
if (!BN_rshift1(a, a))
|
||||
goto err;
|
||||
if (BN_is_odd(u))
|
||||
if (!BN_GF2m_add(u, u, p))
|
||||
goto err;
|
||||
if (!BN_rshift1(u, u))
|
||||
goto err;
|
||||
}
|
||||
|
||||
do {
|
||||
if (BN_GF2m_cmp(b, a) > 0) {
|
||||
if (!BN_GF2m_add(b, b, a))
|
||||
goto err;
|
||||
if (!BN_GF2m_add(v, v, u))
|
||||
goto err;
|
||||
do {
|
||||
if (!BN_rshift1(b, b))
|
||||
goto err;
|
||||
if (BN_is_odd(v))
|
||||
if (!BN_GF2m_add(v, v, p))
|
||||
goto err;
|
||||
if (!BN_rshift1(v, v))
|
||||
goto err;
|
||||
} while (!BN_is_odd(b));
|
||||
} else if (BN_abs_is_word(a, 1))
|
||||
break;
|
||||
else {
|
||||
if (!BN_GF2m_add(a, a, b))
|
||||
goto err;
|
||||
if (!BN_GF2m_add(u, u, v))
|
||||
goto err;
|
||||
do {
|
||||
if (!BN_rshift1(a, a))
|
||||
goto err;
|
||||
if (BN_is_odd(u))
|
||||
if (!BN_GF2m_add(u, u, p))
|
||||
goto err;
|
||||
if (!BN_rshift1(u, u))
|
||||
goto err;
|
||||
} while (!BN_is_odd(a));
|
||||
}
|
||||
} while (1);
|
||||
|
||||
if (!BN_copy(r, u))
|
||||
goto err;
|
||||
bn_check_top(r);
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
BN_CTX_end(ctx);
|
||||
return ret;
|
||||
}
|
||||
# endif
|
||||
|
||||
/*
|
||||
* Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
|
||||
|
||||
+35
-4
@@ -300,6 +300,11 @@ BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
|
||||
return a;
|
||||
}
|
||||
|
||||
#define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA \
|
||||
| BN_FLG_CONSTTIME \
|
||||
| BN_FLG_SECURE))
|
||||
#define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
|
||||
|
||||
void BN_swap(BIGNUM *a, BIGNUM *b)
|
||||
{
|
||||
int flags_old_a, flags_old_b;
|
||||
@@ -327,10 +332,8 @@ void BN_swap(BIGNUM *a, BIGNUM *b)
|
||||
b->dmax = tmp_dmax;
|
||||
b->neg = tmp_neg;
|
||||
|
||||
a->flags =
|
||||
(flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA);
|
||||
b->flags =
|
||||
(flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA);
|
||||
a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
|
||||
b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
|
||||
bn_check_top(a);
|
||||
bn_check_top(b);
|
||||
}
|
||||
@@ -739,6 +742,34 @@ void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
|
||||
a->top ^= t;
|
||||
b->top ^= t;
|
||||
|
||||
t = (a->neg ^ b->neg) & condition;
|
||||
a->neg ^= t;
|
||||
b->neg ^= t;
|
||||
|
||||
/*-
|
||||
* Idea behind BN_FLG_STATIC_DATA is actually to
|
||||
* indicate that data may not be written to.
|
||||
* Intention is actually to treat it as it's
|
||||
* read-only data, and some (if not most) of it does
|
||||
* reside in read-only segment. In other words
|
||||
* observation of BN_FLG_STATIC_DATA in
|
||||
* BN_consttime_swap should be treated as fatal
|
||||
* condition. It would either cause SEGV or
|
||||
* effectively cause data corruption.
|
||||
* BN_FLG_MALLOCED refers to BN structure itself,
|
||||
* and hence must be preserved. Remaining flags are
|
||||
* BN_FLG_CONSTIME and BN_FLG_SECURE. Latter must be
|
||||
* preserved, because it determines how x->d was
|
||||
* allocated and hence how to free it. This leaves
|
||||
* BN_FLG_CONSTTIME that one can do something about.
|
||||
* To summarize it's sufficient to mask and swap
|
||||
* BN_FLG_CONSTTIME alone. BN_FLG_STATIC_DATA should
|
||||
* be treated as fatal.
|
||||
*/
|
||||
t = ((a->flags ^ b->flags) & BN_FLG_CONSTTIME) & condition;
|
||||
a->flags ^= t;
|
||||
b->flags ^= t;
|
||||
|
||||
#define BN_CONSTTIME_SWAP(ind) \
|
||||
do { \
|
||||
t = (a->d[ind] ^ b->d[ind]) & condition; \
|
||||
|
||||
+17
-10
@@ -95,6 +95,8 @@ static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
|
||||
|
||||
/* clear the top words of T */
|
||||
i = max - r->top;
|
||||
if (i < 0)
|
||||
return 0;
|
||||
if (i)
|
||||
memset(&rp[r->top], 0, sizeof(*rp) * i);
|
||||
|
||||
@@ -197,8 +199,10 @@ BN_MONT_CTX *BN_MONT_CTX_new(void)
|
||||
{
|
||||
BN_MONT_CTX *ret;
|
||||
|
||||
if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
|
||||
if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
|
||||
BNerr(BN_F_BN_MONT_CTX_NEW, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BN_MONT_CTX_init(ret);
|
||||
ret->flags = BN_FLG_MALLOCED;
|
||||
@@ -208,9 +212,9 @@ BN_MONT_CTX *BN_MONT_CTX_new(void)
|
||||
void BN_MONT_CTX_init(BN_MONT_CTX *ctx)
|
||||
{
|
||||
ctx->ri = 0;
|
||||
bn_init(&(ctx->RR));
|
||||
bn_init(&(ctx->N));
|
||||
bn_init(&(ctx->Ni));
|
||||
bn_init(&ctx->RR);
|
||||
bn_init(&ctx->N);
|
||||
bn_init(&ctx->Ni);
|
||||
ctx->n0[0] = ctx->n0[1] = 0;
|
||||
ctx->flags = 0;
|
||||
}
|
||||
@@ -219,10 +223,9 @@ void BN_MONT_CTX_free(BN_MONT_CTX *mont)
|
||||
{
|
||||
if (mont == NULL)
|
||||
return;
|
||||
|
||||
BN_clear_free(&(mont->RR));
|
||||
BN_clear_free(&(mont->N));
|
||||
BN_clear_free(&(mont->Ni));
|
||||
BN_clear_free(&mont->RR);
|
||||
BN_clear_free(&mont->N);
|
||||
BN_clear_free(&mont->Ni);
|
||||
if (mont->flags & BN_FLG_MALLOCED)
|
||||
OPENSSL_free(mont);
|
||||
}
|
||||
@@ -278,7 +281,9 @@ int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
|
||||
if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))
|
||||
tmod.top = 2;
|
||||
|
||||
if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
|
||||
if (BN_is_one(&tmod))
|
||||
BN_zero(Ri);
|
||||
else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
|
||||
goto err;
|
||||
if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))
|
||||
goto err; /* R*Ri */
|
||||
@@ -311,7 +316,9 @@ int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
|
||||
buf[1] = 0;
|
||||
tmod.top = buf[0] != 0 ? 1 : 0;
|
||||
/* Ri = R^-1 mod N */
|
||||
if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
|
||||
if (BN_is_one(&tmod))
|
||||
BN_zero(Ri);
|
||||
else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
|
||||
goto err;
|
||||
if (!BN_lshift(Ri, Ri, BN_BITS2))
|
||||
goto err; /* R*Ri */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -279,6 +279,7 @@ static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
|
||||
char is_single_word = bits <= BN_BITS2;
|
||||
|
||||
again:
|
||||
/* TODO: Not all primes are private */
|
||||
if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
|
||||
return 0;
|
||||
/* we now have a random number 'rnd' to test. */
|
||||
@@ -363,7 +364,7 @@ int bn_probable_prime_dh(BIGNUM *rnd, int bits,
|
||||
if ((t1 = BN_CTX_get(ctx)) == NULL)
|
||||
goto err;
|
||||
|
||||
if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
|
||||
if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
|
||||
goto err;
|
||||
|
||||
/* we need ((rnd-rem) % add) == 0 */
|
||||
@@ -419,7 +420,7 @@ static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
|
||||
if (!BN_rshift1(qadd, padd))
|
||||
goto err;
|
||||
|
||||
if (!BN_priv_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
|
||||
if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
|
||||
goto err;
|
||||
|
||||
/* we need ((rnd-rem) % add) == 0 */
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -239,7 +239,7 @@ int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
|
||||
memset(private_bytes + todo, 0, sizeof(private_bytes) - todo);
|
||||
|
||||
for (done = 0; done < num_k_bytes;) {
|
||||
if (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1)
|
||||
if (RAND_priv_bytes(random_bytes, sizeof(random_bytes)) != 1)
|
||||
goto err;
|
||||
SHA512_Init(&sha);
|
||||
SHA512_Update(&sha, &done, sizeof(done));
|
||||
|
||||
+6
-5
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -21,8 +21,10 @@ BN_RECP_CTX *BN_RECP_CTX_new(void)
|
||||
{
|
||||
BN_RECP_CTX *ret;
|
||||
|
||||
if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
|
||||
if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
|
||||
BNerr(BN_F_BN_RECP_CTX_NEW, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bn_init(&(ret->N));
|
||||
bn_init(&(ret->Nr));
|
||||
@@ -34,9 +36,8 @@ void BN_RECP_CTX_free(BN_RECP_CTX *recp)
|
||||
{
|
||||
if (recp == NULL)
|
||||
return;
|
||||
|
||||
BN_free(&(recp->N));
|
||||
BN_free(&(recp->Nr));
|
||||
BN_free(&recp->N);
|
||||
BN_free(&recp->Nr);
|
||||
if (recp->flags & BN_FLG_MALLOCED)
|
||||
OPENSSL_free(recp);
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -179,7 +179,7 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
|
||||
if (!BN_set_word(y, i))
|
||||
goto end;
|
||||
} else {
|
||||
if (!BN_rand(y, BN_num_bits(p), 0, 0))
|
||||
if (!BN_priv_rand(y, BN_num_bits(p), 0, 0))
|
||||
goto end;
|
||||
if (BN_ucmp(y, p) >= 0) {
|
||||
if (!(p->neg ? BN_add : BN_sub) (y, y, p))
|
||||
|
||||
@@ -28,7 +28,7 @@ void RSAZ_1024_mod_exp_avx2(BN_ULONG result[16],
|
||||
const BN_ULONG exponent[16],
|
||||
const BN_ULONG m_norm[16], const BN_ULONG RR[16],
|
||||
BN_ULONG k0);
|
||||
int rsaz_avx2_eligible();
|
||||
int rsaz_avx2_eligible(void);
|
||||
|
||||
void RSAZ_512_mod_exp(BN_ULONG result[8],
|
||||
const BN_ULONG base_norm[8], const BN_ULONG exponent[8],
|
||||
|
||||
Reference in New Issue
Block a user