2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 4917233 6461727 6490213
|
|
27 |
* @summary test the KeyGenerator
|
|
28 |
* @author Andreas Sterbenz
|
|
29 |
* @library ..
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.util.*;
|
|
33 |
|
|
34 |
import java.security.*;
|
|
35 |
|
|
36 |
import javax.crypto.*;
|
|
37 |
|
|
38 |
enum TestResult {
|
|
39 |
PASS,
|
|
40 |
FAIL,
|
|
41 |
TBD
|
|
42 |
}
|
|
43 |
|
|
44 |
public class TestKeyGenerator extends PKCS11Test {
|
|
45 |
|
|
46 |
public static void main(String[] args) throws Exception {
|
|
47 |
main(new TestKeyGenerator());
|
|
48 |
}
|
|
49 |
|
|
50 |
private TestResult test(String algorithm, int keyLen, Provider p,
|
|
51 |
TestResult expected)
|
|
52 |
throws Exception {
|
|
53 |
TestResult actual = TestResult.TBD;
|
|
54 |
System.out.println("Testing " + algorithm + ", " + keyLen + " bits...");
|
|
55 |
KeyGenerator kg;
|
|
56 |
try {
|
|
57 |
kg = KeyGenerator.getInstance(algorithm, p);
|
|
58 |
} catch (NoSuchAlgorithmException e) {
|
|
59 |
System.out.println("Not supported, skipping: " + e);
|
|
60 |
return TestResult.PASS;
|
|
61 |
}
|
|
62 |
try {
|
|
63 |
kg.init(keyLen);
|
|
64 |
actual = TestResult.PASS;
|
|
65 |
} catch (InvalidParameterException ipe) {
|
|
66 |
actual = TestResult.FAIL;
|
|
67 |
}
|
|
68 |
if (actual == TestResult.PASS) {
|
|
69 |
try {
|
|
70 |
SecretKey key = kg.generateKey();
|
|
71 |
if (expected == TestResult.FAIL) {
|
|
72 |
throw new Exception("Generated " + key +
|
|
73 |
" using invalid key length");
|
|
74 |
}
|
|
75 |
} catch (ProviderException e) {
|
|
76 |
e.printStackTrace();
|
|
77 |
throw (Exception) (new Exception
|
|
78 |
("key generation failed using valid length").initCause(e));
|
|
79 |
}
|
|
80 |
}
|
|
81 |
if (expected != TestResult.TBD && expected != actual) {
|
|
82 |
throw new Exception("Expected to " + expected + ", but " +
|
|
83 |
actual);
|
|
84 |
}
|
|
85 |
return actual;
|
|
86 |
}
|
|
87 |
|
|
88 |
public void main(Provider p) throws Exception {
|
|
89 |
test("DES", 0, p, TestResult.FAIL);
|
|
90 |
test("DES", 56, p, TestResult.PASS); // ensure JCE-Compatibility
|
|
91 |
test("DES", 64, p, TestResult.PASS);
|
|
92 |
test("DES", 128, p, TestResult.FAIL);
|
|
93 |
|
|
94 |
test("DESede", 0, p, TestResult.FAIL);
|
|
95 |
// Special handling since not all PKCS11 providers support
|
|
96 |
// 2-key DESede, e.g. SunPKCS11-Solaris.
|
|
97 |
TestResult temp = test("DESede", 112, p, TestResult.TBD);
|
|
98 |
test("DESede", 128, p, temp);
|
|
99 |
test("DESede", 168, p, TestResult.PASS);
|
|
100 |
test("DESede", 192, p, TestResult.PASS);
|
|
101 |
test("DESede", 64, p, TestResult.FAIL);
|
|
102 |
test("DESede", 256, p, TestResult.FAIL);
|
|
103 |
|
|
104 |
// Different PKCS11 impls have different ranges
|
|
105 |
// of supported key sizes for variable-key-length
|
|
106 |
// algorithms.
|
|
107 |
// Solaris> Blowfish: 32-128 bits, RC4: 8-128 bits
|
|
108 |
// NSS> Blowfish: n/a, RC4: 8-2048 bits
|
|
109 |
// However, we explicitly disallowed key sizes less
|
|
110 |
// than 40-bits.
|
|
111 |
|
|
112 |
test("Blowfish", 0, p, TestResult.FAIL);
|
|
113 |
test("Blowfish", 24, p, TestResult.FAIL);
|
|
114 |
test("Blowfish", 32, p, TestResult.FAIL);
|
|
115 |
test("Blowfish", 40, p, TestResult.PASS);
|
|
116 |
test("Blowfish", 128, p, TestResult.PASS);
|
|
117 |
test("Blowfish", 136, p, TestResult.FAIL);
|
|
118 |
test("Blowfish", 448, p, TestResult.FAIL);
|
|
119 |
test("Blowfish", 456, p, TestResult.FAIL);
|
|
120 |
|
|
121 |
test("ARCFOUR", 0, p, TestResult.FAIL);
|
|
122 |
test("ARCFOUR", 32, p, TestResult.FAIL);
|
|
123 |
test("ARCFOUR", 40, p, TestResult.PASS);
|
|
124 |
test("ARCFOUR", 128, p, TestResult.PASS);
|
|
125 |
|
|
126 |
if (p.getName().equals("SunPKCS11-Solaris")) {
|
|
127 |
test("ARCFOUR", 1024, p, TestResult.FAIL);
|
|
128 |
} else if (p.getName().equals("SunPKCS11-NSS")) {
|
|
129 |
test("ARCFOUR", 1024, p, TestResult.PASS);
|
|
130 |
test("ARCFOUR", 2048, p, TestResult.PASS);
|
|
131 |
test("ARCFOUR", 2056, p, TestResult.FAIL);
|
|
132 |
}
|
|
133 |
}
|
|
134 |
}
|