test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEPParameterSpec.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    26
 * @bug 4923484 8146293
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test ASN.1 encoding generation/parsing for the OAEPParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * implementation in SunJCE provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.spec.MGF1ParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.spec.OAEPParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.spec.PSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class TestOAEPParameterSpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static Provider cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        byte[] p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            mgfSpec, new PSource.PSpecified(p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        cp = Security.getProvider("SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        System.out.println("Testing provider " + cp.getName() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        ap.init(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        byte[] encoding = ap.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ap2.init(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                (OAEPParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return compareSpec(spec, spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static boolean compareMD(OAEPParameterSpec s1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        OAEPParameterSpec s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        boolean result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        String alg1 = s1.getDigestAlgorithm().toUpperCase().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        String alg2 = s2.getDigestAlgorithm().toUpperCase().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        alg1 = alg1.replaceAll("\\-", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        alg2 = alg2.replaceAll("\\-", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (alg1.equals("SHA") || alg1.equals("SHA1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            result = (alg2.equals("SHA") || alg2.equals("SHA1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            result = (alg1.equals(alg2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static boolean compareMGF(OAEPParameterSpec s1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        OAEPParameterSpec s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        String alg1 = s1.getMGFAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        String alg2 = s2.getMGFAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (alg1.equals(alg2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            alg1 = mp1.getDigestAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            alg2 = mp2.getDigestAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (alg1.equals(alg2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            System.out.println("MGF algos: " + alg1 + " vs " + alg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static boolean comparePSource(OAEPParameterSpec s1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        OAEPParameterSpec s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        PSource src1 = s1.getPSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        PSource src2 = s2.getPSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        String alg1 = src1.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        String alg2 = src2.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (alg1.equals(alg2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            // assumes they are PSource.PSpecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return Arrays.equals(((PSource.PSpecified) src1).getValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                ((PSource.PSpecified) src2).getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.out.println("PSource algos: " + alg1 + " vs " + alg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static boolean compareSpec(OAEPParameterSpec s1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        OAEPParameterSpec s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return (compareMD(s1, s2) && compareMGF(s1, s2) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                comparePSource(s1, s2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        boolean status = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   124
        status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   128
        status &= runTest("SHA-512/224", MGF1ParameterSpec.SHA512_224, p);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   129
        status &= runTest("SHA-512/256", MGF1ParameterSpec.SHA512_256, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.out.println("Test Passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new Exception("One or More Test Failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}