diff options
| author | J08nY | 2016-11-21 14:25:58 +0100 |
|---|---|---|
| committer | J08nY | 2016-11-21 14:31:37 +0100 |
| commit | f6054733ede030b83b97a0f82f1f437082c2c48b (patch) | |
| tree | 3cdf3d3b090a1bb79848723f00dee35ffa2f808b /src/applets/SimpleECCApplet.java | |
| parent | 7d946796d87638a5f54cc8562c9d3a95309cf3cc (diff) | |
| parent | b1b21aae218bec813a853938563234ad4e2d8668 (diff) | |
| download | ECTester-f6054733ede030b83b97a0f82f1f437082c2c48b.tar.gz ECTester-f6054733ede030b83b97a0f82f1f437082c2c48b.tar.zst ECTester-f6054733ede030b83b97a0f82f1f437082c2c48b.zip | |
Diffstat (limited to 'src/applets/SimpleECCApplet.java')
| -rw-r--r-- | src/applets/SimpleECCApplet.java | 107 |
1 files changed, 70 insertions, 37 deletions
diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java index 404bf00..8ec9e67 100644 --- a/src/applets/SimpleECCApplet.java +++ b/src/applets/SimpleECCApplet.java @@ -14,18 +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; + 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; @@ -199,11 +203,11 @@ public class SimpleECCApplet extends Applet { case INS_ALLOCATEKEYPAIRS: AllocateKeyPairs(apdu); break; +*/ case INS_GENERATEKEY: - GenerateKey(apdu); + GenerateAndReturnKey(apdu); break; -*/ - default: + default : // The INS code is not supported by the dispatcher ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); break; @@ -802,6 +806,55 @@ public class SimpleECCApplet extends Applet { apdu.setOutgoingAndSend((short) 0, secretLen); } + + void GenerateAndReturnKey(APDU apdu) { + byte[] apdubuf = apdu.getBuffer(); + apdu.setIncomingAndReceive(); + + short bitLen = Util.getShort(apdubuf, ISO7816.OFFSET_CDATA); + + short offset = 0; + + switch (apdubuf[ISO7816.OFFSET_P1]) { + case P1_SETCURVE: { + ecKeyGenerator.allocatePair(KeyPair.ALG_EC_FP, bitLen); + + ecKeyGenerator.generatePair(); + ecPubKey = ecKeyGenerator.getPublicKey(); + ecPrivKey = ecKeyGenerator.getPrivateKey(); + + // If required, initialize curve parameters first + ecKeyGenerator.setCustomCurve(KeyPair.ALG_EC_FP, bitLen, m_ramArray, (short) 0); + break; + } + case P1_GENERATEKEYPAIR: { + // Assumption: proper EC keyPair is already allocated and initialized + ecKeyGenerator.generatePair(); + ecPubKey = ecKeyGenerator.getPublicKey(); + ecPrivKey = ecKeyGenerator.getPrivateKey(); + + offset = 0; + apdubuf[offset] = EC_Consts.TAG_ECPUBKEY; + offset++; + offset += 2; // reserve space for length + short len = ecPubKey.getW(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); + Util.setShort(apdubuf, (short) (offset - 2), len); + offset += len; + + break; + } + default: + ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2); + } + + apdu.setOutgoingAndSend((short) 0, offset); + } /* void AllocateKeyPair(byte algorithm, short bitLen) { @@ -851,28 +904,8 @@ public class SimpleECCApplet extends Applet { EC_Consts.setValidECKeyParams(ecPubKey, ecPrivKey, KeyPair.ALG_EC_FP, bitLen, m_ramArray); } - void GenerateAndReturnKey(APDU apdu) { - byte[] apdubuf = apdu.getBuffer(); - apdu.setIncomingAndReceive(); - - // Assumption: proper EC keyPair is already allocated and initialized - - ecKeyPair.genKeyPair(); - ecPubKey = (ECPublicKey) ecKeyPair.getPrivate(); - ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); - - short offset = 0; - offset += 2; // reserve space for length - short len = ecPubKey.getW(apdubuf, offset); - Util.setShort(apdubuf, (short) (offset - 2), len); - offset += len; - offset += 2; // reserve space for length - len = ecPrivKey.getS(apdubuf, offset); - Util.setShort(apdubuf, (short) (offset - 2), len); - offset += len; - apdu.setOutgoingAndSend((short) 0, offset); - } -*/ +*/ + } |
