jdk/test/com/sun/crypto/provider/Cipher/DES/PerformanceTest.java
author bgopularam
Wed, 20 Jan 2016 09:21:57 -0800
changeset 35301 12dabfdd1997
parent 31422 67cd03bc9cce
child 45837 bc6fdf743662
permissions -rw-r--r--
8133085: Avoid creating instances of security providers when possible Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35301
12dabfdd1997 8133085: Avoid creating instances of security providers when possible
bgopularam
parents: 31422
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 0000000
23357
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    27
 * @summary This test checks performance of various ciphers.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Jan Luehe
23357
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    29
 * @run main/manual PerformanceTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class PerformanceTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    IvParameterSpec params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    SecretKey cipherKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    StringBuffer col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static byte[] key = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static byte[] key3 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        (byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        (byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        (byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        (byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static byte[] iv  = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        (byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        (byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static byte[] plain_data = "Isaiah, a little boy is dead. He fell from the roof of an apartment house, just a few days before Christmas. The only person who truly grieves for the boy is Smilla Jasperson, a polar researcher. Smilla was Isaiah's only friend. Isaiah came with his alcoholic mother from Greenland but never really got used to life in Copenhagen. Smilla's mother was also from Greenland. Smilla feels a particular affinity to the arctic landscape. She knows a lot about the snow and how to read its movements and is convinced that the boy's death was not accidental.".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // The codemgrtool won't let me checkin this line, so I had to break it up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* Smilla decides to embark upon her own investigations but soon finds  herself running up against a brick wall. Isaiah's mother, Juliane, mistrusts her and the authorities also make life difficult for Smilla. But she won't let go. Then there's this mechanic ­ a reticent, inscrutable sort of guy ­ who lives in the same apartment building. He also knew the boy and even constructed a workbench for him in his cellar workshop. The mechanic tells Smilla that he'd like to help her, but then again, he may just be one of those who seem to want to dog her heels. End ".getBytes();*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static int[] dataSizes = {1024, 8192};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static String[] crypts = {"DES", "DESede"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static String[] modes = {"ECB", "CBC", "CFB64", "OFB64", "PCBC"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static String[] paddings = {"PKCS5Padding", "NoPadding"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static int[] rounds = {100, 1000};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    int sum =0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        PerformanceTest test = new PerformanceTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        byte[] in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        col = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        printHeadings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (int i=0; i<crypts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            for (int j=0; j<modes.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                for (int k=0; k<paddings.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    col.append(crypts[i]+"/"+modes[j]+"/"+paddings[k]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    int len = 32 - col.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    for(; len>0; len--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        col.append(" "); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        for (int l=0; l<dataSizes.length; l++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                            in = new byte[dataSizes[l]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                            int g = Math.min(dataSizes[l],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                             plain_data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                            for(len=0; len < dataSizes[l] - g; len += g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                System.arraycopy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                    (plain_data, 0, in, len, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                            if ((dataSizes[l] - len) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                System.arraycopy(plain_data, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                                 in, len, dataSizes[l]- len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                            col.append(dataSizes[l]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            len = 40 - col.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            for (; len>0; len--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                col.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                            for (int m=0; m<rounds.length; m++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                col.append(rounds[m]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                len = 50 - col.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                for (; len>0; len--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                    col.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                init(crypts[i], modes[j], paddings[k]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                runTest(in, rounds[m]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                System.out.println(col.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                col.setLength(40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                            col.setLength(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        col.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        col.append("Average:                                            " + (sum/(dataSizes.length*rounds.length)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        System.out.println(col.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        col.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void init(String crypt, String mode, String padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        KeySpec desKeySpec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        SecretKeyFactory factory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        StringBuffer cipherName = new StringBuffer(crypt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (mode.length() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            cipherName.append("/" + mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (padding.length() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            cipherName.append("/" + padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 23357
diff changeset
   152
        cipher = Cipher.getInstance(cipherName.toString(), "SunJCE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (crypt.endsWith("ede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            desKeySpec = new DESedeKeySpec(key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            factory = SecretKeyFactory.getInstance("DESede", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            desKeySpec = new DESKeySpec(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            factory = SecretKeyFactory.getInstance("DES", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // retrieve the cipher key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        cipherKey = factory.generateSecret(desKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // retrieve iv
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if ( !mode.equals("ECB"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            params = new IvParameterSpec(iv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public void runTest(byte[] data, int count) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        long start, end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        cipher.init(Cipher.ENCRYPT_MODE, cipherKey, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for (int i=0; i<count-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            cipher.update(data, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        cipher.doFinal(data, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        end = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int speed = (int)((data.length * count)/(end - start));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        sum += speed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        col.append(speed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public void printHeadings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            ("The following tests numbers are generated using");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            ("our JCA calling through Cipher to a particular provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            ("=========================================================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            ("Algorithm                      DataSize Rounds Kbytes/sec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}