001: package org.bouncycastle.crypto.engines;
002:
003: import org.bouncycastle.crypto.BlockCipher;
004: import org.bouncycastle.crypto.CipherParameters;
005: import org.bouncycastle.crypto.DataLengthException;
006: import org.bouncycastle.crypto.params.KeyParameter;
007:
008: /**
009: * a class that provides a basic DES engine.
010: */
011: public class DESEngine implements BlockCipher {
012: protected static final int BLOCK_SIZE = 8;
013:
014: private int[] workingKey = null;
015:
016: /**
017: * standard constructor.
018: */
019: public DESEngine() {
020: }
021:
022: /**
023: * initialise a DES cipher.
024: *
025: * @param encrypting whether or not we are for encryption.
026: * @param params the parameters required to set up the cipher.
027: * @exception IllegalArgumentException if the params argument is
028: * inappropriate.
029: */
030: public void init(boolean encrypting, CipherParameters params) {
031: if (params instanceof KeyParameter) {
032: if (((KeyParameter) params).getKey().length > 8) {
033: throw new IllegalArgumentException(
034: "DES key too long - should be 8 bytes");
035: }
036:
037: workingKey = generateWorkingKey(encrypting,
038: ((KeyParameter) params).getKey());
039:
040: return;
041: }
042:
043: throw new IllegalArgumentException(
044: "invalid parameter passed to DES init - "
045: + params.getClass().getName());
046: }
047:
048: public String getAlgorithmName() {
049: return "DES";
050: }
051:
052: public int getBlockSize() {
053: return BLOCK_SIZE;
054: }
055:
056: public int processBlock(byte[] in, int inOff, byte[] out, int outOff) {
057: if (workingKey == null) {
058: throw new IllegalStateException(
059: "DES engine not initialised");
060: }
061:
062: if ((inOff + BLOCK_SIZE) > in.length) {
063: throw new DataLengthException("input buffer too short");
064: }
065:
066: if ((outOff + BLOCK_SIZE) > out.length) {
067: throw new DataLengthException("output buffer too short");
068: }
069:
070: desFunc(workingKey, in, inOff, out, outOff);
071:
072: return BLOCK_SIZE;
073: }
074:
075: public void reset() {
076: }
077:
078: /**
079: * what follows is mainly taken from "Applied Cryptography", by
080: * Bruce Schneier, however it also bears great resemblance to Richard
081: * Outerbridge's D3DES...
082: */
083:
084: private static final short[] Df_Key = { 0x01, 0x23, 0x45, 0x67,
085: 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
086: 0x32, 0x10, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67 };
087:
088: private static final short[] bytebit = { 0200, 0100, 040, 020, 010,
089: 04, 02, 01 };
090:
091: private static final int[] bigbyte = { 0x800000, 0x400000,
092: 0x200000, 0x100000, 0x80000, 0x40000, 0x20000, 0x10000,
093: 0x8000, 0x4000, 0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100,
094: 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 };
095:
096: /*
097: * Use the key schedule specified in the Standard (ANSI X3.92-1981).
098: */
099:
100: private static final byte[] pc1 = { 56, 48, 40, 32, 24, 16, 8, 0,
101: 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10,
102: 2, 59, 51, 43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53,
103: 45, 37, 29, 21, 13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27,
104: 19, 11, 3 };
105:
106: private static final byte[] totrot = { 1, 2, 4, 6, 8, 10, 12, 14,
107: 15, 17, 19, 21, 23, 25, 27, 28 };
108:
109: private static final byte[] pc2 = { 13, 16, 10, 23, 0, 4, 2, 27,
110: 14, 5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,
111: 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38,
112: 55, 33, 52, 45, 41, 49, 35, 28, 31 };
113:
114: private static final int[] SP1 = { 0x01010400, 0x00000000,
115: 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004,
116: 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400,
117: 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404,
118: 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000,
119: 0x01010000, 0x01000404, 0x00010004, 0x01000004, 0x01000004,
120: 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000,
121: 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400,
122: 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000,
123: 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404,
124: 0x00010404, 0x01010404, 0x00010004, 0x01010000, 0x01000404,
125: 0x01000004, 0x00000404, 0x00010404, 0x01010400, 0x00000404,
126: 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400,
127: 0x00000000, 0x01010004 };
128:
129: private static final int[] SP2 = { 0x80108020, 0x80008000,
130: 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020,
131: 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000,
132: 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000,
133: 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000,
134: 0x00108020, 0x80100000, 0x00100020, 0x80000020, 0x00000000,
135: 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020,
136: 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020,
137: 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000,
138: 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000,
139: 0x80000000, 0x00008020, 0x80108000, 0x00100000, 0x80000020,
140: 0x00100020, 0x80008020, 0x80000020, 0x00100020, 0x00108000,
141: 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020,
142: 0x80108020, 0x00108000 };
143:
144: private static final int[] SP3 = { 0x00000208, 0x08020200,
145: 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208,
146: 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000,
147: 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000,
148: 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000,
149: 0x08020008, 0x00020208, 0x08000208, 0x00020200, 0x00020000,
150: 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000,
151: 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000,
152: 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008,
153: 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000,
154: 0x08020008, 0x08000208, 0x00020000, 0x08000000, 0x08020208,
155: 0x00000008, 0x00020208, 0x00020200, 0x08000008, 0x08020000,
156: 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008,
157: 0x08020008, 0x00020200 };
158:
159: private static final int[] SP4 = { 0x00802001, 0x00002081,
160: 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001,
161: 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081,
162: 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001,
163: 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000,
164: 0x00002001, 0x00002080, 0x00800081, 0x00000001, 0x00002080,
165: 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081,
166: 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081,
167: 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080,
168: 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081,
169: 0x00000080, 0x00802081, 0x00000081, 0x00000001, 0x00002000,
170: 0x00800001, 0x00002001, 0x00802080, 0x00800081, 0x00002001,
171: 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000,
172: 0x00002000, 0x00802080 };
173:
174: private static final int[] SP5 = { 0x00000100, 0x02080100,
175: 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000,
176: 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100,
177: 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000,
178: 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100,
179: 0x42080100, 0x02000100, 0x42080000, 0x40000100, 0x00000000,
180: 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100,
181: 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000,
182: 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000,
183: 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000,
184: 0x42080000, 0x42080100, 0x00080100, 0x42000000, 0x42080100,
185: 0x02080000, 0x00000000, 0x40080000, 0x42000000, 0x00080100,
186: 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000,
187: 0x02080100, 0x40000100 };
188:
189: private static final int[] SP6 = { 0x20000010, 0x20400000,
190: 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010,
191: 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010,
192: 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000,
193: 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010,
194: 0x00000010, 0x20400010, 0x20400010, 0x00000000, 0x00404010,
195: 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000,
196: 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010,
197: 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000,
198: 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000,
199: 0x20400000, 0x00404010, 0x20404000, 0x00000000, 0x20400010,
200: 0x00000010, 0x00004000, 0x20400000, 0x00404010, 0x00004000,
201: 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000,
202: 0x00400010, 0x20004010 };
203:
204: private static final int[] SP7 = { 0x00200000, 0x04200002,
205: 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802,
206: 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002,
207: 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800,
208: 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000,
209: 0x04200800, 0x00200002, 0x04200000, 0x00000800, 0x00000802,
210: 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800,
211: 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802,
212: 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000,
213: 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802,
214: 0x04200800, 0x00000802, 0x04000002, 0x04200802, 0x04200000,
215: 0x00200800, 0x00000000, 0x00000002, 0x04200802, 0x00000000,
216: 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800,
217: 0x00000800, 0x00200002 };
218:
219: private static final int[] SP8 = { 0x10001040, 0x00001000,
220: 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040,
221: 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000,
222: 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000,
223: 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040,
224: 0x10040040, 0x10041000, 0x00001040, 0x00000000, 0x00000000,
225: 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000,
226: 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040,
227: 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040,
228: 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000,
229: 0x10001040, 0x00000000, 0x10041040, 0x00040040, 0x10000040,
230: 0x10040000, 0x10001000, 0x10001040, 0x00000000, 0x10041040,
231: 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040,
232: 0x10000000, 0x10041000 };
233:
234: /**
235: * generate an integer based working key based on our secret key
236: * and what we processing we are planning to do.
237: *
238: * Acknowledgements for this routine go to James Gillogly & Phil Karn.
239: * (whoever, and wherever they are!).
240: */
241: protected int[] generateWorkingKey(boolean encrypting, byte[] key) {
242: int[] newKey = new int[32];
243: boolean[] pc1m = new boolean[56], pcr = new boolean[56];
244:
245: for (int j = 0; j < 56; j++) {
246: int l = pc1[j];
247:
248: pc1m[j] = ((key[l >>> 3] & bytebit[l & 07]) != 0);
249: }
250:
251: for (int i = 0; i < 16; i++) {
252: int l, m, n;
253:
254: if (encrypting) {
255: m = i << 1;
256: } else {
257: m = (15 - i) << 1;
258: }
259:
260: n = m + 1;
261: newKey[m] = newKey[n] = 0;
262:
263: for (int j = 0; j < 28; j++) {
264: l = j + totrot[i];
265: if (l < 28) {
266: pcr[j] = pc1m[l];
267: } else {
268: pcr[j] = pc1m[l - 28];
269: }
270: }
271:
272: for (int j = 28; j < 56; j++) {
273: l = j + totrot[i];
274: if (l < 56) {
275: pcr[j] = pc1m[l];
276: } else {
277: pcr[j] = pc1m[l - 28];
278: }
279: }
280:
281: for (int j = 0; j < 24; j++) {
282: if (pcr[pc2[j]]) {
283: newKey[m] |= bigbyte[j];
284: }
285:
286: if (pcr[pc2[j + 24]]) {
287: newKey[n] |= bigbyte[j];
288: }
289: }
290: }
291:
292: //
293: // store the processed key
294: //
295: for (int i = 0; i != 32; i += 2) {
296: int i1, i2;
297:
298: i1 = newKey[i];
299: i2 = newKey[i + 1];
300:
301: newKey[i] = ((i1 & 0x00fc0000) << 6)
302: | ((i1 & 0x00000fc0) << 10)
303: | ((i2 & 0x00fc0000) >>> 10)
304: | ((i2 & 0x00000fc0) >>> 6);
305:
306: newKey[i + 1] = ((i1 & 0x0003f000) << 12)
307: | ((i1 & 0x0000003f) << 16)
308: | ((i2 & 0x0003f000) >>> 4) | (i2 & 0x0000003f);
309: }
310:
311: return newKey;
312: }
313:
314: /**
315: * the DES engine.
316: */
317: protected void desFunc(int[] wKey, byte[] in, int inOff,
318: byte[] out, int outOff) {
319: int work, right, left;
320:
321: left = (in[inOff + 0] & 0xff) << 24;
322: left |= (in[inOff + 1] & 0xff) << 16;
323: left |= (in[inOff + 2] & 0xff) << 8;
324: left |= (in[inOff + 3] & 0xff);
325:
326: right = (in[inOff + 4] & 0xff) << 24;
327: right |= (in[inOff + 5] & 0xff) << 16;
328: right |= (in[inOff + 6] & 0xff) << 8;
329: right |= (in[inOff + 7] & 0xff);
330:
331: work = ((left >>> 4) ^ right) & 0x0f0f0f0f;
332: right ^= work;
333: left ^= (work << 4);
334: work = ((left >>> 16) ^ right) & 0x0000ffff;
335: right ^= work;
336: left ^= (work << 16);
337: work = ((right >>> 2) ^ left) & 0x33333333;
338: left ^= work;
339: right ^= (work << 2);
340: work = ((right >>> 8) ^ left) & 0x00ff00ff;
341: left ^= work;
342: right ^= (work << 8);
343: right = ((right << 1) | ((right >>> 31) & 1)) & 0xffffffff;
344: work = (left ^ right) & 0xaaaaaaaa;
345: left ^= work;
346: right ^= work;
347: left = ((left << 1) | ((left >>> 31) & 1)) & 0xffffffff;
348:
349: for (int round = 0; round < 8; round++) {
350: int fval;
351:
352: work = (right << 28) | (right >>> 4);
353: work ^= wKey[round * 4 + 0];
354: fval = SP7[work & 0x3f];
355: fval |= SP5[(work >>> 8) & 0x3f];
356: fval |= SP3[(work >>> 16) & 0x3f];
357: fval |= SP1[(work >>> 24) & 0x3f];
358: work = right ^ wKey[round * 4 + 1];
359: fval |= SP8[work & 0x3f];
360: fval |= SP6[(work >>> 8) & 0x3f];
361: fval |= SP4[(work >>> 16) & 0x3f];
362: fval |= SP2[(work >>> 24) & 0x3f];
363: left ^= fval;
364: work = (left << 28) | (left >>> 4);
365: work ^= wKey[round * 4 + 2];
366: fval = SP7[work & 0x3f];
367: fval |= SP5[(work >>> 8) & 0x3f];
368: fval |= SP3[(work >>> 16) & 0x3f];
369: fval |= SP1[(work >>> 24) & 0x3f];
370: work = left ^ wKey[round * 4 + 3];
371: fval |= SP8[work & 0x3f];
372: fval |= SP6[(work >>> 8) & 0x3f];
373: fval |= SP4[(work >>> 16) & 0x3f];
374: fval |= SP2[(work >>> 24) & 0x3f];
375: right ^= fval;
376: }
377:
378: right = (right << 31) | (right >>> 1);
379: work = (left ^ right) & 0xaaaaaaaa;
380: left ^= work;
381: right ^= work;
382: left = (left << 31) | (left >>> 1);
383: work = ((left >>> 8) ^ right) & 0x00ff00ff;
384: right ^= work;
385: left ^= (work << 8);
386: work = ((left >>> 2) ^ right) & 0x33333333;
387: right ^= work;
388: left ^= (work << 2);
389: work = ((right >>> 16) ^ left) & 0x0000ffff;
390: left ^= work;
391: right ^= (work << 16);
392: work = ((right >>> 4) ^ left) & 0x0f0f0f0f;
393: left ^= work;
394: right ^= (work << 4);
395:
396: out[outOff + 0] = (byte) ((right >>> 24) & 0xff);
397: out[outOff + 1] = (byte) ((right >>> 16) & 0xff);
398: out[outOff + 2] = (byte) ((right >>> 8) & 0xff);
399: out[outOff + 3] = (byte) (right & 0xff);
400: out[outOff + 4] = (byte) ((left >>> 24) & 0xff);
401: out[outOff + 5] = (byte) ((left >>> 16) & 0xff);
402: out[outOff + 6] = (byte) ((left >>> 8) & 0xff);
403: out[outOff + 7] = (byte) (left & 0xff);
404: }
405: }
|