author | bgopularam |
Wed, 20 Jan 2016 09:21:57 -0800 | |
changeset 35301 | 12dabfdd1997 |
parent 31422 | 67cd03bc9cce |
child 45837 | bc6fdf743662 |
permissions | -rw-r--r-- |
2 | 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 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
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 | 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 | 30 |
*/ |
31 |
import java.security.*; |
|
32 |
import java.security.spec.*; |
|
33 |
import java.io.*; |
|
34 |
import javax.crypto.*; |
|
35 |
import javax.crypto.spec.*; |
|
36 |
||
37 |
public class PerformanceTest { |
|
38 |
||
39 |
Cipher cipher; |
|
40 |
IvParameterSpec params = null; |
|
41 |
SecretKey cipherKey = null; |
|
42 |
StringBuffer col; |
|
43 |
||
44 |
public static byte[] key = { |
|
45 |
(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67, |
|
46 |
(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef |
|
47 |
}; |
|
48 |
||
49 |
public static byte[] key3 = { |
|
50 |
(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67, |
|
51 |
(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef, |
|
52 |
(byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3, |
|
53 |
(byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87, |
|
54 |
(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98, |
|
55 |
(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10}; |
|
56 |
||
57 |
public static byte[] iv = { |
|
58 |
(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98, |
|
59 |
(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10}; |
|
60 |
||
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(); |
|
62 |
||
63 |
// The codemgrtool won't let me checkin this line, so I had to break it up. |
|
64 |
||
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();*/ |
|
66 |
||
67 |
static int[] dataSizes = {1024, 8192}; |
|
68 |
static String[] crypts = {"DES", "DESede"}; |
|
69 |
static String[] modes = {"ECB", "CBC", "CFB64", "OFB64", "PCBC"}; |
|
70 |
static String[] paddings = {"PKCS5Padding", "NoPadding"}; |
|
71 |
static int[] rounds = {100, 1000}; |
|
72 |
int sum =0; |
|
73 |
||
74 |
public static void main(String[] args) throws Exception { |
|
75 |
PerformanceTest test = new PerformanceTest(); |
|
76 |
test.run(); |
|
77 |
} |
|
78 |
||
79 |
public void run() throws Exception { |
|
80 |
||
81 |
byte[] in; |
|
82 |
||
83 |
col = new StringBuffer(); |
|
84 |
||
85 |
printHeadings(); |
|
86 |
||
87 |
for (int i=0; i<crypts.length; i++) { |
|
88 |
for (int j=0; j<modes.length; j++) { |
|
89 |
for (int k=0; k<paddings.length; k++) { |
|
90 |
||
91 |
col.append(crypts[i]+"/"+modes[j]+"/"+paddings[k]); |
|
92 |
int len = 32 - col.length(); |
|
93 |
for(; len>0; len--) |
|
94 |
col.append(" "); { |
|
95 |
||
96 |
for (int l=0; l<dataSizes.length; l++) { |
|
97 |
||
98 |
in = new byte[dataSizes[l]]; |
|
99 |
int g = Math.min(dataSizes[l], |
|
100 |
plain_data.length); |
|
101 |
for(len=0; len < dataSizes[l] - g; len += g) { |
|
102 |
System.arraycopy |
|
103 |
(plain_data, 0, in, len, g); |
|
104 |
} |
|
105 |
||
106 |
if ((dataSizes[l] - len) > 0) |
|
107 |
System.arraycopy(plain_data, 0, |
|
108 |
in, len, dataSizes[l]- len); |
|
109 |
||
110 |
col.append(dataSizes[l]); |
|
111 |
len = 40 - col.length(); |
|
112 |
for (; len>0; len--) |
|
113 |
col.append(" "); |
|
114 |
||
115 |
for (int m=0; m<rounds.length; m++) { |
|
116 |
||
117 |
col.append(rounds[m]); |
|
118 |
len = 50 - col.length(); |
|
119 |
for (; len>0; len--) |
|
120 |
col.append(" "); |
|
121 |
||
122 |
init(crypts[i], modes[j], paddings[k]); |
|
123 |
runTest(in, rounds[m]); |
|
124 |
System.out.println(col.toString()); |
|
125 |
col.setLength(40); |
|
126 |
} |
|
127 |
col.setLength(32); |
|
128 |
} |
|
129 |
col.setLength(0); |
|
130 |
col.append("Average: " + (sum/(dataSizes.length*rounds.length))); |
|
131 |
sum = 0; |
|
132 |
System.out.println(col.toString() + "\n"); |
|
133 |
col.setLength(0); |
|
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
public void init(String crypt, String mode, String padding) |
|
141 |
throws Exception { |
|
142 |
||
143 |
KeySpec desKeySpec = null; |
|
144 |
SecretKeyFactory factory = null; |
|
145 |
||
146 |
StringBuffer cipherName = new StringBuffer(crypt); |
|
147 |
if (mode.length() != 0) |
|
148 |
cipherName.append("/" + mode); |
|
149 |
if (padding.length() != 0) |
|
150 |
cipherName.append("/" + padding); |
|
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 | 153 |
if (crypt.endsWith("ede")) { |
154 |
desKeySpec = new DESedeKeySpec(key3); |
|
155 |
factory = SecretKeyFactory.getInstance("DESede", "SunJCE"); |
|
156 |
} |
|
157 |
else { |
|
158 |
desKeySpec = new DESKeySpec(key); |
|
159 |
factory = SecretKeyFactory.getInstance("DES", "SunJCE"); |
|
160 |
} |
|
161 |
||
162 |
// retrieve the cipher key |
|
163 |
cipherKey = factory.generateSecret(desKeySpec); |
|
164 |
||
165 |
// retrieve iv |
|
166 |
if ( !mode.equals("ECB")) |
|
167 |
params = new IvParameterSpec(iv); |
|
168 |
else |
|
169 |
params = null; |
|
170 |
} |
|
171 |
||
172 |
public void runTest(byte[] data, int count) throws Exception { |
|
173 |
||
174 |
long start, end; |
|
175 |
cipher.init(Cipher.ENCRYPT_MODE, cipherKey, params); |
|
176 |
||
177 |
start = System.currentTimeMillis(); |
|
178 |
for (int i=0; i<count-1; i++) { |
|
179 |
cipher.update(data, 0, data.length); |
|
180 |
} |
|
181 |
cipher.doFinal(data, 0, data.length); |
|
182 |
end = System.currentTimeMillis(); |
|
183 |
||
184 |
int speed = (int)((data.length * count)/(end - start)); |
|
185 |
sum += speed; |
|
186 |
col.append(speed); |
|
187 |
} |
|
188 |
||
189 |
public void printHeadings() { |
|
190 |
System.out.println |
|
191 |
("The following tests numbers are generated using"); |
|
192 |
System.out.println |
|
193 |
("our JCA calling through Cipher to a particular provider"); |
|
194 |
System.out.println |
|
195 |
("========================================================="); |
|
196 |
System.out.println |
|
197 |
("Algorithm DataSize Rounds Kbytes/sec"); |
|
198 |
||
199 |
} |
|
200 |
||
201 |
} |