3030 * @key randomness
3131 * @modules jdk.crypto.cryptoki
3232 * @run main/othervm TestSymmCiphers
33- */
33+ */
3434
3535import jtreg .SkippedException ;
3636
3939import java .security .AlgorithmParameters ;
4040import java .security .NoSuchAlgorithmException ;
4141import java .security .Provider ;
42+ import java .util .ArrayList ;
4243import java .util .List ;
4344import java .util .Random ;
4445import javax .crypto .Cipher ;
4748
4849public class TestSymmCiphers extends PKCS11Test {
4950
51+ private record CI (String transformation , String keyAlgo , int dataSize ){} // record for holding Cipher Information
52+
53+ private static final CI [] TEST_LIST = {
54+ new CI ("ARCFOUR" , "ARCFOUR" , 400 ),
55+ new CI ("RC4" , "RC4" , 401 ),
56+ new CI ("DES/CBC/NoPadding" , "DES" , 400 ),
57+ new CI ("DESede/CBC/NoPadding" , "DESede" , 160 ),
58+ new CI ("AES/CBC/NoPadding" , "AES" , 4800 ),
59+ new CI ("Blowfish/CBC/NoPadding" , "Blowfish" , 24 ),
60+ new CI ("DES/cbc/PKCS5Padding" , "DES" , 6401 ),
61+ new CI ("DESede/CBC/PKCS5Padding" , "DESede" , 402 ),
62+ new CI ("AES/CBC/PKCS5Padding" , "AES" , 30 ),
63+ new CI ("Blowfish/CBC/PKCS5Padding" , "Blowfish" , 19 ),
64+ new CI ("DES/ECB/NoPadding" , "DES" , 400 ),
65+ new CI ("DESede/ECB/NoPadding" , "DESede" , 160 ),
66+ new CI ("AES/ECB/NoPadding" , "AES" , 4800 ),
67+ new CI ("DES/ECB/PKCS5Padding" , "DES" , 32 ),
68+ new CI ("DES/ECB/PKCS5Padding" , "DES" , 6400 ),
69+ new CI ("DESede/ECB/PKCS5Padding" , "DESede" , 400 ),
70+ new CI ("AES/ECB/PKCS5Padding" , "AES" , 64 ),
71+
72+ new CI ("DES" , "DES" , 6400 ),
73+ new CI ("DESede" , "DESede" , 408 ),
74+ new CI ("AES" , "AES" , 128 ),
75+
76+ new CI ("AES/CTR/NoPadding" , "AES" , 3200 ),
77+ new CI ("AES/CTS/NoPadding" , "AES" , 3200 ),
78+
79+ };
5080 private static final StringBuffer debugBuf = new StringBuffer ();
5181
52- private final String transformation ;
53- private final String keyAlgo ;
54- private final int dataSize ;
55-
56- public TestSymmCiphers (String transformation ,
57- String keyAlgo ,
58- int dataSize ) {
59- this .transformation = transformation ;
60- this .keyAlgo = keyAlgo ;
61- this .dataSize = dataSize ;
62- }
63-
6482 @ Override
6583 public void main (Provider p ) throws Exception {
6684 // NSS reports CKR_DEVICE_ERROR when the data passed to
6785 // its EncryptUpdate/DecryptUpdate is not multiple of blocks
6886 int firstBlkSize = 16 ;
87+ List <CI > skippedList = new ArrayList <>();
6988 Random random = new Random ();
7089 try {
71- System .out .println ("===" + transformation + "===" );
72- try {
73- KeyGenerator kg =
74- KeyGenerator .getInstance (keyAlgo , p );
75- SecretKey key = kg .generateKey ();
76- Cipher c1 = Cipher .getInstance (transformation , p );
77- Cipher c2 = Cipher .getInstance (transformation ,
78- System .getProperty ("test.provider.name" , "SunJCE" ));
79-
80- byte [] plainTxt = new byte [dataSize ];
81- random .nextBytes (plainTxt );
82- System .out .println ("Testing inLen = " + plainTxt .length );
83-
84- c2 .init (Cipher .ENCRYPT_MODE , key );
85- AlgorithmParameters params = c2 .getParameters ();
86- byte [] answer = c2 .doFinal (plainTxt );
87- System .out .println ("Encryption tests: START" );
88- test (c1 , Cipher .ENCRYPT_MODE , key , params , firstBlkSize ,
89- plainTxt , answer );
90- System .out .println ("Encryption tests: DONE" );
91- c2 .init (Cipher .DECRYPT_MODE , key , params );
92- byte [] answer2 = c2 .doFinal (answer );
93- System .out .println ("Decryption tests: START" );
94- test (c1 , Cipher .DECRYPT_MODE , key , params , firstBlkSize ,
95- answer , answer2 );
96- System .out .println ("Decryption tests: DONE" );
97- } catch (NoSuchAlgorithmException nsae ) {
98- throw new SkippedException ("Skipping unsupported algorithm: " +
99- nsae );
90+ for (CI currTest : TEST_LIST ) {
91+ System .out .println ("===" + currTest .transformation + "===" );
92+ try {
93+ KeyGenerator kg =
94+ KeyGenerator .getInstance (currTest .keyAlgo , p );
95+ SecretKey key = kg .generateKey ();
96+ Cipher c1 = Cipher .getInstance (currTest .transformation , p );
97+ Cipher c2 = Cipher .getInstance (currTest .transformation ,
98+ System .getProperty ("test.provider.name" , "SunJCE" ));
99+
100+ byte [] plainTxt = new byte [currTest .dataSize ];
101+ random .nextBytes (plainTxt );
102+ System .out .println ("Testing inLen = " + plainTxt .length );
103+
104+ c2 .init (Cipher .ENCRYPT_MODE , key );
105+ AlgorithmParameters params = c2 .getParameters ();
106+ byte [] answer = c2 .doFinal (plainTxt );
107+ System .out .println ("Encryption tests: START" );
108+ test (c1 , Cipher .ENCRYPT_MODE , key , params , firstBlkSize ,
109+ plainTxt , answer );
110+ System .out .println ("Encryption tests: DONE" );
111+ c2 .init (Cipher .DECRYPT_MODE , key , params );
112+ byte [] answer2 = c2 .doFinal (answer );
113+ System .out .println ("Decryption tests: START" );
114+ test (c1 , Cipher .DECRYPT_MODE , key , params , firstBlkSize ,
115+ answer , answer2 );
116+ System .out .println ("Decryption tests: DONE" );
117+ } catch (NoSuchAlgorithmException nsae ) {
118+ System .out .println ("Skipping unsupported algorithm: " +
119+ nsae );
120+ skippedList .add (currTest );
121+ }
100122 }
101123 } catch (Exception ex ) {
102124 // print out debug info when exception is encountered
103125 System .out .println (debugBuf );
104126 throw ex ;
105127 }
128+
129+ if (!skippedList .isEmpty ()){
130+ throw new SkippedException ("Some tests failed: " + skippedList );
131+ }
106132 }
107133
108134 private static void test (Cipher cipher , int mode , SecretKey key ,
@@ -112,6 +138,7 @@ private static void test(Cipher cipher, int mode, SecretKey key,
112138 long startTime , endTime ;
113139 cipher .init (mode , key , params );
114140 int outLen = cipher .getOutputSize (in .length );
141+ //debugOut("Estimated output size = " + outLen + "\n");
115142
116143 // test data preparation
117144 ByteBuffer inBuf = ByteBuffer .allocate (in .length );
@@ -124,6 +151,8 @@ private static void test(Cipher cipher, int mode, SecretKey key,
124151 ByteBuffer outDirectBuf = ByteBuffer .allocateDirect (outLen );
125152
126153 // test#1: byte[] in + byte[] out
154+ //debugOut("Test#1:\n");
155+
127156 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
128157
129158 startTime = System .nanoTime ();
@@ -141,6 +170,10 @@ private static void test(Cipher cipher, int mode, SecretKey key,
141170 match (testOut1 , answer );
142171
143172 // test#2: Non-direct Buffer in + non-direct Buffer out
173+ //debugOut("Test#2:\n");
174+ //debugOut("inputBuf: " + inBuf + "\n");
175+ //debugOut("outputBuf: " + outBuf + "\n");
176+
144177 startTime = System .nanoTime ();
145178 cipher .update (inBuf , outBuf );
146179 cipher .doFinal (inBuf , outBuf );
@@ -149,17 +182,26 @@ private static void test(Cipher cipher, int mode, SecretKey key,
149182 match (outBuf , answer );
150183
151184 // test#3: Direct Buffer in + direc Buffer out
185+ //debugOut("Test#3:\n");
186+ //debugOut("(pre) inputBuf: " + inDirectBuf + "\n");
187+ //debugOut("(pre) outputBuf: " + outDirectBuf + "\n");
188+
152189 startTime = System .nanoTime ();
153190 cipher .update (inDirectBuf , outDirectBuf );
154191 cipher .doFinal (inDirectBuf , outDirectBuf );
155192 endTime = System .nanoTime ();
156193 perfOut ("direct InBuf + direct OutBuf" , endTime - startTime );
157194
195+ //debugOut("(post) inputBuf: " + inDirectBuf + "\n");
196+ //debugOut("(post) outputBuf: " + outDirectBuf + "\n");
158197 match (outDirectBuf , answer );
159198
160199 // test#4: Direct Buffer in + non-direct Buffer out
200+ //debugOut("Test#4:\n");
161201 inDirectBuf .position (0 );
162202 outBuf .position (0 );
203+ //debugOut("inputBuf: " + inDirectBuf + "\n");
204+ //debugOut("outputBuf: " + outBuf + "\n");
163205
164206 startTime = System .nanoTime ();
165207 cipher .update (inDirectBuf , outBuf );
@@ -169,15 +211,21 @@ private static void test(Cipher cipher, int mode, SecretKey key,
169211 match (outBuf , answer );
170212
171213 // test#5: Non-direct Buffer in + direct Buffer out
214+ //debugOut("Test#5:\n");
172215 inBuf .position (0 );
173216 outDirectBuf .position (0 );
174217
218+ //debugOut("(pre) inputBuf: " + inBuf + "\n");
219+ //debugOut("(pre) outputBuf: " + outDirectBuf + "\n");
220+
175221 startTime = System .nanoTime ();
176222 cipher .update (inBuf , outDirectBuf );
177223 cipher .doFinal (inBuf , outDirectBuf );
178224 endTime = System .nanoTime ();
179225 perfOut ("non-direct InBuf + direct OutBuf" , endTime - startTime );
180226
227+ //debugOut("(post) inputBuf: " + inBuf + "\n");
228+ //debugOut("(post) outputBuf: " + outDirectBuf + "\n");
181229 match (outDirectBuf , answer );
182230
183231 debugBuf .setLength (0 );
@@ -215,47 +263,6 @@ private static void match(ByteBuffer bb, byte[] answer) throws Exception {
215263 }
216264
217265 public static void main (String [] args ) throws Exception {
218-
219- final List <String []> tests = List .of (
220- new String []{"ARCFOUR" , "ARCFOUR" , "400" },
221- new String []{"RC4" , "RC4" , "401" },
222- new String []{"DES/CBC/NoPadding" , "DES" , "400" },
223- new String []{"DESede/CBC/NoPadding" , "DESede" , "160" },
224- new String []{"AES/CBC/NoPadding" , "AES" , "4800" },
225- new String []{"Blowfish/CBC/NoPadding" , "Blowfish" , "24" },
226- new String []{"DES/cbc/PKCS5Padding" , "DES" , "6401" },
227- new String []{"DESede/CBC/PKCS5Padding" , "DESede" , "402" },
228- new String []{"AES/CBC/PKCS5Padding" , "AES" , "30" },
229- new String []{"Blowfish/CBC/PKCS5Padding" , "Blowfish" , "19" },
230- new String []{"DES/ECB/NoPadding" , "DES" , "400" },
231- new String []{"DESede/ECB/NoPadding" , "DESede" , "160" },
232- new String []{"AES/ECB/NoPadding" , "AES" , "4800" },
233- new String []{"DES/ECB/PKCS5Padding" , "DES" , "32" },
234- new String []{"DES/ECB/PKCS5Padding" , "DES" , "6400" },
235- new String []{"DESede/ECB/PKCS5Padding" , "DESede" , "400" },
236- new String []{"AES/ECB/PKCS5Padding" , "AES" , "64" },
237-
238- new String []{"DES" , "DES" , "6400" },
239- new String []{"DESede" , "DESede" , "408" },
240- new String []{"AES" , "AES" , "128" },
241-
242- new String []{"AES/CTR/NoPadding" , "AES" , "3200" },
243- new String []{"AES/CTS/NoPadding" , "AES" , "3200" }
244-
245- );
246-
247- boolean skipEncountered = false ;
248- for (final String [] t : tests ) {
249- try {
250- main (new TestSymmCiphers (t [0 ], t [1 ], Integer .parseInt (t [2 ])), args );
251- } catch (SkippedException skippedException ) {
252- skippedException .printStackTrace (System .err );
253- skipEncountered = true ;
254- }
255- }
256-
257- if (skipEncountered ) {
258- throw new SkippedException ("One or more tests skipped" );
259- }
266+ main (new TestSymmCiphers (), args );
260267 }
261268}
0 commit comments