test/jdk/sun/security/rsa/KeySizeTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 32509 jdk/test/sun/security/rsa/KeySizeTest.java@142bd680022d
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
/*
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
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
 */
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    23
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    24
import java.security.KeyFactory;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    25
import java.security.KeyPair;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    26
import java.security.KeyPairGenerator;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    27
import java.security.PrivateKey;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    28
import java.security.PublicKey;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    29
import java.security.interfaces.RSAKey;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    30
import java.security.interfaces.RSAPrivateKey;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    31
import java.security.interfaces.RSAPublicKey;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    32
import java.security.spec.RSAPrivateKeySpec;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    33
import java.security.spec.RSAPublicKeySpec;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    34
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
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    37
 * @bug 8044199
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    38
 * @summary test if the private and public key size are the same as what is set
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    39
 * through KeyPairGenerator.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    40
 * @run main KeySizeTest 512 10
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    41
 * @run main KeySizeTest 768 10
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    42
 * @run main KeySizeTest 1024 10
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    43
 * @run main KeySizeTest 2048 5
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    44
 * @run main KeySizeTest 4096 1
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    45
 */
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    46
public class KeySizeTest {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    47
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    48
    /**
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    49
     * ALGORITHM name, fixed as RSA.
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
    private static final String KEYALG = "RSA";
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
    /**
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    54
     * JDK default RSA Provider.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    55
     */
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    56
    private static final String PROVIDER_NAME = "SunRsaSign";
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
    public static void main(String[] args) throws Exception {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    59
        int iKeyPairSize = Integer.parseInt(args[0]);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    60
        int maxLoopCnt = Integer.parseInt(args[1]);
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
        int failCount = 0;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    63
        KeyPairGenerator keyPairGen
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    64
                = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    65
        keyPairGen.initialize(iKeyPairSize);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    66
        // Generate RSA keypair
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    67
        KeyPair keyPair = keyPairGen.generateKeyPair();
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    68
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    69
        // Get priavte and public keys
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    70
        PrivateKey privateKey = keyPair.getPrivate();
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    71
        PublicKey publicKey = keyPair.getPublic();
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    72
        try {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    73
            if (!sizeTest(keyPair)) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    74
                failCount++;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    75
            }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    76
        } catch (Exception ex) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    77
            ex.printStackTrace(System.err);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    78
            failCount++;
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
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    81
        for (int iCnt = 0; iCnt < maxLoopCnt; iCnt++) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    82
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    83
            // Get keysize (modulus) of keys
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    84
            KeyFactory keyFact = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    85
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    86
            // Comparing binary length.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    87
            RSAPrivateKeySpec privateKeySpec
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    88
                    = (RSAPrivateKeySpec) keyFact.getKeySpec(privateKey,
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    89
                            RSAPrivateKeySpec.class);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    90
            int iPrivateKeySize = privateKeySpec.getModulus().bitLength();
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    91
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    92
            RSAPublicKeySpec publicKeySpec
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    93
                    = (RSAPublicKeySpec) keyFact.getKeySpec(publicKey,
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    94
                            RSAPublicKeySpec.class);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    95
            int iPublicKeySize = publicKeySpec.getModulus().bitLength();
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    96
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    97
            if ((iKeyPairSize != iPublicKeySize) || (iKeyPairSize != iPrivateKeySize)) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    98
                System.err.println("iKeyPairSize : " + iKeyPairSize);
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
    99
                System.err.println("Generated a " + iPrivateKeySize
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   100
                        + " bit RSA private key");
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   101
                System.err.println("Generated a " + iPublicKeySize
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   102
                        + " bit RSA public key");
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   103
                failCount++;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   104
            }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   105
        }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   106
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   107
        if (failCount > 0) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   108
            throw new RuntimeException("There are " + failCount + " tests failed.");
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   109
        }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   110
    }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   111
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   112
    /**
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   113
     * @param kpair test key pair.
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   114
     * @return true if test passed. false if test failed.
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 boolean sizeTest(KeyPair kpair) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   117
        RSAPrivateKey priv = (RSAPrivateKey) kpair.getPrivate();
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   118
        RSAPublicKey pub = (RSAPublicKey) kpair.getPublic();
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   119
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   120
        // test the getModulus method
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   121
        if ((priv instanceof RSAKey) && (pub instanceof RSAKey)) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   122
            if (!priv.getModulus().equals(pub.getModulus())) {
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   123
                System.err.println("priv.getModulus() = " + priv.getModulus());
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   124
                System.err.println("pub.getModulus() = " + pub.getModulus());
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   125
                return false;
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
        }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   128
        return true;
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   129
    }
142bd680022d 8044199: Tests for RSA keys and key specifications
tyan
parents:
diff changeset
   130
}