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