File: CTSMode.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (364 lines) | stat: -rw-r--r-- 16,616 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 5083253
 * @run main CTSMode
 * @summary Verify that CTR mode works as expected
 * @author Valerie Peng
 */

import java.util.*;
import java.security.AlgorithmParameters;
import javax.crypto.*;
import javax.crypto.spec.*;

public class CTSMode {

    private final static byte[] toByteArray(String s) {
        char[] c = s.toCharArray();
        byte[] t = new byte[c.length / 2];
        int n = 0;
        int d1 = -1;
        int d2 = -1;
        for (int i = 0; i < c.length; i++) {
            char ch = c[i];
            if (d1 == -1) {
                d1 = Character.digit(ch, 16);
            } else {
                d2 = Character.digit(ch, 16);
                if (d2 != -1) {
                    t[n++] = (byte)((d1 << 4) | d2);
                    d1 = -1;
                    d2 = -1;
                }
            }
        }
        if (d1 != -1) {
            throw new RuntimeException();
        }
        if (n == t.length) {
            return t;
        }
        byte[] b = new byte[n];
        System.arraycopy(t, 0, b, 0, n);
        return b;
    }

    private final static SecretKey KEY1 =
        new SecretKeySpec(toByteArray("636869636b656e207465726979616b69"),
                          "AES");

    private final static IvParameterSpec IV1 =
        new IvParameterSpec(new byte[16]);

    /*
     * Use test vectors, i.e. PLAIN1 and CIPHER1, from the appendix B
     * of RFC 3962 "Advanced Encryption Standard (AES) Encryption for
     * Kerberos 5".
     */
    private final static byte[][] PLAIN1 = {
        toByteArray("4920776f756c64206c696b652074686520"),
        toByteArray("4920776f756c64206c696b6520746865" +
                    "2047656e6572616c20476175277320"),
        toByteArray("4920776f756c64206c696b6520746865" +
                    "2047656e6572616c2047617527732043"),
        toByteArray("4920776f756c64206c696b6520746865" +
                    "2047656e6572616c2047617527732043" +
                    "6869636b656e2c20706c656173652c"),
        toByteArray("4920776f756c64206c696b6520746865" +
                    "2047656e6572616c2047617527732043" +
                    "6869636b656e2c20706c656173652c20"),
        toByteArray("4920776f756c64206c696b6520746865" +
                    "2047656e6572616c2047617527732043" +
                    "6869636b656e2c20706c656173652c20" +
                    "616e6420776f6e746f6e20736f75702e")
    };
    private final static byte[][] CIPHER1 = {
        toByteArray("c6353568f2bf8cb4d8a580362da7ff7f97"),
        toByteArray("fc00783e0efdb2c1d445d4c8eff7ed22" +
                    "97687268d6ecccc0c07b25e25ecfe5"),
        toByteArray("39312523a78662d5be7fcbcc98ebf5a8" +
                    "97687268d6ecccc0c07b25e25ecfe584"),
        toByteArray("97687268d6ecccc0c07b25e25ecfe584" +
                    "b3fffd940c16a18c1b5549d2f838029e" +
                    "39312523a78662d5be7fcbcc98ebf5"),
        toByteArray("97687268d6ecccc0c07b25e25ecfe584" +
                    "9dad8bbb96c4cdc03bc103e1a194bbd8" +
                    "39312523a78662d5be7fcbcc98ebf5a8"),
        toByteArray("97687268d6ecccc0c07b25e25ecfe584" +
                    "39312523a78662d5be7fcbcc98ebf5a8" +
                    "4807efe836ee89a526730dbc2f7bc840" +
                    "9dad8bbb96c4cdc03bc103e1a194bbd8"),
    };

    private final static byte[] IV2_SRC =
        toByteArray("11223344556677880011223344556677");

    private final static String[] ALGORITHMS2 = {
        "DES", "DESede", "Blowfish", "AES"
    };
    private final static int[] KEYSIZES2 = {
        8, 24, 16, 16
    };

    private static String toString(byte[] b) {
        StringBuffer sb = new StringBuffer(b.length * 3);
        for (int i = 0; i < b.length; i++) {
            int k = b[i] & 0xff;
            if (i != 0) {
                sb.append(':');
            }
            sb.append(Character.forDigit(k >> 4, 16));
            sb.append(Character.forDigit(k & 0xf, 16));
        }
        return sb.toString();
    }

    public static void main(String[] args) throws Exception {
        test1();
        test2();
        test3();
   }

    /**
     * Test with the test vectors and see if results match.
     */
    private static void test1() throws Exception {
        for (int i = 0; i < PLAIN1.length; i++) {
            String algo = KEY1.getAlgorithm();
            int MAX_KEYSIZE = Cipher.getMaxAllowedKeyLength(algo);
            if (KEY1.getEncoded().length > MAX_KEYSIZE) {
                // skip tests using keys whose length exceeds
                // what's configured in jce jurisdiction policy files.
                continue;
            }
            System.out.println("Running test1_" + i +  " (" + algo + ")");
            Cipher cipher = Cipher.getInstance(algo+ "/CTS/NoPadding",
                                               "SunJCE");
            byte[] plainText = PLAIN1[i];
            byte[] cipherText = CIPHER1[i];
            cipher.init(Cipher.ENCRYPT_MODE, KEY1, IV1);
            byte[] enc = cipher.doFinal(plainText);
            if (Arrays.equals(cipherText, enc) == false) {
                System.out.println("plain:  " + toString(plainText));
                System.out.println("cipher: " + toString(cipherText));
                System.out.println("actual: " + toString(enc));
                throw new RuntimeException("Encryption failure for test " + i);
            }
            cipher.init(Cipher.DECRYPT_MODE, KEY1, IV1);
            byte[] dec = cipher.doFinal(cipherText);
            if (Arrays.equals(plainText, dec) == false) {
                System.out.println("cipher: " + toString(cipherText));
                System.out.println("plain:  " + toString(plainText));
                System.out.println("actual: " + toString(enc));
                throw new RuntimeException("Decryption failure for test " + i);
            }
        }
    }

    /**
     * Test with a combination of update/doFinal calls and make
     * sure that same data is recovered after decryption.
     */
    private static void test2() throws Exception {
        for (int i = 0; i < ALGORITHMS2.length; i++) {
            String algo = ALGORITHMS2[i];
            System.out.println("Running test2_" + i +  " (" + algo + ")");
            int keySize = KEYSIZES2[i];
            int MAX_KEYSIZE = Cipher.getMaxAllowedKeyLength(algo);
            if (keySize > MAX_KEYSIZE) {
                // skip tests using keys whose length exceeds
                // what's configured in jce jurisdiction policy files.
                continue;
            }
            Cipher cipher =
                Cipher.getInstance(algo+"/CTS/NoPadding", "SunJCE");
            int blockSize = cipher.getBlockSize();
            SecretKeySpec key = new SecretKeySpec(new byte[keySize], algo);
            // Make sure encryption works for inputs with valid length
            byte[] plainText = PLAIN1[3];
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] cipherText = new byte[plainText.length];
            int firstPartLen = blockSize + 1;
            int processed1 = cipher.update(plainText, 0, firstPartLen,
                                           cipherText, 0);
            int processed2 = cipher.doFinal(plainText, firstPartLen,
                                            plainText.length-firstPartLen,
                                            cipherText, processed1);
            AlgorithmParameters params = cipher.getParameters();
            if ((processed1 + processed2) != plainText.length) {
                System.out.println("processed1 = " + processed1);
                System.out.println("processed2 = " + processed2);
                System.out.println("total length = " + plainText.length);
                throw new RuntimeException("Encryption failure for test " + i);
            }
            // Make sure IllegalBlockSizeException is thrown for inputs
            // with less-than-a-block length
            try {
                cipher.doFinal(new byte[blockSize-1]);
                throw new RuntimeException("Expected IBSE is not thrown");
            } catch (IllegalBlockSizeException ibse) {
            }
            // Make sure data is encrypted as in CBC mode for inputs
            // which is exactly one block long
            IvParameterSpec iv2 = new IvParameterSpec(IV2_SRC, 0, blockSize);
            cipher.init(Cipher.ENCRYPT_MODE, key, iv2);
            Cipher cipher2 =
                Cipher.getInstance(algo+"/CBC/NoPadding", "SunJCE");
            cipher2.init(Cipher.ENCRYPT_MODE, key, iv2);

            byte[] eout = cipher.doFinal(IV2_SRC, 0, blockSize);
            byte[] eout2 = cipher2.doFinal(IV2_SRC, 0, blockSize);
            if (!Arrays.equals(eout, eout2)) {
                throw new RuntimeException("Different encryption output " +
                                           "for CBC and CTS");
            }
            // Make sure decryption works for inputs with valid length
            cipher.init(Cipher.DECRYPT_MODE, key, params);
            byte[] recoveredText =
                new byte[cipher.getOutputSize(cipherText.length)];
            processed1 = cipher.update(cipherText, 0, firstPartLen,
                                       recoveredText, 0);
            processed2 = cipher.update(cipherText, firstPartLen,
                                       cipherText.length-firstPartLen,
                                       recoveredText, processed1);
            int processed3 =
                cipher.doFinal(recoveredText, processed1+processed2);
            if ((processed1 + processed2 + processed3) != plainText.length) {
                System.out.println("processed1 = " + processed1);
                System.out.println("processed2 = " + processed2);
                System.out.println("processed3 = " + processed3);
                System.out.println("total length = " + plainText.length);
                throw new RuntimeException("Decryption failure for test " + i);
            }
            if (Arrays.equals(plainText, recoveredText) == false) {
                System.out.println("plain:  " + toString(plainText));
                System.out.println("recovered: " + toString(recoveredText));
                throw new RuntimeException("Decryption failure for test " + i);
            }
            // Make sure IllegalBlockSizeException is thrown for inputs
            // with less-than-a-block length
            try {
                cipher.doFinal(new byte[blockSize-1]);
                throw new RuntimeException("Expected IBSE is not thrown");
            } catch (IllegalBlockSizeException ibse) {
            }
            // Make sure data is decrypted as in CBC mode for inputs
            // which is exactly one block long
            cipher.init(Cipher.DECRYPT_MODE, key, iv2);
            cipher2.init(Cipher.DECRYPT_MODE, key, iv2);
            byte[] dout = cipher.doFinal(eout);
            byte[] dout2 = cipher2.doFinal(eout2);
            if (!Arrays.equals(dout, dout2)) {
                throw new RuntimeException("Different decryption output " +
                                           "for CBC and CTS");
            }
        }
    }

    /**
     * Test with a shortBuffer and see if encryption/decryption
     * still works correctly afterwards.
     */
    private static void test3() throws Exception {
        // Skip PLAIN1[0, 1, 2] due to their lengths
        for (int i = 3; i < PLAIN1.length; i++) {
            String algo = KEY1.getAlgorithm();
            System.out.println("Running test3_" + i +  " (" + algo + ")");
            int MAX_KEYSIZE = Cipher.getMaxAllowedKeyLength(algo);
            if (KEY1.getEncoded().length > MAX_KEYSIZE) {
                // skip tests using keys whose length exceeds
                // what's configured in jce jurisdiction policy files.
                continue;
            }
            Cipher cipher =
                Cipher.getInstance(algo+ "/CTS/NoPadding", "SunJCE");
            byte[] plainText = PLAIN1[i];
            byte[] cipherText = CIPHER1[i];
            byte[] enc = new byte[plainText.length];
            cipher.init(Cipher.ENCRYPT_MODE, KEY1, IV1);
            int halfInput = plainText.length/2;
            int processed1 = cipher.update(plainText, 0, halfInput,
                                           enc, 0);
            try {
                cipher.doFinal(plainText, halfInput,
                               plainText.length-halfInput,
                               new byte[1], 0);
                throw new RuntimeException("Expected Exception is not thrown");
            } catch (ShortBufferException sbe) {
                // expected exception thrown; retry
                int processed2 =
                    cipher.doFinal(plainText, halfInput,
                                   plainText.length-halfInput,
                                   enc, processed1);
                if ((processed1 + processed2) != enc.length) {
                    System.out.println("processed1 = " + processed1);
                    System.out.println("processed2 = " + processed2);
                    System.out.println("total length = " + enc.length);
                    throw new RuntimeException("Encryption length check " +
                                               "failed");
                }
            }
            if (Arrays.equals(cipherText, enc) == false) {
                System.out.println("plain:  " + toString(plainText));
                System.out.println("cipher: " + toString(cipherText));
                System.out.println("actual: " + toString(enc));
                throw new RuntimeException("Encryption failure for test " + i);
            }
            cipher.init(Cipher.DECRYPT_MODE, KEY1, IV1);
            byte[] dec =
                new byte[cipher.getOutputSize(cipherText.length)];
            processed1 = cipher.update(cipherText, 0, halfInput,
                                       dec, 0);
            try {
                cipher.update(cipherText, halfInput,
                              cipherText.length-halfInput,
                              new byte[1], 0);
                throw new RuntimeException("Expected Exception is not thrown");
            } catch (ShortBufferException sbe) {
                // expected exception thrown; retry
                int processed2 = cipher.update(cipherText, halfInput,
                                               cipherText.length-halfInput,
                                               dec, processed1);
                int processed3 =
                    cipher.doFinal(dec, processed1+processed2);
                if ((processed1 + processed2 + processed3) != dec.length) {
                    System.out.println("processed1 = " + processed1);
                    System.out.println("processed2 = " + processed2);
                    System.out.println("processed3 = " + processed3);
                    System.out.println("total length = " + dec.length);
                    throw new RuntimeException("Decryption length check " +
                                               "failed");
                }
            }
            if (Arrays.equals(plainText, dec) == false) {
                System.out.println("cipher: " + toString(cipherText));
                System.out.println("plain:  " + toString(plainText));
                System.out.println("actualD: " + toString(dec));
                throw new RuntimeException("Decryption failure for test " + i);
            }
        }
    }
}