|
41 | 41 | import java.security.AlgorithmParameters; |
42 | 42 | import java.security.NoSuchAlgorithmException; |
43 | 43 | import java.security.Provider; |
| 44 | +import java.util.ArrayList; |
44 | 45 | import java.util.List; |
45 | 46 | import java.util.Random; |
46 | 47 | import javax.crypto.Cipher; |
|
50 | 51 |
|
51 | 52 | public class TestSymmCiphersNoPad extends PKCS11Test { |
52 | 53 |
|
53 | | - private static final StringBuffer debugBuf = new StringBuffer(); |
| 54 | + private record CI (String transformation, String keyAlgo, int dataSize){} // record for holding Cipher Information |
54 | 55 |
|
55 | | - private final String transformation; |
56 | | - private final String keyAlgo; |
57 | | - private final int dataSize; |
| 56 | + private static final StringBuffer debugBuf = new StringBuffer(); |
58 | 57 |
|
59 | | - public TestSymmCiphersNoPad(String transformation, |
60 | | - String keyAlgo, |
61 | | - int dataSize) { |
62 | | - this.transformation = transformation; |
63 | | - this.keyAlgo = keyAlgo; |
64 | | - this.dataSize = dataSize; |
65 | | - } |
| 58 | + private static final CI[] TEST_LIST = { |
| 59 | + new CI("ARCFOUR", "ARCFOUR", 400), |
| 60 | + new CI("RC4", "RC4", 401), |
| 61 | + new CI("DES/CBC/NoPadding", "DES", 400), |
| 62 | + new CI("DESede/CBC/NoPadding", "DESede", 160), |
| 63 | + new CI("AES/CBC/NoPadding", "AES", 4800), |
| 64 | + new CI("Blowfish/CBC/NoPadding", "Blowfish", 24), |
| 65 | + new CI("AES/CTR/NoPadding", "AES", 1600), |
| 66 | + new CI("AES/CTR/NoPadding", "AES", 65), |
| 67 | + new CI("AES/CTS/NoPadding", "AES", 1600), |
| 68 | + new CI("AES/CTS/NoPadding", "AES", 65), |
| 69 | + }; |
66 | 70 |
|
67 | 71 | @Override |
68 | 72 | public void main(Provider p) throws Exception { |
| 73 | + List<CI> skippedList = new ArrayList<>(); |
69 | 74 | Random random = new Random(); |
70 | 75 | 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 | | - test(c1, Cipher.ENCRYPT_MODE, key, params, |
88 | | - plainTxt, answer); |
89 | | - System.out.println("Encryption tests: DONE"); |
90 | | - c2.init(Cipher.DECRYPT_MODE, key, params); |
91 | | - byte[] answer2 = c2.doFinal(answer); |
92 | | - test(c1, Cipher.DECRYPT_MODE, key, params, |
93 | | - answer, answer2); |
94 | | - System.out.println("Decryption tests: DONE"); |
95 | | - } catch (NoSuchAlgorithmException nsae) { |
96 | | - throw new SkippedException("Skipping unsupported algorithm: " + |
97 | | - nsae); |
| 76 | + for (CI currTest : TEST_LIST) { |
| 77 | + System.out.println("===" + currTest.transformation + "==="); |
| 78 | + try { |
| 79 | + KeyGenerator kg = |
| 80 | + KeyGenerator.getInstance(currTest.keyAlgo, p); |
| 81 | + SecretKey key = kg.generateKey(); |
| 82 | + Cipher c1 = Cipher.getInstance(currTest.transformation, p); |
| 83 | + Cipher c2 = Cipher.getInstance(currTest.transformation, |
| 84 | + System.getProperty("test.provider.name", "SunJCE")); |
| 85 | + |
| 86 | + byte[] plainTxt = new byte[currTest.dataSize]; |
| 87 | + random.nextBytes(plainTxt); |
| 88 | + System.out.println("Testing inLen = " + plainTxt.length); |
| 89 | + |
| 90 | + c2.init(Cipher.ENCRYPT_MODE, key); |
| 91 | + AlgorithmParameters params = c2.getParameters(); |
| 92 | + byte[] answer = c2.doFinal(plainTxt); |
| 93 | + test(c1, Cipher.ENCRYPT_MODE, key, params, |
| 94 | + plainTxt, answer); |
| 95 | + System.out.println("Encryption tests: DONE"); |
| 96 | + c2.init(Cipher.DECRYPT_MODE, key, params); |
| 97 | + byte[] answer2 = c2.doFinal(answer); |
| 98 | + test(c1, Cipher.DECRYPT_MODE, key, params, |
| 99 | + answer, answer2); |
| 100 | + System.out.println("Decryption tests: DONE"); |
| 101 | + } catch (NoSuchAlgorithmException nsae) { |
| 102 | + System.out.println("Skipping unsupported algorithm: " + |
| 103 | + nsae); |
| 104 | + skippedList.add(currTest); |
| 105 | + } |
98 | 106 | } |
99 | 107 | } catch (Exception ex) { |
100 | 108 | // print out debug info when exception is encountered |
101 | 109 | System.out.println(debugBuf); |
102 | 110 | throw ex; |
103 | 111 | } |
| 112 | + |
| 113 | + if (!skippedList.isEmpty()){ |
| 114 | + throw new SkippedException("Some tests skipped: " + skippedList); |
| 115 | + } |
104 | 116 | } |
105 | 117 |
|
106 | 118 | private static void test(Cipher cipher, int mode, SecretKey key, |
@@ -225,32 +237,6 @@ private static void match(ByteBuffer bb, byte[] answer) throws Exception { |
225 | 237 | } |
226 | 238 |
|
227 | 239 | public static void main(String[] args) throws Exception { |
228 | | - |
229 | | - final List<String[]> tests = List.of( |
230 | | - new String[]{"ARCFOUR", "ARCFOUR", "400"}, |
231 | | - new String[]{"RC4", "RC4", "401"}, |
232 | | - new String[]{"DES/CBC/NoPadding", "DES", "400"}, |
233 | | - new String[]{"DESede/CBC/NoPadding", "DESede", "160"}, |
234 | | - new String[]{"AES/CBC/NoPadding", "AES", "4800"}, |
235 | | - new String[]{"Blowfish/CBC/NoPadding", "Blowfish", "24"}, |
236 | | - new String[]{"AES/CTR/NoPadding", "AES", "1600"}, |
237 | | - new String[]{"AES/CTR/NoPadding", "AES", "65"}, |
238 | | - new String[]{"AES/CTS/NoPadding", "AES", "1600"}, |
239 | | - new String[]{"AES/CTS/NoPadding", "AES", "65"} |
240 | | - ); |
241 | | - |
242 | | - boolean skipEncountered = false; |
243 | | - for (final String[] t : tests) { |
244 | | - try { |
245 | | - main(new TestSymmCiphersNoPad(t[0], t[1], Integer.parseInt(t[2])), args); |
246 | | - } catch (SkippedException skippedException) { |
247 | | - skippedException.printStackTrace(System.err); |
248 | | - skipEncountered = true; |
249 | | - } |
250 | | - } |
251 | | - |
252 | | - if (skipEncountered) { |
253 | | - throw new SkippedException("One or more tests skipped"); |
254 | | - } |
| 240 | + main(new TestSymmCiphersNoPad(), args); |
255 | 241 | } |
256 | 242 | } |
0 commit comments