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 test ASN.1 encoding generation/parsing for the OAEPParameters
0N/A * implementation in SunJCE provider.
0N/A * @author Valerie Peng
0N/A */
0N/Aimport java.math.BigInteger;
0N/Aimport java.util.*;
0N/Aimport java.security.*;
0N/Aimport java.security.spec.MGF1ParameterSpec;
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.OAEPParameterSpec;
0N/Aimport javax.crypto.spec.PSource;
0N/A
0N/Apublic class TestOAEPParameterSpec {
0N/A
0N/A private static Provider cp;
0N/A
0N/A private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
0N/A byte[] p) throws Exception {
0N/A OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
0N/A mgfSpec, new PSource.PSpecified(p));
0N/A cp = Security.getProvider("SunJCE");
0N/A System.out.println("Testing provider " + cp.getName() + "...");
0N/A AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);
0N/A
0N/A ap.init(spec);
0N/A byte[] encoding = ap.getEncoded();
0N/A
0N/A AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
0N/A ap2.init(encoding);
0N/A
0N/A OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
0N/A (OAEPParameterSpec.class);
0N/A return compareSpec(spec, spec2);
0N/A }
0N/A
0N/A private static boolean compareMD(OAEPParameterSpec s1,
0N/A OAEPParameterSpec s2) {
0N/A boolean result = false;
0N/A String alg1 = s1.getDigestAlgorithm().toUpperCase().trim();
0N/A String alg2 = s2.getDigestAlgorithm().toUpperCase().trim();
0N/A alg1 = alg1.replaceAll("\\-", "");
0N/A alg2 = alg2.replaceAll("\\-", "");
0N/A if (alg1.equals("SHA") || alg1.equals("SHA1")) {
0N/A result = (alg2.equals("SHA") || alg2.equals("SHA1"));
0N/A } else {
0N/A result = (alg1.equals(alg2));
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A
0N/A private static boolean compareMGF(OAEPParameterSpec s1,
0N/A OAEPParameterSpec s2) {
0N/A String alg1 = s1.getMGFAlgorithm();
0N/A String alg2 = s2.getMGFAlgorithm();
0N/A if (alg1.equals(alg2)) {
0N/A MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
0N/A MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
0N/A alg1 = mp1.getDigestAlgorithm();
0N/A alg2 = mp2.getDigestAlgorithm();
0N/A if (alg1.equals(alg2)) {
0N/A return true;
0N/A } else {
0N/A System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
0N/A return false;
0N/A }
0N/A } else {
0N/A System.out.println("MGF algos: " + alg1 + " vs " + alg2);
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A private static boolean comparePSource(OAEPParameterSpec s1,
0N/A OAEPParameterSpec s2) {
0N/A PSource src1 = s1.getPSource();
0N/A PSource src2 = s2.getPSource();
0N/A String alg1 = src1.getAlgorithm();
0N/A String alg2 = src2.getAlgorithm();
0N/A if (alg1.equals(alg2)) {
0N/A // assumes they are PSource.PSpecified
0N/A return Arrays.equals(((PSource.PSpecified) src1).getValue(),
0N/A ((PSource.PSpecified) src2).getValue());
0N/A } else {
0N/A System.out.println("PSource algos: " + alg1 + " vs " + alg2);
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A private static boolean compareSpec(OAEPParameterSpec s1,
0N/A OAEPParameterSpec s2) {
0N/A return (compareMD(s1, s2) && compareMGF(s1, s2) &&
0N/A comparePSource(s1, s2));
0N/A }
0N/A
0N/A public static void main(String[] argv) throws Exception {
0N/A boolean status = true;
0N/A byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
0N/A status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
0N/A status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
0N/A status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
0N/A status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
0N/A status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
0N/A status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
0N/A if (status) {
0N/A System.out.println("Test Passed");
0N/A } else {
0N/A throw new Exception("One or More Test Failed");
0N/A }
0N/A }
0N/A}