test/compiler/7184394/TestAESBase.java

changeset 9789
e55d4d896e30
parent 9788
44ef77ad417c
child 9806
758c07667682
equal deleted inserted replaced
9788:44ef77ad417c 9789:e55d4d896e30
61 byte[] decode; 61 byte[] decode;
62 byte[] expectedDecode; 62 byte[] expectedDecode;
63 Random random = new Random(0); 63 Random random = new Random(0);
64 Cipher cipher; 64 Cipher cipher;
65 Cipher dCipher; 65 Cipher dCipher;
66 AlgorithmParameters algParams; 66 AlgorithmParameters algParams = null;
67 SecretKey key; 67 SecretKey key;
68 GCMParameterSpec gcm_spec; 68 GCMParameterSpec gcm_spec;
69 byte[] aad; 69 byte[] aad = { 0x11, 0x22, 0x33, 0x44, 0x55 };
70 int tlen = 12; 70 int tlen = 12;
71 byte[] iv; 71 byte[] iv = new byte[16];
72 72
73 static int numThreads = 0; 73 static int numThreads = 0;
74 int threadId; 74 int threadId;
75 static synchronized int getThreadId() { 75 static synchronized int getThreadId() {
76 int id = numThreads; 76 int id = numThreads;
80 80
81 abstract public void run(); 81 abstract public void run();
82 82
83 public void prepare() { 83 public void prepare() {
84 try { 84 try {
85 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 );
86 89
87 if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 ) 90 if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 )
88 testingMisalignment = true; 91 testingMisalignment = true;
89 92
90 int keyLenBytes = (keySize == 0 ? 16 : keySize/8); 93 int keyLenBytes = (keySize == 0 ? 16 : keySize/8);
101 } 104 }
102 105
103 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); 106 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
104 dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); 107 dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
105 108
109 // CBC init
106 if (mode.equals("CBC")) { 110 if (mode.equals("CBC")) {
107 int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0); 111 IvParameterSpec initVector = new IvParameterSpec(iv);
108 IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
109 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
110 } else if (mode.equals("GCM")) { 117 } else if (mode.equals("GCM")) {
111 iv = new byte[64]; 118 gcm_init(true);
112 random.nextBytes(iv); 119 gcm_init(false);
113 aad = new byte[5]; 120
114 random.nextBytes(aad); 121 // ECB init
115 gcm_init();
116 } else { 122 } else {
117 algParams = cipher.getParameters();
118 cipher.init(Cipher.ENCRYPT_MODE, key, algParams); 123 cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
119 } 124 dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
120 algParams = cipher.getParameters(); 125 }
121 dCipher.init(Cipher.DECRYPT_MODE, key, algParams); 126
122 if (threadId == 0) { 127 if (threadId == 0) {
123 childShowCipher(); 128 childShowCipher();
124 } 129 }
125 130
126 inputLength = msgSize + encInputOffset; 131 inputLength = msgSize + encInputOffset;
198 System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm()); 203 System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm());
199 } 204 }
200 205
201 abstract void childShowCipher(); 206 abstract void childShowCipher();
202 207
203 void gcm_init() throws Exception { 208 void gcm_init(boolean encrypt) throws Exception {
204 tlen = 12;
205 gcm_spec = new GCMParameterSpec(tlen * 8, iv); 209 gcm_spec = new GCMParameterSpec(tlen * 8, iv);
206 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); 210 if (encrypt) {
207 cipher.init(Cipher.ENCRYPT_MODE, key, gcm_spec); 211 // Get a new instance everytime because of reuse IV restrictions
208 cipher.update(aad); 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 }
209 } 221 }
210 } 222 }

mercurial