blob: 00691535ac135e66ddd8ab0265a8199c48468170 [file] [log] [blame]
Valerie Penga431f982012-09-04 18:41:06 -07001/*
Jason Uhf7f4ab92015-02-17 10:48:24 -08002 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
Valerie Penga431f982012-09-04 18:41:06 -07003 * 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
Jason Uhf7f4ab92015-02-17 10:48:24 -080025 * @bug 7044060 8042967
Valerie Penga431f982012-09-04 18:41:06 -070026 * @run main/othervm/timeout=250 TestDSA2
Jason Uhf7f4ab92015-02-17 10:48:24 -080027 * @summary verify that DSA signature works using SHA and SHA-224 and
28 * SHA-256 digests.
Joe Darcy86c109e2015-04-29 10:25:53 -070029 * @key randomness
Valerie Penga431f982012-09-04 18:41:06 -070030 */
31
32
33import java.security.*;
34import java.security.spec.*;
35import java.security.interfaces.*;
36
37public 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 = {
Jason Uhf7f4ab92015-02-17 10:48:24 -080045 "NONEwithDSA",
46 "SHA1withDSA",
47 "SHA224withDSA",
48 "SHA256withDSA",
49 "NONEwithDSAinP1363Format",
50 "SHA1withDSAinP1363Format",
51 "SHA224withDSAinP1363Format",
52 "SHA256withDSAinP1363Format"
Valerie Penga431f982012-09-04 18:41:06 -070053 };
54
55 private static final int[] KEYSIZES = {
56 1024, 2048
57 };
58
59 public static void main(String[] args) throws Exception {
Jason Uhf7f4ab92015-02-17 10:48:24 -080060 boolean[] expectedToPass = { true, true, true, true,
61 true, true, true, true };
Valerie Penga431f982012-09-04 18:41:06 -070062 test(1024, expectedToPass);
Jason Uhf7f4ab92015-02-17 10:48:24 -080063 boolean[] expectedToPass2 = { true, true, true, true,
64 true, true, true, true };
Valerie Penga431f982012-09-04 18:41:06 -070065 test(2048, expectedToPass2);
66 }
67
68 private static void test(int keySize, boolean[] testStatus)
Jason Uhf7f4ab92015-02-17 10:48:24 -080069 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();
Valerie Penga431f982012-09-04 18:41:06 -070074 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}