001 /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
002 *
003 * Redistribution and use in source and binary forms, with or without
004 * modification, are permitted provided that the following conditions are met:
005 *
006 * 1. Redistributions of source code must retain the above copyright notice,
007 * this list of conditions and the following disclaimer.
008 *
009 * 2. Redistributions in binary form must reproduce the above copyright notice,
010 * this list of conditions and the following disclaimer in the documentation
011 * and/or other materials provided with the distribution.
012 *
013 * 3. The end-user documentation included with the redistribution, if any, must
014 * include the following acknowledgment:
015 *
016 * "This product includes software developed by IAIK of Graz University of
017 * Technology."
018 *
019 * Alternately, this acknowledgment may appear in the software itself, if
020 * and wherever such third-party acknowledgments normally appear.
021 *
022 * 4. The names "Graz University of Technology" and "IAIK of Graz University of
023 * Technology" must not be used to endorse or promote products derived from
024 * this software without prior written permission.
025 *
026 * 5. Products derived from this software may not be called
027 * "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
028 * written permission of Graz University of Technology.
029 *
030 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
031 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
032 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
033 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
034 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
035 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
036 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
037 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
038 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
039 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
040 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
041 * POSSIBILITY OF SUCH DAMAGE.
042 */
043
044 package demo.pkcs.pkcs11;
045
046 import java.io.BufferedReader;
047 import java.io.InputStreamReader;
048 import java.io.PrintWriter;
049
050 import iaik.pkcs.pkcs11.Mechanism;
051 import iaik.pkcs.pkcs11.Module;
052 import iaik.pkcs.pkcs11.Session;
053 import iaik.pkcs.pkcs11.Slot;
054 import iaik.pkcs.pkcs11.Token;
055 import iaik.pkcs.pkcs11.objects.Key;
056 import iaik.pkcs.pkcs11.parameters.DHKeyDerivationParameters;
057 import iaik.pkcs.pkcs11.parameters.Parameters;
058 import iaik.pkcs.pkcs11.parameters.X942DH2KeyDerivationParameters;
059
060
061
062 /**
063 * Just to test the parameters classes.
064 *
065 * @author <a href="mailto:Karl.Scheibelhofer@iaik.at"> Karl Scheibelhofer </a>
066 * @version 0.1
067 * @invariants
068 */
069 public class ParametersTest {
070
071 static PrintWriter output_;
072
073 static BufferedReader input_;
074
075 static {
076 try {
077 //output_ = new PrintWriter(new FileWriter("GetInfo_output.txt"), true);
078 output_ = new PrintWriter(System.out, true);
079 input_ = new BufferedReader(new InputStreamReader(System.in));
080 } catch (Throwable thr) {
081 thr.printStackTrace();
082 output_ = new PrintWriter(System.out, true);
083 input_ = new BufferedReader(new InputStreamReader(System.in));
084 }
085 }
086
087 public static void main(String[] args) {
088 if (args.length != 1) {
089 printUsage();
090 System.exit(1);
091 }
092
093 try {
094
095 Module pkcs11Module = Module.getInstance(args[0]);
096 pkcs11Module.initialize(null);
097
098 Slot[] slots = pkcs11Module.getSlotList(Module.SlotRequirement.TOKEN_PRESENT);
099
100 if (slots.length == 0) {
101 output_.println("No slot with present token found!");
102 output_.flush();
103 System.exit(0);
104 }
105
106 Slot selectedSlot = slots[0];
107 Token token = selectedSlot.getToken();
108
109 Session session =
110 token.openSession(Token.SessionType.SERIAL_SESSION, Token.SessionReadWriteBehavior.RO_SESSION, null, null);
111
112 output_.println("################################################################################");
113
114 Mechanism mechanism;
115 Parameters parameters;
116 Key key = new Key(); // create a dummy key
117 Key anotherKey = new Key(); // create a dummy key
118 key.setObjectHandle(1);
119 anotherKey.setObjectHandle(2);
120
121 /*
122 output_.print("testing DHPkcsDeriveParameters... ");
123 mechanism = (Mechanism) Mechanism.DH_PKCS_DERIVE.clone();
124 byte[] publicValue = { 1, 2, 3, 4, 5, 6, 7, 8 };
125 parameters = new DHPkcsDeriveParameters(publicValue);
126 mechanism.setParameters(parameters);
127 session.deriveKey(mechanism, key, null);
128 output_.println("finished");
129 output_.flush();
130 */
131
132 /*
133 output_.print("testing ExtractParameters... ");
134 mechanism = (Mechanism) Mechanism.EXTRACT_KEY_FROM_KEY.clone();
135 parameters = new ExtractParameters(4);
136 mechanism.setParameters(parameters);
137 session.deriveKey(mechanism, key, null);
138 output_.println("finished");
139 output_.flush();
140 */
141
142 /*
143 output_.print("testing InitializationVectorParameters... ");
144 mechanism = (Mechanism) Mechanism.DES3_CBC_PAD.clone();
145 byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
146 parameters = new InitializationVectorParameters(iv);
147 mechanism.setParameters(parameters);
148 session.encryptInit(mechanism, key);
149 output_.println("finished");
150 output_.flush();
151 */
152
153 /*
154 output_.print("testing KEADeriveParameters... ");
155 mechanism = (Mechanism) Mechanism.EXTRACT_KEY_FROM_KEY.clone();
156 byte[] randomA = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
157 byte[] randomB = { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
158 byte[] publicData = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
159 parameters = new KEADeriveParameters(true, randomA, randomB, publicData);
160 mechanism.setParameters(parameters);
161 session.deriveKey(mechanism, key, null);
162 output_.println("finished");
163 output_.flush();
164 */
165
166 /*
167 output_.print("testing KeyDerivationStringDataParameters... ");
168 byte[] data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
169 parameters = new KeyDerivationStringDataParameters(data);
170 mechanism = (Mechanism) Mechanism.CONCATENATE_BASE_AND_DATA.clone();
171 mechanism.setParameters(parameters);
172 session.deriveKey(mechanism, key, null);
173 output_.println("finished");
174 output_.flush();
175 */
176
177 /*
178 output_.print("testing KeyWrapSetOaepParameters... ");
179 byte[] x = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
180 parameters = new KeyWrapSetOaepParameters((byte) 0xEF, x);
181 mechanism = (Mechanism) Mechanism.KEY_WRAP_SET_OAEP.clone();
182 mechanism.setParameters(parameters);
183 session.wrapKey(mechanism, key, anotherKey);
184 output_.println("finished");
185 output_.flush();
186 */
187
188 /*
189 output_.print("testing MacGeneralParameters... ");
190 byte[] x = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
191 parameters = new MacGeneralParameters(16);
192 mechanism = (Mechanism) Mechanism.DES3_MAC_GENERAL.clone();
193 mechanism.setParameters(parameters);
194 session.signInit(mechanism, key);
195 output_.println("finished");
196 output_.flush();
197 */
198
199 /*
200 output_.print("testing ObjectHandleParameters... ");
201 Key parameterKey = new Key(); // create a dummy key
202 parameterKey.setObjectHandle(3);
203 parameters = new ObjectHandleParameters(parameterKey);
204 mechanism = (Mechanism) Mechanism.CONCATENATE_BASE_AND_KEY.clone();
205 mechanism.setParameters(parameters);
206 session.deriveKey(mechanism, key, anotherKey);
207 output_.println("finished");
208 output_.flush();
209 */
210
211 /*
212 output_.print("testing PBEParameters... ");
213 mechanism = (Mechanism) Mechanism.PBE_SHA1_DES3_EDE_CBC.clone();
214 char[] iv = "initiali".toCharArray();
215 char[] password = "password".toCharArray();
216 char[] salt = "salt".toCharArray();
217 long iterations = 1000;
218 parameters = new PBEParameters(iv, password, salt, iterations);
219 mechanism.setParameters(parameters);
220 session.encryptInit(mechanism, key);
221 output_.println("finished");
222 output_.flush();
223 */
224
225 /*
226 output_.print("testing PKCS5PBKD2Parameters... ");
227 mechanism = (Mechanism) Mechanism.PBE_SHA1_DES3_EDE_CBC.clone();
228 long saltSource = PKCS5PBKD2Parameters.SaltSourceType.SALT_SPECIFIED;
229 byte[] saltSourceData = "salt".getBytes("ASCII");
230 long iterations = 1000;
231 long pseudoRandomFunction = PKCS5PBKD2Parameters.PseudoRandomFunctionType.HMAC_SHA1;
232 byte[] pseudoRandomFunctionData = "datadata".getBytes("ASCII");
233 parameters = new PKCS5PBKD2Parameters(saltSource, saltSourceData, iterations,
234 pseudoRandomFunction, pseudoRandomFunctionData);
235 mechanism.setParameters(parameters);
236 SecretKey secretKeyTemplate = new SecretKey();
237 secretKeyTemplate.setObjectHandle(4);
238 session.generateKey(mechanism, secretKeyTemplate);
239 output_.println("finished");
240 output_.flush();
241 */
242
243 /*
244 output_.print("testing RC2CbcParameters... ");
245 byte[] iv = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
246 parameters = new RC2CbcParameters(42, iv);
247 mechanism = (Mechanism) Mechanism.RC2_CBC_PAD.clone();
248 mechanism.setParameters(parameters);
249 session.encryptInit(mechanism, key);
250 output_.println("finished");
251 output_.flush();
252 */
253
254 /*
255 output_.print("testing RC2MacGeneralParameters... ");
256 parameters = new RC2MacGeneralParameters(128, 16);
257 mechanism = (Mechanism) Mechanism.RC2_MAC_GENERAL.clone();
258 mechanism.setParameters(parameters);
259 session.signInit(mechanism, key);
260 output_.println("finished");
261 output_.flush();
262 */
263
264 /*
265 output_.print("testing RC2Parameters... ");
266 parameters = new RC2Parameters(128);
267 mechanism = (Mechanism) Mechanism.RC2_MAC.clone();
268 mechanism.setParameters(parameters);
269 session.signInit(mechanism, key);
270 output_.println("finished");
271 output_.flush();
272 */
273
274 /*
275 output_.print("testing RC5CbcParameters... ");
276 byte[] iv = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
277 parameters = new RC5CbcParameters(4, 16, iv);
278 mechanism = (Mechanism) Mechanism.RC5_CBC_PAD.clone();
279 mechanism.setParameters(parameters);
280 session.encryptInit(mechanism, key);
281 output_.println("finished");
282 output_.flush();
283 */
284
285 /*
286 output_.print("testing RC5MacGeneralParameters... ");
287 parameters = new RC5MacGeneralParameters(4, 16, 12);
288 mechanism = (Mechanism) Mechanism.RC5_MAC_GENERAL.clone();
289 mechanism.setParameters(parameters);
290 session.signInit(mechanism, key);
291 output_.println("finished");
292 output_.flush();
293 */
294
295 /*
296 output_.print("testing RC5Parameters... ");
297 parameters = new RC5Parameters(4, 16);
298 mechanism = (Mechanism) Mechanism.RC5_MAC.clone();
299 mechanism.setParameters(parameters);
300 session.signInit(mechanism, key);
301 output_.println("finished");
302 output_.flush();
303 */
304
305 /*
306 output_.print("testing RSAPkcsOaepParameters... ");
307 Mechanism hashAlgorithm = Mechanism.SHA_1;
308 long maskGenerationFunction = RSAPkcsParameters.MessageGenerationFunctionType.SHA1;
309 long source = RSAPkcsOaepParameters.SourceType.DATA_SPECIFIED;
310 byte[] sourceData = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
311 parameters = new RSAPkcsOaepParameters(hashAlgorithm, maskGenerationFunction, source, sourceData);
312 mechanism = (Mechanism) Mechanism.RC5_MAC.clone();
313 mechanism.setParameters(parameters);
314 session.encryptInit(mechanism, key);
315 output_.println("finished");
316 output_.flush();
317 */
318
319 /*
320 output_.print("testing SkipJackPrivateWrapParameters... ");
321 byte[] password = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
322 byte[] publicData = { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
323 byte[] randomA = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
324 byte[] primeP = { 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
325 byte[] baseG = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };
326 byte[] subprimeQ = { 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F };
327 parameters = new SkipJackPrivateWrapParameters(password, publicData, randomA, primeP, baseG, subprimeQ);
328 mechanism = (Mechanism) Mechanism.SKIPJACK_PRIVATE_WRAP.clone();
329 mechanism.setParameters(parameters);
330 session.wrapKey(mechanism, key, anotherKey);
331 output_.println("finished");
332 output_.flush();
333 */
334
335 /*
336 output_.print("testing SkipJackRelayXParameters... ");
337 byte[] oldWrappedX = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
338 byte[] oldPassword = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
339 byte[] oldPublicData = { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
340 byte[] oldRandomA = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
341 byte[] newPassword = { 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
342 byte[] newPublicData = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };
343 byte[] newRandomA = { 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F };
344 parameters = new SkipJackRelayXParameters(oldWrappedX, oldPassword, oldPublicData, oldRandomA,
345 newPassword, newPublicData, newRandomA);
346 mechanism = (Mechanism) Mechanism.SKIPJACK_RELAYX.clone();
347 mechanism.setParameters(parameters);
348 session.wrapKey(mechanism, key, anotherKey);
349 output_.println("finished");
350 output_.flush();
351
352 */
353
354 // SSL* and Version parameters tested in SSLMechanismsDemo
355
356 /*
357 output_.print("testing RSAPkcsPssParameters... ");
358 Mechanism hashAlgorithm = Mechanism.SHA_1;
359 long maskGenerationFunction = RSAPkcsParameters.MessageGenerationFunctionType.SHA1;
360 long saltLength = 16;
361 parameters = new RSAPkcsPssParameters(hashAlgorithm, maskGenerationFunction, saltLength);
362 mechanism = (Mechanism) Mechanism.RSA_PKCS_PSS.clone();
363 mechanism.setParameters(parameters);
364 session.signInit(mechanism, key);
365 output_.println("finished");
366 output_.flush();
367 */
368
369 /*
370 output_.print("testing EcDH1KeyDerivationParameters... ");
371 long keyDerivationFunction = DHKeyDerivationParameters.KeyDerivationFunctionType.SHA1_KDF;
372 byte[] sharedData = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
373 byte[] publicData = { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
374 parameters = new EcDH1KeyDerivationParameters(keyDerivationFunction, sharedData, publicData);
375 mechanism = (Mechanism) Mechanism.ECDH1_DERIVE.clone();
376 mechanism.setParameters(parameters);
377 session.deriveKey(mechanism, key, anotherKey);
378 output_.println("finished");
379 output_.flush();
380 */
381
382 /*
383 output_.print("testing EcDH2KeyDerivationParameters... ");
384 long keyDerivationFunction = DHKeyDerivationParameters.KeyDerivationFunctionType.SHA1_KDF;
385 byte[] sharedData = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
386 byte[] publicData = { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
387 byte[] publicData2 = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
388 long privateDataLength = 16;
389 Key privateData = new Key();
390 privateData.setObjectHandle(3);
391 parameters = new EcDH2KeyDerivationParameters(keyDerivationFunction, sharedData, publicData,
392 privateDataLength, privateData, publicData2);
393 mechanism = (Mechanism) Mechanism.ECMQV_DERIVE.clone();
394 mechanism.setParameters(parameters);
395 session.deriveKey(mechanism, key, anotherKey);
396 output_.println("finished");
397 output_.flush();
398 */
399
400 /*
401 output_.print("testing X942DH1KeyDerivationParameters... ");
402 long keyDerivationFunction = DHKeyDerivationParameters.KeyDerivationFunctionType.SHA1_KDF_ASN1;
403 byte[] otherInfo = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
404 byte[] publicData = { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
405 parameters = new X942DH1KeyDerivationParameters(keyDerivationFunction, otherInfo, publicData);
406 mechanism = (Mechanism) Mechanism.X9_42_DH_DERIVE.clone();
407 mechanism.setParameters(parameters);
408 session.deriveKey(mechanism, key, anotherKey);
409 output_.println("finished");
410 output_.flush();
411 */
412
413 output_.print("testing X942DH2KeyDerivationParameters... ");
414 long keyDerivationFunction = DHKeyDerivationParameters.KeyDerivationFunctionType.SHA1_KDF_ASN1;
415 byte[] otherInfo = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
416 byte[] publicData = { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
417 byte[] publicData2 = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
418 long privateDataLength = 16;
419 Key privateData = new Key();
420 privateData.setObjectHandle(3);
421 parameters = new X942DH2KeyDerivationParameters(keyDerivationFunction, otherInfo, publicData,
422 privateDataLength, privateData, publicData2);
423 mechanism = (Mechanism) Mechanism.X9_42_DH_HYBRID_DERIVE.clone();
424 mechanism.setParameters(parameters);
425 session.deriveKey(mechanism, key, anotherKey);
426 output_.println("finished");
427 output_.flush();
428
429 output_.println("################################################################################");
430 session.closeSession();
431 pkcs11Module.finalize(null);
432
433 } catch (Throwable thr) {
434 thr.printStackTrace();
435 }
436 output_.println("press any key to continue...");
437 try {
438 input_.read();
439 } catch (Throwable thr) {
440 thr.printStackTrace();
441 }
442 }
443
444 public static void printUsage() {
445 output_.println("Usage: ParametersTest <PKCS#11 module>");
446 output_.println(" e.g.: ParametersTest pk2priv.dll");
447 output_.println("The given DLL must be in the search path of the system.");
448 }
449
450 }