Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/RC2ArcFour/CipherKAT.java
41161 views
/*1* Copyright (c) 2003, 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 489007826* @summary Basic known-answer-test for RC2 and ARCFOUR27* @author Andreas Sterbenz28*/2930import java.io.*;31import java.util.*;3233import java.security.*;3435import javax.crypto.*;36import javax.crypto.spec.*;3738public class CipherKAT {3940private final static char[] hexDigits = "0123456789abcdef".toCharArray();4142public static String toString(byte[] b) {43if (b == null) {44return "(null)";45}46StringBuffer sb = new StringBuffer(b.length * 3);47for (int i = 0; i < b.length; i++) {48int k = b[i] & 0xff;49if (i != 0) {50sb.append(':');51}52sb.append(hexDigits[k >>> 4]);53sb.append(hexDigits[k & 0xf]);54}55return sb.toString();56}5758public static byte[] parse(String s) {59try {60int n = s.length();61ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);62StringReader r = new StringReader(s);63while (true) {64int b1 = nextNibble(r);65if (b1 < 0) {66break;67}68int b2 = nextNibble(r);69if (b2 < 0) {70throw new RuntimeException("Invalid string " + s);71}72int b = (b1 << 4) | b2;73out.write(b);74}75return out.toByteArray();76} catch (IOException e) {77throw new RuntimeException(e);78}79}8081public static byte[] b(String s) {82return parse(s);83}8485private static int nextNibble(StringReader r) throws IOException {86while (true) {87int ch = r.read();88if (ch == -1) {89return -1;90} else if ((ch >= '0') && (ch <= '9')) {91return ch - '0';92} else if ((ch >= 'a') && (ch <= 'f')) {93return ch - 'a' + 10;94} else if ((ch >= 'A') && (ch <= 'F')) {95return ch - 'A' + 10;96}97}98}99100static abstract class Test {101abstract void run(Provider p) throws Exception;102}103104static class CipherTest extends Test {105private final String alg;106private final byte[] plaintext;107private final byte[] ciphertext;108private final byte[] key;109private final byte[] iv;110CipherTest(String alg, byte[] plaintext, byte[] ciphertext, byte[] key, byte[] iv) {111this.alg = alg;112this.plaintext = plaintext;113this.ciphertext = ciphertext;114this.key = key;115this.iv = iv;116}117void run(Provider p) throws Exception {118Cipher cipher = Cipher.getInstance(alg, p);119SecretKey keySpec = new SecretKeySpec(key, alg.split("/")[0]);120IvParameterSpec ivSpec;121if (iv == null) {122ivSpec = null;123} else {124ivSpec = new IvParameterSpec(iv);125}126cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);127byte[] enc = cipher.doFinal(plaintext);128if (Arrays.equals(ciphertext, enc) == false) {129System.out.println("Cipher test encryption for " + alg + " failed:");130if (plaintext.length < 256) {131System.out.println("plaintext: " + CipherKAT.toString(plaintext));132System.out.println("ciphertext: " + CipherKAT.toString(ciphertext));133System.out.println("encrypted: " + CipherKAT.toString(enc));134}135System.out.println("key: " + CipherKAT.toString(key));136System.out.println("iv: " + CipherKAT.toString(iv));137throw new Exception("Cipher test encryption for " + alg + " failed");138}139enc = cipher.doFinal(plaintext);140if (Arrays.equals(ciphertext, enc) == false) {141throw new Exception("Re-encryption test failed");142}143cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);144byte[] dec = cipher.doFinal(ciphertext);145if (Arrays.equals(plaintext, dec) == false) {146System.out.println("Cipher test decryption for " + alg + " failed:");147if (plaintext.length < 256) {148System.out.println("plaintext: " + CipherKAT.toString(plaintext));149System.out.println("ciphertext: " + CipherKAT.toString(ciphertext));150System.out.println("decrypted: " + CipherKAT.toString(dec));151}152System.out.println("key: " + CipherKAT.toString(key));153System.out.println("iv: " + CipherKAT.toString(iv));154throw new Exception("Cipher test decryption for " + alg + " failed");155}156System.out.println("passed: " + alg);157}158}159160private static byte[] s(String s) {161try {162return s.getBytes("UTF8");163} catch (Exception e) {164throw new RuntimeException(e);165}166}167168private static Test t(String alg, String plaintext, String ciphertext, String key) {169return new CipherTest(alg, b(plaintext), b(ciphertext), b(key), null);170}171172private final static byte[] ALONG;173174static {175ALONG = new byte[1024 * 128];176Arrays.fill(ALONG, (byte)'a');177}178179private final static Test[] tests = {180t("RC4", "00", "74", "0123456789abcdef"),181t("RC4", "000102030405060708090a0b0c0d0e0f", "74:95:c0:e4:14:4e:0e:7e:05:42:df:58:3e:82:10:f3", "0123456789abcdef"),182t("RC4", "000102030405060708090a0b0c0d0e0f", "1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a", "0123456789"), // 40 bit183t("RC4", "000102030405060708090a0b0c0d0e0f", "ad:79:9d:98:d6:38:b1:f8:05:96:5c:e6:75:e7:82:24", "0123456789abcdeffedcba9876543210"), // 128 bit184t("RC4", "74:95:c0:e4:14:4e:0e:7e:05:42:df:58:3e:82:10:f3 1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a ad:79:9d:98:d6:38:b1:f8:05:96:5c:e6:75:e7:82:24 ad:79:9d:98:d6:38:b1:f8:05:96:5c:e6:75:e7:82:24",185"e1:15:95:fc:79:da:5e:a4:e4:2a:a8:ce:cd:7d:1e:f3:f7:ef:19:a5:05:70:9f:f7:59:49:44:d6:fb:5a:b6:54:04:4d:f8:e6:60:a3:96:7d:40:33:09:78:f2:2e:de:25:fe:ad:54:dd:b9:65:97:d6:d4:4c:a8:a6:f2:2c:13:61",186"1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),187t("RC2/ECB/NoPadding", "00:00:00:00:00:00:00:00", "81:07:71:4F:0D:81:88:A7",188"00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"),189t("RC2/ECB/NoPadding", "00:00:00:00:00:00:00:00", "39:88:F7:B8:6C:14:D3:F8",190"00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01"),191t("RC2/ECB/NoPadding", "FF:FF:FF:FF:FF:FF:FF:FF", "BA:B8:12:73:3E:2E:B7:EE",192"00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"),193t("RC2/ECB/NoPadding", "00:00:00:00:00:00:00:00", "9C:4B:FE:6D:FE:73:9C:2B",194"00:01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F"),195t("RC2/ECB/NoPadding", "9C:4B:FE:6D:FE:73:9C:2B", "1d:b2:cb:09:7f:e8:ab:43",196"39:88:F7:B8:6C:14:D3:F8:BA:B8:12:73:3E:2E:B7:EE"),197t("RC2/ECB/NoPadding", "BA:B8:12:73:3E:2E:B7:EE", "14:e5:5b:62:ca:f5:16:24",198"1d:b2:cb:09:7f:e8:ab:43"),199};200201static void runTests(Test[] tests) throws Exception {202long start = System.currentTimeMillis();203Provider p = Security.getProvider("SunJCE");204System.out.println("Testing provider " + p.getName() + "...");205Cipher.getInstance("RC2", p);206Cipher.getInstance("RC4", p);207Cipher.getInstance("ARCFOUR", p);208KeyGenerator.getInstance("RC2", p);209KeyGenerator.getInstance("RC4", p);210KeyGenerator.getInstance("ARCFOUR", p);211for (int i = 0; i < tests.length; i++) {212Test test = tests[i];213test.run(p);214}215System.out.println("All tests passed");216long stop = System.currentTimeMillis();217System.out.println("Done (" + (stop - start) + " ms).");218}219220public static void main(String[] args) throws Exception {221runTests(tests);222}223224}225226227