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