@@ -145,16 +145,18 @@ public void process(APDU apdu) throws ISOException
145
145
case INS_ALLOCATEKEYPAIR :
146
146
AllocateKeyPairReturnDefCourve (apdu );
147
147
break ;
148
+ case INS_DERIVEECDHSECRET :
149
+ DeriveECDHSecret (apdu );
150
+ break ;
151
+
152
+ /*
148
153
case INS_ALLOCATEKEYPAIRS:
149
154
AllocateKeyPairs(apdu);
150
155
break;
151
156
case INS_GENERATEKEY:
152
157
GenerateKey(apdu);
153
158
break;
154
- case INS_DERIVEECDHSECRET :
155
- DeriveECDHSecret (apdu );
156
- break ;
157
-
159
+ */
158
160
default :
159
161
// The INS code is not supported by the dispatcher
160
162
ISOException .throwIt ( ISO7816 .SW_INS_NOT_SUPPORTED ) ;
@@ -166,100 +168,6 @@ public void process(APDU apdu) throws ISOException
166
168
}
167
169
168
170
169
- void AllocateKeyPairReturnDefCourve (APDU apdu ) {
170
- byte [] apdubuf = apdu .getBuffer ();
171
- apdu .setIncomingAndReceive ();
172
-
173
- short bitLen = Util .getShort (apdubuf , ISO7816 .OFFSET_CDATA );
174
-
175
- // Note: all locations shoudl happen in constructor. But here it is intentional
176
- // as we like to test for result of allocation
177
- ecKeyPair = new KeyPair (KeyPair .ALG_EC_FP , bitLen );
178
-
179
- // If required, generate also new key pair
180
- if (apdubuf [ISO7816 .OFFSET_P1 ] == (byte ) 1 ) {
181
- ecPubKey = (ECPublicKey ) ecKeyPair .getPublic ();
182
- ecPrivKey = (ECPrivateKey ) ecKeyPair .getPrivate ();
183
- // Some implementation wil not return valid pub key until ecKeyPair.genKeyPair() is called
184
- // Other implementation will fail with exception if same is called => try catch
185
- try {
186
- if (ecPubKey == null ) {
187
- ecKeyPair .genKeyPair ();
188
- }
189
- } catch (Exception e ) {
190
- } // do nothing
191
-
192
- // If required, initialize curve parameters first
193
- if (apdubuf [ISO7816 .OFFSET_P2 ] == (byte ) 2 ) {
194
- EC_Consts .setValidECKeyParams (ecPubKey , ecPrivKey , KeyPair .ALG_EC_FP , bitLen , m_ramArray );
195
- }
196
-
197
- // Now generate new keypair with either default or custom curve
198
- ecKeyPair .genKeyPair ();
199
- ecPubKey = (ECPublicKey ) ecKeyPair .getPublic ();
200
- ecPrivKey = (ECPrivateKey ) ecKeyPair .getPrivate ();
201
-
202
- short len = 0 ;
203
- short offset = 0 ;
204
-
205
- // Export curve public parameters
206
- offset += 2 ; // reserve space for length
207
- len = ecPubKey .getField (apdubuf , offset );
208
- Util .setShort (apdubuf , (short ) (offset - 2 ), len );
209
- offset += len ;
210
- offset += 2 ; // reserve space for length
211
- len = ecPubKey .getA (apdubuf , offset );
212
- Util .setShort (apdubuf , (short ) (offset - 2 ), len );
213
- offset += len ;
214
-
215
- offset += 2 ; // reserve space for length
216
- len = ecPubKey .getB (apdubuf , offset );
217
- Util .setShort (apdubuf , (short ) (offset - 2 ), len );
218
- offset += len ;
219
- offset += 2 ; // reserve space for length
220
- len = ecPubKey .getR (apdubuf , offset );
221
- Util .setShort (apdubuf , (short ) (offset - 2 ), len );
222
- offset += len ;
223
- /*
224
- offset += 2; // reserve space for length
225
- len = ecPubKey.getW(apdubuf, offset);
226
- Util.setShort(apdubuf, (short) (offset - 2), len);
227
- offset += len;
228
- */
229
- apdu .setOutgoingAndSend ((short ) 0 , offset );
230
- }
231
- }
232
-
233
-
234
-
235
- void DeriveECDHSecret (APDU apdu ) {
236
- byte [] apdubuf = apdu .getBuffer ();
237
- short len = apdu .setIncomingAndReceive ();
238
-
239
- // Assumption: proper EC keyPair is already allocated
240
- // If public key point is provided, then use it
241
- if (len == 0 ) {
242
- // if not provided, use build-in one (valid for for 192 only)
243
- Util .arrayCopyNonAtomic (EC192_FP_PUBLICW , (short ) 0 , apdubuf , ISO7816 .OFFSET_CDATA , (short ) EC192_FP_PUBLICW .length );
244
- len = (short ) EC192_FP_PUBLICW .length ;
245
- }
246
-
247
- // Generate fresh EC keypair
248
- ecKeyPair .genKeyPair ();
249
- ecPrivKey = (ECPrivateKey ) ecKeyPair .getPrivate ();
250
-
251
- if (dhKeyAgreement == null ) {
252
- dhKeyAgreement = KeyAgreement .getInstance (KeyAgreement .ALG_EC_SVDP_DH , false );
253
- }
254
- dhKeyAgreement .init (ecPrivKey );
255
- short secretLen = 0 ;
256
- // Generate and export secret
257
- secretLen = dhKeyAgreement .generateSecret (apdubuf , ISO7816 .OFFSET_CDATA , len , m_ramArray , (short ) 0 );
258
- Util .arrayCopyNonAtomic (m_ramArray , (short ) 0 , apdubuf , (short ) 0 , secretLen );
259
-
260
- apdu .setOutgoingAndSend ((short ) 0 , secretLen );
261
- }
262
-
263
171
short TestECSupport (byte keyClass , short keyLen , byte [] buffer , short bufferOffset ) {
264
172
short baseOffset = bufferOffset ;
265
173
@@ -487,6 +395,98 @@ void TestEC_F2M_SupportAllLengths(APDU apdu) {
487
395
apdu .setOutgoingAndSend ((short ) 0 , dataOffset );
488
396
}
489
397
398
+ void AllocateKeyPairReturnDefCourve (APDU apdu ) {
399
+ byte [] apdubuf = apdu .getBuffer ();
400
+ apdu .setIncomingAndReceive ();
401
+
402
+ short bitLen = Util .getShort (apdubuf , ISO7816 .OFFSET_CDATA );
403
+
404
+ // Note: all locations shoudl happen in constructor. But here it is intentional
405
+ // as we like to test for result of allocation
406
+ ecKeyPair = new KeyPair (KeyPair .ALG_EC_FP , bitLen );
407
+
408
+ // If required, generate also new key pair
409
+ if (apdubuf [ISO7816 .OFFSET_P1 ] == (byte ) 1 ) {
410
+ ecPubKey = (ECPublicKey ) ecKeyPair .getPublic ();
411
+ ecPrivKey = (ECPrivateKey ) ecKeyPair .getPrivate ();
412
+ // Some implementation wil not return valid pub key until ecKeyPair.genKeyPair() is called
413
+ // Other implementation will fail with exception if same is called => try catch
414
+ try {
415
+ if (ecPubKey == null ) {
416
+ ecKeyPair .genKeyPair ();
417
+ }
418
+ } catch (Exception e ) {
419
+ } // do nothing
420
+
421
+ // If required, initialize curve parameters first
422
+ if (apdubuf [ISO7816 .OFFSET_P2 ] == (byte ) 2 ) {
423
+ EC_Consts .setValidECKeyParams (ecPubKey , ecPrivKey , KeyPair .ALG_EC_FP , bitLen , m_ramArray );
424
+ }
425
+
426
+ // Now generate new keypair with either default or custom curve
427
+ ecKeyPair .genKeyPair ();
428
+ ecPubKey = (ECPublicKey ) ecKeyPair .getPublic ();
429
+ ecPrivKey = (ECPrivateKey ) ecKeyPair .getPrivate ();
430
+
431
+ short len = 0 ;
432
+ short offset = 0 ;
433
+
434
+ // Export curve public parameters
435
+ offset += 2 ; // reserve space for length
436
+ len = ecPubKey .getField (apdubuf , offset );
437
+ Util .setShort (apdubuf , (short ) (offset - 2 ), len );
438
+ offset += len ;
439
+ offset += 2 ; // reserve space for length
440
+ len = ecPubKey .getA (apdubuf , offset );
441
+ Util .setShort (apdubuf , (short ) (offset - 2 ), len );
442
+ offset += len ;
443
+
444
+ offset += 2 ; // reserve space for length
445
+ len = ecPubKey .getB (apdubuf , offset );
446
+ Util .setShort (apdubuf , (short ) (offset - 2 ), len );
447
+ offset += len ;
448
+ offset += 2 ; // reserve space for length
449
+ len = ecPubKey .getR (apdubuf , offset );
450
+ Util .setShort (apdubuf , (short ) (offset - 2 ), len );
451
+ offset += len ;
452
+ /*
453
+ offset += 2; // reserve space for length
454
+ len = ecPubKey.getW(apdubuf, offset);
455
+ Util.setShort(apdubuf, (short) (offset - 2), len);
456
+ offset += len;
457
+ */
458
+ apdu .setOutgoingAndSend ((short ) 0 , offset );
459
+ }
460
+ }
461
+
462
+ void DeriveECDHSecret (APDU apdu ) {
463
+ byte [] apdubuf = apdu .getBuffer ();
464
+ short len = apdu .setIncomingAndReceive ();
465
+
466
+ // Assumption: proper EC keyPair is already allocated
467
+ // If public key point is provided, then use it
468
+ if (len == 0 ) {
469
+ // if not provided, use build-in one (valid only for 192 only)
470
+ Util .arrayCopyNonAtomic (EC192_FP_PUBLICW , (short ) 0 , apdubuf , ISO7816 .OFFSET_CDATA , (short ) EC192_FP_PUBLICW .length );
471
+ len = (short ) EC192_FP_PUBLICW .length ;
472
+ }
473
+
474
+ // Generate fresh EC keypair
475
+ ecKeyPair .genKeyPair ();
476
+ ecPrivKey = (ECPrivateKey ) ecKeyPair .getPrivate ();
477
+
478
+ if (dhKeyAgreement == null ) {
479
+ dhKeyAgreement = KeyAgreement .getInstance (KeyAgreement .ALG_EC_SVDP_DH , false );
480
+ }
481
+ dhKeyAgreement .init (ecPrivKey );
482
+ short secretLen = 0 ;
483
+ // Generate and export secret
484
+ secretLen = dhKeyAgreement .generateSecret (apdubuf , ISO7816 .OFFSET_CDATA , len , m_ramArray , (short ) 0 );
485
+ Util .arrayCopyNonAtomic (m_ramArray , (short ) 0 , apdubuf , (short ) 0 , secretLen );
486
+
487
+ apdu .setOutgoingAndSend ((short ) 0 , secretLen );
488
+ }
489
+
490
490
491
491
492
492
@@ -503,8 +503,7 @@ void TestEC_F2M_SupportAllLengths(APDU apdu) {
503
503
504
504
505
505
506
-
507
-
506
+ /*
508
507
void AllocateKeyPair(byte algorithm, short bitLen) {
509
508
// Select proper attributes
510
509
switch (bitLen) {
@@ -552,39 +551,7 @@ void AllocateKeyPair(byte algorithm, short bitLen) {
552
551
EC_Consts.setValidECKeyParams(ecPubKey, ecPrivKey, KeyPair.ALG_EC_FP, bitLen, m_ramArray);
553
552
}
554
553
555
- short TryAllocateKeyPair (byte algorithm , short bitLen , byte [] buffer , short offset ) {
556
- // Try allocation, log result
557
- try {
558
- offset = Util .setShort (buffer , offset , bitLen );
559
- AllocateKeyPair (KeyPair .ALG_EC_FP , bitLen );
560
- buffer [offset ] = 1 ;
561
- offset ++;
562
- } catch (Exception e ) {
563
- buffer [offset ] = 0 ;
564
- offset ++;
565
- }
566
- return offset ;
567
- }
568
- void AllocateKeyPairs (APDU apdu ) {
569
- byte [] apdubuf = apdu .getBuffer ();
570
- apdu .setIncomingAndReceive ();
571
-
572
- short offset = 0 ;
573
-
574
- //offset = TryAllocateKeyPair(KeyPair.ALG_EC_FP, (short) 128, apdubuf, offset);
575
- //offset = TryAllocateKeyPair(KeyPair.ALG_EC_FP, (short) 160, apdubuf, offset);
576
- //offset = TryAllocateKeyPair(KeyPair.ALG_EC_FP, (short) 192, apdubuf, offset);
577
- //offset = TryAllocateKeyPair(KeyPair.ALG_EC_FP, (short) 256, apdubuf, offset);
578
-
579
- apdu .setOutgoingAndSend ((short ) 0 , offset );
580
- }
581
-
582
-
583
-
584
-
585
-
586
-
587
- void GenerateKey (APDU apdu ) {
554
+ void GenerateAndReturnKey(APDU apdu) {
588
555
byte[] apdubuf = apdu.getBuffer();
589
556
apdu.setIncomingAndReceive();
590
557
@@ -606,5 +573,6 @@ void GenerateKey(APDU apdu) {
606
573
607
574
apdu.setOutgoingAndSend((short) 0, offset);
608
575
}
576
+ */
609
577
}
610
578
0 commit comments