0N/A/*
2362N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4923484
0N/A * @summary encryption/decryption test for using OAEPParameterSpec.
0N/A * @author Valerie Peng
0N/A */
0N/A
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.spec.MGF1ParameterSpec;
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.PSource;
0N/Aimport javax.crypto.spec.OAEPParameterSpec;
0N/A
0N/Apublic class TestOAEPWithParams {
0N/A
0N/A private static Provider cp;
0N/A
0N/A private static PrivateKey privateKey;
0N/A
0N/A private static PublicKey publicKey;
0N/A
0N/A private static Random random = new Random();
0N/A
0N/A private static String MD[] = {
0N/A "MD5", "SHA1", "SHA-256"
0N/A };
0N/A private static int DATA_LENGTH[] = {
0N/A 62, 54, 30
0N/A };
0N/A public static void main(String[] args) throws Exception {
0N/A long start = System.currentTimeMillis();
0N/A cp = Security.getProvider("SunJCE");
0N/A System.out.println("Testing provider " + cp.getName() + "...");
0N/A Provider kfp = Security.getProvider("SunRsaSign");
0N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp);
0N/A kpg.initialize(768);
0N/A KeyPair kp = kpg.generateKeyPair();
0N/A privateKey = kp.getPrivate();
0N/A publicKey = kp.getPublic();
0N/A
0N/A for (int i = 0; i < MD.length; i++) {
0N/A // basic test using MD5
0N/A testEncryptDecrypt(MD[i], DATA_LENGTH[i]);
0N/A }
0N/A
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("Done (" + (stop - start) + " ms).");
0N/A }
0N/A
0N/A private static void testEncryptDecrypt(String hashAlg, int dataLength)
0N/A throws Exception {
0N/A System.out.println("Testing OAEP with hash " + hashAlg + ", " + dataLength + " bytes");
0N/A Cipher c = Cipher.getInstance("RSA/ECB/OAEPwith" + hashAlg +
0N/A "andMGF1Padding", cp);
0N/A byte[] pSrc1 = { (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
0N/A (byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x02
0N/A };
0N/A byte[] pSrc2 = { (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
0N/A (byte) 0x02, (byte) 0x02, (byte) 0x03, (byte) 0x04
0N/A };
0N/A OAEPParameterSpec spec1 = new OAEPParameterSpec(hashAlg,
0N/A "MGF1", MGF1ParameterSpec.SHA1, new PSource.PSpecified(pSrc1));
0N/A OAEPParameterSpec spec2 = new OAEPParameterSpec(hashAlg,
0N/A "MGF1", MGF1ParameterSpec.SHA1, new PSource.PSpecified(pSrc2));
0N/A byte[] plainText = new byte[dataLength];
0N/A byte[] cipherText, recovered;
0N/A // do encryption with parameters#1
0N/A System.out.println("Testing with user-supplied parameters...");
0N/A c.init(Cipher.ENCRYPT_MODE, publicKey, spec1);
0N/A cipherText = c.doFinal(plainText);
0N/A
0N/A // test#1: decrypt with parameters#1
0N/A c.init(Cipher.DECRYPT_MODE, privateKey, spec1);
0N/A recovered = c.doFinal(cipherText);
0N/A if (Arrays.equals(plainText, recovered) == false) {
0N/A throw new Exception("Decrypted data does not match");
0N/A }
0N/A
0N/A // test#2: decrypt without parameters
0N/A c.init(Cipher.DECRYPT_MODE, privateKey);
0N/A try {
0N/A recovered = c.doFinal(cipherText);
0N/A throw new Exception("Should throw BadPaddingException");
0N/A } catch (BadPaddingException bpe) {
0N/A // expected
0N/A }
0N/A // test#3: decrypt with different parameters
0N/A c.init(Cipher.DECRYPT_MODE, privateKey, spec2);
0N/A try {
0N/A recovered = c.doFinal(cipherText);
0N/A throw new Exception("Should throw BadPaddingException");
0N/A } catch (BadPaddingException bpe) {
0N/A // expected
0N/A }
0N/A // do encryption without parameters
0N/A System.out.println("Testing with cipher default parameters...");
0N/A c.init(Cipher.ENCRYPT_MODE, publicKey);
0N/A cipherText = c.doFinal(plainText);
0N/A
0N/A // test#1: decrypt with parameters got from cipher
0N/A AlgorithmParameters params = c.getParameters();
0N/A c.init(Cipher.DECRYPT_MODE, privateKey, params);
0N/A recovered = c.doFinal(cipherText);
0N/A if (Arrays.equals(plainText, recovered) == false) {
0N/A throw new Exception("Decrypted data does not match");
0N/A }
0N/A
0N/A // test#2: decrypt without parameters
0N/A c.init(Cipher.DECRYPT_MODE, privateKey);
0N/A recovered = c.doFinal(cipherText);
0N/A if (Arrays.equals(plainText, recovered) == false) {
0N/A throw new Exception("Decrypted data does not match");
0N/A }
0N/A
0N/A // test#3: decrypt with different parameters
0N/A c.init(Cipher.DECRYPT_MODE, privateKey, spec2);
0N/A try {
0N/A recovered = c.doFinal(cipherText);
0N/A throw new Exception("Should throw BadPaddingException");
0N/A } catch (BadPaddingException bpe) {
0N/A // expected
0N/A }
0N/A }
0N/A}