Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/rsa/KeyWrap.java
38855 views
/*1* Copyright (c) 2005, 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 623121626* @summary Verify key wrapping (of extractable keys) works for RSA/PKCS127* @author Andreas Sterbenz28* @library ..29* @key randomness30* @run main/othervm KeyWrap31* @run main/othervm KeyWrap sm32*/3334import java.security.GeneralSecurityException;35import java.security.InvalidKeyException;36import java.security.Key;37import java.security.KeyFactory;38import java.security.KeyPair;39import java.security.KeyPairGenerator;40import java.security.NoSuchAlgorithmException;41import java.security.PrivateKey;42import java.security.Provider;43import java.security.PublicKey;44import java.util.Random;45import javax.crypto.Cipher;46import javax.crypto.SecretKey;47import javax.crypto.spec.SecretKeySpec;4849public class KeyWrap extends PKCS11Test {5051@Override52public void main(Provider p) throws Exception {53try {54Cipher.getInstance("RSA/ECB/PKCS1Padding", p);55} catch (GeneralSecurityException e) {56System.out.println("Not supported by provider, skipping");57return;58}59KeyPair kp;60try {61KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);62kpg.initialize(512);63kp = kpg.generateKeyPair();64} catch (Exception e) {65try {66System.out.println("Could not generate KeyPair on provider " + p + ", trying migration");67KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");68kpg.initialize(512);69kp = kpg.generateKeyPair();70KeyFactory kf = KeyFactory.getInstance("RSA", p);71PublicKey pub = (PublicKey)kf.translateKey(kp.getPublic());72PrivateKey priv = (PrivateKey)kf.translateKey(kp.getPrivate());73kp = new KeyPair(pub, priv);74} catch (NoSuchAlgorithmException | InvalidKeyException ee) {75ee.printStackTrace();76System.out.println("Provider does not support RSA, skipping");77return;78}79}80System.out.println(kp);81Random r = new Random();82byte[] b = new byte[16];83r.nextBytes(b);84String alg = "AES";85SecretKey key = new SecretKeySpec(b, alg);8687Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding", p);88// Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");89c.init(Cipher.WRAP_MODE, kp.getPublic());90byte[] wrapped = c.wrap(key);91System.out.println("wrapped: " + wrapped.length);9293c.init(Cipher.UNWRAP_MODE, kp.getPrivate());94Key unwrapped = c.unwrap(wrapped, alg, Cipher.SECRET_KEY);95System.out.println("unwrapped: " + unwrapped);9697boolean eq = key.equals(unwrapped);98System.out.println(eq);99if (eq == false) {100throw new Exception("Unwrapped key does not match original key");101}102}103104public static void main(String[] args) throws Exception {105main(new KeyWrap(), args);106}107108}109110111