Skip to content

Commit 5a65c1a

Browse files
committed
Use SilentByteArrayOutputStream instead of ByteArrayOutputStream
1 parent f4f3058 commit 5a65c1a

File tree

3 files changed

+37
-62
lines changed

3 files changed

+37
-62
lines changed

src/main/java/de/rub/nds/protocol/crypto/ec/PointFormatter.java

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
import de.rub.nds.protocol.constants.NamedEllipticCurveParameters;
1515
import de.rub.nds.protocol.constants.PointFormat;
1616
import de.rub.nds.protocol.crypto.CyclicGroup;
17-
import de.rub.nds.protocol.exception.PreparationException;
17+
import de.rub.nds.protocol.util.SilentByteArrayOutputStream;
1818
import java.io.ByteArrayInputStream;
19-
import java.io.ByteArrayOutputStream;
2019
import java.io.IOException;
2120
import java.math.BigInteger;
2221
import org.apache.logging.log4j.LogManager;
@@ -28,7 +27,7 @@ public class PointFormatter {
2827

2928
public static byte[] formatToByteArray(
3029
GroupParameters<?> groupParameters, Point point, PointFormat format) {
31-
ByteArrayOutputStream stream = new ByteArrayOutputStream();
30+
SilentByteArrayOutputStream stream = new SilentByteArrayOutputStream();
3231
if (point.isAtInfinity()) {
3332
return new byte[1];
3433
}
@@ -40,16 +39,12 @@ public static byte[] formatToByteArray(
4039
switch (format) {
4140
case UNCOMPRESSED:
4241
stream.write(0x04);
43-
try {
44-
stream.write(
45-
ArrayConverter.bigIntegerToNullPaddedByteArray(
46-
point.getFieldX().getData(), elementLength));
47-
stream.write(
48-
ArrayConverter.bigIntegerToNullPaddedByteArray(
49-
point.getFieldY().getData(), elementLength));
50-
} catch (IOException ex) {
51-
throw new PreparationException("Could not serialize ec point", ex);
52-
}
42+
stream.write(
43+
ArrayConverter.bigIntegerToNullPaddedByteArray(
44+
point.getFieldX().getData(), elementLength));
45+
stream.write(
46+
ArrayConverter.bigIntegerToNullPaddedByteArray(
47+
point.getFieldY().getData(), elementLength));
5348
return stream.toByteArray();
5449
case COMPRESSED:
5550
CyclicGroup<?> group = groupParameters.getGroup();
@@ -66,47 +61,35 @@ public static byte[] formatToByteArray(
6661
} else {
6762
stream.write(0x02);
6863
}
69-
try {
70-
stream.write(
71-
ArrayConverter.bigIntegerToNullPaddedByteArray(
72-
point.getFieldX().getData(), elementLength));
73-
} catch (IOException ex) {
74-
throw new PreparationException("Could not serialize ec point", ex);
75-
}
64+
stream.write(
65+
ArrayConverter.bigIntegerToNullPaddedByteArray(
66+
point.getFieldX().getData(), elementLength));
7667
return stream.toByteArray();
7768
default:
7869
throw new UnsupportedOperationException("Unsupported PointFormat: " + format);
7970
}
8071
} else {
81-
try {
82-
byte[] coordinate =
83-
ArrayConverter.bigIntegerToNullPaddedByteArray(
84-
point.getFieldX().getData(), elementLength);
85-
stream.write(coordinate);
86-
} catch (IOException ex) {
87-
throw new PreparationException("Could not serialize ec point", ex);
88-
}
72+
byte[] coordinate =
73+
ArrayConverter.bigIntegerToNullPaddedByteArray(
74+
point.getFieldX().getData(), elementLength);
75+
stream.write(coordinate);
8976
return stream.toByteArray();
9077
}
9178
}
9279

9380
public static byte[] toRawFormat(Point point) {
94-
ByteArrayOutputStream stream = new ByteArrayOutputStream();
81+
SilentByteArrayOutputStream stream = new SilentByteArrayOutputStream();
9582
if (point.isAtInfinity()) {
9683
return new byte[1];
9784
}
9885
int elementLength =
9986
ArrayConverter.bigIntegerToByteArray(point.getFieldX().getModulus()).length;
100-
try {
101-
stream.write(
102-
ArrayConverter.bigIntegerToNullPaddedByteArray(
103-
point.getFieldX().getData(), elementLength));
104-
stream.write(
105-
ArrayConverter.bigIntegerToNullPaddedByteArray(
106-
point.getFieldY().getData(), elementLength));
107-
} catch (IOException ex) {
108-
throw new PreparationException("Could not serialize ec point", ex);
109-
}
87+
stream.write(
88+
ArrayConverter.bigIntegerToNullPaddedByteArray(
89+
point.getFieldX().getData(), elementLength));
90+
stream.write(
91+
ArrayConverter.bigIntegerToNullPaddedByteArray(
92+
point.getFieldY().getData(), elementLength));
11093
return stream.toByteArray();
11194
}
11295

src/main/java/de/rub/nds/protocol/crypto/signature/SignatureCalculator.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import de.rub.nds.protocol.crypto.key.PrivateKeyContainer;
2121
import de.rub.nds.protocol.crypto.key.RsaPrivateKey;
2222
import de.rub.nds.protocol.exception.CryptoException;
23-
import java.io.ByteArrayOutputStream;
23+
import de.rub.nds.protocol.util.SilentByteArrayOutputStream;
2424
import java.io.IOException;
2525
import java.math.BigInteger;
2626
import java.util.Arrays;
@@ -297,18 +297,13 @@ private byte[] computePkcs1Padding(int toBePaddedLength, int modLengthInByte) {
297297
// Dont pad in this case
298298
return new byte[0];
299299
} else {
300-
try {
301-
ByteArrayOutputStream stream = new ByteArrayOutputStream();
302-
stream.write(new byte[] {0, 1});
303-
while (stream.size() < modLengthInByte - toBePaddedLength - 1) {
304-
stream.write((byte) 0xFF);
305-
}
306-
stream.write(0);
307-
return stream.toByteArray();
308-
} catch (IOException ex) {
309-
LOGGER.error("Could not generate padding", ex);
310-
throw new RuntimeException(ex);
300+
SilentByteArrayOutputStream stream = new SilentByteArrayOutputStream();
301+
stream.write(new byte[] {0, 1});
302+
while (stream.size() < modLengthInByte - toBePaddedLength - 1) {
303+
stream.write((byte) 0xFF);
311304
}
305+
stream.write(0);
306+
return stream.toByteArray();
312307
}
313308
}
314309

@@ -412,7 +407,7 @@ public void computeDsaSignature(
412407
ASN1Encodable[] encodables = new ASN1Encodable[] {asn1IntegerR, asn1IntegerS};
413408
DERSequence derSequence = new DERSequence(encodables);
414409

415-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
410+
SilentByteArrayOutputStream outputStream = new SilentByteArrayOutputStream();
416411
try {
417412
derSequence.encodeTo(outputStream);
418413
} catch (IOException ex) {
@@ -497,13 +492,11 @@ public void computeRawEcdsaSignature(
497492
LOGGER.debug("CurveBasePointOrder: {}", curve.getBasePointOrder().toByteArray());
498493
LOGGER.debug("Modulus: {}", curve.getModulus().toByteArray());
499494

500-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
501-
try {
502-
outputStream.write(ArrayConverter.bigIntegerToByteArray(r, 32, true));
503-
outputStream.write(ArrayConverter.bigIntegerToByteArray(s, 32, true));
504-
} catch (IOException ex) {
505-
throw new CryptoException("Could not write Signature to output stream");
506-
}
495+
SilentByteArrayOutputStream outputStream = new SilentByteArrayOutputStream();
496+
497+
outputStream.write(ArrayConverter.bigIntegerToByteArray(r, 32, true));
498+
outputStream.write(ArrayConverter.bigIntegerToByteArray(s, 32, true));
499+
507500
byte[] completeSignature = outputStream.toByteArray();
508501
computations.setSignatureBytes(completeSignature);
509502
computations.setSignatureValid(true);
@@ -589,7 +582,7 @@ public void computeEcdsaSignature(
589582
ASN1Encodable[] encodables = new ASN1Encodable[] {asn1IntegerR, asn1IntegerS};
590583
DERSequence derSequence = new DERSequence(encodables);
591584

592-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
585+
SilentByteArrayOutputStream outputStream = new SilentByteArrayOutputStream();
593586
try {
594587
derSequence.encodeTo(outputStream);
595588
} catch (IOException ex) {

src/main/java/de/rub/nds/protocol/util/DeepCopyUtil.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package de.rub.nds.protocol.util;
1010

1111
import java.io.ByteArrayInputStream;
12-
import java.io.ByteArrayOutputStream;
1312
import java.io.IOException;
1413
import java.io.ObjectInputStream;
1514
import java.io.ObjectOutputStream;
@@ -19,7 +18,7 @@ private DeepCopyUtil() {}
1918

2019
public static <T> T deepCopy(T object) {
2120
try {
22-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
21+
SilentByteArrayOutputStream outputStream = new SilentByteArrayOutputStream();
2322
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
2423
objectOutputStream.writeObject(object);
2524
objectOutputStream.flush();

0 commit comments

Comments
 (0)