001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.BlockCipher;
004: import org.bouncycastle.crypto.engines.DESEngine;
005: import org.bouncycastle.crypto.modes.CFBBlockCipher;
006: import org.bouncycastle.crypto.modes.OFBBlockCipher;
007: import org.bouncycastle.crypto.params.KeyParameter;
008: import org.bouncycastle.crypto.params.ParametersWithIV;
009: import org.bouncycastle.util.encoders.Hex;
010: import org.bouncycastle.util.test.SimpleTestResult;
011: import org.bouncycastle.util.test.Test;
012: import org.bouncycastle.util.test.TestResult;
013:
014: /**
015: * CFB/OFB Mode test of IV padding.
016: */
017: public class ModeTest implements Test {
018: public ModeTest() {
019: }
020:
021: private boolean isEqualTo(byte[] a, byte[] b) {
022: for (int i = 0; i != a.length; i++) {
023: if (a[i] != b[i]) {
024: return false;
025: }
026: }
027:
028: return true;
029: }
030:
031: public TestResult perform() {
032: KeyParameter key = new KeyParameter(Hex
033: .decode("0011223344556677"));
034: byte[] input = Hex.decode("4e6f7720");
035: byte[] out1 = new byte[4];
036: byte[] out2 = new byte[4];
037:
038: BlockCipher ofb = new OFBBlockCipher(new DESEngine(), 32);
039:
040: ofb.init(true, new ParametersWithIV(key, Hex
041: .decode("1122334455667788")));
042:
043: ofb.processBlock(input, 0, out1, 0);
044:
045: ofb.init(false, new ParametersWithIV(key, Hex
046: .decode("1122334455667788")));
047: ofb.processBlock(out1, 0, out2, 0);
048:
049: if (!isEqualTo(out2, input)) {
050: return new SimpleTestResult(false, getName()
051: + ": test 1 - in != out");
052: }
053:
054: ofb.init(true,
055: new ParametersWithIV(key, Hex.decode("11223344")));
056:
057: ofb.processBlock(input, 0, out1, 0);
058:
059: ofb.init(false, new ParametersWithIV(key, Hex
060: .decode("0000000011223344")));
061: ofb.processBlock(out1, 0, out2, 0);
062:
063: if (!isEqualTo(out2, input)) {
064: return new SimpleTestResult(false, getName()
065: + ": test 2 - in != out");
066: }
067:
068: BlockCipher cfb = new CFBBlockCipher(new DESEngine(), 32);
069:
070: cfb.init(true, new ParametersWithIV(key, Hex
071: .decode("1122334455667788")));
072:
073: cfb.processBlock(input, 0, out1, 0);
074:
075: cfb.init(false, new ParametersWithIV(key, Hex
076: .decode("1122334455667788")));
077: cfb.processBlock(out1, 0, out2, 0);
078:
079: if (!isEqualTo(out2, input)) {
080: return new SimpleTestResult(false, getName()
081: + ": test 3 - in != out");
082: }
083:
084: cfb.init(true,
085: new ParametersWithIV(key, Hex.decode("11223344")));
086:
087: cfb.processBlock(input, 0, out1, 0);
088:
089: cfb.init(false, new ParametersWithIV(key, Hex
090: .decode("0000000011223344")));
091: cfb.processBlock(out1, 0, out2, 0);
092:
093: if (!isEqualTo(out2, input)) {
094: return new SimpleTestResult(false, getName()
095: + ": test 4 - in != out");
096: }
097:
098: return new SimpleTestResult(true, getName() + ": Okay");
099: }
100:
101: public String getName() {
102: return "ModeTest";
103: }
104:
105: public static void main(String[] args) {
106: ModeTest test = new ModeTest();
107: TestResult result = test.perform();
108:
109: System.out.println(result);
110: }
111: }
|