Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/CTR/CounterMode.java
41161 views
/*1* Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 468876826* @library ../UTIL27* @build TestUtil28* @run main CounterMode29* @summary Verify that CTR mode works as expected30* @author Andreas Sterbenz31*/3233import java.util.*;3435import javax.crypto.*;36import javax.crypto.spec.*;3738public class CounterMode {3940private final static byte[] toByteArray(String s) {41char[] c = s.toCharArray();42byte[] t = new byte[c.length / 2];43int n = 0;44int d1 = -1;45int d2 = -1;46for (int i = 0; i < c.length; i++) {47char ch = c[i];48if (d1 == -1) {49d1 = Character.digit(ch, 16);50} else {51d2 = Character.digit(ch, 16);52if (d2 != -1) {53t[n++] = (byte)((d1 << 4) | d2);54d1 = -1;55d2 = -1;56}57}58}59if (d1 != -1) {60throw new RuntimeException();61}62if (n == t.length) {63return t;64}65byte[] b = new byte[n];66System.arraycopy(t, 0, b, 0, n);67return b;68}6970private final static String[] ALGORITHM = {71"AES",72"AES",73"AES",74"AES",75"AES",76"DES",77"DES",78"DESede",79"DESede",80"Blowfish",81"Blowfish",82};8384private final static byte[][] KEYS = {85toByteArray("2b7e151628aed2a6abf7158809cf4f3c"),86toByteArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"),87toByteArray("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"),88toByteArray("2b7e151628aed2a6abf7158809cf4f3c"),89toByteArray("2b7e151628aed2a6abf7158809cf4f3c"),90toByteArray("2b7e151628aed2a6"),91toByteArray("874d6191b620e326"),92toByteArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"),93toByteArray("874d6191b620e3261bef6864990db6ce9806f66b7970fdff"),94toByteArray("2b7e151628aed2a6abf7158809cf4f3c"),95toByteArray("874d6191b620e3261bef6864990db6ce"),96};9798private final static byte[][] IVS = {99toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"),100toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"),101toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"),102toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"),103toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"),104toByteArray("fffffffffffffffd"),105toByteArray("f0f1f2f3f4f5f6f7"),106toByteArray("f0f1f2f3f4f5f6f7"),107toByteArray("f0f1f2f3f4f5f6ff"),108toByteArray("f0f1f2f3f4f5f6f7"),109toByteArray("f0f1f2f3f4f5f6ff"),110};111112private final static byte[][] PLAIN = {113toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),114toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),115toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),116toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c37"),117toByteArray(""),118toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),119toByteArray("6bc1bee22e409f96"),120toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),121toByteArray("6bc1bee22e409f96e9"),122toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),123toByteArray("6bc1bee22e409f"),124};125126private final static byte[][] CIPHER = {127toByteArray("874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee"),128toByteArray("1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e941e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050"),129toByteArray("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c52b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6"),130toByteArray("874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009c"),131toByteArray(""),132toByteArray("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"),133toByteArray("79:5e:90:a6:35:9d:1e:b2"),134toByteArray("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"),135toByteArray("12:06:8f:ae:bb:47:a7:68:8b"),136toByteArray("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"),137toByteArray("65:ff:ee:6b:64:9e:a0"),138};139140private static String toString(byte[] b) {141StringBuffer sb = new StringBuffer(b.length * 3);142for (int i = 0; i < b.length; i++) {143int k = b[i] & 0xff;144if (i != 0) {145sb.append(':');146}147sb.append(Character.forDigit(k >> 4, 16));148sb.append(Character.forDigit(k & 0xf, 16));149}150return sb.toString();151}152153public static void main(String[] args) throws Exception {154for (int i = 0; i < KEYS.length; i++) {155try {156String alg = ALGORITHM[i];157int MAX_KEYSIZE = Cipher.getMaxAllowedKeyLength(alg);158if (KEYS[i].length*8 > MAX_KEYSIZE) {159// skip tests using keys whose length exceeds160// what's configured in jce jurisdiction policy files.161continue;162}163System.out.println("Running test " + i + " (" + alg + ")");164Cipher cipher = Cipher.getInstance(alg + "/CTR/NoPadding", "SunJCE");165SecretKeySpec key = new SecretKeySpec(KEYS[i], alg);166IvParameterSpec iv = new IvParameterSpec(IVS[i]);167byte[] plainText = PLAIN[i];168byte[] cipherText = CIPHER[i];169cipher.init(Cipher.ENCRYPT_MODE, key, iv);170byte[] enc = cipher.doFinal(plainText);171if (Arrays.equals(cipherText, enc) == false) {172System.out.println("plain: " + toString(PLAIN[i]));173System.out.println("cipher: " + toString(CIPHER[i]));174System.out.println("actual: " + toString(enc));175throw new RuntimeException("Encryption failure for test " + i);176}177cipher.init(Cipher.DECRYPT_MODE, key, iv);178byte[] dec = cipher.doFinal(cipherText);179if (Arrays.equals(plainText, dec) == false) {180throw new RuntimeException("Decryption failure for test " + i);181}182} catch (SecurityException ex) {183TestUtil.handleSE(ex);184}185}186System.out.println("Done");187}188}189190191