Skip to content

Commit 6fcd2d8

Browse files
authored
Merge pull request #600 from RUB-NDS/nullpointerfix
fixed nullpointer exception when pms is null
2 parents fb766a0 + 5562aa4 commit 6fcd2d8

File tree

4 files changed

+57
-57
lines changed

4 files changed

+57
-57
lines changed

Attacks/src/main/java/de/rub/nds/tlsattacker/attacks/impl/InvalidCurveAttacker.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ private void setPremasterSecret(EllipticCurve curve, int i, Point point) {
117117
premasterSecret = config.getPremasterSecret();
118118
} else {
119119
Point sharedPoint = curve.mult(new BigInteger("" + (i + 1)), point);
120-
premasterSecret = sharedPoint.getX().getData();
121-
if (premasterSecret == null) {
120+
if (sharedPoint.getX() != null) {
121+
premasterSecret = sharedPoint.getX().getData();
122+
} else {
122123
premasterSecret = BigInteger.ZERO;
123124
}
124125
LOGGER.debug("PMS: " + premasterSecret.toString());

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/crypto/ec/EllipticCurve.java

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* fields.
1616
*/
1717
public abstract class EllipticCurve {
18+
1819
private Point basePoint;
1920
private BigInteger basePointOrder;
2021
/**
@@ -26,7 +27,7 @@ public abstract class EllipticCurve {
2627
* Every child class must define its own public constructor. These
2728
* constructors must be able to set the coefficients for the curve. They can
2829
* use this constructor to set the value of modulus.
29-
*
30+
*
3031
* @param modulus
3132
* The modulus of the field over which the curve is defined.
3233
*/
@@ -39,7 +40,7 @@ protected EllipticCurve(BigInteger modulus) {
3940
* constructors must be able to set the coefficients for the curve. They can
4041
* use this constructor to set the values of modulus, basePoint and
4142
* basePointOrder.
42-
*
43+
*
4344
* @param modulus
4445
* The modulus of the field over which the curve is defined.
4546
* @param basePointX
@@ -60,7 +61,7 @@ protected EllipticCurve(BigInteger modulus, BigInteger basePointX, BigInteger ba
6061
* result will be null. If one point is not on the curve and the
6162
* calculations would require dividing by 0, the result will be the point at
6263
* infinity.
63-
*
64+
*
6465
* @param p
6566
* A point which's coordinates are elements of the field over
6667
* which the curve is defined or the point at infinity.
@@ -69,83 +70,70 @@ protected EllipticCurve(BigInteger modulus, BigInteger basePointX, BigInteger ba
6970
* which the curve is defined or the point at infinity.
7071
*/
7172
public Point add(Point p, Point q) {
73+
if (p.isAtInfinity()) {
74+
// O + q == q
75+
return q;
76+
}
7277

73-
if (p == null || q == null) {
74-
return null;
75-
} else {
76-
if (p.isAtInfinity()) {
77-
// O + q == q
78-
return q;
79-
}
80-
81-
if (q.isAtInfinity()) {
82-
// p + O == p
83-
return p;
84-
}
85-
86-
if (this.inverse(p).equals(q)) {
87-
// p == -q <=> -p == q
88-
// => p + q = O
89-
return new Point();
90-
}
78+
if (q.isAtInfinity()) {
79+
// p + O == p
80+
return p;
81+
}
9182

92-
return this.additionFormular(p, q);
83+
if (this.inverse(p).equals(q)) {
84+
// p == -q <=> -p == q
85+
// => p + q = O
86+
return new Point();
9387
}
88+
89+
return this.additionFormular(p, q);
9490
}
9591

9692
/**
9793
* Returns k*p on this curve. If k or p is null, the result will be null. If
9894
* the point is not on the curve and the calculations would require dividing
9995
* by 0, the result will be the point at infinity.
100-
*
96+
*
10197
* @param p
10298
* A point which's coordinates are elements of the field over
10399
* which the curve is defined or the point at infinity.
104100
*/
105101
public Point mult(BigInteger k, Point p) {
106-
if (k == null || p == null) {
107-
return null;
108-
} else {
109-
if (k.compareTo(BigInteger.ZERO) < 0) {
110-
k = k.negate();
111-
p = this.inverse(p);
112-
}
113-
114-
// Double-and-add
102+
if (k.compareTo(BigInteger.ZERO) < 0) {
103+
k = k.negate();
104+
p = this.inverse(p);
105+
}
115106

116-
Point q = new Point(); // q == O
107+
// Double-and-add
108+
Point q = new Point(); // q == O
117109

118-
for (int i = k.bitLength(); i > 0; i--) {
110+
for (int i = k.bitLength(); i > 0; i--) {
119111

120-
q = this.add(q, q);
112+
q = this.add(q, q);
121113

122-
if (k.testBit(i - 1)) {
123-
q = this.add(q, p);
124-
}
114+
if (k.testBit(i - 1)) {
115+
q = this.add(q, p);
125116
}
126-
127-
return q;
128117
}
118+
119+
return q;
129120
}
130121

131122
/**
132123
* Returns the unique point q with the property p + q = O on this curve. If
133124
* p is null the result will be null.
134-
*
125+
*
135126
* @param p
136127
* A point which's coordinates are elements of the field over
137128
* which the curve is defined or the point at infinity.
138129
*/
139130
public Point inverse(Point p) {
140-
if (p == null) {
141-
return null;
131+
132+
if (p.isAtInfinity()) {
133+
// -O == O
134+
return p;
142135
} else {
143-
if (p.isAtInfinity()) {
144-
// -O == O
145-
return p;
146-
} else {
147-
return this.inverseAffine(p);
148-
}
136+
return this.inverseAffine(p);
149137
}
150138
}
151139

@@ -154,7 +142,7 @@ public Point inverse(Point p) {
154142
* are elements of the field over which this curve is defined. Whenever
155143
* possible, this method should be used instead of creating a point via its
156144
* own constructor.
157-
*
145+
*
158146
* @param x
159147
* The x coordinate of the point.
160148
* @param y
@@ -164,7 +152,7 @@ public Point inverse(Point p) {
164152

165153
/**
166154
* Returns true iff the point p is on the curve.
167-
*
155+
*
168156
* @param p
169157
* An affine point which's coordinates are elements of the field
170158
* over which the curve is defined or the point at infinity.
@@ -174,7 +162,7 @@ public Point inverse(Point p) {
174162
/**
175163
* Returns the unique (affine) point q with the property p + q = O on this
176164
* curve.
177-
*
165+
*
178166
* @param p
179167
* An affine point which's coordinates are elements of the field
180168
* over which the curve is defined.
@@ -185,7 +173,7 @@ public Point inverse(Point p) {
185173
* Returns p+q for two affine points p and q, with p != -q. If one point is
186174
* not on the curve and the calculations would require dividing by 0, the
187175
* result will be the point at infinity.
188-
*
176+
*
189177
* @param p
190178
* An affine point which's coordinates are elements of the field
191179
* over which the curve is defined.

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/crypto/ec/EllipticCurveOverF2m.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
package de.rub.nds.tlsattacker.core.crypto.ec;
1010

1111
import java.math.BigInteger;
12+
import org.apache.logging.log4j.LogManager;
13+
import org.apache.logging.log4j.Logger;
1214

1315
/**
1416
* An elliptic curve over a galois field F_{2^m}.<br />
@@ -18,6 +20,8 @@
1820
*/
1921
public class EllipticCurveOverF2m extends EllipticCurve {
2022

23+
private final static Logger LOGGER = LogManager.getLogger();
24+
2125
private final FieldElementF2m a;
2226
private final FieldElementF2m b;
2327

@@ -147,7 +151,8 @@ protected Point additionFormular(Point p, Point q) {
147151

148152
return new Point(x3, y3);
149153
} catch (ArithmeticException e) {
150-
return new Point();
154+
LOGGER.warn("Encountered an arithmetic exception during addition. Returning point at 0,0");
155+
return this.getPoint(BigInteger.ZERO, BigInteger.ZERO);
151156
}
152157
}
153158

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/crypto/ec/EllipticCurveOverFp.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
*/
99
package de.rub.nds.tlsattacker.core.crypto.ec;
1010

11+
import de.rub.nds.tlsattacker.core.constants.GOSTCurve;
1112
import java.math.BigInteger;
13+
import org.apache.logging.log4j.LogManager;
14+
import org.apache.logging.log4j.Logger;
1215

1316
/**
1417
* An elliptic curve over a galois field F_p, where p is a prime number.
1518
*/
1619
public class EllipticCurveOverFp extends EllipticCurve {
1720

21+
private static final Logger LOGGER = LogManager.getLogger();
22+
1823
private final FieldElementFp a;
1924
private final FieldElementFp b;
2025

@@ -132,7 +137,8 @@ protected Point additionFormular(Point p, Point q) {
132137

133138
return new Point(x3, y3);
134139
} catch (ArithmeticException e) {
135-
return new Point();
140+
LOGGER.warn("Encountered an arithmetic exception during addition. Returning point at 0,0");
141+
return this.getPoint(BigInteger.ZERO, BigInteger.ZERO);
136142
}
137143
}
138144

0 commit comments

Comments
 (0)