001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.asn1.kisa.KISAObjectIdentifiers;
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 SEED
020: */
021: public class SEEDTest extends BaseBlockCipherTest {
022: static String[] cipherTests = { "128",
023: "28DBC3BC49FFD87DCFA509B11D422BE7",
024: "B41E6BE2EBA84A148E2EED84593C5EC7",
025: "9B9B7BFCD1813CB95D0B3618F40F5122" };
026:
027: public SEEDTest() {
028: super ("SEED");
029: }
030:
031: public void test(int strength, byte[] keyBytes, byte[] input,
032: byte[] output) throws Exception {
033: Key key;
034: Cipher in, out;
035: CipherInputStream cIn;
036: CipherOutputStream cOut;
037: ByteArrayInputStream bIn;
038: ByteArrayOutputStream bOut;
039:
040: key = new SecretKeySpec(keyBytes, "SEED");
041:
042: in = Cipher.getInstance("SEED/ECB/NoPadding", "BC");
043: out = Cipher.getInstance("SEED/ECB/NoPadding", "BC");
044:
045: try {
046: out.init(Cipher.ENCRYPT_MODE, key);
047: } catch (Exception e) {
048: fail("SEED failed initialisation - " + e.toString(), e);
049: }
050:
051: try {
052: in.init(Cipher.DECRYPT_MODE, key);
053: } catch (Exception e) {
054: fail("SEED failed initialisation - " + e.toString(), e);
055: }
056:
057: //
058: // encryption pass
059: //
060: bOut = new ByteArrayOutputStream();
061:
062: cOut = new CipherOutputStream(bOut, out);
063:
064: try {
065: for (int i = 0; i != input.length / 2; i++) {
066: cOut.write(input[i]);
067: }
068: cOut.write(input, input.length / 2, input.length
069: - input.length / 2);
070: cOut.close();
071: } catch (IOException e) {
072: fail("SEED failed encryption - " + e.toString(), e);
073: }
074:
075: byte[] bytes;
076:
077: bytes = bOut.toByteArray();
078:
079: if (!areEqual(bytes, output)) {
080: fail("SEED failed encryption - expected "
081: + new String(Hex.encode(output)) + " got "
082: + new String(Hex.encode(bytes)));
083: }
084:
085: //
086: // decryption pass
087: //
088: bIn = new ByteArrayInputStream(bytes);
089:
090: cIn = new CipherInputStream(bIn, in);
091:
092: try {
093: DataInputStream dIn = new DataInputStream(cIn);
094:
095: bytes = new byte[input.length];
096:
097: for (int i = 0; i != input.length / 2; i++) {
098: bytes[i] = (byte) dIn.read();
099: }
100: dIn.readFully(bytes, input.length / 2, bytes.length
101: - input.length / 2);
102: } catch (Exception e) {
103: fail("SEED failed encryption - " + e.toString(), e);
104: }
105:
106: if (!areEqual(bytes, input)) {
107: fail("SEED failed decryption - expected "
108: + new String(Hex.encode(input)) + " got "
109: + new String(Hex.encode(bytes)));
110: }
111: }
112:
113: public void performTest() throws Exception {
114: for (int i = 0; i != cipherTests.length; i += 4) {
115: test(Integer.parseInt(cipherTests[i]), Hex
116: .decode(cipherTests[i + 1]), Hex
117: .decode(cipherTests[i + 2]), Hex
118: .decode(cipherTests[i + 3]));
119: }
120:
121: byte[] kek1 = Hex.decode("000102030405060708090a0b0c0d0e0f");
122: byte[] in1 = Hex.decode("00112233445566778899aabbccddeeff");
123: byte[] out1 = Hex
124: .decode("bf71f77138b5afea05232a8dad54024e812dc8dd7d132559");
125:
126: wrapTest(1, "SEEDWrap", kek1, in1, out1);
127:
128: String[] oids = { KISAObjectIdentifiers.id_seedCBC.getId() };
129:
130: String[] names = { "SEED/CBC/PKCS7Padding" };
131:
132: oidTest(oids, names, 1);
133:
134: String[] wrapOids = { KISAObjectIdentifiers.id_npki_app_cmsSeed_wrap
135: .getId() };
136:
137: wrapOidTest(wrapOids, "SEEDWrap");
138: }
139:
140: public static void main(String[] args) {
141: Security.addProvider(new BouncyCastleProvider());
142:
143: runTest(new SEEDTest());
144: }
145: }
|