Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEPParameterSpec.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 test ASN.1 encoding generation/parsing for the OAEPParameters27* implementation in SunJCE provider.28* @author Valerie Peng29*/30import java.math.BigInteger;31import java.util.*;32import java.security.*;33import java.security.spec.MGF1ParameterSpec;34import javax.crypto.*;35import javax.crypto.spec.OAEPParameterSpec;36import javax.crypto.spec.PSource;3738public class TestOAEPParameterSpec {3940private static Provider cp;4142private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,43byte[] p) throws Exception {44OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",45mgfSpec, new PSource.PSpecified(p));46cp = Security.getProvider("SunJCE");47System.out.println("Testing provider " + cp.getName() + "...");48AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);4950ap.init(spec);51byte[] encoding = ap.getEncoded();5253AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);54ap2.init(encoding);5556OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec57(OAEPParameterSpec.class);58return compareSpec(spec, spec2);59}6061private static boolean compareMD(OAEPParameterSpec s1,62OAEPParameterSpec s2) {63boolean result = false;64String alg1 = s1.getDigestAlgorithm().toUpperCase().trim();65String alg2 = s2.getDigestAlgorithm().toUpperCase().trim();66alg1 = alg1.replaceAll("\\-", "");67alg2 = alg2.replaceAll("\\-", "");68if (alg1.equals("SHA") || alg1.equals("SHA1")) {69result = (alg2.equals("SHA") || alg2.equals("SHA1"));70} else {71result = (alg1.equals(alg2));72}73return result;74}757677private static boolean compareMGF(OAEPParameterSpec s1,78OAEPParameterSpec s2) {79String alg1 = s1.getMGFAlgorithm();80String alg2 = s2.getMGFAlgorithm();81if (alg1.equals(alg2)) {82MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();83MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();84alg1 = mp1.getDigestAlgorithm();85alg2 = mp2.getDigestAlgorithm();86if (alg1.equals(alg2)) {87return true;88} else {89System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);90return false;91}92} else {93System.out.println("MGF algos: " + alg1 + " vs " + alg2);94return false;95}96}9798private static boolean comparePSource(OAEPParameterSpec s1,99OAEPParameterSpec s2) {100PSource src1 = s1.getPSource();101PSource src2 = s2.getPSource();102String alg1 = src1.getAlgorithm();103String alg2 = src2.getAlgorithm();104if (alg1.equals(alg2)) {105// assumes they are PSource.PSpecified106return Arrays.equals(((PSource.PSpecified) src1).getValue(),107((PSource.PSpecified) src2).getValue());108} else {109System.out.println("PSource algos: " + alg1 + " vs " + alg2);110return false;111}112}113114private static boolean compareSpec(OAEPParameterSpec s1,115OAEPParameterSpec s2) {116return (compareMD(s1, s2) && compareMGF(s1, s2) &&117comparePSource(s1, s2));118}119120public static void main(String[] argv) throws Exception {121boolean status = true;122byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };123status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);124status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);125status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);126status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);127status &= runTest("SHA-512/224", MGF1ParameterSpec.SHA512_224, p);128status &= runTest("SHA-512/256", MGF1ParameterSpec.SHA512_256, p);129status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);130status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);131status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);132if (status) {133System.out.println("Test Passed");134} else {135throw new Exception("One or More Test Failed");136}137}138}139140141