Skip to content

Commit 4f5a072

Browse files
committed
EC curve testing in separate apdu due to weird behavior of some cards after multiple errors within single apdu
1 parent 42b8996 commit 4f5a072

File tree

4 files changed

+83
-12
lines changed

4 files changed

+83
-12
lines changed

!uploader/simpleECC.cap

98 Bytes
Binary file not shown.

dist/SimpleAPDU.jar

1.2 KB
Binary file not shown.

src/applets/SimpleECCApplet.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class SimpleECCApplet extends javacard.framework.Applet
2323
final static byte INS_TESTECSUPPORTALL_FP = (byte) 0x5e;
2424
final static byte INS_TESTECSUPPORTALL_F2M = (byte) 0x5f;
2525
final static byte INS_TESTEC_GENERATEINVALID_FP = (byte) 0x70;
26+
final static byte INS_TESTECSUPPORT_GIVENALG = (byte) 0x71;
2627
final static byte INS_TESTEC_LASTUSEDPARAMS = (byte) 0x40;
2728

2829

@@ -165,6 +166,10 @@ public void process(APDU apdu) throws ISOException
165166

166167
if (apduBuffer[ISO7816.OFFSET_CLA] == CLA_SIMPLEECCAPPLET) {
167168
switch ( apduBuffer[ISO7816.OFFSET_INS] ) {
169+
170+
case INS_TESTECSUPPORT_GIVENALG:
171+
TestEC_SupportGivenLength(apdu);
172+
break;
168173
case INS_TESTECSUPPORTALL_FP:
169174
TestEC_FP_SupportAllLengths(apdu);
170175
break;
@@ -433,11 +438,26 @@ short TestECSupport(byte keyClass, short keyLen, byte[] buffer, short bufferOffs
433438
return (short) (bufferOffset - baseOffset);
434439
}
435440

436-
void TestEC_FP_SupportAllLengths(APDU apdu) {
441+
void TestEC_SupportGivenLength(APDU apdu) {
437442
byte[] apdubuf = apdu.getBuffer();
438443
short len = apdu.setIncomingAndReceive();
439444

445+
short dataOffset = ISO7816.OFFSET_CDATA;
446+
byte algType = apdubuf[dataOffset]; dataOffset++;
447+
short keyLength = Util.getShort(apdubuf, dataOffset);
448+
dataOffset += 2;
449+
450+
dataOffset = 0;
451+
dataOffset += TestECSupport(algType, keyLength, apdubuf, dataOffset);
452+
453+
apdu.setOutgoingAndSend((short) 0, dataOffset);
454+
}
455+
456+
void TestEC_FP_SupportAllLengths(APDU apdu) {
457+
byte[] apdubuf = apdu.getBuffer();
458+
short len = apdu.setIncomingAndReceive();
440459
short dataOffset = 0;
460+
441461
// FP
442462
dataOffset += TestECSupport(KeyPair.ALG_EC_FP, (short) 128, apdubuf, dataOffset);
443463
dataOffset += TestECSupport(KeyPair.ALG_EC_FP, (short) 160, apdubuf, dataOffset);

src/simpleapdu/SimpleAPDU.java

+62-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public class SimpleAPDU {
2121

2222
private static final byte TESTECSUPPORTALL_FP[] = {(byte) 0xB0, (byte) 0x5E, (byte) 0x00, (byte) 0x00, (byte) 0x00};
2323
private static final byte TESTECSUPPORTALL_F2M[] = {(byte) 0xB0, (byte) 0x5F, (byte) 0x00, (byte) 0x00, (byte) 0x00};
24+
private static final byte TESTECSUPPORT_GIVENALG[] = {(byte) 0xB0, (byte) 0x71, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00};
25+
private static final short TESTECSUPPORT_ALG_OFFSET = 5;
26+
private static final short TESTECSUPPORT_KEYLENGTH_OFFSET = 6;
27+
2428
private static final byte TESTECSUPPORTALL_LASTUSEDPARAMS[] = {(byte) 0xB0, (byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x00};
2529

2630
private static final byte TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB[] = {(byte) 0xB0, (byte) 0x70, (byte) 0x00, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
@@ -45,34 +49,81 @@ static void testFPkeyGen_rewindOnSuccess(byte[] apduArray, boolean bRewind) {
4549
apduArray[INVALIDCURVEB_REWINDONSUCCESS_OFFSET] = bRewind ? (byte) 1 : (byte) 0;
4650
}
4751

52+
static CardMngr ReconnnectToCard() throws Exception {
53+
cardManager.DisconnectFromCard();
54+
if (cardManager.ConnectToCard()) {
55+
// Select our application on card
56+
cardManager.sendAPDU(SELECT_ECTESTERAPPLET);
57+
}
58+
return cardManager;
59+
}
60+
61+
static void testSupportECGivenAlg(byte[] apdu, CardMngr cardManager) throws Exception {
62+
ReconnnectToCard();
63+
ResponseAPDU resp = cardManager.sendAPDU(apdu);
64+
PrintECSupport(resp);
65+
}
66+
static void testSupportECAll(CardMngr cardManager) throws Exception {
67+
byte[] testAPDU = Arrays.clone(TESTECSUPPORT_GIVENALG);
68+
69+
testAPDU[TESTECSUPPORT_ALG_OFFSET] = KeyPair.ALG_EC_FP;
70+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 128);
71+
testSupportECGivenAlg(testAPDU, cardManager);
72+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 160);
73+
testSupportECGivenAlg(testAPDU, cardManager);
74+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 192);
75+
testSupportECGivenAlg(testAPDU, cardManager);
76+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 224);
77+
testSupportECGivenAlg(testAPDU, cardManager);
78+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 256);
79+
testSupportECGivenAlg(testAPDU, cardManager);
80+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 384);
81+
testSupportECGivenAlg(testAPDU, cardManager);
82+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 521);
83+
testSupportECGivenAlg(testAPDU, cardManager);
84+
85+
testAPDU[TESTECSUPPORT_ALG_OFFSET] = KeyPair.ALG_EC_F2M;
86+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 113);
87+
testSupportECGivenAlg(testAPDU, cardManager);
88+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 131);
89+
testSupportECGivenAlg(testAPDU, cardManager);
90+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 163);
91+
testSupportECGivenAlg(testAPDU, cardManager);
92+
setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 193);
93+
testSupportECGivenAlg(testAPDU, cardManager);
94+
95+
}
4896
public static void main(String[] args) {
4997
try {
5098
//
5199
// REAL CARDS
52100
//
53101
if (cardManager.ConnectToCard()) {
54-
// Select our application on card
55-
cardManager.sendAPDU(SELECT_ECTESTERAPPLET);
56102

57-
// Test setting invalid parameter B of curev
103+
testSupportECAll(cardManager);
104+
105+
// Test setting invalid parameter B of curve
58106
byte[] testAPDU = Arrays.clone(TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB);
59107
//testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_LASTBYTEINCREMENT);
60108
testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_ONEBYTERANDOM);
61109
//testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_FULLRANDOM);
62110
testFPkeyGen_setNumRepeats(testAPDU, (short) 10);
63111
testFPkeyGen_rewindOnSuccess(testAPDU, true);
112+
ReconnnectToCard();
64113
ResponseAPDU resp_fp_keygen = cardManager.sendAPDU(testAPDU);
65114
ResponseAPDU resp_keygen_params = cardManager.sendAPDU(TESTECSUPPORTALL_LASTUSEDPARAMS);
66115
PrintECKeyGenInvalidCurveB(resp_fp_keygen);
67116
PrintECKeyGenInvalidCurveB_lastUserParams(resp_keygen_params);
68-
69-
// Test support for different types of curves
70-
ResponseAPDU resp_fp = cardManager.sendAPDU(TESTECSUPPORTALL_FP);
71-
ResponseAPDU resp_f2m = cardManager.sendAPDU(TESTECSUPPORTALL_F2M);
72-
PrintECSupport(resp_fp);
73-
PrintECSupport(resp_f2m);
74-
75117

118+
/*
119+
// Test support for different types of curves
120+
ReconnnectToCard();
121+
ResponseAPDU resp_fp = cardManager.sendAPDU(TESTECSUPPORTALL_FP);
122+
ReconnnectToCard();
123+
ResponseAPDU resp_f2m = cardManager.sendAPDU(TESTECSUPPORTALL_F2M);
124+
PrintECSupport(resp_fp);
125+
PrintECSupport(resp_f2m);
126+
*/
76127

77128
cardManager.DisconnectFromCard();
78129
} else {
@@ -209,7 +260,7 @@ static void PrintECKeyGenInvalidCurveB(ResponseAPDU resp) {
209260

210261
short numRepeats = getShort(buffer, bufferOffset);
211262
bufferOffset += 2;
212-
System.out.println(String.format("Executed repeats before unexpected error: %d times", numRepeats));
263+
System.out.println(String.format("%-53s%d times", "Executed repeats before unexpected error: ", numRepeats));
213264

214265

215266
bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);

0 commit comments

Comments
 (0)