001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.asn1.ntt.NTTObjectIdentifiers;
004: import org.bouncycastle.jce.provider.BouncyCastleProvider;
005: import org.bouncycastle.util.encoders.Hex;
006:
007: import javax.crypto.Cipher;
008: import javax.crypto.CipherInputStream;
009: import javax.crypto.CipherOutputStream;
010: import javax.crypto.spec.SecretKeySpec;
011: import java.io.ByteArrayInputStream;
012: import java.io.ByteArrayOutputStream;
013: import java.io.DataInputStream;
014: import java.io.IOException;
015: import java.security.Key;
016: import java.security.Security;
017:
018: /**
019: * basic test class for Camellia
020: */
021: public class CamelliaTest extends BaseBlockCipherTest {
022: static String[] cipherTests = {
023: "128",
024: "0123456789abcdeffedcba9876543210",
025: "0123456789abcdeffedcba9876543210",
026: "67673138549669730857065648eabe43",
027: "192",
028: "0123456789abcdeffedcba98765432100011223344556677",
029: "0123456789abcdeffedcba9876543210",
030: "b4993401b3e996f84ee5cee7d79b09b9",
031: "256",
032: "0123456789abcdeffedcba987654321000112233445566778899aabbccddeeff",
033: "0123456789abcdeffedcba9876543210",
034: "9acc237dff16d76c20ef7c919e3a7509", };
035:
036: public CamelliaTest() {
037: super ("Camellia");
038: }
039:
040: public void test(int strength, byte[] keyBytes, byte[] input,
041: byte[] output) throws Exception {
042: Key key;
043: Cipher in, out;
044: CipherInputStream cIn;
045: CipherOutputStream cOut;
046: ByteArrayInputStream bIn;
047: ByteArrayOutputStream bOut;
048:
049: key = new SecretKeySpec(keyBytes, "Camellia");
050:
051: in = Cipher.getInstance("Camellia/ECB/NoPadding", "BC");
052: out = Cipher.getInstance("Camellia/ECB/NoPadding", "BC");
053:
054: try {
055: out.init(Cipher.ENCRYPT_MODE, key);
056: } catch (Exception e) {
057: fail("Camellia failed initialisation - " + e.toString(), e);
058: }
059:
060: try {
061: in.init(Cipher.DECRYPT_MODE, key);
062: } catch (Exception e) {
063: fail("Camellia failed initialisation - " + e.toString(), e);
064: }
065:
066: //
067: // encryption pass
068: //
069: bOut = new ByteArrayOutputStream();
070:
071: cOut = new CipherOutputStream(bOut, out);
072:
073: try {
074: for (int i = 0; i != input.length / 2; i++) {
075: cOut.write(input[i]);
076: }
077: cOut.write(input, input.length / 2, input.length
078: - input.length / 2);
079: cOut.close();
080: } catch (IOException e) {
081: fail("Camellia failed encryption - " + e.toString(), e);
082: }
083:
084: byte[] bytes;
085:
086: bytes = bOut.toByteArray();
087:
088: if (!areEqual(bytes, output)) {
089: fail("Camellia failed encryption - expected "
090: + new String(Hex.encode(output)) + " got "
091: + new String(Hex.encode(bytes)));
092: }
093:
094: //
095: // decryption pass
096: //
097: bIn = new ByteArrayInputStream(bytes);
098:
099: cIn = new CipherInputStream(bIn, in);
100:
101: try {
102: DataInputStream dIn = new DataInputStream(cIn);
103:
104: bytes = new byte[input.length];
105:
106: for (int i = 0; i != input.length / 2; i++) {
107: bytes[i] = (byte) dIn.read();
108: }
109: dIn.readFully(bytes, input.length / 2, bytes.length
110: - input.length / 2);
111: } catch (Exception e) {
112: fail("Camellia failed encryption - " + e.toString(), e);
113: }
114:
115: if (!areEqual(bytes, input)) {
116: fail("Camellia failed decryption - expected "
117: + new String(Hex.encode(input)) + " got "
118: + new String(Hex.encode(bytes)));
119: }
120: }
121:
122: public void performTest() throws Exception {
123: for (int i = 0; i != cipherTests.length; i += 4) {
124: test(Integer.parseInt(cipherTests[i]), Hex
125: .decode(cipherTests[i + 1]), Hex
126: .decode(cipherTests[i + 2]), Hex
127: .decode(cipherTests[i + 3]));
128: }
129:
130: byte[] kek1 = Hex.decode("000102030405060708090a0b0c0d0e0f");
131: byte[] in1 = Hex.decode("00112233445566778899aabbccddeeff");
132: byte[] out1 = Hex
133: .decode("635d6ac46eedebd3a7f4a06421a4cbd1746b24795ba2f708");
134:
135: wrapTest(1, "CamelliaWrap", kek1, in1, out1);
136:
137: String[] oids = {
138: NTTObjectIdentifiers.id_camellia128_cbc.getId(),
139: NTTObjectIdentifiers.id_camellia192_cbc.getId(),
140: NTTObjectIdentifiers.id_camellia256_cbc.getId() };
141:
142: String[] names = { "Camellia/CBC/PKCS7Padding",
143: "Camellia/CBC/PKCS7Padding",
144: "Camellia/CBC/PKCS7Padding" };
145:
146: oidTest(oids, names, 1);
147:
148: String[] wrapOids = {
149: NTTObjectIdentifiers.id_camellia128_wrap.getId(),
150: NTTObjectIdentifiers.id_camellia192_wrap.getId(),
151: NTTObjectIdentifiers.id_camellia256_wrap.getId() };
152:
153: wrapOidTest(wrapOids, "CamelliaWrap");
154: }
155:
156: public static void main(String[] args) {
157: Security.addProvider(new BouncyCastleProvider());
158:
159: runTest(new CamelliaTest());
160: }
161: }
|