Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Cipher/TestRSACipherWrap.java
38855 views
/*1* Copyright (c) 2008, 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 6572331 699400826* @summary basic test for RSA cipher key wrapping functionality27* @author Valerie Peng28* @library ..29* @run main/othervm TestRSACipherWrap30* @run main/othervm TestRSACipherWrap sm31*/3233import java.security.GeneralSecurityException;34import java.security.InvalidParameterException;35import java.security.Key;36import java.security.KeyPair;37import java.security.KeyPairGenerator;38import java.security.Provider;39import java.util.Arrays;40import javax.crypto.Cipher;41import javax.crypto.KeyGenerator;42import javax.crypto.SecretKey;43import javax.crypto.spec.SecretKeySpec;4445public class TestRSACipherWrap extends PKCS11Test {4647private static final String[] RSA_ALGOS =48{ "RSA/ECB/PKCS1Padding", "RSA" };4950@Override51public void main(Provider p) throws Exception {52try {53Cipher.getInstance(RSA_ALGOS[0], p);54} catch (GeneralSecurityException e) {55System.out.println(RSA_ALGOS[0] + " unsupported, skipping");56return;57}58KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);59kpg.initialize(1024);60KeyPair kp = kpg.generateKeyPair();6162for (String rsaAlgo: RSA_ALGOS) {63Cipher cipherPKCS11 = Cipher.getInstance(rsaAlgo, p);64Cipher cipherJce = Cipher.getInstance(rsaAlgo, "SunJCE");6566String algos[] = {"AES", "RC2", "Blowfish"};67int keySizes[] = {128, 256};6869for (int j = 0; j < algos.length; j++) {70String algorithm = algos[j];71KeyGenerator keygen =72KeyGenerator.getInstance(algorithm);7374for (int i = 0; i < keySizes.length; i++) {75SecretKey secretKey = null;76System.out.print("Generate " + keySizes[i] + "-bit " +77algorithm + " key using ");78try {79keygen.init(keySizes[i]);80secretKey = keygen.generateKey();81System.out.println(keygen.getProvider().getName());82} catch (InvalidParameterException ipe) {83secretKey = new SecretKeySpec(new byte[32], algorithm);84System.out.println("SecretKeySpec class");85}86test(kp, secretKey, cipherPKCS11, cipherJce);87test(kp, secretKey, cipherPKCS11, cipherPKCS11);88test(kp, secretKey, cipherJce, cipherPKCS11);89}90}91}92}9394private static void test(KeyPair kp, SecretKey secretKey,95Cipher wrapCipher, Cipher unwrapCipher)96throws Exception {97String algo = secretKey.getAlgorithm();98wrapCipher.init(Cipher.WRAP_MODE, kp.getPublic());99byte[] wrappedKey = wrapCipher.wrap(secretKey);100unwrapCipher.init(Cipher.UNWRAP_MODE, kp.getPrivate());101Key unwrappedKey =102unwrapCipher.unwrap(wrappedKey, algo, Cipher.SECRET_KEY);103104System.out.println("Test " + wrapCipher.getProvider().getName() +105"/" + unwrapCipher.getProvider().getName() + ": ");106if (!Arrays.equals(secretKey.getEncoded(),107unwrappedKey.getEncoded())) {108throw new Exception("Test Failed!");109}110System.out.println("Passed");111}112113public static void main(String[] args) throws Exception {114main(new TestRSACipherWrap(), args);115}116}117118119