Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/KeyGenerator/TestKeyGenerator.java
38854 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4917233 6461727 6490213 672045626* @summary test the KeyGenerator27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TestKeyGenerator30* @run main/othervm TestKeyGenerator sm31*/3233import java.security.InvalidParameterException;34import java.security.NoSuchAlgorithmException;35import java.security.Provider;36import java.security.ProviderException;37import javax.crypto.KeyGenerator;38import javax.crypto.SecretKey;3940enum TestResult {41PASS,42FAIL,43TBD44}4546public class TestKeyGenerator extends PKCS11Test {4748public static void main(String[] args) throws Exception {49main(new TestKeyGenerator(), args);50}5152private TestResult test(String algorithm, int keyLen, Provider p,53TestResult expected)54throws Exception {55TestResult actual = TestResult.TBD;56System.out.println("Testing " + algorithm + ", " + keyLen + " bits...");57KeyGenerator kg;58try {59kg = KeyGenerator.getInstance(algorithm, p);60} catch (NoSuchAlgorithmException e) {61System.out.println("Not supported, skipping: " + e);62return TestResult.PASS;63}64try {65kg.init(keyLen);66actual = TestResult.PASS;67} catch (InvalidParameterException ipe) {68actual = TestResult.FAIL;69}70if (actual == TestResult.PASS) {71try {72SecretKey key = kg.generateKey();73if (expected == TestResult.FAIL) {74throw new Exception("Generated " + key +75" using invalid key length");76}77} catch (ProviderException e) {78e.printStackTrace();79throw (Exception) (new Exception80("key generation failed using valid length").initCause(e));81}82}83if (expected != TestResult.TBD && expected != actual) {84throw new Exception("Expected to " + expected + ", but " +85actual);86}87return actual;88}8990@Override91public void main(Provider p) throws Exception {92test("DES", 0, p, TestResult.FAIL);93test("DES", 56, p, TestResult.PASS); // ensure JCE-Compatibility94test("DES", 64, p, TestResult.PASS);95test("DES", 128, p, TestResult.FAIL);9697test("DESede", 0, p, TestResult.FAIL);98// Special handling since not all PKCS11 providers support99// 2-key DESede, e.g. SunPKCS11-Solaris.100TestResult temp = test("DESede", 112, p, TestResult.TBD);101test("DESede", 128, p, temp);102test("DESede", 168, p, TestResult.PASS);103test("DESede", 192, p, TestResult.PASS);104test("DESede", 64, p, TestResult.FAIL);105test("DESede", 256, p, TestResult.FAIL);106107// Different PKCS11 impls have different ranges108// of supported key sizes for variable-key-length109// algorithms.110// Solaris> Blowfish: 32-128 or even 448 bits, RC4: 8-128 bits or as much as 2048 bits111// NSS> Blowfish: n/a, RC4: 8-2048 bits112// However, we explicitly disallowed key sizes less113// than 40-bits.114115test("Blowfish", 0, p, TestResult.FAIL);116test("Blowfish", 24, p, TestResult.FAIL);117test("Blowfish", 32, p, TestResult.FAIL);118test("Blowfish", 40, p, TestResult.PASS);119test("Blowfish", 128, p, TestResult.PASS);120test("Blowfish", 136, p, TestResult.TBD);121test("Blowfish", 448, p, TestResult.TBD);122test("Blowfish", 456, p, TestResult.FAIL);123124test("ARCFOUR", 0, p, TestResult.FAIL);125test("ARCFOUR", 32, p, TestResult.FAIL);126test("ARCFOUR", 40, p, TestResult.PASS);127test("ARCFOUR", 128, p, TestResult.PASS);128129if (p.getName().equals("SunPKCS11-Solaris")) {130test("ARCFOUR", 1024, p, TestResult.TBD);131} else if (p.getName().equals("SunPKCS11-NSS")) {132test("ARCFOUR", 1024, p, TestResult.PASS);133test("ARCFOUR", 2048, p, TestResult.PASS);134test("ARCFOUR", 2056, p, TestResult.FAIL);135}136}137}138139140