TestAlgParameterGenerator.java 4.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
26
 * @bug 7044060 8055351 8181048
27
 * @summary verify that DSA parameter generation works
28
 * @run main/timeout=600 TestAlgParameterGenerator
29
 */
30 31 32 33 34

import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
import java.security.spec.DSAGenParameterSpec;
import java.security.spec.DSAParameterSpec;
35 36 37 38

public class TestAlgParameterGenerator {

    private static void checkParamStrength(AlgorithmParameters param,
39
            int strength) throws Exception {
40 41
        String algo = param.getAlgorithm();
        if (!algo.equalsIgnoreCase("DSA")) {
42
            throw new RuntimeException("Unexpected type of parameters: " + algo);
43 44 45 46 47
        }
        DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
        int valueL = spec.getP().bitLength();
        if (strength != valueL) {
            System.out.println("Expected " + strength + " but actual " + valueL);
48
            throw new RuntimeException("Wrong P strength");
49 50
        }
    }
51

52
    private static void checkParamStrength(AlgorithmParameters param,
53 54
            DSAGenParameterSpec genParam)
            throws Exception {
55 56
        String algo = param.getAlgorithm();
        if (!algo.equalsIgnoreCase("DSA")) {
57
            throw new RuntimeException("Unexpected type of parameters: " + algo);
58 59 60 61 62 63
        }
        DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
        int valueL = spec.getP().bitLength();
        int strength = genParam.getPrimePLength();
        if (strength != valueL) {
            System.out.println("P: Expected " + strength + " but actual " + valueL);
64
            throw new RuntimeException("Wrong P strength");
65 66 67 68 69
        }
        int valueN = spec.getQ().bitLength();
        strength = genParam.getSubprimeQLength();
        if (strength != valueN) {
            System.out.println("Q: Expected " + strength + " but actual " + valueN);
70
            throw new RuntimeException("Wrong Q strength");
71 72 73 74
        }
    }

    public static void main(String[] args) throws Exception {
75 76
        AlgorithmParameterGenerator apg
                = AlgorithmParameterGenerator.getInstance("DSA", "SUN");
77
        long start, stop;
78

79 80 81 82 83 84 85
        // make sure no-init still works
        start = System.currentTimeMillis();
        AlgorithmParameters param = apg.generateParameters();
        stop = System.currentTimeMillis();
        System.out.println("Time: " + (stop - start) + " ms.");

        // make sure the old model works
86 87
        int[] strengths = {512, 768, 1024};
        for (int sizeP : strengths) {
88 89 90 91 92 93 94 95 96 97 98
            System.out.println("Generating " + sizeP + "-bit DSA Parameters");
            start = System.currentTimeMillis();
            apg.init(sizeP);
            param = apg.generateParameters();
            stop = System.currentTimeMillis();
            System.out.println("Time: " + (stop - start) + " ms.");
            checkParamStrength(param, sizeP);
        }

        // now the newer model
        DSAGenParameterSpec[] specSet = {
99 100 101 102 103
            new DSAGenParameterSpec(1024, 160),
            new DSAGenParameterSpec(2048, 224),
            new DSAGenParameterSpec(2048, 256)
            // no support for prime size 3072
            // ,new DSAGenParameterSpec(3072, 256)
104
        };
105 106 107 108

        for (DSAGenParameterSpec genParam : specSet) {
            System.out.println("Generating (" + genParam.getPrimePLength()
                    + ", " + genParam.getSubprimeQLength() + ") DSA Parameters");
109 110 111 112 113 114 115 116 117
            start = System.currentTimeMillis();
            apg.init(genParam, null);
            param = apg.generateParameters();
            stop = System.currentTimeMillis();
            System.out.println("Time: " + (stop - start) + " ms.");
            checkParamStrength(param, genParam);
        }
    }
}