4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A/**
4632N/A * @test
4632N/A * @bug 6994008
4632N/A * @summary basic test for RSA/ECB/NoPadding cipher
4632N/A * @author Valerie Peng
4632N/A * @library ..
4632N/A */
4632N/A
4632N/Aimport javax.crypto.*;
4632N/Aimport java.io.*;
4632N/Aimport javax.crypto.spec.SecretKeySpec;
4632N/Aimport java.security.*;
4632N/Aimport java.util.*;
4632N/A
4632N/Apublic class TestRawRSACipher extends PKCS11Test {
4632N/A
4632N/A public void main(Provider p) throws Exception {
4632N/A try {
4632N/A Cipher.getInstance("RSA/ECB/NoPadding", p);
4632N/A } catch (GeneralSecurityException e) {
4632N/A System.out.println("Not supported by provider, skipping");
4632N/A return;
4632N/A }
4632N/A
4632N/A final int KEY_LEN = 1024;
4632N/A KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", p);
4632N/A kpGen.initialize(KEY_LEN);
4632N/A KeyPair kp = kpGen.generateKeyPair();
4632N/A Random random = new Random();
4632N/A byte[] plainText, cipherText, recoveredText;
4632N/A plainText = new byte[KEY_LEN/8];
4632N/A random.nextBytes(plainText);
4632N/A plainText[0] = 0; // to ensure that it's less than modulus
4632N/A
4632N/A Cipher c1 = Cipher.getInstance("RSA/ECB/NoPadding", p);
4632N/A Cipher c2 = Cipher.getInstance("RSA/ECB/NoPadding", "SunJCE");
4632N/A
4632N/A c1.init(Cipher.ENCRYPT_MODE, kp.getPublic());
4632N/A c2.init(Cipher.DECRYPT_MODE, kp.getPrivate());
4632N/A
4632N/A cipherText = c1.doFinal(plainText);
4632N/A recoveredText = c2.doFinal(cipherText);
4632N/A if (!Arrays.equals(plainText, recoveredText)) {
4632N/A throw new RuntimeException("E/D Test against SunJCE Failed!");
4632N/A }
4632N/A
4632N/A c2.init(Cipher.ENCRYPT_MODE, kp.getPublic());
4632N/A c1.init(Cipher.DECRYPT_MODE, kp.getPrivate());
4632N/A cipherText = c2.doFinal(plainText);
4632N/A recoveredText = c1.doFinal(cipherText);
4632N/A if (!Arrays.equals(plainText, recoveredText)) {
4632N/A throw new RuntimeException("D/E Test against SunJCE Failed!");
4632N/A }
4632N/A
4632N/A System.out.println("Test Passed");
4632N/A }
4632N/A
4632N/A public static void main(String[] args) throws Exception {
4632N/A main(new TestRawRSACipher());
4632N/A }
4632N/A}
4632N/A