Path: blob/master/test/jdk/javax/crypto/spec/RC2ParameterSpec/RC2AlgorithmParameters.java
41154 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 489415926* @summary unit test to test basic functionality of RC2 AlgorithmParameters27* implementation28* @author Sean Mullan29*/30import java.io.ByteArrayOutputStream;31import java.io.FileOutputStream;32import java.io.IOException;33import java.io.StringReader;34import java.security.AlgorithmParameters;35import java.util.Arrays;36import javax.crypto.Cipher;37import javax.crypto.SecretKey;38import javax.crypto.spec.RC2ParameterSpec;39import javax.crypto.spec.SecretKeySpec;4041public class RC2AlgorithmParameters {4243// much of code copied from test/cipher/rc2arcfour/CipherKAT.java4445private final static char[] hexDigits = "0123456789abcdef".toCharArray();4647public static void main(String[] args) throws Exception {4849byte[] iv_1 = {50(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,51(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,52(byte)0x33,(byte)0x3353};5455// check that RC2 is supported by our provider56AlgorithmParameters rc2Params =57AlgorithmParameters.getInstance("RC2", "SunJCE");5859// check that getAlgorithm returns "RC2"60if (!rc2Params.getAlgorithm().equals("RC2")) {61throw new Exception("getAlgorithm() returned "62+ rc2Params.getAlgorithm() + " instead of RC2");63}6465// test parameters with effective key size and iv66byte[] encoded = testParams(rc2Params, new RC2ParameterSpec(2, iv_1));6768// test parameters with just iv69encoded = testParams(AlgorithmParameters.getInstance("RC2"),70new RC2ParameterSpec(0, iv_1));7172// test vectors in RFC 226873runTests(tests);74}7576static void runTests(CipherTest[] tests) throws Exception {77for (int i = 0; i < tests.length; i++) {78CipherTest test = tests[i];79test.run();80}81System.out.println("All tests passed");82}8384private static byte[] testParams(AlgorithmParameters rc2Params,85RC2ParameterSpec rc2Spec) throws Exception {8687// test getParameterSpec returns object equal to input88rc2Params.init(rc2Spec);89RC2ParameterSpec rc2OtherSpec = (RC2ParameterSpec)90rc2Params.getParameterSpec(RC2ParameterSpec.class);91if (!rc2Spec.equals(rc2OtherSpec)) {92throw new Exception("AlgorithmParameterSpecs should be equal");93}9495// test RC2ParameterSpec with RC2 Cipher96Cipher rc2Cipher = Cipher.getInstance("RC2/CBC/PKCS5PADDING", "SunJCE");97rc2Cipher.init(Cipher.ENCRYPT_MODE,98new SecretKeySpec("secret".getBytes("ASCII"), "RC2"), rc2Spec);99100// get IV101byte[] iv = rc2Cipher.getIV();102if (!Arrays.equals(iv, rc2Spec.getIV())) {103throw new Exception("ivs should be equal");104}105106// test encoding and decoding107byte[] encoded = rc2Params.getEncoded();108AlgorithmParameters params = AlgorithmParameters.getInstance("RC2");109params.init(encoded);110111// test RC2 AlgorithmParameters with RC2 Cipher112rc2Cipher.init(Cipher.ENCRYPT_MODE,113new SecretKeySpec("secret".getBytes("ASCII"), "RC2"), params);114115// get IV116iv = rc2Cipher.getIV();117if (!Arrays.equals(iv, rc2Spec.getIV())) {118throw new Exception("ivs should be equal");119}120return encoded;121}122123private static void dumpBytes(byte[] encoded, String file)124throws Exception {125126FileOutputStream fos = new FileOutputStream(file);127fos.write(encoded);128fos.close();129}130131public static String toString(byte[] b) {132if (b == null) {133return "(null)";134}135StringBuffer sb = new StringBuffer(b.length * 3);136for (int i = 0; i < b.length; i++) {137int k = b[i] & 0xff;138if (i != 0) {139sb.append(':');140}141sb.append(hexDigits[k >>> 4]);142sb.append(hexDigits[k & 0xf]);143}144return sb.toString();145}146147public static byte[] parse(String s) {148try {149int n = s.length();150ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);151StringReader r = new StringReader(s);152while (true) {153int b1 = nextNibble(r);154if (b1 < 0) {155break;156}157int b2 = nextNibble(r);158if (b2 < 0) {159throw new RuntimeException("Invalid string " + s);160}161int b = (b1 << 4) | b2;162out.write(b);163}164return out.toByteArray();165} catch (IOException e) {166throw new RuntimeException(e);167}168}169170public static byte[] b(String s) {171return parse(s);172}173174private static int nextNibble(StringReader r) throws IOException {175while (true) {176int ch = r.read();177if (ch == -1) {178return -1;179} else if ((ch >= '0') && (ch <= '9')) {180return ch - '0';181} else if ((ch >= 'a') && (ch <= 'f')) {182return ch - 'a' + 10;183} else if ((ch >= 'A') && (ch <= 'F')) {184return ch - 'A' + 10;185}186}187}188189static class CipherTest {190private final byte[] plaintext;191private final byte[] ciphertext;192private final byte[] key;193private final int effectiveKeySize;194CipherTest(String plaintext, String ciphertext,195String key, int effectiveKeySize) {196this.plaintext = b(plaintext);197this.ciphertext = b(ciphertext);198this.key = b(key);199this.effectiveKeySize = effectiveKeySize;200}201void run() throws Exception {202Cipher cipher = Cipher.getInstance("RC2/ECB/NOPADDING", "SunJCE");203SecretKey keySpec = new SecretKeySpec(key, "RC2");204RC2ParameterSpec rc2Spec = new RC2ParameterSpec(effectiveKeySize);205cipher.init(Cipher.ENCRYPT_MODE, keySpec, rc2Spec);206byte[] enc = cipher.doFinal(plaintext);207if (Arrays.equals(ciphertext, enc) == false) {208System.out.println("RC2AlgorithmParameters Cipher test " +209"encryption failed:");210System.out.println("plaintext: "211+ RC2AlgorithmParameters.toString(plaintext));212System.out.println("ciphertext: "213+ RC2AlgorithmParameters.toString(ciphertext));214System.out.println("encrypted: "215+ RC2AlgorithmParameters.toString(enc));216System.out.println("key: "217+ RC2AlgorithmParameters.toString(key));218System.out.println("effective key length: "219+ effectiveKeySize);220throw new Exception("RC2AlgorithmParameters Cipher test "221+ "encryption failed");222}223enc = cipher.doFinal(plaintext);224if (Arrays.equals(ciphertext, enc) == false) {225throw new Exception("Re-encryption test failed");226}227cipher.init(Cipher.DECRYPT_MODE, keySpec, rc2Spec);228byte[] dec = cipher.doFinal(ciphertext);229if (Arrays.equals(plaintext, dec) == false) {230System.out.println("RC2AlgorithmParameters Cipher test "231+ "decryption failed:");232System.out.println("plaintext: "233+ RC2AlgorithmParameters.toString(plaintext));234System.out.println("ciphertext: "235+ RC2AlgorithmParameters.toString(ciphertext));236System.out.println("decrypted: "237+ RC2AlgorithmParameters.toString(dec));238System.out.println("key: "239+ RC2AlgorithmParameters.toString(key));240System.out.println("effective key length: "241+ effectiveKeySize);242throw new Exception("RC2AlgorithmParameters Cipher test "243+ "decryption failed");244}245System.out.println("passed");246}247}248249250// test vectors listed in RFC 2268251private final static CipherTest[] tests = {252new CipherTest("00:00:00:00:00:00:00:00", "EB:B7:73:F9:93:27:8E:FF",253"00:00:00:00:00:00:00:00", 63),254new CipherTest("FF:FF:FF:FF:FF:FF:FF:FF", "27:8B:27:E4:2E:2F:0D:49",255"FF:FF:FF:FF:FF:FF:FF:FF", 64),256new CipherTest("10:00:00:00:00:00:00:01", "30:64:9E:DF:9B:E7:D2:C2",257"30:00:00:00:00:00:00:00", 64),258// This vector is not tested because it will throw an exception because the259// key size is too small (less than 40 bits)260// new CipherTest("00:00:00:00:00:00:00:00", "61:A8:A2:44:AD:AC:CC:F0",261// "88", 64),262new CipherTest("00:00:00:00:00:00:00:00", "6C:CF:43:08:97:4C:26:7F",263"88:BC:A9:0E:90:87:5A", 64),264new CipherTest("00:00:00:00:00:00:00:00", "1A:80:7D:27:2B:BE:5D:B1",265"88:BC:A9:0E:90:87:5A:7F:0F:79:C3:84:62:7B:AF:B2", 64),266new CipherTest("00:00:00:00:00:00:00:00", "22:69:55:2A:B0:F8:5C:A6",267"88:BC:A9:0E:90:87:5A:7F:0F:79:C3:84:62:7B:AF:B2", 128),268};269}270271272