summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn
diff options
context:
space:
mode:
authormiod <>2014-04-13 15:16:40 +0000
committermiod <>2014-04-13 15:16:40 +0000
commit52628ee3f51f011b463aaedb1a28aa0524b43cb3 (patch)
tree4bd2adeac981051908ec5756401424bbb4e57d6a /src/lib/libcrypto/bn
parent40c22d3625a3818690c889ed6216fedf2be522c9 (diff)
downloadopenbsd-52628ee3f51f011b463aaedb1a28aa0524b43cb3.tar.gz
openbsd-52628ee3f51f011b463aaedb1a28aa0524b43cb3.tar.bz2
openbsd-52628ee3f51f011b463aaedb1a28aa0524b43cb3.zip
Import OpenSSL 1.0.1g
Diffstat (limited to 'src/lib/libcrypto/bn')
-rw-r--r--src/lib/libcrypto/bn/asm/mips-mont.pl2
-rw-r--r--src/lib/libcrypto/bn/asm/mips.pl46
-rw-r--r--src/lib/libcrypto/bn/asm/modexp512-x86_64.pl3
-rw-r--r--src/lib/libcrypto/bn/asm/parisc-mont.pl4
-rw-r--r--src/lib/libcrypto/bn/asm/x86_64-gf2m.pl3
-rwxr-xr-xsrc/lib/libcrypto/bn/asm/x86_64-mont.pl3
-rwxr-xr-xsrc/lib/libcrypto/bn/asm/x86_64-mont5.pl7
-rw-r--r--src/lib/libcrypto/bn/bn.h11
-rw-r--r--src/lib/libcrypto/bn/bn_div.c2
-rw-r--r--src/lib/libcrypto/bn/bn_gcd.c1
-rw-r--r--src/lib/libcrypto/bn/bn_lcl.h11
-rw-r--r--src/lib/libcrypto/bn/bn_lib.c52
-rw-r--r--src/lib/libcrypto/bn/bn_nist.c55
-rw-r--r--src/lib/libcrypto/bn/bn_word.c25
14 files changed, 150 insertions, 75 deletions
diff --git a/src/lib/libcrypto/bn/asm/mips-mont.pl b/src/lib/libcrypto/bn/asm/mips-mont.pl
index b944a12b8e..caae04ed3a 100644
--- a/src/lib/libcrypto/bn/asm/mips-mont.pl
+++ b/src/lib/libcrypto/bn/asm/mips-mont.pl
@@ -133,7 +133,7 @@ $code.=<<___;
133 bnez $at,1f 133 bnez $at,1f
134 li $t0,0 134 li $t0,0
135 slt $at,$num,17 # on in-order CPU 135 slt $at,$num,17 # on in-order CPU
136 bnezl $at,bn_mul_mont_internal 136 bnez $at,bn_mul_mont_internal
137 nop 137 nop
1381: jr $ra 1381: jr $ra
139 li $a0,0 139 li $a0,0
diff --git a/src/lib/libcrypto/bn/asm/mips.pl b/src/lib/libcrypto/bn/asm/mips.pl
index c162a3ec23..d2f3ef7bbf 100644
--- a/src/lib/libcrypto/bn/asm/mips.pl
+++ b/src/lib/libcrypto/bn/asm/mips.pl
@@ -140,10 +140,10 @@ $code.=<<___;
140 .set reorder 140 .set reorder
141 li $minus4,-4 141 li $minus4,-4
142 and $ta0,$a2,$minus4 142 and $ta0,$a2,$minus4
143 $LD $t0,0($a1)
144 beqz $ta0,.L_bn_mul_add_words_tail 143 beqz $ta0,.L_bn_mul_add_words_tail
145 144
146.L_bn_mul_add_words_loop: 145.L_bn_mul_add_words_loop:
146 $LD $t0,0($a1)
147 $MULTU $t0,$a3 147 $MULTU $t0,$a3
148 $LD $t1,0($a0) 148 $LD $t1,0($a0)
149 $LD $t2,$BNSZ($a1) 149 $LD $t2,$BNSZ($a1)
@@ -200,10 +200,9 @@ $code.=<<___;
200 $ADDU $v0,$ta2 200 $ADDU $v0,$ta2
201 sltu $at,$ta3,$at 201 sltu $at,$ta3,$at
202 $ST $ta3,-$BNSZ($a0) 202 $ST $ta3,-$BNSZ($a0)
203 $ADDU $v0,$at
204 .set noreorder 203 .set noreorder
205 bgtzl $ta0,.L_bn_mul_add_words_loop 204 bgtz $ta0,.L_bn_mul_add_words_loop
206 $LD $t0,0($a1) 205 $ADDU $v0,$at
207 206
208 beqz $a2,.L_bn_mul_add_words_return 207 beqz $a2,.L_bn_mul_add_words_return
209 nop 208 nop
@@ -300,10 +299,10 @@ $code.=<<___;
300 .set reorder 299 .set reorder
301 li $minus4,-4 300 li $minus4,-4
302 and $ta0,$a2,$minus4 301 and $ta0,$a2,$minus4
303 $LD $t0,0($a1)
304 beqz $ta0,.L_bn_mul_words_tail 302 beqz $ta0,.L_bn_mul_words_tail
305 303
306.L_bn_mul_words_loop: 304.L_bn_mul_words_loop:
305 $LD $t0,0($a1)
307 $MULTU $t0,$a3 306 $MULTU $t0,$a3
308 $LD $t2,$BNSZ($a1) 307 $LD $t2,$BNSZ($a1)
309 $LD $ta0,2*$BNSZ($a1) 308 $LD $ta0,2*$BNSZ($a1)
@@ -341,10 +340,9 @@ $code.=<<___;
341 $ADDU $v0,$at 340 $ADDU $v0,$at
342 sltu $ta3,$v0,$at 341 sltu $ta3,$v0,$at
343 $ST $v0,-$BNSZ($a0) 342 $ST $v0,-$BNSZ($a0)
344 $ADDU $v0,$ta3,$ta2
345 .set noreorder 343 .set noreorder
346 bgtzl $ta0,.L_bn_mul_words_loop 344 bgtz $ta0,.L_bn_mul_words_loop
347 $LD $t0,0($a1) 345 $ADDU $v0,$ta3,$ta2
348 346
349 beqz $a2,.L_bn_mul_words_return 347 beqz $a2,.L_bn_mul_words_return
350 nop 348 nop
@@ -429,10 +427,10 @@ $code.=<<___;
429 .set reorder 427 .set reorder
430 li $minus4,-4 428 li $minus4,-4
431 and $ta0,$a2,$minus4 429 and $ta0,$a2,$minus4
432 $LD $t0,0($a1)
433 beqz $ta0,.L_bn_sqr_words_tail 430 beqz $ta0,.L_bn_sqr_words_tail
434 431
435.L_bn_sqr_words_loop: 432.L_bn_sqr_words_loop:
433 $LD $t0,0($a1)
436 $MULTU $t0,$t0 434 $MULTU $t0,$t0
437 $LD $t2,$BNSZ($a1) 435 $LD $t2,$BNSZ($a1)
438 $LD $ta0,2*$BNSZ($a1) 436 $LD $ta0,2*$BNSZ($a1)
@@ -463,11 +461,10 @@ $code.=<<___;
463 mflo $ta3 461 mflo $ta3
464 mfhi $ta2 462 mfhi $ta2
465 $ST $ta3,-2*$BNSZ($a0) 463 $ST $ta3,-2*$BNSZ($a0)
466 $ST $ta2,-$BNSZ($a0)
467 464
468 .set noreorder 465 .set noreorder
469 bgtzl $ta0,.L_bn_sqr_words_loop 466 bgtz $ta0,.L_bn_sqr_words_loop
470 $LD $t0,0($a1) 467 $ST $ta2,-$BNSZ($a0)
471 468
472 beqz $a2,.L_bn_sqr_words_return 469 beqz $a2,.L_bn_sqr_words_return
473 nop 470 nop
@@ -547,10 +544,10 @@ $code.=<<___;
547 .set reorder 544 .set reorder
548 li $minus4,-4 545 li $minus4,-4
549 and $at,$a3,$minus4 546 and $at,$a3,$minus4
550 $LD $t0,0($a1)
551 beqz $at,.L_bn_add_words_tail 547 beqz $at,.L_bn_add_words_tail
552 548
553.L_bn_add_words_loop: 549.L_bn_add_words_loop:
550 $LD $t0,0($a1)
554 $LD $ta0,0($a2) 551 $LD $ta0,0($a2)
555 subu $a3,4 552 subu $a3,4
556 $LD $t1,$BNSZ($a1) 553 $LD $t1,$BNSZ($a1)
@@ -589,11 +586,10 @@ $code.=<<___;
589 $ADDU $t3,$ta3,$v0 586 $ADDU $t3,$ta3,$v0
590 sltu $v0,$t3,$ta3 587 sltu $v0,$t3,$ta3
591 $ST $t3,-$BNSZ($a0) 588 $ST $t3,-$BNSZ($a0)
592 $ADDU $v0,$t9
593 589
594 .set noreorder 590 .set noreorder
595 bgtzl $at,.L_bn_add_words_loop 591 bgtz $at,.L_bn_add_words_loop
596 $LD $t0,0($a1) 592 $ADDU $v0,$t9
597 593
598 beqz $a3,.L_bn_add_words_return 594 beqz $a3,.L_bn_add_words_return
599 nop 595 nop
@@ -679,10 +675,10 @@ $code.=<<___;
679 .set reorder 675 .set reorder
680 li $minus4,-4 676 li $minus4,-4
681 and $at,$a3,$minus4 677 and $at,$a3,$minus4
682 $LD $t0,0($a1)
683 beqz $at,.L_bn_sub_words_tail 678 beqz $at,.L_bn_sub_words_tail
684 679
685.L_bn_sub_words_loop: 680.L_bn_sub_words_loop:
681 $LD $t0,0($a1)
686 $LD $ta0,0($a2) 682 $LD $ta0,0($a2)
687 subu $a3,4 683 subu $a3,4
688 $LD $t1,$BNSZ($a1) 684 $LD $t1,$BNSZ($a1)
@@ -722,11 +718,10 @@ $code.=<<___;
722 $SUBU $t3,$ta3,$v0 718 $SUBU $t3,$ta3,$v0
723 sgtu $v0,$t3,$ta3 719 sgtu $v0,$t3,$ta3
724 $ST $t3,-$BNSZ($a0) 720 $ST $t3,-$BNSZ($a0)
725 $ADDU $v0,$t9
726 721
727 .set noreorder 722 .set noreorder
728 bgtzl $at,.L_bn_sub_words_loop 723 bgtz $at,.L_bn_sub_words_loop
729 $LD $t0,0($a1) 724 $ADDU $v0,$t9
730 725
731 beqz $a3,.L_bn_sub_words_return 726 beqz $a3,.L_bn_sub_words_return
732 nop 727 nop
@@ -819,7 +814,7 @@ ___
819$code.=<<___; 814$code.=<<___;
820 .set reorder 815 .set reorder
821 move $ta3,$ra 816 move $ta3,$ra
822 bal bn_div_words 817 bal bn_div_words_internal
823 move $ra,$ta3 818 move $ra,$ta3
824 $MULTU $ta2,$v0 819 $MULTU $ta2,$v0
825 $LD $t2,-2*$BNSZ($a3) 820 $LD $t2,-2*$BNSZ($a3)
@@ -840,8 +835,9 @@ $code.=<<___;
840 sltu $ta0,$a1,$a2 835 sltu $ta0,$a1,$a2
841 or $t8,$ta0 836 or $t8,$ta0
842 .set noreorder 837 .set noreorder
843 beqzl $at,.L_bn_div_3_words_inner_loop 838 beqz $at,.L_bn_div_3_words_inner_loop
844 $SUBU $v0,1 839 $SUBU $v0,1
840 $ADDU $v0,1
845 .set reorder 841 .set reorder
846.L_bn_div_3_words_inner_loop_done: 842.L_bn_div_3_words_inner_loop_done:
847 .set noreorder 843 .set noreorder
@@ -902,7 +898,8 @@ $code.=<<___;
902 and $t2,$a0 898 and $t2,$a0
903 $SRL $at,$a1,$t1 899 $SRL $at,$a1,$t1
904 .set noreorder 900 .set noreorder
905 bnezl $t2,.+8 901 beqz $t2,.+12
902 nop
906 break 6 # signal overflow 903 break 6 # signal overflow
907 .set reorder 904 .set reorder
908 $SLL $a0,$t9 905 $SLL $a0,$t9
@@ -917,7 +914,8 @@ $code.=<<___;
917 $SRL $DH,$a2,4*$BNSZ # bits 914 $SRL $DH,$a2,4*$BNSZ # bits
918 sgeu $at,$a0,$a2 915 sgeu $at,$a0,$a2
919 .set noreorder 916 .set noreorder
920 bnezl $at,.+8 917 beqz $at,.+12
918 nop
921 $SUBU $a0,$a2 919 $SUBU $a0,$a2
922 .set reorder 920 .set reorder
923 921
diff --git a/src/lib/libcrypto/bn/asm/modexp512-x86_64.pl b/src/lib/libcrypto/bn/asm/modexp512-x86_64.pl
index 54aeb01921..bfd6e97541 100644
--- a/src/lib/libcrypto/bn/asm/modexp512-x86_64.pl
+++ b/src/lib/libcrypto/bn/asm/modexp512-x86_64.pl
@@ -68,7 +68,8 @@ $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
68( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or 68( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
69die "can't locate x86_64-xlate.pl"; 69die "can't locate x86_64-xlate.pl";
70 70
71open STDOUT,"| $^X $xlate $flavour $output"; 71open OUT,"| \"$^X\" $xlate $flavour $output";
72*STDOUT=*OUT;
72 73
73use strict; 74use strict;
74my $code=".text\n\n"; 75my $code=".text\n\n";
diff --git a/src/lib/libcrypto/bn/asm/parisc-mont.pl b/src/lib/libcrypto/bn/asm/parisc-mont.pl
index 4a766a87fb..c02ef6f014 100644
--- a/src/lib/libcrypto/bn/asm/parisc-mont.pl
+++ b/src/lib/libcrypto/bn/asm/parisc-mont.pl
@@ -40,7 +40,7 @@
40# of arithmetic operations, most notably multiplications. It requires 40# of arithmetic operations, most notably multiplications. It requires
41# more memory references, most notably to tp[num], but this doesn't 41# more memory references, most notably to tp[num], but this doesn't
42# seem to exhaust memory port capacity. And indeed, dedicated PA-RISC 42# seem to exhaust memory port capacity. And indeed, dedicated PA-RISC
43# 2.0 code path, provides virtually same performance as pa-risc2[W].s: 43# 2.0 code path provides virtually same performance as pa-risc2[W].s:
44# it's ~10% better for shortest key length and ~10% worse for longest 44# it's ~10% better for shortest key length and ~10% worse for longest
45# one. 45# one.
46# 46#
@@ -988,6 +988,8 @@ foreach (split("\n",$code)) {
988 # assemble 2.0 instructions in 32-bit mode... 988 # assemble 2.0 instructions in 32-bit mode...
989 s/^\s+([a-z]+)([\S]*)\s+([\S]*)/&assemble($1,$2,$3)/e if ($BN_SZ==4); 989 s/^\s+([a-z]+)([\S]*)\s+([\S]*)/&assemble($1,$2,$3)/e if ($BN_SZ==4);
990 990
991 s/\bbv\b/bve/gm if ($SIZE_T==8);
992
991 print $_,"\n"; 993 print $_,"\n";
992} 994}
993close STDOUT; 995close STDOUT;
diff --git a/src/lib/libcrypto/bn/asm/x86_64-gf2m.pl b/src/lib/libcrypto/bn/asm/x86_64-gf2m.pl
index 1658acbbdd..226c66c35e 100644
--- a/src/lib/libcrypto/bn/asm/x86_64-gf2m.pl
+++ b/src/lib/libcrypto/bn/asm/x86_64-gf2m.pl
@@ -31,7 +31,8 @@ $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
31( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or 31( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
32die "can't locate x86_64-xlate.pl"; 32die "can't locate x86_64-xlate.pl";
33 33
34open STDOUT,"| $^X $xlate $flavour $output"; 34open OUT,"| \"$^X\" $xlate $flavour $output";
35*STDOUT=*OUT;
35 36
36($lo,$hi)=("%rax","%rdx"); $a=$lo; 37($lo,$hi)=("%rax","%rdx"); $a=$lo;
37($i0,$i1)=("%rsi","%rdi"); 38($i0,$i1)=("%rsi","%rdi");
diff --git a/src/lib/libcrypto/bn/asm/x86_64-mont.pl b/src/lib/libcrypto/bn/asm/x86_64-mont.pl
index 5d79b35e1c..17fb94c84c 100755
--- a/src/lib/libcrypto/bn/asm/x86_64-mont.pl
+++ b/src/lib/libcrypto/bn/asm/x86_64-mont.pl
@@ -40,7 +40,8 @@ $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
40( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or 40( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
41die "can't locate x86_64-xlate.pl"; 41die "can't locate x86_64-xlate.pl";
42 42
43open STDOUT,"| $^X $xlate $flavour $output"; 43open OUT,"| \"$^X\" $xlate $flavour $output";
44*STDOUT=*OUT;
44 45
45# int bn_mul_mont( 46# int bn_mul_mont(
46$rp="%rdi"; # BN_ULONG *rp, 47$rp="%rdi"; # BN_ULONG *rp,
diff --git a/src/lib/libcrypto/bn/asm/x86_64-mont5.pl b/src/lib/libcrypto/bn/asm/x86_64-mont5.pl
index 057cda28aa..dae0fe2453 100755
--- a/src/lib/libcrypto/bn/asm/x86_64-mont5.pl
+++ b/src/lib/libcrypto/bn/asm/x86_64-mont5.pl
@@ -28,7 +28,8 @@ $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
28( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or 28( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
29die "can't locate x86_64-xlate.pl"; 29die "can't locate x86_64-xlate.pl";
30 30
31open STDOUT,"| $^X $xlate $flavour $output"; 31open OUT,"| \"$^X\" $xlate $flavour $output";
32*STDOUT=*OUT;
32 33
33# int bn_mul_mont_gather5( 34# int bn_mul_mont_gather5(
34$rp="%rdi"; # BN_ULONG *rp, 35$rp="%rdi"; # BN_ULONG *rp,
@@ -900,8 +901,8 @@ $code.=<<___;
900 jnz .Lgather 901 jnz .Lgather
901___ 902___
902$code.=<<___ if ($win64); 903$code.=<<___ if ($win64);
903 movaps %xmm6,(%rsp) 904 movaps (%rsp),%xmm6
904 movaps %xmm7,0x10(%rsp) 905 movaps 0x10(%rsp),%xmm7
905 lea 0x28(%rsp),%rsp 906 lea 0x28(%rsp),%rsp
906___ 907___
907$code.=<<___; 908$code.=<<___;
diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h
index f34248ec4f..21a1a3fe35 100644
--- a/src/lib/libcrypto/bn/bn.h
+++ b/src/lib/libcrypto/bn/bn.h
@@ -538,6 +538,8 @@ BIGNUM *BN_mod_inverse(BIGNUM *ret,
538BIGNUM *BN_mod_sqrt(BIGNUM *ret, 538BIGNUM *BN_mod_sqrt(BIGNUM *ret,
539 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); 539 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
540 540
541void BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords);
542
541/* Deprecated versions */ 543/* Deprecated versions */
542#ifndef OPENSSL_NO_DEPRECATED 544#ifndef OPENSSL_NO_DEPRECATED
543BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe, 545BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe,
@@ -774,11 +776,20 @@ int RAND_pseudo_bytes(unsigned char *buf,int num);
774 776
775#define bn_fix_top(a) bn_check_top(a) 777#define bn_fix_top(a) bn_check_top(a)
776 778
779#define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
780#define bn_wcheck_size(bn, words) \
781 do { \
782 const BIGNUM *_bnum2 = (bn); \
783 assert(words <= (_bnum2)->dmax && words >= (_bnum2)->top); \
784 } while(0)
785
777#else /* !BN_DEBUG */ 786#else /* !BN_DEBUG */
778 787
779#define bn_pollute(a) 788#define bn_pollute(a)
780#define bn_check_top(a) 789#define bn_check_top(a)
781#define bn_fix_top(a) bn_correct_top(a) 790#define bn_fix_top(a) bn_correct_top(a)
791#define bn_check_size(bn, bits)
792#define bn_wcheck_size(bn, words)
782 793
783#endif 794#endif
784 795
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c
index 52b3304293..7b2403185e 100644
--- a/src/lib/libcrypto/bn/bn_div.c
+++ b/src/lib/libcrypto/bn/bn_div.c
@@ -141,6 +141,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
141 * 141 *
142 * <appro@fy.chalmers.se> 142 * <appro@fy.chalmers.se>
143 */ 143 */
144#undef bn_div_words
144# define bn_div_words(n0,n1,d0) \ 145# define bn_div_words(n0,n1,d0) \
145 ({ asm volatile ( \ 146 ({ asm volatile ( \
146 "divl %4" \ 147 "divl %4" \
@@ -155,6 +156,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
155 * Same story here, but it's 128-bit by 64-bit division. Wow! 156 * Same story here, but it's 128-bit by 64-bit division. Wow!
156 * <appro@fy.chalmers.se> 157 * <appro@fy.chalmers.se>
157 */ 158 */
159# undef bn_div_words
158# define bn_div_words(n0,n1,d0) \ 160# define bn_div_words(n0,n1,d0) \
159 ({ asm volatile ( \ 161 ({ asm volatile ( \
160 "divq %4" \ 162 "divq %4" \
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c
index 4a352119ba..a808f53178 100644
--- a/src/lib/libcrypto/bn/bn_gcd.c
+++ b/src/lib/libcrypto/bn/bn_gcd.c
@@ -205,6 +205,7 @@ err:
205/* solves ax == 1 (mod n) */ 205/* solves ax == 1 (mod n) */
206static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in, 206static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
207 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); 207 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
208
208BIGNUM *BN_mod_inverse(BIGNUM *in, 209BIGNUM *BN_mod_inverse(BIGNUM *in,
209 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) 210 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
210 { 211 {
diff --git a/src/lib/libcrypto/bn/bn_lcl.h b/src/lib/libcrypto/bn/bn_lcl.h
index eecfd8cc99..817c773b65 100644
--- a/src/lib/libcrypto/bn/bn_lcl.h
+++ b/src/lib/libcrypto/bn/bn_lcl.h
@@ -282,16 +282,23 @@ extern "C" {
282# endif 282# endif
283# elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)) 283# elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
284# if defined(__GNUC__) && __GNUC__>=2 284# if defined(__GNUC__) && __GNUC__>=2
285# define BN_UMULT_HIGH(a,b) ({ \ 285# if __GNUC__>=4 && __GNUC_MINOR__>=4 /* "h" constraint is no more since 4.4 */
286# define BN_UMULT_HIGH(a,b) (((__uint128_t)(a)*(b))>>64)
287# define BN_UMULT_LOHI(low,high,a,b) ({ \
288 __uint128_t ret=(__uint128_t)(a)*(b); \
289 (high)=ret>>64; (low)=ret; })
290# else
291# define BN_UMULT_HIGH(a,b) ({ \
286 register BN_ULONG ret; \ 292 register BN_ULONG ret; \
287 asm ("dmultu %1,%2" \ 293 asm ("dmultu %1,%2" \
288 : "=h"(ret) \ 294 : "=h"(ret) \
289 : "r"(a), "r"(b) : "l"); \ 295 : "r"(a), "r"(b) : "l"); \
290 ret; }) 296 ret; })
291# define BN_UMULT_LOHI(low,high,a,b) \ 297# define BN_UMULT_LOHI(low,high,a,b)\
292 asm ("dmultu %2,%3" \ 298 asm ("dmultu %2,%3" \
293 : "=l"(low),"=h"(high) \ 299 : "=l"(low),"=h"(high) \
294 : "r"(a), "r"(b)); 300 : "r"(a), "r"(b));
301# endif
295# endif 302# endif
296# endif /* cpu */ 303# endif /* cpu */
297#endif /* OPENSSL_NO_ASM */ 304#endif /* OPENSSL_NO_ASM */
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c
index 7a5676de69..5461e6ee7d 100644
--- a/src/lib/libcrypto/bn/bn_lib.c
+++ b/src/lib/libcrypto/bn/bn_lib.c
@@ -824,3 +824,55 @@ int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,
824 } 824 }
825 return bn_cmp_words(a,b,cl); 825 return bn_cmp_words(a,b,cl);
826 } 826 }
827
828/*
829 * Constant-time conditional swap of a and b.
830 * a and b are swapped if condition is not 0. The code assumes that at most one bit of condition is set.
831 * nwords is the number of words to swap. The code assumes that at least nwords are allocated in both a and b,
832 * and that no more than nwords are used by either a or b.
833 * a and b cannot be the same number
834 */
835void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
836 {
837 BN_ULONG t;
838 int i;
839
840 bn_wcheck_size(a, nwords);
841 bn_wcheck_size(b, nwords);
842
843 assert(a != b);
844 assert((condition & (condition - 1)) == 0);
845 assert(sizeof(BN_ULONG) >= sizeof(int));
846
847 condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
848
849 t = (a->top^b->top) & condition;
850 a->top ^= t;
851 b->top ^= t;
852
853#define BN_CONSTTIME_SWAP(ind) \
854 do { \
855 t = (a->d[ind] ^ b->d[ind]) & condition; \
856 a->d[ind] ^= t; \
857 b->d[ind] ^= t; \
858 } while (0)
859
860
861 switch (nwords) {
862 default:
863 for (i = 10; i < nwords; i++)
864 BN_CONSTTIME_SWAP(i);
865 /* Fallthrough */
866 case 10: BN_CONSTTIME_SWAP(9); /* Fallthrough */
867 case 9: BN_CONSTTIME_SWAP(8); /* Fallthrough */
868 case 8: BN_CONSTTIME_SWAP(7); /* Fallthrough */
869 case 7: BN_CONSTTIME_SWAP(6); /* Fallthrough */
870 case 6: BN_CONSTTIME_SWAP(5); /* Fallthrough */
871 case 5: BN_CONSTTIME_SWAP(4); /* Fallthrough */
872 case 4: BN_CONSTTIME_SWAP(3); /* Fallthrough */
873 case 3: BN_CONSTTIME_SWAP(2); /* Fallthrough */
874 case 2: BN_CONSTTIME_SWAP(1); /* Fallthrough */
875 case 1: BN_CONSTTIME_SWAP(0);
876 }
877#undef BN_CONSTTIME_SWAP
878}
diff --git a/src/lib/libcrypto/bn/bn_nist.c b/src/lib/libcrypto/bn/bn_nist.c
index 43caee4770..e22968d4a3 100644
--- a/src/lib/libcrypto/bn/bn_nist.c
+++ b/src/lib/libcrypto/bn/bn_nist.c
@@ -286,26 +286,25 @@ const BIGNUM *BN_get0_nist_prime_521(void)
286 } 286 }
287 287
288 288
289static void nist_cp_bn_0(BN_ULONG *buf, BN_ULONG *a, int top, int max) 289static void nist_cp_bn_0(BN_ULONG *dst, const BN_ULONG *src, int top, int max)
290 { 290 {
291 int i; 291 int i;
292 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
293 292
294#ifdef BN_DEBUG 293#ifdef BN_DEBUG
295 OPENSSL_assert(top <= max); 294 OPENSSL_assert(top <= max);
296#endif 295#endif
297 for (i = (top); i != 0; i--) 296 for (i = 0; i < top; i++)
298 *_tmp1++ = *_tmp2++; 297 dst[i] = src[i];
299 for (i = (max) - (top); i != 0; i--) 298 for (; i < max; i++)
300 *_tmp1++ = (BN_ULONG) 0; 299 dst[i] = 0;
301 } 300 }
302 301
303static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top) 302static void nist_cp_bn(BN_ULONG *dst, const BN_ULONG *src, int top)
304 { 303 {
305 int i; 304 int i;
306 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a); 305
307 for (i = (top); i != 0; i--) 306 for (i = 0; i < top; i++)
308 *_tmp1++ = *_tmp2++; 307 dst[i] = src[i];
309 } 308 }
310 309
311#if BN_BITS2 == 64 310#if BN_BITS2 == 64
@@ -451,8 +450,9 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
451 */ 450 */
452 mask = 0-(PTR_SIZE_INT)bn_sub_words(c_d,r_d,_nist_p_192[0],BN_NIST_192_TOP); 451 mask = 0-(PTR_SIZE_INT)bn_sub_words(c_d,r_d,_nist_p_192[0],BN_NIST_192_TOP);
453 mask &= 0-(PTR_SIZE_INT)carry; 452 mask &= 0-(PTR_SIZE_INT)carry;
453 res = c_d;
454 res = (BN_ULONG *) 454 res = (BN_ULONG *)
455 (((PTR_SIZE_INT)c_d&~mask) | ((PTR_SIZE_INT)r_d&mask)); 455 (((PTR_SIZE_INT)res&~mask) | ((PTR_SIZE_INT)r_d&mask));
456 nist_cp_bn(r_d, res, BN_NIST_192_TOP); 456 nist_cp_bn(r_d, res, BN_NIST_192_TOP);
457 r->top = BN_NIST_192_TOP; 457 r->top = BN_NIST_192_TOP;
458 bn_correct_top(r); 458 bn_correct_top(r);
@@ -479,8 +479,11 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
479 int top = a->top, i; 479 int top = a->top, i;
480 int carry; 480 int carry;
481 BN_ULONG *r_d, *a_d = a->d; 481 BN_ULONG *r_d, *a_d = a->d;
482 BN_ULONG buf[BN_NIST_224_TOP], 482 union {
483 c_d[BN_NIST_224_TOP], 483 BN_ULONG bn[BN_NIST_224_TOP];
484 unsigned int ui[BN_NIST_224_TOP*sizeof(BN_ULONG)/sizeof(unsigned int)];
485 } buf;
486 BN_ULONG c_d[BN_NIST_224_TOP],
484 *res; 487 *res;
485 PTR_SIZE_INT mask; 488 PTR_SIZE_INT mask;
486 union { bn_addsub_f f; PTR_SIZE_INT p; } u; 489 union { bn_addsub_f f; PTR_SIZE_INT p; } u;
@@ -519,18 +522,18 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
519 /* copy upper 256 bits of 448 bit number ... */ 522 /* copy upper 256 bits of 448 bit number ... */
520 nist_cp_bn_0(c_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP); 523 nist_cp_bn_0(c_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP);
521 /* ... and right shift by 32 to obtain upper 224 bits */ 524 /* ... and right shift by 32 to obtain upper 224 bits */
522 nist_set_224(buf, c_d, 14, 13, 12, 11, 10, 9, 8); 525 nist_set_224(buf.bn, c_d, 14, 13, 12, 11, 10, 9, 8);
523 /* truncate lower part to 224 bits too */ 526 /* truncate lower part to 224 bits too */
524 r_d[BN_NIST_224_TOP-1] &= BN_MASK2l; 527 r_d[BN_NIST_224_TOP-1] &= BN_MASK2l;
525#else 528#else
526 nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP); 529 nist_cp_bn_0(buf.bn, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP);
527#endif 530#endif
528 531
529#if defined(NIST_INT64) && BN_BITS2!=64 532#if defined(NIST_INT64) && BN_BITS2!=64
530 { 533 {
531 NIST_INT64 acc; /* accumulator */ 534 NIST_INT64 acc; /* accumulator */
532 unsigned int *rp=(unsigned int *)r_d; 535 unsigned int *rp=(unsigned int *)r_d;
533 const unsigned int *bp=(const unsigned int *)buf; 536 const unsigned int *bp=(const unsigned int *)buf.ui;
534 537
535 acc = rp[0]; acc -= bp[7-7]; 538 acc = rp[0]; acc -= bp[7-7];
536 acc -= bp[11-7]; rp[0] = (unsigned int)acc; acc >>= 32; 539 acc -= bp[11-7]; rp[0] = (unsigned int)acc; acc >>= 32;
@@ -565,13 +568,13 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
565 { 568 {
566 BN_ULONG t_d[BN_NIST_224_TOP]; 569 BN_ULONG t_d[BN_NIST_224_TOP];
567 570
568 nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0); 571 nist_set_224(t_d, buf.bn, 10, 9, 8, 7, 0, 0, 0);
569 carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP); 572 carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
570 nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0); 573 nist_set_224(t_d, buf.bn, 0, 13, 12, 11, 0, 0, 0);
571 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP); 574 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
572 nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7); 575 nist_set_224(t_d, buf.bn, 13, 12, 11, 10, 9, 8, 7);
573 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP); 576 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
574 nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11); 577 nist_set_224(t_d, buf.bn, 0, 0, 0, 0, 13, 12, 11);
575 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP); 578 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
576 579
577#if BN_BITS2==64 580#if BN_BITS2==64
@@ -606,7 +609,8 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
606 /* otherwise it's effectively same as in BN_nist_mod_192... */ 609 /* otherwise it's effectively same as in BN_nist_mod_192... */
607 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_224[0],BN_NIST_224_TOP); 610 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_224[0],BN_NIST_224_TOP);
608 mask &= 0-(PTR_SIZE_INT)carry; 611 mask &= 0-(PTR_SIZE_INT)carry;
609 res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) | 612 res = c_d;
613 res = (BN_ULONG *)(((PTR_SIZE_INT)res&~mask) |
610 ((PTR_SIZE_INT)r_d&mask)); 614 ((PTR_SIZE_INT)r_d&mask));
611 nist_cp_bn(r_d, res, BN_NIST_224_TOP); 615 nist_cp_bn(r_d, res, BN_NIST_224_TOP);
612 r->top = BN_NIST_224_TOP; 616 r->top = BN_NIST_224_TOP;
@@ -805,7 +809,8 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
805 809
806 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_256[0],BN_NIST_256_TOP); 810 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_256[0],BN_NIST_256_TOP);
807 mask &= 0-(PTR_SIZE_INT)carry; 811 mask &= 0-(PTR_SIZE_INT)carry;
808 res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) | 812 res = c_d;
813 res = (BN_ULONG *)(((PTR_SIZE_INT)res&~mask) |
809 ((PTR_SIZE_INT)r_d&mask)); 814 ((PTR_SIZE_INT)r_d&mask));
810 nist_cp_bn(r_d, res, BN_NIST_256_TOP); 815 nist_cp_bn(r_d, res, BN_NIST_256_TOP);
811 r->top = BN_NIST_256_TOP; 816 r->top = BN_NIST_256_TOP;
@@ -1026,7 +1031,8 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
1026 1031
1027 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_384[0],BN_NIST_384_TOP); 1032 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_384[0],BN_NIST_384_TOP);
1028 mask &= 0-(PTR_SIZE_INT)carry; 1033 mask &= 0-(PTR_SIZE_INT)carry;
1029 res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) | 1034 res = c_d;
1035 res = (BN_ULONG *)(((PTR_SIZE_INT)res&~mask) |
1030 ((PTR_SIZE_INT)r_d&mask)); 1036 ((PTR_SIZE_INT)r_d&mask));
1031 nist_cp_bn(r_d, res, BN_NIST_384_TOP); 1037 nist_cp_bn(r_d, res, BN_NIST_384_TOP);
1032 r->top = BN_NIST_384_TOP; 1038 r->top = BN_NIST_384_TOP;
@@ -1092,7 +1098,8 @@ int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
1092 1098
1093 bn_add_words(r_d,r_d,t_d,BN_NIST_521_TOP); 1099 bn_add_words(r_d,r_d,t_d,BN_NIST_521_TOP);
1094 mask = 0-(PTR_SIZE_INT)bn_sub_words(t_d,r_d,_nist_p_521,BN_NIST_521_TOP); 1100 mask = 0-(PTR_SIZE_INT)bn_sub_words(t_d,r_d,_nist_p_521,BN_NIST_521_TOP);
1095 res = (BN_ULONG *)(((PTR_SIZE_INT)t_d&~mask) | 1101 res = t_d;
1102 res = (BN_ULONG *)(((PTR_SIZE_INT)res&~mask) |
1096 ((PTR_SIZE_INT)r_d&mask)); 1103 ((PTR_SIZE_INT)r_d&mask));
1097 nist_cp_bn(r_d,res,BN_NIST_521_TOP); 1104 nist_cp_bn(r_d,res,BN_NIST_521_TOP);
1098 r->top = BN_NIST_521_TOP; 1105 r->top = BN_NIST_521_TOP;
diff --git a/src/lib/libcrypto/bn/bn_word.c b/src/lib/libcrypto/bn/bn_word.c
index ee7b87c45c..de83a15b99 100644
--- a/src/lib/libcrypto/bn/bn_word.c
+++ b/src/lib/libcrypto/bn/bn_word.c
@@ -144,26 +144,17 @@ int BN_add_word(BIGNUM *a, BN_ULONG w)
144 a->neg=!(a->neg); 144 a->neg=!(a->neg);
145 return(i); 145 return(i);
146 } 146 }
147 /* Only expand (and risk failing) if it's possibly necessary */ 147 for (i=0;w!=0 && i<a->top;i++)
148 if (((BN_ULONG)(a->d[a->top - 1] + 1) == 0) &&
149 (bn_wexpand(a,a->top+1) == NULL))
150 return(0);
151 i=0;
152 for (;;)
153 { 148 {
154 if (i >= a->top) 149 a->d[i] = l = (a->d[i]+w)&BN_MASK2;
155 l=w; 150 w = (w>l)?1:0;
156 else
157 l=(a->d[i]+w)&BN_MASK2;
158 a->d[i]=l;
159 if (w > l)
160 w=1;
161 else
162 break;
163 i++;
164 } 151 }
165 if (i >= a->top) 152 if (w && i==a->top)
153 {
154 if (bn_wexpand(a,a->top+1) == NULL) return 0;
166 a->top++; 155 a->top++;
156 a->d[i]=w;
157 }
167 bn_check_top(a); 158 bn_check_top(a);
168 return(1); 159 return(1);
169 } 160 }