Skip to content

Commit 81bf62a

Browse files
committed
JCE: Zero copy of buffers before returning
1 parent a9268de commit 81bf62a

4 files changed

Lines changed: 26 additions & 6 deletions

File tree

src/main/java/com/wolfssl/provider/jce/WolfCryptKeyAgreement.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,11 @@ protected int engineGenerateSecret(byte[] sharedSecret, int offset)
332332
this.ecPublic = new Ecc();
333333
this.ecPrivate.releaseNativeStruct();
334334
this.ecPrivate = new Ecc();
335-
this.ecPrivate.importPrivateOnCurve(priv, null, this.curveName);
336-
zeroArray(priv);
335+
try {
336+
this.ecPrivate.importPrivateOnCurve(priv, null, this.curveName);
337+
} finally {
338+
zeroArray(priv);
339+
}
337340

338341
this.state = EngineState.WC_PRIVKEY_DONE;
339342

@@ -489,8 +492,11 @@ private void wcInitDHParams(Key key, AlgorithmParameterSpec params)
489492
"Unable to get DH private key from Key object");
490493
}
491494

492-
this.dh.setPrivateKey(dhPriv);
493-
zeroArray(dhPriv);
495+
try {
496+
this.dh.setPrivateKey(dhPriv);
497+
} finally {
498+
zeroArray(dhPriv);
499+
}
494500

495501
return;
496502
}
@@ -531,6 +537,7 @@ private void wcInitECDHParams(Key key, AlgorithmParameterSpec params)
531537
BigInteger privateValue = null;
532538
BigInteger order = null;
533539
ECParameterSpec ecParams = null;
540+
byte[] privKeyBytes;
534541

535542
if (!(key instanceof ECPrivateKey)) {
536543
throw new InvalidKeyException(
@@ -568,8 +575,15 @@ private void wcInitECDHParams(Key key, AlgorithmParameterSpec params)
568575
throw new InvalidAlgorithmParameterException(
569576
"ECC curve is null, please check algorithm parameters");
570577
}
571-
this.ecPrivate.importPrivateOnCurve(ecKey.getS().toByteArray(),
578+
579+
privKeyBytes = ecKey.getS().toByteArray();
580+
581+
try {
582+
this.ecPrivate.importPrivateOnCurve(privKeyBytes,
572583
null, this.curveName);
584+
} finally {
585+
zeroArray(privKeyBytes);
586+
}
573587
}
574588

575589
/**

src/main/java/com/wolfssl/provider/jce/WolfCryptRSAPrivateCrtKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ private byte[] removeLeadingZero(byte[] data) {
240240
if ((data != null) && (data.length > 1) && (data[0] == 0)) {
241241
byte[] result = new byte[data.length - 1];
242242
System.arraycopy(data, 1, result, 0, data.length - 1);
243+
Arrays.fill(data, (byte)0);
243244
return result;
244245
}
245246
return data;

src/main/java/com/wolfssl/provider/jce/WolfCryptRSAPrivateKey.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ private byte[] convertBigIntegerToUnsignedBytes(BigInteger value) {
160160

161161
/* Remove leading zero byte if present (sign byte) */
162162
if ((bytes.length > 0) && (bytes[0] == 0)) {
163-
return Arrays.copyOfRange(bytes, 1, bytes.length);
163+
byte[] cpyBytes = Arrays.copyOfRange(bytes, 1, bytes.length);
164+
Arrays.fill(bytes, (byte)0);
165+
return cpyBytes;
164166
}
165167
return bytes;
166168
}

src/main/java/com/wolfssl/wolfcrypt/Ecc.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.math.BigInteger;
2525
import java.security.InvalidAlgorithmParameterException;
2626
import java.security.spec.EllipticCurve;
27+
import java.util.Arrays;
2728
import java.security.spec.ECParameterSpec;
2829
import java.security.spec.ECFieldFp;
2930

@@ -673,6 +674,7 @@ public static byte[] bigIntToFixedSizeByteArray(BigInteger value,
673674
if ((bytes.length == fieldSizeBytes + 1) && (bytes[0] == 0)) {
674675
byte[] result = new byte[fieldSizeBytes];
675676
System.arraycopy(bytes, 1, result, 0, fieldSizeBytes);
677+
Arrays.fill(bytes, (byte)0);
676678
return result;
677679
}
678680

@@ -681,6 +683,7 @@ public static byte[] bigIntToFixedSizeByteArray(BigInteger value,
681683
byte[] result = new byte[fieldSizeBytes];
682684
System.arraycopy(bytes, 0, result, fieldSizeBytes - bytes.length,
683685
bytes.length);
686+
Arrays.fill(bytes, (byte)0);
684687
return result;
685688
}
686689

0 commit comments

Comments
 (0)