Path: blob/master/test/jdk/sun/security/pkcs11/SecretKeyFactory/TestGeneral.java
41152 views
/*1* Copyright (c) 2021, 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 825541026* @summary test the general SecretKeyFactory functionality27* @library /test/lib ..28* @modules jdk.crypto.cryptoki29* @run main/othervm TestGeneral30*/3132import java.security.NoSuchAlgorithmException;33import java.security.Provider;34import java.security.SecureRandom;35import java.util.Arrays;36import javax.crypto.SecretKeyFactory;37import javax.crypto.SecretKey;38import javax.crypto.spec.SecretKeySpec;3940public class TestGeneral extends PKCS11Test {4142private enum TestResult {43PASS,44FAIL,45TBD // unused for now46}4748public static void main(String[] args) throws Exception {49main(new TestGeneral(), args);50}5152private void test(String algorithm, SecretKey key, Provider p,53TestResult expected) throws RuntimeException {54System.out.println("Testing " + algorithm + " SKF from " + p.getName());55SecretKeyFactory skf;56try {57skf = SecretKeyFactory.getInstance(algorithm, p);58} catch (NoSuchAlgorithmException e) {59System.out.println("Not supported, skipping: " + e);60return;61}62try {63SecretKey key2 = skf.translateKey(key);64if (expected == TestResult.FAIL) {65throw new RuntimeException("translateKey() should FAIL");66}67System.out.println("=> translated key");68if (!key2.getAlgorithm().equalsIgnoreCase(algorithm)) {69throw new RuntimeException("translated key algorithm mismatch");70}71System.out.println("=> checked key algorithm");7273// proceed to check encodings if format match74if (key2.getFormat().equalsIgnoreCase(key.getFormat())) {75if (key2.getEncoded() != null &&76!Arrays.equals(key.getEncoded(), key2.getEncoded())) {77throw new RuntimeException(78"translated key encoding mismatch");79}80System.out.println("=> checked key format and encoding");81}82} catch (Exception e) {83if (expected == TestResult.PASS) {84e.printStackTrace();85throw new RuntimeException("translateKey() should pass", e);86}87}88}8990@Override91public void main(Provider p) throws Exception {9293byte[] rawBytes = new byte[32];94new SecureRandom().nextBytes(rawBytes);9596SecretKey aes_128Key = new SecretKeySpec(rawBytes, 0, 16, "AES");97SecretKey aes_256Key = new SecretKeySpec(rawBytes, 0, 32, "AES");98SecretKey bf_128Key = new SecretKeySpec(rawBytes, 0, 16, "Blowfish");99SecretKey cc20Key = new SecretKeySpec(rawBytes, 0, 32, "ChaCha20");100101// fixed key length102test("AES", aes_128Key, p, TestResult.PASS);103test("AES", aes_256Key, p, TestResult.PASS);104test("AES", cc20Key, p, TestResult.FAIL);105106test("ChaCha20", aes_128Key, p, TestResult.FAIL);107test("ChaCha20", aes_256Key, p, TestResult.FAIL);108test("ChaCha20", cc20Key, p, TestResult.PASS);109110// variable key length111// Different PKCS11 impls may have different ranges112// of supported key sizes for variable-key-length113// algorithms.114test("Blowfish", aes_128Key, p, TestResult.FAIL);115test("Blowfish", cc20Key, p, TestResult.FAIL);116test("Blowfish", bf_128Key, p, TestResult.PASS);117}118}119120121