Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestRSACipherWrap.java
41152 views
/*1* Copyright (c) 2008, 2018, 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 6572331 699400826* @summary basic test for RSA cipher key wrapping functionality27* @author Valerie Peng28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm TestRSACipherWrap31* @run main/othervm -Djava.security.manager=allow TestRSACipherWrap sm32*/3334import java.security.GeneralSecurityException;35import java.security.InvalidParameterException;36import java.security.Key;37import java.security.KeyPair;38import java.security.KeyPairGenerator;39import java.security.Provider;40import java.util.Arrays;41import javax.crypto.Cipher;42import javax.crypto.KeyGenerator;43import javax.crypto.SecretKey;44import javax.crypto.spec.SecretKeySpec;4546public class TestRSACipherWrap extends PKCS11Test {4748private static final String[] RSA_ALGOS =49{ "RSA/ECB/PKCS1Padding", "RSA" };5051@Override52public void main(Provider p) throws Exception {53try {54Cipher.getInstance(RSA_ALGOS[0], p);55} catch (GeneralSecurityException e) {56System.out.println(RSA_ALGOS[0] + " unsupported, skipping");57return;58}59KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);60kpg.initialize(1024);61KeyPair kp = kpg.generateKeyPair();6263for (String rsaAlgo: RSA_ALGOS) {64Cipher cipherPKCS11 = Cipher.getInstance(rsaAlgo, p);65Cipher cipherJce = Cipher.getInstance(rsaAlgo, "SunJCE");6667String algos[] = {"AES", "RC2", "Blowfish"};68int keySizes[] = {128, 256};6970for (int j = 0; j < algos.length; j++) {71String algorithm = algos[j];72KeyGenerator keygen =73KeyGenerator.getInstance(algorithm);7475for (int i = 0; i < keySizes.length; i++) {76SecretKey secretKey = null;77System.out.print("Generate " + keySizes[i] + "-bit " +78algorithm + " key using ");79try {80keygen.init(keySizes[i]);81secretKey = keygen.generateKey();82System.out.println(keygen.getProvider().getName());83} catch (InvalidParameterException ipe) {84secretKey = new SecretKeySpec(new byte[32], algorithm);85System.out.println("SecretKeySpec class");86}87test(kp, secretKey, cipherPKCS11, cipherJce);88test(kp, secretKey, cipherPKCS11, cipherPKCS11);89test(kp, secretKey, cipherJce, cipherPKCS11);90}91}92}93}9495private static void test(KeyPair kp, SecretKey secretKey,96Cipher wrapCipher, Cipher unwrapCipher)97throws Exception {98String algo = secretKey.getAlgorithm();99wrapCipher.init(Cipher.WRAP_MODE, kp.getPublic());100byte[] wrappedKey = wrapCipher.wrap(secretKey);101unwrapCipher.init(Cipher.UNWRAP_MODE, kp.getPrivate());102Key unwrappedKey =103unwrapCipher.unwrap(wrappedKey, algo, Cipher.SECRET_KEY);104105System.out.println("Test " + wrapCipher.getProvider().getName() +106"/" + unwrapCipher.getProvider().getName() + ": ");107if (!Arrays.equals(secretKey.getEncoded(),108unwrappedKey.getEncoded())) {109throw new Exception("Test Failed!");110}111System.out.println("Passed");112}113114public static void main(String[] args) throws Exception {115main(new TestRSACipherWrap(), args);116}117}118119120