001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.KeyGenerationParameters;
004: import org.bouncycastle.crypto.engines.DESEngine;
005: import org.bouncycastle.crypto.generators.DESKeyGenerator;
006: import org.bouncycastle.crypto.modes.CBCBlockCipher;
007: import org.bouncycastle.crypto.modes.CFBBlockCipher;
008: import org.bouncycastle.crypto.modes.OFBBlockCipher;
009: import org.bouncycastle.crypto.params.DESParameters;
010: import org.bouncycastle.crypto.params.KeyParameter;
011: import org.bouncycastle.crypto.params.ParametersWithIV;
012: import org.bouncycastle.util.encoders.Hex;
013: import org.bouncycastle.util.test.SimpleTest;
014:
015: import java.security.SecureRandom;
016:
017: class DESParityTest extends SimpleTest {
018: public String getName() {
019: return "DESParityTest";
020: }
021:
022: public void performTest() {
023: byte[] k1In = { (byte) 0xff, (byte) 0xff, (byte) 0xff,
024: (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
025: (byte) 0xff };
026: byte[] k1Out = { (byte) 0xfe, (byte) 0xfe, (byte) 0xfe,
027: (byte) 0xfe, (byte) 0xfe, (byte) 0xfe, (byte) 0xfe,
028: (byte) 0xfe };
029:
030: byte[] k2In = { (byte) 0xef, (byte) 0xcb, (byte) 0xda,
031: (byte) 0x4f, (byte) 0xaa, (byte) 0x99, (byte) 0x7f,
032: (byte) 0x63 };
033: byte[] k2Out = { (byte) 0xef, (byte) 0xcb, (byte) 0xda,
034: (byte) 0x4f, (byte) 0xab, (byte) 0x98, (byte) 0x7f,
035: (byte) 0x62 };
036:
037: DESParameters.setOddParity(k1In);
038:
039: for (int i = 0; i != k1In.length; i++) {
040: if (k1In[i] != k1Out[i]) {
041: fail("Failed " + "got " + new String(Hex.encode(k1In))
042: + " expected " + new String(Hex.encode(k1Out)));
043: }
044: }
045:
046: DESParameters.setOddParity(k2In);
047:
048: for (int i = 0; i != k2In.length; i++) {
049: if (k2In[i] != k2Out[i]) {
050: fail("Failed " + "got " + new String(Hex.encode(k2In))
051: + " expected " + new String(Hex.encode(k2Out)));
052: }
053: }
054: }
055: }
056:
057: class KeyGenTest extends SimpleTest {
058: public String getName() {
059: return "KeyGenTest";
060: }
061:
062: public void performTest() {
063: DESKeyGenerator keyGen = new DESKeyGenerator();
064:
065: keyGen
066: .init(new KeyGenerationParameters(new SecureRandom(),
067: 56));
068:
069: byte[] kB = keyGen.generateKey();
070:
071: if (kB.length != 8) {
072: fail("DES bit key wrong length.");
073: }
074: }
075: }
076:
077: class DESParametersTest extends SimpleTest {
078: static private byte[] weakKeys = { (byte) 0x01, (byte) 0x01,
079: (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
080: (byte) 0x01, (byte) 0x01, (byte) 0x1f, (byte) 0x1f,
081: (byte) 0x1f, (byte) 0x1f, (byte) 0x0e, (byte) 0x0e,
082: (byte) 0x0e, (byte) 0x0e, (byte) 0xe0, (byte) 0xe0,
083: (byte) 0xe0, (byte) 0xe0, (byte) 0xf1, (byte) 0xf1,
084: (byte) 0xf1, (byte) 0xf1, (byte) 0xfe, (byte) 0xfe,
085: (byte) 0xfe, (byte) 0xfe, (byte) 0xfe, (byte) 0xfe,
086: (byte) 0xfe, (byte) 0xfe,
087: /* semi-weak keys */
088: (byte) 0x01, (byte) 0xfe, (byte) 0x01, (byte) 0xfe,
089: (byte) 0x01, (byte) 0xfe, (byte) 0x01, (byte) 0xfe,
090: (byte) 0x1f, (byte) 0xe0, (byte) 0x1f, (byte) 0xe0,
091: (byte) 0x0e, (byte) 0xf1, (byte) 0x0e, (byte) 0xf1,
092: (byte) 0x01, (byte) 0xe0, (byte) 0x01, (byte) 0xe0,
093: (byte) 0x01, (byte) 0xf1, (byte) 0x01, (byte) 0xf1,
094: (byte) 0x1f, (byte) 0xfe, (byte) 0x1f, (byte) 0xfe,
095: (byte) 0x0e, (byte) 0xfe, (byte) 0x0e, (byte) 0xfe,
096: (byte) 0x01, (byte) 0x1f, (byte) 0x01, (byte) 0x1f,
097: (byte) 0x01, (byte) 0x0e, (byte) 0x01, (byte) 0x0e,
098: (byte) 0xe0, (byte) 0xfe, (byte) 0xe0, (byte) 0xfe,
099: (byte) 0xf1, (byte) 0xfe, (byte) 0xf1, (byte) 0xfe,
100: (byte) 0xfe, (byte) 0x01, (byte) 0xfe, (byte) 0x01,
101: (byte) 0xfe, (byte) 0x01, (byte) 0xfe, (byte) 0x01,
102: (byte) 0xe0, (byte) 0x1f, (byte) 0xe0, (byte) 0x1f,
103: (byte) 0xf1, (byte) 0x0e, (byte) 0xf1, (byte) 0x0e,
104: (byte) 0xe0, (byte) 0x01, (byte) 0xe0, (byte) 0x01,
105: (byte) 0xf1, (byte) 0x01, (byte) 0xf1, (byte) 0x01,
106: (byte) 0xfe, (byte) 0x1f, (byte) 0xfe, (byte) 0x1f,
107: (byte) 0xfe, (byte) 0x0e, (byte) 0xfe, (byte) 0x0e,
108: (byte) 0x1f, (byte) 0x01, (byte) 0x1f, (byte) 0x01,
109: (byte) 0x0e, (byte) 0x01, (byte) 0x0e, (byte) 0x01,
110: (byte) 0xfe, (byte) 0xe0, (byte) 0xfe, (byte) 0xe0,
111: (byte) 0xfe, (byte) 0xf1, (byte) 0xfe, (byte) 0xf1 };
112:
113: public String getName() {
114: return "DESParameters";
115: }
116:
117: public void performTest() throws Exception {
118: try {
119: DESParameters.isWeakKey(new byte[4], 0);
120: fail("no exception on small key");
121: } catch (IllegalArgumentException e) {
122: if (!e.getMessage().equals("key material too short.")) {
123: fail("wrong exception");
124: }
125: }
126:
127: try {
128: new DESParameters(weakKeys);
129: fail("no exception on weak key");
130: } catch (IllegalArgumentException e) {
131: if (!e.getMessage()
132: .equals("attempt to create weak DES key")) {
133: fail("wrong exception");
134: }
135: }
136:
137: for (int i = 0; i != weakKeys.length; i += 8) {
138: if (!DESParameters.isWeakKey(weakKeys, i)) {
139: fail("weakKey test failed");
140: }
141: }
142: }
143: }
144:
145: /**
146: * DES tester - vectors from <a href=http://www.itl.nist.gov/fipspubs/fip81.htm>FIPS 81</a>
147: */
148: public class DESTest extends CipherTest {
149: static String input1 = "4e6f77206973207468652074696d6520666f7220616c6c20";
150: static String input2 = "4e6f7720697320746865";
151: static String input3 = "4e6f7720697320746865aabbcc";
152:
153: static SimpleTest[] tests = {
154: new BlockCipherVectorTest(0, new DESEngine(),
155: new KeyParameter(Hex.decode("0123456789abcdef")),
156: input1,
157: "3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53"),
158: new BlockCipherVectorTest(1, new CBCBlockCipher(
159: new DESEngine()), new ParametersWithIV(
160: new KeyParameter(Hex.decode("0123456789abcdef")),
161: Hex.decode("1234567890abcdef")), input1,
162: "e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6"),
163: new BlockCipherVectorTest(2, new CFBBlockCipher(
164: new DESEngine(), 8), new ParametersWithIV(
165: new KeyParameter(Hex.decode("0123456789abcdef")),
166: Hex.decode("1234567890abcdef")), input2,
167: "f31fda07011462ee187f"),
168: new BlockCipherVectorTest(3, new CFBBlockCipher(
169: new DESEngine(), 64), new ParametersWithIV(
170: new KeyParameter(Hex.decode("0123456789abcdef")),
171: Hex.decode("1234567890abcdef")), input1,
172: "f3096249c7f46e51a69e839b1a92f78403467133898ea622"),
173: new BlockCipherVectorTest(4, new OFBBlockCipher(
174: new DESEngine(), 8), new ParametersWithIV(
175: new KeyParameter(Hex.decode("0123456789abcdef")),
176: Hex.decode("1234567890abcdef")), input2,
177: "f34a2850c9c64985d684"),
178: new BlockCipherVectorTest(5, new CFBBlockCipher(
179: new DESEngine(), 64), new ParametersWithIV(
180: new KeyParameter(Hex.decode("0123456789abcdef")),
181: Hex.decode("1234567890abcdef")), input3,
182: "f3096249c7f46e51a69e0954bf"),
183: new BlockCipherVectorTest(6, new OFBBlockCipher(
184: new DESEngine(), 64), new ParametersWithIV(
185: new KeyParameter(Hex.decode("0123456789abcdef")),
186: Hex.decode("1234567890abcdef")), input3,
187: "f3096249c7f46e5135f2c0eb8b"), new DESParityTest(),
188: new DESParametersTest(), new KeyGenTest() };
189:
190: public DESTest() {
191: super (tests, new DESEngine(), new KeyParameter(new byte[8]));
192: }
193:
194: public String getName() {
195: return "DES";
196: }
197:
198: public static void main(String[] args) {
199: runTest(new DESTest());
200: }
201: }
|