aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2016-12-11 14:20:00 +0100
committerJ08nY2016-12-11 14:20:00 +0100
commitda1ee8381f2f7eaf1cfcd4941ec9f95287435625 (patch)
tree823084e2c74015f38afca4ce8e0807a9548fa84a
parent5f0ec2706a9dbb6aa72ea3c7eb798cd83045e867 (diff)
downloadECTester-da1ee8381f2f7eaf1cfcd4941ec9f95287435625.tar.gz
ECTester-da1ee8381f2f7eaf1cfcd4941ec9f95287435625.tar.zst
ECTester-da1ee8381f2f7eaf1cfcd4941ec9f95287435625.zip
-rw-r--r--!uploader/simpleECC.capbin18296 -> 18330 bytes
-rw-r--r--dist/SimpleAPDU.jarbin470966 -> 471079 bytes
-rw-r--r--src/applets/ECKeyGenerator.java8
-rw-r--r--src/applets/SimpleECCApplet.java63
-rw-r--r--src/simpleapdu/SimpleAPDU.java11
5 files changed, 47 insertions, 35 deletions
diff --git a/!uploader/simpleECC.cap b/!uploader/simpleECC.cap
index 52fef17..886ce88 100644
--- a/!uploader/simpleECC.cap
+++ b/!uploader/simpleECC.cap
Binary files differ
diff --git a/dist/SimpleAPDU.jar b/dist/SimpleAPDU.jar
index 1e96259..be5599a 100644
--- a/dist/SimpleAPDU.jar
+++ b/dist/SimpleAPDU.jar
Binary files differ
diff --git a/src/applets/ECKeyGenerator.java b/src/applets/ECKeyGenerator.java
index 491cd86..2eb5679 100644
--- a/src/applets/ECKeyGenerator.java
+++ b/src/applets/ECKeyGenerator.java
@@ -215,8 +215,8 @@ public class ECKeyGenerator {
public short exportParameter(byte key, short param, byte[] outputBuffer, short outputOffset) {
if (key == KEY_BOTH) {
- return -1;
- }//TODO: change error handling.
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
short length = 0;
try {
switch (param) {
@@ -255,9 +255,9 @@ public class ECKeyGenerator {
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
} catch (CryptoException ce) {
- length = -1;
+ ISOException.throwIt(ce.getReason());
} catch (Exception e) {
- length = -1;
+ ISOException.throwIt(ISO7816.SW_UNKNOWN);
}
return length;
}
diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java
index ecdfa4e..a56250c 100644
--- a/src/applets/SimpleECCApplet.java
+++ b/src/applets/SimpleECCApplet.java
@@ -14,22 +14,22 @@ public class SimpleECCApplet extends Applet {
final static byte CLA_SIMPLEECCAPPLET = (byte) 0xB0;
// INSTRUCTIONS
- final static byte INS_GENERATEKEY = (byte) 0x5a;
- final static byte INS_ALLOCATEKEYPAIRS = (byte) 0x5b;
-
- final static byte INS_ALLOCATEKEYPAIR = (byte) 0x5c;
- final static byte INS_DERIVEECDHSECRET = (byte) 0x5d;
-
- final static byte INS_TESTECSUPPORTALL_FP = (byte) 0x5e;
- final static byte INS_TESTECSUPPORTALL_F2M = (byte) 0x5f;
- final static byte INS_TESTEC_GENERATEINVALID_FP = (byte) 0x70;
- final static byte INS_TESTECSUPPORT_GIVENALG = (byte) 0x71;
- final static byte INS_TESTECSUPPORT_EXTERNAL = (byte) 0x72;
- final static byte INS_TESTEC_LASTUSEDPARAMS = (byte) 0x40;
-
-
- public final static byte P1_SETCURVE = (byte) 0x01;
- public final static byte P1_GENERATEKEYPAIR = (byte) 0x02;
+ final static byte INS_GENERATEKEY = (byte) 0x5a;
+ final static byte INS_ALLOCATEKEYPAIRS = (byte) 0x5b;
+
+ final static byte INS_ALLOCATEKEYPAIR = (byte) 0x5c;
+ final static byte INS_DERIVEECDHSECRET = (byte) 0x5d;
+
+ final static byte INS_TESTECSUPPORTALL_FP = (byte) 0x5e;
+ final static byte INS_TESTECSUPPORTALL_F2M = (byte) 0x5f;
+ final static byte INS_TESTEC_GENERATEINVALID_FP = (byte) 0x70;
+ final static byte INS_TESTECSUPPORT_GIVENALG = (byte) 0x71;
+ final static byte INS_TESTECSUPPORT_EXTERNAL = (byte) 0x72;
+ final static byte INS_TESTEC_LASTUSEDPARAMS = (byte) 0x40;
+
+
+ public final static byte P1_SETCURVE = (byte) 0x01;
+ public final static byte P1_GENERATEKEYPAIR = (byte) 0x02;
final static short ARRAY_LENGTH = (short) 0xff;
@@ -213,11 +213,11 @@ public class SimpleECCApplet extends Applet {
case INS_ALLOCATEKEYPAIRS:
AllocateKeyPairs(apdu);
break;
-*/
+*/
case INS_GENERATEKEY:
GenerateAndReturnKey(apdu);
break;
- default :
+ default:
// The INS code is not supported by the dispatcher
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
break;
@@ -393,7 +393,7 @@ public class SimpleECCApplet extends Applet {
bufferOffset++;
sw = SW_SKIPPED;
if ((testFlags & FLAG_ECTEST_ECDH_AGREEMENT_SMALL_DEGREE_POINT) != (short) 0) {
- short pubLength = EC_Consts.getCurveParameter(EC_Consts.getAnomalousCurve(keyClass,keyLen), EC_Consts.PARAMETER_W, m_ramArray, (short) 0);
+ short pubLength = EC_Consts.getCurveParameter(EC_Consts.getAnomalousCurve(keyClass, keyLen), EC_Consts.PARAMETER_W, m_ramArray, (short) 0);
ecPrivKey = ecKeyGenerator.getPrivateKey();
sw = ecKeyTester.testECDH(ecPrivKey, m_ramArray, (short) 0, pubLength, m_ramArray2, (short) 1);
}
@@ -718,7 +718,6 @@ public class SimpleECCApplet extends Applet {
bufferOffset += 2;
-
//
// 2. Set invalid custom curve (many times)
//
@@ -919,23 +918,23 @@ public class SimpleECCApplet extends Applet {
short offset = ISO7816.OFFSET_CDATA;
byte keyClass = apdubuf[offset];
offset++;
-
+
short keyLength = Util.getShort(apdubuf, offset);
- offset+=2;
+ offset += 2;
byte anomalous = apdubuf[offset];
offset = 0;
-
+
switch (apdubuf[ISO7816.OFFSET_P1]) {
case P1_SETCURVE: {
ecKeyGenerator.allocatePair(keyClass, keyLength);
- if(anomalous != 0) {
+
+ if (anomalous != 0) {
ecKeyGenerator.setCustomAnomalousCurve(keyClass, keyLength, m_ramArray, (short) 0);
} else {
ecKeyGenerator.setCustomCurve(keyClass, keyLength, m_ramArray, (short) 0);
}
-
ecKeyGenerator.generatePair();
ecPubKey = ecKeyGenerator.getPublicKey();
ecPrivKey = ecKeyGenerator.getPrivateKey();
@@ -943,7 +942,10 @@ public class SimpleECCApplet extends Applet {
}
case P1_GENERATEKEYPAIR: {
// Assumption: proper EC keyPair is already allocated and initialized
- ecKeyGenerator.generatePair();
+ short sw = ecKeyGenerator.generatePair();
+ if (sw != ISO7816.SW_NO_ERROR) {
+ ISOException.throwIt(sw);
+ }
ecPubKey = ecKeyGenerator.getPublicKey();
ecPrivKey = ecKeyGenerator.getPrivateKey();
@@ -951,19 +953,18 @@ public class SimpleECCApplet extends Applet {
apdubuf[offset] = EC_Consts.TAG_ECPUBKEY;
offset++;
offset += 2; // reserve space for length
- short len = ecPubKey.getW(apdubuf, offset);
+ short len = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_W, apdubuf, offset);
Util.setShort(apdubuf, (short) (offset - 2), len);
offset += len;
apdubuf[offset] = EC_Consts.TAG_ECPRIVKEY;
offset++;
offset += 2; // reserve space for length
- len = ecPrivKey.getS(apdubuf, offset);
+ len = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PRIVATE, EC_Consts.PARAMETER_S, apdubuf, offset);
Util.setShort(apdubuf, (short) (offset - 2), len);
offset += len;
-
break;
}
- default:
+ default:
ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
}
@@ -1019,7 +1020,7 @@ public class SimpleECCApplet extends Applet {
}
-*/
+*/
}
diff --git a/src/simpleapdu/SimpleAPDU.java b/src/simpleapdu/SimpleAPDU.java
index 241ef4c..4f291f0 100644
--- a/src/simpleapdu/SimpleAPDU.java
+++ b/src/simpleapdu/SimpleAPDU.java
@@ -234,7 +234,13 @@ public class SimpleAPDU {
gatherKeyAPDU[GENERATEECKEY_ALG_OFFSET] = keyClass;
setShort(gatherKeyAPDU, GENERATEECKEY_KEYLENGTH_OFFSET, keyLength);
gatherKeyAPDU[GENERATEECKEY_ANOMALOUS_OFFSET] = anomalous ? (byte) 1 : (byte) 0;
+
ResponseAPDU respGather = cardManager.sendAPDU(gatherKeyAPDU);
+ if (respGather.getSW() != ISO7816.SW_NO_ERROR) {
+ systemOutLogger.println(String.format("Card error: %x", respGather.getSW()));
+ keysFile.close();
+ return;
+ }
// Generate new keypair
gatherKeyAPDU[ISO7816.OFFSET_P1] = SimpleECCApplet.P1_GENERATEKEYPAIR;
@@ -245,6 +251,10 @@ public class SimpleAPDU {
respGather = cardManager.sendAPDU(gatherKeyAPDU);
elapsed += System.nanoTime();
+ if (respGather.getSW() != ISO7816.SW_NO_ERROR) {
+ systemOutLogger.println(String.format("Card error: %x", respGather.getSW()));
+ break;
+ }
byte[] data = respGather.getData();
int offset = 0;
String pubKeyW = "";
@@ -274,6 +284,7 @@ public class SimpleAPDU {
if (counter >= amount && amount != 0)
break;
}
+ keysFile.close();
}
}