1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
package cz.crcs.ectester.reader;
import cz.crcs.ectester.applet.ECTesterApplet;
import javax.smartcardio.CardException;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import java.util.ArrayList;
import java.util.List;
/**
* @author Jan Jancar johny@neuromancer.sk
*/
public abstract class Command {
protected CommandAPDU cmd;
protected CardMngr cardManager;
protected Command(CardMngr cardManager) {
this.cardManager = cardManager;
}
public CommandAPDU getAPDU() {
return cmd;
}
public abstract Response send() throws CardException;
public static List<Response> sendAll(List<Command> commands) throws CardException {
List<Response> result = new ArrayList<>();
for (Command cmd : commands) {
result.add(cmd.send());
}
return result;
}
/**
*
*/
public static class Allocate extends Command {
private byte keyPair;
private short keyLength;
private byte keyClass;
/**
* Creates the INS_ALLOCATE instruction.
*
* @param cardManager
* @param keyPair which keyPair to use, local/remote (KEYPAIR_* | ...)
* @param keyLength key length to set
* @param keyClass key class to allocate
*/
public Allocate(CardMngr cardManager, byte keyPair, short keyLength, byte keyClass) {
super(cardManager);
this.keyPair = keyPair;
this.keyLength = keyLength;
this.keyClass = keyClass;
byte[] data = new byte[]{0, 0, keyClass};
Util.setShort(data, 0, keyLength);
this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_ALLOCATE, keyPair, 0x00, data);
}
@Override
public Response.Allocate send() throws CardException {
long elapsed = -System.nanoTime();
ResponseAPDU response = cardManager.send(cmd);
elapsed += System.nanoTime();
return new Response.Allocate(response, elapsed, keyPair, keyLength, keyClass);
}
}
/**
*
*/
public static class Clear extends Command {
private byte keyPair;
/**
* @param cardManager
* @param keyPair which keyPair clear, local/remote (KEYPAIR_* || ...)
*/
public Clear(CardMngr cardManager, byte keyPair) {
super(cardManager);
this.keyPair = keyPair;
this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_CLEAR, keyPair, 0x00);
}
@Override
public Response.Clear send() throws CardException {
long elapsed = -System.nanoTime();
ResponseAPDU response = cardManager.send(cmd);
elapsed += System.nanoTime();
return new Response.Clear(response, elapsed, keyPair);
}
}
/**
*
*/
public static class Set extends Command {
private byte keyPair;
private byte curve;
private short params;
private byte[] external;
/**
* Creates the INS_SET instruction.
*
* @param cardManager
* @param keyPair which keyPair to set params on, local/remote (KEYPAIR_* || ...)
* @param curve curve to set (EC_Consts.CURVE_*)
* @param params parameters to set (EC_Consts.PARAMETER_* | ...)
* @param external external curve data, can be null
*/
public Set(CardMngr cardManager, byte keyPair, byte curve, short params, byte[] external) {
super(cardManager);
this.keyPair = keyPair;
this.curve = curve;
this.params = params;
this.external = external;
int len = external != null ? 2 + external.length : 2;
byte[] data = new byte[len];
Util.setShort(data, 0, params);
if (external != null) {
System.arraycopy(external, 0, data, 2, external.length);
}
this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_SET, keyPair, curve, data);
}
@Override
public Response.Set send() throws CardException {
long elapsed = -System.nanoTime();
ResponseAPDU response = cardManager.send(cmd);
elapsed += System.nanoTime();
return new Response.Set(response, elapsed, keyPair, curve, params);
}
}
/**
*
*/
public static class Corrupt extends Command {
private byte keyPair;
private byte key;
private short params;
private byte corruption;
/**
* @param cardManager
* @param keyPair which keyPair to corrupt, local/remote (KEYPAIR_* || ...)
* @param key
* @param params parameters to corrupt (EC_Consts.PARAMETER_* | ...)
* @param corruption corruption type (EC_Consts.CORRUPTION_*)
*/
protected Corrupt(CardMngr cardManager, byte keyPair, byte key, short params, byte corruption) {
super(cardManager);
this.keyPair = keyPair;
this.key = key;
this.params = params;
this.corruption = corruption;
byte[] data = new byte[3];
Util.setShort(data, 0, params);
data[2] = corruption;
this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_CORRUPT, keyPair, key, data);
}
@Override
public Response.Corrupt send() throws CardException {
long elapsed = -System.nanoTime();
ResponseAPDU response = cardManager.send(cmd);
elapsed += System.nanoTime();
return new Response.Corrupt(response, elapsed, keyPair, key, params, corruption);
}
}
/**
*
*/
public static class Generate extends Command {
private byte keyPair;
/**
* Creates the INS_GENERATE instruction.
*
* @param cardManager
* @param keyPair which keyPair to generate, local/remote (KEYPAIR_* || ...)
*/
public Generate(CardMngr cardManager, byte keyPair) {
super(cardManager);
this.keyPair = keyPair;
this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_GENERATE, keyPair, 0);
}
@Override
public Response.Generate send() throws CardException {
long elapsed = -System.nanoTime();
ResponseAPDU response = cardManager.send(cmd);
elapsed += System.nanoTime();
return new Response.Generate(response, elapsed, keyPair);
}
}
/**
*
*/
public static class Export extends Command {
private byte keyPair;
private byte key;
private short params;
/**
* Creates the INS_EXPORT instruction.
*
* @param cardManager
* @param keyPair keyPair to export from (KEYPAIR_* | ...)
* @param key key to export from (EC_Consts.KEY_* | ...)
* @param params params to export (EC_Consts.PARAMETER_* | ...)
*/
public Export(CardMngr cardManager, byte keyPair, byte key, short params) {
super(cardManager);
this.keyPair = keyPair;
this.key = key;
this.params = params;
byte[] data = new byte[2];
Util.setShort(data, 0, params);
this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_EXPORT, keyPair, key, data);
}
@Override
public Response.Export send() throws CardException {
long elapsed = -System.nanoTime();
ResponseAPDU response = cardManager.send(cmd);
elapsed += System.nanoTime();
return new Response.Export(response, elapsed, keyPair, key, params);
}
}
/**
*
*/
public static class ECDH extends Command {
private byte pubkey;
private byte privkey;
private byte export;
private byte invalid;
/**
* Creates the INS_ECDH instruction.
*
* @param cardManager
* @param pubkey keyPair to use for public key, (KEYPAIR_LOCAL || KEYPAIR_REMOTE)
* @param privkey keyPair to use for private key, (KEYPAIR_LOCAL || KEYPAIR_REMOTE)
* @param export whether to export ECDH secret
* @param invalid whether to invalidate the pubkey before ECDH
*/
public ECDH(CardMngr cardManager, byte pubkey, byte privkey, byte export, byte invalid) {
super(cardManager);
this.pubkey = pubkey;
this.privkey = privkey;
this.export = export;
this.invalid = invalid;
byte[] data = new byte[]{export, invalid};
this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_ECDH, pubkey, privkey, data);
}
@Override
public Response.ECDH send() throws CardException {
long elapsed = -System.nanoTime();
ResponseAPDU response = cardManager.send(cmd);
elapsed += System.nanoTime();
return new Response.ECDH(response, elapsed, pubkey, privkey, export, invalid);
}
}
public static class ECDSA extends Command {
private byte keyPair;
private byte export;
private byte[] raw;
/**
* Creates the INS_ECDSA instruction.
*
* @param cardManager
* @param keyPair keyPair to use for signing and verification (KEYPAIR_LOCAL || KEYPAIR_REMOTE)
* @param export whether to export ECDSA signature
* @param raw data to sign, can be null, in which case random data is signed.
*/
public ECDSA(CardMngr cardManager, byte keyPair, byte export, byte[] raw) {
super(cardManager);
this.keyPair = keyPair;
this.export = export;
this.raw = raw;
int len = raw != null ? raw.length : 0;
byte[] data = new byte[2 + len];
Util.setShort(data, 0, (short) len);
if (raw != null) {
System.arraycopy(raw, 0, data, 2, len);
}
this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_ECDSA, keyPair, export, data);
}
@Override
public Response.ECDSA send() throws CardException {
long elapsed = -System.nanoTime();
ResponseAPDU response = cardManager.send(cmd);
elapsed += System.nanoTime();
return new Response.ECDSA(response, elapsed, keyPair, export, raw);
}
}
}
|