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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
/*
* Copyright (c) 2016-2017 Petr Svenda <petr@svenda.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* PACKAGEID: 4C6162616B417070
* APPLETID: 4C6162616B4170706C6574
*/
package cz.crcs.ectester.applet;
import javacard.framework.*;
import javacard.security.ECPrivateKey;
import javacard.security.ECPublicKey;
import javacard.security.KeyPair;
import javacard.security.RandomData;
/**
* Reader part of ECTester, a tool for testing Elliptic curve support on javacards.
*
* @author Petr Svenda petr@svenda.com
* @author Jan Jancar johny@neuromancer.sk
*/
public class ECTesterApplet extends Applet {
// MAIN INSTRUCTION CLASS
public static final byte CLA_ECTESTERAPPLET = (byte) 0xB0;
//INSTRUCTIONS
public static final byte INS_ALLOCATE = (byte) 0x5a;
public static final byte INS_CLEAR = (byte) 0x5b;
public static final byte INS_SET = (byte) 0x5c;
public static final byte INS_GENERATE = (byte) 0x5d;
public static final byte INS_ECDH = (byte) 0x5e;
public static final byte INS_ECDSA = (byte) 0x5f;
//PARAMETERS for P1 and P2
public static final byte KEYPAIR_LOCAL = (byte) 0x01;
public static final byte KEYPAIR_REMOTE = (byte) 0x02;
public static final byte KEYPAIR_BOTH = KEYPAIR_LOCAL | KEYPAIR_REMOTE;
public static final byte EXPORT_NONE = (byte) 0x00;
public static final byte EXPORT_PUBLIC = (byte) 0x04;
public static final byte EXPORT_PRIVATE = (byte) 0x08;
public static final byte EXPORT_BOTH = EXPORT_PUBLIC | EXPORT_PRIVATE;
public static final byte EXPORT_ECDH = (byte) 0x10;
public static final byte EXPORT_SIG = (byte) 0x20;
//STATUS WORDS
public static final short SW_SIG_VERIFY_FAIL = (short) 0x0ee1;
private static final short ARRAY_LENGTH = (short) 0xff;
// TEMPORARRY ARRAY IN RAM
private byte ramArray[] = null;
private byte ramArray2[] = null;
// PERSISTENT ARRAY IN EEPROM
private byte dataArray[] = null; // unused
private RandomData randomData = null;
private KeyPair localKeypair = null;
private KeyPair remoteKeypair = null;
private ECKeyTester keyTester = null;
private ECKeyGenerator keyGenerator = null;
protected ECTesterApplet(byte[] buffer, short offset, byte length) {
if (length > 9) {
/*
short dataOffset = offset;
// shift to privilege offset
dataOffset += (short) (1 + buffer[offset]);
// finally shift to Application specific offset
dataOffset += (short) (1 + buffer[dataOffset]);
// go to proprietary data
dataOffset++;
*/
ramArray = JCSystem.makeTransientByteArray(ARRAY_LENGTH, JCSystem.CLEAR_ON_RESET);
ramArray2 = JCSystem.makeTransientByteArray(ARRAY_LENGTH, JCSystem.CLEAR_ON_RESET);
dataArray = new byte[ARRAY_LENGTH];
Util.arrayFillNonAtomic(dataArray, (short) 0, ARRAY_LENGTH, (byte) 0);
randomData = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
EC_Consts.randomData = randomData;
keyGenerator = new ECKeyGenerator();
keyTester = new ECKeyTester();
keyTester.allocateECDH();
keyTester.allocateECDHC();
keyTester.allocateECDSA();
}
register();
}
public static void install(byte[] bArray, short bOffset, byte bLength) throws ISOException {
// applet instance creation
new ECTesterApplet(bArray, bOffset, bLength);
}
public void process(APDU apdu) throws ISOException {
// get the APDU buffer
byte[] apduBuffer = apdu.getBuffer();
// ignore the applet select command dispached to the process
if (selectingApplet())
return;
if (apduBuffer[ISO7816.OFFSET_CLA] == CLA_ECTESTERAPPLET) {
switch (apduBuffer[ISO7816.OFFSET_INS]) {
case INS_ALLOCATE:
insAllocate(apdu);
break;
case INS_CLEAR:
insClear(apdu);
break;
case INS_SET:
insSet(apdu);
break;
case INS_GENERATE:
insGenerate(apdu);
break;
case INS_ECDH:
insECDH(apdu);
break;
case INS_ECDSA:
insECDSA(apdu);
break;
default:
// The INS code is not supported by the dispatcher
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
break;
}
} else ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
/**
* Allocates local and remote keyPairs.
* returns allocate SWs
*
* @param apdu P1 = byte keyPair (KEYPAIR_* | ...)
* P2 =
* DATA = short keyLength
* byte keyClass
*/
private void insAllocate(APDU apdu) {
apdu.setIncomingAndReceive();
byte[] apdubuf = apdu.getBuffer();
byte keyPair = apdubuf[ISO7816.OFFSET_P1];
short keyLength = Util.getShort(apdubuf, ISO7816.OFFSET_CDATA);
byte keyClass = apdubuf[ISO7816.OFFSET_CDATA + 2];
short len = allocate(keyPair, keyLength, keyClass, apdubuf, (short) 0);
apdu.setOutgoingAndSend((short) 0, len);
}
/**
*
* @param apdu P1 = byte keyPair (KEYPAIR_* | ...)
* P2 =
*/
private void insClear(APDU apdu) {
apdu.setIncomingAndReceive();
byte[] apdubuf = apdu.getBuffer();
byte keyPair = apdubuf[ISO7816.OFFSET_P1];
short len = 0;
if ((keyPair & KEYPAIR_LOCAL) != 0)
len += clear(localKeypair, apdubuf, (short) 0);
if ((keyPair & KEYPAIR_REMOTE) != 0)
len += clear(remoteKeypair, apdubuf, len);
apdu.setOutgoingAndSend((short) 0, len);
}
/**
* Sets curve parameters on local and remote keyPairs.
* returns setCurve SWs, set params if export
*
* @param apdu P1 = byte keyPair (KEYPAIR_* | ...)
* P2 = byte export (EXPORT_* | KEYPAIR_*)
* DATA = byte curve (EC_Consts.CURVE_*)
* short params (EC_Consts.PARAMETER_* | ...)
* short corruptedParams (EC_Consts.PARAMETER_* | ...)
* byte corruptionType (EC_Consts.CORRUPTION_*)
* <p>
* if curveID = CURVE_EXTERNAL:
* [short paramLength, byte[] param],
* for all params in params,
* in order: field,a,b,g,r,k,w,s
*/
private void insSet(APDU apdu) {
apdu.setIncomingAndReceive();
byte[] apdubuf = apdu.getBuffer();
byte keyPair = apdubuf[ISO7816.OFFSET_P1];
byte export = apdubuf[ISO7816.OFFSET_P2];
byte curve = apdubuf[ISO7816.OFFSET_CDATA];
short params = Util.getShort(apdubuf, (short) (ISO7816.OFFSET_CDATA + 1));
short corruptedParams = Util.getShort(apdubuf, (short) (ISO7816.OFFSET_CDATA + 3));
byte corruptionType = apdubuf[(short) (ISO7816.OFFSET_CDATA + 5)];
short len = 0;
if ((keyPair & KEYPAIR_LOCAL) != 0)
len += set(localKeypair, curve, params, corruptedParams, corruptionType, apdubuf, (short) (ISO7816.OFFSET_CDATA + 6), (short) 0);
if ((keyPair & KEYPAIR_REMOTE) != 0)
len += set(remoteKeypair, curve, params, corruptedParams, corruptionType, apdubuf, (short) (ISO7816.OFFSET_CDATA + 6), len);
if ((export & KEYPAIR_LOCAL) != 0)
len += export(localKeypair, export, params, apdubuf, len);
if ((export & KEYPAIR_REMOTE) != 0)
len += export(remoteKeypair, export, params, apdubuf, len);
apdu.setOutgoingAndSend((short) 0, len);
}
/**
* Generates the local and remote keyPairs.
* returns generate SWs, pubkey and privkey if export
*
* @param apdu P1 = byte keyPair (KEYPAIR_* | ...)
* P2 = byte export (EXPORT_* | KEYPAIR_*)
*/
private void insGenerate(APDU apdu) {
apdu.setIncomingAndReceive();
byte[] apdubuf = apdu.getBuffer();
byte keyPair = apdubuf[ISO7816.OFFSET_P1];
byte export = apdubuf[ISO7816.OFFSET_P2];
short len = 0;
if ((keyPair & KEYPAIR_LOCAL) != 0)
len += generate(localKeypair, apdubuf, (short) 0);
if ((keyPair & KEYPAIR_REMOTE) != 0)
len += generate(remoteKeypair, apdubuf, len);
if ((export & KEYPAIR_LOCAL) != 0)
len += export(localKeypair, export, (short) (EC_Consts.PARAMETER_W | EC_Consts.PARAMETER_S), apdubuf, len);
if ((export & KEYPAIR_REMOTE) != 0)
len += export(remoteKeypair, export, (short) (EC_Consts.PARAMETER_W | EC_Consts.PARAMETER_S), apdubuf, len);
apdu.setOutgoingAndSend((short) 0, len);
}
/**
* Performs ECDH, between the pubkey specified in P1(local/remote) and the privkey specified in P2(local/remote).
* returns deriveSecret SW, if export != 0 => short secretlen, byte[] secret
*
* @param apdu P1 = byte pubkey (KEYPAIR_*)
* P2 = byte privkey (KEYPAIR_*)
* DATA = byte export (EXPORT_ECDH || 0)
* byte invalid (00 = valid, !00 = invalid)
*/
private void insECDH(APDU apdu) {
apdu.setIncomingAndReceive();
byte[] apdubuf = apdu.getBuffer();
byte pubkey = apdubuf[ISO7816.OFFSET_P1];
byte privkey = apdubuf[ISO7816.OFFSET_P2];
byte export = apdubuf[ISO7816.OFFSET_CDATA];
byte invalid = apdubuf[(short) (ISO7816.OFFSET_CDATA + 1)];
short len = ecdh(pubkey, privkey, export, invalid, apdubuf, (short) 0);
apdu.setOutgoingAndSend((short) 0, len);
}
/**
* Performs ECDSA signature and verification on data provided or random, using the keyPair in P1(local/remote).
* returns ecdsa SW, if export != 0 => short signature_length, byte[] signature
*
* @param apdu P1 = byte keyPair (KEYPAIR_*)
* P2 = byte export (EXPORT_SIG || 0)
* DATA = short dataLength (00 = random data generated, !00 = data length)
* byte[] data
*/
private void insECDSA(APDU apdu) {
apdu.setIncomingAndReceive();
byte[] apdubuf = apdu.getBuffer();
byte keyPair = apdubuf[ISO7816.OFFSET_P1];
byte export = apdubuf[ISO7816.OFFSET_P2];
short len = ecdsa(keyPair, export, apdubuf, ISO7816.OFFSET_CDATA, (short) 0);
apdu.setOutgoingAndSend((short) 0, len);
}
/**
* @param keyPair which keyPair to use, local/remote (KEYPAIR_* | ...)
* @param keyLength key length to set
* @param keyClass key class to allocate
* @param buffer apdu buffer
* @param offset offset into apdu buffer
* @return length of data written to the buffer
*/
private short allocate(byte keyPair, short keyLength, byte keyClass, byte[] buffer, short offset) {
short length = 0;
if ((keyPair & KEYPAIR_LOCAL) != 0) {
localKeypair = keyGenerator.allocatePair(keyClass, keyLength);
Util.setShort(buffer, offset, keyGenerator.getSW());
length += 2;
}
if ((keyPair & KEYPAIR_REMOTE) != 0) {
remoteKeypair = keyGenerator.allocatePair(keyClass, keyLength);
Util.setShort(buffer, (short) (offset + length), keyGenerator.getSW());
length += 2;
}
return length;
}
private short clear(KeyPair keyPair, byte[] buffer, short offset) {
short sw = keyGenerator.clearPair(keyPair, ECKeyGenerator.KEY_BOTH);
Util.setShort(buffer, offset, sw);
return 2;
}
/**
* @param keyPair KeyPair to set params on
* @param curve curve to set (EC_Consts.CURVE_*)
* @param params parameters to set (EC_Consts.PARAMETER_* | ...)
* @param corrupted parameters to corrupt (EC_Consts.PARAMETER_* | ...)
* @param corruption corruption type (EC_Consts.CORRUPTION_*)
* @param buffer buffer to read params from and write sw to
* @param inOffset input offset in buffer
* @param outOffset output offset in buffer
* @return length of data written to the buffer
*/
private short set(KeyPair keyPair, byte curve, short params, short corrupted, byte corruption, byte[] buffer, short inOffset, short outOffset) {
short sw = ISO7816.SW_NO_ERROR;
switch (curve) {
case EC_Consts.CURVE_default:
//default, dont set anything
break;
case EC_Consts.CURVE_external:
//external
sw = keyGenerator.setExternalCurve(keyPair, params, buffer, inOffset);
break;
default:
//custom
sw = keyGenerator.setCurve(keyPair, curve, params, ramArray, (short) 0);
break;
}
if (sw == ISO7816.SW_NO_ERROR)
sw = keyGenerator.corruptCurve(keyPair, corrupted, corruption, ramArray, (short) 0);
Util.setShort(buffer, outOffset, sw);
return 2;
}
/**
* @param keyPair KeyPair to generate
* @param buffer buffer to write sw to
* @param offset output offset in buffer
* @return length of data written to the buffer
*/
private short generate(KeyPair keyPair, byte[] buffer, short offset) {
short sw = keyGenerator.generatePair(keyPair);
Util.setShort(buffer, offset, sw);
return 2;
}
/**
* @param keyPair KeyPair to export from
* @param export which key to export from (EXPORT_PUBLIC | EXPORT_PRIVATE)
* @param params which params to export (EC_Consts.PARAMETER_* | ...)
* @param buffer buffer to export params to
* @param offset output offset in buffer
* @return length of data written to the buffer
*/
private short export(KeyPair keyPair, byte export, short params, byte[] buffer, short offset) {
short length = 0;
if ((export & EXPORT_PUBLIC) != 0) {
//export params from public
length += keyGenerator.exportParameters(keyPair, ECKeyGenerator.KEY_PUBLIC, params, buffer, offset);
}
if ((export & EXPORT_PRIVATE) != 0) {
//export params from private
length += keyGenerator.exportParameters(keyPair, ECKeyGenerator.KEY_PRIVATE, params, buffer, (short) (offset + length));
}
return length;
}
/**
* @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
* @param buffer buffer to write sw to, and export ECDH secret if (export & EXPORT_ECDH) != 0
* @param offset output offset in buffer
* @return length of data written to the buffer
*/
private short ecdh(byte pubkey, byte privkey, byte export, byte invalid, byte[] buffer, short offset) {
short length = 0;
KeyPair pub = ((pubkey & KEYPAIR_LOCAL) != 0) ? localKeypair : remoteKeypair;
KeyPair priv = ((privkey & KEYPAIR_LOCAL) != 0) ? localKeypair : remoteKeypair;
short secretLength;
if (invalid != 0) {
secretLength = keyTester.testECDH_invalidPoint((ECPrivateKey) priv.getPrivate(), (ECPublicKey) pub.getPublic(), ramArray, (short) 0, ramArray2, (short) 0);
} else {
secretLength = keyTester.testECDH_validPoint((ECPrivateKey) priv.getPrivate(), (ECPublicKey) pub.getPublic(), ramArray, (short) 0, ramArray2, (short) 0);
}
Util.setShort(buffer, offset, keyTester.getSW());
length += 2;
if ((export & EXPORT_ECDH) != 0) {
Util.setShort(buffer, (short) (offset + length), secretLength);
length += 2;
Util.arrayCopyNonAtomic(ramArray2, (short) 0, buffer, (short) (offset + length), secretLength);
length += secretLength;
}
return length;
}
/**
* @param keyPair keyPair to use for signing and verification (KEYPAIR_LOCAL || KEYPAIR_REMOTE)
* @param export whether to export ECDSA signature
* @param buffer buffer to write sw to, and export ECDSA signature if (export & EXPORT_SIG) != 0
* @param inOffset input offset in buffer
* @param outOffset output offset in buffer
* @return length of data written to the buffer
*/
private short ecdsa(byte keyPair, byte export, byte[] buffer, short inOffset, short outOffset) {
short length = 0;
short dataLength = Util.getShort(buffer, inOffset);
if (dataLength == 0) { //no data to sign
//generate random
dataLength = 32;
randomData.generateData(ramArray, (short) 0, dataLength);
} else {
Util.arrayCopyNonAtomic(buffer, (short) (inOffset + 2), ramArray, (short) 0, dataLength);
}
KeyPair sign = ((keyPair & KEYPAIR_LOCAL) != 0) ? localKeypair : remoteKeypair;
short signatureLength = keyTester.testECDSA((ECPrivateKey) sign.getPrivate(), (ECPublicKey) sign.getPublic(), ramArray, (short) 0, dataLength, ramArray2, (short) 0);
Util.setShort(buffer, outOffset, keyTester.getSW());
length += 2;
if ((export & EXPORT_SIG) != 0) {
Util.setShort(buffer, (short) (outOffset + length), signatureLength);
length += 2;
Util.arrayCopyNonAtomic(ramArray2, (short) 0, buffer, (short) (outOffset + length), signatureLength);
length += signatureLength;
}
return length;
}
}
|