0N/A/*
3261N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
3120N/A * @bug 4917233 6461727 6490213 6720456
0N/A * @summary test the KeyGenerator
0N/A * @author Andreas Sterbenz
0N/A * @library ..
0N/A */
0N/A
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/A
0N/Aimport javax.crypto.*;
0N/A
0N/Aenum TestResult {
0N/A PASS,
0N/A FAIL,
0N/A TBD
0N/A}
0N/A
0N/Apublic class TestKeyGenerator extends PKCS11Test {
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A main(new TestKeyGenerator());
0N/A }
0N/A
0N/A private TestResult test(String algorithm, int keyLen, Provider p,
0N/A TestResult expected)
0N/A throws Exception {
0N/A TestResult actual = TestResult.TBD;
0N/A System.out.println("Testing " + algorithm + ", " + keyLen + " bits...");
0N/A KeyGenerator kg;
0N/A try {
0N/A kg = KeyGenerator.getInstance(algorithm, p);
0N/A } catch (NoSuchAlgorithmException e) {
0N/A System.out.println("Not supported, skipping: " + e);
0N/A return TestResult.PASS;
0N/A }
0N/A try {
0N/A kg.init(keyLen);
0N/A actual = TestResult.PASS;
0N/A } catch (InvalidParameterException ipe) {
0N/A actual = TestResult.FAIL;
0N/A }
0N/A if (actual == TestResult.PASS) {
0N/A try {
0N/A SecretKey key = kg.generateKey();
0N/A if (expected == TestResult.FAIL) {
0N/A throw new Exception("Generated " + key +
0N/A " using invalid key length");
0N/A }
0N/A } catch (ProviderException e) {
0N/A e.printStackTrace();
0N/A throw (Exception) (new Exception
0N/A ("key generation failed using valid length").initCause(e));
0N/A }
0N/A }
0N/A if (expected != TestResult.TBD && expected != actual) {
0N/A throw new Exception("Expected to " + expected + ", but " +
0N/A actual);
0N/A }
0N/A return actual;
0N/A }
0N/A
0N/A public void main(Provider p) throws Exception {
0N/A test("DES", 0, p, TestResult.FAIL);
0N/A test("DES", 56, p, TestResult.PASS); // ensure JCE-Compatibility
0N/A test("DES", 64, p, TestResult.PASS);
0N/A test("DES", 128, p, TestResult.FAIL);
0N/A
0N/A test("DESede", 0, p, TestResult.FAIL);
0N/A // Special handling since not all PKCS11 providers support
0N/A // 2-key DESede, e.g. SunPKCS11-Solaris.
0N/A TestResult temp = test("DESede", 112, p, TestResult.TBD);
0N/A test("DESede", 128, p, temp);
0N/A test("DESede", 168, p, TestResult.PASS);
0N/A test("DESede", 192, p, TestResult.PASS);
0N/A test("DESede", 64, p, TestResult.FAIL);
0N/A test("DESede", 256, p, TestResult.FAIL);
0N/A
0N/A // Different PKCS11 impls have different ranges
0N/A // of supported key sizes for variable-key-length
0N/A // algorithms.
3120N/A // Solaris> Blowfish: 32-128 or even 448 bits, RC4: 8-128 bits or as much as 2048 bits
0N/A // NSS> Blowfish: n/a, RC4: 8-2048 bits
0N/A // However, we explicitly disallowed key sizes less
0N/A // than 40-bits.
0N/A
0N/A test("Blowfish", 0, p, TestResult.FAIL);
0N/A test("Blowfish", 24, p, TestResult.FAIL);
0N/A test("Blowfish", 32, p, TestResult.FAIL);
0N/A test("Blowfish", 40, p, TestResult.PASS);
0N/A test("Blowfish", 128, p, TestResult.PASS);
3120N/A test("Blowfish", 136, p, TestResult.TBD);
3120N/A test("Blowfish", 448, p, TestResult.TBD);
0N/A test("Blowfish", 456, p, TestResult.FAIL);
0N/A
0N/A test("ARCFOUR", 0, p, TestResult.FAIL);
0N/A test("ARCFOUR", 32, p, TestResult.FAIL);
0N/A test("ARCFOUR", 40, p, TestResult.PASS);
0N/A test("ARCFOUR", 128, p, TestResult.PASS);
0N/A
0N/A if (p.getName().equals("SunPKCS11-Solaris")) {
3120N/A test("ARCFOUR", 1024, p, TestResult.TBD);
0N/A } else if (p.getName().equals("SunPKCS11-NSS")) {
0N/A test("ARCFOUR", 1024, p, TestResult.PASS);
0N/A test("ARCFOUR", 2048, p, TestResult.PASS);
0N/A test("ARCFOUR", 2056, p, TestResult.FAIL);
0N/A }
0N/A }
0N/A}