diff options
| author | petrs | 2016-09-08 16:36:03 +0200 |
|---|---|---|
| committer | petrs | 2016-09-08 16:36:03 +0200 |
| commit | 1796567aada62e02bb262340a166555b632f3d15 (patch) | |
| tree | 90f7169c9d7903e9fa6076d923a41c2fdc870e39 /src/simpleapdu | |
| parent | b97597bf20f5b44fb024c994cd3961c0a10d204b (diff) | |
| download | ECTester-1796567aada62e02bb262340a166555b632f3d15.tar.gz ECTester-1796567aada62e02bb262340a166555b632f3d15.tar.zst ECTester-1796567aada62e02bb262340a166555b632f3d15.zip | |
- added testing of EC_FP 128-521bits (KeyPair allocation, gen key with def. curve, set custom curve, gen keypair with custom curve, set invalid curve, gen keypair with invalid curve)
- parsing code with formating and printing
Diffstat (limited to 'src/simpleapdu')
| -rw-r--r-- | src/simpleapdu/CardMngr.java | 1 | ||||
| -rw-r--r-- | src/simpleapdu/SimpleAPDU.java | 81 |
2 files changed, 78 insertions, 4 deletions
diff --git a/src/simpleapdu/CardMngr.java b/src/simpleapdu/CardMngr.java index d3ff86b..1ab6408 100644 --- a/src/simpleapdu/CardMngr.java +++ b/src/simpleapdu/CardMngr.java @@ -165,6 +165,7 @@ public class CardMngr { return buf.toString(); } + public char toHexChar(int i) { if ((0 <= i) && (i <= 9)) { return (char) ('0' + i); diff --git a/src/simpleapdu/SimpleAPDU.java b/src/simpleapdu/SimpleAPDU.java index 240a1cf..d1e6ff1 100644 --- a/src/simpleapdu/SimpleAPDU.java +++ b/src/simpleapdu/SimpleAPDU.java @@ -1,6 +1,9 @@ package simpleapdu; import applets.SimpleECCApplet; +import javacard.framework.ISO7816; +import javacard.security.CryptoException; +import javacard.security.KeyPair; import javax.smartcardio.ResponseAPDU; /** @@ -24,6 +27,12 @@ public class SimpleAPDU { private static byte GENERATEKEY[] = {(byte) 0xB0, (byte) 0x5A, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00}; private static byte RESPONDDATA[] = {(byte) 0xB0, (byte) 0x5B, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x30}; + private static byte TESTECSUPPORTALL[] = {(byte) 0xB0, (byte) 0x5E, (byte) 0x00, (byte) 0x00, (byte) 0x00}; + + static short getShort(byte[] array, int offset) { + return (short) (((array[offset] & 0xFF) << 8) | (array[offset + 1] & 0xFF)); + } + public static void main(String[] args) { try { // @@ -62,10 +71,8 @@ public class SimpleAPDU { // Select our application on card cardManager.sendAPDU(SELECT_SIMPLEAPPLET); - for (int i = 0; i < 10; i++) { - cardManager.sendAPDU(GENERATEKEY); - } - cardManager.sendAPDU(RESPONDDATA); + ResponseAPDU resp = cardManager.sendAPDU(TESTECSUPPORTALL); + PrintECSupport(resp); cardManager.DisconnectFromCard(); } else { @@ -75,4 +82,70 @@ public class SimpleAPDU { System.out.println("Exception : " + ex); } } + + static String getPrintError(short code) { + if (code == ISO7816.SW_NO_ERROR) { + return "OK\t(0x9000)"; + } + else { + String codeStr = "unknown"; + if (code == CryptoException.ILLEGAL_VALUE) { + codeStr = "ILLEGAL_VALUE"; + } + if (code == CryptoException.UNINITIALIZED_KEY) { + codeStr = "UNINITIALIZED_KEY"; + } + if (code == CryptoException.NO_SUCH_ALGORITHM) { + codeStr = "NO_SUCH_ALG"; + } + if (code == CryptoException.INVALID_INIT) { + codeStr = "INVALID_INIT"; + } + if (code == CryptoException.ILLEGAL_USE) { + codeStr = "ILLEGAL_USE"; + } + if (code == SimpleECCApplet.SW_SKIPPED) { + codeStr = "skipped"; + } + return String.format("fail\t(%s,\t0x%4x)", codeStr, code); + } + } + static int VerifyPrintResult(String message, byte expectedTag, byte[] buffer, int bufferOffset) { + assert (buffer[bufferOffset] == expectedTag); + bufferOffset++; + short resCode = getShort(buffer, bufferOffset); + bufferOffset += 2; + System.out.println(String.format("%-40s%s", message, getPrintError(resCode))); + return bufferOffset; + } + static void PrintECSupport(ResponseAPDU resp) { + byte[] buffer = resp.getData(); + + int bufferOffset = 0; + while (bufferOffset < buffer.length) { + assert(buffer[bufferOffset] == SimpleECCApplet.ECTEST_SEPARATOR); + bufferOffset++; + String ecType = "unknown"; + if (buffer[bufferOffset] == KeyPair.ALG_EC_FP) { + ecType = "ALG_EC_FP"; + } + if (buffer[bufferOffset] == KeyPair.ALG_EC_F2M) { + ecType = "ALG_EC_F2M"; + } + System.out.println(String.format("%-40s%s", "EC type:", ecType)); + bufferOffset++; + short keyLen = getShort(buffer, bufferOffset); + System.out.println(String.format("%-40s%d bits", "EC key length (bits):", keyLen)); + bufferOffset += 2; + + bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset); + bufferOffset = VerifyPrintResult("Generate key with def curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_DEFCURVE, buffer, bufferOffset); + bufferOffset = VerifyPrintResult("Set valid custom curve:", SimpleECCApplet.ECTEST_SET_VALIDCURVE, buffer, bufferOffset); + bufferOffset = VerifyPrintResult("Generate key with valid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE, buffer, bufferOffset); + bufferOffset = VerifyPrintResult("Set invalid custom curve:", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset); + bufferOffset = VerifyPrintResult("Generate key with invalid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE, buffer, bufferOffset); + + System.out.println(); + } + } } |
