package org.bouncycastle.crypto.params;

public class DESedeParameters
    extends DESParameters
{
    /*
     * DES-EDE Key length in bytes.
     */
    static public final int DES_EDE_KEY_LENGTH = 24;

    public DESedeParameters(
        byte[]  key)
    {
        super(key);

        if (isWeakKey(key, 0, key.length))
        {
            throw new IllegalArgumentException("attempt to create weak DESede key");
        }
    }

    /**
     * return true if the passed in key is a DES-EDE weak key.
     *
     * @param key bytes making up the key
     * @param offset offset into the byte array the key starts at
     * @param length number of bytes making up the key
     */
    public static boolean isWeakKey(
        byte[]  key,
        int     offset,
        int     length)
    {
        for (int i = offset; i < length; i += DES_KEY_LENGTH)
        {
            if (DESParameters.isWeakKey(key, i))
            {
                return true;
            }
        }

        return false;
    }

    /**
     * return true if the passed in key is a DES-EDE weak key.
     *
     * @param key bytes making up the key
     * @param offset offset into the byte array the key starts at
     */
    public static boolean isWeakKey(
        byte[]  key,
        int     offset)
    {
        return isWeakKey(key, offset, key.length - offset);
    }

    /**
     * return true if the passed in key is a real 2/3 part DES-EDE key.
     *
     * @param key bytes making up the key
     * @param offset offset into the byte array the key starts at
     */
    public static boolean isRealEDEKey(byte[] key, int offset)
    {
        return key.length == 16 ? isReal2Key(key, offset) : isReal3Key(key, offset);
    }

    /**
     * return true if the passed in key is a real 2 part DES-EDE key.
     *
     * @param key bytes making up the key
     * @param offset offset into the byte array the key starts at
     */
    public static boolean isReal2Key(byte[] key, int offset)
    {
        boolean isValid = false;
        for (int i = offset; i != offset + 8; i++)
        {
            if (key[i] != key[i + 8])
            {
                isValid = true;
            }
        }

        return isValid;
    }

    /**
     * return true if the passed in key is a real 3 part DES-EDE key.
     *
     * @param key bytes making up the key
     * @param offset offset into the byte array the key starts at
     */
    public static boolean isReal3Key(byte[] key, int offset)
    {
        boolean isValid = false;
        for (int i = offset; i != offset + 8; i++)
        {
            if (key[i] != key[i + 8] && key[i] != key[i + 16] && key[i + 8] != key[i + 16])
            {
                isValid = true;
            }
        }

        return isValid;
    }

}

@@ -2499,39 +2499,70 @@
         boolean 
-isValid
+diff12 = false, diff13 = false, diff23
  = false;%0A      
@@ -2620,34 +2620,41 @@
   %7B%0A            
+d
 if
+f12 %7C=
  (key%5Bi%5D != key%5B
@@ -2663,46 +2663,39 @@
 + 8%5D
- && key%5Bi%5D != key%5Bi + 16%5D && 
+);%0A            diff13 %7C= (
 key%5Bi
- + 8
 %5D !=
@@ -2707,16 +2707,17 @@
 i + 16%5D)
+;
 %0A       
@@ -2725,55 +2725,46 @@
     
-%7B%0A                isValid = true;%0A            %7D
+diff23 %7C= (key%5Bi + 8%5D != key%5Bi + 16%5D);
 %0A   
@@ -2762,33 +2762,32 @@
 16%5D);%0A        %7D%0A
-%0A
         return i
@@ -2785,26 +2785,45 @@
  return 
-isValid
+diff12 && diff13 && diff23
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -2819,11 +2819,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Fix logic in isReal3Key (was overly strict before)