/* DES.java -- Copyright (C) 2002, 2003, 2006 Free Software Foundation, Inc. This file is a part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package gnu.javax.crypto.cipher; import gnu.java.security.Registry; import gnu.java.security.Properties; import gnu.java.security.util.Util; import java.security.InvalidKeyException; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; /** * The Data Encryption Standard. DES is a 64-bit block cipher with a 56-bit * key, developed by IBM in the 1970's for the standardization process begun by * the National Bureau of Standards (now NIST). *
* New applications should not use DES except for compatibility. *
* This version is based upon the description and sample implementation in * [1]. *
* References: *
true
if the first 8 bytes of kb have been
* parity adjusted. false
otherwise.
*/
public static boolean isParityAdjusted(byte[] kb, int offset)
{
int w = 0x88888888;
int n = PARITY[kb[offset + 0] & 0xff];
n <<= 4;
n |= PARITY[kb[offset + 1] & 0xff];
n <<= 4;
n |= PARITY[kb[offset + 2] & 0xff];
n <<= 4;
n |= PARITY[kb[offset + 3] & 0xff];
n <<= 4;
n |= PARITY[kb[offset + 4] & 0xff];
n <<= 4;
n |= PARITY[kb[offset + 5] & 0xff];
n <<= 4;
n |= PARITY[kb[offset + 6] & 0xff];
n <<= 4;
n |= PARITY[kb[offset + 7] & 0xff];
return (n & w) == 0;
}
/**
* Test if a key is a weak key.
*
* @param kb The key to test.
* @return true
if the key is weak.
*/
public static boolean isWeak(byte[] kb)
{
for (int i = 0; i < WEAK_KEYS.length; i++)
if (Arrays.equals(WEAK_KEYS[i], kb))
return true;
return false;
}
/**
* Test if a key is a semi-weak key.
*
* @param kb The key to test.
* @return true
if this key is semi-weak.
*/
public static boolean isSemiWeak(byte[] kb)
{
for (int i = 0; i < SEMIWEAK_KEYS.length; i++)
if (Arrays.equals(SEMIWEAK_KEYS[i], kb))
return true;
return false;
}
/**
* Test if the designated byte array represents a possibly weak key.
*
* @param kb the byte array to test.
* @return true
if kb
represents a possibly weak key.
* Returns false
otherwise.
*/
public static boolean isPossibleWeak(byte[] kb)
{
for (int i = 0; i < POSSIBLE_WEAK_KEYS.length; i++)
if (Arrays.equals(POSSIBLE_WEAK_KEYS[i], kb))
return true;
return false;
}
/**
* The core DES function. This is used for both encryption and decryption,
* the only difference being the key.
*
* @param in The input bytes.
* @param i The starting offset into the input bytes.
* @param out The output bytes.
* @param o The starting offset into the output bytes.
* @param key The working key.
*/
private static void desFunc(byte[] in, int i, byte[] out, int o, int[] key)
{
int right, left, work;
// Load.
left = (in[i++] & 0xff) << 24
| (in[i++] & 0xff) << 16
| (in[i++] & 0xff) << 8
| in[i++] & 0xff;
right = (in[i++] & 0xff) << 24
| (in[i++] & 0xff) << 16
| (in[i++] & 0xff) << 8
| in[i ] & 0xff;
// Initial permutation.
work = ((left >>> 4) ^ right) & 0x0F0F0F0F;
left ^= work << 4;
right ^= work;
work = ((left >>> 16) ^ right) & 0x0000FFFF;
left ^= work << 16;
right ^= work;
work = ((right >>> 2) ^ left) & 0x33333333;
right ^= work << 2;
left ^= work;
work = ((right >>> 8) ^ left) & 0x00FF00FF;
right ^= work << 8;
left ^= work;
right = ((right << 1) | ((right >>> 31) & 1)) & 0xFFFFFFFF;
work = (left ^ right) & 0xAAAAAAAA;
left ^= work;
right ^= work;
left = ((left << 1) | ((left >>> 31) & 1)) & 0xFFFFFFFF;
int k = 0, t;
for (int round = 0; round < 8; round++)
{
work = right >>> 4 | right << 28;
work ^= key[k++];
t = SP7[work & 0x3F];
work >>>= 8;
t |= SP5[work & 0x3F];
work >>>= 8;
t |= SP3[work & 0x3F];
work >>>= 8;
t |= SP1[work & 0x3F];
work = right ^ key[k++];
t |= SP8[work & 0x3F];
work >>>= 8;
t |= SP6[work & 0x3F];
work >>>= 8;
t |= SP4[work & 0x3F];
work >>>= 8;
t |= SP2[work & 0x3F];
left ^= t;
work = left >>> 4 | left << 28;
work ^= key[k++];
t = SP7[work & 0x3F];
work >>>= 8;
t |= SP5[work & 0x3F];
work >>>= 8;
t |= SP3[work & 0x3F];
work >>>= 8;
t |= SP1[work & 0x3F];
work = left ^ key[k++];
t |= SP8[work & 0x3F];
work >>>= 8;
t |= SP6[work & 0x3F];
work >>>= 8;
t |= SP4[work & 0x3F];
work >>>= 8;
t |= SP2[work & 0x3F];
right ^= t;
}
// The final permutation.
right = (right << 31) | (right >>> 1);
work = (left ^ right) & 0xAAAAAAAA;
left ^= work;
right ^= work;
left = (left << 31) | (left >>> 1);
work = ((left >>> 8) ^ right) & 0x00FF00FF;
left ^= work << 8;
right ^= work;
work = ((left >>> 2) ^ right) & 0x33333333;
left ^= work << 2;
right ^= work;
work = ((right >>> 16) ^ left) & 0x0000FFFF;
right ^= work << 16;
left ^= work;
work = ((right >>> 4) ^ left) & 0x0F0F0F0F;
right ^= work << 4;
left ^= work;
out[o++] = (byte)(right >>> 24);
out[o++] = (byte)(right >>> 16);
out[o++] = (byte)(right >>> 8);
out[o++] = (byte) right;
out[o++] = (byte)(left >>> 24);
out[o++] = (byte)(left >>> 16);
out[o++] = (byte)(left >>> 8);
out[o ] = (byte) left;
}
public Object clone()
{
return new DES();
}
public Iterator blockSizes()
{
return Collections.singleton(Integer.valueOf(BLOCK_SIZE)).iterator();
}
public Iterator keySizes()
{
return Collections.singleton(Integer.valueOf(KEY_SIZE)).iterator();
}
public Object makeKey(byte[] kb, int bs) throws InvalidKeyException
{
if (kb == null || kb.length != KEY_SIZE)
throw new InvalidKeyException("DES keys must be 8 bytes long");
if (Properties.checkForWeakKeys()
&& (isWeak(kb) || isSemiWeak(kb) || isPossibleWeak(kb)))
throw new WeakKeyException();
int i, j, l, m, n;
long pc1m = 0, pcr = 0;
for (i = 0; i < 56; i++)
{
l = PC1[i];
pc1m |= ((kb[l >>> 3] & (0x80 >>> (l & 7))) != 0) ? (1L << (55 - i))
: 0;
}
Context ctx = new Context();
// Encryption key first.
for (i = 0; i < 16; i++)
{
pcr = 0;
m = i << 1;
n = m + 1;
for (j = 0; j < 28; j++)
{
l = j + ROTARS[i];
if (l < 28)
pcr |= ((pc1m & 1L << (55 - l)) != 0) ? (1L << (55 - j)) : 0;
else
pcr |= ((pc1m & 1L << (55 - (l - 28))) != 0) ? (1L << (55 - j))
: 0;
}
for (j = 28; j < 56; j++)
{
l = j + ROTARS[i];
if (l < 56)
pcr |= ((pc1m & 1L << (55 - l)) != 0) ? (1L << (55 - j)) : 0;
else
pcr |= ((pc1m & 1L << (55 - (l - 28))) != 0) ? (1L << (55 - j))
: 0;
}
for (j = 0; j < 24; j++)
{
if ((pcr & 1L << (55 - PC2[j])) != 0)
ctx.ek[m] |= 1 << (23 - j);
if ((pcr & 1L << (55 - PC2[j + 24])) != 0)
ctx.ek[n] |= 1 << (23 - j);
}
}
// The decryption key is the same, but in reversed order.
for (i = 0; i < Context.EXPANDED_KEY_SIZE; i += 2)
{
ctx.dk[30 - i] = ctx.ek[i];
ctx.dk[31 - i] = ctx.ek[i + 1];
}
// "Cook" the keys.
for (i = 0; i < 32; i += 2)
{
int x, y;
x = ctx.ek[i];
y = ctx.ek[i + 1];
ctx.ek[i ] = ((x & 0x00FC0000) << 6)
| ((x & 0x00000FC0) << 10)
| ((y & 0x00FC0000) >>> 10)
| ((y & 0x00000FC0) >>> 6);
ctx.ek[i + 1] = ((x & 0x0003F000) << 12)
| ((x & 0x0000003F) << 16)
| ((y & 0x0003F000) >>> 4)
| (y & 0x0000003F);
x = ctx.dk[i];
y = ctx.dk[i + 1];
ctx.dk[i ] = ((x & 0x00FC0000) << 6)
| ((x & 0x00000FC0) << 10)
| ((y & 0x00FC0000) >>> 10)
| ((y & 0x00000FC0) >>> 6);
ctx.dk[i + 1] = ((x & 0x0003F000) << 12)
| ((x & 0x0000003F) << 16)
| ((y & 0x0003F000) >>> 4)
| (y & 0x0000003F);
}
return ctx;
}
public void encrypt(byte[] in, int i, byte[] out, int o, Object K, int bs)
{
desFunc(in, i, out, o, ((Context) K).ek);
}
public void decrypt(byte[] in, int i, byte[] out, int o, Object K, int bs)
{
desFunc(in, i, out, o, ((Context) K).dk);
}
/**
* Simple wrapper class around the session keys. Package-private so TripleDES
* can see it.
*/
final class Context
{
private static final int EXPANDED_KEY_SIZE = 32;
/** The encryption key. */
int[] ek;
/** The decryption key. */
int[] dk;
/** Default 0-arguments constructor. */
Context()
{
ek = new int[EXPANDED_KEY_SIZE];
dk = new int[EXPANDED_KEY_SIZE];
}
byte[] getEncryptionKeyBytes()
{
return toByteArray(ek);
}
byte[] getDecryptionKeyBytes()
{
return toByteArray(dk);
}
byte[] toByteArray(int[] k)
{
byte[] result = new byte[4 * k.length];
for (int i = 0, j = 0; i < k.length; i++)
{
result[j++] = (byte)(k[i] >>> 24);
result[j++] = (byte)(k[i] >>> 16);
result[j++] = (byte)(k[i] >>> 8);
result[j++] = (byte) k[i];
}
return result;
}
}
}