J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4688768 |
| 27 | * @library ../UTIL |
| 28 | * @build TestUtil |
| 29 | * @run main CounterMode |
| 30 | * @summary Verify that CTR mode works as expected |
| 31 | * @author Andreas Sterbenz |
| 32 | */ |
| 33 | |
| 34 | import java.util.*; |
| 35 | |
| 36 | import javax.crypto.*; |
| 37 | import javax.crypto.spec.*; |
| 38 | |
| 39 | public class CounterMode { |
| 40 | |
| 41 | private final static byte[] toByteArray(String s) { |
| 42 | char[] c = s.toCharArray(); |
| 43 | byte[] t = new byte[c.length / 2]; |
| 44 | int n = 0; |
| 45 | int d1 = -1; |
| 46 | int d2 = -1; |
| 47 | for (int i = 0; i < c.length; i++) { |
| 48 | char ch = c[i]; |
| 49 | if (d1 == -1) { |
| 50 | d1 = Character.digit(ch, 16); |
| 51 | } else { |
| 52 | d2 = Character.digit(ch, 16); |
| 53 | if (d2 != -1) { |
| 54 | t[n++] = (byte)((d1 << 4) | d2); |
| 55 | d1 = -1; |
| 56 | d2 = -1; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | if (d1 != -1) { |
| 61 | throw new RuntimeException(); |
| 62 | } |
| 63 | if (n == t.length) { |
| 64 | return t; |
| 65 | } |
| 66 | byte[] b = new byte[n]; |
| 67 | System.arraycopy(t, 0, b, 0, n); |
| 68 | return b; |
| 69 | } |
| 70 | |
| 71 | private final static String[] ALGORITHM = { |
| 72 | "AES", |
| 73 | "AES", |
| 74 | "AES", |
| 75 | "AES", |
| 76 | "AES", |
| 77 | "DES", |
| 78 | "DES", |
| 79 | "DESede", |
| 80 | "DESede", |
| 81 | "Blowfish", |
| 82 | "Blowfish", |
| 83 | }; |
| 84 | |
| 85 | private final static byte[][] KEYS = { |
| 86 | toByteArray("2b7e151628aed2a6abf7158809cf4f3c"), |
| 87 | toByteArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"), |
| 88 | toByteArray("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"), |
| 89 | toByteArray("2b7e151628aed2a6abf7158809cf4f3c"), |
| 90 | toByteArray("2b7e151628aed2a6abf7158809cf4f3c"), |
| 91 | toByteArray("2b7e151628aed2a6"), |
| 92 | toByteArray("874d6191b620e326"), |
| 93 | toByteArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"), |
| 94 | toByteArray("874d6191b620e3261bef6864990db6ce9806f66b7970fdff"), |
| 95 | toByteArray("2b7e151628aed2a6abf7158809cf4f3c"), |
| 96 | toByteArray("874d6191b620e3261bef6864990db6ce"), |
| 97 | }; |
| 98 | |
| 99 | private final static byte[][] IVS = { |
| 100 | toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), |
| 101 | toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), |
| 102 | toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), |
| 103 | toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), |
| 104 | toByteArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), |
| 105 | toByteArray("fffffffffffffffd"), |
| 106 | toByteArray("f0f1f2f3f4f5f6f7"), |
| 107 | toByteArray("f0f1f2f3f4f5f6f7"), |
| 108 | toByteArray("f0f1f2f3f4f5f6ff"), |
| 109 | toByteArray("f0f1f2f3f4f5f6f7"), |
| 110 | toByteArray("f0f1f2f3f4f5f6ff"), |
| 111 | }; |
| 112 | |
| 113 | private final static byte[][] PLAIN = { |
| 114 | toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"), |
| 115 | toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"), |
| 116 | toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"), |
| 117 | toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c37"), |
| 118 | toByteArray(""), |
| 119 | toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"), |
| 120 | toByteArray("6bc1bee22e409f96"), |
| 121 | toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"), |
| 122 | toByteArray("6bc1bee22e409f96e9"), |
| 123 | toByteArray("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"), |
| 124 | toByteArray("6bc1bee22e409f"), |
| 125 | }; |
| 126 | |
| 127 | private final static byte[][] CIPHER = { |
| 128 | toByteArray("874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee"), |
| 129 | toByteArray("1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e941e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050"), |
| 130 | toByteArray("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c52b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6"), |
| 131 | toByteArray("874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009c"), |
| 132 | toByteArray(""), |
| 133 | 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"), |
| 134 | toByteArray("79:5e:90:a6:35:9d:1e:b2"), |
| 135 | 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"), |
| 136 | toByteArray("12:06:8f:ae:bb:47:a7:68:8b"), |
| 137 | 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"), |
| 138 | toByteArray("65:ff:ee:6b:64:9e:a0"), |
| 139 | }; |
| 140 | |
| 141 | private static String toString(byte[] b) { |
| 142 | StringBuffer sb = new StringBuffer(b.length * 3); |
| 143 | for (int i = 0; i < b.length; i++) { |
| 144 | int k = b[i] & 0xff; |
| 145 | if (i != 0) { |
| 146 | sb.append(':'); |
| 147 | } |
| 148 | sb.append(Character.forDigit(k >> 4, 16)); |
| 149 | sb.append(Character.forDigit(k & 0xf, 16)); |
| 150 | } |
| 151 | return sb.toString(); |
| 152 | } |
| 153 | |
| 154 | public static void main(String[] args) throws Exception { |
| 155 | for (int i = 0; i < KEYS.length; i++) { |
| 156 | try { |
| 157 | String alg = ALGORITHM[i]; |
| 158 | int MAX_KEYSIZE = Cipher.getMaxAllowedKeyLength(alg); |
| 159 | if (KEYS[i].length*8 > MAX_KEYSIZE) { |
| 160 | // skip tests using keys whose length exceeds |
| 161 | // what's configured in jce jurisdiction policy files. |
| 162 | continue; |
| 163 | } |
| 164 | System.out.println("Running test " + i + " (" + alg + ")"); |
| 165 | Cipher cipher = Cipher.getInstance(alg + "/CTR/NoPadding", "SunJCE"); |
| 166 | SecretKeySpec key = new SecretKeySpec(KEYS[i], alg); |
| 167 | IvParameterSpec iv = new IvParameterSpec(IVS[i]); |
| 168 | byte[] plainText = PLAIN[i]; |
| 169 | byte[] cipherText = CIPHER[i]; |
| 170 | cipher.init(Cipher.ENCRYPT_MODE, key, iv); |
| 171 | byte[] enc = cipher.doFinal(plainText); |
| 172 | if (Arrays.equals(cipherText, enc) == false) { |
| 173 | System.out.println("plain: " + toString(PLAIN[i])); |
| 174 | System.out.println("cipher: " + toString(CIPHER[i])); |
| 175 | System.out.println("actual: " + toString(enc)); |
| 176 | throw new RuntimeException("Encryption failure for test " + i); |
| 177 | } |
| 178 | cipher.init(Cipher.DECRYPT_MODE, key, iv); |
| 179 | byte[] dec = cipher.doFinal(cipherText); |
| 180 | if (Arrays.equals(plainText, dec) == false) { |
| 181 | throw new RuntimeException("Decryption failure for test " + i); |
| 182 | } |
| 183 | } catch (SecurityException ex) { |
| 184 | TestUtil.handleSE(ex); |
| 185 | } |
| 186 | } |
| 187 | System.out.println("Done"); |
| 188 | } |
| 189 | } |