test/compiler/7184394/TestAESBase.java

changeset 9806
758c07667682
parent 6876
710a3c8b516e
parent 9789
e55d4d896e30
equal deleted inserted replaced
9762:c97db0855565 9806:758c07667682
27 */ 27 */
28 28
29 import javax.crypto.Cipher; 29 import javax.crypto.Cipher;
30 import javax.crypto.KeyGenerator; 30 import javax.crypto.KeyGenerator;
31 import javax.crypto.SecretKey; 31 import javax.crypto.SecretKey;
32 import javax.crypto.spec.GCMParameterSpec;
32 import javax.crypto.spec.IvParameterSpec; 33 import javax.crypto.spec.IvParameterSpec;
33 import javax.crypto.spec.SecretKeySpec; 34 import javax.crypto.spec.SecretKeySpec;
34 import java.security.AlgorithmParameters; 35 import java.security.AlgorithmParameters;
35 36
36 import java.util.Random; 37 import java.util.Random;
60 byte[] decode; 61 byte[] decode;
61 byte[] expectedDecode; 62 byte[] expectedDecode;
62 Random random = new Random(0); 63 Random random = new Random(0);
63 Cipher cipher; 64 Cipher cipher;
64 Cipher dCipher; 65 Cipher dCipher;
65 AlgorithmParameters algParams; 66 AlgorithmParameters algParams = null;
66 SecretKey key; 67 SecretKey key;
68 GCMParameterSpec gcm_spec;
69 byte[] aad = { 0x11, 0x22, 0x33, 0x44, 0x55 };
70 int tlen = 12;
71 byte[] iv = new byte[16];
67 72
68 static int numThreads = 0; 73 static int numThreads = 0;
69 int threadId; 74 int threadId;
70 static synchronized int getThreadId() { 75 static synchronized int getThreadId() {
71 int id = numThreads; 76 int id = numThreads;
75 80
76 abstract public void run(); 81 abstract public void run();
77 82
78 public void prepare() { 83 public void prepare() {
79 try { 84 try {
80 System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", paddingStr=" + paddingStr + ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit + ", checkOutput=" + checkOutput + ", encInputOffset=" + encInputOffset + ", encOutputOffset=" + encOutputOffset + ", decOutputOffset=" + decOutputOffset + ", lastChunkSize=" +lastChunkSize ); 85 System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", paddingStr=" + paddingStr +
86 ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit +
87 ", checkOutput=" + checkOutput + ", encInputOffset=" + encInputOffset + ", encOutputOffset=" +
88 encOutputOffset + ", decOutputOffset=" + decOutputOffset + ", lastChunkSize=" +lastChunkSize );
81 89
82 if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 ) 90 if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 )
83 testingMisalignment = true; 91 testingMisalignment = true;
84 92
85 int keyLenBytes = (keySize == 0 ? 16 : keySize/8); 93 int keyLenBytes = (keySize == 0 ? 16 : keySize/8);
96 } 104 }
97 105
98 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); 106 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
99 dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); 107 dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
100 108
109 // CBC init
101 if (mode.equals("CBC")) { 110 if (mode.equals("CBC")) {
102 int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0); 111 IvParameterSpec initVector = new IvParameterSpec(iv);
103 IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
104 cipher.init(Cipher.ENCRYPT_MODE, key, initVector); 112 cipher.init(Cipher.ENCRYPT_MODE, key, initVector);
113 algParams = cipher.getParameters();
114 dCipher.init(Cipher.DECRYPT_MODE, key, initVector);
115
116 // GCM init
117 } else if (mode.equals("GCM")) {
118 gcm_init(true);
119 gcm_init(false);
120
121 // ECB init
105 } else { 122 } else {
106 algParams = cipher.getParameters();
107 cipher.init(Cipher.ENCRYPT_MODE, key, algParams); 123 cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
108 } 124 dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
109 algParams = cipher.getParameters(); 125 }
110 dCipher.init(Cipher.DECRYPT_MODE, key, algParams); 126
111 if (threadId == 0) { 127 if (threadId == 0) {
112 childShowCipher(); 128 childShowCipher();
113 } 129 }
114 130
115 inputLength = msgSize + encInputOffset; 131 inputLength = msgSize + encInputOffset;
186 System.out.println(kind + " cipher provider: " + cipher.getProvider()); 202 System.out.println(kind + " cipher provider: " + cipher.getProvider());
187 System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm()); 203 System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm());
188 } 204 }
189 205
190 abstract void childShowCipher(); 206 abstract void childShowCipher();
207
208 void gcm_init(boolean encrypt) throws Exception {
209 gcm_spec = new GCMParameterSpec(tlen * 8, iv);
210 if (encrypt) {
211 // Get a new instance everytime because of reuse IV restrictions
212 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
213 cipher.init(Cipher.ENCRYPT_MODE, key, gcm_spec);
214 cipher.updateAAD(aad);
215 } else {
216 dCipher.init(Cipher.DECRYPT_MODE, key, gcm_spec);
217 dCipher.updateAAD(aad);
218
219
220 }
221 }
191 } 222 }

mercurial