Try to remove sensitive parts from memory more aggressively - #123
Open
kwin wants to merge 1 commit into
Open
Conversation
Call SecretKey.destroy() and PBEKeySpec.clearPassword() to overwrite memory locations with 0 instead of just waiting for GC. Unfortunately right now not well supported by Java (https://bugs.openjdk.org/browse/JDK-8389121).
kwin
force-pushed
the
feature/try-to-dispose-key-asap
branch
from
July 29, 2026 12:03
efd1d76 to
1ca998a
Compare
There was a problem hiding this comment.
Pull request overview
This PR aims to more aggressively reduce the lifetime of sensitive key material in memory during AES-GCM encryption/decryption by explicitly clearing password-derived key data instead of relying solely on garbage collection.
Changes:
- Clears
PBEKeySpecpassword material viaclearPassword()after key derivation. - Attempts to destroy derived
SecretKeyinstances via a newdestroyQuietly(...)helper. - Destroys encryption/decryption keys after
doFinal(...).
Comments suppressed due to low confidence (2)
src/main/java/org/codehaus/plexus/components/secdispatcher/internal/cipher/AESGCMNoPadding.java:91
secretKeyis only destroyed on the happy path. If decoding, key derivation,init(...), ordoFinal(...)throws, the key won't be destroyed. Wrap the method body sodestroyQuietly(secretKey)always runs infinally.
SecretKey secretKey = getAESKeyFromPassword(password.toCharArray(), salt);
Cipher cipher = Cipher.getInstance(CIPHER_ALG);
cipher.init(Cipher.DECRYPT_MODE, secretKey, new GCMParameterSpec(TAG_LENGTH_BIT, iv));
byte[] plainText = cipher.doFinal(cipherText);
destroyQuietly(secretKey);
return new String(plainText, StandardCharsets.UTF_8);
src/main/java/org/codehaus/plexus/components/secdispatcher/internal/cipher/AESGCMNoPadding.java:110
spec.clearPassword()anddestroyQuietly(originalKey)should run even ifgenerateSecret(...)orgetEncoded()throws. Usingtry/finallyhere ensures password/key material is cleared on both success and failure paths.
PBEKeySpec spec = new PBEKeySpec(password, salt, PBE_ITERATIONS, PBE_KEY_SIZE);
SecretKey originalKey = factory.generateSecret(spec);
spec.clearPassword();
SecretKey derivedKey = new SecretKeySpec(originalKey.getEncoded(), KEY_ALGORITHM);
destroyQuietly(originalKey);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+114
to
121
| private static void destroyQuietly(SecretKey key) { | ||
| try { | ||
| key.destroy(); | ||
| } catch (DestroyFailedException e) { | ||
| // Ignore exception during key destruction as not all SecretKey implementations support destruction | ||
| // https://bugs.openjdk.org/browse/JDK-8389121 | ||
| } | ||
| } |
Comment on lines
59
to
64
| SecretKey secretKey = getAESKeyFromPassword(password.toCharArray(), salt); | ||
| Cipher cipher = Cipher.getInstance(CIPHER_ALG); | ||
| cipher.init(Cipher.ENCRYPT_MODE, secretKey, new GCMParameterSpec(TAG_LENGTH_BIT, iv)); | ||
| byte[] cipherText = cipher.doFinal(clearText.getBytes(StandardCharsets.UTF_8)); | ||
| destroyQuietly(secretKey); | ||
| byte[] cipherTextWithIvSalt = ByteBuffer.allocate(iv.length + salt.length + cipherText.length) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Call SecretKey.destroy() and PBEKeySpec.clearPassword() to overwrite memory locations with 0 instead of just waiting for GC. Unfortunately right now not well supported by Java (https://bugs.openjdk.org/browse/JDK-8389121).