Path: blob/master/test/jdk/sun/security/pkcs11/KeyGenerator/TestKeyGenerator.java
41154 views
/*1* Copyright (c) 2003, 2020, 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 6720456 824233226* @summary test the KeyGenerator27* @author Andreas Sterbenz28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm TestKeyGenerator31* @run main/othervm -Djava.security.manager=allow TestKeyGenerator sm32*/3334import java.security.InvalidParameterException;35import java.security.NoSuchAlgorithmException;36import java.security.Provider;37import java.security.ProviderException;38import javax.crypto.KeyGenerator;39import javax.crypto.SecretKey;4041enum TestResult {42PASS,43FAIL,44TBD45}4647public class TestKeyGenerator extends PKCS11Test {4849public static void main(String[] args) throws Exception {50main(new TestKeyGenerator(), args);51}5253private TestResult test(String algorithm, int keyLen, Provider p,54TestResult expected)55throws Exception {56TestResult actual = TestResult.TBD;57System.out.println("Testing " + algorithm + ", " + keyLen + " bits...");58KeyGenerator kg;59try {60kg = KeyGenerator.getInstance(algorithm, p);61} catch (NoSuchAlgorithmException e) {62System.out.println("Not supported, skipping: " + e);63return TestResult.PASS;64}65try {66kg.init(keyLen);67actual = TestResult.PASS;68} catch (InvalidParameterException ipe) {69actual = TestResult.FAIL;70}71if (actual == TestResult.PASS) {72try {73SecretKey key = kg.generateKey();74if (expected == TestResult.FAIL) {75throw new Exception("Generated " + key +76" using invalid key length");77}78} catch (ProviderException e) {79e.printStackTrace();80throw (Exception) (new Exception81("key generation failed using valid length").initCause(e));82}83}84if (expected != TestResult.TBD && expected != actual) {85throw new Exception("Expected to " + expected + ", but " +86actual);87}88return actual;89}9091@Override92public void main(Provider p) throws Exception {93test("DES", 0, p, TestResult.FAIL);94test("DES", 56, p, TestResult.PASS); // ensure JCE-Compatibility95test("DES", 64, p, TestResult.PASS);96test("DES", 128, p, TestResult.FAIL);9798test("DESede", 0, p, TestResult.FAIL);99// Special handling since not all PKCS11 providers support100// 2-key DESede, e.g. SunPKCS11-Solaris.101TestResult temp = test("DESede", 112, p, TestResult.TBD);102test("DESede", 128, p, temp);103test("DESede", 168, p, TestResult.PASS);104test("DESede", 192, p, TestResult.PASS);105test("DESede", 64, p, TestResult.FAIL);106test("DESede", 256, p, TestResult.FAIL);107108// Different PKCS11 impls have different ranges109// of supported key sizes for variable-key-length110// algorithms.111// 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);128129String[] HMAC_ALGS = {130"HmacSHA1", "HmacSHA224", "HmacSHA256", "HmacSHA384", "HmacSHA512",131"HmacSHA512/224", "HmacSHA512/256", "HmacSHA3-224", "HmacSHA3-256",132"HmacSHA3-384", "HmacSHA3-512",133};134135for (String hmacAlg : HMAC_ALGS) {136test(hmacAlg, 0, p, TestResult.FAIL);137test(hmacAlg, 128, p, TestResult.PASS);138test(hmacAlg, 224, p, TestResult.PASS);139}140141if (p.getName().equals("SunPKCS11-NSS")) {142test("ARCFOUR", 1024, p, TestResult.PASS);143test("ARCFOUR", 2048, p, TestResult.PASS);144test("ARCFOUR", 2056, p, TestResult.FAIL);145}146}147}148149150