1 package iaik.pkcs.pkcs11.parameters;
2 
3 import iaik.pkcs.pkcs11.wrapper.CK_X9_42_DH2_DERIVE_PARAMS;
4 import iaik.pkcs.pkcs11.wrapper.Constants;
5 import iaik.pkcs.pkcs11.wrapper.Functions;
6 
7 /**
8  * This abstract class encapsulates parameters for the X9.42 DH mechanisms
9  * Mechanism.X9_42_DH_HYBRID_DERIVE and Mechanism.X9_42_MQV_DERIVE.
10  *
11  * @author Karl Scheibelhofer
12  * @version 1.0
13  * @invariants (privateData <> null)
14  *             and (publicData2 <> null)
15  */
16 public class X942DH2KeyDerivationParameters extends X942DH1KeyDerivationParameters {
17 
18 	/**
19 	 * The length in bytes of the second EC private key.
20 	 */
21 	protected long privateDataLength_;
22 
23 	/**
24 	 * The key for the second EC private key value.
25 	 */
26 	protected iaik.pkcs.pkcs11.objects.Object privateData_;
27 
28 	/**
29 	 * The other party’s second EC public key value.
30 	 */
31 	protected byte[] publicData2_;
32 
33 	/**
34 	 * Create a new X942DH1KeyDerivationParameters object with the given attributes.
35 	 *
36 	 * @param keyDerivationFunction The key derivation function used on the shared
37 	 *                              secret value.
38 	 *                              One of the values defined in
39 	 *                              KeyDerivationFunctionType.
40 	 * @param sharedData The data shared between the two parties.
41 	 * @param publicData The other partie's public key value.
42 	 * @param privateDataLength The length in bytes of the second EC private key.
43 	 * @param privateData The key for the second X9.42 private key value.
44 	 * @param publicData2 The other party’s second X9.42 public key value.
45 	 * @preconditions ((keyDerivationFunction == KeyDerivationFunctionType.NULL)
46 	 *                 or (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF)
47 	 *                 or (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF_ASN1)
48 	 *                 or (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF_CONCATENATE))
49 	 *                and (publicData <> null)
50 	 *                and (privateData <> null)
51 	 *                and (publicData2 <> null)
52 	 * @postconditions
53 	 */
X942DH2KeyDerivationParameters(long keyDerivationFunction, byte[] sharedData, byte[] publicData, long privateDataLength, iaik.pkcs.pkcs11.objects.Object privateData, byte[] publicData2)54 	public X942DH2KeyDerivationParameters(long keyDerivationFunction,
55 	                                      byte[] sharedData,
56 	                                      byte[] publicData,
57 	                                      long privateDataLength,
58 	                                      iaik.pkcs.pkcs11.objects.Object privateData,
59 	                                      byte[] publicData2)
60 	{
61 		super(keyDerivationFunction, sharedData, publicData);
62 		if (privateData == null) {
63 			throw new NullPointerException("Argument \"privateData\" must not be null.");
64 		}
65 		if (publicData2 == null) {
66 			throw new NullPointerException("Argument \"publicData2\" must not be null.");
67 		}
68 		privateDataLength_ = privateDataLength;
69 		privateData_ = privateData;
70 		publicData2_ = publicData2;
71 	}
72 
73 	/**
74 	 * Create a (deep) clone of this object.
75 	 *
76 	 * @return A clone of this object.
77 	 * @preconditions
78 	 * @postconditions (result <> null)
79 	 *                 and (result instanceof X942DH2KeyDerivationParameters)
80 	 *                 and (result.equals(this))
81 	 */
clone()82 	public java.lang.Object clone() {
83 		X942DH2KeyDerivationParameters clone = (X942DH2KeyDerivationParameters) super.clone();
84 
85 		clone.privateData_ = (iaik.pkcs.pkcs11.objects.Object) this.privateData_.clone();
86 		clone.publicData2_ = (byte[]) this.publicData2_.clone();
87 
88 		return clone;
89 	}
90 
91 	/**
92 	 * Get this parameters object as an object of the CK_X9_42_DH2_DERIVE_PARAMS
93 	 * class.
94 	 *
95 	 * @return This object as a CK_X9_42_DH2_DERIVE_PARAMS object.
96 	 * @preconditions
97 	 * @postconditions (result <> null)
98 	 */
getPKCS11ParamsObject()99 	public Object getPKCS11ParamsObject() {
100 		CK_X9_42_DH2_DERIVE_PARAMS params = new CK_X9_42_DH2_DERIVE_PARAMS();
101 
102 		params.kdf = keyDerivationFunction_;
103 		params.pOtherInfo = otherInfo_;
104 		params.pPublicData = publicData_;
105 		params.ulPrivateDataLen = privateDataLength_;
106 		params.hPrivateData = privateData_.getObjectHandle();
107 		params.pPublicData2 = publicData2_;
108 
109 		return params;
110 	}
111 
112 	/**
113 	 * Get the key for the second X9.42 private key value.
114 	 *
115 	 * @return The key for the second X9.42 private key value.
116 	 * @preconditions
117 	 * @postconditions (result <> null)
118 	 */
getPrivateData()119 	public iaik.pkcs.pkcs11.objects.Object getPrivateData() {
120 		return privateData_;
121 	}
122 
123 	/**
124 	 * Get the length in bytes of the second X9.42 private key.
125 	 *
126 	 * @return The length in bytes of the second X9.42 private key.
127 	 * @preconditions
128 	 * @postconditions
129 	 */
getPrivateDataLength()130 	public long getPrivateDataLength() {
131 		return privateDataLength_;
132 	}
133 
134 	/**
135 	 * Get the other party’s second X9.42 public key value.
136 	 *
137 	 * @return The other party’s second X9.42 public key value.
138 	 * @preconditions
139 	 * @postconditions (result <> null)
140 	 */
getPublicData2()141 	public byte[] getPublicData2() {
142 		return publicData2_;
143 	}
144 
145 	/**
146 	 * Set the key for the second X9.42 private key value.
147 	 *
148 	 * @param privateData The key for the second X9.42 private key value.
149 	 * @preconditions (privateData <> null)
150 	 * @postconditions
151 	 */
setPrivateData(iaik.pkcs.pkcs11.objects.Object privateData)152 	public void setPrivateData(iaik.pkcs.pkcs11.objects.Object privateData) {
153 		if (privateData == null) {
154 			throw new NullPointerException("Argument \"privateData\" must not be null.");
155 		}
156 		privateData_ = privateData;
157 	}
158 
159 	/**
160 	 * Set the length in bytes of the second X9.42 private key.
161 	 *
162 	 * @param privateDataLength The length in bytes of the second X9.42 private key.
163 	 * @preconditions
164 	 * @postconditions
165 	 */
setPrivateDataLength(long privateDataLength)166 	public void setPrivateDataLength(long privateDataLength) {
167 		privateDataLength_ = privateDataLength;
168 	}
169 
170 	/**
171 	 * Set the other party’s second X9.42 public key value.
172 	 *
173 	 * @param publicData2 The other party’s second X9.42 public key value.
174 	 * @preconditions (publicData2 <> null)
175 	 * @postconditions
176 	 */
setPublicData2(byte[] publicData2)177 	public void setPublicData2(byte[] publicData2) {
178 		if (publicData2 == null) {
179 			throw new NullPointerException("Argument \"publicData2\" must not be null.");
180 		}
181 		publicData2_ = publicData2;
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(super.toString());
194 		buffer.append(Constants.NEWLINE);
195 
196 		buffer.append(Constants.INDENT);
197 		buffer.append("Private Data Length (dec): ");
198 		buffer.append(privateDataLength_);
199 		buffer.append(Constants.NEWLINE);
200 
201 		buffer.append(Constants.INDENT);
202 		buffer.append("Private Data: ");
203 		buffer.append(privateData_);
204 		buffer.append(Constants.NEWLINE);
205 
206 		buffer.append(Constants.INDENT);
207 		buffer.append("Public Data 2: ");
208 		buffer.append(Functions.toHexString(publicData2_));
209 		// buffer.append(Constants.NEWLINE);
210 
211 		return buffer.toString();
212 	}
213 
214 	/**
215 	 * Compares all member variables of this object with the other object.
216 	 * Returns only true, if all are equal in both objects.
217 	 *
218 	 * @param otherObject The other object to compare to.
219 	 * @return True, if other is an instance of this class and all member
220 	 *         variables of both objects are equal. False, otherwise.
221 	 * @preconditions
222 	 * @postconditions
223 	 */
equals(java.lang.Object otherObject)224 	public boolean equals(java.lang.Object otherObject) {
225 		boolean equal = false;
226 
227 		if (otherObject instanceof X942DH2KeyDerivationParameters) {
228 			X942DH2KeyDerivationParameters other = (X942DH2KeyDerivationParameters) otherObject;
229 			equal = (this == other)
230 			    || (super.equals(other)
231 			        && (this.privateDataLength_ == other.privateDataLength_)
232 			        && this.privateData_.equals(other.privateData_) && Functions.equals(
233 			        this.publicData2_, other.publicData2_));
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 super.hashCode() ^ ((int) privateDataLength_) ^ privateData_.hashCode()
249 		    ^ Functions.hashCode(publicData2_);
250 	}
251 
252 }
253