jdk/test/sun/security/provider/DSA/TestDSA2.java
author valeriep
Tue, 04 Sep 2012 18:41:06 -0700
changeset 13672 604588823b5a
child 28262 ca047b87ba96
permissions -rw-r--r--
7044060: Need to support NSA Suite B Cryptography algorithms Summary: Add support for DSA parameter generation and OIDs for NSA Suite B algorithms. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     1
/*
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     4
 *
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     8
 *
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    13
 * accompanied this code).
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    14
 *
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    18
 *
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    21
 * questions.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    22
 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    23
/*
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    24
 * @test
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    25
 * @bug 7044060
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    26
 * @run main/othervm/timeout=250 TestDSA2
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    27
 * @summary verify that DSA signature works using SHA and SHA-224 and SHA-256 digests.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    28
 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    29
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    30
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    31
import java.security.*;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    32
import java.security.spec.*;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    33
import java.security.interfaces.*;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    34
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    35
public class TestDSA2 {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    36
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    37
    // NOTE: need to explictly specify provider since the more
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    38
    // preferred provider SunPKCS11 provider only supports up
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    39
    // 1024 bits.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    40
    private static final String PROV = "SUN";
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    41
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    42
    private static final String[] SIG_ALGOS = {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    43
        "SHA1withDSA", "SHA224withDSA", "SHA256withDSA"
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    44
    };
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    45
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    46
    private static final int[] KEYSIZES = {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    47
        1024, 2048
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    48
    };
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    49
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    50
    public static void main(String[] args) throws Exception {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    51
        boolean[] expectedToPass = { true, true, true };
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    52
        test(1024, expectedToPass);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    53
        boolean[] expectedToPass2 = { false, true, true };
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    54
        test(2048, expectedToPass2);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    55
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    56
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    57
    private static void test(int keySize, boolean[] testStatus)
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    58
        throws Exception {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    59
        byte[] data = "1234567890".getBytes();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    60
        System.out.println("Test against key size: " + keySize);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    61
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    62
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", PROV);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    63
        keyGen.initialize(keySize, new SecureRandom());
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    64
        KeyPair pair = keyGen.generateKeyPair();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    65
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    66
        if (testStatus.length != SIG_ALGOS.length) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    67
            throw new RuntimeException("TestError: incorrect status array!");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    68
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    69
        for (int i = 0; i < SIG_ALGOS.length; i++) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    70
            Signature dsa = Signature.getInstance(SIG_ALGOS[i], PROV);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    71
            try {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    72
                dsa.initSign(pair.getPrivate());
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    73
                dsa.update(data);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    74
                byte[] sig = dsa.sign();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    75
                dsa.initVerify(pair.getPublic());
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    76
                dsa.update(data);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    77
                boolean verifies = dsa.verify(sig);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    78
                if (verifies == testStatus[i]) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    79
                    System.out.println(SIG_ALGOS[i] + ": Passed");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    80
                } else {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    81
                    System.out.println(SIG_ALGOS[i] + ": should " +
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    82
                                       (testStatus[i]? "pass":"fail"));
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    83
                    throw new RuntimeException(SIG_ALGOS[i] + ": Unexpected Test result!");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    84
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    85
                }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    86
            } catch (Exception ex) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    87
                if (testStatus[i]) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    88
                    ex.printStackTrace();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    89
                    throw new RuntimeException(SIG_ALGOS[i] + ": Unexpected exception " + ex);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    90
                } else {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    91
                    System.out.println(SIG_ALGOS[i] + ": Passed, expected " + ex);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    92
                }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    93
            }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    94
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    95
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
diff changeset
    96
}