1 /*
   2  * Copyright (c) 2012, 2015, 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  * @test
  25  * @bug 7044060 8042967
  26  * @run main/othervm/timeout=250 TestDSA2
  27  * @summary verify that DSA signature works using SHA and SHA-224 and
  28  *          SHA-256 digests.
  29  * @key randomness
  30  */
  31 
  32 
  33 import java.security.*;
  34 import java.security.spec.*;
  35 import java.security.interfaces.*;
  36 
  37 public class TestDSA2 {
  38 
  39     // NOTE: need to explictly specify provider since the more
  40     // preferred provider SunPKCS11 provider only supports up
  41     // 1024 bits.
  42     private static final String PROV = "SUN";
  43 
  44     private static final String[] SIG_ALGOS = {
  45         "NONEwithDSA",
  46         "SHA1withDSA",
  47         "SHA224withDSA",
  48         "SHA256withDSA",
  49         "NONEwithDSAinP1363Format",
  50         "SHA1withDSAinP1363Format",
  51         "SHA224withDSAinP1363Format",
  52         "SHA256withDSAinP1363Format"
  53     };
  54 
  55     private static final int[] KEYSIZES = {
  56         1024, 2048
  57     };
  58 
  59     public static void main(String[] args) throws Exception {
  60         boolean[] expectedToPass = { true, true, true, true,
  61                                      true, true, true, true };
  62         test(1024, expectedToPass);
  63         boolean[] expectedToPass2 = { true, true, true, true,
  64                                       true, true, true, true };
  65         test(2048, expectedToPass2);
  66     }
  67 
  68     private static void test(int keySize, boolean[] testStatus)
  69             throws Exception {
  70         // Raw DSA requires the data to be exactly 20 bytes long. Use a
  71         // 20-byte array for these tests so that the NONEwithDSA* algorithms
  72         // don't complain.
  73         byte[] data = "12345678901234567890".getBytes();
  74         System.out.println("Test against key size: " + keySize);
  75 
  76         KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", PROV);
  77         keyGen.initialize(keySize, new SecureRandom());
  78         KeyPair pair = keyGen.generateKeyPair();
  79 
  80         if (testStatus.length != SIG_ALGOS.length) {
  81             throw new RuntimeException("TestError: incorrect status array!");
  82         }
  83         for (int i = 0; i < SIG_ALGOS.length; i++) {
  84             Signature dsa = Signature.getInstance(SIG_ALGOS[i], PROV);
  85             try {
  86                 dsa.initSign(pair.getPrivate());
  87                 dsa.update(data);
  88                 byte[] sig = dsa.sign();
  89                 dsa.initVerify(pair.getPublic());
  90                 dsa.update(data);
  91                 boolean verifies = dsa.verify(sig);
  92                 if (verifies == testStatus[i]) {
  93                     System.out.println(SIG_ALGOS[i] + ": Passed");
  94                 } else {
  95                     System.out.println(SIG_ALGOS[i] + ": should " +
  96                                        (testStatus[i]? "pass":"fail"));
  97                     throw new RuntimeException(SIG_ALGOS[i] + ": Unexpected Test result!");
  98 
  99                 }
 100             } catch (Exception ex) {
 101                 if (testStatus[i]) {
 102                     ex.printStackTrace();
 103                     throw new RuntimeException(SIG_ALGOS[i] + ": Unexpected exception " + ex);
 104                 } else {
 105                     System.out.println(SIG_ALGOS[i] + ": Passed, expected " + ex);
 106                 }
 107             }
 108         }
 109     }
 110 }