Skip to content
Open
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 @@ -25,6 +25,7 @@
import java.security.KeyPair;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.util.Arrays;

/** Diffie-Hellman-based P-256, P-384 and P-521 HPKE KEM variant. */
@Immutable
Expand Down Expand Up @@ -150,8 +151,12 @@ public byte[] decapsulate(byte[] encapsulatedKey, HpkeKemPrivateKey recipientPri
ECPublicKey publicKey =
EllipticCurves.getEcPublicKey(curve, PointFormatType.UNCOMPRESSED, encapsulatedKey);
byte[] dhSharedSecret = EllipticCurves.computeSharedSecret(privateKey, publicKey);
return deriveKemSharedSecret(
dhSharedSecret, encapsulatedKey, recipientPrivateKey.getSerializedPublic().toByteArray());
try {
return deriveKemSharedSecret(
dhSharedSecret, encapsulatedKey, recipientPrivateKey.getSerializedPublic().toByteArray());
} finally {
Arrays.fill(dhSharedSecret, (byte) 0);
}
}

@Override
Expand All @@ -171,11 +176,15 @@ public byte[] authDecapsulate(
privateKey,
EllipticCurves.getEcPublicKey(
curve, PointFormatType.UNCOMPRESSED, senderPublicKey)));
return deriveKemSharedSecret(
dhSharedSecret,
encapsulatedKey,
recipientPrivateKey.getSerializedPublic().toByteArray(),
senderPublicKey);
try {
return deriveKemSharedSecret(
dhSharedSecret,
encapsulatedKey,
recipientPrivateKey.getSerializedPublic().toByteArray(),
senderPublicKey);
} finally {
Arrays.fill(dhSharedSecret, (byte) 0);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,12 @@ public byte[] decapsulate(byte[] encapsulatedKey, HpkeKemPrivateKey recipientPri
byte[] dhSharedSecret =
x25519.computeSharedSecret(
recipientPrivateKey.getSerializedPrivate().toByteArray(), encapsulatedKey);
return deriveKemSharedSecret(
dhSharedSecret, encapsulatedKey, recipientPrivateKey.getSerializedPublic().toByteArray());
try {
return deriveKemSharedSecret(
dhSharedSecret, encapsulatedKey, recipientPrivateKey.getSerializedPublic().toByteArray());
} finally {
Arrays.fill(dhSharedSecret, (byte) 0);
}
}

@Override
Expand All @@ -159,8 +163,13 @@ public byte[] authDecapsulate(
x25519.computeSharedSecret(privateKey, encapsulatedKey),
x25519.computeSharedSecret(privateKey, senderPublicKey));
byte[] recipientPublicKey = recipientPrivateKey.getSerializedPublic().toByteArray();
return deriveKemSharedSecret(
dhSharedSecret, encapsulatedKey, recipientPublicKey, senderPublicKey);
try {
return deriveKemSharedSecret(
dhSharedSecret, encapsulatedKey, recipientPublicKey, senderPublicKey);
} finally {
Arrays.fill(privateKey, (byte) 0);
Arrays.fill(dhSharedSecret, (byte) 0);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.errorprone.annotations.Immutable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Arrays;

/** A class storing a secret BigInteger, protecting the value via {@link SecretKeyAccess}. */
@Immutable
Expand Down Expand Up @@ -65,6 +66,11 @@ public boolean equalsSecretBigInteger(SecretBigInteger other) {
// BigInteger of the same values return different encodings.
byte[] myArray = value.toByteArray();
byte[] otherArray = other.value.toByteArray();
return MessageDigest.isEqual(myArray, otherArray);
try {
return MessageDigest.isEqual(myArray, otherArray);
} finally {
Arrays.fill(myArray, (byte) 0);
Arrays.fill(otherArray, (byte) 0);
}
}
}