Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Cipher/TestRSACipher.java
38855 views
/*1* Copyright (c) 2003, 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 4898468 699400826* @summary basic test for RSA cipher27* @author Andreas Sterbenz28* @library ..29* @key randomness30* @run main/othervm TestRSACipher31* @run main/othervm TestRSACipher sm32*/3334import java.security.GeneralSecurityException;35import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.PrivateKey;38import java.security.Provider;39import java.security.PublicKey;40import java.util.Arrays;41import java.util.Random;42import javax.crypto.BadPaddingException;43import javax.crypto.Cipher;44import javax.crypto.IllegalBlockSizeException;4546public class TestRSACipher 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("Not supported by provider, skipping");57return;58}59KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);60kpg.initialize(1024);61KeyPair kp = kpg.generateKeyPair();62PublicKey publicKey = kp.getPublic();63PrivateKey privateKey = kp.getPrivate();64Random random = new Random();65byte[] b, e, d;66b = new byte[16];67random.nextBytes(b);6869for (String rsaAlgo: RSA_ALGOS) {70Cipher c1 = Cipher.getInstance(rsaAlgo, p);71Cipher c2 = Cipher.getInstance(rsaAlgo, "SunJCE");7273c1.init(Cipher.ENCRYPT_MODE, publicKey);74e = c1.doFinal(b);75c1.init(Cipher.DECRYPT_MODE, privateKey);76d = c1.doFinal(e);77match(b, d);78c2.init(Cipher.DECRYPT_MODE, privateKey);79d = c2.doFinal(e);80match(b, d);8182// invalid data83c1.init(Cipher.DECRYPT_MODE, publicKey);84try {85d = c1.doFinal(e);86throw new Exception("completed call");87} catch (BadPaddingException ee) {88ee.printStackTrace();89}9091c1.init(Cipher.ENCRYPT_MODE, privateKey);92e = c1.doFinal(b);93c1.init(Cipher.DECRYPT_MODE, publicKey);94d = c1.doFinal(e);95match(b, d);96c2.init(Cipher.DECRYPT_MODE, publicKey);97d = c2.doFinal(e);98match(b, d);99100// reinit tests101c1.init(Cipher.ENCRYPT_MODE, privateKey);102c1.init(Cipher.ENCRYPT_MODE, privateKey);103e = c1.doFinal(b);104e = c1.doFinal(b);105c1.update(b);106c1.update(b);107c1.init(Cipher.ENCRYPT_MODE, privateKey);108e = c1.doFinal();109e = c1.doFinal();110c1.update(b);111e = c1.doFinal();112113c1.update(new byte[256]);114try {115e = c1.doFinal();116throw new Exception("completed call");117} catch (IllegalBlockSizeException ee) {118System.out.println(ee);119}120}121}122123private static void match(byte[] b1, byte[] b2) throws Exception {124if (Arrays.equals(b1, b2) == false) {125System.out.println(toString(b1));126System.out.println(toString(b2));127throw new Exception("mismatch");128}129}130131public static void main(String[] args) throws Exception {132main(new TestRSACipher(), args);133}134135}136137138