1 package iaik.pkcs.pkcs11.parameters; 2 3 import iaik.pkcs.pkcs11.TokenRuntimeException; 4 import iaik.pkcs.pkcs11.wrapper.Constants; 5 import iaik.pkcs.pkcs11.wrapper.Functions; 6 import iaik.pkcs.pkcs11.wrapper.PKCS11Constants; 7 8 /** 9 * This abstract class encapsulates parameters for the DH mechanisms 10 * Mechanism.ECDH1_DERIVE, Mechanism.ECDH1_COFACTOR_DERIVE, 11 * Mechanism.ECMQV_DERIVE, Mechanism.X9_42_DH_DERIVE , 12 * Mechanism.X9_42_DH_HYBRID_DERIVE and Mechanism.X9_42_MQV_DERIVE. 13 * 14 * @author Karl Scheibelhofer 15 * @version 1.0 16 * @invariants (keyDerivationFunction_ == KeyDerivationFunctionType.NULL) 17 * or (keyDerivationFunction_ == KeyDerivationFunctionType.SHA1_KDF) 18 * or (keyDerivationFunction_ == KeyDerivationFunctionType.SHA1_KDF_ASN1) 19 * or (keyDerivationFunction_ == KeyDerivationFunctionType.SHA1_KDF_CONCATENATE)) 20 * and (publicData_ <> null) 21 */ 22 abstract public class DHKeyDerivationParameters implements Parameters { 23 24 /** 25 * This interface defines the available key derivation function types as 26 * defined by PKCS#11: CKD_NULL, CKD_SHA1_KDF, CKD_SHA1_KDF_ASN1, 27 * CKD_SHA1_KDF_CONCATENATE. 28 * 29 * @author Karl Scheibelhofer 30 * @version 1.0 31 * @invariants 32 */ 33 public interface KeyDerivationFunctionType { 34 35 /** 36 * The indentifier for CKD_NULL. 37 */ 38 static public final long NULL = PKCS11Constants.CKD_NULL; 39 40 /** 41 * The indentifier for CKD_SHA1_KDF. 42 */ 43 static public final long SHA1_KDF = PKCS11Constants.CKD_SHA1_KDF; 44 45 /** 46 * The indentifier for CKD_SHA1_KDF_ASN1. 47 */ 48 static public final long SHA1_KDF_ASN1 = PKCS11Constants.CKD_SHA1_KDF_ASN1; 49 50 /** 51 * The indentifier for CKD_SHA1_KDF_CONCATENATE. 52 */ 53 static public final long SHA1_KDF_CONCATENATE = PKCS11Constants.CKD_SHA1_KDF_CONCATENATE; 54 55 } 56 57 /** 58 * The key derivation function used on the shared secret value. 59 */ 60 protected long keyDerivationFunction_; 61 62 /** 63 * The other partie's public key value. 64 */ 65 protected byte[] publicData_; 66 67 /** 68 * Create a new DHKeyDerivationParameters object with the given attributes. 69 * 70 * @param keyDerivationFunction The key derivation function used on the shared 71 * secret value. 72 * One of the values defined in 73 * KeyDerivationFunctionType. 74 * @param publicData The other partie's public key value. 75 * @preconditions ((keyDerivationFunction == KeyDerivationFunctionType.NULL) 76 * or (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF) 77 * or (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF_ASN1) 78 * or (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF_CONCATENATE)) 79 * and (publicData <> null) 80 * @postconditions 81 */ DHKeyDerivationParameters(long keyDerivationFunction, byte[] publicData)82 protected DHKeyDerivationParameters(long keyDerivationFunction, byte[] publicData) { 83 if ((keyDerivationFunction != KeyDerivationFunctionType.NULL) 84 && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF) 85 && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF_ASN1) 86 && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF_CONCATENATE)) { 87 throw new IllegalArgumentException( 88 "Illegal value for argument\"keyDerivationFunction\": " 89 + Functions.toHexString(keyDerivationFunction)); 90 } 91 if (publicData == null) { 92 throw new NullPointerException("Argument \"publicData\" must not be null."); 93 } 94 keyDerivationFunction_ = keyDerivationFunction; 95 publicData_ = publicData; 96 } 97 98 /** 99 * Create a (deep) clone of this object. 100 * 101 * @return A clone of this object. 102 * @preconditions 103 * @postconditions (result <> null) 104 * and (result instanceof DHKeyDerivationParameters) 105 * and (result.equals(this)) 106 */ clone()107 public java.lang.Object clone() { 108 DHKeyDerivationParameters clone; 109 110 try { 111 clone = (DHKeyDerivationParameters) super.clone(); 112 113 clone.publicData_ = (byte[]) this.publicData_.clone(); 114 } catch (CloneNotSupportedException ex) { 115 // this must not happen, because this class is cloneable 116 throw new TokenRuntimeException("An unexpected clone exception occurred.", ex); 117 } 118 119 return clone; 120 } 121 122 /** 123 * Get the key derivation function used on the shared secret value. 124 * 125 * @return The key derivation function used on the shared secret value. 126 * One of the values defined in KeyDerivationFunctionType. 127 * @preconditions 128 * @postconditions 129 */ getKeyDerivationFunction()130 public long getKeyDerivationFunction() { 131 return keyDerivationFunction_; 132 } 133 134 /** 135 * Get the other partie's public key value. 136 * 137 * @return The other partie's public key value. 138 * @preconditions 139 * @postconditions (result <> null) 140 */ getPublicData()141 public byte[] getPublicData() { 142 return publicData_; 143 } 144 145 /** 146 * Set the ey derivation function used on the shared secret value. 147 * 148 * @param keyDerivationFunction The key derivation function used on the shared 149 * secret value. 150 * One of the values defined in 151 * KeyDerivationFunctionType. 152 * @preconditions (keyDerivationFunction == KeyDerivationFunctionType.NULL) 153 * or (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF)) 154 * or (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF_ASN1)) 155 * or (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF_CONCATENATE)) 156 * @postconditions 157 */ setKeyDerivationFunction(long keyDerivationFunction)158 public void setKeyDerivationFunction(long keyDerivationFunction) { 159 if ((keyDerivationFunction != KeyDerivationFunctionType.NULL) 160 && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF) 161 && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF_ASN1) 162 && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF_CONCATENATE)) { 163 throw new IllegalArgumentException( 164 "Illegal value for argument\"keyDerivationFunction\": " 165 + Functions.toHexString(keyDerivationFunction)); 166 } 167 keyDerivationFunction_ = keyDerivationFunction; 168 } 169 170 /** 171 * Set the other partie's public key value. 172 * 173 * @param publicData The other partie's public key value. 174 * @preconditions (publicData <> null) 175 * @postconditions 176 */ setPublicData(byte[] publicData)177 public void setPublicData(byte[] publicData) { 178 if (publicData == null) { 179 throw new NullPointerException("Argument \"publicData\" must not be null."); 180 } 181 publicData_ = publicData; 182 } 183 184 /** 185 * Returns the string representation of this object. Do not parse data from 186 * this string, it is for debugging only. 187 * 188 * @return A string representation of this object. 189 */ toString()190 public String toString() { 191 StringBuffer buffer = new StringBuffer(); 192 193 buffer.append(Constants.INDENT); 194 buffer.append("Key Derivation Function: "); 195 if (keyDerivationFunction_ == KeyDerivationFunctionType.NULL) { 196 buffer.append("NULL"); 197 } else if (keyDerivationFunction_ == KeyDerivationFunctionType.SHA1_KDF) { 198 buffer.append("SHA1_KDF"); 199 } else if (keyDerivationFunction_ == KeyDerivationFunctionType.SHA1_KDF_ASN1) { 200 buffer.append("SHA1_KDF_ASN1"); 201 } else if (keyDerivationFunction_ == KeyDerivationFunctionType.SHA1_KDF_CONCATENATE) { 202 buffer.append("SHA1_KDF_CONCATENATE"); 203 } else { 204 buffer.append("<unknown>"); 205 } 206 buffer.append(Constants.NEWLINE); 207 208 buffer.append(Constants.INDENT); 209 buffer.append("Public Data: "); 210 buffer.append(Functions.toHexString(publicData_)); 211 // buffer.append(Constants.NEWLINE); 212 213 return buffer.toString(); 214 } 215 216 /** 217 * Compares all member variables of this object with the other object. 218 * Returns only true, if all are equal in both objects. 219 * 220 * @param otherObject The other object to compare to. 221 * @return True, if other is an instance of this class and all member 222 * variables of both objects are equal. False, otherwise. 223 * @preconditions 224 * @postconditions 225 */ equals(java.lang.Object otherObject)226 public boolean equals(java.lang.Object otherObject) { 227 boolean equal = false; 228 229 if (otherObject instanceof DHKeyDerivationParameters) { 230 DHKeyDerivationParameters other = (DHKeyDerivationParameters) otherObject; 231 equal = (this == other) 232 || ((this.keyDerivationFunction_ == other.keyDerivationFunction_) && Functions 233 .equals(this.publicData_, other.publicData_)); 234 } 235 236 return equal; 237 } 238 239 /** 240 * The overriding of this method should ensure that the objects of this class 241 * work correctly in a hashtable. 242 * 243 * @return The hash code of this object. 244 * @preconditions 245 * @postconditions 246 */ hashCode()247 public int hashCode() { 248 return ((int) keyDerivationFunction_) ^ Functions.hashCode(publicData_); 249 } 250 251 } 252