1package org.bouncycastle.crypto.params; 2 3public class DESedeParameters 4 extends DESParameters 5{ 6 /* 7 * DES-EDE Key length in bytes. 8 */ 9 static public final int DES_EDE_KEY_LENGTH = 24; 10 11 public DESedeParameters( 12 byte[] key) 13 { 14 super(key); 15 16 if (isWeakKey(key, 0, key.length)) 17 { 18 throw new IllegalArgumentException("attempt to create weak DESede key"); 19 } 20 } 21 22 /** 23 * return true if the passed in key is a DES-EDE weak key. 24 * 25 * @param key bytes making up the key 26 * @param offset offset into the byte array the key starts at 27 * @param length number of bytes making up the key 28 */ 29 public static boolean isWeakKey( 30 byte[] key, 31 int offset, 32 int length) 33 { 34 for (int i = offset; i < length; i += DES_KEY_LENGTH) 35 { 36 if (DESParameters.isWeakKey(key, i)) 37 { 38 return true; 39 } 40 } 41 42 return false; 43 } 44 45 /** 46 * return true if the passed in key is a DES-EDE weak key. 47 * 48 * @param key bytes making up the key 49 * @param offset offset into the byte array the key starts at 50 */ 51 public static boolean isWeakKey( 52 byte[] key, 53 int offset) 54 { 55 return isWeakKey(key, offset, key.length - offset); 56 } 57 58 /** 59 * return true if the passed in key is a real 2/3 part DES-EDE key. 60 * 61 * @param key bytes making up the key 62 * @param offset offset into the byte array the key starts at 63 */ 64 public static boolean isRealEDEKey(byte[] key, int offset) 65 { 66 return key.length == 16 ? isReal2Key(key, offset) : isReal3Key(key, offset); 67 } 68 69 /** 70 * return true if the passed in key is a real 2 part DES-EDE key. 71 * 72 * @param key bytes making up the key 73 * @param offset offset into the byte array the key starts at 74 */ 75 public static boolean isReal2Key(byte[] key, int offset) 76 { 77 boolean isValid = false; 78 for (int i = offset; i != offset + 8; i++) 79 { 80 if (key[i] != key[i + 8]) 81 { 82 isValid = true; 83 } 84 } 85 86 return isValid; 87 } 88 89 /** 90 * return true if the passed in key is a real 3 part DES-EDE key. 91 * 92 * @param key bytes making up the key 93 * @param offset offset into the byte array the key starts at 94 */ 95 public static boolean isReal3Key(byte[] key, int offset) 96 { 97 boolean diff12 = false, diff13 = false, diff23 = false; 98 for (int i = offset; i != offset + 8; i++) 99 { 100 diff12 |= (key[i] != key[i + 8]); 101 diff13 |= (key[i] != key[i + 16]); 102 diff23 |= (key[i + 8] != key[i + 16]); 103 } 104 return diff12 && diff13 && diff23; 105 } 106} 107