test/sun/security/provider/DSA/TestKeyPairGenerator.java

Print this page
8072452 Support DHE sizes up to 8192-bits
   1 /*
   2  * Copyright (c) 2003, 2012, 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 4800108
  27  * @summary verify that precomputed DSA parameters are always used (512, 768, 1024, 2048 bit)

  28  * @run main/othervm/timeout=15 TestKeyPairGenerator
  29  */
  30 
  31 // this fix is really a performance fix, so this test is not foolproof
  32 // without it, it will take a minute or more (unless you have a very fast machine)
  33 // with the fix, the test should complete in <2 seconds
  34 // use 15 second timeout to leave some room


  35 
  36 import java.security.*;
  37 import java.security.interfaces.*;
  38 
  39 public class TestKeyPairGenerator {
  40 
  41     private static void checkKeyLength(KeyPair kp, int len) throws Exception {
  42         DSAPublicKey key = (DSAPublicKey)kp.getPublic();
  43         int n = key.getParams().getP().bitLength();
  44         System.out.println("Key length: " + n);
  45         if (len != n) {
  46             throw new Exception("Wrong key length");
  47         }
  48     }
  49 
  50     public static void main(String[] args) throws Exception {
  51         long start = System.currentTimeMillis();
  52         KeyPairGenerator kpg;
  53         KeyPair kp;
  54         // problem was when not calling initialize()


  65         // some other basic tests
  66         kp = kpg.generateKeyPair();
  67         checkKeyLength(kp, 1024);
  68 
  69         kpg.initialize(1024);
  70         kp = kpg.generateKeyPair();
  71         checkKeyLength(kp, 1024);
  72 
  73         kpg.initialize(768);
  74         kp = kpg.generateKeyPair();
  75         checkKeyLength(kp, 768);
  76 
  77         kpg.initialize(512);
  78         kp = kpg.generateKeyPair();
  79         checkKeyLength(kp, 512);
  80 
  81         kpg.initialize(2048);
  82         kp = kpg.generateKeyPair();
  83         checkKeyLength(kp, 2048);
  84 




  85         long stop = System.currentTimeMillis();
  86         System.out.println("Time: " + (stop - start) + " ms.");
  87     }
  88 
  89 }
   1 /*
   2  * Copyright (c) 2003, 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 4800108 8072452
  27  * @summary verify that precomputed DSA parameters are always used (512, 768,
  28  *          1024, 2048, 3072 bit)
  29  * @run main/othervm/timeout=15 TestKeyPairGenerator
  30  */
  31 
  32 //
  33 // This fix is really a performance fix, so this test is not foolproof.
  34 // Without the precomputed parameters, it will take a minute or more
  35 // (unless you have a very fast machine).  With the fix, the test should
  36 // complete in less than 2 seconds.  Use 15 second timeout to leave some room.
  37 //
  38 
  39 import java.security.*;
  40 import java.security.interfaces.*;
  41 
  42 public class TestKeyPairGenerator {
  43 
  44     private static void checkKeyLength(KeyPair kp, int len) throws Exception {
  45         DSAPublicKey key = (DSAPublicKey)kp.getPublic();
  46         int n = key.getParams().getP().bitLength();
  47         System.out.println("Key length: " + n);
  48         if (len != n) {
  49             throw new Exception("Wrong key length");
  50         }
  51     }
  52 
  53     public static void main(String[] args) throws Exception {
  54         long start = System.currentTimeMillis();
  55         KeyPairGenerator kpg;
  56         KeyPair kp;
  57         // problem was when not calling initialize()


  68         // some other basic tests
  69         kp = kpg.generateKeyPair();
  70         checkKeyLength(kp, 1024);
  71 
  72         kpg.initialize(1024);
  73         kp = kpg.generateKeyPair();
  74         checkKeyLength(kp, 1024);
  75 
  76         kpg.initialize(768);
  77         kp = kpg.generateKeyPair();
  78         checkKeyLength(kp, 768);
  79 
  80         kpg.initialize(512);
  81         kp = kpg.generateKeyPair();
  82         checkKeyLength(kp, 512);
  83 
  84         kpg.initialize(2048);
  85         kp = kpg.generateKeyPair();
  86         checkKeyLength(kp, 2048);
  87 
  88         kpg.initialize(3072);
  89         kp = kpg.generateKeyPair();
  90         checkKeyLength(kp, 3072);
  91 
  92         long stop = System.currentTimeMillis();
  93         System.out.println("Time: " + (stop - start) + " ms.");
  94     }

  95 }