Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEPPadding.java
41161 views
/*1* Copyright (c) 2013, 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 8020081 802266926* @summary encryption/decryption test for using OAEPPadding with27* OAEPParameterSpec specified and not specified during a Cipher.init().28* @author Anthony Scarpino29*/3031import java.util.Arrays;3233import java.security.Security;34import java.security.Provider;35import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.interfaces.RSAPrivateKey;38import java.security.interfaces.RSAPublicKey;39import java.security.spec.MGF1ParameterSpec;4041import javax.crypto.Cipher;42import javax.crypto.spec.OAEPParameterSpec;43import javax.crypto.IllegalBlockSizeException;44import javax.crypto.spec.PSource;454647public class TestOAEPPadding {48private static RSAPrivateKey privateKey;49private static RSAPublicKey publicKey;50static Provider cp;51static boolean failed = false;5253public static void main(String args[]) throws Exception {54cp = Security.getProvider("SunJCE");55System.out.println("Testing provider " + cp.getName() + "...");56Provider kfp = Security.getProvider("SunRsaSign");57KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp);58kpg.initialize(2048);59KeyPair kp = kpg.generateKeyPair();60privateKey = (RSAPrivateKey)kp.getPrivate();61publicKey = (RSAPublicKey)kp.getPublic();6263// Test using a spec with each digest algorithm case64// MD565test(new OAEPParameterSpec("MD5", "MGF1",66MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));67test(new OAEPParameterSpec("MD5", "MGF1",68MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));69test(new OAEPParameterSpec("MD5", "MGF1",70MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));71test(new OAEPParameterSpec("MD5", "MGF1",72MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));73test(new OAEPParameterSpec("MD5", "MGF1",74MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));75// SHA176test(new OAEPParameterSpec("SHA1", "MGF1",77MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));78test(new OAEPParameterSpec("SHA1", "MGF1",79MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));80test(new OAEPParameterSpec("SHA1", "MGF1",81MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));82test(new OAEPParameterSpec("SHA1", "MGF1",83MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));84test(new OAEPParameterSpec("SHA1", "MGF1",85MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));86// For default OAEPParameterSpec case (SHA1)87test(null);88// SHA-22489test(new OAEPParameterSpec("SHA-224", "MGF1",90MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));91test(new OAEPParameterSpec("SHA-224", "MGF1",92MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));93test(new OAEPParameterSpec("SHA-224", "MGF1",94MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));95test(new OAEPParameterSpec("SHA-224", "MGF1",96MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));97test(new OAEPParameterSpec("SHA-224", "MGF1",98MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));99// SHA-256100test(new OAEPParameterSpec("SHA-256", "MGF1",101MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));102test(new OAEPParameterSpec("SHA-256", "MGF1",103MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));104test(new OAEPParameterSpec("SHA-256", "MGF1",105MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));106test(new OAEPParameterSpec("SHA-256", "MGF1",107MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));108test(new OAEPParameterSpec("SHA-256", "MGF1",109MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));110// SHA-384111test(new OAEPParameterSpec("SHA-384", "MGF1",112MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));113test(new OAEPParameterSpec("SHA-384", "MGF1",114MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));115test(new OAEPParameterSpec("SHA-384", "MGF1",116MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));117test(new OAEPParameterSpec("SHA-384", "MGF1",118MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));119test(new OAEPParameterSpec("SHA-384", "MGF1",120MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));121// SHA-512122test(new OAEPParameterSpec("SHA-512", "MGF1",123MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));124test(new OAEPParameterSpec("SHA-512", "MGF1",125MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));126test(new OAEPParameterSpec("SHA-512", "MGF1",127MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));128test(new OAEPParameterSpec("SHA-512", "MGF1",129MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));130test(new OAEPParameterSpec("SHA-512", "MGF1",131MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));132// SHA-512/224 and SHA-512/256133test(new OAEPParameterSpec("SHA-512/224", "MGF1",134MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));135test(new OAEPParameterSpec("SHA-512/224", "MGF1",136MGF1ParameterSpec.SHA512_224, PSource.PSpecified.DEFAULT));137test(new OAEPParameterSpec("SHA-512/256", "MGF1",138MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));139test(new OAEPParameterSpec("SHA-512/256", "MGF1",140MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));141142if (failed) {143throw new Exception("Test failed");144}145}146147/*148* Test with one byte, the max bytes, and the max + 1 bytes allowed by149* the RSA key size and the digest algorithm150*/151static void test(OAEPParameterSpec spec) throws Exception {152int dlen = 0;153String algo;154155// For default OAEPParameterSpec case (SHA1)156if (spec == null) {157dlen = 20;158algo = "Default";159} else {160// Use the digest algorith provided in the spec161algo = spec.getDigestAlgorithm();162if (algo.equals("MD5")) {163dlen = 16;164} else if (algo.equals("SHA1")) {165dlen = 20;166} else if (algo.equals("SHA-224") || algo.equals("SHA-512/224")) {167dlen = 28;168} else if (algo.equals("SHA-256") || algo.equals("SHA-512/256")) {169dlen = 32;170} else if (algo.equals("SHA-384")) {171dlen = 48;172} else if (algo.equals("SHA-512")) {173dlen = 64;174}175}176177// OAEP maximum length for a given digest algorith & RSA key length178int max = ((publicKey.getModulus().bitLength() / 8) - (2 * dlen) - 2);179180// Test with data length of 1181try {182testEncryptDecrypt(spec, 1);183} catch (Exception e) {184System.out.println(algo + " failed with data length of 1");185e.printStackTrace();186failed = true;187}188189// Test with data length of maximum allowed190try {191testEncryptDecrypt(spec, max);192} catch (Exception e) {193System.out.println(algo + " failed with data length of " + max);194e.printStackTrace();195failed = true;196}197198// Test with data length of maximum allowed + 1199try {200testEncryptDecrypt(spec, max + 1);201throw new Exception();202} catch (IllegalBlockSizeException ie) {203// expected to fail204} catch (Exception e) {205System.err.println(algo + " failed with data length of " +206(max + 1));207e.printStackTrace();208failed = true;209210}211}212213private static void testEncryptDecrypt(OAEPParameterSpec spec,214int dataLength) throws Exception {215216System.out.print("Testing OAEP with hash ");217if (spec != null) {218System.out.print(spec.getDigestAlgorithm() + " and MGF " +219((MGF1ParameterSpec)spec.getMGFParameters()).220getDigestAlgorithm());221} else {222System.out.print("Default");223}224System.out.println(", " + dataLength + " bytes");225226Cipher c = Cipher.getInstance("RSA/ECB/OAEPPadding", cp);227if (spec != null) {228c.init(Cipher.ENCRYPT_MODE, publicKey, spec);229} else {230c.init(Cipher.ENCRYPT_MODE, publicKey);231}232233byte[] data = new byte[dataLength];234byte[] enc = c.doFinal(data);235if (spec != null) {236c.init(Cipher.DECRYPT_MODE, privateKey, spec);237} else {238c.init(Cipher.DECRYPT_MODE, privateKey);239}240byte[] dec = c.doFinal(enc);241if (Arrays.equals(data, dec) == false) {242throw new Exception("Data does not match");243}244}245}246247248