1 /*
   2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 7044060
  27  * @summary verify that DSA parameter generation works
  28  * @run main/othervm/timeout=300 TestAlgParameterGenerator
  29  */
  30 import java.security.*;
  31 import java.security.spec.*;
  32 import java.security.interfaces.*;
  33 
  34 public class TestAlgParameterGenerator {
  35 
  36     private static void checkParamStrength(AlgorithmParameters param,
  37                                            int strength) throws Exception {
  38         String algo = param.getAlgorithm();
  39         if (!algo.equalsIgnoreCase("DSA")) {
  40             throw new Exception("Unexpected type of parameters: " + algo);
  41         }
  42         DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
  43         int valueL = spec.getP().bitLength();
  44         if (strength != valueL) {
  45             System.out.println(
  46                     "Expected " + strength + " but actual " + valueL);
  47             throw new Exception("Wrong P strength");
  48         }
  49     }
  50 
  51     private static void checkParamStrength(AlgorithmParameters param,
  52             DSAGenParameterSpec genParam) throws Exception {
  53 
  54         String algo = param.getAlgorithm();
  55         if (!algo.equalsIgnoreCase("DSA")) {
  56             throw new Exception("Unexpected type of parameters: " + algo);
  57         }
  58         DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
  59         int valueL = spec.getP().bitLength();
  60         int strength = genParam.getPrimePLength();
  61         if (strength != valueL) {
  62             System.out.println(
  63                     "P: Expected " + strength + " but actual " + valueL);
  64             throw new Exception("Wrong P strength");
  65         }
  66         int valueN = spec.getQ().bitLength();
  67         strength = genParam.getSubprimeQLength();
  68         if (strength != valueN) {
  69             System.out.println(
  70                     "Q: Expected " + strength + " but actual " + valueN);
  71             throw new Exception("Wrong Q strength");
  72         }
  73     }
  74 
  75     public static void main(String[] args) throws Exception {
  76         AlgorithmParameterGenerator apg =
  77             AlgorithmParameterGenerator.getInstance("DSA", "SUN");
  78 
  79         long start, stop;
  80         // make sure no-init still works
  81         start = System.currentTimeMillis();
  82         AlgorithmParameters param = apg.generateParameters();
  83         stop = System.currentTimeMillis();
  84         System.out.println("Time: " + (stop - start) + " ms.");
  85         checkParamStrength(param, 1024);
  86 
  87         // make sure the old model works
  88         int[] strengths = { 512, 768, 1024 };
  89         for (int i = 0; i < strengths.length; i++) {
  90             int sizeP = strengths[i];
  91             System.out.println("Generating " + sizeP + "-bit DSA Parameters");
  92             start = System.currentTimeMillis();
  93             apg.init(sizeP);
  94             param = apg.generateParameters();
  95             stop = System.currentTimeMillis();
  96             System.out.println("Time: " + (stop - start) + " ms.");
  97             checkParamStrength(param, sizeP);
  98         }
  99 
 100         // now the newer model
 101         DSAGenParameterSpec spec1 = new DSAGenParameterSpec(1024, 160);
 102         DSAGenParameterSpec spec2 = new DSAGenParameterSpec(2048, 224);
 103         DSAGenParameterSpec spec3 = new DSAGenParameterSpec(2048, 256);
 104         DSAGenParameterSpec[] specSet = {
 105             spec1, spec2, spec3
 106         };
 107         for (int i = 0; i < specSet.length; i++) {
 108             DSAGenParameterSpec genParam = specSet[i];
 109             System.out.println("Generating (" + genParam.getPrimePLength() +
 110                                ", " + genParam.getSubprimeQLength() +
 111                                ") DSA Parameters");
 112             start = System.currentTimeMillis();
 113             apg.init(genParam, null);
 114             param = apg.generateParameters();
 115             stop = System.currentTimeMillis();
 116             System.out.println("Time: " + (stop - start) + " ms.");
 117             checkParamStrength(param, genParam);
 118         }
 119     }
 120 }