This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 2161b5f210cb236ac7fde09d3745034f4fcf83a7 Author: Gary Gregory <[email protected]> AuthorDate: Wed Dec 2 15:41:23 2026 -0500 [7-Zip] Simplify AES256Options constructors Remove the configurable password constructor and use a private copy constructor to share the derived key while generating a fresh IV. Replace per-instance salt and cycle-count fields with fixed defaults. Update tests to exercise the password constructor and verify key reuse after the caller clears the password. --- .../compress/archivers/sevenz/AES256Options.java | 46 ++++++++-------------- .../archivers/sevenz/AES256OptionsTest.java | 36 ++++++++--------- 2 files changed, 33 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256Options.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256Options.java index 9bb33caed..17ae66125 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256Options.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256Options.java @@ -39,6 +39,11 @@ final class AES256Options { private static final byte[] EMPTY_BYTE_ARRAY = {}; + /** + * Base-2 logarithm of the number of key derivation iterations. + */ + private static final int NUM_CYCLES_POWER = 19; + static final String ALGORITHM = "AES"; static final String TRANSFORMATION = "AES/CBC/NoPadding"; @@ -57,11 +62,8 @@ private static byte[] randomBytes(final int size) throws ArchiveException { return bytes; } - private final byte[] salt; private final byte[] iv; - private final int numCyclesPower; - private final SecretKey aesKey; /** @@ -71,35 +73,19 @@ private static byte[] randomBytes(final int size) throws ArchiveException { * @throws ArchiveException if key derivation or IV generation fails. */ AES256Options(final char[] password) throws ArchiveException { - this(password, EMPTY_BYTE_ARRAY, randomBytes(16), 19); - } - - /** - * Creates encryption options by deriving an AES key from the password. - * - * @param password password used for encryption. - * @param salt salt used for key derivation. - * @param iv initialization vector used for encryption. - * @param numCyclesPower base-2 logarithm of the number of key derivation iterations. - * @throws ArchiveException if key derivation fails. - */ - AES256Options(final char[] password, final byte[] salt, final byte[] iv, final int numCyclesPower) throws ArchiveException { - this(newSecretKeySpec(AES256SHA256Decoder.sha256Password(password, numCyclesPower, salt)), salt, iv, numCyclesPower); + iv = randomBytes(16); + aesKey = newSecretKeySpec(AES256SHA256Decoder.sha256Password(password, NUM_CYCLES_POWER, EMPTY_BYTE_ARRAY)); } /** - * Creates encryption options using an already derived key. + * Creates options sharing the derived key with a fresh random IV. * - * @param aesKey the derived AES key. - * @param salt salt used to derive the key. - * @param iv initialization vector used for encryption. - * @param numCyclesPower base-2 logarithm of the number of key derivation iterations. + * @param options the options whose derived key to reuse. + * @throws ArchiveException if IV generation fails. */ - private AES256Options(final SecretKey aesKey, final byte[] salt, final byte[] iv, final int numCyclesPower) { - this.aesKey = aesKey; - this.salt = salt; - this.iv = iv; - this.numCyclesPower = numCyclesPower; + private AES256Options(final AES256Options options) throws ArchiveException { + aesKey = options.aesKey; + iv = randomBytes(16); } byte[] getIv() { @@ -107,11 +93,11 @@ byte[] getIv() { } int getNumCyclesPower() { - return numCyclesPower; + return NUM_CYCLES_POWER; } byte[] getSalt() { - return salt; + return EMPTY_BYTE_ARRAY; } /** @@ -142,6 +128,6 @@ Cipher newCipher() throws ArchiveException { * @throws ArchiveException if IV generation fails. */ AES256Options withFreshIv() throws ArchiveException { - return new AES256Options(aesKey, salt, randomBytes(16), numCyclesPower); + return new AES256Options(this); } } diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/AES256OptionsTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/AES256OptionsTest.java index eb179687d..670b70044 100644 --- a/src/test/java/org/apache/commons/compress/archivers/sevenz/AES256OptionsTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/AES256OptionsTest.java @@ -22,9 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import java.security.InvalidAlgorithmParameterException; import java.util.Arrays; import javax.crypto.Cipher; @@ -40,9 +38,17 @@ */ class AES256OptionsTest { + @Test + void testDefaultParameters() throws ArchiveException { + final AES256Options options = new AES256Options("password".toCharArray()); + assertArrayEquals(new byte[0], options.getSalt()); + assertEquals(19, options.getNumCyclesPower()); + assertEquals(16, options.getIv().length); + } + @Test void testIndependentCiphers() throws Exception { - final AES256Options options = new AES256Options("password".toCharArray(), new byte[0], new byte[16], 1); + final AES256Options options = new AES256Options("password".toCharArray()); final Cipher first = options.newCipher(); final Cipher second = options.newCipher(); assertNotSame(first, second); @@ -55,26 +61,18 @@ void testIndependentCiphers() throws Exception { assertArrayEquals(expected, first.doFinal(block, 8, 8)); } - @Test - void testInvalidIv() throws ArchiveException { - final AES256Options options = new AES256Options("password".toCharArray(), new byte[0], new byte[1], 1); - final ArchiveException exception = assertThrows(ArchiveException.class, options::newCipher); - assertEquals(InvalidAlgorithmParameterException.class, exception.getCause().getClass()); - } - @ParameterizedTest - @ValueSource(ints = {0, 1, 5}) - void testWithFreshIvPreservesKeyDerivationParameters(final int cycles) throws Exception { - final char[] password = "password".toCharArray(); - final byte[] salt = {1, 2, 3, 4, 5, 6, 7, 8}; - final byte[] iv = new byte[16]; - final AES256Options original = new AES256Options(password, salt, iv, cycles); + @ValueSource(strings = {"", "password", "p\u00e4ss\uD83D\uDD11"}) + void testWithFreshIvPreservesKey(final String passwordString) throws Exception { + final char[] password = passwordString.toCharArray(); + final AES256Options original = new AES256Options(password); + final byte[] iv = original.getIv().clone(); // The caller may discard the password before subsequent entries are written. Arrays.fill(password, '\0'); final AES256Options fresh = original.withFreshIv(); assertNotSame(original, fresh); - assertArrayEquals(salt, fresh.getSalt()); - assertEquals(cycles, fresh.getNumCyclesPower()); + assertArrayEquals(original.getSalt(), fresh.getSalt()); + assertEquals(original.getNumCyclesPower(), fresh.getNumCyclesPower()); assertArrayEquals(iv, original.getIv()); assertEquals(16, fresh.getIv().length); assertFalse(Arrays.equals(original.getIv(), fresh.getIv())); @@ -84,7 +82,7 @@ void testWithFreshIvPreservesKeyDerivationParameters(final int cycles) throws Ex final byte[] encrypted = fresh.newCipher().doFinal(plaintext); final Cipher decoder = Cipher.getInstance(AES256Options.TRANSFORMATION); decoder.init(Cipher.DECRYPT_MODE, - AES256Options.newSecretKeySpec(AES256SHA256Decoder.sha256Password("password".toCharArray(), cycles, salt)), new IvParameterSpec(fresh.getIv())); + AES256Options.newSecretKeySpec(AES256SHA256Decoder.sha256Password(passwordString.toCharArray(), 19, new byte[0])), new IvParameterSpec(fresh.getIv())); assertArrayEquals(plaintext, decoder.doFinal(encrypted)); assertFalse(Arrays.equals(original.newCipher().doFinal(plaintext), encrypted)); }
