Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEPWithParams.java
41161 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 4923484 814629326* @summary encryption/decryption test for using OAEPParameterSpec.27* @author Valerie Peng28*/2930import java.util.*;3132import java.security.*;33import java.security.spec.MGF1ParameterSpec;34import javax.crypto.*;35import javax.crypto.spec.PSource;36import javax.crypto.spec.OAEPParameterSpec;3738public class TestOAEPWithParams {3940private static Provider cp;4142private static PrivateKey privateKey;4344private static PublicKey publicKey;4546private static Random random = new Random();4748private static String MD[] = {49"MD5", "SHA1", "SHA-224", "SHA-256", "SHA-512/224", "SHA-512/256"50};51private static int DATA_LENGTH[] = {5262, 54, 34, 30, 34, 3053};54public static void main(String[] args) throws Exception {55long start = System.currentTimeMillis();56cp = Security.getProvider("SunJCE");57System.out.println("Testing provider " + cp.getName() + "...");58Provider kfp = Security.getProvider("SunRsaSign");59KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp);60kpg.initialize(768);61KeyPair kp = kpg.generateKeyPair();62privateKey = kp.getPrivate();63publicKey = kp.getPublic();6465for (int i = 0; i < MD.length; i++) {66// basic test using MD567testEncryptDecrypt(MD[i], DATA_LENGTH[i]);68}6970long stop = System.currentTimeMillis();71System.out.println("Done (" + (stop - start) + " ms).");72}7374private static void testEncryptDecrypt(String hashAlg, int dataLength)75throws Exception {76System.out.println("Testing OAEP with hash " + hashAlg + ", " + dataLength + " bytes");77Cipher c = Cipher.getInstance("RSA/ECB/OAEPwith" + hashAlg +78"andMGF1Padding", cp);79byte[] pSrc1 = { (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,80(byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x0281};82byte[] pSrc2 = { (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,83(byte) 0x02, (byte) 0x02, (byte) 0x03, (byte) 0x0484};85OAEPParameterSpec spec1 = new OAEPParameterSpec(hashAlg,86"MGF1", MGF1ParameterSpec.SHA1, new PSource.PSpecified(pSrc1));87OAEPParameterSpec spec2 = new OAEPParameterSpec(hashAlg,88"MGF1", MGF1ParameterSpec.SHA1, new PSource.PSpecified(pSrc2));89byte[] plainText = new byte[dataLength];90byte[] cipherText, recovered;91// do encryption with parameters#192System.out.println("Testing with user-supplied parameters...");93c.init(Cipher.ENCRYPT_MODE, publicKey, spec1);94cipherText = c.doFinal(plainText);9596// test#1: decrypt with parameters#197c.init(Cipher.DECRYPT_MODE, privateKey, spec1);98recovered = c.doFinal(cipherText);99if (Arrays.equals(plainText, recovered) == false) {100throw new Exception("Decrypted data does not match");101}102103// test#2: decrypt without parameters104c.init(Cipher.DECRYPT_MODE, privateKey);105try {106recovered = c.doFinal(cipherText);107throw new Exception("Should throw BadPaddingException");108} catch (BadPaddingException bpe) {109// expected110}111// test#3: decrypt with different parameters112c.init(Cipher.DECRYPT_MODE, privateKey, spec2);113try {114recovered = c.doFinal(cipherText);115throw new Exception("Should throw BadPaddingException");116} catch (BadPaddingException bpe) {117// expected118}119// do encryption without parameters120System.out.println("Testing with cipher default parameters...");121c.init(Cipher.ENCRYPT_MODE, publicKey);122cipherText = c.doFinal(plainText);123124// test#1: decrypt with parameters got from cipher125AlgorithmParameters params = c.getParameters();126c.init(Cipher.DECRYPT_MODE, privateKey, params);127recovered = c.doFinal(cipherText);128if (Arrays.equals(plainText, recovered) == false) {129throw new Exception("Decrypted data does not match");130}131132// test#2: decrypt without parameters133c.init(Cipher.DECRYPT_MODE, privateKey);134recovered = c.doFinal(cipherText);135if (Arrays.equals(plainText, recovered) == false) {136throw new Exception("Decrypted data does not match");137}138139// test#3: decrypt with different parameters140c.init(Cipher.DECRYPT_MODE, privateKey, spec2);141try {142recovered = c.doFinal(cipherText);143throw new Exception("Should throw BadPaddingException");144} catch (BadPaddingException bpe) {145// expected146}147}148}149150151