Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Cipher/TestRawRSACipher.java
38855 views
/*1* Copyright (c) 2011, 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 699400826* @summary basic test for RSA/ECB/NoPadding cipher27* @author Valerie Peng28* @library ..29* @key randomness30* @run main/othervm TestRawRSACipher31* @run main/othervm TestRawRSACipher sm32*/3334import java.security.GeneralSecurityException;35import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.Provider;38import java.util.Arrays;39import java.util.Random;40import javax.crypto.Cipher;4142public class TestRawRSACipher extends PKCS11Test {4344@Override45public void main(Provider p) throws Exception {46try {47Cipher.getInstance("RSA/ECB/NoPadding", p);48} catch (GeneralSecurityException e) {49System.out.println("Not supported by provider, skipping");50return;51}5253final int KEY_LEN = 1024;54KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", p);55kpGen.initialize(KEY_LEN);56KeyPair kp = kpGen.generateKeyPair();57Random random = new Random();58byte[] plainText, cipherText, recoveredText;59plainText = new byte[KEY_LEN/8];60random.nextBytes(plainText);61plainText[0] = 0; // to ensure that it's less than modulus6263Cipher c1 = Cipher.getInstance("RSA/ECB/NoPadding", p);64Cipher c2 = Cipher.getInstance("RSA/ECB/NoPadding", "SunJCE");6566c1.init(Cipher.ENCRYPT_MODE, kp.getPublic());67c2.init(Cipher.DECRYPT_MODE, kp.getPrivate());6869cipherText = c1.doFinal(plainText);70recoveredText = c2.doFinal(cipherText);71if (!Arrays.equals(plainText, recoveredText)) {72throw new RuntimeException("E/D Test against SunJCE Failed!");73}7475c2.init(Cipher.ENCRYPT_MODE, kp.getPublic());76c1.init(Cipher.DECRYPT_MODE, kp.getPrivate());77cipherText = c2.doFinal(plainText);78recoveredText = c1.doFinal(cipherText);79if (!Arrays.equals(plainText, recoveredText)) {80throw new RuntimeException("D/E Test against SunJCE Failed!");81}8283System.out.println("Test Passed");84}8586public static void main(String[] args) throws Exception {87main(new TestRawRSACipher(), args);88}89}909192