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
18 changes: 9 additions & 9 deletions src/java.base/share/classes/javax/crypto/spec/DESKeySpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ public DESKeySpec(byte[] key, int offset) throws InvalidKeyException {
if (key == null) {
throw new NullPointerException("null key");
}
if (key.length - offset < DES_KEY_LEN) {
throw new InvalidKeyException("Wrong key size");
}
if (offset < 0) {
throw new ArrayIndexOutOfBoundsException("offset is negative");
}
if (key.length - offset < DES_KEY_LEN) {
throw new InvalidKeyException("Wrong key size");
}
this.key = new byte[DES_KEY_LEN];
System.arraycopy(key, offset, this.key, 0, DES_KEY_LEN);
}
Expand Down Expand Up @@ -198,12 +198,12 @@ public static boolean isParityAdjusted(byte[] key, int offset)
if (key == null) {
throw new InvalidKeyException("null key");
}
if (key.length - offset < DES_KEY_LEN) {
throw new InvalidKeyException("Wrong key size");
}
if (offset < 0) {
throw new ArrayIndexOutOfBoundsException("offset is negative");
}
if (key.length - offset < DES_KEY_LEN) {
throw new InvalidKeyException("Wrong key size");
}
for (int i = 0; i < DES_KEY_LEN; i++) {
int k = Integer.bitCount(key[offset++] & 0xff);
if ((k & 1) == 0) {
Expand Down Expand Up @@ -235,12 +235,12 @@ public static boolean isWeak(byte[] key, int offset)
if (key == null) {
throw new InvalidKeyException("null key");
}
if (key.length - offset < DES_KEY_LEN) {
throw new InvalidKeyException("Wrong key size");
}
if (offset < 0) {
throw new ArrayIndexOutOfBoundsException("offset is negative");
}
if (key.length - offset < DES_KEY_LEN) {
throw new InvalidKeyException("Wrong key size");
}
for (int i = 0; i < WEAK_KEYS.length; i++) {
boolean found = true;
for (int j = 0; j < DES_KEY_LEN; j++) {
Expand Down
12 changes: 6 additions & 6 deletions src/java.base/share/classes/javax/crypto/spec/DESedeKeySpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ public DESedeKeySpec(byte[] key, int offset) throws InvalidKeyException {
if (key == null) {
throw new NullPointerException("null key");
}
if (key.length - offset < DES_EDE_KEY_LEN) {
throw new InvalidKeyException("Wrong key size");
}
if (offset < 0) {
throw new ArrayIndexOutOfBoundsException("offset is negative");
}
if (key.length - offset < DES_EDE_KEY_LEN) {
throw new InvalidKeyException("Wrong key size");
}
this.key = new byte[24];
System.arraycopy(key, offset, this.key, 0, DES_EDE_KEY_LEN);
}
Expand Down Expand Up @@ -126,12 +126,12 @@ public static boolean isParityAdjusted(byte[] key, int offset)
if (key == null) {
throw new InvalidKeyException("null key");
}
if (key.length - offset < DES_EDE_KEY_LEN) {
throw new InvalidKeyException("Wrong key size");
}
if (offset < 0) {
throw new ArrayIndexOutOfBoundsException("offset is negative");
}
if (key.length - offset < DES_EDE_KEY_LEN) {
throw new InvalidKeyException("Wrong key size");
}
return DESKeySpec.isParityAdjusted(key, offset)
&& DESKeySpec.isParityAdjusted(key, offset + 8)
&& DESKeySpec.isParityAdjusted(key, offset + 16);
Expand Down
37 changes: 30 additions & 7 deletions test/jdk/javax/crypto/spec/DESKeySpec/OffsetKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@

/*
* @test
* @bug 8364121
* @summary DESKeySpec.isWeak should throw aiobe exception if the offset is
* negative.
* @bug 8364121 8386473
* @summary Test DES[ede]KeySpec for negative and integer overflow offsets
*/
import java.security.InvalidKeyException;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.DESKeySpec;

Expand All @@ -52,21 +50,46 @@ public static void main(String[] args) throws Exception {
boolean weak = DESKeySpec.isWeak(strongKey, -1);
throw new Exception("expected ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aiobe) {}
try{
try {
boolean parityAdjusted = DESKeySpec.isParityAdjusted(strongKey, -1);
throw new Exception("expected ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aiobe) {}

try {
DESKeySpec desKey = new DESKeySpec(strongKey, Integer.MIN_VALUE);
throw new Exception("expected ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aiobe) {}
try {
boolean weak = DESKeySpec.isWeak(strongKey, Integer.MIN_VALUE);
throw new Exception("expected ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aiobe) {}
try {
boolean parityAdjusted = DESKeySpec.isParityAdjusted(strongKey,
Integer.MIN_VALUE);
throw new Exception("expected ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aiobe) {}

// Test triple-DES
try{
try {
DESedeKeySpec desEdeKey = new DESedeKeySpec(strongKey, -1);
throw new Exception("expected ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aiobe) {}
try{
try {
boolean parityAdjusted = DESedeKeySpec.isParityAdjusted(strongKey,
-1);
throw new Exception("expected ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aiobe) {}

try {
DESedeKeySpec desEdeKey = new DESedeKeySpec(strongKey,
Integer.MIN_VALUE);
throw new Exception("expected ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aiobe) {}
try {
boolean parityAdjusted = DESedeKeySpec.isParityAdjusted(strongKey,
Integer.MIN_VALUE);
throw new Exception("expected ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aiobe) {}
}

}