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.Token;
054 import iaik.pkcs.pkcs11.TokenInfo;
055 import iaik.pkcs.pkcs11.objects.DES3SecretKey;
056 import iaik.pkcs.pkcs11.objects.Key;
057 import iaik.pkcs.pkcs11.objects.SecretKey;
058 import iaik.pkcs.pkcs11.parameters.DesCbcEncryptDataParameters;
059
060 /**
061 * This demo program shows how to derive a DES3 key.
062 *
063 * @invariants
064 */
065 public class DeriveKeyDemo {
066
067 static BufferedReader input_;
068 static PrintWriter output_;
069
070 static {
071 try {
072 //output_ = new PrintWriter(new FileWriter("SignAndVerify_output.txt"), true);
073 output_ = new PrintWriter(System.out, true);
074 input_ = new BufferedReader(new InputStreamReader(System.in));
075 } catch (Throwable thr) {
076 thr.printStackTrace();
077 output_ = new PrintWriter(System.out, true);
078 input_ = new BufferedReader(new InputStreamReader(System.in));
079 }
080 }
081
082 public static void main(String[] args) {
083 if (args.length != 1) {
084 printUsage();
085 System.exit(1);
086 }
087 try {
088 Module pkcs11Module = Module.getInstance(args[0]);
089 pkcs11Module.initialize(null);
090
091 Token token = Util.selectToken(pkcs11Module, output_, input_);
092 TokenInfo tokenInfo = token.getTokenInfo();
093
094 output_.println("################################################################################");
095 output_.println("Using token:");
096 output_.println(tokenInfo);
097 output_.println("################################################################################");
098
099 Session session = Util.openAuthorizedSession(token, Token.SessionReadWriteBehavior.RO_SESSION, output_, input_);
100
101 Mechanism keyGenerationMechanism = (Mechanism) Mechanism.DES3_KEY_GEN.clone();
102 DES3SecretKey baseKeyTemplate = new DES3SecretKey();
103
104 baseKeyTemplate.getDerive().setBooleanValue(Boolean.TRUE);
105 // we only have a read-only session, thus we only create a session object
106 baseKeyTemplate.getToken().setBooleanValue(Boolean.FALSE);
107 baseKeyTemplate.getSensitive().setBooleanValue(Boolean.FALSE);
108 baseKeyTemplate.getExtractable().setBooleanValue(Boolean.TRUE);
109 SecretKey baseKey = (SecretKey) session.generateKey(keyGenerationMechanism, baseKeyTemplate);
110
111 System.out.println("Base key: ");
112 System.out.println(baseKey.toString());
113
114 output_.println("################################################################################");
115 output_.println("derive key");
116
117 //DES3 Key Template
118 DES3SecretKey derived3DESKeyTemplate = new DES3SecretKey();
119 SecretKey derivedKeyTemplate = derived3DESKeyTemplate;
120
121 derivedKeyTemplate.getSensitive().setBooleanValue(Boolean.FALSE);
122 derivedKeyTemplate.getExtractable().setBooleanValue(Boolean.TRUE);
123
124 byte[] iv = new byte[8];
125 byte[] data = new byte[24];
126
127 DesCbcEncryptDataParameters param = new DesCbcEncryptDataParameters(iv, data);
128 Mechanism mechanism = (Mechanism) Mechanism.DES3_CBC_ENCRYPT_DATA;
129 mechanism.setParameters(param);
130
131 System.out.println("Derivation Mechanism: ");
132 output_.println(mechanism.toString());
133 output_.println("--------------------------------------------------------------------------------");
134
135
136 Key derivedKey = session.deriveKey(mechanism, baseKey, derivedKeyTemplate);
137
138 if (derivedKey == null){
139 output_.println("Found NO key that can be used for encryption.");
140 output_.flush();
141 System.exit(0);
142 }
143 System.out.println("Derived key: ");
144 output_.println(derivedKey.toString());
145
146 output_.println("################################################################################");
147 output_.println("finished");
148
149 session.closeSession();
150 pkcs11Module.finalize(null);
151
152 } catch (Throwable thr) {
153 thr.printStackTrace();
154 }
155 }
156
157 public static void printUsage() {
158 output_.println("Usage: TestDeriveParams <PKCS#11 module>");
159 output_.println(" e.g.: TestDeriveParams cryptoki.dll");
160 output_.println("The given DLL must be in the search path of the system.");
161 }
162
163 }