0N/A/*
2362N/A * Copyright (c) 2003, 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 4890078
0N/A * @summary Basic known-answer-test for RC2 and ARCFOUR
0N/A * @author Andreas Sterbenz
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/A
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/A
0N/Apublic class CipherKAT {
0N/A
0N/A private final static char[] hexDigits = "0123456789abcdef".toCharArray();
0N/A
0N/A public static String toString(byte[] b) {
0N/A if (b == null) {
0N/A return "(null)";
0N/A }
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(hexDigits[k >>> 4]);
0N/A sb.append(hexDigits[k & 0xf]);
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A public static byte[] parse(String s) {
0N/A try {
0N/A int n = s.length();
0N/A ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
0N/A StringReader r = new StringReader(s);
0N/A while (true) {
0N/A int b1 = nextNibble(r);
0N/A if (b1 < 0) {
0N/A break;
0N/A }
0N/A int b2 = nextNibble(r);
0N/A if (b2 < 0) {
0N/A throw new RuntimeException("Invalid string " + s);
0N/A }
0N/A int b = (b1 << 4) | b2;
0N/A out.write(b);
0N/A }
0N/A return out.toByteArray();
0N/A } catch (IOException e) {
0N/A throw new RuntimeException(e);
0N/A }
0N/A }
0N/A
0N/A public static byte[] b(String s) {
0N/A return parse(s);
0N/A }
0N/A
0N/A private static int nextNibble(StringReader r) throws IOException {
0N/A while (true) {
0N/A int ch = r.read();
0N/A if (ch == -1) {
0N/A return -1;
0N/A } else if ((ch >= '0') && (ch <= '9')) {
0N/A return ch - '0';
0N/A } else if ((ch >= 'a') && (ch <= 'f')) {
0N/A return ch - 'a' + 10;
0N/A } else if ((ch >= 'A') && (ch <= 'F')) {
0N/A return ch - 'A' + 10;
0N/A }
0N/A }
0N/A }
0N/A
0N/A static abstract class Test {
0N/A abstract void run(Provider p) throws Exception;
0N/A }
0N/A
0N/A static class CipherTest extends Test {
0N/A private final String alg;
0N/A private final byte[] plaintext;
0N/A private final byte[] ciphertext;
0N/A private final byte[] key;
0N/A private final byte[] iv;
0N/A CipherTest(String alg, byte[] plaintext, byte[] ciphertext, byte[] key, byte[] iv) {
0N/A this.alg = alg;
0N/A this.plaintext = plaintext;
0N/A this.ciphertext = ciphertext;
0N/A this.key = key;
0N/A this.iv = iv;
0N/A }
0N/A void run(Provider p) throws Exception {
0N/A Cipher cipher = Cipher.getInstance(alg, p);
0N/A SecretKey keySpec = new SecretKeySpec(key, alg.split("/")[0]);
0N/A IvParameterSpec ivSpec;
0N/A if (iv == null) {
0N/A ivSpec = null;
0N/A } else {
0N/A ivSpec = new IvParameterSpec(iv);
0N/A }
0N/A cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
0N/A byte[] enc = cipher.doFinal(plaintext);
0N/A if (Arrays.equals(ciphertext, enc) == false) {
0N/A System.out.println("Cipher test encryption for " + alg + " failed:");
0N/A if (plaintext.length < 256) {
0N/A System.out.println("plaintext: " + CipherKAT.toString(plaintext));
0N/A System.out.println("ciphertext: " + CipherKAT.toString(ciphertext));
0N/A System.out.println("encrypted: " + CipherKAT.toString(enc));
0N/A }
0N/A System.out.println("key: " + CipherKAT.toString(key));
0N/A System.out.println("iv: " + CipherKAT.toString(iv));
0N/A throw new Exception("Cipher test encryption for " + alg + " failed");
0N/A }
0N/A enc = cipher.doFinal(plaintext);
0N/A if (Arrays.equals(ciphertext, enc) == false) {
0N/A throw new Exception("Re-encryption test failed");
0N/A }
0N/A cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
0N/A byte[] dec = cipher.doFinal(ciphertext);
0N/A if (Arrays.equals(plaintext, dec) == false) {
0N/A System.out.println("Cipher test decryption for " + alg + " failed:");
0N/A if (plaintext.length < 256) {
0N/A System.out.println("plaintext: " + CipherKAT.toString(plaintext));
0N/A System.out.println("ciphertext: " + CipherKAT.toString(ciphertext));
0N/A System.out.println("decrypted: " + CipherKAT.toString(dec));
0N/A }
0N/A System.out.println("key: " + CipherKAT.toString(key));
0N/A System.out.println("iv: " + CipherKAT.toString(iv));
0N/A throw new Exception("Cipher test decryption for " + alg + " failed");
0N/A }
0N/A System.out.println("passed: " + alg);
0N/A }
0N/A }
0N/A
0N/A private static byte[] s(String s) {
0N/A try {
0N/A return s.getBytes("UTF8");
0N/A } catch (Exception e) {
0N/A throw new RuntimeException(e);
0N/A }
0N/A }
0N/A
0N/A private static Test t(String alg, String plaintext, String ciphertext, String key) {
0N/A return new CipherTest(alg, b(plaintext), b(ciphertext), b(key), null);
0N/A }
0N/A
0N/A private final static byte[] ALONG;
0N/A
0N/A static {
0N/A ALONG = new byte[1024 * 128];
0N/A Arrays.fill(ALONG, (byte)'a');
0N/A }
0N/A
0N/A private final static Test[] tests = {
0N/A t("RC4", "00", "74", "0123456789abcdef"),
0N/A t("RC4", "000102030405060708090a0b0c0d0e0f", "74:95:c0:e4:14:4e:0e:7e:05:42:df:58:3e:82:10:f3", "0123456789abcdef"),
0N/A t("RC4", "000102030405060708090a0b0c0d0e0f", "1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a", "0123456789"), // 40 bit
0N/A t("RC4", "000102030405060708090a0b0c0d0e0f", "ad:79:9d:98:d6:38:b1:f8:05:96:5c:e6:75:e7:82:24", "0123456789abcdeffedcba9876543210"), // 128 bit
0N/A t("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",
0N/A "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",
0N/A "1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
0N/A t("RC2/ECB/NoPadding", "00:00:00:00:00:00:00:00", "81:07:71:4F:0D:81:88:A7",
0N/A "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"),
0N/A t("RC2/ECB/NoPadding", "00:00:00:00:00:00:00:00", "39:88:F7:B8:6C:14:D3:F8",
0N/A "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01"),
0N/A t("RC2/ECB/NoPadding", "FF:FF:FF:FF:FF:FF:FF:FF", "BA:B8:12:73:3E:2E:B7:EE",
0N/A "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"),
0N/A t("RC2/ECB/NoPadding", "00:00:00:00:00:00:00:00", "9C:4B:FE:6D:FE:73:9C:2B",
0N/A "00:01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F"),
0N/A t("RC2/ECB/NoPadding", "9C:4B:FE:6D:FE:73:9C:2B", "1d:b2:cb:09:7f:e8:ab:43",
0N/A "39:88:F7:B8:6C:14:D3:F8:BA:B8:12:73:3E:2E:B7:EE"),
0N/A t("RC2/ECB/NoPadding", "BA:B8:12:73:3E:2E:B7:EE", "14:e5:5b:62:ca:f5:16:24",
0N/A "1d:b2:cb:09:7f:e8:ab:43"),
0N/A };
0N/A
0N/A static void runTests(Test[] tests) throws Exception {
0N/A long start = System.currentTimeMillis();
0N/A Provider p = Security.getProvider("SunJCE");
0N/A System.out.println("Testing provider " + p.getName() + "...");
0N/A Cipher.getInstance("RC2", p);
0N/A Cipher.getInstance("RC4", p);
0N/A Cipher.getInstance("ARCFOUR", p);
0N/A KeyGenerator.getInstance("RC2", p);
0N/A KeyGenerator.getInstance("RC4", p);
0N/A KeyGenerator.getInstance("ARCFOUR", p);
0N/A for (int i = 0; i < tests.length; i++) {
0N/A Test test = tests[i];
0N/A test.run(p);
0N/A }
0N/A System.out.println("All tests passed");
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("Done (" + (stop - start) + " ms).");
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A runTests(tests);
0N/A }
0N/A
0N/A}