Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.security.spec.X509EncodedKeySpec;
import java.security.spec.XECPrivateKeySpec;
import java.security.spec.XECPublicKeySpec;
import java.util.Arrays;
import java.util.Optional;

class XDHKeyFactory extends KeyFactorySpi {
Expand Down Expand Up @@ -169,7 +170,12 @@ protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
}

Optional<byte[]> scalar = xecPrivKey.getScalar();
return keySpec.cast(new XECPrivateKeySpec(params, scalar.get()));
byte[] scalarArray = scalar.get();
try {
return keySpec.cast(new XECPrivateKeySpec(params, scalarArray));
} finally {
Arrays.fill(scalarArray, (byte) 0x00);
}
} else if (keySpec.isAssignableFrom(pkcs8KeySpec))
return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ final class XDHPrivateKeyImpl extends PKCS8Key implements XECPrivateKey, Seriali
private static final long serialVersionUID = 6034044314589513430L;

private OpenJCEPlusProvider provider = null;
private transient Optional<byte[]> scalar;
private transient NamedParameterSpec params;
private CURVE curve;
private byte[] k; // The raw key bytes, without OctetString or DER encoded
Expand All @@ -56,7 +55,6 @@ private void setFieldsFromXeckey() throws Exception {
if (k == null) {
k = extractPrivateKeyFromOCK(xecKey.getPrivateKeyBytes()); // Extract key from GSKit and sets params
setPKCS8KeyByte(k);
this.scalar = Optional.of(k);
this.algid = CurveUtil.getAlgId(this.params.getName());
}
}
Expand Down Expand Up @@ -96,7 +94,6 @@ public XDHPrivateKeyImpl(OpenJCEPlusProvider provider, byte[] encoded)
// to fit with GSKit and sets params
int encodingSize = CurveUtil.getDEREncodingSize(curve);
this.xecKey = XECKey.createPrivateKey(provider.getOCKContext(), alteredEncoded, encodingSize, provider);
this.scalar = Optional.of(k);
} catch (Exception exception) {
InvalidKeyException ike = new InvalidKeyException("Failed to create XEC private key");
provider.setOCKExceptionCause(ike, exception);
Expand Down Expand Up @@ -135,7 +132,6 @@ public XDHPrivateKeyImpl(OpenJCEPlusProvider provider, AlgorithmParameterSpec pa
// TODO: figure out how to build FFDHE curves from paramspec

this.provider = provider;
this.scalar = scalar;
if (scalar != null)
k = scalar.get();
try {
Expand Down Expand Up @@ -391,7 +387,7 @@ public Optional<byte[]> getScalar() {
} catch (Exception exception) {
this.exception = exception;
}
return scalar;
return Optional.of(getKeyBytes());
}

public byte[] getKeyBytes() {
Expand Down Expand Up @@ -527,7 +523,6 @@ public void destroy() throws DestroyFailedException {
if (this.key != null)
Arrays.fill(this.key, (byte) 0x00);
this.xecKey = null;
this.scalar = null;
this.params = null;
}
}
Expand Down