jdk/test/com/oracle/security/ucrypto/TestDigest.java
author valeriep
Thu, 12 Jan 2012 16:04:03 -0800
changeset 11523 85131f3172a1
permissions -rw-r--r--
7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs Summary: Added the OracleUcrypto provider for utilizing the Solaris ucrypto API. Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11523
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
     1
/*
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
     4
 *
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
     8
 *
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    13
 * accompanied this code).
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    14
 *
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    18
 *
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    21
 * questions.
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    22
 */
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    23
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    24
/*
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    25
 * @test
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    26
 * @bug     7088989
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    27
 * @summary Ensure the various message digests works correctly
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    28
 */
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    29
import java.io.*;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    30
import java.security.*;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    31
import java.security.spec.*;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    32
import java.util.*;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    33
import javax.crypto.*;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    34
import javax.crypto.spec.*;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    35
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    36
public class TestDigest extends UcryptoTest {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    37
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    38
    private static final String[] MD_ALGOS = {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    39
        "MD5",
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    40
        "SHA",
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    41
        "SHA-256",
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    42
        "SHA-384",
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    43
        "SHA-512"
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    44
    };
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    45
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    47
        main(new TestDigest(), null);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    48
    }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    49
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    50
    public void doTest(Provider p) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    51
        boolean testPassed = true;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    52
        byte[] msg = new byte[200];
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    53
        (new SecureRandom()).nextBytes(msg);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    54
        String interopProvName = "SUN";
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    55
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    56
        for (String a : MD_ALGOS) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    57
            try {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    58
                MessageDigest md, md2;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    59
                try {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    60
                    md = MessageDigest.getInstance(a, p);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    61
                } catch (NoSuchAlgorithmException nsae) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    62
                    System.out.println("Skipping Unsupported MD algo: " + a);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    63
                    continue;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    64
                }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    65
                md2 = MessageDigest.getInstance(a, interopProvName);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    66
                // Test Interoperability for update+digest calls
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    67
                for (int i = 0; i < 3; i++) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    68
                    md.update(msg);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    69
                    byte[] digest = md.digest();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    70
                    md2.update(msg);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    71
                    byte[] digest2 = md2.digest();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    72
                    if (!Arrays.equals(digest, digest2)) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    73
                        System.out.println("DIFF1 FAILED for: " + a + " at iter " + i);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    74
                        testPassed = false;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    75
                    }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    76
                }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    77
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    78
                // Test Interoperability for digest calls
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    79
                md = MessageDigest.getInstance(a, p);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    80
                md2 = MessageDigest.getInstance(a, interopProvName);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    81
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    82
                for (int i = 0; i < 3; i++) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    83
                    byte[] digest = md.digest();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    84
                    byte[] digest2 = md2.digest();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    85
                    if (!Arrays.equals(digest, digest2)) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    86
                        System.out.println("DIFF2 FAILED for: " + a + " at iter " + i);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    87
                        testPassed = false;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    88
                    }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    89
                }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    90
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    91
                // Test Cloning functionality
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    92
                md = MessageDigest.getInstance(a, p);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    93
                md2 = (MessageDigest) md.clone(); // clone right after construction
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    94
                byte[] digest = md.digest();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    95
                byte[] digest2 = md2.digest();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    96
                if (!Arrays.equals(digest, digest2)) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    97
                    System.out.println("DIFF-3.1 FAILED for: " + a);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    98
                    testPassed = false;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
    99
                }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   100
                md.update(msg);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   101
                md2 = (MessageDigest) md.clone(); // clone again after update call
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   102
                digest = md.digest();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   103
                digest2 = md2.digest();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   104
                if (!Arrays.equals(digest, digest2)) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   105
                    System.out.println("DIFF-3.2 FAILED for: " + a);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   106
                    testPassed = false;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   107
                }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   108
                md2 = (MessageDigest) md.clone(); // clone after digest
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   109
                digest = md.digest();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   110
                digest2 = md2.digest();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   111
                if (!Arrays.equals(digest, digest2)) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   112
                    System.out.println("DIFF-3.3 FAILED for: " + a);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   113
                    testPassed = false;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   114
                }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   115
            } catch(Exception ex) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   116
                System.out.println("Unexpected Exception: " + a);
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   117
                ex.printStackTrace();
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   118
                testPassed = false;
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   119
            }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   120
        }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   121
        if (!testPassed) {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   122
            throw new RuntimeException("One or more MD test failed!");
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   123
        } else {
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   124
            System.out.println("MD Tests Passed");
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   125
        }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   126
    }
85131f3172a1 7088989: Improve the performance for T4 by utilizing the newly provided crypto APIs
valeriep
parents:
diff changeset
   127
}