1
From 4b4c0a19211bf73d81de52de697a1a9dc60aed82 Mon Sep 17 00:00:00 2001
2
From: Andy Polyakov <appro@openssl.org>
3
Date: Mon, 5 Jan 2015 14:52:56 +0100
4
Subject: [PATCH] Fix for CVE-2014-3570.
6
Reviewed-by: Emilia Kasper <emilia@openssl.org>
7
(cherry picked from commit e793809ba50c1e90ab592fb640a856168e50f3de)
9
crypto/bn/asm/mips3.s | 514 ++++++++++++++++++++++----------------------
10
crypto/bn/asm/x86_64-gcc.c | 34 ++-
11
crypto/bn/bn_asm.c | 16 +-
12
crypto/bn/bntest.c | 102 ++++++---
13
4 files changed, 360 insertions(+), 306 deletions(-)
15
Index: openssl-0.9.8k/crypto/bn/asm/mips3.s
16
===================================================================
17
--- openssl-0.9.8k.orig/crypto/bn/asm/mips3.s 2015-01-09 11:13:58.810255208 -0500
18
+++ openssl-0.9.8k/crypto/bn/asm/mips3.s 2015-01-09 11:13:58.806255173 -0500
19
@@ -1584,17 +1584,17 @@
20
dmultu a_2,a_0 /* mul_add_c2(a[2],b[0],c3,c1,c2); */
42
dmultu a_1,a_1 /* mul_add_c(a[1],b[1],c3,c1,c2); */
45
@@ -1609,63 +1609,63 @@
46
dmultu a_0,a_3 /* mul_add_c2(a[0],b[3],c1,c2,c3); */
68
dmultu a_1,a_2 /* mul_add_c2(a[1],b[2],c1,c2,c3); */
93
dmultu a_4,a_0 /* mul_add_c2(a[4],b[0],c2,c3,c1); */
115
dmultu a_3,a_1 /* mul_add_c2(a[3],b[1],c2,c3,c1); */
138
dmultu a_2,a_2 /* mul_add_c(a[2],b[2],c2,c3,c1); */
141
@@ -1680,93 +1680,93 @@
142
dmultu a_0,a_5 /* mul_add_c2(a[0],b[5],c3,c1,c2); */
164
dmultu a_1,a_4 /* mul_add_c2(a[1],b[4],c3,c1,c2); */
187
dmultu a_2,a_3 /* mul_add_c2(a[2],b[3],c3,c1,c2); */
212
dmultu a_6,a_0 /* mul_add_c2(a[6],b[0],c1,c2,c3); */
234
dmultu a_5,a_1 /* mul_add_c2(a[5],b[1],c1,c2,c3); */
257
dmultu a_4,a_2 /* mul_add_c2(a[4],b[2],c1,c2,c3); */
280
dmultu a_3,a_3 /* mul_add_c(a[3],b[3],c1,c2,c3); */
283
@@ -1781,108 +1781,108 @@
284
dmultu a_0,a_7 /* mul_add_c2(a[0],b[7],c2,c3,c1); */
306
dmultu a_1,a_6 /* mul_add_c2(a[1],b[6],c2,c3,c1); */
329
dmultu a_2,a_5 /* mul_add_c2(a[2],b[5],c2,c3,c1); */
352
dmultu a_3,a_4 /* mul_add_c2(a[3],b[4],c2,c3,c1); */
377
dmultu a_7,a_1 /* mul_add_c2(a[7],b[1],c3,c1,c2); */
399
dmultu a_6,a_2 /* mul_add_c2(a[6],b[2],c3,c1,c2); */
422
dmultu a_5,a_3 /* mul_add_c2(a[5],b[3],c3,c1,c2); */
445
dmultu a_4,a_4 /* mul_add_c(a[4],b[4],c3,c1,c2); */
448
@@ -1897,78 +1897,78 @@
449
dmultu a_2,a_7 /* mul_add_c2(a[2],b[7],c1,c2,c3); */
471
dmultu a_3,a_6 /* mul_add_c2(a[3],b[6],c1,c2,c3); */
494
dmultu a_4,a_5 /* mul_add_c2(a[4],b[5],c1,c2,c3); */
519
dmultu a_7,a_3 /* mul_add_c2(a[7],b[3],c2,c3,c1); */
541
dmultu a_6,a_4 /* mul_add_c2(a[6],b[4],c2,c3,c1); */
564
dmultu a_5,a_5 /* mul_add_c(a[5],b[5],c2,c3,c1); */
567
@@ -1983,48 +1983,48 @@
568
dmultu a_4,a_7 /* mul_add_c2(a[4],b[7],c3,c1,c2); */
590
dmultu a_5,a_6 /* mul_add_c2(a[5],b[6],c3,c1,c2); */
615
dmultu a_7,a_5 /* mul_add_c2(a[7],b[5],c1,c2,c3); */
637
dmultu a_6,a_6 /* mul_add_c(a[6],b[6],c1,c2,c3); */
640
@@ -2039,17 +2039,17 @@
641
dmultu a_6,a_7 /* mul_add_c2(a[6],b[7],c2,c3,c1); */
665
dmultu a_7,a_7 /* mul_add_c(a[7],b[7],c3,c1,c2); */
666
@@ -2070,9 +2070,9 @@
670
+ dmultu a_0,a_0 /* mul_add_c(a[0],b[0],c1,c2,c3); */
673
- dmultu a_0,a_0 /* mul_add_c(a[0],b[0],c1,c2,c3); */
677
@@ -2093,17 +2093,17 @@
678
dmultu a_2,a_0 /* mul_add_c2(a[2],b[0],c3,c1,c2); */
700
dmultu a_1,a_1 /* mul_add_c(a[1],b[1],c3,c1,c2); */
703
@@ -2118,48 +2118,48 @@
704
dmultu a_0,a_3 /* mul_add_c2(a[0],b[3],c1,c2,c3); */
726
dmultu a_1,a_2 /* mul_add_c(a2[1],b[2],c1,c2,c3); */
751
dmultu a_3,a_1 /* mul_add_c2(a[3],b[1],c2,c3,c1); */
773
dmultu a_2,a_2 /* mul_add_c(a[2],b[2],c2,c3,c1); */
776
@@ -2174,17 +2174,17 @@
777
dmultu a_2,a_3 /* mul_add_c2(a[2],b[3],c3,c1,c2); */
801
dmultu a_3,a_3 /* mul_add_c(a[3],b[3],c1,c2,c3); */
802
Index: openssl-0.9.8k/crypto/bn/asm/x86_64-gcc.c
803
===================================================================
804
--- openssl-0.9.8k.orig/crypto/bn/asm/x86_64-gcc.c 2015-01-09 11:13:58.810255208 -0500
805
+++ openssl-0.9.8k/crypto/bn/asm/x86_64-gcc.c 2015-01-09 11:13:58.806255173 -0500
807
/* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
808
/* sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number c=(c2,c1,c0) */
811
+ * Keep in mind that carrying into high part of multiplication result
812
+ * can not overflow, because it cannot be all-ones.
815
/* original macros are kept for reference purposes */
816
#define mul_add_c(a,b,c0,c1,c2) { \
817
@@ -278,10 +282,10 @@
818
BN_ULONG ta=(a),tb=(b),t0; \
819
t1 = BN_UMULT_HIGH(ta,tb); \
821
- t2 = t1+t1; c2 += (t2<t1)?1:0; \
822
- t1 = t0+t0; t2 += (t1<t0)?1:0; \
823
- c0 += t1; t2 += (c0<t1)?1:0; \
824
+ c0 += t0; t2 = t1+((c0<t0)?1:0);\
825
c1 += t2; c2 += (c1<t2)?1:0; \
826
+ c0 += t0; t1 += (c0<t0)?1:0; \
827
+ c1 += t1; c2 += (c1<t1)?1:0; \
830
#define mul_add_c(a,b,c0,c1,c2) do { \
831
@@ -319,22 +323,14 @@
832
: "=a"(t1),"=d"(t2) \
835
- asm ("addq %0,%0; adcq %2,%1" \
836
- : "+d"(t2),"+r"(c2) \
839
- asm ("addq %0,%0; adcq %2,%1" \
840
- : "+a"(t1),"+d"(t2) \
843
- asm ("addq %2,%0; adcq %3,%1" \
844
- : "+r"(c0),"+d"(t2) \
847
- asm ("addq %2,%0; adcq %3,%1" \
848
- : "+r"(c1),"+r"(c2) \
851
+ asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \
852
+ : "+r"(c0),"+r"(c1),"+r"(c2) \
853
+ : "r"(t1),"r"(t2),"g"(0) \
855
+ asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \
856
+ : "+r"(c0),"+r"(c1),"+r"(c2) \
857
+ : "r"(t1),"r"(t2),"g"(0) \
862
Index: openssl-0.9.8k/crypto/bn/bn_asm.c
863
===================================================================
864
--- openssl-0.9.8k.orig/crypto/bn/bn_asm.c 2015-01-09 11:13:58.810255208 -0500
865
+++ openssl-0.9.8k/crypto/bn/bn_asm.c 2015-01-09 11:13:58.806255173 -0500
867
/* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
868
/* sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number c=(c2,c1,c0) */
871
+ * Keep in mind that carrying into high part of multiplication result
872
+ * can not overflow, because it cannot be all-ones.
875
#define mul_add_c(a,b,c0,c1,c2) \
877
@@ -471,10 +475,10 @@
878
#define mul_add_c2(a,b,c0,c1,c2) { \
879
BN_ULONG ta=(a),tb=(b),t0; \
880
BN_UMULT_LOHI(t0,t1,ta,tb); \
881
- t2 = t1+t1; c2 += (t2<t1)?1:0; \
882
- t1 = t0+t0; t2 += (t1<t0)?1:0; \
883
- c0 += t1; t2 += (c0<t1)?1:0; \
884
+ c0 += t0; t2 = t1+((c0<t0)?1:0);\
885
c1 += t2; c2 += (c1<t2)?1:0; \
886
+ c0 += t0; t1 += (c0<t0)?1:0; \
887
+ c1 += t1; c2 += (c1<t1)?1:0; \
890
#define sqr_add_c(a,i,c0,c1,c2) { \
891
@@ -501,10 +505,10 @@
892
BN_ULONG ta=(a),tb=(b),t0; \
893
t1 = BN_UMULT_HIGH(ta,tb); \
895
- t2 = t1+t1; c2 += (t2<t1)?1:0; \
896
- t1 = t0+t0; t2 += (t1<t0)?1:0; \
897
- c0 += t1; t2 += (c0<t1)?1:0; \
898
+ c0 += t0; t2 = t1+((c0<t0)?1:0);\
899
c1 += t2; c2 += (c1<t2)?1:0; \
900
+ c0 += t0; t1 += (c0<t0)?1:0; \
901
+ c1 += t1; c2 += (c1<t1)?1:0; \
904
#define sqr_add_c(a,i,c0,c1,c2) { \
905
Index: openssl-0.9.8k/crypto/bn/bntest.c
906
===================================================================
907
--- openssl-0.9.8k.orig/crypto/bn/bntest.c 2015-01-09 11:13:58.810255208 -0500
908
+++ openssl-0.9.8k/crypto/bn/bntest.c 2015-01-09 11:13:58.806255173 -0500
909
@@ -676,44 +676,98 @@
911
int test_sqr(BIO *bp, BN_CTX *ctx)
915
+ BIGNUM *a,*c,*d,*e;
926
+ if (a == NULL || c == NULL || d == NULL || e == NULL)
931
for (i=0; i<num0; i++)
933
- BN_bntest_rand(&a,40+i*10,0,0);
936
+ BN_bntest_rand(a,40+i*10,0,0);
954
- BN_div(&d,&e,&c,&a,ctx);
956
- if(!BN_is_zero(&d) || !BN_is_zero(&e))
958
- fprintf(stderr,"Square test failed!\n");
967
+ BN_div(d,e,c,a,ctx);
969
+ if(!BN_is_zero(d) || !BN_is_zero(e))
971
+ fprintf(stderr,"Square test failed!\n");
976
+ /* Regression test for a BN_sqr overflow bug. */
978
+ "80000000000000008000000000000001FFFFFFFFFFFFFFFE0000000000000000");
985
+ BIO_puts(bp," * ");
987
+ BIO_puts(bp," - ");
992
+ BN_mul(d, a, a, ctx);
995
+ fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
996
+ "different results!\n");
1000
+ /* Regression test for a BN_sqr overflow bug. */
1002
+ "80000000000000000000000080000001FFFFFFFE000000000000000000000000");
1003
+ BN_sqr(c, a, ctx);
1009
+ BIO_puts(bp," * ");
1011
+ BIO_puts(bp," - ");
1014
+ BIO_puts(bp,"\n");
1016
+ BN_mul(d, a, a, ctx);
1019
+ fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
1020
+ "different results!\n");
1025
+ if (a != NULL) BN_free(a);
1026
+ if (c != NULL) BN_free(c);
1027
+ if (d != NULL) BN_free(d);
1028
+ if (e != NULL) BN_free(e);
1032
int test_mont(BIO *bp, BN_CTX *ctx)