1 /* 2 * Copyright (c) 2012, 2017, 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 8181048 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("Expected " + strength + " but actual " + valueL); 46 throw new Exception("Wrong P strength"); 47 } 48 } 49 private static void checkParamStrength(AlgorithmParameters param, 50 DSAGenParameterSpec genParam) 51 throws Exception { 52 String algo = param.getAlgorithm(); 53 if (!algo.equalsIgnoreCase("DSA")) { 54 throw new Exception("Unexpected type of parameters: " + algo); 55 } 56 DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class); 57 int valueL = spec.getP().bitLength(); 58 int strength = genParam.getPrimePLength(); 59 if (strength != valueL) { 60 System.out.println("P: Expected " + strength + " but actual " + valueL); 61 throw new Exception("Wrong P strength"); 62 } 63 int valueN = spec.getQ().bitLength(); 64 strength = genParam.getSubprimeQLength(); 65 if (strength != valueN) { 66 System.out.println("Q: Expected " + strength + " but actual " + valueN); 67 throw new Exception("Wrong Q strength"); 68 } 69 } 70 71 public static void main(String[] args) throws Exception { 72 AlgorithmParameterGenerator apg = 73 AlgorithmParameterGenerator.getInstance("DSA", "SUN"); 74 75 long start, stop; 76 // make sure no-init still works 77 start = System.currentTimeMillis(); 78 AlgorithmParameters param = apg.generateParameters(); 79 stop = System.currentTimeMillis(); 80 System.out.println("Time: " + (stop - start) + " ms."); 81 82 // make sure the old model works 83 int[] strengths = { 512, 768, 1024 }; 84 for (int i = 0; i < strengths.length; i++) { 85 int sizeP = strengths[i]; 86 System.out.println("Generating " + sizeP + "-bit DSA Parameters"); 87 start = System.currentTimeMillis(); 88 apg.init(sizeP); 89 param = apg.generateParameters(); 90 stop = System.currentTimeMillis(); 91 System.out.println("Time: " + (stop - start) + " ms."); 92 checkParamStrength(param, sizeP); 93 } 94 95 // now the newer model 96 DSAGenParameterSpec spec1 = new DSAGenParameterSpec(1024, 160); 97 DSAGenParameterSpec spec2 = new DSAGenParameterSpec(2048, 224); 98 DSAGenParameterSpec spec3 = new DSAGenParameterSpec(2048, 256); 99 //DSAGenParameterSpec spec4 = new DSAGenParameterSpec(3072, 256); 100 DSAGenParameterSpec[] specSet = { 101 spec1, spec2, spec3//, spec4 102 }; 103 for (int i = 0; i < specSet.length; i++) { 104 DSAGenParameterSpec genParam = specSet[i]; 105 System.out.println("Generating (" + genParam.getPrimePLength() + 106 ", " + genParam.getSubprimeQLength() + 107 ") DSA Parameters"); 108 start = System.currentTimeMillis(); 109 apg.init(genParam, null); 110 param = apg.generateParameters(); 111 stop = System.currentTimeMillis(); 112 System.out.println("Time: " + (stop - start) + " ms."); 113 checkParamStrength(param, genParam); 114 } 115 } 116 }