001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.jce.provider.BouncyCastleProvider;
004: import org.bouncycastle.util.encoders.Hex;
005:
006: import javax.crypto.Cipher;
007: import javax.crypto.CipherInputStream;
008: import javax.crypto.CipherOutputStream;
009: import javax.crypto.spec.SecretKeySpec;
010: import java.io.ByteArrayInputStream;
011: import java.io.ByteArrayOutputStream;
012: import java.io.DataInputStream;
013: import java.io.IOException;
014: import java.security.Key;
015: import java.security.Security;
016:
017: /**
018: * basic test class for SEED
019: */
020: public class NoekeonTest extends BaseBlockCipherTest {
021: static String[] cipherTests = { "128",
022: "b1656851699e29fa24b70148503d2dfc",
023: "2a78421b87c7d0924f26113f1d1349b2",
024: "e2f687e07b75660ffc372233bc47532c" };
025:
026: public NoekeonTest() {
027: super ("Noekeon");
028: }
029:
030: public void test(int strength, byte[] keyBytes, byte[] input,
031: byte[] output) throws Exception {
032: Key key;
033: Cipher in, out;
034: CipherInputStream cIn;
035: CipherOutputStream cOut;
036: ByteArrayInputStream bIn;
037: ByteArrayOutputStream bOut;
038:
039: key = new SecretKeySpec(keyBytes, "Noekeon");
040:
041: in = Cipher.getInstance("Noekeon/ECB/NoPadding", "BC");
042: out = Cipher.getInstance("Noekeon/ECB/NoPadding", "BC");
043:
044: try {
045: out.init(Cipher.ENCRYPT_MODE, key);
046: } catch (Exception e) {
047: fail("Noekeon failed initialisation - " + e.toString(), e);
048: }
049:
050: try {
051: in.init(Cipher.DECRYPT_MODE, key);
052: } catch (Exception e) {
053: fail("Noekeoen failed initialisation - " + e.toString(), e);
054: }
055:
056: //
057: // encryption pass
058: //
059: bOut = new ByteArrayOutputStream();
060:
061: cOut = new CipherOutputStream(bOut, out);
062:
063: try {
064: for (int i = 0; i != input.length / 2; i++) {
065: cOut.write(input[i]);
066: }
067: cOut.write(input, input.length / 2, input.length
068: - input.length / 2);
069: cOut.close();
070: } catch (IOException e) {
071: fail("Noekeon failed encryption - " + e.toString(), e);
072: }
073:
074: byte[] bytes;
075:
076: bytes = bOut.toByteArray();
077:
078: if (!areEqual(bytes, output)) {
079: fail("Noekeon failed encryption - expected "
080: + new String(Hex.encode(output)) + " got "
081: + new String(Hex.encode(bytes)));
082: }
083:
084: //
085: // decryption pass
086: //
087: bIn = new ByteArrayInputStream(bytes);
088:
089: cIn = new CipherInputStream(bIn, in);
090:
091: try {
092: DataInputStream dIn = new DataInputStream(cIn);
093:
094: bytes = new byte[input.length];
095:
096: for (int i = 0; i != input.length / 2; i++) {
097: bytes[i] = (byte) dIn.read();
098: }
099: dIn.readFully(bytes, input.length / 2, bytes.length
100: - input.length / 2);
101: } catch (Exception e) {
102: fail("Noekeon failed encryption - " + e.toString(), e);
103: }
104:
105: if (!areEqual(bytes, input)) {
106: fail("Noekeon failed decryption - expected "
107: + new String(Hex.encode(input)) + " got "
108: + new String(Hex.encode(bytes)));
109: }
110: }
111:
112: public void performTest() throws Exception {
113: for (int i = 0; i != cipherTests.length; i += 4) {
114: test(Integer.parseInt(cipherTests[i]), Hex
115: .decode(cipherTests[i + 1]), Hex
116: .decode(cipherTests[i + 2]), Hex
117: .decode(cipherTests[i + 3]));
118: }
119: }
120:
121: public static void main(String[] args) {
122: Security.addProvider(new BouncyCastleProvider());
123:
124: runTest(new NoekeonTest());
125: }
126: }
|