test/jdk/sun/security/rsa/SignatureTest.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:
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
     4
 *
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
     8
 *
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    13
 * accompanied this code).
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    14
 *
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    18
 *
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    21
 * questions.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    22
 */
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    23
import java.security.*;
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    24
import java.security.interfaces.RSAPrivateKey;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    25
import java.security.interfaces.RSAPublicKey;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    26
import java.security.spec.*;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    27
import java.util.*;
37903
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    28
import java.util.stream.IntStream;
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    29
import static javax.crypto.Cipher.PRIVATE_KEY;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    30
import static javax.crypto.Cipher.PUBLIC_KEY;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    31
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    32
import jdk.test.lib.SigTestUtil;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    33
import static jdk.test.lib.SigTestUtil.SignatureType;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    34
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    35
/**
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    36
 * @test
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    37
 * @bug 8044199 8146293
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    38
 * @summary Create a signature for RSA and get its signed data. re-initiate
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    39
 *          the signature with the public key. The signature can be verified
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    40
 *          by acquired signed data.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    41
 * @library /test/lib
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    42
 * @build jdk.test.lib.SigTestUtil
37903
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    43
 * @run main SignatureTest 512
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    44
 * @run main SignatureTest 768
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    45
 * @run main SignatureTest 1024
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    46
 * @run main SignatureTest 2048
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    47
 * @run main/timeout=240 SignatureTest 4096
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    48
 * @run main/timeout=240 SignatureTest 5120
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    49
 * @run main/timeout=480 SignatureTest 6144
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    50
 */
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    51
public class SignatureTest {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    52
    /**
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    53
     * ALGORITHM name, fixed as RSA.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    54
     */
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    55
    private static final String KEYALG = "RSA";
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    56
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    57
    /**
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    58
     * JDK default RSA Provider.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    59
     */
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    60
    private static final String PROVIDER = "SunRsaSign";
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    61
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    62
    /**
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    63
     * How much times signature updated.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    64
     */
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    65
    private static final int UPDATE_TIMES_FIFTY = 50;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    66
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    67
    /**
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    68
     * How much times signature initial updated.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    69
     */
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    70
    private static final int UPDATE_TIMES_HUNDRED = 100;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    71
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    72
    public static void main(String[] args) throws Exception {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    73
        int keySize = Integer.parseInt(args[0]);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    74
        Iterable<String> md_alg_pkcs15 =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    75
            SigTestUtil.getDigestAlgorithms(SignatureType.RSA, keySize);
37903
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    76
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    77
        Iterable<String> md_alg_pss =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    78
            SigTestUtil.getDigestAlgorithms(SignatureType.RSASSA_PSS, keySize);
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    79
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    80
        byte[] data = new byte[100];
37903
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    81
        IntStream.range(0, data.length).forEach(j -> {
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    82
            data[j] = (byte) j;
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    83
        });
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    84
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    85
        // create a key pair
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    86
        KeyPair kpair = generateKeys(KEYALG, keySize);
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    87
        Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    88
        Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    89
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    90
        test(SignatureType.RSA, md_alg_pkcs15, privs, pubs, data);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    91
        test(SignatureType.RSASSA_PSS, md_alg_pss, privs, pubs, data);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    92
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    93
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    94
    private static void test(SignatureType type, Iterable<String> digestAlgs,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    95
            Key[] privs, Key[] pubs, byte[] data) throws RuntimeException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    96
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    97
        // For signature algorithm, create and verify a signature
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    98
        Arrays.stream(privs).forEach(priv
37903
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
    99
                -> Arrays.stream(pubs).forEach(pub
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   100
                -> digestAlgs.forEach(digestAlg -> {
37903
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
   101
            try {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   102
                AlgorithmParameterSpec sigParams =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   103
                    SigTestUtil.generateDefaultParameter(type, digestAlg);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   104
                String sigAlg = SigTestUtil.generateSigAlg(type, digestAlg);
37903
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
   105
                checkSignature(data, (PublicKey) pub, (PrivateKey) priv,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   106
                        sigAlg, sigParams);
37903
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
   107
            } catch (NoSuchAlgorithmException | InvalidKeyException |
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   108
                    SignatureException | NoSuchProviderException |
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   109
                    InvalidAlgorithmParameterException ex) {
37903
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
   110
                throw new RuntimeException(ex);
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
   111
            }
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
   112
        }
8be873ca5e79 8156671: Refactor sun/security/rsa/SignatureTest.java
rhalade
parents: 32509
diff changeset
   113
        )));
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   114
    }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   115
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   116
    private static KeyPair generateKeys(String keyalg, int size)
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   117
            throws NoSuchAlgorithmException {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   118
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   119
        kpg.initialize(size);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   120
        return kpg.generateKeyPair();
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   121
    }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   122
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   123
    private static Key[] manipulateKey(int type, Key key)
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   124
            throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   125
        KeyFactory kf = KeyFactory.getInstance(KEYALG, PROVIDER);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   126
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   127
        switch (type) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   128
            case PUBLIC_KEY:
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   129
                try {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   130
                    kf.getKeySpec(key, RSAPrivateKeySpec.class);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   131
                    throw new RuntimeException("Expected InvalidKeySpecException "
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   132
                            + "not thrown");
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   133
                } catch (InvalidKeySpecException expected) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   134
                }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   135
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   136
                return new Key[]{
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   137
                    kf.generatePublic(kf.getKeySpec(key, RSAPublicKeySpec.class)),
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   138
                    kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   139
                    kf.generatePublic(new RSAPublicKeySpec(
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   140
                    ((RSAPublicKey) key).getModulus(),
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   141
                    ((RSAPublicKey) key).getPublicExponent()))
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   142
                };
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   143
            case PRIVATE_KEY:
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   144
                try {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   145
                    kf.getKeySpec(key, RSAPublicKeySpec.class);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   146
                    throw new RuntimeException("Expected InvalidKeySpecException"
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   147
                            + " not thrown");
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   148
                } catch (InvalidKeySpecException expected) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   149
                }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   150
                return new Key[]{
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   151
                    kf.generatePrivate(kf.getKeySpec(key,
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   152
                    RSAPrivateKeySpec.class)),
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   153
                    kf.generatePrivate(new PKCS8EncodedKeySpec(
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   154
                    key.getEncoded())),
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   155
                    kf.generatePrivate(new RSAPrivateKeySpec(((RSAPrivateKey) key).getModulus(),
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   156
                    ((RSAPrivateKey) key).getPrivateExponent()))
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   157
                };
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   158
        }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   159
        throw new RuntimeException("We shouldn't reach here");
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   160
    }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   161
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   162
    private static void checkSignature(byte[] data, PublicKey pub,
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   163
            PrivateKey priv, String sigAlg, AlgorithmParameterSpec sigParams)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   164
            throws NoSuchAlgorithmException, InvalidKeyException,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   165
            SignatureException, NoSuchProviderException,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   166
            InvalidAlgorithmParameterException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   167
        System.out.println("Testing " + sigAlg);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   168
        Signature sig = Signature.getInstance(sigAlg, PROVIDER);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   169
        sig.setParameter(sigParams);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   170
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   171
        sig.initSign(priv);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   172
        for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   173
            sig.update(data);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   174
        }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   175
        byte[] signedData = sig.sign();
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   176
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   177
        // Make sure signature verifies with original data
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   178
        sig.setParameter(sigParams);
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   179
        sig.initVerify(pub);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   180
        for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   181
            sig.update(data);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   182
        }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   183
        if (!sig.verify(signedData)) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   184
            throw new RuntimeException("Failed to verify " + sigAlg
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   185
                    + " signature");
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   186
        }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   187
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   188
        // Make sure signature does NOT verify when the original data
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   189
        // has changed
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   190
        sig.initVerify(pub);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   191
        for (int i = 0; i < UPDATE_TIMES_FIFTY; i++) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   192
            sig.update(data);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   193
        }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   194
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   195
        if (sig.verify(signedData)) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   196
            throw new RuntimeException("Failed to detect bad " + sigAlg
32509
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   197
                    + " signature");
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   198
        }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   199
    }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   200
}