001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.KeyGenerationParameters;
004: import org.bouncycastle.crypto.Wrapper;
005: import org.bouncycastle.crypto.engines.DESedeEngine;
006: import org.bouncycastle.crypto.engines.DESedeWrapEngine;
007: import org.bouncycastle.crypto.generators.DESedeKeyGenerator;
008: import org.bouncycastle.crypto.params.DESedeParameters;
009: import org.bouncycastle.crypto.params.KeyParameter;
010: import org.bouncycastle.crypto.params.ParametersWithIV;
011: import org.bouncycastle.util.encoders.Hex;
012: import org.bouncycastle.util.test.SimpleTest;
013:
014: import java.security.SecureRandom;
015:
016: /**
017: * DESede tester
018: */
019: public class DESedeTest extends CipherTest {
020: static private byte[] weakKey = // first 8 bytes non-weak
021: { (byte) 0x06, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
022: (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x1f,
023: (byte) 0x1f, (byte) 0x1f, (byte) 0x1f, (byte) 0x0e,
024: (byte) 0x0e, (byte) 0x0e, (byte) 0x0e, (byte) 0xe0,
025: (byte) 0xe0, (byte) 0xe0, (byte) 0xe0, (byte) 0xf1,
026: (byte) 0xf1, (byte) 0xf1, (byte) 0xf1, };
027:
028: static String input1 = "4e6f77206973207468652074696d6520666f7220616c6c20";
029: static String input2 = "4e6f7720697320746865";
030:
031: static SimpleTest[] tests = {
032: new BlockCipherVectorTest(
033: 0,
034: new DESedeEngine(),
035: new DESedeParameters(Hex
036: .decode("0123456789abcdef0123456789abcdef")),
037: input1,
038: "3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53"),
039: new BlockCipherVectorTest(
040: 1,
041: new DESedeEngine(),
042: new DESedeParameters(Hex
043: .decode("0123456789abcdeffedcba9876543210")),
044: input1,
045: "d80a0d8b2bae5e4e6a0094171abcfc2775d2235a706e232c"),
046: new BlockCipherVectorTest(
047: 2,
048: new DESedeEngine(),
049: new DESedeParameters(
050: Hex
051: .decode("0123456789abcdef0123456789abcdef0123456789abcdef")),
052: input1,
053: "3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53"),
054: new BlockCipherVectorTest(
055: 3,
056: new DESedeEngine(),
057: new DESedeParameters(
058: Hex
059: .decode("0123456789abcdeffedcba98765432100123456789abcdef")),
060: input1,
061: "d80a0d8b2bae5e4e6a0094171abcfc2775d2235a706e232c") };
062:
063: DESedeTest() {
064: super (tests, new DESedeEngine(), new KeyParameter(new byte[16]));
065: }
066:
067: private void wrapTest(int id, byte[] kek, byte[] iv, byte[] in,
068: byte[] out) {
069: Wrapper wrapper = new DESedeWrapEngine();
070:
071: wrapper.init(true, new ParametersWithIV(new KeyParameter(kek),
072: iv));
073:
074: try {
075: byte[] cText = wrapper.wrap(in, 0, in.length);
076: if (!areEqual(cText, out)) {
077: fail(": failed wrap test " + id + " expected "
078: + new String(Hex.encode(out)) + " got "
079: + new String(Hex.encode(cText)));
080: }
081: } catch (Exception e) {
082: fail("failed wrap test exception: " + e.toString(), e);
083: }
084:
085: wrapper.init(false, new KeyParameter(kek));
086:
087: try {
088: byte[] pText = wrapper.unwrap(out, 0, out.length);
089: if (!areEqual(pText, in)) {
090: fail("failed unwrap test " + id + " expected "
091: + new String(Hex.encode(in)) + " got "
092: + new String(Hex.encode(pText)));
093: }
094: } catch (Exception e) {
095: fail("failed unwrap test exception: " + e.toString(), e);
096: }
097: }
098:
099: public void performTest() throws Exception {
100: super .performTest();
101:
102: byte[] kek1 = Hex
103: .decode("255e0d1c07b646dfb3134cc843ba8aa71f025b7c0838251f");
104: byte[] iv1 = Hex.decode("5dd4cbfc96f5453b");
105: byte[] in1 = Hex
106: .decode("2923bf85e06dd6ae529149f1f1bae9eab3a7da3d860d3e98");
107: byte[] out1 = Hex
108: .decode("690107618ef092b3b48ca1796b234ae9fa33ebb4159604037db5d6a84eb3aac2768c632775a467d4");
109:
110: wrapTest(1, kek1, iv1, in1, out1);
111:
112: //
113: // key generation
114: //
115: SecureRandom random = new SecureRandom();
116: DESedeKeyGenerator keyGen = new DESedeKeyGenerator();
117:
118: keyGen.init(new KeyGenerationParameters(random, 112));
119:
120: byte[] kB = keyGen.generateKey();
121:
122: if (kB.length != 16) {
123: fail("112 bit key wrong length.");
124: }
125:
126: keyGen.init(new KeyGenerationParameters(random, 168));
127:
128: kB = keyGen.generateKey();
129:
130: if (kB.length != 24) {
131: fail("168 bit key wrong length.");
132: }
133:
134: try {
135: keyGen.init(new KeyGenerationParameters(random, 200));
136:
137: fail("invalid key length not detected.");
138: } catch (IllegalArgumentException e) {
139: // expected
140: }
141:
142: try {
143: DESedeParameters.isWeakKey(new byte[4], 0);
144: fail("no exception on small key");
145: } catch (IllegalArgumentException e) {
146: if (!e.getMessage().equals("key material too short.")) {
147: fail("wrong exception");
148: }
149: }
150:
151: try {
152: new DESedeParameters(weakKey);
153: fail("no exception on weak key");
154: } catch (IllegalArgumentException e) {
155: if (!e.getMessage().equals(
156: "attempt to create weak DESede key")) {
157: fail("wrong exception");
158: }
159: }
160: }
161:
162: public String getName() {
163: return "DESede";
164: }
165:
166: public static void main(String[] args) {
167: runTest(new DESedeTest());
168: }
169: }
|