Skip to content

Commit 0bcf176

Browse files
driverktBradford Wetmore
authored andcommitted
6227536: KeyGenerator.init() methods do not throw IllegalArgumentException for keysize == 0
Reviewed-by: wetmore
1 parent cc2861a commit 0bcf176

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

src/java.base/share/classes/com/sun/crypto/provider/HmacMD5KeyGenerator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -88,6 +88,11 @@ protected void engineInit(AlgorithmParameterSpec params,
8888
* @param random the source of randomness for this key generator
8989
*/
9090
protected void engineInit(int keysize, SecureRandom random) {
91+
92+
if (keysize <= 0) {
93+
throw new IllegalArgumentException("keysize must not be <= 0");
94+
}
95+
9196
this.keysize = (keysize+7) / 8;
9297
this.engineInit(random);
9398
}

src/java.base/share/classes/com/sun/crypto/provider/HmacSHA1KeyGenerator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -88,6 +88,11 @@ protected void engineInit(AlgorithmParameterSpec params,
8888
* @param random the source of randomness for this key generator
8989
*/
9090
protected void engineInit(int keysize, SecureRandom random) {
91+
92+
if (keysize <= 0) {
93+
throw new IllegalArgumentException("keysize must not be <= 0");
94+
}
95+
9196
this.keysize = (keysize+7) / 8;
9297
this.engineInit(random);
9398
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @library /test/lib
27+
* @bug 6227536
28+
* @summary Verify HmacSHA1 and HmacMD5 KeyGenerators throw an Exception when
29+
* a keysize of zero is requested
30+
*/
31+
32+
import jdk.test.lib.Utils;
33+
34+
import javax.crypto.KeyGenerator;
35+
36+
public class Test6227536 {
37+
38+
String[] keyGensToTest = new String[]{"HmacSHA1", "HmacMD5"};
39+
40+
public boolean execute(String algo) throws Exception {
41+
KeyGenerator kg = KeyGenerator.getInstance(algo, "SunJCE");
42+
43+
Utils.runAndCheckException(() -> kg.init(0),
44+
IllegalArgumentException.class);
45+
46+
return true;
47+
}
48+
49+
public static void main(String[] args) throws Exception {
50+
Test6227536 test = new Test6227536();
51+
String testName = test.getClass().getName();
52+
53+
for (String keyGenToTest : test.keyGensToTest) {
54+
55+
if (test.execute(keyGenToTest)) {
56+
57+
System.out.println(testName + ": " + keyGenToTest + " Passed!");
58+
59+
}
60+
61+
}
62+
63+
}
64+
}

0 commit comments

Comments
 (0)