Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestRSACipher.java
41152 views
/*1* Copyright (c) 2003, 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 4898468 699400826* @summary basic test for RSA cipher27* @author Andreas Sterbenz28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm TestRSACipher32* @run main/othervm -Djava.security.manager=allow TestRSACipher sm33*/3435import java.security.GeneralSecurityException;36import java.security.KeyPair;37import java.security.KeyPairGenerator;38import java.security.PrivateKey;39import java.security.Provider;40import java.security.PublicKey;41import java.util.Arrays;42import java.util.Random;43import javax.crypto.BadPaddingException;44import javax.crypto.Cipher;45import javax.crypto.IllegalBlockSizeException;4647public class TestRSACipher extends PKCS11Test {4849private static final String[] RSA_ALGOS =50{ "RSA/ECB/PKCS1Padding", "RSA" };5152@Override53public void main(Provider p) throws Exception {54try {55Cipher.getInstance(RSA_ALGOS[0], p);56} catch (GeneralSecurityException e) {57System.out.println("Not supported by provider, skipping");58return;59}60KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);61kpg.initialize(1024);62KeyPair kp = kpg.generateKeyPair();63PublicKey publicKey = kp.getPublic();64PrivateKey privateKey = kp.getPrivate();65Random random = new Random();66byte[] b, e, d;67b = new byte[16];68random.nextBytes(b);6970for (String rsaAlgo: RSA_ALGOS) {71Cipher c1 = Cipher.getInstance(rsaAlgo, p);72Cipher c2 = Cipher.getInstance(rsaAlgo, "SunJCE");7374c1.init(Cipher.ENCRYPT_MODE, publicKey);75e = c1.doFinal(b);76c1.init(Cipher.DECRYPT_MODE, privateKey);77d = c1.doFinal(e);78match(b, d);79c2.init(Cipher.DECRYPT_MODE, privateKey);80d = c2.doFinal(e);81match(b, d);8283// invalid data84c1.init(Cipher.DECRYPT_MODE, publicKey);85try {86d = c1.doFinal(e);87throw new Exception("completed call");88} catch (BadPaddingException ee) {89ee.printStackTrace();90}9192c1.init(Cipher.ENCRYPT_MODE, privateKey);93e = c1.doFinal(b);94c1.init(Cipher.DECRYPT_MODE, publicKey);95d = c1.doFinal(e);96match(b, d);97c2.init(Cipher.DECRYPT_MODE, publicKey);98d = c2.doFinal(e);99match(b, d);100101// reinit tests102c1.init(Cipher.ENCRYPT_MODE, privateKey);103c1.init(Cipher.ENCRYPT_MODE, privateKey);104e = c1.doFinal(b);105e = c1.doFinal(b);106c1.update(b);107c1.update(b);108c1.init(Cipher.ENCRYPT_MODE, privateKey);109e = c1.doFinal();110e = c1.doFinal();111c1.update(b);112e = c1.doFinal();113114c1.update(new byte[256]);115try {116e = c1.doFinal();117throw new Exception("completed call");118} catch (IllegalBlockSizeException ee) {119System.out.println(ee);120}121}122}123124private static void match(byte[] b1, byte[] b2) throws Exception {125if (Arrays.equals(b1, b2) == false) {126System.out.println(toString(b1));127System.out.println(toString(b2));128throw new Exception("mismatch");129}130}131132public static void main(String[] args) throws Exception {133main(new TestRSACipher(), args);134}135136}137138139