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