0N/A/*
2362N/A * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4688768
0N/A * @library ../UTIL
0N/A * @build TestUtil
0N/A * @run main CounterMode
0N/A * @summary Verify that CTR mode works as expected
0N/A * @author Andreas Sterbenz
0N/A */
0N/A
0N/Aimport java.util.*;
0N/A
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/A
0N/Apublic class CounterMode {
0N/A
0N/A private final static byte[] toByteArray(String s) {
0N/A char[] c = s.toCharArray();
0N/A byte[] t = new byte[c.length / 2];
0N/A int n = 0;
0N/A int d1 = -1;
0N/A int d2 = -1;
0N/A for (int i = 0; i < c.length; i++) {
0N/A char ch = c[i];
0N/A if (d1 == -1) {
0N/A d1 = Character.digit(ch, 16);
0N/A } else {
0N/A d2 = Character.digit(ch, 16);
0N/A if (d2 != -1) {
0N/A t[n++] = (byte)((d1 << 4) | d2);
0N/A d1 = -1;
0N/A d2 = -1;
0N/A }
0N/A }
0N/A }
0N/A if (d1 != -1) {
0N/A throw new RuntimeException();
0N/A }
0N/A if (n == t.length) {
0N/A return t;
0N/A }
0N/A byte[] b = new byte[n];
0N/A System.arraycopy(t, 0, b, 0, n);
0N/A return b;
0N/A }
0N/A
0N/A private final static String[] ALGORITHM = {
0N/A "AES",
0N/A "AES",
0N/A "AES",
0N/A "AES",
0N/A "AES",
0N/A "DES",
0N/A "DES",
0N/A "DESede",
0N/A "DESede",
0N/A "Blowfish",
0N/A "Blowfish",
0N/A };
0N/A
0N/A private final static byte[][] KEYS = {
0N/A toByteArray("2b7e151628aed2a6abf7158809cf4f3c"),
0N/A toByteArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"),
0N/A toByteArray("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"),
0N/A toByteArray("2b7e151628aed2a6abf7158809cf4f3c"),
0N/A toByteArray("2b7e151628aed2a6abf7158809cf4f3c"),
0N/A toByteArray("2b7e151628aed2a6"),
0N/A toByteArray("874d6191b620e326"),
0N/A toByteArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"),
0N/A toByteArray("874d6191b620e3261bef6864990db6ce9806f66b7970fdff"),
0N/A toByteArray("2b7e151628aed2a6abf7158809cf4f3c"),
0N/A toByteArray("874d6191b620e3261bef6864990db6ce"),
0N/A };
0N/A
0N/A private final static byte[][] IVS = {
0N/A toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"),
0N/A toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"),
0N/A toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"),
0N/A toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"),
0N/A toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"),
0N/A toByteArray("fffffffffffffffd"),
0N/A toByteArray("f0f1f2f3f4f5f6f7"),
0N/A toByteArray("f0f1f2f3f4f5f6f7"),
0N/A toByteArray("f0f1f2f3f4f5f6ff"),
0N/A toByteArray("f0f1f2f3f4f5f6f7"),
0N/A toByteArray("f0f1f2f3f4f5f6ff"),
0N/A };
0N/A
0N/A private final static byte[][] PLAIN = {
0N/A toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),
0N/A toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),
0N/A toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),
0N/A toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c37"),
0N/A toByteArray(""),
0N/A toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),
0N/A toByteArray("6bc1bee22e409f96"),
0N/A toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),
0N/A toByteArray("6bc1bee22e409f96e9"),
0N/A toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),
0N/A toByteArray("6bc1bee22e409f"),
0N/A };
0N/A
0N/A private final static byte[][] CIPHER = {
0N/A toByteArray("874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee"),
0N/A toByteArray("1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e941e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050"),
0N/A toByteArray("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c52b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6"),
0N/A toByteArray("874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009c"),
0N/A toByteArray(""),
0N/A toByteArray("79:62:2c:42:59:8e:9a:1a:e3:da:3d:7c:88:4f:0a:d2:eb:1a:e2:27:b0:d7:16:f6:6d:8d:50:fe:8b:9f:87:a7:ba:83:65:48:5b:87:12:07:d0:3f:3d:f0:b2:c8:98:e9:bb:a0:ad:35:98:e8:4f:d8:ef:91:85:2c:55:c0:b0:b9"),
0N/A toByteArray("79:5e:90:a6:35:9d:1e:b2"),
0N/A toByteArray("0a:ce:b5:1e:20:74:2e:29:17:28:55:d6:c9:28:62:c3:43:a5:da:30:90:9f:8f:46:60:3d:b6:5c:b6:bc:21:52:06:2f:6f:b4:14:0d:ef:23:e2:99:fd:4b:dc:65:ee:6a:50:76:5b:86:43:65:56:ee:1a:7a:25:07:43:b4:f0:88"),
0N/A toByteArray("12:06:8f:ae:bb:47:a7:68:8b"),
0N/A toByteArray("ac:92:83:dd:79:f3:c2:36:0c:da:06:bd:ed:69:ca:47:42:2e:68:11:5e:6b:63:56:3e:87:b0:be:98:17:42:7d:8b:4a:5b:be:af:0c:2b:f0:1c:1c:62:60:b9:2c:69:04:ca:c9:23:d2:08:9d:8d:57:7f:61:f7:e6:79:83:ae:89"),
0N/A toByteArray("65:ff:ee:6b:64:9e:a0"),
0N/A };
0N/A
0N/A private static String toString(byte[] b) {
0N/A StringBuffer sb = new StringBuffer(b.length * 3);
0N/A for (int i = 0; i < b.length; i++) {
0N/A int k = b[i] & 0xff;
0N/A if (i != 0) {
0N/A sb.append(':');
0N/A }
0N/A sb.append(Character.forDigit(k >> 4, 16));
0N/A sb.append(Character.forDigit(k & 0xf, 16));
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A for (int i = 0; i < KEYS.length; i++) {
0N/A try {
0N/A String alg = ALGORITHM[i];
0N/A int MAX_KEYSIZE = Cipher.getMaxAllowedKeyLength(alg);
0N/A if (KEYS[i].length*8 > MAX_KEYSIZE) {
0N/A // skip tests using keys whose length exceeds
0N/A // what's configured in jce jurisdiction policy files.
0N/A continue;
0N/A }
0N/A System.out.println("Running test " + i + " (" + alg + ")");
0N/A Cipher cipher = Cipher.getInstance(alg + "/CTR/NoPadding", "SunJCE");
0N/A SecretKeySpec key = new SecretKeySpec(KEYS[i], alg);
0N/A IvParameterSpec iv = new IvParameterSpec(IVS[i]);
0N/A byte[] plainText = PLAIN[i];
0N/A byte[] cipherText = CIPHER[i];
0N/A cipher.init(Cipher.ENCRYPT_MODE, key, iv);
0N/A byte[] enc = cipher.doFinal(plainText);
0N/A if (Arrays.equals(cipherText, enc) == false) {
0N/A System.out.println("plain: " + toString(PLAIN[i]));
0N/A System.out.println("cipher: " + toString(CIPHER[i]));
0N/A System.out.println("actual: " + toString(enc));
0N/A throw new RuntimeException("Encryption failure for test " + i);
0N/A }
0N/A cipher.init(Cipher.DECRYPT_MODE, key, iv);
0N/A byte[] dec = cipher.doFinal(cipherText);
0N/A if (Arrays.equals(plainText, dec) == false) {
0N/A throw new RuntimeException("Decryption failure for test " + i);
0N/A }
0N/A } catch (SecurityException ex) {
0N/A TestUtil.handleSE(ex);
0N/A }
0N/A }
0N/A System.out.println("Done");
0N/A }
0N/A}