From b1b21aae218bec813a853938563234ad4e2d8668 Mon Sep 17 00:00:00 2001 From: petrs Date: Fri, 18 Nov 2016 21:18:22 +0100 Subject: Added support for multiple readers Added support for mass gathering of ECC keys (refactoring needed) --- src/applets/SimpleECCApplet.java | 88 ++++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 25 deletions(-) (limited to 'src/applets/SimpleECCApplet.java') diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java index 720ee4e..18eec5a 100644 --- a/src/applets/SimpleECCApplet.java +++ b/src/applets/SimpleECCApplet.java @@ -27,6 +27,8 @@ public class SimpleECCApplet extends javacard.framework.Applet 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; @@ -192,10 +194,10 @@ public class SimpleECCApplet extends javacard.framework.Applet case INS_ALLOCATEKEYPAIRS: AllocateKeyPairs(apdu); break; +*/ case INS_GENERATEKEY: - GenerateKey(apdu); + GenerateAndReturnKey(apdu); break; -*/ default : // The INS code is not supported by the dispatcher ISOException.throwIt( ISO7816.SW_INS_NOT_SUPPORTED ) ; @@ -741,7 +743,7 @@ public class SimpleECCApplet extends javacard.framework.Applet short bitLen = Util.getShort(apdubuf, ISO7816.OFFSET_CDATA); - // Note: all locations shoudl happen in constructor. But here it is intentional + // Note: all locations should happen in constructor. But here it is intentional // as we like to test for result of allocation ecKeyPair = new KeyPair(KeyPair.ALG_EC_FP, bitLen); @@ -827,7 +829,64 @@ public class SimpleECCApplet extends javacard.framework.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: { + ecKeyPair = new KeyPair(KeyPair.ALG_EC_FP, bitLen); + + ecPubKey = (ECPublicKey) ecKeyPair.getPublic(); + ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); + // Some implementation wil not return valid pub key until ecKeyPair.genKeyPair() is called + // Other implementation will fail with exception if same is called => try catch + try { + if (ecPubKey == null) { + ecKeyPair.genKeyPair(); + } + } catch (Exception e) { + } // do nothing + + // If required, initialize curve parameters first + EC_Consts.setValidECKeyParams(ecPubKey, ecPrivKey, KeyPair.ALG_EC_FP, bitLen, m_ramArray); + + break; + } + case P1_GENERATEKEYPAIR: { + // Assumption: proper EC keyPair is already allocated and initialized + ecKeyPair.genKeyPair(); + ecPubKey = (ECPublicKey) ecKeyPair.getPublic(); + ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); + + 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); + } @@ -891,28 +950,7 @@ public class SimpleECCApplet extends javacard.framework.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); - } */ } -- cgit v1.3.1 From b4d72715e7d770b4925fef70a192665744a6273d Mon Sep 17 00:00:00 2001 From: J08nY Date: Sun, 30 Oct 2016 17:58:27 +0100 Subject: EC_Consts: added F2M curve support, + F2M curve sect163r1 to start --- src/applets/ECKeyGenerator.java | 143 +++-- src/applets/ECKeyTester.java | 1 + src/applets/EC_Consts.java | 1238 ++++++++++++++++++++++---------------- src/applets/SimpleECCApplet.java | 699 ++++++++------------- src/simpleapdu/SimpleAPDU.java | 75 +-- 5 files changed, 1087 insertions(+), 1069 deletions(-) (limited to 'src/applets/SimpleECCApplet.java') diff --git a/src/applets/ECKeyGenerator.java b/src/applets/ECKeyGenerator.java index ff2193c..c7155de 100644 --- a/src/applets/ECKeyGenerator.java +++ b/src/applets/ECKeyGenerator.java @@ -16,26 +16,17 @@ public class ECKeyGenerator { private ECPrivateKey ecPrivateKey = null; private ECPublicKey ecPublicKey = null; - public static final byte PARAMETER_FP = 1; - public static final byte PARAMETER_F2M_ONE = 2; - public static final byte PARAMETER_F2M_THREE = 3; - public static final byte PARAMETER_A = 4; - public static final byte PARAMETER_B = 5; - public static final byte PARAMETER_G = 6; - public static final byte PARAMETER_R = 7; - public static final byte PARAMETER_K = 8; - - private static final byte PARAMETER_S = 9; //private key - private static final byte PARAMETER_W = 10;//public key - public static final byte KEY_PUBLIC = 0x1; public static final byte KEY_PRIVATE = 0x2; public static final byte KEY_BOTH = KEY_PUBLIC & KEY_PRIVATE; + public short allocatePair(byte algorithm, short keyLength) { short result = ISO7816.SW_NO_ERROR; try { ecKeyPair = new KeyPair(algorithm, keyLength); + ecPrivateKey = (ECPrivateKey) ecKeyPair.getPrivate(); + ecPublicKey = (ECPublicKey) ecKeyPair.getPublic(); } catch (CryptoException ce) { result = ce.getReason(); } catch (Exception e) { @@ -44,7 +35,7 @@ public class ECKeyGenerator { return result; } - public boolean isAlocated() { + public boolean isAllocated() { return ecKeyPair != null && ecPrivateKey != null && ecPublicKey != null; } @@ -52,7 +43,7 @@ public class ECKeyGenerator { short result = ISO7816.SW_NO_ERROR; try { ecKeyPair.genKeyPair(); - ecPrivateKey = (ECPrivateKey) ecKeyPair.getPrivate(); //TODO, do I want to keep private and pubkey separate from the keypair? + ecPrivateKey = (ECPrivateKey) ecKeyPair.getPrivate(); ecPublicKey = (ECPublicKey) ecKeyPair.getPublic(); } catch (CryptoException ce) { result = ce.getReason(); @@ -62,61 +53,84 @@ public class ECKeyGenerator { return result; } - public short setCustomCurve(byte keyClass, short keyLength) { - //TODO - return 0; + public short setCustomCurve(byte keyClass, short keyLength, byte[] buffer, short offset) { + return setCustomCurve(EC_Consts.getCurve(keyClass, keyLength), buffer, offset); + } + + public short setCustomCurve(byte curve, byte[] buffer, short offset) { + byte alg = EC_Consts.getCurveType(curve); + short sw = ISO7816.SW_NO_ERROR; + short length; + if (alg == KeyPair.ALG_EC_FP) { + length = EC_Consts.getCurveParameter(curve, EC_Consts.PARAMETER_FP, buffer, offset); + sw = setExternalParameter(KEY_BOTH, EC_Consts.PARAMETER_FP, buffer, offset, length); + } else if (alg == KeyPair.ALG_EC_F2M) { + length = EC_Consts.getCurveParameter(curve, EC_Consts.PARAMETER_F2M, buffer, offset); + sw = setExternalParameter(KEY_BOTH, EC_Consts.PARAMETER_F2M, buffer, offset, length); + } + if (sw != ISO7816.SW_NO_ERROR) return sw; + + for (byte param = EC_Consts.PARAMETER_A; param < EC_Consts.PARAMETER_K; ++param) { + length = EC_Consts.getCurveParameter(curve, param, buffer, offset); + sw = setExternalParameter(KEY_BOTH, param, buffer, offset, length); + if (sw != ISO7816.SW_NO_ERROR) break; + } + return sw; + } + + public short setCustomInvalidCurve(short keyClass, short keyLength, byte key, byte param, short corruptionType, byte[] buffer, short offset) { + return setCustomInvalidCurve(EC_Consts.getCurve(keyClass, keyLength), key, param, corruptionType, buffer, offset); } - public short setCustomCurve(byte curve) { - //TODO - return 0; + public short setCustomInvalidCurve(byte curve, byte key, byte param, short corruptionType, byte[] buffer, short offset) { + short sw = setCustomCurve(curve, buffer, offset); + if (sw != ISO7816.SW_NO_ERROR) return sw; + + short length = EC_Consts.getCorruptCurveParameter(curve, param, buffer, offset, corruptionType); + sw = setExternalParameter(key, param, buffer, offset, length); + return sw; } public short setExternalParameter(byte key, byte param, byte[] data, short offset, short length) { short result = ISO7816.SW_NO_ERROR; try { switch (param) { - case PARAMETER_FP: + case EC_Consts.PARAMETER_FP: if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldFP(data, offset, length); if ((key & KEY_PUBLIC) != 0) ecPublicKey.setFieldFP(data, offset, length); break; - case PARAMETER_F2M_ONE: - if (length != 2) { - result = ISO7816.SW_UNKNOWN; - } else { + case EC_Consts.PARAMETER_F2M: + if (length == 2) { short i = Util.makeShort(data[offset], data[(short) (offset + 1)]); if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldF2M(i); if ((key & KEY_PUBLIC) != 0) ecPublicKey.setFieldF2M(i); - } - break; - case PARAMETER_F2M_THREE: - if (length != 6) { - result = ISO7816.SW_UNKNOWN; - } else { + } else if (length == 6) { short i1 = Util.makeShort(data[offset], data[(short) (offset + 1)]); short i2 = Util.makeShort(data[(short) (offset + 2)], data[(short) (offset + 3)]); short i3 = Util.makeShort(data[(short) (offset + 4)], data[(short) (offset + 5)]); if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldF2M(i1, i2, i3); if ((key & KEY_PUBLIC) != 0) ecPublicKey.setFieldF2M(i1, i2, i3); + } else { + result = ISO7816.SW_UNKNOWN; } break; - case PARAMETER_A: + case EC_Consts.PARAMETER_A: if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setA(data, offset, length); if ((key & KEY_PUBLIC) != 0) ecPublicKey.setA(data, offset, length); break; - case PARAMETER_B: + case EC_Consts.PARAMETER_B: if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setB(data, offset, length); if ((key & KEY_PUBLIC) != 0) ecPublicKey.setB(data, offset, length); break; - case PARAMETER_G: + case EC_Consts.PARAMETER_G: if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setG(data, offset, length); if ((key & KEY_PUBLIC) != 0) ecPublicKey.setG(data, offset, length); break; - case PARAMETER_R: + case EC_Consts.PARAMETER_R: if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setR(data, offset, length); if ((key & KEY_PUBLIC) != 0) ecPublicKey.setR(data, offset, length); break; - case PARAMETER_K: + case EC_Consts.PARAMETER_K: if (length != 2) { result = ISO7816.SW_UNKNOWN; } else { @@ -125,10 +139,10 @@ public class ECKeyGenerator { if ((key & KEY_PUBLIC) != 0) ecPublicKey.setK(k); } break; - case PARAMETER_S: + case EC_Consts.PARAMETER_S: if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setS(data, offset, length); break; - case PARAMETER_W: + case EC_Consts.PARAMETER_W: if ((key & KEY_PUBLIC) != 0) ecPublicKey.setW(data, offset, length); break; default: @@ -142,27 +156,54 @@ public class ECKeyGenerator { return result; } - public short exportParameter(byte key, byte param, byte[] outputBuffer, short outputOffset) { + public short exportParameter(byte key, short param, byte[] outputBuffer, short outputOffset) { if (key == KEY_BOTH) { - return ISO7816.SW_UNKNOWN; - } - short result = ISO7816.SW_NO_ERROR; + return -1; + }//TODO: change error handling. + short length = 0; try { - switch(param){ - case PARAMETER_FP: - + switch (param) { + case EC_Consts.PARAMETER_FP: + case EC_Consts.PARAMETER_F2M: + if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getField(outputBuffer, outputOffset); + if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getField(outputBuffer, outputOffset); + break; + case EC_Consts.PARAMETER_A: + if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getA(outputBuffer, outputOffset); + if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getA(outputBuffer, outputOffset); + break; + case EC_Consts.PARAMETER_B: + if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getB(outputBuffer, outputOffset); + if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getB(outputBuffer, outputOffset); + break; + case EC_Consts.PARAMETER_G: + if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getG(outputBuffer, outputOffset); + if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getG(outputBuffer, outputOffset); + break; + case EC_Consts.PARAMETER_R: + if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getR(outputBuffer, outputOffset); + if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getR(outputBuffer, outputOffset); + break; + case EC_Consts.PARAMETER_K: + if ((key & KEY_PUBLIC) != 0) Util.setShort(outputBuffer, outputOffset, ecPublicKey.getK()); + if ((key & KEY_PRIVATE) != 0) Util.setShort(outputBuffer, outputOffset, ecPrivateKey.getK()); + length = 2; + break; + case EC_Consts.PARAMETER_S: + if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getS(outputBuffer, outputOffset); + break; + case EC_Consts.PARAMETER_W: + if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getW(outputBuffer, outputOffset); break; - default: - + length = -1; } } catch (CryptoException ce) { - + length = -1; } catch (Exception e) { - + length = -1; } - //TODO - return result; + return length; } public ECPrivateKey getPrivateKey() { diff --git a/src/applets/ECKeyTester.java b/src/applets/ECKeyTester.java index 91d7a8b..757ece7 100644 --- a/src/applets/ECKeyTester.java +++ b/src/applets/ECKeyTester.java @@ -13,6 +13,7 @@ public class ECKeyTester { private KeyAgreement ecdhcKeyAgreement = null; private Signature ecdsaSignature = null; + //TODO: move these SW definitions to the main applet class. public final static short SW_SIG_LENGTH_MISMATCH = (short) 0xee4; public final static short SW_SIG_VERIFY_FAIL = (short) 0xee5; diff --git a/src/applets/EC_Consts.java b/src/applets/EC_Consts.java index 3758f0b..7521979 100644 --- a/src/applets/EC_Consts.java +++ b/src/applets/EC_Consts.java @@ -3,642 +3,812 @@ package applets; import javacard.framework.ISO7816; import javacard.framework.ISOException; import javacard.framework.Util; -import javacard.security.ECPrivateKey; -import javacard.security.ECPublicKey; import javacard.security.KeyPair; import javacard.security.RandomData; public class EC_Consts { - public static byte[] EC_FP_P = null; - public static byte[] EC_FP_A = null; - public static byte[] EC_FP_B = null; - public static byte[] EC_FP_G_X = null; - public static byte[] EC_FP_G_Y = null; - public static byte[] EC_FP_R = null; - public static short EC_FP_K = 1; - - public static RandomData m_random = null; + private static byte[] EC_FP_P = null; //p + private static byte[] EC_A = null; //a + private static byte[] EC_B = null; //b + private static byte[] EC_G_X = null; //G[x,y] + private static byte[] EC_G_Y = null; // + private static byte[] EC_R = null; //n + private static short EC_K = 1; //h + + private static byte[] EC_F2M_F2M = null; //[short ii, short i2, short i3], f = x^m + x^i1 + x^i2 + x^i3 + 1 + + public static final byte PARAMETER_FP = 1; + public static final byte PARAMETER_F2M = 2; + + public static final byte PARAMETER_A = 3; + public static final byte PARAMETER_B = 4; + public static final byte PARAMETER_G = 5; + public static final byte PARAMETER_R = 6; + public static final byte PARAMETER_K = 7; + + //TODO make params maskable, to allow for PARAMETER_A | PARAMETER_B passed to for example ECKeyGenerator.setInvalidCustomCurve + public static final byte PARAMETER_S = 8; //private key + public static final byte PARAMETER_W = 9; //public key + + + public static RandomData m_random = null; // secp128r1 public static final byte[] EC128_FP_P = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFD, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; - + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFD, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; + public static final byte[] EC128_FP_A = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFD, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; - + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFD, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; + public static final byte[] EC128_FP_B = new byte[]{ - (byte) 0xE8, (byte) 0x75, (byte) 0x79, (byte) 0xC1, - (byte) 0x10, (byte) 0x79, (byte) 0xF4, (byte) 0x3D, - (byte) 0xD8, (byte) 0x24, (byte) 0x99, (byte) 0x3C, - (byte) 0x2C, (byte) 0xEE, (byte) 0x5E, (byte) 0xD3}; - + (byte) 0xE8, (byte) 0x75, (byte) 0x79, (byte) 0xC1, + (byte) 0x10, (byte) 0x79, (byte) 0xF4, (byte) 0x3D, + (byte) 0xD8, (byte) 0x24, (byte) 0x99, (byte) 0x3C, + (byte) 0x2C, (byte) 0xEE, (byte) 0x5E, (byte) 0xD3}; + // G in compressed form / first part of ucompressed public static final byte[] EC128_FP_G_X = new byte[]{ - (byte) 0x16, (byte) 0x1F, (byte) 0xF7, (byte) 0x52, - (byte) 0x8B, (byte) 0x89, (byte) 0x9B, (byte) 0x2D, - (byte) 0x0C, (byte) 0x28, (byte) 0x60, (byte) 0x7C, - (byte) 0xA5, (byte) 0x2C, (byte) 0x5B, (byte) 0x86 }; - + (byte) 0x16, (byte) 0x1F, (byte) 0xF7, (byte) 0x52, + (byte) 0x8B, (byte) 0x89, (byte) 0x9B, (byte) 0x2D, + (byte) 0x0C, (byte) 0x28, (byte) 0x60, (byte) 0x7C, + (byte) 0xA5, (byte) 0x2C, (byte) 0x5B, (byte) 0x86}; + // second part of G uncompressed public static final byte[] EC128_FP_G_Y = new byte[]{ - (byte) 0xCF, (byte) 0x5A, (byte) 0xC8, (byte) 0x39, - (byte) 0x5B, (byte) 0xAF, (byte) 0xEB, (byte) 0x13, - (byte) 0xC0, (byte) 0x2D, (byte) 0xA2, (byte) 0x92, - (byte) 0xDD, (byte) 0xED, (byte) 0x7A, (byte) 0x83}; + (byte) 0xCF, (byte) 0x5A, (byte) 0xC8, (byte) 0x39, + (byte) 0x5B, (byte) 0xAF, (byte) 0xEB, (byte) 0x13, + (byte) 0xC0, (byte) 0x2D, (byte) 0xA2, (byte) 0x92, + (byte) 0xDD, (byte) 0xED, (byte) 0x7A, (byte) 0x83}; // Order of G public static final byte[] EC128_FP_R = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x75, (byte) 0xA3, (byte) 0x0D, (byte) 0x1B, - (byte) 0x90, (byte) 0x38, (byte) 0xA1, (byte) 0x15}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x75, (byte) 0xA3, (byte) 0x0D, (byte) 0x1B, + (byte) 0x90, (byte) 0x38, (byte) 0xA1, (byte) 0x15}; // cofactor of G public static final short EC128_FP_K = 1; - + // secp160r1 public static final byte[] EC160_FP_P = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; public static final byte[] EC160_FP_A = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; public static final byte[] EC160_FP_B = new byte[]{ - (byte) 0x1C, (byte) 0x97, (byte) 0xBE, (byte) 0xFC, - (byte) 0x54, (byte) 0xBD, (byte) 0x7A, (byte) 0x8B, - (byte) 0x65, (byte) 0xAC, (byte) 0xF8, (byte) 0x9F, - (byte) 0x81, (byte) 0xD4, (byte) 0xD4, (byte) 0xAD, - (byte) 0xC5, (byte) 0x65, (byte) 0xFA, (byte) 0x45}; + (byte) 0x1C, (byte) 0x97, (byte) 0xBE, (byte) 0xFC, + (byte) 0x54, (byte) 0xBD, (byte) 0x7A, (byte) 0x8B, + (byte) 0x65, (byte) 0xAC, (byte) 0xF8, (byte) 0x9F, + (byte) 0x81, (byte) 0xD4, (byte) 0xD4, (byte) 0xAD, + (byte) 0xC5, (byte) 0x65, (byte) 0xFA, (byte) 0x45}; // G in compressed form / first part of ucompressed public static final byte[] EC160_FP_G_X = new byte[]{ - (byte) 0x4A, (byte) 0x96, (byte) 0xB5, (byte) 0x68, - (byte) 0x8E, (byte) 0xF5, (byte) 0x73, (byte) 0x28, - (byte) 0x46, (byte) 0x64, (byte) 0x69, (byte) 0x89, - (byte) 0x68, (byte) 0xC3, (byte) 0x8B, (byte) 0xB9, - (byte) 0x13, (byte) 0xCB, (byte) 0xFC, (byte) 0x82}; + (byte) 0x4A, (byte) 0x96, (byte) 0xB5, (byte) 0x68, + (byte) 0x8E, (byte) 0xF5, (byte) 0x73, (byte) 0x28, + (byte) 0x46, (byte) 0x64, (byte) 0x69, (byte) 0x89, + (byte) 0x68, (byte) 0xC3, (byte) 0x8B, (byte) 0xB9, + (byte) 0x13, (byte) 0xCB, (byte) 0xFC, (byte) 0x82}; // second part of G uncompressed public static final byte[] EC160_FP_G_Y = new byte[]{ - (byte) 0x23, (byte) 0xA6, (byte) 0x28, (byte) 0x55, - (byte) 0x31, (byte) 0x68, (byte) 0x94, (byte) 0x7D, - (byte) 0x59, (byte) 0xDC, (byte) 0xC9, (byte) 0x12, - (byte) 0x04, (byte) 0x23, (byte) 0x51, (byte) 0x37, - (byte) 0x7A, (byte) 0xC5, (byte) 0xFB, (byte) 0x32}; + (byte) 0x23, (byte) 0xA6, (byte) 0x28, (byte) 0x55, + (byte) 0x31, (byte) 0x68, (byte) 0x94, (byte) 0x7D, + (byte) 0x59, (byte) 0xDC, (byte) 0xC9, (byte) 0x12, + (byte) 0x04, (byte) 0x23, (byte) 0x51, (byte) 0x37, + (byte) 0x7A, (byte) 0xC5, (byte) 0xFB, (byte) 0x32}; // Order of G public static final byte[] EC160_FP_R = new byte[]{ - (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x01, (byte) 0xF4, (byte) 0xC8, - (byte) 0xF9, (byte) 0x27, (byte) 0xAE, (byte) 0xD3, - (byte) 0xCA, (byte) 0x75, (byte) 0x22, (byte) 0x57}; + (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x01, (byte) 0xF4, (byte) 0xC8, + (byte) 0xF9, (byte) 0x27, (byte) 0xAE, (byte) 0xD3, + (byte) 0xCA, (byte) 0x75, (byte) 0x22, (byte) 0x57}; // cofactor of G - public static final short EC160_FP_K = 1; - - + public static final short EC160_FP_K = 1; + + // secp192r1 from http://www.secg.org/sec2-v2.pdf public static final byte[] EC192_FP_P = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; public static final byte[] EC192_FP_A = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; public static final byte[] EC192_FP_B = new byte[]{ - (byte) 0x64, (byte) 0x21, (byte) 0x05, (byte) 0x19, - (byte) 0xE5, (byte) 0x9C, (byte) 0x80, (byte) 0xE7, - (byte) 0x0F, (byte) 0xA7, (byte) 0xE9, (byte) 0xAB, - (byte) 0x72, (byte) 0x24, (byte) 0x30, (byte) 0x49, - (byte) 0xFE, (byte) 0xB8, (byte) 0xDE, (byte) 0xEC, - (byte) 0xC1, (byte) 0x46, (byte) 0xB9, (byte) 0xB1}; + (byte) 0x64, (byte) 0x21, (byte) 0x05, (byte) 0x19, + (byte) 0xE5, (byte) 0x9C, (byte) 0x80, (byte) 0xE7, + (byte) 0x0F, (byte) 0xA7, (byte) 0xE9, (byte) 0xAB, + (byte) 0x72, (byte) 0x24, (byte) 0x30, (byte) 0x49, + (byte) 0xFE, (byte) 0xB8, (byte) 0xDE, (byte) 0xEC, + (byte) 0xC1, (byte) 0x46, (byte) 0xB9, (byte) 0xB1}; // G in compressed form / first part of ucompressed public static final byte[] EC192_FP_G_X = new byte[]{ - (byte) 0x18, (byte) 0x8D, (byte) 0xA8, (byte) 0x0E, - (byte) 0xB0, (byte) 0x30, (byte) 0x90, (byte) 0xF6, - (byte) 0x7C, (byte) 0xBF, (byte) 0x20, (byte) 0xEB, - (byte) 0x43, (byte) 0xA1, (byte) 0x88, (byte) 0x00, - (byte) 0xF4, (byte) 0xFF, (byte) 0x0A, (byte) 0xFD, - (byte) 0x82, (byte) 0xFF, (byte) 0x10, (byte) 0x12}; + (byte) 0x18, (byte) 0x8D, (byte) 0xA8, (byte) 0x0E, + (byte) 0xB0, (byte) 0x30, (byte) 0x90, (byte) 0xF6, + (byte) 0x7C, (byte) 0xBF, (byte) 0x20, (byte) 0xEB, + (byte) 0x43, (byte) 0xA1, (byte) 0x88, (byte) 0x00, + (byte) 0xF4, (byte) 0xFF, (byte) 0x0A, (byte) 0xFD, + (byte) 0x82, (byte) 0xFF, (byte) 0x10, (byte) 0x12}; // second part of G uncompressed - public static final byte[] EC192_FP_G_Y = new byte[]{ - (byte) 0x07, (byte) 0x19, (byte) 0x2B, (byte) 0x95, - (byte) 0xFF, (byte) 0xC8, (byte) 0xDA, (byte) 0x78, - (byte) 0x63, (byte) 0x10, (byte) 0x11, (byte) 0xED, - (byte) 0x6B, (byte) 0x24, (byte) 0xCD, (byte) 0xD5, - (byte) 0x73, (byte) 0xF9, (byte) 0x77, (byte) 0xA1, - (byte) 0x1E, (byte) 0x79, (byte) 0x48, (byte) 0x11}; + public static final byte[] EC192_FP_G_Y = new byte[]{ + (byte) 0x07, (byte) 0x19, (byte) 0x2B, (byte) 0x95, + (byte) 0xFF, (byte) 0xC8, (byte) 0xDA, (byte) 0x78, + (byte) 0x63, (byte) 0x10, (byte) 0x11, (byte) 0xED, + (byte) 0x6B, (byte) 0x24, (byte) 0xCD, (byte) 0xD5, + (byte) 0x73, (byte) 0xF9, (byte) 0x77, (byte) 0xA1, + (byte) 0x1E, (byte) 0x79, (byte) 0x48, (byte) 0x11}; // Order of G public static final byte[] EC192_FP_R = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x99, (byte) 0xDE, (byte) 0xF8, (byte) 0x36, - (byte) 0x14, (byte) 0x6B, (byte) 0xC9, (byte) 0xB1, - (byte) 0xB4, (byte) 0xD2, (byte) 0x28, (byte) 0x31}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0x99, (byte) 0xDE, (byte) 0xF8, (byte) 0x36, + (byte) 0x14, (byte) 0x6B, (byte) 0xC9, (byte) 0xB1, + (byte) 0xB4, (byte) 0xD2, (byte) 0x28, (byte) 0x31}; // cofactor of G - public static final short EC192_FP_K = 1; - + public static final short EC192_FP_K = 1; + // secp224r1 from http://www.secg.org/sec2-v2.pdf public static final byte[] EC224_FP_P = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01}; - + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01}; + public static final byte[] EC224_FP_A = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE}; - + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE}; + public static final byte[] EC224_FP_B = new byte[]{ - (byte) 0xB4, (byte) 0x05, (byte) 0x0A, (byte) 0x85, - (byte) 0x0C, (byte) 0x04, (byte) 0xB3, (byte) 0xAB, - (byte) 0xF5, (byte) 0x41, (byte) 0x32, (byte) 0x56, - (byte) 0x50, (byte) 0x44, (byte) 0xB0, (byte) 0xB7, - (byte) 0xD7, (byte) 0xBF, (byte) 0xD8, (byte) 0xBA, - (byte) 0x27, (byte) 0x0B, (byte) 0x39, (byte) 0x43, - (byte) 0x23, (byte) 0x55, (byte) 0xFF, (byte) 0xB4}; - + (byte) 0xB4, (byte) 0x05, (byte) 0x0A, (byte) 0x85, + (byte) 0x0C, (byte) 0x04, (byte) 0xB3, (byte) 0xAB, + (byte) 0xF5, (byte) 0x41, (byte) 0x32, (byte) 0x56, + (byte) 0x50, (byte) 0x44, (byte) 0xB0, (byte) 0xB7, + (byte) 0xD7, (byte) 0xBF, (byte) 0xD8, (byte) 0xBA, + (byte) 0x27, (byte) 0x0B, (byte) 0x39, (byte) 0x43, + (byte) 0x23, (byte) 0x55, (byte) 0xFF, (byte) 0xB4}; + // G in compressed form / first part of ucompressed public static final byte[] EC224_FP_G_X = new byte[]{ - (byte) 0xB7, (byte) 0x0E, (byte) 0x0C, (byte) 0xBD, - (byte) 0x6B, (byte) 0xB4, (byte) 0xBF, (byte) 0x7F, - (byte) 0x32, (byte) 0x13, (byte) 0x90, (byte) 0xB9, - (byte) 0x4A, (byte) 0x03, (byte) 0xC1, (byte) 0xD3, - (byte) 0x56, (byte) 0xC2, (byte) 0x11, (byte) 0x22, - (byte) 0x34, (byte) 0x32, (byte) 0x80, (byte) 0xD6, - (byte) 0x11, (byte) 0x5C, (byte) 0x1D, (byte) 0x21}; + (byte) 0xB7, (byte) 0x0E, (byte) 0x0C, (byte) 0xBD, + (byte) 0x6B, (byte) 0xB4, (byte) 0xBF, (byte) 0x7F, + (byte) 0x32, (byte) 0x13, (byte) 0x90, (byte) 0xB9, + (byte) 0x4A, (byte) 0x03, (byte) 0xC1, (byte) 0xD3, + (byte) 0x56, (byte) 0xC2, (byte) 0x11, (byte) 0x22, + (byte) 0x34, (byte) 0x32, (byte) 0x80, (byte) 0xD6, + (byte) 0x11, (byte) 0x5C, (byte) 0x1D, (byte) 0x21}; // second part of G uncompressed public static final byte[] EC224_FP_G_Y = new byte[]{ - (byte) 0xBD, (byte) 0x37, (byte) 0x63, (byte) 0x88, - (byte) 0xB5, (byte) 0xF7, (byte) 0x23, (byte) 0xFB, - (byte) 0x4C, (byte) 0x22, (byte) 0xDF, (byte) 0xE6, - (byte) 0xCD, (byte) 0x43, (byte) 0x75, (byte) 0xA0, - (byte) 0x5A, (byte) 0x07, (byte) 0x47, (byte) 0x64, - (byte) 0x44, (byte) 0xD5, (byte) 0x81, (byte) 0x99, - (byte) 0x85, (byte) 0x00, (byte) 0x7E, (byte) 0x34}; + (byte) 0xBD, (byte) 0x37, (byte) 0x63, (byte) 0x88, + (byte) 0xB5, (byte) 0xF7, (byte) 0x23, (byte) 0xFB, + (byte) 0x4C, (byte) 0x22, (byte) 0xDF, (byte) 0xE6, + (byte) 0xCD, (byte) 0x43, (byte) 0x75, (byte) 0xA0, + (byte) 0x5A, (byte) 0x07, (byte) 0x47, (byte) 0x64, + (byte) 0x44, (byte) 0xD5, (byte) 0x81, (byte) 0x99, + (byte) 0x85, (byte) 0x00, (byte) 0x7E, (byte) 0x34}; // Order of G public static final byte[] EC224_FP_R = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0xA2, - (byte) 0xE0, (byte) 0xB8, (byte) 0xF0, (byte) 0x3E, - (byte) 0x13, (byte) 0xDD, (byte) 0x29, (byte) 0x45, - (byte) 0x5C, (byte) 0x5C, (byte) 0x2A, (byte) 0x3D}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0xA2, + (byte) 0xE0, (byte) 0xB8, (byte) 0xF0, (byte) 0x3E, + (byte) 0x13, (byte) 0xDD, (byte) 0x29, (byte) 0x45, + (byte) 0x5C, (byte) 0x5C, (byte) 0x2A, (byte) 0x3D}; // cofactor of G - public static final short EC224_FP_K = 1; - + public static final short EC224_FP_K = 1; + // secp256r1 from http://www.secg.org/sec2-v2.pdf public static final byte[] EC256_FP_P = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; public static final byte[] EC256_FP_A = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; public static final byte[] EC256_FP_B = new byte[]{ - (byte) 0x5A, (byte) 0xC6, (byte) 0x35, (byte) 0xD8, - (byte) 0xAA, (byte) 0x3A, (byte) 0x93, (byte) 0xE7, - (byte) 0xB3, (byte) 0xEB, (byte) 0xBD, (byte) 0x55, - (byte) 0x76, (byte) 0x98, (byte) 0x86, (byte) 0xBC, - (byte) 0x65, (byte) 0x1D, (byte) 0x06, (byte) 0xB0, - (byte) 0xCC, (byte) 0x53, (byte) 0xB0, (byte) 0xF6, - (byte) 0x3B, (byte) 0xCE, (byte) 0x3C, (byte) 0x3E, - (byte) 0x27, (byte) 0xD2, (byte) 0x60, (byte) 0x4B}; + (byte) 0x5A, (byte) 0xC6, (byte) 0x35, (byte) 0xD8, + (byte) 0xAA, (byte) 0x3A, (byte) 0x93, (byte) 0xE7, + (byte) 0xB3, (byte) 0xEB, (byte) 0xBD, (byte) 0x55, + (byte) 0x76, (byte) 0x98, (byte) 0x86, (byte) 0xBC, + (byte) 0x65, (byte) 0x1D, (byte) 0x06, (byte) 0xB0, + (byte) 0xCC, (byte) 0x53, (byte) 0xB0, (byte) 0xF6, + (byte) 0x3B, (byte) 0xCE, (byte) 0x3C, (byte) 0x3E, + (byte) 0x27, (byte) 0xD2, (byte) 0x60, (byte) 0x4B}; // G in compressed form / first part of ucompressed public static final byte[] EC256_FP_G_X = new byte[]{ - (byte) 0x6B, (byte) 0x17, (byte) 0xD1, (byte) 0xF2, - (byte) 0xE1, (byte) 0x2C, (byte) 0x42, (byte) 0x47, - (byte) 0xF8, (byte) 0xBC, (byte) 0xE6, (byte) 0xE5, - (byte) 0x63, (byte) 0xA4, (byte) 0x40, (byte) 0xF2, - (byte) 0x77, (byte) 0x03, (byte) 0x7D, (byte) 0x81, - (byte) 0x2D, (byte) 0xEB, (byte) 0x33, (byte) 0xA0, - (byte) 0xF4, (byte) 0xA1, (byte) 0x39, (byte) 0x45, - (byte) 0xD8, (byte) 0x98, (byte) 0xC2, (byte) 0x96}; + (byte) 0x6B, (byte) 0x17, (byte) 0xD1, (byte) 0xF2, + (byte) 0xE1, (byte) 0x2C, (byte) 0x42, (byte) 0x47, + (byte) 0xF8, (byte) 0xBC, (byte) 0xE6, (byte) 0xE5, + (byte) 0x63, (byte) 0xA4, (byte) 0x40, (byte) 0xF2, + (byte) 0x77, (byte) 0x03, (byte) 0x7D, (byte) 0x81, + (byte) 0x2D, (byte) 0xEB, (byte) 0x33, (byte) 0xA0, + (byte) 0xF4, (byte) 0xA1, (byte) 0x39, (byte) 0x45, + (byte) 0xD8, (byte) 0x98, (byte) 0xC2, (byte) 0x96}; // second part of G uncompressed public static final byte[] EC256_FP_G_Y = new byte[]{ - (byte) 0x4F, (byte) 0xE3, (byte) 0x42, (byte) 0xE2, - (byte) 0xFE, (byte) 0x1A, (byte) 0x7F, (byte) 0x9B, - (byte) 0x8E, (byte) 0xE7, (byte) 0xEB, (byte) 0x4A, - (byte) 0x7C, (byte) 0x0F, (byte) 0x9E, (byte) 0x16, - (byte) 0x2B, (byte) 0xCE, (byte) 0x33, (byte) 0x57, - (byte) 0x6B, (byte) 0x31, (byte) 0x5E, (byte) 0xCE, - (byte) 0xCB, (byte) 0xB6, (byte) 0x40, (byte) 0x68, - (byte) 0x37, (byte) 0xBF, (byte) 0x51, (byte) 0xF5}; + (byte) 0x4F, (byte) 0xE3, (byte) 0x42, (byte) 0xE2, + (byte) 0xFE, (byte) 0x1A, (byte) 0x7F, (byte) 0x9B, + (byte) 0x8E, (byte) 0xE7, (byte) 0xEB, (byte) 0x4A, + (byte) 0x7C, (byte) 0x0F, (byte) 0x9E, (byte) 0x16, + (byte) 0x2B, (byte) 0xCE, (byte) 0x33, (byte) 0x57, + (byte) 0x6B, (byte) 0x31, (byte) 0x5E, (byte) 0xCE, + (byte) 0xCB, (byte) 0xB6, (byte) 0x40, (byte) 0x68, + (byte) 0x37, (byte) 0xBF, (byte) 0x51, (byte) 0xF5}; // Order of G public static final byte[] EC256_FP_R = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xBC, (byte) 0xE6, (byte) 0xFA, (byte) 0xAD, - (byte) 0xA7, (byte) 0x17, (byte) 0x9E, (byte) 0x84, - (byte) 0xF3, (byte) 0xB9, (byte) 0xCA, (byte) 0xC2, - (byte) 0xFC, (byte) 0x63, (byte) 0x25, (byte) 0x51}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xBC, (byte) 0xE6, (byte) 0xFA, (byte) 0xAD, + (byte) 0xA7, (byte) 0x17, (byte) 0x9E, (byte) 0x84, + (byte) 0xF3, (byte) 0xB9, (byte) 0xCA, (byte) 0xC2, + (byte) 0xFC, (byte) 0x63, (byte) 0x25, (byte) 0x51}; // cofactor of G - public static final short EC256_FP_K = 1; - + public static final short EC256_FP_K = 1; + // secp384r1 from http://www.secg.org/sec2-v2.pdf public static final byte[] EC384_FP_P = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; - + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; + public static final byte[] EC384_FP_A = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; - + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; + public static final byte[] EC384_FP_B = new byte[]{ - (byte) 0xB3, (byte) 0x31, (byte) 0x2F, (byte) 0xA7, - (byte) 0xE2, (byte) 0x3E, (byte) 0xE7, (byte) 0xE4, - (byte) 0x98, (byte) 0x8E, (byte) 0x05, (byte) 0x6B, - (byte) 0xE3, (byte) 0xF8, (byte) 0x2D, (byte) 0x19, - (byte) 0x18, (byte) 0x1D, (byte) 0x9C, (byte) 0x6E, - (byte) 0xFE, (byte) 0x81, (byte) 0x41, (byte) 0x12, - (byte) 0x03, (byte) 0x14, (byte) 0x08, (byte) 0x8F, - (byte) 0x50, (byte) 0x13, (byte) 0x87, (byte) 0x5A, - (byte) 0xC6, (byte) 0x56, (byte) 0x39, (byte) 0x8D, - (byte) 0x8A, (byte) 0x2E, (byte) 0xD1, (byte) 0x9D, - (byte) 0x2A, (byte) 0x85, (byte) 0xC8, (byte) 0xED, - (byte) 0xD3, (byte) 0xEC, (byte) 0x2A, (byte) 0xEF}; - + (byte) 0xB3, (byte) 0x31, (byte) 0x2F, (byte) 0xA7, + (byte) 0xE2, (byte) 0x3E, (byte) 0xE7, (byte) 0xE4, + (byte) 0x98, (byte) 0x8E, (byte) 0x05, (byte) 0x6B, + (byte) 0xE3, (byte) 0xF8, (byte) 0x2D, (byte) 0x19, + (byte) 0x18, (byte) 0x1D, (byte) 0x9C, (byte) 0x6E, + (byte) 0xFE, (byte) 0x81, (byte) 0x41, (byte) 0x12, + (byte) 0x03, (byte) 0x14, (byte) 0x08, (byte) 0x8F, + (byte) 0x50, (byte) 0x13, (byte) 0x87, (byte) 0x5A, + (byte) 0xC6, (byte) 0x56, (byte) 0x39, (byte) 0x8D, + (byte) 0x8A, (byte) 0x2E, (byte) 0xD1, (byte) 0x9D, + (byte) 0x2A, (byte) 0x85, (byte) 0xC8, (byte) 0xED, + (byte) 0xD3, (byte) 0xEC, (byte) 0x2A, (byte) 0xEF}; + // G in compressed form / first part of ucompressed public static final byte[] EC384_FP_G_X = new byte[]{ - (byte) 0xAA, (byte) 0x87, (byte) 0xCA, (byte) 0x22, - (byte) 0xBE, (byte) 0x8B, (byte) 0x05, (byte) 0x37, - (byte) 0x8E, (byte) 0xB1, (byte) 0xC7, (byte) 0x1E, - (byte) 0xF3, (byte) 0x20, (byte) 0xAD, (byte) 0x74, - (byte) 0x6E, (byte) 0x1D, (byte) 0x3B, (byte) 0x62, - (byte) 0x8B, (byte) 0xA7, (byte) 0x9B, (byte) 0x98, - (byte) 0x59, (byte) 0xF7, (byte) 0x41, (byte) 0xE0, - (byte) 0x82, (byte) 0x54, (byte) 0x2A, (byte) 0x38, - (byte) 0x55, (byte) 0x02, (byte) 0xF2, (byte) 0x5D, - (byte) 0xBF, (byte) 0x55, (byte) 0x29, (byte) 0x6C, - (byte) 0x3A, (byte) 0x54, (byte) 0x5E, (byte) 0x38, - (byte) 0x72, (byte) 0x76, (byte) 0x0A, (byte) 0xB7}; + (byte) 0xAA, (byte) 0x87, (byte) 0xCA, (byte) 0x22, + (byte) 0xBE, (byte) 0x8B, (byte) 0x05, (byte) 0x37, + (byte) 0x8E, (byte) 0xB1, (byte) 0xC7, (byte) 0x1E, + (byte) 0xF3, (byte) 0x20, (byte) 0xAD, (byte) 0x74, + (byte) 0x6E, (byte) 0x1D, (byte) 0x3B, (byte) 0x62, + (byte) 0x8B, (byte) 0xA7, (byte) 0x9B, (byte) 0x98, + (byte) 0x59, (byte) 0xF7, (byte) 0x41, (byte) 0xE0, + (byte) 0x82, (byte) 0x54, (byte) 0x2A, (byte) 0x38, + (byte) 0x55, (byte) 0x02, (byte) 0xF2, (byte) 0x5D, + (byte) 0xBF, (byte) 0x55, (byte) 0x29, (byte) 0x6C, + (byte) 0x3A, (byte) 0x54, (byte) 0x5E, (byte) 0x38, + (byte) 0x72, (byte) 0x76, (byte) 0x0A, (byte) 0xB7}; // second part of G uncompressed public static final byte[] EC384_FP_G_Y = new byte[]{ - (byte) 0x36, (byte) 0x17, (byte) 0xDE, (byte) 0x4A, - (byte) 0x96, (byte) 0x26, (byte) 0x2C, (byte) 0x6F, - (byte) 0x5D, (byte) 0x9E, (byte) 0x98, (byte) 0xBF, - (byte) 0x92, (byte) 0x92, (byte) 0xDC, (byte) 0x29, - (byte) 0xF8, (byte) 0xF4, (byte) 0x1D, (byte) 0xBD, - (byte) 0x28, (byte) 0x9A, (byte) 0x14, (byte) 0x7C, - (byte) 0xE9, (byte) 0xDA, (byte) 0x31, (byte) 0x13, - (byte) 0xB5, (byte) 0xF0, (byte) 0xB8, (byte) 0xC0, - (byte) 0x0A, (byte) 0x60, (byte) 0xB1, (byte) 0xCE, - (byte) 0x1D, (byte) 0x7E, (byte) 0x81, (byte) 0x9D, - (byte) 0x7A, (byte) 0x43, (byte) 0x1D, (byte) 0x7C, - (byte) 0x90, (byte) 0xEA, (byte) 0x0E, (byte) 0x5F}; - + (byte) 0x36, (byte) 0x17, (byte) 0xDE, (byte) 0x4A, + (byte) 0x96, (byte) 0x26, (byte) 0x2C, (byte) 0x6F, + (byte) 0x5D, (byte) 0x9E, (byte) 0x98, (byte) 0xBF, + (byte) 0x92, (byte) 0x92, (byte) 0xDC, (byte) 0x29, + (byte) 0xF8, (byte) 0xF4, (byte) 0x1D, (byte) 0xBD, + (byte) 0x28, (byte) 0x9A, (byte) 0x14, (byte) 0x7C, + (byte) 0xE9, (byte) 0xDA, (byte) 0x31, (byte) 0x13, + (byte) 0xB5, (byte) 0xF0, (byte) 0xB8, (byte) 0xC0, + (byte) 0x0A, (byte) 0x60, (byte) 0xB1, (byte) 0xCE, + (byte) 0x1D, (byte) 0x7E, (byte) 0x81, (byte) 0x9D, + (byte) 0x7A, (byte) 0x43, (byte) 0x1D, (byte) 0x7C, + (byte) 0x90, (byte) 0xEA, (byte) 0x0E, (byte) 0x5F}; + // Order of G public static final byte[] EC384_FP_R = new byte[]{ - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xC7, (byte) 0x63, (byte) 0x4D, (byte) 0x81, - (byte) 0xF4, (byte) 0x37, (byte) 0x2D, (byte) 0xDF, - (byte) 0x58, (byte) 0x1A, (byte) 0x0D, (byte) 0xB2, - (byte) 0x48, (byte) 0xB0, (byte) 0xA7, (byte) 0x7A, - (byte) 0xEC, (byte) 0xEC, (byte) 0x19, (byte) 0x6A, - (byte) 0xCC, (byte) 0xC5, (byte) 0x29, (byte) 0x73}; + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xC7, (byte) 0x63, (byte) 0x4D, (byte) 0x81, + (byte) 0xF4, (byte) 0x37, (byte) 0x2D, (byte) 0xDF, + (byte) 0x58, (byte) 0x1A, (byte) 0x0D, (byte) 0xB2, + (byte) 0x48, (byte) 0xB0, (byte) 0xA7, (byte) 0x7A, + (byte) 0xEC, (byte) 0xEC, (byte) 0x19, (byte) 0x6A, + (byte) 0xCC, (byte) 0xC5, (byte) 0x29, (byte) 0x73}; // cofactor of G - public static final short EC384_FP_K = 1; - - + public static final short EC384_FP_K = 1; + + // secp521r1 from http://www.secg.org/sec2-v2.pdf public static final byte[] EC521_FP_P = new byte[]{ - (byte) 0x01, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; + (byte) 0x01, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; public static final byte[] EC521_FP_A = new byte[]{ - (byte) 0x01, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; + (byte) 0x01, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC}; public static final byte[] EC521_FP_B = new byte[]{ - (byte) 0x00, (byte) 0x51, (byte) 0x95, (byte) 0x3E, - (byte) 0xB9, (byte) 0x61, (byte) 0x8E, (byte) 0x1C, - (byte) 0x9A, (byte) 0x1F, (byte) 0x92, (byte) 0x9A, - (byte) 0x21, (byte) 0xA0, (byte) 0xB6, (byte) 0x85, - (byte) 0x40, (byte) 0xEE, (byte) 0xA2, (byte) 0xDA, - (byte) 0x72, (byte) 0x5B, (byte) 0x99, (byte) 0xB3, - (byte) 0x15, (byte) 0xF3, (byte) 0xB8, (byte) 0xB4, - (byte) 0x89, (byte) 0x91, (byte) 0x8E, (byte) 0xF1, - (byte) 0x09, (byte) 0xE1, (byte) 0x56, (byte) 0x19, - (byte) 0x39, (byte) 0x51, (byte) 0xEC, (byte) 0x7E, - (byte) 0x93, (byte) 0x7B, (byte) 0x16, (byte) 0x52, - (byte) 0xC0, (byte) 0xBD, (byte) 0x3B, (byte) 0xB1, - (byte) 0xBF, (byte) 0x07, (byte) 0x35, (byte) 0x73, - (byte) 0xDF, (byte) 0x88, (byte) 0x3D, (byte) 0x2C, - (byte) 0x34, (byte) 0xF1, (byte) 0xEF, (byte) 0x45, - (byte) 0x1F, (byte) 0xD4, (byte) 0x6B, (byte) 0x50, - (byte) 0x3F, (byte) 0x00}; + (byte) 0x00, (byte) 0x51, (byte) 0x95, (byte) 0x3E, + (byte) 0xB9, (byte) 0x61, (byte) 0x8E, (byte) 0x1C, + (byte) 0x9A, (byte) 0x1F, (byte) 0x92, (byte) 0x9A, + (byte) 0x21, (byte) 0xA0, (byte) 0xB6, (byte) 0x85, + (byte) 0x40, (byte) 0xEE, (byte) 0xA2, (byte) 0xDA, + (byte) 0x72, (byte) 0x5B, (byte) 0x99, (byte) 0xB3, + (byte) 0x15, (byte) 0xF3, (byte) 0xB8, (byte) 0xB4, + (byte) 0x89, (byte) 0x91, (byte) 0x8E, (byte) 0xF1, + (byte) 0x09, (byte) 0xE1, (byte) 0x56, (byte) 0x19, + (byte) 0x39, (byte) 0x51, (byte) 0xEC, (byte) 0x7E, + (byte) 0x93, (byte) 0x7B, (byte) 0x16, (byte) 0x52, + (byte) 0xC0, (byte) 0xBD, (byte) 0x3B, (byte) 0xB1, + (byte) 0xBF, (byte) 0x07, (byte) 0x35, (byte) 0x73, + (byte) 0xDF, (byte) 0x88, (byte) 0x3D, (byte) 0x2C, + (byte) 0x34, (byte) 0xF1, (byte) 0xEF, (byte) 0x45, + (byte) 0x1F, (byte) 0xD4, (byte) 0x6B, (byte) 0x50, + (byte) 0x3F, (byte) 0x00}; // G in compressed form / first part of ucompressed public static final byte[] EC521_FP_G_X = new byte[]{ - (byte) 0x00, (byte) 0xC6, (byte) 0x85, (byte) 0x8E, - (byte) 0x06, (byte) 0xB7, (byte) 0x04, (byte) 0x04, - (byte) 0xE9, (byte) 0xCD, (byte) 0x9E, (byte) 0x3E, - (byte) 0xCB, (byte) 0x66, (byte) 0x23, (byte) 0x95, - (byte) 0xB4, (byte) 0x42, (byte) 0x9C, (byte) 0x64, - (byte) 0x81, (byte) 0x39, (byte) 0x05, (byte) 0x3F, - (byte) 0xB5, (byte) 0x21, (byte) 0xF8, (byte) 0x28, - (byte) 0xAF, (byte) 0x60, (byte) 0x6B, (byte) 0x4D, - (byte) 0x3D, (byte) 0xBA, (byte) 0xA1, (byte) 0x4B, - (byte) 0x5E, (byte) 0x77, (byte) 0xEF, (byte) 0xE7, - (byte) 0x59, (byte) 0x28, (byte) 0xFE, (byte) 0x1D, - (byte) 0xC1, (byte) 0x27, (byte) 0xA2, (byte) 0xFF, - (byte) 0xA8, (byte) 0xDE, (byte) 0x33, (byte) 0x48, - (byte) 0xB3, (byte) 0xC1, (byte) 0x85, (byte) 0x6A, - (byte) 0x42, (byte) 0x9B, (byte) 0xF9, (byte) 0x7E, - (byte) 0x7E, (byte) 0x31, (byte) 0xC2, (byte) 0xE5, - (byte) 0xBD, (byte) 0x66}; - + (byte) 0x00, (byte) 0xC6, (byte) 0x85, (byte) 0x8E, + (byte) 0x06, (byte) 0xB7, (byte) 0x04, (byte) 0x04, + (byte) 0xE9, (byte) 0xCD, (byte) 0x9E, (byte) 0x3E, + (byte) 0xCB, (byte) 0x66, (byte) 0x23, (byte) 0x95, + (byte) 0xB4, (byte) 0x42, (byte) 0x9C, (byte) 0x64, + (byte) 0x81, (byte) 0x39, (byte) 0x05, (byte) 0x3F, + (byte) 0xB5, (byte) 0x21, (byte) 0xF8, (byte) 0x28, + (byte) 0xAF, (byte) 0x60, (byte) 0x6B, (byte) 0x4D, + (byte) 0x3D, (byte) 0xBA, (byte) 0xA1, (byte) 0x4B, + (byte) 0x5E, (byte) 0x77, (byte) 0xEF, (byte) 0xE7, + (byte) 0x59, (byte) 0x28, (byte) 0xFE, (byte) 0x1D, + (byte) 0xC1, (byte) 0x27, (byte) 0xA2, (byte) 0xFF, + (byte) 0xA8, (byte) 0xDE, (byte) 0x33, (byte) 0x48, + (byte) 0xB3, (byte) 0xC1, (byte) 0x85, (byte) 0x6A, + (byte) 0x42, (byte) 0x9B, (byte) 0xF9, (byte) 0x7E, + (byte) 0x7E, (byte) 0x31, (byte) 0xC2, (byte) 0xE5, + (byte) 0xBD, (byte) 0x66}; + // second part of G uncompressed public static final byte[] EC521_FP_G_Y = new byte[]{ - (byte) 0x01, (byte) 0x18, (byte) 0x39, (byte) 0x29, - (byte) 0x6A, (byte) 0x78, (byte) 0x9A, (byte) 0x3B, - (byte) 0xC0, (byte) 0x04, (byte) 0x5C, (byte) 0x8A, - (byte) 0x5F, (byte) 0xB4, (byte) 0x2C, (byte) 0x7D, - (byte) 0x1B, (byte) 0xD9, (byte) 0x98, (byte) 0xF5, - (byte) 0x44, (byte) 0x49, (byte) 0x57, (byte) 0x9B, - (byte) 0x44, (byte) 0x68, (byte) 0x17, (byte) 0xAF, - (byte) 0xBD, (byte) 0x17, (byte) 0x27, (byte) 0x3E, - (byte) 0x66, (byte) 0x2C, (byte) 0x97, (byte) 0xEE, - (byte) 0x72, (byte) 0x99, (byte) 0x5E, (byte) 0xF4, - (byte) 0x26, (byte) 0x40, (byte) 0xC5, (byte) 0x50, - (byte) 0xB9, (byte) 0x01, (byte) 0x3F, (byte) 0xAD, - (byte) 0x07, (byte) 0x61, (byte) 0x35, (byte) 0x3C, - (byte) 0x70, (byte) 0x86, (byte) 0xA2, (byte) 0x72, - (byte) 0xC2, (byte) 0x40, (byte) 0x88, (byte) 0xBE, - (byte) 0x94, (byte) 0x76, (byte) 0x9F, (byte) 0xD1, - (byte) 0x66, (byte) 0x50}; + (byte) 0x01, (byte) 0x18, (byte) 0x39, (byte) 0x29, + (byte) 0x6A, (byte) 0x78, (byte) 0x9A, (byte) 0x3B, + (byte) 0xC0, (byte) 0x04, (byte) 0x5C, (byte) 0x8A, + (byte) 0x5F, (byte) 0xB4, (byte) 0x2C, (byte) 0x7D, + (byte) 0x1B, (byte) 0xD9, (byte) 0x98, (byte) 0xF5, + (byte) 0x44, (byte) 0x49, (byte) 0x57, (byte) 0x9B, + (byte) 0x44, (byte) 0x68, (byte) 0x17, (byte) 0xAF, + (byte) 0xBD, (byte) 0x17, (byte) 0x27, (byte) 0x3E, + (byte) 0x66, (byte) 0x2C, (byte) 0x97, (byte) 0xEE, + (byte) 0x72, (byte) 0x99, (byte) 0x5E, (byte) 0xF4, + (byte) 0x26, (byte) 0x40, (byte) 0xC5, (byte) 0x50, + (byte) 0xB9, (byte) 0x01, (byte) 0x3F, (byte) 0xAD, + (byte) 0x07, (byte) 0x61, (byte) 0x35, (byte) 0x3C, + (byte) 0x70, (byte) 0x86, (byte) 0xA2, (byte) 0x72, + (byte) 0xC2, (byte) 0x40, (byte) 0x88, (byte) 0xBE, + (byte) 0x94, (byte) 0x76, (byte) 0x9F, (byte) 0xD1, + (byte) 0x66, (byte) 0x50}; // Order of G public static final byte[] EC521_FP_R = new byte[]{ - (byte) 0x01, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, - (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFA, - (byte) 0x51, (byte) 0x86, (byte) 0x87, (byte) 0x83, - (byte) 0xBF, (byte) 0x2F, (byte) 0x96, (byte) 0x6B, - (byte) 0x7F, (byte) 0xCC, (byte) 0x01, (byte) 0x48, - (byte) 0xF7, (byte) 0x09, (byte) 0xA5, (byte) 0xD0, - (byte) 0x3B, (byte) 0xB5, (byte) 0xC9, (byte) 0xB8, - (byte) 0x89, (byte) 0x9C, (byte) 0x47, (byte) 0xAE, - (byte) 0xBB, (byte) 0x6F, (byte) 0xB7, (byte) 0x1E, - (byte) 0x91, (byte) 0x38, (byte) 0x64, (byte) 0x09}; - + (byte) 0x01, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFA, + (byte) 0x51, (byte) 0x86, (byte) 0x87, (byte) 0x83, + (byte) 0xBF, (byte) 0x2F, (byte) 0x96, (byte) 0x6B, + (byte) 0x7F, (byte) 0xCC, (byte) 0x01, (byte) 0x48, + (byte) 0xF7, (byte) 0x09, (byte) 0xA5, (byte) 0xD0, + (byte) 0x3B, (byte) 0xB5, (byte) 0xC9, (byte) 0xB8, + (byte) 0x89, (byte) 0x9C, (byte) 0x47, (byte) 0xAE, + (byte) 0xBB, (byte) 0x6F, (byte) 0xB7, (byte) 0x1E, + (byte) 0x91, (byte) 0x38, (byte) 0x64, (byte) 0x09}; + // cofactor of G public static final short EC521_FP_K = 1; + //sect163r1 from http://www.secg.org/sec2-v2.pdf + // [short i1, short i2, short i3] f = x^163 + x^i1 + x^i2 + x^i3 + 1 + public static final byte[] EC163_F2M_F = new byte[]{ + (byte) 0, (byte) 7, (byte) 0, (byte) 6, (byte) 0, (byte) 3 + }; + public static final byte[] EC163_F2M_A = new byte[]{ + (byte) 0x07, (byte) 0xB6, (byte) 0x88, (byte) 0x2C, + (byte) 0xAA, (byte) 0xEF, (byte) 0xA8, (byte) 0x4F, + (byte) 0x95, (byte) 0x54, (byte) 0xFF, (byte) 0x84, + (byte) 0x28, (byte) 0xBD, (byte) 0x88, (byte) 0xE2, + (byte) 0x46, (byte) 0xD2, (byte) 0x78, (byte) 0x2A, + (byte) 0xE2 + }; - - public static final byte VALID_KEY = 1; - public static final byte INVALIDB_FIXED = 2; - public static final byte INVALIDB_RANDOM = 3; + public static final byte[] EC163_F2M_B = new byte[]{ + (byte) 0x07, (byte) 0x13, (byte) 0x61, (byte) 0x2D, + (byte) 0xCD, (byte) 0xDC, (byte) 0xB4, (byte) 0x0A, + (byte) 0xAB, (byte) 0x94, (byte) 0x6B, (byte) 0xDA, + (byte) 0x29, (byte) 0xCA, (byte) 0x91, (byte) 0xF7, + (byte) 0x3A, (byte) 0xF9, (byte) 0x58, (byte) 0xAF, + (byte) 0xD9 + }; - public static void setValidECKeyParams(ECPublicKey ecPubKey, ECPrivateKey ecPrivKey, byte ecClass, short ecLength, byte[] auxBuffer) { - setECKeyParams(ecPubKey, ecPrivKey, ecClass, ecLength, auxBuffer, VALID_KEY); - } - public static void setInValidECKeyParams(ECPublicKey ecPubKey, ECPrivateKey ecPrivKey, byte ecClass, short ecLength, byte[] auxBuffer) { - setECKeyParams(ecPubKey, ecPrivKey, ecClass, ecLength, auxBuffer, INVALIDB_FIXED); - } - public static void setInValidECKeyParamsRandomB(ECPublicKey ecPubKey, ECPrivateKey ecPrivKey, byte ecClass, short ecLength, byte[] auxBuffer) { - setECKeyParams(ecPubKey, ecPrivKey, ecClass, ecLength, auxBuffer, INVALIDB_RANDOM); - } - private static void setECKeyParams(ECPublicKey ecPubKey, ECPrivateKey ecPrivKey, byte ecClass, short ecLength, byte[] auxBuffer, byte bInvalidKeyType) { - if (ecClass == KeyPair.ALG_EC_FP) { - // Select proper courve parameters - switch (ecLength) { - case (short) 128: { - EC_FP_P = EC128_FP_P; - EC_FP_A = EC128_FP_A; - EC_FP_B = EC128_FP_B; - EC_FP_G_X = EC128_FP_G_X; - EC_FP_G_Y = EC128_FP_G_Y; - EC_FP_R = EC128_FP_R; - EC_FP_K = EC128_FP_K; - break; - } - case (short) 160: { - EC_FP_P = EC160_FP_P; - EC_FP_A = EC160_FP_A; - EC_FP_B = EC160_FP_B; - EC_FP_G_X = EC160_FP_G_X; - EC_FP_G_Y = EC160_FP_G_Y; - EC_FP_R = EC160_FP_R; - EC_FP_K = EC160_FP_K; - break; - } - case (short) 192: { - EC_FP_P = EC192_FP_P; - EC_FP_A = EC192_FP_A; - EC_FP_B = EC192_FP_B; - EC_FP_G_X = EC192_FP_G_X; - EC_FP_G_Y = EC192_FP_G_Y; - EC_FP_R = EC192_FP_R; - EC_FP_K = EC192_FP_K; - break; - } - case (short) 224: { - EC_FP_P = EC224_FP_P; - EC_FP_A = EC224_FP_A; - EC_FP_B = EC224_FP_B; - EC_FP_G_X = EC224_FP_G_X; - EC_FP_G_Y = EC224_FP_G_Y; - EC_FP_R = EC224_FP_R; - EC_FP_K = EC224_FP_K; - break; - } - case (short) 256: { - EC_FP_P = EC256_FP_P; - EC_FP_A = EC256_FP_A; - EC_FP_B = EC256_FP_B; - EC_FP_G_X = EC256_FP_G_X; - EC_FP_G_Y = EC256_FP_G_Y; - EC_FP_R = EC256_FP_R; - EC_FP_K = EC256_FP_K; - break; - } - case (short) 384: { - EC_FP_P = EC384_FP_P; - EC_FP_A = EC384_FP_A; - EC_FP_B = EC384_FP_B; - EC_FP_G_X = EC384_FP_G_X; - EC_FP_G_Y = EC384_FP_G_Y; - EC_FP_R = EC384_FP_R; - EC_FP_K = EC384_FP_K; - break; - } - case (short) 521: { - EC_FP_P = EC521_FP_P; - EC_FP_A = EC521_FP_A; - EC_FP_B = EC521_FP_B; - EC_FP_G_X = EC521_FP_G_X; - EC_FP_G_Y = EC521_FP_G_Y; - EC_FP_R = EC521_FP_R; - EC_FP_K = EC521_FP_K; - break; - } - default: { + // G in compressed form / first part of ucompressed + public static final byte[] EC163_F2M_G_X = new byte[]{ + (byte) 0x03, (byte) 0x69, (byte) 0x97, (byte) 0x96, + (byte) 0x97, (byte) 0xAB, (byte) 0x43, (byte) 0x89, + (byte) 0x77, (byte) 0x89, (byte) 0x56, (byte) 0x67, + (byte) 0x89, (byte) 0x56, (byte) 0x7F, (byte) 0x78, + (byte) 0x7A, (byte) 0x78, (byte) 0x76, (byte) 0xA6, + (byte) 0x54 + }; + + // second part of G uncompressed + public static final byte[] EC163_F2M_G_Y = new byte[]{ + (byte) 0x00, (byte) 0x43, (byte) 0x5E, (byte) 0xDB, + (byte) 0x42, (byte) 0xEF, (byte) 0xAF, (byte) 0xB2, + (byte) 0x98, (byte) 0x9D, (byte) 0x51, (byte) 0xFE, + (byte) 0xFC, (byte) 0xE3, (byte) 0xC8, (byte) 0x09, + (byte) 0x88, (byte) 0xF4, (byte) 0x1F, (byte) 0xF8, + (byte) 0x83 + }; + + // order of G + public static final byte[] EC163_F2M_R = new byte[]{ + (byte) 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x48, + (byte) 0xAA, (byte) 0xB6, (byte) 0x89, (byte) 0xC2, + (byte) 0x9C, (byte) 0xA7, (byte) 0x10, (byte) 0x27, + (byte) 0x9B + }; + + // cofactor of G + public static final short EC163_F2M_K = 2; + + // getCorruptCurveParameter PARAMETER_CORRUPTION TYPES + public static final short CORRUPTION_NONE = 0x01; + public static final short CORRUPTION_FIXED = 0x02; + public static final short CORRUPTION_FULLRANDOM = 0x03; + public static final short CORRUPTION_ONEBYTERANDOM = 0x04; + public static final short CORRUPTION_ZERO = 0x05; + public static final short CORRUPTION_ONE = 0x06; + + // Supported embedded curves, getCurveParameter + // SECP recommended curves over FP + public static final byte CURVE_secp128r1 = 1; + public static final byte CURVE_secp160r1 = 2; + public static final byte CURVE_secp192r1 = 3; + public static final byte CURVE_secp224r1 = 4; + public static final byte CURVE_secp256r1 = 5; + public static final byte CURVE_secp384r1 = 6; + public static final byte CURVE_secp521r1 = 7; + + public static final byte FP_CURVES = 7; + + // SECP recommended curves over F2M + public static final byte CURVE_sect163r1 = 8; + public static final byte CURVE_sect233r1 = 9; + public static final byte CURVE_sect283r1 = 10; + public static final byte CURVE_sect409r1 = 11; + public static final byte CURVE_sect571r1 = 12; + + public static final byte F2M_CURVES = 12; + + public static byte getCurve(short keyClass, short keyLength) { + if (keyClass == KeyPair.ALG_EC_FP) { + switch (keyLength) { + case (short) 128: + return CURVE_secp128r1; + case (short) 160: + return CURVE_secp160r1; + case (short) 192: + return CURVE_secp192r1; + case (short) 224: + return CURVE_secp224r1; + case (short) 256: + return CURVE_secp256r1; + case (short) 384: + return CURVE_secp384r1; + case (short) 521: + return CURVE_secp521r1; + default: ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED); - } } - - // prepare an ANSI X9.62 uncompressed EC point representation for G - short gSize = (short) 1; - gSize += (short) EC_FP_G_X.length; - gSize += (short) EC_FP_G_Y.length; - auxBuffer[0] = 0x04; - short off = 1; - off = Util.arrayCopyNonAtomic(EC_FP_G_X, (short) 0, auxBuffer, off, (short) EC_FP_G_X.length); - Util.arrayCopyNonAtomic(EC_FP_G_Y, (short) 0, auxBuffer, off, (short) EC_FP_G_Y.length); - ecPubKey.setG(auxBuffer, (short) 0, gSize); - ecPrivKey.setG(auxBuffer, (short) 0, gSize); - - // pre-set basic EC parameters: - ecPubKey.setFieldFP(EC_FP_P, (short) 0, (short) EC_FP_P.length); - ecPrivKey.setFieldFP(EC_FP_P, (short) 0, (short) EC_FP_P.length); - ecPubKey.setA(EC_FP_A, (short) 0, (short) EC_FP_A.length); - ecPrivKey.setA(EC_FP_A, (short) 0, (short) EC_FP_A.length); - - if (bInvalidKeyType == VALID_KEY) { - // No corruption - ecPubKey.setB(EC_FP_B, (short) 0, (short) EC_FP_B.length); + } else if (keyClass == KeyPair.ALG_EC_F2M) { + switch (keyLength) { + case (short) 163: + return CURVE_sect163r1; + case (short) 233: + return CURVE_sect233r1; + case (short) 283: + return CURVE_sect283r1; + case (short) 409: + return CURVE_sect409r1; + case (short) 571: + return CURVE_sect571r1; + default: + ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED); + } + } else { + ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED); + } + return 0; //will not be reached + } + + public static short getCurveParameter(byte curve, byte param, byte[] outputBuffer, short outputOffset) { + byte alg = getCurveType(curve); + switch (curve) { + case CURVE_secp128r1: { + EC_FP_P = EC128_FP_P; + EC_A = EC128_FP_A; + EC_B = EC128_FP_B; + EC_G_X = EC128_FP_G_X; + EC_G_Y = EC128_FP_G_Y; + EC_R = EC128_FP_R; + EC_K = EC128_FP_K; + break; + } + case CURVE_secp160r1: { + EC_FP_P = EC160_FP_P; + EC_A = EC160_FP_A; + EC_B = EC160_FP_B; + EC_G_X = EC160_FP_G_X; + EC_G_Y = EC160_FP_G_Y; + EC_R = EC160_FP_R; + EC_K = EC160_FP_K; + break; + } + case CURVE_secp192r1: { + EC_FP_P = EC192_FP_P; + EC_A = EC192_FP_A; + EC_B = EC192_FP_B; + EC_G_X = EC192_FP_G_X; + EC_G_Y = EC192_FP_G_Y; + EC_R = EC192_FP_R; + EC_K = EC192_FP_K; + break; + } + case CURVE_secp224r1: { + EC_FP_P = EC224_FP_P; + EC_A = EC224_FP_A; + EC_B = EC224_FP_B; + EC_G_X = EC224_FP_G_X; + EC_G_Y = EC224_FP_G_Y; + EC_R = EC224_FP_R; + EC_K = EC224_FP_K; + break; + } + case CURVE_secp256r1: { + EC_FP_P = EC256_FP_P; + EC_A = EC256_FP_A; + EC_B = EC256_FP_B; + EC_G_X = EC256_FP_G_X; + EC_G_Y = EC256_FP_G_Y; + EC_R = EC256_FP_R; + EC_K = EC256_FP_K; + break; + } + case CURVE_secp384r1: { + EC_FP_P = EC384_FP_P; + EC_A = EC384_FP_A; + EC_B = EC384_FP_B; + EC_G_X = EC384_FP_G_X; + EC_G_Y = EC384_FP_G_Y; + EC_R = EC384_FP_R; + EC_K = EC384_FP_K; + break; } - if (bInvalidKeyType == INVALIDB_FIXED) { - // corrupt curve if required for testing - Util.arrayCopyNonAtomic(EC_FP_B, (short) 0, auxBuffer, (short) 0, (short) EC_FP_B.length); - auxBuffer[(byte) 10] = (byte) 0xcc; - auxBuffer[(byte) 11] = (byte) 0xcc; - ecPubKey.setB(auxBuffer, (short) 0, (short) EC_FP_B.length); + case CURVE_secp521r1: { + EC_FP_P = EC521_FP_P; + EC_A = EC521_FP_A; + EC_B = EC521_FP_B; + EC_G_X = EC521_FP_G_X; + EC_G_Y = EC521_FP_G_Y; + EC_R = EC521_FP_R; + EC_K = EC521_FP_K; + break; } - if (bInvalidKeyType == INVALIDB_RANDOM) { - // corrupt curve if required for testing - m_random.generateData(auxBuffer, (short) 0, (short) EC_FP_B.length); - ecPubKey.setB(auxBuffer, (short) 0, (short) EC_FP_B.length); + case CURVE_sect163r1: { + EC_F2M_F2M = EC163_F2M_F; + EC_A = EC163_F2M_A; + EC_B = EC163_F2M_B; + EC_G_X = EC163_F2M_G_X; + EC_G_Y = EC163_F2M_G_Y; + EC_R = EC163_F2M_R; + EC_K = EC163_F2M_K; + break; } - ecPrivKey.setB(EC_FP_B, (short) 0, (short) EC_FP_B.length); - - ecPubKey.setR(EC_FP_R, (short) 0, (short) EC_FP_R.length); - ecPrivKey.setR(EC_FP_R, (short) 0, (short) EC_FP_R.length); + default: + ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED); + } + short length = 0; + switch (param) { + case PARAMETER_FP: + if (alg == KeyPair.ALG_EC_FP) { + length = Util.arrayCopyNonAtomic(outputBuffer, outputOffset, EC_FP_P, (short) 0, (short) EC_FP_P.length); + } + break; + case PARAMETER_F2M: + if (alg == KeyPair.ALG_EC_F2M) { + length = Util.arrayCopyNonAtomic(outputBuffer, outputOffset, EC_F2M_F2M, (short) 0, (short) EC_F2M_F2M.length); + } + break; + case PARAMETER_A: + length = Util.arrayCopyNonAtomic(outputBuffer, outputOffset, EC_A, (short) 0, (short) EC_A.length); + break; + case PARAMETER_B: + length = Util.arrayCopyNonAtomic(outputBuffer, outputOffset, EC_B, (short) 0, (short) EC_B.length); + break; + case PARAMETER_G: + length = decompressG(outputBuffer, outputOffset, EC_G_X, (short) 0, (short) EC_G_X.length, EC_G_Y, (short) 0, (short) EC_G_Y.length); + break; + case PARAMETER_R: + length = Util.arrayCopyNonAtomic(outputBuffer, outputOffset, EC_R, (short) 0, (short) EC_R.length); + break; + case PARAMETER_K: + length = 2; + Util.setShort(outputBuffer, outputOffset, EC_K); + break; + default: + length = -1; + } + return length; + } - ecPubKey.setK(EC_FP_K); - ecPrivKey.setK(EC_FP_K); + public static short getCorruptCurveParameter(byte curve, byte param, byte[] outputBuffer, short outputOffset, short corruptionType) { + short length = getCurveParameter(curve, param, outputBuffer, outputOffset); + if (length <= 0) { + return length; } - if (ecClass == KeyPair.ALG_EC_F2M) { - // Not supported yet - ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED); + switch (corruptionType) { + case CORRUPTION_NONE: + break; + case CORRUPTION_FIXED: + if (length >= 1) { + outputBuffer[outputOffset] = (byte) 0xcc; + outputBuffer[(short) (outputOffset + length - 1)] = (byte) 0xcc; + } + break; + case CORRUPTION_FULLRANDOM: + m_random.generateData(outputBuffer, outputOffset, length); + break; + case CORRUPTION_ONEBYTERANDOM: + short first = Util.getShort(outputBuffer, (short) 0); // save first two bytes + + m_random.generateData(outputBuffer, (short) 0, (short) 2); // generate position + short rngPos = Util.getShort(outputBuffer, (short) 0); // save generated position + + Util.setShort(outputBuffer, (short) 0, first); // restore first two bytes + + if (rngPos < 0) { // make positive + rngPos = (short) -rngPos; + } + rngPos %= length; // make < param length + + byte original = outputBuffer[rngPos]; + while (original != outputBuffer[rngPos]){ + m_random.generateData(outputBuffer, rngPos, (short) 1); + } + break; + case CORRUPTION_ZERO: + Util.arrayFillNonAtomic(outputBuffer, outputOffset, length, (byte) 0); + break; + case CORRUPTION_ONE: + Util.arrayFillNonAtomic(outputBuffer, outputOffset, length, (byte) 1); + break; + default: + ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED); } - - } -/* - void setFPCurveParams(ECPublicKey ecPubKey, byte[] EC_FP_P, byte[] EC_FP_A, EC_FP_B) { - ecPubKey.setFieldFP(EC_FP_P, (short) 0, (short) EC_FP_P.length); - ecPrivKey.setA(EC_FP_A, (short) 0, (short) EC_FP_A.length); - ecPrivKey.setB(EC_FP_B, (short) 0, (short) EC_FP_B.length); - ecPrivKey.setG(auxBuffer, (short) 0, gSize); - ecPrivKey.setR(EC_FP_R, (short) 0, (short) EC_FP_R.length); - ecPrivKey.setK(EC_FP_K); + return length; + } + + public static byte getCurveType(byte curve) { + return curve <= FP_CURVES ? KeyPair.ALG_EC_FP : KeyPair.ALG_EC_F2M; + } + + private static short decompressG(byte[] outputBuffer, short outputOffset, byte[] gx, short gxOffset, short gxLength, byte[] gy, short gyOffset, short gyLength) { + short size = 1; + size += gxLength; + size += gyLength; + short offset = outputOffset; + offset += 1; + + outputBuffer[offset] = 0x04; + offset = Util.arrayCopyNonAtomic(gx, gxOffset, outputBuffer, offset, gxLength); + Util.arrayCopyNonAtomic(gy, gyOffset, outputBuffer, offset, gyLength); + return size; } - - , ECPrivateKey ecPrivKey , -*/ } diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java index 720ee4e..79abd0e 100644 --- a/src/applets/SimpleECCApplet.java +++ b/src/applets/SimpleECCApplet.java @@ -6,97 +6,85 @@ package applets; import javacard.framework.*; import javacard.security.*; -import javacardx.crypto.*; -public class SimpleECCApplet extends javacard.framework.Applet -{ +import javax.print.attribute.standard.MediaSize; + +public class SimpleECCApplet extends javacard.framework.Applet { // MAIN INSTRUCTION CLASS - final static byte CLA_SIMPLEECCAPPLET = (byte) 0xB0; + 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_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 short ARRAY_LENGTH = (short) 0xff; - final static byte AES_BLOCK_LENGTH = (short) 0x16; + 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_TESTEC_LASTUSEDPARAMS = (byte) 0x40; + + + final static short ARRAY_LENGTH = (short) 0xff; + final static byte AES_BLOCK_LENGTH = (short) 0x16; final static short EC_LENGTH_BITS = KeyBuilder.LENGTH_EC_FP_192; //final static short EC_LENGTH_BITS = KeyBuilder.LENGTH_EC_FP_160; //final static short EC_LENGTH_BITS = (short) 256; - - public final static byte ECTEST_SEPARATOR = (byte) 0xff; - public final static byte ECTEST_ALLOCATE_KEYPAIR = (byte) 0xc1; - public final static byte ECTEST_GENERATE_KEYPAIR_DEFCURVE = (byte) 0xc2; - public final static byte ECTEST_SET_VALIDCURVE = (byte) 0xc3; + + public final static byte ECTEST_SEPARATOR = (byte) 0xff; + public final static byte ECTEST_ALLOCATE_KEYPAIR = (byte) 0xc1; + public final static byte ECTEST_GENERATE_KEYPAIR_DEFCURVE = (byte) 0xc2; + public final static byte ECTEST_SET_VALIDCURVE = (byte) 0xc3; public final static byte ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE = (byte) 0xc4; - public final static byte ECTEST_SET_INVALIDCURVE = (byte) 0xc5; + public final static byte ECTEST_SET_INVALIDCURVE = (byte) 0xc5; public final static byte ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE = (byte) 0xc6; public final static byte ECTEST_ECDH_AGREEMENT_VALID_POINT = (byte) 0xc7; public final static byte ECTEST_ECDH_AGREEMENT_INVALID_POINT = (byte) 0xc8; public final static byte ECTEST_EXECUTED_REPEATS = (byte) 0xc9; public final static byte ECTEST_DH_GENERATESECRET = (byte) 0xca; - public final static short FLAG_ECTEST_ALLOCATE_KEYPAIR = (short) 0x0001; + public final static short FLAG_ECTEST_ALLOCATE_KEYPAIR = (short) 0x0001; public final static short FLAG_ECTEST_GENERATE_KEYPAIR_DEFCURVE = (short) 0x0002; - public final static short FLAG_ECTEST_SET_VALIDCURVE = (short) 0x0004; + public final static short FLAG_ECTEST_SET_VALIDCURVE = (short) 0x0004; public final static short FLAG_ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE = (short) 0x0008; - public final static short FLAG_ECTEST_SET_INVALIDCURVE = (short) 0x0010; + public final static short FLAG_ECTEST_SET_INVALIDCURVE = (short) 0x0010; public final static short FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE = (short) 0x0020; public final static short FLAG_ECTEST_ECDH_AGREEMENT_VALID_POINT = (short) 0x0040; public final static short FLAG_ECTEST_ECDH_AGREEMENT_INVALID_POINT = (short) 0x0080; - + public final static short FLAG_ECTEST_ALL = (short) 0x00ff; - - public final static short CORRUPT_B_FULLRANDOM = (short) 0x0001; - public final static short CORRUPT_B_ONEBYTERANDOM = (short) 0x0002; - public final static short CORRUPT_B_LASTBYTEINCREMENT = (short) 0x0003; - - - + + public final static short SW_SKIPPED = (short) 0x0ee1; public final static short SW_KEYPAIR_GENERATED_INVALID = (short) 0x0ee2; public final static short SW_INVALID_CORRUPTION_TYPE = (short) 0x0ee3; -/* + /* + public static final byte[] EC192_FP_PUBLICW = new byte[]{ + (byte) 0x04, (byte) 0xC9, (byte) 0xC0, (byte) 0xED, (byte) 0xFB, (byte) 0x27, + (byte) 0xB7, (byte) 0x1E, (byte) 0xBE, (byte) 0x30, (byte) 0x93, (byte) 0xFC, + (byte) 0x4F, (byte) 0x33, (byte) 0x76, (byte) 0x38, (byte) 0xCE, (byte) 0xE0, + (byte) 0x2F, (byte) 0x78, (byte) 0xF6, (byte) 0x3C, (byte) 0xEA, (byte) 0x90, + (byte) 0x22, (byte) 0x61, (byte) 0x32, (byte) 0x8E, (byte) 0x9F, (byte) 0x03, + (byte) 0x8A, (byte) 0xFD, (byte) 0x60, (byte) 0xA0, (byte) 0xCE, (byte) 0x01, + (byte) 0x9B, (byte) 0x76, (byte) 0x34, (byte) 0x59, (byte) 0x79, (byte) 0x64, + (byte) 0xD7, (byte) 0x79, (byte) 0x8E, (byte) 0x3B, (byte) 0x16, (byte) 0xD5, + (byte) 0x15}; + */ public static final byte[] EC192_FP_PUBLICW = new byte[]{ - (byte) 0x04, (byte) 0xC9, (byte) 0xC0, (byte) 0xED, (byte) 0xFB, (byte) 0x27, - (byte) 0xB7, (byte) 0x1E, (byte) 0xBE, (byte) 0x30, (byte) 0x93, (byte) 0xFC, - (byte) 0x4F, (byte) 0x33, (byte) 0x76, (byte) 0x38, (byte) 0xCE, (byte) 0xE0, - (byte) 0x2F, (byte) 0x78, (byte) 0xF6, (byte) 0x3C, (byte) 0xEA, (byte) 0x90, - (byte) 0x22, (byte) 0x61, (byte) 0x32, (byte) 0x8E, (byte) 0x9F, (byte) 0x03, - (byte) 0x8A, (byte) 0xFD, (byte) 0x60, (byte) 0xA0, (byte) 0xCE, (byte) 0x01, - (byte) 0x9B, (byte) 0x76, (byte) 0x34, (byte) 0x59, (byte) 0x79, (byte) 0x64, - (byte) 0xD7, (byte) 0x79, (byte) 0x8E, (byte) 0x3B, (byte) 0x16, (byte) 0xD5, - (byte) 0x15}; - */ - public static final byte[] EC192_FP_PUBLICW = new byte[]{ - (byte) 0x04, - (byte) 0x9d, (byte) 0x42, (byte) 0x76, (byte) 0x9d, (byte) 0xfd, (byte) 0xbe, - (byte) 0x11, (byte) 0x3a, (byte) 0x85, (byte) 0x1b, (byte) 0xb6, (byte) 0xb0, - (byte) 0x1b, (byte) 0x1a, (byte) 0x51, (byte) 0x5d, (byte) 0x89, (byte) 0x3b, - (byte) 0x5a, (byte) 0xdb, (byte) 0xc1, (byte) 0xf6, (byte) 0x13, (byte) 0x29, - (byte) 0x74, (byte) 0x74, (byte) 0x9a, (byte) 0xc0, (byte) 0x96, (byte) 0x7a, - (byte) 0x8f, (byte) 0xf4, (byte) 0xcc, (byte) 0x54, (byte) 0xd9, (byte) 0x31, - (byte) 0x87, (byte) 0x60, (byte) 0x2d, (byte) 0xd6, (byte) 0x7e, (byte) 0xb3, - (byte) 0xd2, (byte) 0x29, (byte) 0x70a, (byte) 0xca, (byte) 0x2ca}; - - - private KeyPair ecKeyPair = null; - private KeyPair ecKeyPair128 = null; - private KeyPair ecKeyPair160 = null; - private KeyPair ecKeyPair192 = null; - private KeyPair ecKeyPair256 = null; + (byte) 0x04, + (byte) 0x9d, (byte) 0x42, (byte) 0x76, (byte) 0x9d, (byte) 0xfd, (byte) 0xbe, + (byte) 0x11, (byte) 0x3a, (byte) 0x85, (byte) 0x1b, (byte) 0xb6, (byte) 0xb0, + (byte) 0x1b, (byte) 0x1a, (byte) 0x51, (byte) 0x5d, (byte) 0x89, (byte) 0x3b, + (byte) 0x5a, (byte) 0xdb, (byte) 0xc1, (byte) 0xf6, (byte) 0x13, (byte) 0x29, + (byte) 0x74, (byte) 0x74, (byte) 0x9a, (byte) 0xc0, (byte) 0x96, (byte) 0x7a, + (byte) 0x8f, (byte) 0xf4, (byte) 0xcc, (byte) 0x54, (byte) 0xd9, (byte) 0x31, + (byte) 0x87, (byte) 0x60, (byte) 0x2d, (byte) 0xd6, (byte) 0x7e, (byte) 0xb3, + (byte) 0xd2, (byte) 0x29, (byte) 0x70a, (byte) 0xca, (byte) 0x2ca}; + + private ECPublicKey ecPubKey = null; private ECPublicKey ecPubKey128 = null; private ECPublicKey ecPubKey160 = null; @@ -107,56 +95,66 @@ public class SimpleECCApplet extends javacard.framework.Applet private ECPrivateKey ecPrivKey160 = null; private ECPrivateKey ecPrivKey192 = null; private ECPrivateKey ecPrivKey256 = null; - + + private ECKeyGenerator ecKeyGenerator = null; + private ECKeyTester ecKeyTester = null; + private KeyAgreement dhKeyAgreement = null; private RandomData randomData = null; - + // TEMPORARRY ARRAY IN RAM private byte m_ramArray[] = null; private byte m_ramArray2[] = null; // PERSISTENT ARRAY IN EEPROM - private byte m_dataArray[] = null; - - short m_lenB = 0; + private byte m_dataArray[] = null; + + short m_lenB = 0; protected SimpleECCApplet(byte[] buffer, short offset, byte length) { short dataOffset = offset; - if(length > 9) { + if (length > 9) { // shift to privilege offset - dataOffset += (short)( 1 + buffer[offset]); + dataOffset += (short) (1 + buffer[offset]); // finally shift to Application specific offset - dataOffset += (short)( 1 + buffer[dataOffset]); + dataOffset += (short) (1 + buffer[dataOffset]); // go to proprietary data dataOffset++; m_ramArray = JCSystem.makeTransientByteArray(ARRAY_LENGTH, JCSystem.CLEAR_ON_RESET); m_ramArray2 = JCSystem.makeTransientByteArray(ARRAY_LENGTH, JCSystem.CLEAR_ON_RESET); - + m_dataArray = new byte[ARRAY_LENGTH]; Util.arrayFillNonAtomic(m_dataArray, (short) 0, ARRAY_LENGTH, (byte) 0); - + randomData = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM); - } + EC_Consts.m_random = randomData; + + ecKeyGenerator = new ECKeyGenerator(); + ecKeyTester = new ECKeyTester(); + ecKeyTester.allocateECDH(); + ecKeyTester.allocateECDHC(); + ecKeyTester.allocateECDSA(); + + } register(); } public static void install(byte[] bArray, short bOffset, byte bLength) throws ISOException { // applet instance creation - new SimpleECCApplet (bArray, bOffset, bLength); + new SimpleECCApplet(bArray, bOffset, bLength); } public boolean select() { - return true; + return true; } public void deselect() { return; } - public void process(APDU apdu) throws ISOException - { + public void process(APDU apdu) throws ISOException { // get the APDU buffer byte[] apduBuffer = apdu.getBuffer(); @@ -165,8 +163,8 @@ public class SimpleECCApplet extends javacard.framework.Applet return; if (apduBuffer[ISO7816.OFFSET_CLA] == CLA_SIMPLEECCAPPLET) { - switch ( apduBuffer[ISO7816.OFFSET_INS] ) { - + switch (apduBuffer[ISO7816.OFFSET_INS]) { + case INS_TESTECSUPPORT_GIVENALG: TestEC_SupportGivenLength(apdu); break; @@ -177,7 +175,7 @@ public class SimpleECCApplet extends javacard.framework.Applet TestEC_F2M_SupportAllLengths(apdu); break; case INS_ALLOCATEKEYPAIR: - AllocateKeyPairReturnDefCourve(apdu); + AllocateKeyPairReturnDefCurve(apdu); break; case INS_DERIVEECDHSECRET: DeriveECDHSecret(apdu); @@ -185,7 +183,7 @@ public class SimpleECCApplet extends javacard.framework.Applet case INS_TESTEC_GENERATEINVALID_FP: TestEC_FP_GenerateInvalidCurve(apdu); break; - case INS_TESTEC_LASTUSEDPARAMS: + case INS_TESTEC_LASTUSEDPARAMS: TestECSupportInvalidCurve_lastUsedParams(apdu); break; /* @@ -195,264 +193,171 @@ public class SimpleECCApplet extends javacard.framework.Applet case INS_GENERATEKEY: GenerateKey(apdu); break; -*/ - default : +*/ + default: // The INS code is not supported by the dispatcher - ISOException.throwIt( ISO7816.SW_INS_NOT_SUPPORTED ) ; - break ; + ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); + break; } - } - else ISOException.throwIt( ISO7816.SW_CLA_NOT_SUPPORTED); + } else ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED); } - + short TestECSupport(byte keyClass, short keyLen, byte[] buffer, short bufferOffset) { short baseOffset = bufferOffset; - + short testFlags = FLAG_ECTEST_ALL; - ecKeyPair = null; ecPubKey = null; ecPrivKey = null; - - buffer[bufferOffset] = ECTEST_SEPARATOR; bufferOffset++; - buffer[bufferOffset] = keyClass; bufferOffset++; - Util.setShort(buffer, bufferOffset, keyLen); bufferOffset += 2; - + + buffer[bufferOffset] = ECTEST_SEPARATOR; + bufferOffset++; + buffer[bufferOffset] = keyClass; + bufferOffset++; + Util.setShort(buffer, bufferOffset, keyLen); + bufferOffset += 2; + + short sw; + // // 1. Allocate KeyPair object // - buffer[bufferOffset] = ECTEST_ALLOCATE_KEYPAIR; bufferOffset++; + buffer[bufferOffset] = ECTEST_ALLOCATE_KEYPAIR; + bufferOffset++; + sw = SW_SKIPPED; if ((testFlags & FLAG_ECTEST_ALLOCATE_KEYPAIR) != (short) 0) { - try { - ecKeyPair = new KeyPair(keyClass, keyLen); - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); bufferOffset += 2; - } - catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); bufferOffset += 2; - testFlags = 0; // Can't continue if keypair was not allocated - } - catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; - testFlags = 0; // Can't continue if keypair was not allocated + sw = ecKeyGenerator.allocatePair(keyClass, keyLen); + + if (sw != ISO7816.SW_NO_ERROR) { + testFlags = 0; //keyPair allocation failed, cannot continue with tests } - } else { - Util.setShort(buffer, bufferOffset, SW_SKIPPED); - bufferOffset += 2; - } + } + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; + // // 2. Test keypair generation without explicit curve (=> default curve preset) // buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_DEFCURVE; bufferOffset++; + sw = SW_SKIPPED; if ((testFlags & FLAG_ECTEST_GENERATE_KEYPAIR_DEFCURVE) != (short) 0) { - try { - ecKeyPair.genKeyPair(); - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); - bufferOffset += 2; - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - } - catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; - } - } else { - Util.setShort(buffer, bufferOffset, SW_SKIPPED); - bufferOffset += 2; + sw = ecKeyGenerator.generatePair(); } + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; // // 3. Set valid custom curve // buffer[bufferOffset] = ECTEST_SET_VALIDCURVE; bufferOffset++; + sw = SW_SKIPPED; if ((testFlags & FLAG_ECTEST_SET_VALIDCURVE) != (short) 0) { - try { - ecPubKey = (ECPublicKey) ecKeyPair.getPublic(); - ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); - // Some implementation wil not return valid pub key until ecKeyPair.genKeyPair() is called - // Other implementation will fail with exception if same is called => try catch - try { - if (ecPubKey == null) { - ecKeyPair.genKeyPair(); - } - } catch (Exception e) {} // do intentionally nothing - - // Initialize curve parameters - EC_Consts.setValidECKeyParams(ecPubKey, ecPrivKey, keyClass, keyLen, m_ramArray); - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); - bufferOffset += 2; - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE; // Don't try generate keypair if valid custom curve was not set - } - catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; - testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE; // Don't try generate keypair if valid custom curve was not set + sw = ecKeyGenerator.setCustomCurve(keyClass, keyLen, m_ramArray, (short) 0); + + if (sw != ISO7816.SW_NO_ERROR) { + testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE; } - } else { - Util.setShort(buffer, bufferOffset, SW_SKIPPED); - bufferOffset += 2; } + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; // // 4. Generate keypair with custom curve // buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE; bufferOffset++; + sw = SW_SKIPPED; if ((testFlags & FLAG_ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE) != (short) 0) { - try { - ecKeyPair.genKeyPair(); - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); - bufferOffset += 2; - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - } catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; - } - } else { - Util.setShort(buffer, bufferOffset, SW_SKIPPED); - bufferOffset += 2; + sw = ecKeyGenerator.generatePair(); } - + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; + // // 5. ECDH agreement with valid public key // buffer[bufferOffset] = ECTEST_ECDH_AGREEMENT_VALID_POINT; bufferOffset++; + sw = SW_SKIPPED; if ((testFlags & FLAG_ECTEST_ECDH_AGREEMENT_VALID_POINT) != (short) 0) { - try { - // Generate fresh EC keypair - ecKeyPair.genKeyPair(); - ecPubKey = (ECPublicKey) ecKeyPair.getPublic(); - ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); - if (dhKeyAgreement == null) { - dhKeyAgreement = KeyAgreement.getInstance(KeyAgreement.ALG_EC_SVDP_DH, false); - } - dhKeyAgreement.init(ecPrivKey); - - short pubKeyLen = ecPubKey.getW(m_ramArray, (short) 0); - short secretLen = dhKeyAgreement.generateSecret(m_ramArray, (short) 0, pubKeyLen, m_ramArray2, (short) 0); - - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); - bufferOffset += 2; - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - } catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; + sw = ecKeyGenerator.generatePair(); + ecPubKey = ecKeyGenerator.getPublicKey(); + ecPrivKey = ecKeyGenerator.getPrivateKey(); + if (sw == ISO7816.SW_NO_ERROR) { + sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 1); } - } else { - Util.setShort(buffer, bufferOffset, SW_SKIPPED); - bufferOffset += 2; } - + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; + // // 6. ECDH agreement with invalid public key // buffer[bufferOffset] = ECTEST_ECDH_AGREEMENT_INVALID_POINT; bufferOffset++; + sw = SW_SKIPPED; if ((testFlags & FLAG_ECTEST_ECDH_AGREEMENT_INVALID_POINT) != (short) 0) { - try { - // Generate fresh EC keypair - ecKeyPair.genKeyPair(); - ecPubKey = (ECPublicKey) ecKeyPair.getPublic(); - ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); - dhKeyAgreement.init(ecPrivKey); - - short pubKeyLen = ecPubKey.getW(m_ramArray, (short) 0); - m_ramArray[(byte) 10] = (byte) 0xcc; // Corrupt public key - m_ramArray[(byte) 11] = (byte) 0xcc; - short secretLen = dhKeyAgreement.generateSecret(m_ramArray, (short) 0, pubKeyLen, m_ramArray2, (short) 0); - - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); - bufferOffset += 2; - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - } catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; + sw = ecKeyGenerator.generatePair(); + ecPubKey = ecKeyGenerator.getPublicKey(); + ecPrivKey = ecKeyGenerator.getPrivateKey(); + if (sw == ISO7816.SW_NO_ERROR) { + sw = ecKeyTester.testECDH_invalidPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 1); } - } else { - Util.setShort(buffer, bufferOffset, SW_SKIPPED); - bufferOffset += 2; } - + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; + // // 7. Set invalid custom curve // buffer[bufferOffset] = ECTEST_SET_INVALIDCURVE; bufferOffset++; + sw = SW_SKIPPED; if ((testFlags & FLAG_ECTEST_SET_INVALIDCURVE) != (short) 0) { - try { - // Initialize curve parameters - EC_Consts.setInValidECKeyParams(ecPubKey, ecPrivKey, keyClass, keyLen, m_ramArray); - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); - bufferOffset += 2; - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE; // Don't try generate keypair if invalid custom curve was not set - } catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; - testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE; // Don't try generate keypair if invalid custom curve was not set + sw = ecKeyGenerator.setCustomInvalidCurve(keyClass, keyLen, ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_B, EC_Consts.CORRUPTION_FIXED, m_ramArray, (short) 0); + + if (sw != ISO7816.SW_NO_ERROR) { + testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE; } - } else { - Util.setShort(buffer, bufferOffset, SW_SKIPPED); - bufferOffset += 2; } - + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; + // // 8. Generate keypair with invalid custom curve // buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE; bufferOffset++; + sw = SW_SKIPPED; if ((testFlags & FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE) != (short) 0) { - try { - ecKeyPair.genKeyPair(); - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); - bufferOffset += 2; - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - } catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; - } - } else { - Util.setShort(buffer, bufferOffset, SW_SKIPPED); - bufferOffset += 2; + sw = ecKeyGenerator.generatePair(); } + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; return (short) (bufferOffset - baseOffset); } - + void TestEC_SupportGivenLength(APDU apdu) { byte[] apdubuf = apdu.getBuffer(); short len = apdu.setIncomingAndReceive(); short dataOffset = ISO7816.OFFSET_CDATA; - byte algType = apdubuf[dataOffset]; dataOffset++; + byte algType = apdubuf[dataOffset]; + dataOffset++; short keyLength = Util.getShort(apdubuf, dataOffset); dataOffset += 2; dataOffset = 0; dataOffset += TestECSupport(algType, keyLength, apdubuf, dataOffset); - + apdu.setOutgoingAndSend((short) 0, dataOffset); } - + void TestEC_FP_SupportAllLengths(APDU apdu) { byte[] apdubuf = apdu.getBuffer(); short len = apdu.setIncomingAndReceive(); @@ -468,7 +373,8 @@ public class SimpleECCApplet extends javacard.framework.Applet dataOffset += TestECSupport(KeyPair.ALG_EC_FP, (short) 521, apdubuf, dataOffset); apdu.setOutgoingAndSend((short) 0, dataOffset); - } + } + void TestEC_F2M_SupportAllLengths(APDU apdu) { byte[] apdubuf = apdu.getBuffer(); short len = apdu.setIncomingAndReceive(); @@ -479,10 +385,10 @@ public class SimpleECCApplet extends javacard.framework.Applet dataOffset += TestECSupport(KeyPair.ALG_EC_F2M, (short) 131, apdubuf, dataOffset); dataOffset += TestECSupport(KeyPair.ALG_EC_F2M, (short) 163, apdubuf, dataOffset); dataOffset += TestECSupport(KeyPair.ALG_EC_F2M, (short) 193, apdubuf, dataOffset); - + apdu.setOutgoingAndSend((short) 0, dataOffset); } - + void TestEC_FP_GenerateInvalidCurve(APDU apdu) { byte[] apdubuf = apdu.getBuffer(); short len = apdu.setIncomingAndReceive(); @@ -494,7 +400,7 @@ public class SimpleECCApplet extends javacard.framework.Applet offset += 2; byte bRewindOnSuccess = apdubuf[offset]; offset++; - + short dataOffset = 0; // FP @@ -502,13 +408,12 @@ public class SimpleECCApplet extends javacard.framework.Applet apdu.setOutgoingAndSend((short) 0, dataOffset); } - + short TestECSupportInvalidCurve(byte keyClass, short keyLen, byte[] buffer, short bufferOffset, short repeats, short corruptionType, byte bRewindOnSuccess) { short baseOffset = bufferOffset; short testFlags = FLAG_ECTEST_ALL; - ecKeyPair = null; ecPubKey = null; ecPrivKey = null; @@ -518,53 +423,38 @@ public class SimpleECCApplet extends javacard.framework.Applet bufferOffset++; Util.setShort(buffer, bufferOffset, keyLen); bufferOffset += 2; - + short numExecutionsOffset = bufferOffset; // num executions to be stored later bufferOffset += 2; + short sw; + // // 1. Allocate KeyPair object // buffer[bufferOffset] = ECTEST_ALLOCATE_KEYPAIR; bufferOffset++; + sw = SW_SKIPPED; if ((testFlags & FLAG_ECTEST_ALLOCATE_KEYPAIR) != (short) 0) { - try { - ecKeyPair = new KeyPair(keyClass, keyLen); - ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); - ecPubKey = (ECPublicKey) ecKeyPair.getPublic(); - // Some implementation wil not return valid pub key until ecKeyPair.genKeyPair() is called - // Other implementation will fail with exception if same is called => try catch - try { - if (ecPubKey == null) { - ecKeyPair.genKeyPair(); - } - } catch (Exception e) { - } // do intentionally nothing - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); - bufferOffset += 2; - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - testFlags = 0; // Can't continue if keypair was not allocated - } catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; - testFlags = 0; // Can't continue if keypair was not allocated + sw = ecKeyGenerator.allocatePair(keyClass, keyLen); + if (sw == ISO7816.SW_NO_ERROR) { + ecPrivKey = ecKeyGenerator.getPrivateKey(); + ecPubKey = ecKeyGenerator.getPublicKey(); + } else { + testFlags = 0; } - } else { - Util.setShort(buffer, bufferOffset, SW_SKIPPED); - bufferOffset += 2; } + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; // // 2. Set invalid custom curve (many times) // - EC_Consts.m_random = randomData; - EC_Consts.setValidECKeyParams(ecPubKey, ecPrivKey, keyClass, keyLen, m_ramArray); + sw = ecKeyGenerator.setCustomCurve(keyClass, keyLen, m_ramArray, (short) 0); + ecPrivKey = ecKeyGenerator.getPrivateKey(); + ecPubKey = ecKeyGenerator.getPublicKey(); + - m_lenB = ecPubKey.getB(m_ramArray, (short) 0); // store valid B - Util.arrayCopyNonAtomic(m_ramArray, (short) 0, m_ramArray2, (short) 0, m_lenB); // also in m_ramArray2 - short startOffset = bufferOffset; short i; for (i = 0; i < repeats; i++) { @@ -572,41 +462,27 @@ public class SimpleECCApplet extends javacard.framework.Applet if (bRewindOnSuccess == 1) { // if nothing unexpected happened, rewind bufferOffset back again bufferOffset = startOffset; - } - - // Store valid curve B param - ecPubKey.getB(m_ramArray, (short) 0); // store valid B - Util.arrayCopyNonAtomic(m_ramArray, (short) 0, m_ramArray2, (short) 0, m_lenB); // also in m_ramArray2 + } // set invalid curve buffer[bufferOffset] = ECTEST_SET_INVALIDCURVE; bufferOffset++; - + // Supported types of invalid curve: - // 1. Completely random B - // 2. Valid B but with one random byte randomly changed - // 3. Valid B but with last byte incremented - switch (corruptionType) { - case CORRUPT_B_FULLRANDOM: - randomData.generateData(m_ramArray2, (short) 0, m_lenB); - break; - case CORRUPT_B_ONEBYTERANDOM: - // Copy valid B into m_ramArray2 - Util.arrayCopyNonAtomic(m_ramArray, (short) 0, m_ramArray2, (short) 0, m_lenB); - // Generate random position and one random byte for subsequent change - // Note - we are using same array m_ramArray2, but in area unsued by stored B - randomData.generateData(m_ramArray2, m_lenB, (short) 2); - - short rngPos = m_ramArray2[m_lenB]; // random position (within B) - if (rngPos < 0) { rngPos = (short) -rngPos; } // make it positive - rngPos %= m_lenB; - m_ramArray2[rngPos] = m_ramArray2[(short) (m_lenB + 1)]; // set random byte on random position - // Make sure its not the valid byte again - if (m_ramArray[rngPos] == m_ramArray2[rngPos]) { - m_ramArray2[rngPos] += 1; // if yes, just increment - } - - break; + // CORRUPTION_NONE = 0x01, valid parameter + // CORRUPTION_FIXED = 0x02, first and last byte changed to a fixed value + // CORRUPTION_FULLRANDOM = 0x03, completely random parameter data + // CORRUPTION_ONEBYTERANDOM = 0x04, one random byte randomly changed + // CORRUPTION_ZERO = 0x05, parameter competely zero + // CORRUPTION_ONE = 0x06, parameter completely one + sw = ecKeyGenerator.setCustomInvalidCurve(keyClass, keyClass, ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_B, corruptionType, m_ramArray, (short) 0); + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; + if (sw != ISO7816.SW_NO_ERROR) { + // if we reach this line, we are interested in value of B that caused incorrect response + break; // stop execution, return B + } + /* //TODO implement CORRUPT_B_LASTBYTEINCREMENT somehow case CORRUPT_B_LASTBYTEINCREMENT: m_ramArray2[(short) (m_lenB - 1)] += 1; // Make sure its not the valid byte again @@ -614,101 +490,54 @@ public class SimpleECCApplet extends javacard.framework.Applet m_ramArray2[(short) (m_lenB - 1)] += 1; // if yes, increment once more } break; - default: - ISOException.throwIt(SW_INVALID_CORRUPTION_TYPE); - break; - } - - - // Set corrupted B parameter - try { - ecPubKey.setB(m_ramArray2, (short) 0, m_lenB); - ecPrivKey.setB(m_ramArray2, (short) 0, m_lenB); - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); // ok if setB itself will not emit exception - bufferOffset += 2; - }catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - // if we reach this line, we are interested in value of B that caused incorrect response - break; // stop execution, return B - }catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; - // if we reach this line, we are interested in value of B that caused incorrect response - break; // stop execution, return B } + */ // Gen key pair with invalid curve - try { - buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE; - bufferOffset++; - // Should fail - ecKeyPair.genKeyPair(); + + buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE; + bufferOffset++; + // Should fail + sw = ecKeyGenerator.generatePair(); + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; + + if (sw == ISO7816.SW_NO_ERROR) { // If this line is reached, we generated key pair - what should not happen - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); - bufferOffset += 2; - - // if we reach this line, we are interested in value of B - try { - buffer[bufferOffset] = ECTEST_DH_GENERATESECRET; - bufferOffset++; - ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); - if (dhKeyAgreement == null) { - dhKeyAgreement = KeyAgreement.getInstance(KeyAgreement.ALG_EC_SVDP_DH, false); - } - dhKeyAgreement.init(ecPrivKey); - short lenW = ecPubKey.getW(m_ramArray2, (short) 0); // store valid B - dhKeyAgreement.generateSecret(m_ramArray2, (short) 0, lenW, m_ramArray, (short) 0); - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - } catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; - } - - break; // stop execution, return B - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - } catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); + buffer[bufferOffset] = ECTEST_DH_GENERATESECRET; + bufferOffset++; + + ecPrivKey = ecKeyGenerator.getPrivateKey(); + ecPubKey = ecKeyGenerator.getPublicKey(); + + sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 0); + Util.setShort(buffer, bufferOffset, sw); bufferOffset += 2; + break; //stop execution, return B } - - // + // Generate keypair with valid curve - to check that whole engine is not somehow blocked // after previous attempt with invalid curve // // set valid curve buffer[bufferOffset] = ECTEST_SET_VALIDCURVE; bufferOffset++; - EC_Consts.setValidECKeyParams(ecPubKey, ecPrivKey, keyClass, keyLen, m_ramArray); - - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); + sw = ecKeyGenerator.setCustomCurve(keyClass, keyLen, m_ramArray, (short) 0); + + Util.setShort(buffer, bufferOffset, sw); bufferOffset += 2; // Gen key pair with valid curve - try { - buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE; - bufferOffset++; - // Should succeed - ecKeyPair.genKeyPair(); - // If this line is reached, we generated valid key pair (expected) - Util.setShort(buffer, bufferOffset, ISO7816.SW_NO_ERROR); - bufferOffset += 2; - } catch (CryptoException e) { - Util.setShort(buffer, bufferOffset, e.getReason()); - bufferOffset += 2; - // if we reach this line, we are interested in value of B that caused incorrect response - break; // stop execution, return B - } catch (Exception e) { - Util.setShort(buffer, bufferOffset, ISO7816.SW_UNKNOWN); - bufferOffset += 2; - // if we reach this line, we are interested in value of B that caused incorrect response - break; // stop execution, return B + buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE; + bufferOffset++; + + sw = ecKeyGenerator.generatePair(); + Util.setShort(buffer, bufferOffset, sw); + bufferOffset += 2; + if (sw != ISO7816.SW_NO_ERROR) { + break; } - + // If we reach this line => everything was as expected // Rewind offset in array back (no storage of info about expected runs) // bufferOffset = startOffset; done at beginning @@ -717,76 +546,64 @@ public class SimpleECCApplet extends javacard.framework.Applet bufferOffset += 2; } } - + // Set number of executed repeats Util.setShort(buffer, numExecutionsOffset, i); - + return (short) (bufferOffset - baseOffset); } - + void TestECSupportInvalidCurve_lastUsedParams(APDU apdu) { byte[] apdubuf = apdu.getBuffer(); apdu.setIncomingAndReceive(); - + short offset = 0; Util.arrayCopyNonAtomic(m_ramArray2, (short) 0, apdubuf, offset, m_lenB); offset += m_lenB; - + apdu.setOutgoingAndSend((short) 0, offset); } - - void AllocateKeyPairReturnDefCourve(APDU apdu) { + + void AllocateKeyPairReturnDefCurve(APDU apdu) { byte[] apdubuf = apdu.getBuffer(); apdu.setIncomingAndReceive(); short bitLen = Util.getShort(apdubuf, ISO7816.OFFSET_CDATA); - // Note: all locations shoudl happen in constructor. But here it is intentional + // Note: all locations should happen in constructor. But here it is intentional // as we like to test for result of allocation - ecKeyPair = new KeyPair(KeyPair.ALG_EC_FP, bitLen); + ecKeyGenerator.allocatePair(KeyPair.ALG_EC_FP, bitLen); // If required, generate also new key pair if (apdubuf[ISO7816.OFFSET_P1] == (byte) 1) { - ecPubKey = (ECPublicKey) ecKeyPair.getPublic(); - ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); - // Some implementation wil not return valid pub key until ecKeyPair.genKeyPair() is called - // Other implementation will fail with exception if same is called => try catch - try { - if (ecPubKey == null) { - ecKeyPair.genKeyPair(); - } - } catch (Exception e) { - } // do nothing // If required, initialize curve parameters first if (apdubuf[ISO7816.OFFSET_P2] == (byte) 2) { - EC_Consts.setValidECKeyParams(ecPubKey, ecPrivKey, KeyPair.ALG_EC_FP, bitLen, m_ramArray); + ecKeyGenerator.setCustomCurve(KeyPair.ALG_EC_FP, bitLen, m_ramArray, (short) 0); } // Now generate new keypair with either default or custom curve - ecKeyPair.genKeyPair(); - ecPubKey = (ECPublicKey) ecKeyPair.getPublic(); - ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); + ecKeyGenerator.generatePair(); - short len = 0; + short len; short offset = 0; // Export curve public parameters offset += 2; // reserve space for length - len = ecPubKey.getField(apdubuf, offset); + len = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_FP, apdubuf, offset); Util.setShort(apdubuf, (short) (offset - 2), len); offset += len; offset += 2; // reserve space for length - len = ecPubKey.getA(apdubuf, offset); + len = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_A, apdubuf, offset); Util.setShort(apdubuf, (short) (offset - 2), len); offset += len; offset += 2; // reserve space for length - len = ecPubKey.getB(apdubuf, offset); + len = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_B, apdubuf, offset); Util.setShort(apdubuf, (short) (offset - 2), len); offset += len; offset += 2; // reserve space for length - len = ecPubKey.getR(apdubuf, offset); + len = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_R, apdubuf, offset); Util.setShort(apdubuf, (short) (offset - 2), len); offset += len; /* @@ -812,8 +629,8 @@ public class SimpleECCApplet extends javacard.framework.Applet } // Generate fresh EC keypair - ecKeyPair.genKeyPair(); - ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate(); + ecKeyGenerator.generatePair(); + ecPrivKey = ecKeyGenerator.getPrivateKey(); if (dhKeyAgreement == null) { dhKeyAgreement = KeyAgreement.getInstance(KeyAgreement.ALG_EC_SVDP_DH, false); @@ -826,22 +643,6 @@ public class SimpleECCApplet extends javacard.framework.Applet apdu.setOutgoingAndSend((short) 0, secretLen); } - - - - - - - - - - - - - - - - /* void AllocateKeyPair(byte algorithm, short bitLen) { @@ -913,6 +714,6 @@ public class SimpleECCApplet extends javacard.framework.Applet apdu.setOutgoingAndSend((short) 0, offset); } -*/ +*/ } diff --git a/src/simpleapdu/SimpleAPDU.java b/src/simpleapdu/SimpleAPDU.java index 0ea9ca3..5baf9a6 100644 --- a/src/simpleapdu/SimpleAPDU.java +++ b/src/simpleapdu/SimpleAPDU.java @@ -1,50 +1,53 @@ package simpleapdu; +import applets.EC_Consts; import applets.SimpleECCApplet; -import static applets.SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE; -import static applets.SimpleECCApplet.ECTEST_SET_INVALIDCURVE; import javacard.framework.ISO7816; import javacard.security.CryptoException; import javacard.security.KeyPair; -import javax.smartcardio.ResponseAPDU; import org.bouncycastle.util.Arrays; +import javax.smartcardio.ResponseAPDU; + /** - * * @author Petr Svenda petr@svenda.com */ public class SimpleAPDU { static CardMngr cardManager = new CardMngr(); - private final static byte SELECT_ECTESTERAPPLET[] = {(byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, (byte) 0x0a, - (byte) 0x45, (byte) 0x43, (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x30, (byte) 0x31}; + private final static byte SELECT_ECTESTERAPPLET[] = {(byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, (byte) 0x0a, + (byte) 0x45, (byte) 0x43, (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x30, (byte) 0x31}; private static final byte TESTECSUPPORTALL_FP[] = {(byte) 0xB0, (byte) 0x5E, (byte) 0x00, (byte) 0x00, (byte) 0x00}; private static final byte TESTECSUPPORTALL_F2M[] = {(byte) 0xB0, (byte) 0x5F, (byte) 0x00, (byte) 0x00, (byte) 0x00}; private static final byte TESTECSUPPORT_GIVENALG[] = {(byte) 0xB0, (byte) 0x71, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00}; private static final short TESTECSUPPORT_ALG_OFFSET = 5; private static final short TESTECSUPPORT_KEYLENGTH_OFFSET = 6; - + private static final byte TESTECSUPPORTALL_LASTUSEDPARAMS[] = {(byte) 0xB0, (byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x00}; - + 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}; private static final short INVALIDCURVEB_NUMREPEATS_OFFSET = 5; private static final short INVALIDCURVEB_CORRUPTIONTYPE_OFFSET = 7; private static final short INVALIDCURVEB_REWINDONSUCCESS_OFFSET = 9; - + static short getShort(byte[] array, int offset) { - return (short) (((array[offset] & 0xFF) << 8) | (array[offset + 1] & 0xFF)); + return (short) (((array[offset] & 0xFF) << 8) | (array[offset + 1] & 0xFF)); } + static void setShort(byte[] array, int offset, short value) { array[offset + 1] = (byte) (value & 0xFF); array[offset] = (byte) ((value >> 8) & 0xFF); - } + } + static void testFPkeyGen_setNumRepeats(byte[] apduArray, short numRepeats) { setShort(apduArray, INVALIDCURVEB_NUMREPEATS_OFFSET, numRepeats); } + static void testFPkeyGen_setCorruptionType(byte[] apduArray, short corruptionType) { setShort(apduArray, INVALIDCURVEB_CORRUPTIONTYPE_OFFSET, corruptionType); } + static void testFPkeyGen_rewindOnSuccess(byte[] apduArray, boolean bRewind) { apduArray[INVALIDCURVEB_REWINDONSUCCESS_OFFSET] = bRewind ? (byte) 1 : (byte) 0; } @@ -57,16 +60,17 @@ public class SimpleAPDU { } return cardManager; } - + static void testSupportECGivenAlg(byte[] apdu, CardMngr cardManager) throws Exception { ReconnnectToCard(); ResponseAPDU resp = cardManager.sendAPDU(apdu); PrintECSupport(resp); } + static void testSupportECAll(CardMngr cardManager) throws Exception { byte[] testAPDU = Arrays.clone(TESTECSUPPORT_GIVENALG); - testAPDU[TESTECSUPPORT_ALG_OFFSET] = KeyPair.ALG_EC_FP; + testAPDU[TESTECSUPPORT_ALG_OFFSET] = KeyPair.ALG_EC_FP; setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 128); testSupportECGivenAlg(testAPDU, cardManager); setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 160); @@ -81,7 +85,7 @@ public class SimpleAPDU { testSupportECGivenAlg(testAPDU, cardManager); setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 521); testSupportECGivenAlg(testAPDU, cardManager); - + testAPDU[TESTECSUPPORT_ALG_OFFSET] = KeyPair.ALG_EC_F2M; setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 113); testSupportECGivenAlg(testAPDU, cardManager); @@ -91,8 +95,9 @@ public class SimpleAPDU { testSupportECGivenAlg(testAPDU, cardManager); setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 193); testSupportECGivenAlg(testAPDU, cardManager); - + } + public static void main(String[] args) { try { // @@ -101,11 +106,11 @@ public class SimpleAPDU { if (cardManager.ConnectToCard()) { testSupportECAll(cardManager); - + // Test setting invalid parameter B of curve byte[] testAPDU = Arrays.clone(TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB); //testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_LASTBYTEINCREMENT); - testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_ONEBYTERANDOM); + testFPkeyGen_setCorruptionType(testAPDU, EC_Consts.CORRUPTION_ONEBYTERANDOM); //testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_FULLRANDOM); testFPkeyGen_setNumRepeats(testAPDU, (short) 10); testFPkeyGen_rewindOnSuccess(testAPDU, true); @@ -124,7 +129,7 @@ public class SimpleAPDU { PrintECSupport(resp_fp); PrintECSupport(resp_f2m); */ - + cardManager.DisconnectFromCard(); } else { System.out.println("Failed to connect to card"); @@ -133,12 +138,11 @@ public class SimpleAPDU { System.out.println("Exception : " + ex); } } - + static String getPrintError(short code) { if (code == ISO7816.SW_NO_ERROR) { return "OK\t(0x9000)"; - } - else { + } else { String codeStr = "unknown"; if (code == CryptoException.ILLEGAL_VALUE) { codeStr = "ILLEGAL_VALUE"; @@ -165,22 +169,22 @@ public class SimpleAPDU { codeStr = "SW_INVALID_CORRUPTION_TYPE"; } return String.format("fail\t(%s,\t0x%4x)", codeStr, code); - } + } } - + enum ExpResult { SHOULD_SUCCEDD, MAY_FAIL, MUST_FAIL } + static int VerifyPrintResult(String message, byte expectedTag, byte[] buffer, int bufferOffset, ExpResult expRes) { if (bufferOffset >= buffer.length) { System.out.println(" No more data returned"); - } - else { + } else { if (buffer[bufferOffset] != expectedTag) { System.out.println(" ERROR: mismatched tag"); - assert(buffer[bufferOffset] == expectedTag); + assert (buffer[bufferOffset] == expectedTag); } bufferOffset++; short resCode = getShort(buffer, bufferOffset); @@ -195,13 +199,13 @@ public class SimpleAPDU { } if (bHiglight) { System.out.println(String.format("!! %-50s%s", message, getPrintError(resCode))); - } - else { + } else { System.out.println(String.format(" %-50s%s", message, getPrintError(resCode))); } } return bufferOffset; } + static void PrintECSupport(ResponseAPDU resp) { byte[] buffer = resp.getData(); @@ -209,7 +213,7 @@ public class SimpleAPDU { System.out.println("### Test for support and with valid and invalid EC curves"); int bufferOffset = 0; while (bufferOffset < buffer.length) { - assert(buffer[bufferOffset] == SimpleECCApplet.ECTEST_SEPARATOR); + assert (buffer[bufferOffset] == SimpleECCApplet.ECTEST_SEPARATOR); bufferOffset++; String ecType = "unknown"; if (buffer[bufferOffset] == KeyPair.ALG_EC_FP) { @@ -232,10 +236,11 @@ public class SimpleAPDU { bufferOffset = VerifyPrintResult("ECDH agreement with invalid point (fail is good):", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_INVALID_POINT, buffer, bufferOffset, ExpResult.MUST_FAIL); bufferOffset = VerifyPrintResult("Set invalid custom curve (may fail):", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset, ExpResult.MAY_FAIL); bufferOffset = VerifyPrintResult("Generate key with invalid curve (fail is good):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE, buffer, bufferOffset, ExpResult.MUST_FAIL); - + System.out.println(); } } + static void PrintECKeyGenInvalidCurveB(ResponseAPDU resp) { byte[] buffer = resp.getData(); @@ -261,8 +266,8 @@ public class SimpleAPDU { short numRepeats = getShort(buffer, bufferOffset); bufferOffset += 2; System.out.println(String.format("%-53s%d times", "Executed repeats before unexpected error: ", numRepeats)); - - + + bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD); while (bufferOffset < buffer.length) { bufferOffset = VerifyPrintResult("Set invalid custom curve:", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD); @@ -277,7 +282,7 @@ public class SimpleAPDU { System.out.println(); } } - + static void PrintECKeyGenInvalidCurveB_lastUserParams(ResponseAPDU resp) { byte[] buffer = resp.getData(); short offset = 0; @@ -286,6 +291,6 @@ public class SimpleAPDU { System.out.print(String.format("%x ", buffer[offset])); offset++; } - - } + + } } -- cgit v1.3.1 From 6a68401612f5e61f966def58ed60eaff52b6f7f6 Mon Sep 17 00:00:00 2001 From: J08nY Date: Wed, 2 Nov 2016 00:21:42 +0100 Subject: EC_Consts: made parameters maskable ECKeyGenerator: since parameters are maskable, one can now setCustomInvalidCurve(curve, PARAMETER_A | PARAMETER_B,...) and the resulting curve will have both A and B invalid. --- src/applets/ECKeyGenerator.java | 27 ++++++++++++--------------- src/applets/ECKeyTester.java | 8 ++------ src/applets/EC_Consts.java | 20 ++++++++------------ src/applets/SimpleECCApplet.java | 23 ++++++++++++++--------- 4 files changed, 36 insertions(+), 42 deletions(-) (limited to 'src/applets/SimpleECCApplet.java') diff --git a/src/applets/ECKeyGenerator.java b/src/applets/ECKeyGenerator.java index c7155de..393c249 100644 --- a/src/applets/ECKeyGenerator.java +++ b/src/applets/ECKeyGenerator.java @@ -70,7 +70,7 @@ public class ECKeyGenerator { } if (sw != ISO7816.SW_NO_ERROR) return sw; - for (byte param = EC_Consts.PARAMETER_A; param < EC_Consts.PARAMETER_K; ++param) { + for (byte param = EC_Consts.PARAMETER_A; param <= EC_Consts.PARAMETER_K; param = (byte)(param << 1)) { length = EC_Consts.getCurveParameter(curve, param, buffer, offset); sw = setExternalParameter(KEY_BOTH, param, buffer, offset, length); if (sw != ISO7816.SW_NO_ERROR) break; @@ -86,8 +86,17 @@ public class ECKeyGenerator { short sw = setCustomCurve(curve, buffer, offset); if (sw != ISO7816.SW_NO_ERROR) return sw; - short length = EC_Consts.getCorruptCurveParameter(curve, param, buffer, offset, corruptionType); - sw = setExternalParameter(key, param, buffer, offset, length); + //go through param bit by bit, and invalidate all selected params + byte paramMask = 0x01; + while (paramMask <= EC_Consts.PARAMETER_K) { + byte masked = (byte)(paramMask & param); + if (masked != 0){ + short length = EC_Consts.getCorruptCurveParameter(curve, masked, buffer, offset, corruptionType); + sw = setExternalParameter(key, masked, buffer, offset, length); + if (sw != ISO7816.SW_NO_ERROR) return sw; + } + paramMask = (byte)(paramMask << 1); + } return sw; } @@ -139,12 +148,6 @@ public class ECKeyGenerator { if ((key & KEY_PUBLIC) != 0) ecPublicKey.setK(k); } break; - case EC_Consts.PARAMETER_S: - if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setS(data, offset, length); - break; - case EC_Consts.PARAMETER_W: - if ((key & KEY_PUBLIC) != 0) ecPublicKey.setW(data, offset, length); - break; default: result = ISO7816.SW_UNKNOWN; } @@ -189,12 +192,6 @@ public class ECKeyGenerator { if ((key & KEY_PRIVATE) != 0) Util.setShort(outputBuffer, outputOffset, ecPrivateKey.getK()); length = 2; break; - case EC_Consts.PARAMETER_S: - if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getS(outputBuffer, outputOffset); - break; - case EC_Consts.PARAMETER_W: - if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getW(outputBuffer, outputOffset); - break; default: length = -1; } diff --git a/src/applets/ECKeyTester.java b/src/applets/ECKeyTester.java index 757ece7..1031b06 100644 --- a/src/applets/ECKeyTester.java +++ b/src/applets/ECKeyTester.java @@ -13,10 +13,6 @@ public class ECKeyTester { private KeyAgreement ecdhcKeyAgreement = null; private Signature ecdsaSignature = null; - //TODO: move these SW definitions to the main applet class. - public final static short SW_SIG_LENGTH_MISMATCH = (short) 0xee4; - public final static short SW_SIG_VERIFY_FAIL = (short) 0xee5; - public short allocateECDH() { short result = ISO7816.SW_NO_ERROR; try { @@ -150,12 +146,12 @@ public class ECKeyTester { short sigLength = ecdsaSignature.sign(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset); if (sigLength != 20) { // per javacard.security.Signature an ALG_ECDSA_SHA should be 20 bytes. - result = ECKeyTester.SW_SIG_LENGTH_MISMATCH; + result = SimpleECCApplet.SW_SIG_LENGTH_MISMATCH; } else { ecdsaSignature.init(verifyKey, Signature.MODE_VERIFY); boolean correct = ecdsaSignature.verify(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset, sigLength); if (!correct) { - result = ECKeyTester.SW_SIG_VERIFY_FAIL; + result = SimpleECCApplet.SW_SIG_VERIFY_FAIL; } } } catch (CryptoException ce) { diff --git a/src/applets/EC_Consts.java b/src/applets/EC_Consts.java index 7521979..8bfa7ff 100644 --- a/src/applets/EC_Consts.java +++ b/src/applets/EC_Consts.java @@ -17,18 +17,14 @@ public class EC_Consts { private static byte[] EC_F2M_F2M = null; //[short ii, short i2, short i3], f = x^m + x^i1 + x^i2 + x^i3 + 1 - public static final byte PARAMETER_FP = 1; - public static final byte PARAMETER_F2M = 2; - - public static final byte PARAMETER_A = 3; - public static final byte PARAMETER_B = 4; - public static final byte PARAMETER_G = 5; - public static final byte PARAMETER_R = 6; - public static final byte PARAMETER_K = 7; - - //TODO make params maskable, to allow for PARAMETER_A | PARAMETER_B passed to for example ECKeyGenerator.setInvalidCustomCurve - public static final byte PARAMETER_S = 8; //private key - public static final byte PARAMETER_W = 9; //public key + public static final byte PARAMETER_FP = 0x01; + public static final byte PARAMETER_F2M = 0x02; + + public static final byte PARAMETER_A = 0x04; + public static final byte PARAMETER_B = 0x08; + public static final byte PARAMETER_G = 0x10; + public static final byte PARAMETER_R = 0x20; + public static final byte PARAMETER_K = 0x40; public static RandomData m_random = null; diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java index 79abd0e..683e61d 100644 --- a/src/applets/SimpleECCApplet.java +++ b/src/applets/SimpleECCApplet.java @@ -7,24 +7,24 @@ package applets; import javacard.framework.*; import javacard.security.*; -import javax.print.attribute.standard.MediaSize; public class SimpleECCApplet extends javacard.framework.Applet { + // MAIN INSTRUCTION CLASS - final static byte CLA_SIMPLEECCAPPLET = (byte) 0xB0; + 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_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_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_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_TESTEC_LASTUSEDPARAMS = (byte) 0x40; + final static byte INS_TESTEC_LASTUSEDPARAMS = (byte) 0x40; final static short ARRAY_LENGTH = (short) 0xff; @@ -61,6 +61,8 @@ public class SimpleECCApplet extends javacard.framework.Applet { public final static short SW_SKIPPED = (short) 0x0ee1; public final static short SW_KEYPAIR_GENERATED_INVALID = (short) 0x0ee2; public final static short SW_INVALID_CORRUPTION_TYPE = (short) 0x0ee3; + public final static short SW_SIG_LENGTH_MISMATCH = (short) 0xee4; + public final static short SW_SIG_VERIFY_FAIL = (short) 0xee5; /* public static final byte[] EC192_FP_PUBLICW = new byte[]{ (byte) 0x04, (byte) 0xC9, (byte) 0xC0, (byte) 0xED, (byte) 0xFB, (byte) 0x27, @@ -454,6 +456,7 @@ public class SimpleECCApplet extends javacard.framework.Applet { ecPrivKey = ecKeyGenerator.getPrivateKey(); ecPubKey = ecKeyGenerator.getPublicKey(); + m_lenB = ecPubKey.getB(m_ramArray2, (short) 0); //store valid B short startOffset = bufferOffset; short i; @@ -511,6 +514,7 @@ public class SimpleECCApplet extends javacard.framework.Applet { ecPubKey = ecKeyGenerator.getPublicKey(); sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 0); + m_lenB = ecPubKey.getB(m_ramArray2, (short) 0); //store B Util.setShort(buffer, bufferOffset, sw); bufferOffset += 2; break; //stop execution, return B @@ -553,6 +557,7 @@ public class SimpleECCApplet extends javacard.framework.Applet { return (short) (bufferOffset - baseOffset); } + //TODO: generalize invalid B setting to all curve params void TestECSupportInvalidCurve_lastUsedParams(APDU apdu) { byte[] apdubuf = apdu.getBuffer(); apdu.setIncomingAndReceive(); -- cgit v1.3.1 From c2be46f5881a3a8e63c0815de28f0516ceeb7bcc Mon Sep 17 00:00:00 2001 From: J08nY Date: Wed, 2 Nov 2016 21:05:56 +0100 Subject: Implemented External curve setting on applet side SimpleECCApplet: added TestEC_SupportExternal which receives an FP or F2M elliptic curve parameters in an APDU, sets it and tries ECDH, ECDSA. ECKeyGenerator: added setExternalCurve, which reads and sets external parameters from one buffer, with data order: field | a | b | g | r | k --- src/applets/ECKeyGenerator.java | 33 ++++++++ src/applets/SimpleECCApplet.java | 169 +++++++++++++++++++++++++++++++++++++-- src/simpleapdu/SimpleAPDU.java | 6 ++ 3 files changed, 200 insertions(+), 8 deletions(-) (limited to 'src/applets/SimpleECCApplet.java') diff --git a/src/applets/ECKeyGenerator.java b/src/applets/ECKeyGenerator.java index 393c249..28b3ed3 100644 --- a/src/applets/ECKeyGenerator.java +++ b/src/applets/ECKeyGenerator.java @@ -70,6 +70,7 @@ public class ECKeyGenerator { } if (sw != ISO7816.SW_NO_ERROR) return sw; + //go through all params for (byte param = EC_Consts.PARAMETER_A; param <= EC_Consts.PARAMETER_K; param = (byte)(param << 1)) { length = EC_Consts.getCurveParameter(curve, param, buffer, offset); sw = setExternalParameter(KEY_BOTH, param, buffer, offset, length); @@ -159,6 +160,38 @@ public class ECKeyGenerator { return result; } + public short setExternalCurve(byte key, byte keyClass, byte[] buffer, short offset, short fieldLength, short aLength, short bLength, short gxLength, short gyLength, short rLength){ + short sw = ISO7816.SW_NO_ERROR; + if (keyClass == KeyPair.ALG_EC_FP) { + sw = setExternalParameter(key, EC_Consts.PARAMETER_FP, buffer, offset, fieldLength); + } else if (keyClass == KeyPair.ALG_EC_F2M) { + sw = setExternalParameter(key, EC_Consts.PARAMETER_F2M, buffer, offset, fieldLength); + } + if (sw != ISO7816.SW_NO_ERROR) return sw; + + offset += fieldLength; + + //go through all params + sw = setExternalParameter(key, EC_Consts.PARAMETER_A, buffer, offset, aLength); + if (sw != ISO7816.SW_NO_ERROR) return sw; + offset += aLength; + sw = setExternalParameter(key, EC_Consts.PARAMETER_B, buffer, offset, bLength); + if (sw != ISO7816.SW_NO_ERROR) return sw; + offset += bLength; + + sw = setExternalParameter(key, EC_Consts.PARAMETER_G, buffer, offset, (short) (gxLength + gyLength)); + if (sw != ISO7816.SW_NO_ERROR) return sw; + offset += gxLength + gyLength; + + + sw = setExternalParameter(key, EC_Consts.PARAMETER_R, buffer, offset, aLength); + if (sw != ISO7816.SW_NO_ERROR) return sw; + offset += rLength; + + sw = setExternalParameter(key, EC_Consts.PARAMETER_K, buffer, offset, (short) 2); + return sw; + } + public short exportParameter(byte key, short param, byte[] outputBuffer, short outputOffset) { if (key == KEY_BOTH) { return -1; diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java index 683e61d..3b5b514 100644 --- a/src/applets/SimpleECCApplet.java +++ b/src/applets/SimpleECCApplet.java @@ -11,20 +11,21 @@ import javacard.security.*; public class SimpleECCApplet extends javacard.framework.Applet { // MAIN INSTRUCTION CLASS - final static byte CLA_SIMPLEECCAPPLET = (byte) 0xB0; + 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_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_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_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_TESTEC_LASTUSEDPARAMS = (byte) 0x40; + final static byte INS_TESTECSUPPORT_EXTERNAL = (byte) 0x72; + final static byte INS_TESTEC_LASTUSEDPARAMS = (byte) 0x40; final static short ARRAY_LENGTH = (short) 0xff; @@ -45,6 +46,9 @@ public class SimpleECCApplet extends javacard.framework.Applet { public final static byte ECTEST_ECDH_AGREEMENT_INVALID_POINT = (byte) 0xc8; public final static byte ECTEST_EXECUTED_REPEATS = (byte) 0xc9; public final static byte ECTEST_DH_GENERATESECRET = (byte) 0xca; + public final static byte ECTEST_SET_EXTERNALCURVE = (byte) 0xcb; + public final static byte ECTEST_GENERATE_KEYPAIR_EXTERNALCURVE = (byte) 0xcc; + public final static byte ECTEST_ECDSA_SIGNATURE = (byte) 0xcd; public final static short FLAG_ECTEST_ALLOCATE_KEYPAIR = (short) 0x0001; public final static short FLAG_ECTEST_GENERATE_KEYPAIR_DEFCURVE = (short) 0x0002; @@ -188,6 +192,9 @@ public class SimpleECCApplet extends javacard.framework.Applet { case INS_TESTEC_LASTUSEDPARAMS: TestECSupportInvalidCurve_lastUsedParams(apdu); break; + case INS_TESTECSUPPORT_EXTERNAL: + TestEC_SupportExternal(apdu); + break; /* case INS_ALLOCATEKEYPAIRS: AllocateKeyPairs(apdu); @@ -391,6 +398,152 @@ public class SimpleECCApplet extends javacard.framework.Applet { apdu.setOutgoingAndSend((short) 0, dataOffset); } + short TestECSupportExternalCurve(byte keyClass, short keyLength, byte[] buffer, short bufferOffset, short outputOffset) { + short startOffset = outputOffset; + + short fieldLength = Util.getShort(buffer, bufferOffset); + bufferOffset += 2; + short aLength = Util.getShort(buffer, bufferOffset); + bufferOffset += 2; + short bLength = Util.getShort(buffer, bufferOffset); + bufferOffset += 2; + short gxLength = Util.getShort(buffer, bufferOffset); + bufferOffset += 2; + short gyLength = Util.getShort(buffer, bufferOffset); + bufferOffset += 2; + short rLength = Util.getShort(buffer, bufferOffset); + bufferOffset += 2; + + buffer[outputOffset] = ECTEST_SEPARATOR; + outputOffset++; + + // allocatePair + buffer[outputOffset] = ECTEST_ALLOCATE_KEYPAIR; + outputOffset++; + short sw = ecKeyGenerator.allocatePair(keyClass, keyLength); + Util.setShort(buffer, outputOffset, sw); + outputOffset += 2; + if (sw != ISO7816.SW_NO_ERROR) { + return (short) (outputOffset - startOffset); + } + + // setExternalParam -> forall in {field, a, b, g, r, k} + buffer[outputOffset] = ECTEST_SET_EXTERNALCURVE; + outputOffset++; + sw = ecKeyGenerator.setExternalCurve(ECKeyGenerator.KEY_BOTH, keyClass, buffer, bufferOffset, fieldLength, aLength, bLength, gxLength, gyLength, rLength); + Util.setShort(buffer, outputOffset, sw); + outputOffset += 2; + if (sw != ISO7816.SW_NO_ERROR) { + return (short) (outputOffset - startOffset); + } + + // generatePair + buffer[outputOffset] = ECTEST_GENERATE_KEYPAIR_EXTERNALCURVE; + outputOffset++; + sw = ecKeyGenerator.generatePair(); + Util.setShort(buffer, outputOffset, sw); + outputOffset += 2; + if (sw != ISO7816.SW_NO_ERROR) { + return (short) (outputOffset - startOffset); + } + + ecPubKey = ecKeyGenerator.getPublicKey(); + ecPrivKey = ecKeyGenerator.getPrivateKey(); + + // test_ECDH + buffer[outputOffset] = ECTEST_ECDH_AGREEMENT_VALID_POINT; + outputOffset++; + sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 0); + Util.setShort(buffer, outputOffset, sw); + outputOffset += 2; + if (sw != ISO7816.SW_NO_ERROR) { + return (short) (outputOffset - startOffset); + } + + // test_ECDH invalid + buffer[outputOffset] = ECTEST_ECDH_AGREEMENT_INVALID_POINT; + outputOffset++; + sw = ecKeyTester.testECDH_invalidPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 0); + Util.setShort(buffer, outputOffset, sw); + outputOffset += 2; + if (sw != ISO7816.SW_NO_ERROR) { + return (short) (outputOffset - startOffset); + } + + // test_ECDSA + buffer[outputOffset] = ECTEST_ECDSA_SIGNATURE; + outputOffset++; + randomData.generateData(m_ramArray, (short) 0, (short) (ARRAY_LENGTH / 2)); + sw = ecKeyTester.testECDSA(ecPrivKey, ecPubKey, m_ramArray, (short) 0, (short) (ARRAY_LENGTH / 2), m_ramArray2, (short) 0); + Util.setShort(buffer, outputOffset, sw); + outputOffset += 2; + if (sw != ISO7816.SW_NO_ERROR) { + return (short) (outputOffset - startOffset); + } + + return (short) (outputOffset - startOffset); + } + + /** + * Receives an FP or F2M elliptic curve parameters in the APDU. + * Then allocates a new keypair, sets said curve and tries ECDH, ECDSA. + * APDU format: + * byte CLA = CLA_SIMPLEECCAPPLET + * byte INS = INS_TESTECSUPPORT_EXTERNAL + * byte P0 + * byte P1 + *
+ * CDATA: + * byte keyClass -> KeyPair.ALG_EC_FP or KeyPair.ALG_EC_F2\M + * short keyLength + * short fieldLength + * short aLength + * short bLength + * short gxLength + * short gyLength + * short rLength + * field -> FP: prime / F2M: three or one short representing the reduction polynomial + * a + * b + * gx + * gy + * r + * short k + *
+ * Response APDU format:
+ * CDATA:
+ * byte ECTEST_SEPARATOR
+ * byte ECTEST_ALLOCATE_KEYPAIR
+ * short sw
+ * byte ECTEST_SET_EXTERNALCURVE
+ * short sw
+ * byte ECTEST_GENERATE_KEYPAIR_EXTERNALCURVE
+ * short sw
+ * byte ECTEST_ECDH_AGREEMENT_VALID_POINT
+ * short sw
+ * byte ECTEST_ECDH_AGREEMENT_INVALID_POINT
+ * short sw
+ * byte ECTEST_ECDSA_SIGNATURE
+ * short sw
+ *
+ * @param apdu
+ */
+ void TestEC_SupportExternal(APDU apdu) {
+ byte[] apdubuf = apdu.getBuffer();
+ short len = apdu.setIncomingAndReceive();
+
+ short offset = ISO7816.OFFSET_CDATA;
+ byte keyClass = apdubuf[offset];
+ ++offset;
+ short keyLength = Util.getShort(apdubuf, offset);
+ offset += 2;
+
+ short dataLength = TestECSupportExternalCurve(keyClass, keyLength, apdubuf, offset, (short) 0);
+
+ apdu.setOutgoingAndSend((short) 0, dataLength);
+ }
+
+
void TestEC_FP_GenerateInvalidCurve(APDU apdu) {
byte[] apdubuf = apdu.getBuffer();
short len = apdu.setIncomingAndReceive();
diff --git a/src/simpleapdu/SimpleAPDU.java b/src/simpleapdu/SimpleAPDU.java
index 5baf9a6..7401509 100644
--- a/src/simpleapdu/SimpleAPDU.java
+++ b/src/simpleapdu/SimpleAPDU.java
@@ -168,6 +168,12 @@ public class SimpleAPDU {
if (code == SimpleECCApplet.SW_INVALID_CORRUPTION_TYPE) {
codeStr = "SW_INVALID_CORRUPTION_TYPE";
}
+ if (code == SimpleECCApplet.SW_SIG_LENGTH_MISMATCH) {
+ codeStr = "SW_SIG_LENGTH_MISMATCH";
+ }
+ if (code == SimpleECCApplet.SW_SIG_VERIFY_FAIL) {
+ codeStr = "SW_SIG_VERIFY_FAIL";
+ }
return String.format("fail\t(%s,\t0x%4x)", codeStr, code);
}
}
--
cgit v1.3.1
From 7d946796d87638a5f54cc8562c9d3a95309cf3cc Mon Sep 17 00:00:00 2001
From: J08nY
Date: Mon, 7 Nov 2016 20:26:50 +0100
Subject: ECKeyGenerator: fixes to key genertion, ECKeyTester: fixed ECDH/ECDHC
pubkey length issue. EC_Consts: fixed decompressG and getCurveParameter array
copy mismatch SimpleECCApplet: fixed B parameter test
All tests now pass as before.
---
src/applets/ECKeyGenerator.java | 102 ++++++++++++++++++++++-----------------
src/applets/ECKeyTester.java | 16 +++---
src/applets/EC_Consts.java | 31 ++++++++----
src/applets/SimpleECCApplet.java | 29 +++++------
src/simpleapdu/SimpleAPDU.java | 2 +-
5 files changed, 102 insertions(+), 78 deletions(-)
(limited to 'src/applets/SimpleECCApplet.java')
diff --git a/src/applets/ECKeyGenerator.java b/src/applets/ECKeyGenerator.java
index 28b3ed3..e9bdfa6 100644
--- a/src/applets/ECKeyGenerator.java
+++ b/src/applets/ECKeyGenerator.java
@@ -1,6 +1,7 @@
package applets;
import javacard.framework.ISO7816;
+import javacard.framework.ISOException;
import javacard.framework.Util;
import javacard.security.CryptoException;
import javacard.security.ECPrivateKey;
@@ -16,17 +17,18 @@ public class ECKeyGenerator {
private ECPrivateKey ecPrivateKey = null;
private ECPublicKey ecPublicKey = null;
- public static final byte KEY_PUBLIC = 0x1;
- public static final byte KEY_PRIVATE = 0x2;
- public static final byte KEY_BOTH = KEY_PUBLIC & KEY_PRIVATE;
+ public static final byte KEY_PUBLIC = 0x01;
+ public static final byte KEY_PRIVATE = 0x02;
+ public static final byte KEY_BOTH = KEY_PUBLIC | KEY_PRIVATE;
- public short allocatePair(byte algorithm, short keyLength) {
+ //TODO: add something like allocateGenerate, or modify allocate to auto-generate a key-pair if it returns null key references after allocating
+ public short allocatePair(byte keyClass, short keyLength) {
short result = ISO7816.SW_NO_ERROR;
try {
- ecKeyPair = new KeyPair(algorithm, keyLength);
- ecPrivateKey = (ECPrivateKey) ecKeyPair.getPrivate();
+ ecKeyPair = new KeyPair(keyClass, keyLength);
ecPublicKey = (ECPublicKey) ecKeyPair.getPublic();
+ ecPrivateKey = (ECPrivateKey) ecKeyPair.getPrivate();
} catch (CryptoException ce) {
result = ce.getReason();
} catch (Exception e) {
@@ -36,15 +38,15 @@ public class ECKeyGenerator {
}
public boolean isAllocated() {
- return ecKeyPair != null && ecPrivateKey != null && ecPublicKey != null;
+ return ecKeyPair != null;
}
public short generatePair() {
short result = ISO7816.SW_NO_ERROR;
try {
ecKeyPair.genKeyPair();
- ecPrivateKey = (ECPrivateKey) ecKeyPair.getPrivate();
ecPublicKey = (ECPublicKey) ecKeyPair.getPublic();
+ ecPrivateKey = (ECPrivateKey) ecKeyPair.getPrivate();
} catch (CryptoException ce) {
result = ce.getReason();
} catch (Exception e) {
@@ -63,18 +65,20 @@ public class ECKeyGenerator {
short length;
if (alg == KeyPair.ALG_EC_FP) {
length = EC_Consts.getCurveParameter(curve, EC_Consts.PARAMETER_FP, buffer, offset);
- sw = setExternalParameter(KEY_BOTH, EC_Consts.PARAMETER_FP, buffer, offset, length);
+ sw = setParameter(KEY_BOTH, EC_Consts.PARAMETER_FP, buffer, offset, length);
} else if (alg == KeyPair.ALG_EC_F2M) {
length = EC_Consts.getCurveParameter(curve, EC_Consts.PARAMETER_F2M, buffer, offset);
- sw = setExternalParameter(KEY_BOTH, EC_Consts.PARAMETER_F2M, buffer, offset, length);
+ sw = setParameter(KEY_BOTH, EC_Consts.PARAMETER_F2M, buffer, offset, length);
}
if (sw != ISO7816.SW_NO_ERROR) return sw;
//go through all params
- for (byte param = EC_Consts.PARAMETER_A; param <= EC_Consts.PARAMETER_K; param = (byte)(param << 1)) {
+ byte param = EC_Consts.PARAMETER_A;
+ while (param > 0) {
length = EC_Consts.getCurveParameter(curve, param, buffer, offset);
- sw = setExternalParameter(KEY_BOTH, param, buffer, offset, length);
+ sw = setParameter(KEY_BOTH, param, buffer, offset, length);
if (sw != ISO7816.SW_NO_ERROR) break;
+ param = (byte) (param << 1);
}
return sw;
}
@@ -89,68 +93,76 @@ public class ECKeyGenerator {
//go through param bit by bit, and invalidate all selected params
byte paramMask = 0x01;
- while (paramMask <= EC_Consts.PARAMETER_K) {
- byte masked = (byte)(paramMask & param);
- if (masked != 0){
+ while (paramMask > 0) {
+ byte masked = (byte) (paramMask & param);
+ if (masked != 0) {
short length = EC_Consts.getCorruptCurveParameter(curve, masked, buffer, offset, corruptionType);
- sw = setExternalParameter(key, masked, buffer, offset, length);
+ sw = setParameter(key, masked, buffer, offset, length);
if (sw != ISO7816.SW_NO_ERROR) return sw;
}
- paramMask = (byte)(paramMask << 1);
+ paramMask = (byte) (paramMask << 1);
}
return sw;
}
- public short setExternalParameter(byte key, byte param, byte[] data, short offset, short length) {
+ public short setParameter(byte key, byte param, byte[] data, short offset, short length) {
short result = ISO7816.SW_NO_ERROR;
try {
switch (param) {
- case EC_Consts.PARAMETER_FP:
- if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldFP(data, offset, length);
+ case EC_Consts.PARAMETER_FP: {
if ((key & KEY_PUBLIC) != 0) ecPublicKey.setFieldFP(data, offset, length);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldFP(data, offset, length);
break;
- case EC_Consts.PARAMETER_F2M:
+ }
+ case EC_Consts.PARAMETER_F2M: {
if (length == 2) {
short i = Util.makeShort(data[offset], data[(short) (offset + 1)]);
- if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldF2M(i);
if ((key & KEY_PUBLIC) != 0) ecPublicKey.setFieldF2M(i);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldF2M(i);
} else if (length == 6) {
short i1 = Util.makeShort(data[offset], data[(short) (offset + 1)]);
short i2 = Util.makeShort(data[(short) (offset + 2)], data[(short) (offset + 3)]);
short i3 = Util.makeShort(data[(short) (offset + 4)], data[(short) (offset + 5)]);
- if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldF2M(i1, i2, i3);
if ((key & KEY_PUBLIC) != 0) ecPublicKey.setFieldF2M(i1, i2, i3);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldF2M(i1, i2, i3);
} else {
result = ISO7816.SW_UNKNOWN;
}
break;
- case EC_Consts.PARAMETER_A:
- if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setA(data, offset, length);
+ }
+ case EC_Consts.PARAMETER_A: {
if ((key & KEY_PUBLIC) != 0) ecPublicKey.setA(data, offset, length);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setA(data, offset, length);
break;
- case EC_Consts.PARAMETER_B:
- if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setB(data, offset, length);
+ }
+ case EC_Consts.PARAMETER_B: {
if ((key & KEY_PUBLIC) != 0) ecPublicKey.setB(data, offset, length);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setB(data, offset, length);
break;
- case EC_Consts.PARAMETER_G:
- if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setG(data, offset, length);
+ }
+ case EC_Consts.PARAMETER_G: {
if ((key & KEY_PUBLIC) != 0) ecPublicKey.setG(data, offset, length);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setG(data, offset, length);
break;
- case EC_Consts.PARAMETER_R:
- if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setR(data, offset, length);
+ }
+ case EC_Consts.PARAMETER_R: {
if ((key & KEY_PUBLIC) != 0) ecPublicKey.setR(data, offset, length);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setR(data, offset, length);
break;
- case EC_Consts.PARAMETER_K:
+ }
+ case EC_Consts.PARAMETER_K: {
if (length != 2) {
result = ISO7816.SW_UNKNOWN;
} else {
- short k = Util.makeShort(data[offset], data[(short) (offset + 1)]);
- if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setK(k);
+ short k = Util.getShort(data, offset);
if ((key & KEY_PUBLIC) != 0) ecPublicKey.setK(k);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setK(k);
}
break;
- default:
- result = ISO7816.SW_UNKNOWN;
+ }
+ default: {
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
}
} catch (CryptoException ce) {
result = ce.getReason();
@@ -160,35 +172,35 @@ public class ECKeyGenerator {
return result;
}
- public short setExternalCurve(byte key, byte keyClass, byte[] buffer, short offset, short fieldLength, short aLength, short bLength, short gxLength, short gyLength, short rLength){
+ public short setExternalCurve(byte key, byte keyClass, byte[] buffer, short offset, short fieldLength, short aLength, short bLength, short gxLength, short gyLength, short rLength) {
short sw = ISO7816.SW_NO_ERROR;
if (keyClass == KeyPair.ALG_EC_FP) {
- sw = setExternalParameter(key, EC_Consts.PARAMETER_FP, buffer, offset, fieldLength);
+ sw = setParameter(key, EC_Consts.PARAMETER_FP, buffer, offset, fieldLength);
} else if (keyClass == KeyPair.ALG_EC_F2M) {
- sw = setExternalParameter(key, EC_Consts.PARAMETER_F2M, buffer, offset, fieldLength);
+ sw = setParameter(key, EC_Consts.PARAMETER_F2M, buffer, offset, fieldLength);
}
if (sw != ISO7816.SW_NO_ERROR) return sw;
offset += fieldLength;
//go through all params
- sw = setExternalParameter(key, EC_Consts.PARAMETER_A, buffer, offset, aLength);
+ sw = setParameter(key, EC_Consts.PARAMETER_A, buffer, offset, aLength);
if (sw != ISO7816.SW_NO_ERROR) return sw;
offset += aLength;
- sw = setExternalParameter(key, EC_Consts.PARAMETER_B, buffer, offset, bLength);
+ sw = setParameter(key, EC_Consts.PARAMETER_B, buffer, offset, bLength);
if (sw != ISO7816.SW_NO_ERROR) return sw;
offset += bLength;
- sw = setExternalParameter(key, EC_Consts.PARAMETER_G, buffer, offset, (short) (gxLength + gyLength));
+ sw = setParameter(key, EC_Consts.PARAMETER_G, buffer, offset, (short) (gxLength + gyLength));
if (sw != ISO7816.SW_NO_ERROR) return sw;
offset += gxLength + gyLength;
- sw = setExternalParameter(key, EC_Consts.PARAMETER_R, buffer, offset, aLength);
+ sw = setParameter(key, EC_Consts.PARAMETER_R, buffer, offset, aLength);
if (sw != ISO7816.SW_NO_ERROR) return sw;
offset += rLength;
- sw = setExternalParameter(key, EC_Consts.PARAMETER_K, buffer, offset, (short) 2);
+ sw = setParameter(key, EC_Consts.PARAMETER_K, buffer, offset, (short) 2);
return sw;
}
@@ -226,7 +238,7 @@ public class ECKeyGenerator {
length = 2;
break;
default:
- length = -1;
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
} catch (CryptoException ce) {
length = -1;
diff --git a/src/applets/ECKeyTester.java b/src/applets/ECKeyTester.java
index 1031b06..d6ff6c1 100644
--- a/src/applets/ECKeyTester.java
+++ b/src/applets/ECKeyTester.java
@@ -91,13 +91,13 @@ public class ECKeyTester {
* exception reason otherwise
**/
public short testECDH_validPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
- publicKey.getW(pubkeyBuffer, pubkeyOffset);
- return testDH_validPoint(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, (short) pubkeyBuffer.length, outputBuffer, outputOffset);
+ short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
+ return testDH_validPoint(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
}
public short testECDH_invalidPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
- publicKey.getW(pubkeyBuffer, pubkeyOffset);
- return testDH_invalidPoint(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, (short) pubkeyBuffer.length, outputBuffer, outputOffset);
+ short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
+ return testDH_invalidPoint(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
}
@@ -115,13 +115,13 @@ public class ECKeyTester {
* exception reason otherwise
*/
public short testECDHC_validPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
- publicKey.getW(pubkeyBuffer, pubkeyOffset);
- return testDH_validPoint(ecdhcKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, (short) pubkeyBuffer.length, outputBuffer, outputOffset);
+ short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
+ return testDH_validPoint(ecdhcKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
}
public short testECDHC_invalidPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
- publicKey.getW(pubkeyBuffer, pubkeyOffset);
- return testDH_invalidPoint(ecdhcKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, (short) pubkeyBuffer.length, outputBuffer, outputOffset);
+ short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
+ return testDH_invalidPoint(ecdhcKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
}
/**
diff --git a/src/applets/EC_Consts.java b/src/applets/EC_Consts.java
index e39570a..d5f23d1 100644
--- a/src/applets/EC_Consts.java
+++ b/src/applets/EC_Consts.java
@@ -70,7 +70,7 @@ public class EC_Consts {
// cofactor of G
public static final short EC128_FP_K = 1;
- // secp160r1
+ // secp160r1
public static final byte[] EC160_FP_P = new byte[]{
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
@@ -1108,32 +1108,32 @@ public class EC_Consts {
switch (param) {
case PARAMETER_FP:
if (alg == KeyPair.ALG_EC_FP) {
- length = Util.arrayCopyNonAtomic(outputBuffer, outputOffset, EC_FP_P, (short) 0, (short) EC_FP_P.length);
+ length = Util.arrayCopyNonAtomic(EC_FP_P, (short) 0, outputBuffer, outputOffset, (short) EC_FP_P.length);
}
break;
case PARAMETER_F2M:
if (alg == KeyPair.ALG_EC_F2M) {
- length = Util.arrayCopyNonAtomic(outputBuffer, outputOffset, EC_F2M_F2M, (short) 0, (short) EC_F2M_F2M.length);
+ length = Util.arrayCopyNonAtomic(EC_F2M_F2M, (short) 0, outputBuffer, outputOffset, (short) EC_F2M_F2M.length);
}
break;
case PARAMETER_A:
- length = Util.arrayCopyNonAtomic(outputBuffer, outputOffset, EC_A, (short) 0, (short) EC_A.length);
+ length = Util.arrayCopyNonAtomic(EC_A, (short) 0, outputBuffer, outputOffset, (short) EC_A.length);
break;
case PARAMETER_B:
- length = Util.arrayCopyNonAtomic(outputBuffer, outputOffset, EC_B, (short) 0, (short) EC_B.length);
+ length = Util.arrayCopyNonAtomic(EC_B, (short) 0, outputBuffer, outputOffset, (short) EC_B.length);
break;
case PARAMETER_G:
length = decompressG(outputBuffer, outputOffset, EC_G_X, (short) 0, (short) EC_G_X.length, EC_G_Y, (short) 0, (short) EC_G_Y.length);
break;
case PARAMETER_R:
- length = Util.arrayCopyNonAtomic(outputBuffer, outputOffset, EC_R, (short) 0, (short) EC_R.length);
+ length = Util.arrayCopyNonAtomic(EC_R, (short) 0, outputBuffer, outputOffset, (short) EC_R.length);
break;
case PARAMETER_K:
length = 2;
Util.setShort(outputBuffer, outputOffset, EC_K);
break;
default:
- length = -1;
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
return length;
}
@@ -1169,9 +1169,9 @@ public class EC_Consts {
rngPos %= length; // make < param length
byte original = outputBuffer[rngPos];
- while (original != outputBuffer[rngPos]) {
+ do {
m_random.generateData(outputBuffer, rngPos, (short) 1);
- }
+ } while (original == outputBuffer[rngPos]);
break;
case CORRUPTION_ZERO:
Util.arrayFillNonAtomic(outputBuffer, outputOffset, length, (byte) 0);
@@ -1181,6 +1181,16 @@ public class EC_Consts {
break;
default:
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ /* //TODO implement CORRUPT_B_LASTBYTEINCREMENT somehow
+ case CORRUPT_B_LASTBYTEINCREMENT:
+ m_ramArray2[(short) (m_lenB - 1)] += 1;
+ // Make sure its not the valid byte again
+ if (m_ramArray[(short) (m_lenB - 1)] == m_ramArray2[(short) (m_lenB - 1)]) {
+ m_ramArray2[(short) (m_lenB - 1)] += 1; // if yes, increment once more
+ }
+ break;
+ }
+ */
}
return length;
}
@@ -1193,10 +1203,11 @@ public class EC_Consts {
short size = 1;
size += gxLength;
size += gyLength;
+
short offset = outputOffset;
+ outputBuffer[offset] = 0x04;
offset += 1;
- outputBuffer[offset] = 0x04;
offset = Util.arrayCopyNonAtomic(gx, gxOffset, outputBuffer, offset, gxLength);
Util.arrayCopyNonAtomic(gy, gyOffset, outputBuffer, offset, gyLength);
return size;
diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java
index 3b5b514..404bf00 100644
--- a/src/applets/SimpleECCApplet.java
+++ b/src/applets/SimpleECCApplet.java
@@ -8,7 +8,7 @@ import javacard.framework.*;
import javacard.security.*;
-public class SimpleECCApplet extends javacard.framework.Applet {
+public class SimpleECCApplet extends Applet {
// MAIN INSTRUCTION CLASS
final static byte CLA_SIMPLEECCAPPLET = (byte) 0xB0;
@@ -297,7 +297,7 @@ public class SimpleECCApplet extends javacard.framework.Applet {
ecPubKey = ecKeyGenerator.getPublicKey();
ecPrivKey = ecKeyGenerator.getPrivateKey();
if (sw == ISO7816.SW_NO_ERROR) {
- sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 1);
+ sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 0);
}
}
Util.setShort(buffer, bufferOffset, sw);
@@ -427,7 +427,7 @@ public class SimpleECCApplet extends javacard.framework.Applet {
return (short) (outputOffset - startOffset);
}
- // setExternalParam -> forall in {field, a, b, g, r, k}
+ // setExternalCurve
buffer[outputOffset] = ECTEST_SET_EXTERNALCURVE;
outputOffset++;
sw = ecKeyGenerator.setExternalCurve(ECKeyGenerator.KEY_BOTH, keyClass, buffer, bufferOffset, fieldLength, aLength, bLength, gxLength, gyLength, rLength);
@@ -598,10 +598,18 @@ public class SimpleECCApplet extends javacard.framework.Applet {
} else {
testFlags = 0;
}
+
+ if (ecPubKey == null || ecPrivKey == null) {
+ ecKeyGenerator.generatePair();
+ ecPrivKey = ecKeyGenerator.getPrivateKey();
+ ecPubKey = ecKeyGenerator.getPublicKey();
+ }
}
Util.setShort(buffer, bufferOffset, sw);
bufferOffset += 2;
+
+
//
// 2. Set invalid custom curve (many times)
//
@@ -620,6 +628,8 @@ public class SimpleECCApplet extends javacard.framework.Applet {
bufferOffset = startOffset;
}
+ ecPubKey.getB(m_ramArray2, (short) 0); //store valid B
+
// set invalid curve
buffer[bufferOffset] = ECTEST_SET_INVALIDCURVE;
bufferOffset++;
@@ -631,23 +641,13 @@ public class SimpleECCApplet extends javacard.framework.Applet {
// CORRUPTION_ONEBYTERANDOM = 0x04, one random byte randomly changed
// CORRUPTION_ZERO = 0x05, parameter competely zero
// CORRUPTION_ONE = 0x06, parameter completely one
- sw = ecKeyGenerator.setCustomInvalidCurve(keyClass, keyClass, ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_B, corruptionType, m_ramArray, (short) 0);
+ sw = ecKeyGenerator.setCustomInvalidCurve(keyClass, keyLen, ECKeyGenerator.KEY_BOTH, EC_Consts.PARAMETER_B, corruptionType, m_ramArray, (short) 0);
Util.setShort(buffer, bufferOffset, sw);
bufferOffset += 2;
if (sw != ISO7816.SW_NO_ERROR) {
// if we reach this line, we are interested in value of B that caused incorrect response
break; // stop execution, return B
}
- /* //TODO implement CORRUPT_B_LASTBYTEINCREMENT somehow
- case CORRUPT_B_LASTBYTEINCREMENT:
- m_ramArray2[(short) (m_lenB - 1)] += 1;
- // Make sure its not the valid byte again
- if (m_ramArray[(short) (m_lenB - 1)] == m_ramArray2[(short) (m_lenB - 1)]) {
- m_ramArray2[(short) (m_lenB - 1)] += 1; // if yes, increment once more
- }
- break;
- }
- */
// Gen key pair with invalid curve
@@ -668,6 +668,7 @@ public class SimpleECCApplet extends javacard.framework.Applet {
sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 0);
m_lenB = ecPubKey.getB(m_ramArray2, (short) 0); //store B
+ //TODO: note, according to the previous version of this method, sw should get appended to the buffer only if sw != SW_NO_ERROR
Util.setShort(buffer, bufferOffset, sw);
bufferOffset += 2;
break; //stop execution, return B
diff --git a/src/simpleapdu/SimpleAPDU.java b/src/simpleapdu/SimpleAPDU.java
index 7401509..ead8fb7 100644
--- a/src/simpleapdu/SimpleAPDU.java
+++ b/src/simpleapdu/SimpleAPDU.java
@@ -106,7 +106,7 @@ public class SimpleAPDU {
if (cardManager.ConnectToCard()) {
testSupportECAll(cardManager);
-
+ //
// Test setting invalid parameter B of curve
byte[] testAPDU = Arrays.clone(TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB);
//testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_LASTBYTEINCREMENT);
--
cgit v1.3.1
From 1a0d0b14ff7720b98ffdda4651bf1b58d34203bd Mon Sep 17 00:00:00 2001
From: J08nY
Date: Sat, 26 Nov 2016 19:24:37 +0100
Subject: Added tests for non-prime p, ECDSA into the main test suite. Also
fixed ECDSA test.
---
!uploader/simpleECC.cap | Bin 9854 -> 14367 bytes
dist/SimpleAPDU.jar | Bin 52783 -> 3071803 bytes
src/applets/ECKeyTester.java | 14 ++++-----
src/applets/SimpleECCApplet.java | 61 +++++++++++++++++++++++++++++++++++----
src/simpleapdu/SimpleAPDU.java | 52 ++++++++++++---------------------
5 files changed, 79 insertions(+), 48 deletions(-)
(limited to 'src/applets/SimpleECCApplet.java')
diff --git a/!uploader/simpleECC.cap b/!uploader/simpleECC.cap
index 9d36664..7d019f4 100644
Binary files a/!uploader/simpleECC.cap and b/!uploader/simpleECC.cap differ
diff --git a/dist/SimpleAPDU.jar b/dist/SimpleAPDU.jar
index 3a13dc6..88b4ab9 100644
Binary files a/dist/SimpleAPDU.jar and b/dist/SimpleAPDU.jar differ
diff --git a/src/applets/ECKeyTester.java b/src/applets/ECKeyTester.java
index d6ff6c1..acfb64e 100644
--- a/src/applets/ECKeyTester.java
+++ b/src/applets/ECKeyTester.java
@@ -143,16 +143,12 @@ public class ECKeyTester {
short result = ISO7816.SW_NO_ERROR;
try {
ecdsaSignature.init(signKey, Signature.MODE_SIGN);
-
short sigLength = ecdsaSignature.sign(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset);
- if (sigLength != 20) { // per javacard.security.Signature an ALG_ECDSA_SHA should be 20 bytes.
- result = SimpleECCApplet.SW_SIG_LENGTH_MISMATCH;
- } else {
- ecdsaSignature.init(verifyKey, Signature.MODE_VERIFY);
- boolean correct = ecdsaSignature.verify(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset, sigLength);
- if (!correct) {
- result = SimpleECCApplet.SW_SIG_VERIFY_FAIL;
- }
+
+ ecdsaSignature.init(verifyKey, Signature.MODE_VERIFY);
+ boolean correct = ecdsaSignature.verify(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset, sigLength);
+ if (!correct) {
+ result = SimpleECCApplet.SW_SIG_VERIFY_FAIL;
}
} catch (CryptoException ce) {
result = ce.getReason();
diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java
index 8ec9e67..9901aee 100644
--- a/src/applets/SimpleECCApplet.java
+++ b/src/applets/SimpleECCApplet.java
@@ -53,6 +53,8 @@ public class SimpleECCApplet extends Applet {
public final static byte ECTEST_SET_EXTERNALCURVE = (byte) 0xcb;
public final static byte ECTEST_GENERATE_KEYPAIR_EXTERNALCURVE = (byte) 0xcc;
public final static byte ECTEST_ECDSA_SIGNATURE = (byte) 0xcd;
+ public final static byte ECTEST_SET_INVALIDFIELD = (byte) 0xce;
+ public final static byte ECTEST_GENERATE_KEYPAIR_INVALIDFIELD = (byte) 0xcf;
public final static short FLAG_ECTEST_ALLOCATE_KEYPAIR = (short) 0x0001;
public final static short FLAG_ECTEST_GENERATE_KEYPAIR_DEFCURVE = (short) 0x0002;
@@ -62,15 +64,17 @@ public class SimpleECCApplet extends Applet {
public final static short FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE = (short) 0x0020;
public final static short FLAG_ECTEST_ECDH_AGREEMENT_VALID_POINT = (short) 0x0040;
public final static short FLAG_ECTEST_ECDH_AGREEMENT_INVALID_POINT = (short) 0x0080;
+ public final static short FLAG_ECTEST_ECDSA_SIGNATURE = (short) 0x0100;
+ public final static short FLAG_ECTEST_SET_INVALIDFIELD = (short) 0x0200;
+ public final static short FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDFIELD = (short) 0x0400;
- public final static short FLAG_ECTEST_ALL = (short) 0x00ff;
+ public final static short FLAG_ECTEST_ALL = (short) 0xffff;
public final static short SW_SKIPPED = (short) 0x0ee1;
public final static short SW_KEYPAIR_GENERATED_INVALID = (short) 0x0ee2;
public final static short SW_INVALID_CORRUPTION_TYPE = (short) 0x0ee3;
- public final static short SW_SIG_LENGTH_MISMATCH = (short) 0xee4;
- public final static short SW_SIG_VERIFY_FAIL = (short) 0xee5;
+ public final static short SW_SIG_VERIFY_FAIL = (short) 0xee4;
/*
public static final byte[] EC192_FP_PUBLICW = new byte[]{
(byte) 0x04, (byte) 0xC9, (byte) 0xC0, (byte) 0xED, (byte) 0xFB, (byte) 0x27,
@@ -325,7 +329,25 @@ public class SimpleECCApplet extends Applet {
bufferOffset += 2;
//
- // 7. Set invalid custom curve
+ // 7. ECDSA test
+ //
+ buffer[bufferOffset] = ECTEST_ECDSA_SIGNATURE;
+ bufferOffset++;
+ sw = SW_SKIPPED;
+ if ((testFlags & FLAG_ECTEST_ECDSA_SIGNATURE) != (short) 0) {
+ sw = ecKeyGenerator.generatePair();
+ ecPubKey = ecKeyGenerator.getPublicKey();
+ ecPrivKey = ecKeyGenerator.getPrivateKey();
+ if (sw == ISO7816.SW_NO_ERROR) {
+ sw = ecKeyTester.testECDSA(ecPrivKey, ecPubKey, m_ramArray2, (short) 0, (short) m_ramArray2.length, m_ramArray, (short) 0);
+ }
+
+ }
+ Util.setShort(buffer, bufferOffset, sw);
+ bufferOffset += 2;
+
+ //
+ // 8. Set invalid custom curve
//
buffer[bufferOffset] = ECTEST_SET_INVALIDCURVE;
bufferOffset++;
@@ -341,7 +363,7 @@ public class SimpleECCApplet extends Applet {
bufferOffset += 2;
//
- // 8. Generate keypair with invalid custom curve
+ // 9. Generate keypair with invalid custom curve
//
buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE;
bufferOffset++;
@@ -352,6 +374,35 @@ public class SimpleECCApplet extends Applet {
Util.setShort(buffer, bufferOffset, sw);
bufferOffset += 2;
+ //
+ // 10. Set invalid field
+ //
+ buffer[bufferOffset] = ECTEST_SET_INVALIDFIELD;
+ bufferOffset++;
+ sw = SW_SKIPPED;
+ if ((testFlags & FLAG_ECTEST_SET_INVALIDFIELD) != (short) 0) {
+ if (keyClass == KeyPair.ALG_EC_FP)
+ sw = ecKeyGenerator.setCustomInvalidCurve(keyClass, keyLen, ECKeyGenerator.KEY_BOTH, EC_Consts.PARAMETER_FP, EC_Consts.CORRUPTION_FULLRANDOM, m_ramArray, (short) 0);
+ else
+ sw = ecKeyGenerator.setCustomInvalidCurve(keyClass, keyLen, ECKeyGenerator.KEY_BOTH, EC_Consts.PARAMETER_F2M, EC_Consts.CORRUPTION_FULLRANDOM, m_ramArray, (short) 0);
+
+ if (sw != ISO7816.SW_NO_ERROR) {
+ testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDFIELD;
+ }
+ }
+ Util.setShort(buffer, bufferOffset, sw);
+ bufferOffset += 2;
+
+ // 11. Generate key with invalid field
+ buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_INVALIDFIELD;
+ bufferOffset++;
+ sw = SW_SKIPPED;
+ if ((testFlags & FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDFIELD) != (short) 0) {
+ sw = ecKeyGenerator.generatePair();
+ }
+ Util.setShort(buffer, bufferOffset, sw);
+ bufferOffset += 2;
+
return (short) (bufferOffset - baseOffset);
}
diff --git a/src/simpleapdu/SimpleAPDU.java b/src/simpleapdu/SimpleAPDU.java
index 8775217..77478a0 100644
--- a/src/simpleapdu/SimpleAPDU.java
+++ b/src/simpleapdu/SimpleAPDU.java
@@ -107,9 +107,8 @@ public class SimpleAPDU {
}
-
public static void main(String[] args) throws FileNotFoundException, IOException {
-
+ //parse cli args. Should be replaced with some cli parsing library code in the future.
boolean genKeys = false;
int genAmount = 0;
boolean testAll = false;
@@ -120,7 +119,7 @@ public class SimpleAPDU {
if (args.length >= i + 1) {
try {
genAmount = Integer.parseInt(args[i + 1]);
- }catch (NumberFormatException ignored)Â {
+ }catch (NumberFormatException ignored) {
//is another param, genAmount = 0 by default
genAmount = 0;
}
@@ -145,36 +144,21 @@ public class SimpleAPDU {
try {
if (testAll) {
if (cardManager.ConnectToCard()) {
- byte[] testAPDU2 = Arrays.copyOf(TESTECSUPPORT_GIVENALG, TESTECSUPPORT_GIVENALG.length);
- testAPDU2[TESTECSUPPORT_ALG_OFFSET] = KeyPair.ALG_EC_FP;
- setShort(testAPDU2, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 384);
- testSupportECGivenAlg(testAPDU2, cardManager);
-
+ // Test all default curves for both fields
testSupportECAll(cardManager);
- //
+
// Test setting invalid parameter B of curve
byte[] testAPDU = Arrays.copyOf(TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB, TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB.length);
- //testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_LASTBYTEINCREMENT);
testFPkeyGen_setCorruptionType(testAPDU, EC_Consts.CORRUPTION_ONEBYTERANDOM);
- //testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_FULLRANDOM);
testFPkeyGen_setNumRepeats(testAPDU, (short) 10);
testFPkeyGen_rewindOnSuccess(testAPDU, true);
+
ReconnnectToCard();
ResponseAPDU resp_fp_keygen = cardManager.sendAPDU(testAPDU);
ResponseAPDU resp_keygen_params = cardManager.sendAPDU(TESTECSUPPORTALL_LASTUSEDPARAMS);
PrintECKeyGenInvalidCurveB(resp_fp_keygen);
PrintECKeyGenInvalidCurveB_lastUserParams(resp_keygen_params);
- /*
- // Test support for different types of curves
- ReconnnectToCard();
- ResponseAPDU resp_fp = cardManager.sendAPDU(TESTECSUPPORTALL_FP);
- ReconnnectToCard();
- ResponseAPDU resp_f2m = cardManager.sendAPDU(TESTECSUPPORTALL_F2M);
- PrintECSupport(resp_fp);
- PrintECSupport(resp_f2m);
- */
-
cardManager.DisconnectFromCard();
} else {
m_SystemOutLogger.println("Failed to connect to card");
@@ -275,9 +259,6 @@ public class SimpleAPDU {
if (code == SimpleECCApplet.SW_INVALID_CORRUPTION_TYPE) {
codeStr = "SW_INVALID_CORRUPTION_TYPE";
}
- if (code == SimpleECCApplet.SW_SIG_LENGTH_MISMATCH) {
- codeStr = "SW_SIG_LENGTH_MISMATCH";
- }
if (code == SimpleECCApplet.SW_SIG_VERIFY_FAIL) {
codeStr = "SW_SIG_VERIFY_FAIL";
}
@@ -286,7 +267,7 @@ public class SimpleAPDU {
}
enum ExpResult {
- SHOULD_SUCCEDD,
+ SHOULD_SUCCEED,
MAY_FAIL,
MUST_FAIL
}
@@ -307,7 +288,7 @@ public class SimpleAPDU {
if ((expRes == ExpResult.MUST_FAIL) && (resCode == ISO7816.SW_NO_ERROR)) {
bHiglight = true;
}
- if ((expRes == ExpResult.SHOULD_SUCCEDD) && (resCode != ISO7816.SW_NO_ERROR)) {
+ if ((expRes == ExpResult.SHOULD_SUCCEED) && (resCode != ISO7816.SW_NO_ERROR)) {
bHiglight = true;
}
if (bHiglight) {
@@ -341,14 +322,17 @@ public class SimpleAPDU {
m_SystemOutLogger.println(String.format("%-53s%d bits", "EC key length (bits):", keyLen));
bufferOffset += 2;
- bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
+ bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
bufferOffset = VerifyPrintResult("Generate key with def curve (fails if no def):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_DEFCURVE, buffer, bufferOffset, ExpResult.MAY_FAIL);
- bufferOffset = VerifyPrintResult("Set valid custom curve:", SimpleECCApplet.ECTEST_SET_VALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
- bufferOffset = VerifyPrintResult("Generate key with valid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
- bufferOffset = VerifyPrintResult("ECDH agreement with valid point:", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_VALID_POINT, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
+ bufferOffset = VerifyPrintResult("Set valid custom curve:", SimpleECCApplet.ECTEST_SET_VALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
+ bufferOffset = VerifyPrintResult("Generate key with valid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
+ bufferOffset = VerifyPrintResult("ECDH agreement with valid point:", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_VALID_POINT, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
bufferOffset = VerifyPrintResult("ECDH agreement with invalid point (fail is good):", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_INVALID_POINT, buffer, bufferOffset, ExpResult.MUST_FAIL);
+ bufferOffset = VerifyPrintResult("ECDSA signature on random data:", SimpleECCApplet.ECTEST_ECDSA_SIGNATURE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
bufferOffset = VerifyPrintResult("Set invalid custom curve (may fail):", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset, ExpResult.MAY_FAIL);
bufferOffset = VerifyPrintResult("Generate key with invalid curve (fail is good):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE, buffer, bufferOffset, ExpResult.MUST_FAIL);
+ bufferOffset = VerifyPrintResult("Set invalid field (may fail):", SimpleECCApplet.ECTEST_SET_INVALIDFIELD, buffer, bufferOffset, ExpResult.MAY_FAIL);
+ bufferOffset = VerifyPrintResult("Generate key with invalid field (fail si good):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDFIELD, buffer, bufferOffset, ExpResult.MUST_FAIL);
m_SystemOutLogger.println();
}
@@ -380,15 +364,15 @@ public class SimpleAPDU {
bufferOffset += 2;
m_SystemOutLogger.println(String.format("%-53s%d times", "Executed repeats before unexpected error: ", numRepeats));
- bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
+ bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
while (bufferOffset < buffer.length) {
- bufferOffset = VerifyPrintResult("Set invalid custom curve:", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
+ bufferOffset = VerifyPrintResult("Set invalid custom curve:", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
bufferOffset = VerifyPrintResult("Generate key with invalid curve (fail is good):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE, buffer, bufferOffset, ExpResult.MUST_FAIL);
if (buffer[bufferOffset] == SimpleECCApplet.ECTEST_DH_GENERATESECRET) {
bufferOffset = VerifyPrintResult("ECDH agreement with invalid point (fail is good):", SimpleECCApplet.ECTEST_DH_GENERATESECRET, buffer, bufferOffset, ExpResult.MUST_FAIL);
}
- bufferOffset = VerifyPrintResult("Set valid custom curve:", SimpleECCApplet.ECTEST_SET_VALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
- bufferOffset = VerifyPrintResult("Generate key with valid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
+ bufferOffset = VerifyPrintResult("Set valid custom curve:", SimpleECCApplet.ECTEST_SET_VALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
+ bufferOffset = VerifyPrintResult("Generate key with valid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
}
m_SystemOutLogger.println();
--
cgit v1.3.1
From f4b67923063ca35ae1405d701b461bfda0c10515 Mon Sep 17 00:00:00 2001
From: J08nY
Date: Mon, 28 Nov 2016 22:34:50 +0100
Subject: Added test for small public key degree in ECDH
---
!uploader/simpleECC.cap | Bin 14367 -> 18068 bytes
dist/SimpleAPDU.jar | Bin 3071803 -> 3078811 bytes
src/applets/ECKeyGenerator.java | 31 +++++++++++++------
src/applets/EC_Consts.java | 50 ++++++++++++++++++++++++------
src/applets/SimpleECCApplet.java | 64 ++++++++++++++++++++++++++++++++++-----
src/simpleapdu/SimpleAPDU.java | 19 ++++++++++--
6 files changed, 134 insertions(+), 30 deletions(-)
(limited to 'src/applets/SimpleECCApplet.java')
diff --git a/!uploader/simpleECC.cap b/!uploader/simpleECC.cap
index 7d019f4..d9c360b 100644
Binary files a/!uploader/simpleECC.cap and b/!uploader/simpleECC.cap differ
diff --git a/dist/SimpleAPDU.jar b/dist/SimpleAPDU.jar
index 88b4ab9..9bb85ff 100644
Binary files a/dist/SimpleAPDU.jar and b/dist/SimpleAPDU.jar differ
diff --git a/src/applets/ECKeyGenerator.java b/src/applets/ECKeyGenerator.java
index e9bdfa6..c4b71c0 100644
--- a/src/applets/ECKeyGenerator.java
+++ b/src/applets/ECKeyGenerator.java
@@ -73,39 +73,39 @@ public class ECKeyGenerator {
if (sw != ISO7816.SW_NO_ERROR) return sw;
//go through all params
- byte param = EC_Consts.PARAMETER_A;
- while (param > 0) {
+ short param = EC_Consts.PARAMETER_A;
+ while (param <= EC_Consts.PARAMETER_K) {
length = EC_Consts.getCurveParameter(curve, param, buffer, offset);
sw = setParameter(KEY_BOTH, param, buffer, offset, length);
if (sw != ISO7816.SW_NO_ERROR) break;
- param = (byte) (param << 1);
+ param = (short) (param << 1);
}
return sw;
}
- public short setCustomInvalidCurve(short keyClass, short keyLength, byte key, byte param, short corruptionType, byte[] buffer, short offset) {
+ public short setCustomInvalidCurve(short keyClass, short keyLength, byte key, short param, short corruptionType, byte[] buffer, short offset) {
return setCustomInvalidCurve(EC_Consts.getCurve(keyClass, keyLength), key, param, corruptionType, buffer, offset);
}
- public short setCustomInvalidCurve(byte curve, byte key, byte param, short corruptionType, byte[] buffer, short offset) {
+ public short setCustomInvalidCurve(byte curve, byte key, short param, short corruptionType, byte[] buffer, short offset) {
short sw = setCustomCurve(curve, buffer, offset);
if (sw != ISO7816.SW_NO_ERROR) return sw;
//go through param bit by bit, and invalidate all selected params
- byte paramMask = 0x01;
- while (paramMask > 0) {
- byte masked = (byte) (paramMask & param);
+ short paramMask = 0x01;
+ while (paramMask <= EC_Consts.PARAMETER_K) {
+ short masked = (short) (paramMask & param);
if (masked != 0) {
short length = EC_Consts.getCorruptCurveParameter(curve, masked, buffer, offset, corruptionType);
sw = setParameter(key, masked, buffer, offset, length);
if (sw != ISO7816.SW_NO_ERROR) return sw;
}
- paramMask = (byte) (paramMask << 1);
+ paramMask = (short) (paramMask << 1);
}
return sw;
}
- public short setParameter(byte key, byte param, byte[] data, short offset, short length) {
+ public short setParameter(byte key, short param, byte[] data, short offset, short length) {
short result = ISO7816.SW_NO_ERROR;
try {
switch (param) {
@@ -160,6 +160,12 @@ public class ECKeyGenerator {
}
break;
}
+ case EC_Consts.PARAMETER_S:
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setS(data, offset, length);
+ break;
+ case EC_Consts.PARAMETER_W:
+ if ((key & KEY_PUBLIC) != 0) ecPublicKey.setW(data, offset, length);
+ break;
default: {
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
@@ -237,6 +243,11 @@ public class ECKeyGenerator {
if ((key & KEY_PRIVATE) != 0) Util.setShort(outputBuffer, outputOffset, ecPrivateKey.getK());
length = 2;
break;
+ case EC_Consts.PARAMETER_S:
+ if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getS(outputBuffer, outputOffset);
+ break;
+ case EC_Consts.PARAMETER_W:
+ if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getW(outputBuffer, outputOffset);
default:
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
diff --git a/src/applets/EC_Consts.java b/src/applets/EC_Consts.java
index 24d854c..bcf2b14 100644
--- a/src/applets/EC_Consts.java
+++ b/src/applets/EC_Consts.java
@@ -21,14 +21,16 @@ public class EC_Consts {
private static byte[] EC_F2M_F2M = null; //[short i1, short i2, short i3], f = x^m + x^i1 + x^i2 + x^i3 + 1
- public static final byte PARAMETER_FP = 0x01;
- public static final byte PARAMETER_F2M = 0x02;
+ public static final short PARAMETER_FP = 0x0001;
+ public static final short PARAMETER_F2M = 0x0002;
- public static final byte PARAMETER_A = 0x04;
- public static final byte PARAMETER_B = 0x08;
- public static final byte PARAMETER_G = 0x10;
- public static final byte PARAMETER_R = 0x20;
- public static final byte PARAMETER_K = 0x40;
+ public static final short PARAMETER_A = 0x0004;
+ public static final short PARAMETER_B = 0x0008;
+ public static final short PARAMETER_G = 0x0010;
+ public static final short PARAMETER_R = 0x0020;
+ public static final short PARAMETER_K = 0x0040;
+ public static final short PARAMETER_S = 0x0080;
+ public static final short PARAMETER_W = 0x0100;
public static RandomData m_random = null;
@@ -1658,10 +1660,38 @@ public class EC_Consts {
} else {
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
- return 0; //will not be reached
+ return 0;
}
- public static short getCurveParameter(byte curve, byte param, byte[] outputBuffer, short outputOffset) {
+ public static byte getAnomalousCurve(short keyClass, short keyLength) {
+ if (keyClass == KeyPair.ALG_EC_FP) {
+ switch (keyLength) {
+ case (short) 128:
+ return CURVE_sp128;
+ case (short) 160:
+ return CURVE_sp160;
+ case (short) 192:
+ return CURVE_sp192;
+ case (short) 224:
+ return CURVE_sp224;
+ case (short) 256:
+ return CURVE_sp256;
+ case (short) 384:
+ return CURVE_sp384;
+ case (short) 521:
+ return CURVE_sp521;
+ default:
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
+ } else if (keyClass == KeyPair.ALG_EC_F2M) {
+ return 0;
+ } else {
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
+ return 0;
+ }
+
+ public static short getCurveParameter(byte curve, short param, byte[] outputBuffer, short outputOffset) {
byte alg = getCurveType(curve);
switch (curve) {
case CURVE_secp128r1: {
@@ -1905,7 +1935,7 @@ public class EC_Consts {
return length;
}
- public static short getCorruptCurveParameter(byte curve, byte param, byte[] outputBuffer, short outputOffset, short corruptionType) {
+ public static short getCorruptCurveParameter(byte curve, short param, byte[] outputBuffer, short outputOffset, short corruptionType) {
short length = getCurveParameter(curve, param, outputBuffer, outputOffset);
if (length <= 0) {
return length;
diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java
index 9901aee..8cc4237 100644
--- a/src/applets/SimpleECCApplet.java
+++ b/src/applets/SimpleECCApplet.java
@@ -53,8 +53,11 @@ public class SimpleECCApplet extends Applet {
public final static byte ECTEST_SET_EXTERNALCURVE = (byte) 0xcb;
public final static byte ECTEST_GENERATE_KEYPAIR_EXTERNALCURVE = (byte) 0xcc;
public final static byte ECTEST_ECDSA_SIGNATURE = (byte) 0xcd;
- public final static byte ECTEST_SET_INVALIDFIELD = (byte) 0xce;
- public final static byte ECTEST_GENERATE_KEYPAIR_INVALIDFIELD = (byte) 0xcf;
+ public final static byte ECTEST_SET_ANOMALOUSCURVE = (byte) 0xce;
+ public final static byte ECTEST_GENERATE_KEYPAIR_ANOMALOUSCURVE = (byte) 0xcf;
+ public final static byte ECTEST_ECDH_AGREEMENT_SMALL_DEGREE_POINT = (byte) 0xd0;
+ public final static byte ECTEST_SET_INVALIDFIELD = (byte) 0xd1;
+ public final static byte ECTEST_GENERATE_KEYPAIR_INVALIDFIELD = (byte) 0xd2;
public final static short FLAG_ECTEST_ALLOCATE_KEYPAIR = (short) 0x0001;
public final static short FLAG_ECTEST_GENERATE_KEYPAIR_DEFCURVE = (short) 0x0002;
@@ -65,8 +68,11 @@ public class SimpleECCApplet extends Applet {
public final static short FLAG_ECTEST_ECDH_AGREEMENT_VALID_POINT = (short) 0x0040;
public final static short FLAG_ECTEST_ECDH_AGREEMENT_INVALID_POINT = (short) 0x0080;
public final static short FLAG_ECTEST_ECDSA_SIGNATURE = (short) 0x0100;
- public final static short FLAG_ECTEST_SET_INVALIDFIELD = (short) 0x0200;
- public final static short FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDFIELD = (short) 0x0400;
+ public final static short FLAG_ECTEST_SET_ANOMALOUSCURVE = (short) 0x0200;
+ public final static short FLAG_ECTEST_GENERATE_KEYPAIR_ANOMALOUSCUVE = (short) 0x0400;
+ public final static short FLAG_ECTEST_ECDH_AGREEMENT_SMALL_DEGREE_POINT = (short) 0x0800;
+ public final static short FLAG_ECTEST_SET_INVALIDFIELD = (short) 0x1000;
+ public final static short FLAG_ECTEST_GENERATE_KEYPAIR_INVALIDFIELD = (short) 0x2000;
public final static short FLAG_ECTEST_ALL = (short) 0xffff;
@@ -341,13 +347,55 @@ public class SimpleECCApplet extends Applet {
if (sw == ISO7816.SW_NO_ERROR) {
sw = ecKeyTester.testECDSA(ecPrivKey, ecPubKey, m_ramArray2, (short) 0, (short) m_ramArray2.length, m_ramArray, (short) 0);
}
+ }
+ Util.setShort(buffer, bufferOffset, sw);
+ bufferOffset += 2;
+
+ //
+ // 8. Set anomalous custom curve
+ //
+ buffer[bufferOffset] = ECTEST_SET_ANOMALOUSCURVE;
+ bufferOffset++;
+ sw = SW_SKIPPED;
+ if ((testFlags & FLAG_ECTEST_SET_ANOMALOUSCURVE) != (short) 0) {
+ sw = ecKeyGenerator.setCustomCurve(EC_Consts.getAnomalousCurve(keyClass, keyLen), m_ramArray, (short) 0);
+ if (sw != ISO7816.SW_NO_ERROR) {
+ testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_ANOMALOUSCUVE;
+ }
+ }
+ Util.setShort(buffer, bufferOffset, sw);
+ bufferOffset += 2;
+
+ //
+ // 9. Generate keypair with anomalous custom curve
+ //
+
+ buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_ANOMALOUSCURVE;
+ bufferOffset++;
+ sw = SW_SKIPPED;
+ if ((testFlags & FLAG_ECTEST_GENERATE_KEYPAIR_ANOMALOUSCUVE) != (short) 0) {
+ sw = ecKeyGenerator.generatePair();
+ }
+ Util.setShort(buffer, bufferOffset, sw);
+ bufferOffset += 2;
+ //
+ // 10. Test small degree pubkey
+ //
+
+ buffer[bufferOffset] = ECTEST_ECDH_AGREEMENT_SMALL_DEGREE_POINT;
+ bufferOffset++;
+ sw = SW_SKIPPED;
+ if ((testFlags & FLAG_ECTEST_ECDH_AGREEMENT_SMALL_DEGREE_POINT) != (short) 0) {
+ ecPubKey = ecKeyGenerator.getPublicKey();
+ ecPrivKey = ecKeyGenerator.getPrivateKey();
+ sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 1);
}
Util.setShort(buffer, bufferOffset, sw);
bufferOffset += 2;
//
- // 8. Set invalid custom curve
+ // 11. Set invalid custom curve
//
buffer[bufferOffset] = ECTEST_SET_INVALIDCURVE;
bufferOffset++;
@@ -363,7 +411,7 @@ public class SimpleECCApplet extends Applet {
bufferOffset += 2;
//
- // 9. Generate keypair with invalid custom curve
+ // 12. Generate keypair with invalid custom curve
//
buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE;
bufferOffset++;
@@ -375,7 +423,7 @@ public class SimpleECCApplet extends Applet {
bufferOffset += 2;
//
- // 10. Set invalid field
+ // 13. Set invalid field
//
buffer[bufferOffset] = ECTEST_SET_INVALIDFIELD;
bufferOffset++;
@@ -393,7 +441,7 @@ public class SimpleECCApplet extends Applet {
Util.setShort(buffer, bufferOffset, sw);
bufferOffset += 2;
- // 11. Generate key with invalid field
+ // 14. Generate key with invalid field
buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_INVALIDFIELD;
bufferOffset++;
sw = SW_SKIPPED;
diff --git a/src/simpleapdu/SimpleAPDU.java b/src/simpleapdu/SimpleAPDU.java
index 77478a0..44bf302 100644
--- a/src/simpleapdu/SimpleAPDU.java
+++ b/src/simpleapdu/SimpleAPDU.java
@@ -5,6 +5,7 @@ import applets.SimpleECCApplet;
import javacard.framework.ISO7816;
import javacard.security.CryptoException;
import javacard.security.KeyPair;
+import sun.java2d.pipe.SpanShapeRenderer;
import javax.smartcardio.ResponseAPDU;
import java.io.FileNotFoundException;
@@ -73,6 +74,7 @@ public class SimpleAPDU {
static void testSupportECGivenAlg(byte[] apdu, CardMngr cardManager) throws Exception {
ReconnnectToCard();
ResponseAPDU resp = cardManager.sendAPDU(apdu);
+ //byte[] resp = cardManager.sendAPDUSimulator(apdu);
PrintECSupport(resp);
}
@@ -143,7 +145,11 @@ public class SimpleAPDU {
try {
if (testAll) {
+ //byte[] installData = new byte[10];
+ //byte[] AID = {(byte) 0x4C, (byte) 0x61, (byte) 0x62, (byte) 0x61, (byte) 0x6B, (byte) 0x41, (byte) 0x70, (byte) 0x70, (byte) 0x6C, (byte) 0x65, (byte) 0x74};
+ //cardManager.prepareLocalSimulatorApplet(AID, installData, SimpleECCApplet.class);
if (cardManager.ConnectToCard()) {
+
// Test all default curves for both fields
testSupportECAll(cardManager);
@@ -301,7 +307,10 @@ public class SimpleAPDU {
}
static void PrintECSupport(ResponseAPDU resp) {
- byte[] buffer = resp.getData();
+ PrintECSupport(resp.getData());
+ }
+
+ static void PrintECSupport(byte[] buffer) {
m_SystemOutLogger.println();
m_SystemOutLogger.println("### Test for support and with valid and invalid EC curves");
@@ -329,6 +338,9 @@ public class SimpleAPDU {
bufferOffset = VerifyPrintResult("ECDH agreement with valid point:", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_VALID_POINT, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
bufferOffset = VerifyPrintResult("ECDH agreement with invalid point (fail is good):", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_INVALID_POINT, buffer, bufferOffset, ExpResult.MUST_FAIL);
bufferOffset = VerifyPrintResult("ECDSA signature on random data:", SimpleECCApplet.ECTEST_ECDSA_SIGNATURE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
+ bufferOffset = VerifyPrintResult("Set anomalous custom curve (may fail):", SimpleECCApplet.ECTEST_SET_ANOMALOUSCURVE, buffer, bufferOffset, ExpResult.MAY_FAIL);
+ bufferOffset = VerifyPrintResult("Generate key with anomalous curve (may fail):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_ANOMALOUSCURVE, buffer, bufferOffset, ExpResult.MAY_FAIL);
+ bufferOffset = VerifyPrintResult("ECDH agreement with small order point (fail is good):", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_SMALL_DEGREE_POINT, buffer, bufferOffset, ExpResult.MUST_FAIL);
bufferOffset = VerifyPrintResult("Set invalid custom curve (may fail):", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset, ExpResult.MAY_FAIL);
bufferOffset = VerifyPrintResult("Generate key with invalid curve (fail is good):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE, buffer, bufferOffset, ExpResult.MUST_FAIL);
bufferOffset = VerifyPrintResult("Set invalid field (may fail):", SimpleECCApplet.ECTEST_SET_INVALIDFIELD, buffer, bufferOffset, ExpResult.MAY_FAIL);
@@ -339,7 +351,10 @@ public class SimpleAPDU {
}
static void PrintECKeyGenInvalidCurveB(ResponseAPDU resp) {
- byte[] buffer = resp.getData();
+ PrintECKeyGenInvalidCurveB(resp.getData());
+ }
+
+ static void PrintECKeyGenInvalidCurveB(byte[] buffer) {
m_SystemOutLogger.println();
m_SystemOutLogger.println("### Test for computation with invalid parameter B for EC curve");
--
cgit v1.3.1
From 022f37b399583053fc256b9fcab397f828309ebf Mon Sep 17 00:00:00 2001
From: J08nY
Date: Mon, 28 Nov 2016 23:21:25 +0100
Subject: fixed small pubkey order test, this times out on some
cards(SCARD_E_NOT_TRANSACTED) yet some cards run fine and report illegal
value.
---
src/applets/EC_Consts.java | 67 ++++++++++++++++++++++------------------
src/applets/SimpleECCApplet.java | 15 +++++----
2 files changed, 46 insertions(+), 36 deletions(-)
(limited to 'src/applets/SimpleECCApplet.java')
diff --git a/src/applets/EC_Consts.java b/src/applets/EC_Consts.java
index bcf2b14..49eb59f 100644
--- a/src/applets/EC_Consts.java
+++ b/src/applets/EC_Consts.java
@@ -16,8 +16,9 @@ public class EC_Consts {
private static byte[] EC_R = null; //n
private static short EC_K = 1; //h
- private static byte[] EC_P_X = null; //Pubkey[x,y]
- private static byte[] EC_P_Y = null;
+ private static byte[] EC_W_X = null; //Pubkey[x,y]
+ private static byte[] EC_W_Y = null;
+ private static byte[] EC_S = null; //Private
private static byte[] EC_F2M_F2M = null; //[short i1, short i2, short i3], f = x^m + x^i1 + x^i2 + x^i3 + 1
@@ -962,14 +963,14 @@ public class EC_Consts {
public static final short ECSP128_FP_K = 1;
- public static final byte[] ECSP128_FP_P_X = {
+ public static final byte[] ECSP128_FP_W_X = {
(byte) 0x63, (byte) 0x90, (byte) 0x1e, (byte) 0x12,
(byte) 0x27, (byte) 0x61, (byte) 0xd9, (byte) 0xc1,
(byte) 0x65, (byte) 0x65, (byte) 0xb2, (byte) 0xf3,
(byte) 0x8e, (byte) 0x99, (byte) 0x1f, (byte) 0x71
};
- public static final byte[] ECSP128_FP_P_Y = {
+ public static final byte[] ECSP128_FP_W_Y = {
(byte) 0xb9, (byte) 0xd9, (byte) 0x9f, (byte) 0xbc,
(byte) 0x31, (byte) 0x54, (byte) 0xa9, (byte) 0x6c,
(byte) 0xa2, (byte) 0x3e, (byte) 0xcf, (byte) 0xf7,
@@ -1028,7 +1029,7 @@ public class EC_Consts {
public static final short ECSP160_FP_K = 1;
- public static final byte[] ECSP160_FP_P_X = {
+ public static final byte[] ECSP160_FP_W_X = {
(byte) 0x59, (byte) 0xc9, (byte) 0xc3, (byte) 0xc8,
(byte) 0xae, (byte) 0xf2, (byte) 0x9f, (byte) 0x1c,
(byte) 0x1c, (byte) 0x50, (byte) 0x0c, (byte) 0xaf,
@@ -1036,7 +1037,7 @@ public class EC_Consts {
(byte) 0x08, (byte) 0x6e, (byte) 0x6e, (byte) 0xb0
};
- public static final byte[] ECSP160_FP_P_Y = {
+ public static final byte[] ECSP160_FP_W_Y = {
(byte) 0xd6, (byte) 0x95, (byte) 0xa7, (byte) 0x60,
(byte) 0x05, (byte) 0xed, (byte) 0xdb, (byte) 0x26,
(byte) 0xaf, (byte) 0xd4, (byte) 0x0e, (byte) 0xe2,
@@ -1102,7 +1103,7 @@ public class EC_Consts {
public static final short ECSP192_FP_K = 1;
- public static final byte[] ECSP192_FP_P_X = {
+ public static final byte[] ECSP192_FP_W_X = {
(byte) 0xaa, (byte) 0xd0, (byte) 0xdb, (byte) 0xf8,
(byte) 0xad, (byte) 0x1c, (byte) 0x2c, (byte) 0x4e,
(byte) 0xf0, (byte) 0x67, (byte) 0xda, (byte) 0x63,
@@ -1111,7 +1112,7 @@ public class EC_Consts {
(byte) 0xb7, (byte) 0x7a, (byte) 0x59, (byte) 0x9c
};
- public static final byte[] ECSP192_FP_P_Y = {
+ public static final byte[] ECSP192_FP_W_Y = {
(byte) 0xae, (byte) 0x28, (byte) 0xd7, (byte) 0xea,
(byte) 0xde, (byte) 0xba, (byte) 0x10, (byte) 0x48,
(byte) 0x40, (byte) 0x64, (byte) 0x0d, (byte) 0x9b,
@@ -1184,7 +1185,7 @@ public class EC_Consts {
public static final short ECSP224_FP_K = 1;
- public static final byte[] ECSP224_FP_P_X = {
+ public static final byte[] ECSP224_FP_W_X = {
(byte) 0xcf, (byte) 0xd9, (byte) 0x2a, (byte) 0xea,
(byte) 0x0f, (byte) 0x79, (byte) 0x19, (byte) 0x0c,
(byte) 0x48, (byte) 0xca, (byte) 0x70, (byte) 0x3e,
@@ -1194,7 +1195,7 @@ public class EC_Consts {
(byte) 0xfe, (byte) 0x4d, (byte) 0x0f, (byte) 0x04
};
- public static final byte[] ECSP224_FP_P_Y = {
+ public static final byte[] ECSP224_FP_W_Y = {
(byte) 0x25, (byte) 0x7a, (byte) 0x3d, (byte) 0x98,
(byte) 0xde, (byte) 0x44, (byte) 0xbd, (byte) 0x25,
(byte) 0x40, (byte) 0x49, (byte) 0x77, (byte) 0xa4,
@@ -1274,7 +1275,7 @@ public class EC_Consts {
public static final short ECSP256_FP_K = 1;
- public static final byte[] ECSP256_FP_P_X = {
+ public static final byte[] ECSP256_FP_W_X = {
(byte) 0x75, (byte) 0xfc, (byte) 0xe7, (byte) 0x09,
(byte) 0x68, (byte) 0x86, (byte) 0x2d, (byte) 0x53,
(byte) 0xe2, (byte) 0x95, (byte) 0x48, (byte) 0xaa,
@@ -1285,7 +1286,7 @@ public class EC_Consts {
(byte) 0xf8, (byte) 0xda, (byte) 0xd6, (byte) 0x53
};
- public static final byte[] ECSP256_FP_P_Y = {
+ public static final byte[] ECSP256_FP_W_Y = {
(byte) 0x55, (byte) 0xaa, (byte) 0x4b, (byte) 0x7d,
(byte) 0x38, (byte) 0x82, (byte) 0xfb, (byte) 0x0a,
(byte) 0x83, (byte) 0xbd, (byte) 0x00, (byte) 0xc9,
@@ -1390,7 +1391,7 @@ public class EC_Consts {
public static final short ECSP384_FP_K = 1;
- public static final byte[] ECSP384_FP_P_X = {
+ public static final byte[] ECSP384_FP_W_X = {
(byte) 0xa4, (byte) 0xbd, (byte) 0x57, (byte) 0x5b,
(byte) 0xf2, (byte) 0x03, (byte) 0x00, (byte) 0xb0,
(byte) 0xcf, (byte) 0x8a, (byte) 0x2f, (byte) 0x41,
@@ -1405,7 +1406,7 @@ public class EC_Consts {
(byte) 0x34, (byte) 0x83, (byte) 0x4e, (byte) 0xf1
};
- public static final byte[] ECSP384_FP_P_Y = {
+ public static final byte[] ECSP384_FP_W_Y = {
(byte) 0x38, (byte) 0xd5, (byte) 0x1c, (byte) 0x8f,
(byte) 0x9e, (byte) 0x90, (byte) 0x59, (byte) 0x2f,
(byte) 0x56, (byte) 0x7e, (byte) 0x81, (byte) 0xd0,
@@ -1544,7 +1545,7 @@ public class EC_Consts {
public static final short ECSP521_FP_K = 1;
- public static final byte[] ECSP521_FP_P_X = {
+ public static final byte[] ECSP521_FP_W_X = {
(byte) 0xfc, (byte) 0xcf, (byte) 0x5c, (byte) 0x11,
(byte) 0x3b, (byte) 0xec, (byte) 0x94, (byte) 0x61,
(byte) 0xdb, (byte) 0x3e, (byte) 0x56, (byte) 0x73,
@@ -1564,7 +1565,7 @@ public class EC_Consts {
(byte) 0xde
};
- public static final byte[] ECSP521_FP_P_Y = {
+ public static final byte[] ECSP521_FP_W_Y = {
(byte) 0xc3, (byte) 0x6d, (byte) 0x08, (byte) 0x8f,
(byte) 0xc2, (byte) 0xfe, (byte) 0x3b, (byte) 0x42,
(byte) 0x90, (byte) 0x7b, (byte) 0xbf, (byte) 0x8a,
@@ -1822,8 +1823,8 @@ public class EC_Consts {
EC_G_Y = ECSP128_FP_G_Y;
EC_R = ECSP128_FP_R;
EC_K = ECSP128_FP_K;
- EC_P_X = ECSP128_FP_P_X;
- EC_P_Y = ECSP128_FP_P_Y;
+ EC_W_X = ECSP128_FP_W_X;
+ EC_W_Y = ECSP128_FP_W_Y;
break;
}
case CURVE_sp160: {
@@ -1834,8 +1835,8 @@ public class EC_Consts {
EC_G_Y = ECSP160_FP_G_Y;
EC_R = ECSP160_FP_R;
EC_K = ECSP160_FP_K;
- EC_P_X = ECSP160_FP_P_X;
- EC_P_Y = ECSP160_FP_P_Y;
+ EC_W_X = ECSP160_FP_W_X;
+ EC_W_Y = ECSP160_FP_W_Y;
break;
}
case CURVE_sp192: {
@@ -1846,8 +1847,8 @@ public class EC_Consts {
EC_G_Y = ECSP192_FP_G_Y;
EC_R = ECSP192_FP_R;
EC_K = ECSP192_FP_K;
- EC_P_X = ECSP192_FP_P_X;
- EC_P_Y = ECSP192_FP_P_Y;
+ EC_W_X = ECSP192_FP_W_X;
+ EC_W_Y = ECSP192_FP_W_Y;
break;
}
case CURVE_sp224: {
@@ -1858,8 +1859,8 @@ public class EC_Consts {
EC_G_Y = ECSP224_FP_G_Y;
EC_R = ECSP224_FP_R;
EC_K = ECSP224_FP_K;
- EC_P_X = ECSP224_FP_P_X;
- EC_P_Y = ECSP224_FP_P_Y;
+ EC_W_X = ECSP224_FP_W_X;
+ EC_W_Y = ECSP224_FP_W_Y;
break;
}
case CURVE_sp256: {
@@ -1870,8 +1871,8 @@ public class EC_Consts {
EC_G_Y = ECSP256_FP_G_Y;
EC_R = ECSP256_FP_R;
EC_K = ECSP256_FP_K;
- EC_P_X = ECSP256_FP_P_X;
- EC_P_Y = ECSP256_FP_P_Y;
+ EC_W_X = ECSP256_FP_W_X;
+ EC_W_Y = ECSP256_FP_W_Y;
break;
}
case CURVE_sp384: {
@@ -1882,8 +1883,8 @@ public class EC_Consts {
EC_G_Y = ECSP384_FP_G_Y;
EC_R = ECSP384_FP_R;
EC_K = ECSP384_FP_K;
- EC_P_X = ECSP384_FP_P_X;
- EC_P_Y = ECSP384_FP_P_Y;
+ EC_W_X = ECSP384_FP_W_X;
+ EC_W_Y = ECSP384_FP_W_Y;
break;
}
case CURVE_sp521: {
@@ -1894,8 +1895,8 @@ public class EC_Consts {
EC_G_Y = ECSP521_FP_G_Y;
EC_R = ECSP521_FP_R;
EC_K = ECSP521_FP_K;
- EC_P_X = ECSP521_FP_P_X;
- EC_P_Y = ECSP521_FP_P_Y;
+ EC_W_X = ECSP521_FP_W_X;
+ EC_W_Y = ECSP521_FP_W_Y;
break;
}
default:
@@ -1929,6 +1930,12 @@ public class EC_Consts {
length = 2;
Util.setShort(outputBuffer, outputOffset, EC_K);
break;
+ case PARAMETER_W:
+ length = toX962(outputBuffer, outputOffset, EC_W_X, (short) 0, (short) EC_W_X.length, EC_W_Y, (short) 0, (short) EC_W_Y.length);
+ break;
+ case PARAMETER_S:
+ length = Util.arrayCopyNonAtomic(EC_S, (short) 0, outputBuffer, outputOffset, (short) EC_S.length);
+ break;
default:
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
}
diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java
index 8cc4237..8043153 100644
--- a/src/applets/SimpleECCApplet.java
+++ b/src/applets/SimpleECCApplet.java
@@ -308,9 +308,9 @@ public class SimpleECCApplet extends Applet {
sw = SW_SKIPPED;
if ((testFlags & FLAG_ECTEST_ECDH_AGREEMENT_VALID_POINT) != (short) 0) {
sw = ecKeyGenerator.generatePair();
- ecPubKey = ecKeyGenerator.getPublicKey();
- ecPrivKey = ecKeyGenerator.getPrivateKey();
if (sw == ISO7816.SW_NO_ERROR) {
+ ecPubKey = ecKeyGenerator.getPublicKey();
+ ecPrivKey = ecKeyGenerator.getPrivateKey();
sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 0);
}
}
@@ -325,9 +325,9 @@ public class SimpleECCApplet extends Applet {
sw = SW_SKIPPED;
if ((testFlags & FLAG_ECTEST_ECDH_AGREEMENT_INVALID_POINT) != (short) 0) {
sw = ecKeyGenerator.generatePair();
- ecPubKey = ecKeyGenerator.getPublicKey();
- ecPrivKey = ecKeyGenerator.getPrivateKey();
if (sw == ISO7816.SW_NO_ERROR) {
+ ecPubKey = ecKeyGenerator.getPublicKey();
+ ecPrivKey = ecKeyGenerator.getPrivateKey();
sw = ecKeyTester.testECDH_invalidPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 1);
}
}
@@ -342,9 +342,9 @@ public class SimpleECCApplet extends Applet {
sw = SW_SKIPPED;
if ((testFlags & FLAG_ECTEST_ECDSA_SIGNATURE) != (short) 0) {
sw = ecKeyGenerator.generatePair();
- ecPubKey = ecKeyGenerator.getPublicKey();
- ecPrivKey = ecKeyGenerator.getPrivateKey();
if (sw == ISO7816.SW_NO_ERROR) {
+ ecPubKey = ecKeyGenerator.getPublicKey();
+ ecPrivKey = ecKeyGenerator.getPrivateKey();
sw = ecKeyTester.testECDSA(ecPrivKey, ecPubKey, m_ramArray2, (short) 0, (short) m_ramArray2.length, m_ramArray, (short) 0);
}
}
@@ -387,6 +387,9 @@ public class SimpleECCApplet extends Applet {
bufferOffset++;
sw = SW_SKIPPED;
if ((testFlags & FLAG_ECTEST_ECDH_AGREEMENT_SMALL_DEGREE_POINT) != (short) 0) {
+ //TODO: this needs refactor, just quickly to see if it works
+ short pubLength = EC_Consts.getCurveParameter(EC_Consts.getAnomalousCurve(keyClass,keyLen), EC_Consts.PARAMETER_W, m_ramArray, (short) 0);
+ ecKeyGenerator.setParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_W, m_ramArray, (short)0, pubLength);
ecPubKey = ecKeyGenerator.getPublicKey();
ecPrivKey = ecKeyGenerator.getPrivateKey();
sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 1);
--
cgit v1.3.1
From 2a455c2a12f69d5dc81d2b1f78db011e92d7f754 Mon Sep 17 00:00:00 2001
From: J08nY
Date: Wed, 30 Nov 2016 00:23:29 +0100
Subject: Fixed small-pub degree test, still crashes most cards.
---
!uploader/simpleECC.cap | Bin 18068 -> 18215 bytes
dist/SimpleAPDU.jar | Bin 3078811 -> 3078963 bytes
src/applets/ECKeyTester.java | 22 +++++++++++++---------
src/applets/SimpleECCApplet.java | 12 +++++++-----
4 files changed, 20 insertions(+), 14 deletions(-)
(limited to 'src/applets/SimpleECCApplet.java')
diff --git a/!uploader/simpleECC.cap b/!uploader/simpleECC.cap
index d9c360b..e76168c 100644
Binary files a/!uploader/simpleECC.cap and b/!uploader/simpleECC.cap differ
diff --git a/dist/SimpleAPDU.jar b/dist/SimpleAPDU.jar
index 9bb85ff..09b7e73 100644
Binary files a/dist/SimpleAPDU.jar and b/dist/SimpleAPDU.jar differ
diff --git a/src/applets/ECKeyTester.java b/src/applets/ECKeyTester.java
index acfb64e..9dd0fd5 100644
--- a/src/applets/ECKeyTester.java
+++ b/src/applets/ECKeyTester.java
@@ -49,7 +49,7 @@ public class ECKeyTester {
return result;
}
- private short testDH(KeyAgreement ka, ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
+ private short testKA(KeyAgreement ka, ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
short result = ISO7816.SW_NO_ERROR;
try {
ka.init(privateKey);
@@ -64,19 +64,23 @@ public class ECKeyTester {
return result;
}
- private short testDH_validPoint(KeyAgreement ka, ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
- return testDH(ka, privateKey, pubkeyBuffer, pubkeyOffset, pubkeyLength, outputBuffer, outputOffset);
+ private short testKA_validPoint(KeyAgreement ka, ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
+ return testKA(ka, privateKey, pubkeyBuffer, pubkeyOffset, pubkeyLength, outputBuffer, outputOffset);
}
- private short testDH_invalidPoint(KeyAgreement ka, ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
+ private short testKA_invalidPoint(KeyAgreement ka, ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
pubkeyBuffer[(short)(pubkeyLength - 2)] += 0xcc;
pubkeyBuffer[(short)(pubkeyLength - 3)] += 0xcc;
- short result = testDH(ka, privateKey, pubkeyBuffer, pubkeyOffset, pubkeyLength, outputBuffer, outputOffset);
+ short result = testKA(ka, privateKey, pubkeyBuffer, pubkeyOffset, pubkeyLength, outputBuffer, outputOffset);
pubkeyBuffer[(short)(pubkeyLength - 2)] -= 0xcc;
pubkeyBuffer[(short)(pubkeyLength - 3)] -= 0xcc;
return result;
}
+ public short testECDH(ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
+ return testKA(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, pubkeyLength, outputBuffer, outputOffset);
+ }
+
/**
* Tests ECDH secret generation with given {@code privateKey} and {@code publicKey}.
* Uses {@code pubkeyBuffer} at {@code pubkeyOffset} for computations.
@@ -92,12 +96,12 @@ public class ECKeyTester {
**/
public short testECDH_validPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
- return testDH_validPoint(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
+ return testKA_validPoint(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
}
public short testECDH_invalidPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
- return testDH_invalidPoint(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
+ return testKA_invalidPoint(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
}
@@ -116,12 +120,12 @@ public class ECKeyTester {
*/
public short testECDHC_validPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
- return testDH_validPoint(ecdhcKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
+ return testKA_validPoint(ecdhcKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
}
public short testECDHC_invalidPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
- return testDH_invalidPoint(ecdhcKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
+ return testKA_invalidPoint(ecdhcKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
}
/**
diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java
index 8043153..d653c10 100644
--- a/src/applets/SimpleECCApplet.java
+++ b/src/applets/SimpleECCApplet.java
@@ -358,7 +358,9 @@ public class SimpleECCApplet extends Applet {
bufferOffset++;
sw = SW_SKIPPED;
if ((testFlags & FLAG_ECTEST_SET_ANOMALOUSCURVE) != (short) 0) {
- sw = ecKeyGenerator.setCustomCurve(EC_Consts.getAnomalousCurve(keyClass, keyLen), m_ramArray, (short) 0);
+ if (keyClass == KeyPair.ALG_EC_FP) { //Only FP supported at the moment
+ sw = ecKeyGenerator.setCustomCurve(EC_Consts.getAnomalousCurve(keyClass, keyLen), m_ramArray, (short) 0);
+ }
if (sw != ISO7816.SW_NO_ERROR) {
testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_ANOMALOUSCUVE;
}
@@ -375,6 +377,9 @@ public class SimpleECCApplet extends Applet {
sw = SW_SKIPPED;
if ((testFlags & FLAG_ECTEST_GENERATE_KEYPAIR_ANOMALOUSCUVE) != (short) 0) {
sw = ecKeyGenerator.generatePair();
+ if (sw != ISO7816.SW_NO_ERROR) {
+ testFlags &= ~FLAG_ECTEST_ECDH_AGREEMENT_SMALL_DEGREE_POINT;
+ }
}
Util.setShort(buffer, bufferOffset, sw);
bufferOffset += 2;
@@ -387,12 +392,9 @@ public class SimpleECCApplet extends Applet {
bufferOffset++;
sw = SW_SKIPPED;
if ((testFlags & FLAG_ECTEST_ECDH_AGREEMENT_SMALL_DEGREE_POINT) != (short) 0) {
- //TODO: this needs refactor, just quickly to see if it works
short pubLength = EC_Consts.getCurveParameter(EC_Consts.getAnomalousCurve(keyClass,keyLen), EC_Consts.PARAMETER_W, m_ramArray, (short) 0);
- ecKeyGenerator.setParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_W, m_ramArray, (short)0, pubLength);
- ecPubKey = ecKeyGenerator.getPublicKey();
ecPrivKey = ecKeyGenerator.getPrivateKey();
- sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 1);
+ sw = ecKeyTester.testECDH(ecPrivKey, m_ramArray, (short) 0, pubLength, m_ramArray2, (short) 1);
}
Util.setShort(buffer, bufferOffset, sw);
bufferOffset += 2;
--
cgit v1.3.1
From 31a9da5493cb5085354dbfa034cea4b1d45df3cb Mon Sep 17 00:00:00 2001
From: J08nY
Date: Wed, 30 Nov 2016 15:10:43 +0100
Subject: Changed ECSP curves, 192b and 521b, to ones that have a prime pubkey
order.
---
src/applets/EC_Consts.java | 368 +++++++++++++++++++--------------------
src/applets/SimpleECCApplet.java | 1 +
2 files changed, 185 insertions(+), 184 deletions(-)
(limited to 'src/applets/SimpleECCApplet.java')
diff --git a/src/applets/EC_Consts.java b/src/applets/EC_Consts.java
index 49eb59f..d1f6842 100644
--- a/src/applets/EC_Consts.java
+++ b/src/applets/EC_Consts.java
@@ -1048,77 +1048,77 @@ public class EC_Consts {
//Anomalous curve(small-pub-192), with pubkey of order 4
public static final byte[] ECSP192_FP_P = {
- (byte) 0xee, (byte) 0x8a, (byte) 0x97, (byte) 0x03,
- (byte) 0x3b, (byte) 0xb1, (byte) 0x00, (byte) 0x60,
- (byte) 0x0c, (byte) 0x3a, (byte) 0x9f, (byte) 0x9d,
- (byte) 0x88, (byte) 0x2a, (byte) 0xca, (byte) 0xeb,
- (byte) 0x6e, (byte) 0x24, (byte) 0xfc, (byte) 0x63,
- (byte) 0x04, (byte) 0xd8, (byte) 0x60, (byte) 0x8f
+ (byte) 0xce, (byte) 0x71, (byte) 0x4c, (byte) 0xc3,
+ (byte) 0xa1, (byte) 0x5c, (byte) 0xe7, (byte) 0xe5,
+ (byte) 0xda, (byte) 0xb0, (byte) 0x68, (byte) 0xc9,
+ (byte) 0xa1, (byte) 0xf8, (byte) 0xbe, (byte) 0x00,
+ (byte) 0xaa, (byte) 0xd4, (byte) 0x80, (byte) 0xab,
+ (byte) 0xcc, (byte) 0xae, (byte) 0xef, (byte) 0xc3
};
public static final byte[] ECSP192_FP_A = {
- (byte) 0xc3, (byte) 0xf5, (byte) 0x83, (byte) 0x61,
- (byte) 0x41, (byte) 0x18, (byte) 0xd6, (byte) 0xc4,
- (byte) 0x85, (byte) 0xde, (byte) 0x1c, (byte) 0xd9,
- (byte) 0x0a, (byte) 0x86, (byte) 0xda, (byte) 0x7d,
- (byte) 0xff, (byte) 0x3a, (byte) 0xa6, (byte) 0xbb,
- (byte) 0x77, (byte) 0x5c, (byte) 0xe1, (byte) 0x24
+ (byte) 0x59, (byte) 0x7c, (byte) 0x78, (byte) 0x1f,
+ (byte) 0x64, (byte) 0xc3, (byte) 0x3e, (byte) 0xb8,
+ (byte) 0xef, (byte) 0x91, (byte) 0x9c, (byte) 0x41,
+ (byte) 0x59, (byte) 0x11, (byte) 0x51, (byte) 0x8e,
+ (byte) 0xa3, (byte) 0x23, (byte) 0xbe, (byte) 0x88,
+ (byte) 0xb9, (byte) 0x43, (byte) 0x7c, (byte) 0xaf
};
public static final byte[] ECSP192_FP_B = {
- (byte) 0x96, (byte) 0x78, (byte) 0x63, (byte) 0x29,
- (byte) 0x6d, (byte) 0x32, (byte) 0x01, (byte) 0x61,
- (byte) 0xe6, (byte) 0x88, (byte) 0x0f, (byte) 0xa6,
- (byte) 0xd9, (byte) 0xa4, (byte) 0x86, (byte) 0x79,
- (byte) 0xdf, (byte) 0xdb, (byte) 0xb1, (byte) 0x2b,
- (byte) 0xb7, (byte) 0xe3, (byte) 0x54, (byte) 0xb1
+ (byte) 0xf8, (byte) 0x15, (byte) 0x85, (byte) 0xa1,
+ (byte) 0xb1, (byte) 0x8f, (byte) 0x23, (byte) 0x3d,
+ (byte) 0x70, (byte) 0xad, (byte) 0xd7, (byte) 0xee,
+ (byte) 0x13, (byte) 0x42, (byte) 0xd2, (byte) 0x03,
+ (byte) 0x5c, (byte) 0x38, (byte) 0x6a, (byte) 0x92,
+ (byte) 0xe3, (byte) 0xab, (byte) 0x83, (byte) 0x20
};
public static final byte[] ECSP192_FP_G_X = {
- (byte) 0x7d, (byte) 0x6e, (byte) 0x93, (byte) 0x4a,
- (byte) 0xbb, (byte) 0x41, (byte) 0x6c, (byte) 0x64,
- (byte) 0xd4, (byte) 0x28, (byte) 0x90, (byte) 0xea,
- (byte) 0x64, (byte) 0x40, (byte) 0xf5, (byte) 0x8a,
- (byte) 0x0a, (byte) 0x5c, (byte) 0x5b, (byte) 0x31,
- (byte) 0x2f, (byte) 0x35, (byte) 0x6b, (byte) 0x29
+ (byte) 0x15, (byte) 0x0f, (byte) 0xf0, (byte) 0xa4,
+ (byte) 0x0d, (byte) 0xea, (byte) 0xc6, (byte) 0x46,
+ (byte) 0x2b, (byte) 0x59, (byte) 0x87, (byte) 0x41,
+ (byte) 0x86, (byte) 0x17, (byte) 0xfd, (byte) 0xee,
+ (byte) 0xb6, (byte) 0xbf, (byte) 0xd7, (byte) 0x6d,
+ (byte) 0x4d, (byte) 0x60, (byte) 0xa0, (byte) 0x67
};
public static final byte[] ECSP192_FP_G_Y = {
- (byte) 0x47, (byte) 0x37, (byte) 0x7f, (byte) 0xed,
- (byte) 0x17, (byte) 0xe2, (byte) 0x31, (byte) 0x74,
- (byte) 0xf1, (byte) 0xb1, (byte) 0xb9, (byte) 0x01,
- (byte) 0x6e, (byte) 0x28, (byte) 0x5e, (byte) 0x9c,
- (byte) 0xac, (byte) 0x39, (byte) 0xe3, (byte) 0xbc,
- (byte) 0xaa, (byte) 0x65, (byte) 0x22, (byte) 0xfd
+ (byte) 0x84, (byte) 0x3d, (byte) 0x57, (byte) 0x73,
+ (byte) 0x71, (byte) 0xc5, (byte) 0xdc, (byte) 0xe1,
+ (byte) 0x22, (byte) 0xc2, (byte) 0xff, (byte) 0x20,
+ (byte) 0x6b, (byte) 0x2f, (byte) 0x42, (byte) 0xfa,
+ (byte) 0x0b, (byte) 0x84, (byte) 0x2b, (byte) 0x49,
+ (byte) 0xbd, (byte) 0xaf, (byte) 0x99, (byte) 0x0f
};
public static final byte[] ECSP192_FP_R = {
- (byte) 0xee, (byte) 0x8a, (byte) 0x97, (byte) 0x03,
- (byte) 0x3b, (byte) 0xb1, (byte) 0x00, (byte) 0x60,
- (byte) 0x0c, (byte) 0x3a, (byte) 0x9f, (byte) 0x9e,
- (byte) 0xcd, (byte) 0x2b, (byte) 0xb6, (byte) 0x46,
- (byte) 0x75, (byte) 0x84, (byte) 0x34, (byte) 0xad,
- (byte) 0xd3, (byte) 0xd0, (byte) 0xdf, (byte) 0xd0
+ (byte) 0xce, (byte) 0x71, (byte) 0x4c, (byte) 0xc3,
+ (byte) 0xa1, (byte) 0x5c, (byte) 0xe7, (byte) 0xe5,
+ (byte) 0xda, (byte) 0xb0, (byte) 0x68, (byte) 0xc9,
+ (byte) 0xa3, (byte) 0x0b, (byte) 0xc9, (byte) 0x29,
+ (byte) 0x15, (byte) 0xbd, (byte) 0x86, (byte) 0x62,
+ (byte) 0xae, (byte) 0x88, (byte) 0x28, (byte) 0x87
};
public static final short ECSP192_FP_K = 1;
public static final byte[] ECSP192_FP_W_X = {
- (byte) 0xaa, (byte) 0xd0, (byte) 0xdb, (byte) 0xf8,
- (byte) 0xad, (byte) 0x1c, (byte) 0x2c, (byte) 0x4e,
- (byte) 0xf0, (byte) 0x67, (byte) 0xda, (byte) 0x63,
- (byte) 0x97, (byte) 0x23, (byte) 0xe2, (byte) 0x0d,
- (byte) 0xcf, (byte) 0xb4, (byte) 0x53, (byte) 0x52,
- (byte) 0xb7, (byte) 0x7a, (byte) 0x59, (byte) 0x9c
+ (byte) 0x17, (byte) 0x04, (byte) 0x7f, (byte) 0x91,
+ (byte) 0xdb, (byte) 0xe3, (byte) 0x30, (byte) 0x32,
+ (byte) 0xc9, (byte) 0xd0, (byte) 0x9b, (byte) 0xd2,
+ (byte) 0x9c, (byte) 0xea, (byte) 0xdd, (byte) 0x8a,
+ (byte) 0x09, (byte) 0xcc, (byte) 0xc3, (byte) 0x2a,
+ (byte) 0xc6, (byte) 0x30, (byte) 0x95, (byte) 0x41
};
public static final byte[] ECSP192_FP_W_Y = {
- (byte) 0xae, (byte) 0x28, (byte) 0xd7, (byte) 0xea,
- (byte) 0xde, (byte) 0xba, (byte) 0x10, (byte) 0x48,
- (byte) 0x40, (byte) 0x64, (byte) 0x0d, (byte) 0x9b,
- (byte) 0x6e, (byte) 0x2c, (byte) 0x2d, (byte) 0x22,
- (byte) 0x25, (byte) 0xd2, (byte) 0x5d, (byte) 0x79,
- (byte) 0x3a, (byte) 0x65, (byte) 0x5f, (byte) 0xb1
+ (byte) 0x6a, (byte) 0x72, (byte) 0x6d, (byte) 0xe5,
+ (byte) 0x4f, (byte) 0xbd, (byte) 0x59, (byte) 0xcf,
+ (byte) 0xc3, (byte) 0x52, (byte) 0xe8, (byte) 0x38,
+ (byte) 0xb3, (byte) 0x37, (byte) 0xfa, (byte) 0x00,
+ (byte) 0x5a, (byte) 0x97, (byte) 0x18, (byte) 0x08,
+ (byte) 0x16, (byte) 0x13, (byte) 0x5e, (byte) 0x6a
};
@@ -1424,165 +1424,165 @@ public class EC_Consts {
//Anomalous curve(small-pub-521), with pubkey of order 4
public static final byte[] ECSP521_FP_P = {
- (byte) 0x01, (byte) 0x9f, (byte) 0x9b, (byte) 0x18,
- (byte) 0x84, (byte) 0x55, (byte) 0xfc, (byte) 0xb2,
- (byte) 0x4e, (byte) 0x68, (byte) 0xee, (byte) 0xba,
- (byte) 0xbf, (byte) 0x2a, (byte) 0xfd, (byte) 0xa0,
- (byte) 0xb5, (byte) 0x11, (byte) 0x4e, (byte) 0xc5,
- (byte) 0xe8, (byte) 0x2b, (byte) 0x6d, (byte) 0xa1,
- (byte) 0x8f, (byte) 0xa2, (byte) 0x64, (byte) 0x31,
- (byte) 0xee, (byte) 0x72, (byte) 0x03, (byte) 0xa2,
- (byte) 0x3d, (byte) 0x8b, (byte) 0xd7, (byte) 0xc4,
- (byte) 0x16, (byte) 0x9b, (byte) 0x73, (byte) 0x0d,
- (byte) 0xbc, (byte) 0x9c, (byte) 0xff, (byte) 0xd8,
- (byte) 0xc0, (byte) 0xe7, (byte) 0x9d, (byte) 0xc4,
- (byte) 0x03, (byte) 0x74, (byte) 0x12, (byte) 0x8d,
- (byte) 0xeb, (byte) 0x03, (byte) 0x44, (byte) 0x56,
- (byte) 0x96, (byte) 0x0b, (byte) 0x87, (byte) 0x3d,
- (byte) 0xfd, (byte) 0x26, (byte) 0x2b, (byte) 0xe0,
- (byte) 0xb6, (byte) 0xd5
+ (byte) 0x01, (byte) 0xd3, (byte) 0xdf, (byte) 0x43,
+ (byte) 0x09, (byte) 0x24, (byte) 0x95, (byte) 0x6e,
+ (byte) 0x21, (byte) 0x0a, (byte) 0x60, (byte) 0x5b,
+ (byte) 0x4d, (byte) 0xbf, (byte) 0x4a, (byte) 0x2e,
+ (byte) 0x90, (byte) 0x9d, (byte) 0x7a, (byte) 0x80,
+ (byte) 0x16, (byte) 0x58, (byte) 0x97, (byte) 0x8c,
+ (byte) 0x88, (byte) 0xff, (byte) 0xd6, (byte) 0x8d,
+ (byte) 0xcc, (byte) 0x81, (byte) 0x7f, (byte) 0x5c,
+ (byte) 0xc7, (byte) 0x9c, (byte) 0xf1, (byte) 0x88,
+ (byte) 0xd9, (byte) 0xee, (byte) 0x82, (byte) 0xd1,
+ (byte) 0xa5, (byte) 0x1c, (byte) 0x44, (byte) 0xcb,
+ (byte) 0xd3, (byte) 0x1e, (byte) 0x9c, (byte) 0xc5,
+ (byte) 0xb8, (byte) 0x16, (byte) 0xd7, (byte) 0x6d,
+ (byte) 0x5b, (byte) 0x13, (byte) 0x12, (byte) 0xb0,
+ (byte) 0x05, (byte) 0xf7, (byte) 0xb6, (byte) 0x89,
+ (byte) 0x19, (byte) 0xe2, (byte) 0x75, (byte) 0xda,
+ (byte) 0xc9, (byte) 0x9f
};
public static final byte[] ECSP521_FP_A = {
- (byte) 0x8e, (byte) 0xdc, (byte) 0x39, (byte) 0xcd,
- (byte) 0xdd, (byte) 0x0f, (byte) 0x31, (byte) 0x73,
- (byte) 0x71, (byte) 0x67, (byte) 0x46, (byte) 0xac,
- (byte) 0x53, (byte) 0x94, (byte) 0xb6, (byte) 0x1e,
- (byte) 0x11, (byte) 0xc0, (byte) 0x56, (byte) 0x67,
- (byte) 0xec, (byte) 0xba, (byte) 0x2f, (byte) 0x25,
- (byte) 0x05, (byte) 0xb7, (byte) 0x28, (byte) 0x6c,
- (byte) 0x5b, (byte) 0xab, (byte) 0x09, (byte) 0x02,
- (byte) 0x09, (byte) 0x1d, (byte) 0xf8, (byte) 0xa6,
- (byte) 0xbe, (byte) 0x38, (byte) 0xcd, (byte) 0x4a,
- (byte) 0x45, (byte) 0xa8, (byte) 0x17, (byte) 0xad,
- (byte) 0x2a, (byte) 0xfd, (byte) 0x57, (byte) 0x6f,
- (byte) 0xdc, (byte) 0xbd, (byte) 0x2f, (byte) 0x27,
- (byte) 0x01, (byte) 0xc4, (byte) 0x5e, (byte) 0xae,
- (byte) 0x1a, (byte) 0x55, (byte) 0x88, (byte) 0xee,
- (byte) 0xc7, (byte) 0xd1, (byte) 0x08, (byte) 0x42,
- (byte) 0x3d
+ (byte) 0x00, (byte) 0x40, (byte) 0x16, (byte) 0x39,
+ (byte) 0xf3, (byte) 0x6f, (byte) 0x2e, (byte) 0xe4,
+ (byte) 0x5f, (byte) 0xc1, (byte) 0x64, (byte) 0xea,
+ (byte) 0x3e, (byte) 0x1f, (byte) 0x14, (byte) 0xf4,
+ (byte) 0x80, (byte) 0x3f, (byte) 0xd7, (byte) 0xa7,
+ (byte) 0x7f, (byte) 0xfd, (byte) 0xfb, (byte) 0x39,
+ (byte) 0x2c, (byte) 0x3f, (byte) 0x8f, (byte) 0xe9,
+ (byte) 0x5d, (byte) 0x1a, (byte) 0xea, (byte) 0x33,
+ (byte) 0x14, (byte) 0x67, (byte) 0xf4, (byte) 0x61,
+ (byte) 0x8d, (byte) 0x59, (byte) 0xae, (byte) 0xee,
+ (byte) 0x49, (byte) 0xd5, (byte) 0xd7, (byte) 0xc7,
+ (byte) 0x0c, (byte) 0xaf, (byte) 0x32, (byte) 0x0f,
+ (byte) 0x7d, (byte) 0xd1, (byte) 0xac, (byte) 0x16,
+ (byte) 0x61, (byte) 0x14, (byte) 0xf5, (byte) 0x62,
+ (byte) 0x41, (byte) 0x34, (byte) 0x49, (byte) 0x99,
+ (byte) 0x1d, (byte) 0x3a, (byte) 0xa1, (byte) 0xa2,
+ (byte) 0xc4, (byte) 0x9e
};
public static final byte[] ECSP521_FP_B = {
- (byte) 0x2d, (byte) 0x83, (byte) 0x7c, (byte) 0xac,
- (byte) 0x89, (byte) 0x02, (byte) 0xfe, (byte) 0x65,
- (byte) 0x1e, (byte) 0x81, (byte) 0x7f, (byte) 0x20,
- (byte) 0x6d, (byte) 0x5c, (byte) 0x9e, (byte) 0xb7,
- (byte) 0x8a, (byte) 0xed, (byte) 0xef, (byte) 0x80,
- (byte) 0xd1, (byte) 0x6e, (byte) 0xa1, (byte) 0x67,
- (byte) 0xbb, (byte) 0x37, (byte) 0x7c, (byte) 0xb7,
- (byte) 0xff, (byte) 0xfc, (byte) 0x0c, (byte) 0x94,
- (byte) 0x8e, (byte) 0xef, (byte) 0xea, (byte) 0x5f,
- (byte) 0xd3, (byte) 0x46, (byte) 0xfe, (byte) 0x4c,
- (byte) 0x82, (byte) 0xa1, (byte) 0x6d, (byte) 0x1a,
- (byte) 0x46, (byte) 0xb8, (byte) 0xeb, (byte) 0x05,
- (byte) 0xe5, (byte) 0x70, (byte) 0xcb, (byte) 0x76,
- (byte) 0xe7, (byte) 0x1d, (byte) 0xb0, (byte) 0xad,
- (byte) 0xc1, (byte) 0x99, (byte) 0xbd, (byte) 0xe2,
- (byte) 0x83, (byte) 0x4a, (byte) 0xd6, (byte) 0x74,
- (byte) 0x1f
+ (byte) 0x00, (byte) 0x4a, (byte) 0x26, (byte) 0xa8,
+ (byte) 0xc4, (byte) 0x7f, (byte) 0xce, (byte) 0x20,
+ (byte) 0x4b, (byte) 0xa9, (byte) 0x53, (byte) 0x01,
+ (byte) 0x5f, (byte) 0xa8, (byte) 0x67, (byte) 0x08,
+ (byte) 0xc0, (byte) 0xde, (byte) 0x72, (byte) 0x0f,
+ (byte) 0x27, (byte) 0x52, (byte) 0x39, (byte) 0x88,
+ (byte) 0xb0, (byte) 0x97, (byte) 0xe7, (byte) 0x74,
+ (byte) 0x16, (byte) 0x8c, (byte) 0x15, (byte) 0xf7,
+ (byte) 0xa2, (byte) 0x15, (byte) 0xaa, (byte) 0xf1,
+ (byte) 0x8a, (byte) 0x5f, (byte) 0x1b, (byte) 0x95,
+ (byte) 0x79, (byte) 0xab, (byte) 0x3d, (byte) 0xb9,
+ (byte) 0x35, (byte) 0xd4, (byte) 0x5b, (byte) 0xe1,
+ (byte) 0x4c, (byte) 0x9a, (byte) 0x87, (byte) 0xb7,
+ (byte) 0x11, (byte) 0x70, (byte) 0x39, (byte) 0x69,
+ (byte) 0x09, (byte) 0xb1, (byte) 0x4d, (byte) 0x06,
+ (byte) 0xf7, (byte) 0xa0, (byte) 0x99, (byte) 0x75,
+ (byte) 0xb3, (byte) 0xa6
};
public static final byte[] ECSP521_FP_G_X = {
- (byte) 0xe5, (byte) 0xa6, (byte) 0x65, (byte) 0xfc,
- (byte) 0x52, (byte) 0x93, (byte) 0xfa, (byte) 0xd7,
- (byte) 0x05, (byte) 0x0f, (byte) 0x63, (byte) 0x31,
- (byte) 0x18, (byte) 0xfb, (byte) 0x91, (byte) 0x55,
- (byte) 0x23, (byte) 0x85, (byte) 0x71, (byte) 0xc8,
- (byte) 0x17, (byte) 0xe6, (byte) 0x62, (byte) 0x6c,
- (byte) 0xe5, (byte) 0x8e, (byte) 0x1c, (byte) 0xb5,
- (byte) 0x11, (byte) 0x79, (byte) 0x3d, (byte) 0x03,
- (byte) 0xab, (byte) 0x2e, (byte) 0x18, (byte) 0x0a,
- (byte) 0x5d, (byte) 0xaf, (byte) 0xf4, (byte) 0x9c,
- (byte) 0xfb, (byte) 0xb1, (byte) 0xf8, (byte) 0x88,
- (byte) 0xc7, (byte) 0xd8, (byte) 0x06, (byte) 0x9a,
- (byte) 0x60, (byte) 0xcd, (byte) 0x09, (byte) 0xcc,
- (byte) 0x6a, (byte) 0xfc, (byte) 0x2b, (byte) 0x6b,
- (byte) 0xb1, (byte) 0xea, (byte) 0x54, (byte) 0xea,
- (byte) 0x98, (byte) 0x5e, (byte) 0x78, (byte) 0xd3,
- (byte) 0x1f
+ (byte) 0x01, (byte) 0xc8, (byte) 0x80, (byte) 0xae,
+ (byte) 0x0a, (byte) 0x35, (byte) 0x5a, (byte) 0x52,
+ (byte) 0x79, (byte) 0x1f, (byte) 0xc9, (byte) 0x60,
+ (byte) 0x0f, (byte) 0xd8, (byte) 0xb3, (byte) 0x57,
+ (byte) 0x26, (byte) 0xe9, (byte) 0xd7, (byte) 0x99,
+ (byte) 0x10, (byte) 0x14, (byte) 0x89, (byte) 0x16,
+ (byte) 0x1c, (byte) 0x8f, (byte) 0x90, (byte) 0xa9,
+ (byte) 0xc6, (byte) 0x63, (byte) 0x1d, (byte) 0x09,
+ (byte) 0xb3, (byte) 0xcb, (byte) 0x34, (byte) 0x75,
+ (byte) 0x84, (byte) 0x83, (byte) 0x7d, (byte) 0x9d,
+ (byte) 0xeb, (byte) 0x85, (byte) 0x66, (byte) 0xa9,
+ (byte) 0xc5, (byte) 0x84, (byte) 0x6a, (byte) 0xde,
+ (byte) 0xd0, (byte) 0xd0, (byte) 0x1e, (byte) 0xb9,
+ (byte) 0x47, (byte) 0xb4, (byte) 0xaf, (byte) 0xfd,
+ (byte) 0x34, (byte) 0xe8, (byte) 0xea, (byte) 0x7d,
+ (byte) 0xbe, (byte) 0x73, (byte) 0x3c, (byte) 0xbe,
+ (byte) 0xda, (byte) 0xfa
};
public static final byte[] ECSP521_FP_G_Y = {
- (byte) 0x01, (byte) 0x06, (byte) 0x7b, (byte) 0x42,
- (byte) 0x85, (byte) 0x9e, (byte) 0x5c, (byte) 0x33,
- (byte) 0x98, (byte) 0xab, (byte) 0xa9, (byte) 0xec,
- (byte) 0xc0, (byte) 0x2b, (byte) 0xf2, (byte) 0x89,
- (byte) 0xf9, (byte) 0xe3, (byte) 0x13, (byte) 0x37,
- (byte) 0x1e, (byte) 0x70, (byte) 0x21, (byte) 0xbf,
- (byte) 0x1d, (byte) 0xb5, (byte) 0xae, (byte) 0xbe,
- (byte) 0x2d, (byte) 0x52, (byte) 0x9e, (byte) 0x2f,
- (byte) 0x66, (byte) 0x23, (byte) 0xa0, (byte) 0x6f,
- (byte) 0x3b, (byte) 0x10, (byte) 0xe7, (byte) 0xf9,
- (byte) 0x2c, (byte) 0x3e, (byte) 0xcc, (byte) 0x18,
- (byte) 0x3d, (byte) 0xe1, (byte) 0xe5, (byte) 0x91,
- (byte) 0x9b, (byte) 0x0d, (byte) 0x6d, (byte) 0x2e,
- (byte) 0xd6, (byte) 0x55, (byte) 0xec, (byte) 0x31,
- (byte) 0xc3, (byte) 0x13, (byte) 0x15, (byte) 0x9f,
- (byte) 0x2c, (byte) 0x9d, (byte) 0xf9, (byte) 0x15,
- (byte) 0xfb, (byte) 0xe0
+ (byte) 0x00, (byte) 0x05, (byte) 0x0f, (byte) 0x12,
+ (byte) 0x67, (byte) 0x2f, (byte) 0x16, (byte) 0x3f,
+ (byte) 0x19, (byte) 0xd5, (byte) 0xd4, (byte) 0x93,
+ (byte) 0xeb, (byte) 0x82, (byte) 0xef, (byte) 0x77,
+ (byte) 0x7b, (byte) 0x02, (byte) 0x13, (byte) 0xdd,
+ (byte) 0x4e, (byte) 0x0c, (byte) 0xf7, (byte) 0x5a,
+ (byte) 0x9b, (byte) 0x99, (byte) 0x72, (byte) 0x4f,
+ (byte) 0xbd, (byte) 0xb5, (byte) 0x4b, (byte) 0x0c,
+ (byte) 0xc4, (byte) 0xe0, (byte) 0x37, (byte) 0xbf,
+ (byte) 0x86, (byte) 0xa4, (byte) 0x8b, (byte) 0xac,
+ (byte) 0x28, (byte) 0x46, (byte) 0x7b, (byte) 0xdd,
+ (byte) 0x93, (byte) 0x6c, (byte) 0x31, (byte) 0x4c,
+ (byte) 0xe1, (byte) 0x3f, (byte) 0x6e, (byte) 0xc7,
+ (byte) 0xec, (byte) 0x69, (byte) 0xea, (byte) 0x09,
+ (byte) 0xae, (byte) 0x4f, (byte) 0x54, (byte) 0x44,
+ (byte) 0xdf, (byte) 0x4b, (byte) 0x2a, (byte) 0x11,
+ (byte) 0x7a, (byte) 0x66
};
public static final byte[] ECSP521_FP_R = {
- (byte) 0x01, (byte) 0x9f, (byte) 0x9b, (byte) 0x18,
- (byte) 0x84, (byte) 0x55, (byte) 0xfc, (byte) 0xb2,
- (byte) 0x4e, (byte) 0x68, (byte) 0xee, (byte) 0xba,
- (byte) 0xbf, (byte) 0x2a, (byte) 0xfd, (byte) 0xa0,
- (byte) 0xb5, (byte) 0x11, (byte) 0x4e, (byte) 0xc5,
- (byte) 0xe8, (byte) 0x2b, (byte) 0x6d, (byte) 0xa1,
- (byte) 0x8f, (byte) 0xa2, (byte) 0x64, (byte) 0x31,
- (byte) 0xee, (byte) 0x72, (byte) 0x03, (byte) 0xa2,
- (byte) 0x3d, (byte) 0x75, (byte) 0xdd, (byte) 0xed,
- (byte) 0x80, (byte) 0x28, (byte) 0x58, (byte) 0xff,
- (byte) 0xab, (byte) 0x06, (byte) 0x8f, (byte) 0x74,
- (byte) 0xf8, (byte) 0x9c, (byte) 0xc7, (byte) 0x73,
- (byte) 0x85, (byte) 0x0e, (byte) 0x1b, (byte) 0x56,
- (byte) 0x84, (byte) 0x3f, (byte) 0x76, (byte) 0x7c,
- (byte) 0x15, (byte) 0xef, (byte) 0x65, (byte) 0xb4,
- (byte) 0x12, (byte) 0xe6, (byte) 0x50, (byte) 0xc9,
- (byte) 0x7b, (byte) 0xd0
+ (byte) 0x01, (byte) 0xd3, (byte) 0xdf, (byte) 0x43,
+ (byte) 0x09, (byte) 0x24, (byte) 0x95, (byte) 0x6e,
+ (byte) 0x21, (byte) 0x0a, (byte) 0x60, (byte) 0x5b,
+ (byte) 0x4d, (byte) 0xbf, (byte) 0x4a, (byte) 0x2e,
+ (byte) 0x90, (byte) 0x9d, (byte) 0x7a, (byte) 0x80,
+ (byte) 0x16, (byte) 0x58, (byte) 0x97, (byte) 0x8c,
+ (byte) 0x88, (byte) 0xff, (byte) 0xd6, (byte) 0x8d,
+ (byte) 0xcc, (byte) 0x81, (byte) 0x7f, (byte) 0x5c,
+ (byte) 0xc7, (byte) 0xba, (byte) 0x08, (byte) 0x38,
+ (byte) 0x71, (byte) 0x7c, (byte) 0x19, (byte) 0x47,
+ (byte) 0xf9, (byte) 0x3c, (byte) 0xfd, (byte) 0xd3,
+ (byte) 0xed, (byte) 0x87, (byte) 0xec, (byte) 0x2c,
+ (byte) 0x2d, (byte) 0xf1, (byte) 0x81, (byte) 0xc7,
+ (byte) 0xad, (byte) 0xa5, (byte) 0x53, (byte) 0x34,
+ (byte) 0x6e, (byte) 0xc1, (byte) 0x49, (byte) 0x57,
+ (byte) 0x32, (byte) 0xa1, (byte) 0xe7, (byte) 0xff,
+ (byte) 0xe9, (byte) 0xb3
};
public static final short ECSP521_FP_K = 1;
public static final byte[] ECSP521_FP_W_X = {
- (byte) 0xfc, (byte) 0xcf, (byte) 0x5c, (byte) 0x11,
- (byte) 0x3b, (byte) 0xec, (byte) 0x94, (byte) 0x61,
- (byte) 0xdb, (byte) 0x3e, (byte) 0x56, (byte) 0x73,
- (byte) 0x34, (byte) 0xcb, (byte) 0xf9, (byte) 0x8e,
- (byte) 0x32, (byte) 0xde, (byte) 0x58, (byte) 0x12,
- (byte) 0x92, (byte) 0x07, (byte) 0x74, (byte) 0xdb,
- (byte) 0x40, (byte) 0xd2, (byte) 0x94, (byte) 0x18,
- (byte) 0xd2, (byte) 0x92, (byte) 0xc3, (byte) 0xc4,
- (byte) 0xf6, (byte) 0xce, (byte) 0x08, (byte) 0xb2,
- (byte) 0x00, (byte) 0x21, (byte) 0xfe, (byte) 0x0f,
- (byte) 0x07, (byte) 0xf0, (byte) 0xe4, (byte) 0xc9,
- (byte) 0xc3, (byte) 0xd1, (byte) 0x43, (byte) 0xe7,
- (byte) 0xd0, (byte) 0xf8, (byte) 0xcd, (byte) 0xb6,
- (byte) 0x16, (byte) 0x71, (byte) 0xa7, (byte) 0xe4,
- (byte) 0x46, (byte) 0x8a, (byte) 0x93, (byte) 0xde,
- (byte) 0xe6, (byte) 0x0c, (byte) 0x1d, (byte) 0x29,
- (byte) 0xde
+ (byte) 0x00, (byte) 0x28, (byte) 0x44, (byte) 0xdf,
+ (byte) 0x0f, (byte) 0x31, (byte) 0xf4, (byte) 0x6a,
+ (byte) 0x40, (byte) 0xe6, (byte) 0xc7, (byte) 0x00,
+ (byte) 0x6c, (byte) 0xde, (byte) 0x99, (byte) 0x15,
+ (byte) 0x5b, (byte) 0xd5, (byte) 0xd1, (byte) 0x8d,
+ (byte) 0x0e, (byte) 0x41, (byte) 0x50, (byte) 0x17,
+ (byte) 0x8a, (byte) 0x8e, (byte) 0x30, (byte) 0x7d,
+ (byte) 0x6a, (byte) 0xec, (byte) 0x08, (byte) 0xfd,
+ (byte) 0x02, (byte) 0xd4, (byte) 0x66, (byte) 0xc0,
+ (byte) 0x3c, (byte) 0x49, (byte) 0xb4, (byte) 0x9c,
+ (byte) 0x26, (byte) 0x54, (byte) 0xb7, (byte) 0xc9,
+ (byte) 0xa3, (byte) 0x2d, (byte) 0x88, (byte) 0xca,
+ (byte) 0x01, (byte) 0x40, (byte) 0x16, (byte) 0xa7,
+ (byte) 0xed, (byte) 0xdd, (byte) 0x44, (byte) 0x21,
+ (byte) 0x7b, (byte) 0xe9, (byte) 0x15, (byte) 0x50,
+ (byte) 0x5d, (byte) 0x22, (byte) 0x8e, (byte) 0xfb,
+ (byte) 0x93, (byte) 0x89
};
public static final byte[] ECSP521_FP_W_Y = {
- (byte) 0xc3, (byte) 0x6d, (byte) 0x08, (byte) 0x8f,
- (byte) 0xc2, (byte) 0xfe, (byte) 0x3b, (byte) 0x42,
- (byte) 0x90, (byte) 0x7b, (byte) 0xbf, (byte) 0x8a,
- (byte) 0xf7, (byte) 0xf1, (byte) 0x9e, (byte) 0xda,
- (byte) 0x94, (byte) 0x82, (byte) 0x10, (byte) 0x1d,
- (byte) 0x4f, (byte) 0x73, (byte) 0xf8, (byte) 0xcd,
- (byte) 0x46, (byte) 0x73, (byte) 0x6e, (byte) 0x06,
- (byte) 0x35, (byte) 0xe1, (byte) 0xc5, (byte) 0xca,
- (byte) 0xe1, (byte) 0x71, (byte) 0x09, (byte) 0x30,
- (byte) 0x8c, (byte) 0x3a, (byte) 0xec, (byte) 0x74,
- (byte) 0x10, (byte) 0xf7, (byte) 0xec, (byte) 0x06,
- (byte) 0xfb, (byte) 0x78, (byte) 0xec, (byte) 0xa4,
- (byte) 0xb8, (byte) 0xcb, (byte) 0xac, (byte) 0xb6,
- (byte) 0x4d, (byte) 0xaf, (byte) 0x54, (byte) 0x8f,
- (byte) 0x95, (byte) 0x02, (byte) 0xf4, (byte) 0x87,
- (byte) 0x77, (byte) 0x53, (byte) 0xda, (byte) 0x15,
- (byte) 0x2e
+ (byte) 0x01, (byte) 0x05, (byte) 0x92, (byte) 0x1e,
+ (byte) 0x21, (byte) 0x72, (byte) 0xc3, (byte) 0x05,
+ (byte) 0x0b, (byte) 0xa4, (byte) 0xc9, (byte) 0xd2,
+ (byte) 0xe7, (byte) 0x44, (byte) 0xfc, (byte) 0x5b,
+ (byte) 0x7b, (byte) 0x5e, (byte) 0x84, (byte) 0x51,
+ (byte) 0x75, (byte) 0x1e, (byte) 0x67, (byte) 0x80,
+ (byte) 0xc6, (byte) 0xde, (byte) 0x88, (byte) 0x22,
+ (byte) 0x94, (byte) 0x97, (byte) 0xbe, (byte) 0x7d,
+ (byte) 0x23, (byte) 0x55, (byte) 0x0b, (byte) 0xee,
+ (byte) 0xfa, (byte) 0x0c, (byte) 0xb7, (byte) 0xfa,
+ (byte) 0xfe, (byte) 0xbb, (byte) 0x4d, (byte) 0xd9,
+ (byte) 0xfa, (byte) 0xd1, (byte) 0x24, (byte) 0x4c,
+ (byte) 0x67, (byte) 0x33, (byte) 0xbe, (byte) 0xfe,
+ (byte) 0x5a, (byte) 0x97, (byte) 0x71, (byte) 0x0f,
+ (byte) 0x0d, (byte) 0xc5, (byte) 0x6d, (byte) 0xc0,
+ (byte) 0x8d, (byte) 0x9d, (byte) 0x9d, (byte) 0xf9,
+ (byte) 0xd8, (byte) 0x46
};
diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java
index d653c10..e63bcf2 100644
--- a/src/applets/SimpleECCApplet.java
+++ b/src/applets/SimpleECCApplet.java
@@ -363,6 +363,7 @@ public class SimpleECCApplet extends Applet {
}
if (sw != ISO7816.SW_NO_ERROR) {
testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_ANOMALOUSCUVE;
+ testFlags &= ~FLAG_ECTEST_ECDH_AGREEMENT_SMALL_DEGREE_POINT;
}
}
Util.setShort(buffer, bufferOffset, sw);
--
cgit v1.3.1
From 5f0ec2706a9dbb6aa72ea3c7eb798cd83045e867 Mon Sep 17 00:00:00 2001
From: J08nY
Date: Sat, 10 Dec 2016 23:32:41 +0100
Subject: Added CLI options, Apache commons-cli lib, anomalous curve key export
- Added CLI options, see SimpleAPDU.jar -h - Added Apache commons-cli for
CLI options, it uses Apache license - Added support for anomalous curve
export both reader/applet side: `java -jar SimpleAPDU.jar -g 10 -b 256 -fp
-s` generates 10 curves over ECSP256 an anomalous 256bit curve.
---
!uploader/simpleECC.cap | Bin 18215 -> 18296 bytes
README.md | 39 ++--
dist/SimpleAPDU.jar | Bin 3078963 -> 470966 bytes
lib/commons-cli-1.3.1-javadoc.jar | Bin 0 -> 169756 bytes
lib/commons-cli-1.3.1.jar | Bin 0 -> 52988 bytes
src/applets/ECKeyGenerator.java | 5 +-
src/applets/SimpleECCApplet.java | 23 ++-
src/simpleapdu/CardMngr.java | 7 +-
src/simpleapdu/DirtyLogger.java | 12 +-
src/simpleapdu/SimpleAPDU.java | 419 +++++++++++++++++++++-----------------
10 files changed, 285 insertions(+), 220 deletions(-)
create mode 100644 lib/commons-cli-1.3.1-javadoc.jar
create mode 100644 lib/commons-cli-1.3.1.jar
(limited to 'src/applets/SimpleECCApplet.java')
diff --git a/!uploader/simpleECC.cap b/!uploader/simpleECC.cap
index e76168c..52fef17 100644
Binary files a/!uploader/simpleECC.cap and b/!uploader/simpleECC.cap differ
diff --git a/README.md b/README.md
index df80750..ad76fc1 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ Tests support and behavior of smartcards with JavaCard platform with focus on El
Usage
------
1. Upload simpleECC.cap using your favorite tool (e.g., [GlobalPlatformPro tool](https://github.com/martinpaljak/GlobalPlatform))
-2. Run java -jar SimpleAPDU.jar
+2. Run `java -jar SimpleAPDU.jar`
3. Inspect output log with annotated results
Following operations are tested:
@@ -14,24 +14,33 @@ Following operations are tested:
- Generation of keypair with default curve
- Setting of custom curve and keypair generation
- Generation of shared secret via ECDH
-- Behavior of card when invalid curves/points are provided (shoudl fail)
+- Signature via ECDSA
+- Behavior of card when invalid curves/points are provided (should fail)
+
+See `java -jar SimpleAPDU.jar -h` for more.
Example output
--------------
- EC type: ALG_EC_FP
- EC key length (bits): 224 bits
- KeyPair object allocation: OK (0x9000)
- Generate key with def curve (fails if no def): fail (ILLEGAL_VALUE, 0x1)
- Set valid custom curve: OK (0x9000)
- Generate key with valid curve: OK (0x9000)
- !!ECDH agreement with valid point: fail (0x6f00)
- ECDH agreement with invalid point (fail is good): fail (unknown, 0x6f00)
- Set invalid custom curve (fail is good): fail (ILLEGAL_VALUE, 0x1)
- Generate key with invalid curve (fail is good): fail (skipped, 0xee1)
-
-
-*Explanation: ALG_EC_FP with 224b curve was tested. Is supported by card (KeyPair object allocation: OK), don't have preset default curve (Generate key with def curve: fail), custom curve can be set (Set valid custom curve: OK), new keypair can be generated (Generate key with valid curve: OK), ECDH key agreement failed to execute (ECDH agreement with valid point: fail) altough it was supposed to suceed (log line is therefore marked with !!), ECDH wil fail (expected behavior) if invalid point is provided (ECDH agreement with invalid point: fail), invalid custom curve cannot be set (expected behavior) (Set invalid custom curve: fail) and new keypair cannot be generated with invalid curve (Generate key with invalid curve: skipped) - last test was skipped as invalid curve canot be set.*
+ ### Test for support and with valid and invalid EC curves
+ EC type: ALG_EC_FP
+ EC key length (bits): 256 bits
+ KeyPair object allocation: OK (0x9000)
+ Generate key with def curve (fails if no def): OK (0x9000)
+ Set valid custom curve: OK (0x9000)
+ Generate key with valid curve: OK (0x9000)
+ !! ECDH agreement with valid point: fail (unknown, 0x6f00)
+ ECDH agreement with invalid point (fail is good): fail (ILLEGAL_VALUE, 0x 1)
+ ECDSA signature on random data: OK (0x9000)
+ Set anomalous custom curve (may fail): OK (0x9000)
+ Generate key with anomalous curve (may fail): fail (unknown, 0x6f00)
+ ECDH agreement with small order point (fail is good):fail (skipped, 0x ee1)
+ Set invalid custom curve (may fail): OK (0x9000)
+ Generate key with invalid curve (fail is good): fail (unknown, 0x6f00)
+ Set invalid field (may fail): OK (0x9000)
+ Generate key with invalid field (fail si good): fail (unknown, 0x6f00)
+
+*Explanation: ALG_EC_FP with 256b curve was tested. Is supported by card (KeyPair object allocation: OK), don't have preset default curve (Generate key with def curve: fail), custom curve can be set (Set valid custom curve: OK), new keypair can be generated (Generate key with valid curve: OK), ECDH key agreement failed to execute (ECDH agreement with valid point: fail) although it was supposed to succeed (log line is therefore marked with !!), ECDH wil fail (expected behavior) if invalid point is provided (ECDH agreement with invalid point: fail), ECDSA signature worked and verified correctly (ECDSA signature on random data: OK), anomalous curve can be set (Set anomalous custom curve: OK), however generating a key on it will fail (Generate key with anomalous curve: fail), ECDH with small-order public key provided will fail as intended (ECDH agreement with small order point: fail), invalid custom curve could be set (Set invalid custom curve: OK), new keypair cannot be generated with invalid curve (Generate key with invalid curve: fail), invalid field (non-prime) could be set (Set invalid field: OK), however a key could not be generated (Generate key with invalid field: fail).*
If you are interested in testing support for other JavaCard algorithms, please visit JCAlgTester project: https://github.com/crocs-muni/JCAlgTest
diff --git a/dist/SimpleAPDU.jar b/dist/SimpleAPDU.jar
index 09b7e73..1e96259 100644
Binary files a/dist/SimpleAPDU.jar and b/dist/SimpleAPDU.jar differ
diff --git a/lib/commons-cli-1.3.1-javadoc.jar b/lib/commons-cli-1.3.1-javadoc.jar
new file mode 100644
index 0000000..c741ec4
Binary files /dev/null and b/lib/commons-cli-1.3.1-javadoc.jar differ
diff --git a/lib/commons-cli-1.3.1.jar b/lib/commons-cli-1.3.1.jar
new file mode 100644
index 0000000..c3e7a1f
Binary files /dev/null and b/lib/commons-cli-1.3.1.jar differ
diff --git a/src/applets/ECKeyGenerator.java b/src/applets/ECKeyGenerator.java
index c4b71c0..491cd86 100644
--- a/src/applets/ECKeyGenerator.java
+++ b/src/applets/ECKeyGenerator.java
@@ -22,7 +22,6 @@ public class ECKeyGenerator {
public static final byte KEY_BOTH = KEY_PUBLIC | KEY_PRIVATE;
- //TODO: add something like allocateGenerate, or modify allocate to auto-generate a key-pair if it returns null key references after allocating
public short allocatePair(byte keyClass, short keyLength) {
short result = ISO7816.SW_NO_ERROR;
try {
@@ -105,6 +104,10 @@ public class ECKeyGenerator {
return sw;
}
+ public short setCustomAnomalousCurve(short keyClass, short keyLength, byte[] buffer, short offset) {
+ return setCustomCurve(EC_Consts.getAnomalousCurve(keyClass, keyLength), buffer, offset);
+ }
+
public short setParameter(byte key, short param, byte[] data, short offset, short length) {
short result = ISO7816.SW_NO_ERROR;
try {
diff --git a/src/applets/SimpleECCApplet.java b/src/applets/SimpleECCApplet.java
index e63bcf2..ecdfa4e 100644
--- a/src/applets/SimpleECCApplet.java
+++ b/src/applets/SimpleECCApplet.java
@@ -359,7 +359,7 @@ public class SimpleECCApplet extends Applet {
sw = SW_SKIPPED;
if ((testFlags & FLAG_ECTEST_SET_ANOMALOUSCURVE) != (short) 0) {
if (keyClass == KeyPair.ALG_EC_FP) { //Only FP supported at the moment
- sw = ecKeyGenerator.setCustomCurve(EC_Consts.getAnomalousCurve(keyClass, keyLen), m_ramArray, (short) 0);
+ sw = ecKeyGenerator.setCustomAnomalousCurve(keyClass, keyLen, m_ramArray, (short) 0);
}
if (sw != ISO7816.SW_NO_ERROR) {
testFlags &= ~FLAG_ECTEST_GENERATE_KEYPAIR_ANOMALOUSCUVE;
@@ -915,21 +915,30 @@ public class SimpleECCApplet extends Applet {
void GenerateAndReturnKey(APDU apdu) {
byte[] apdubuf = apdu.getBuffer();
apdu.setIncomingAndReceive();
+
+ short offset = ISO7816.OFFSET_CDATA;
+ byte keyClass = apdubuf[offset];
+ offset++;
- short bitLen = Util.getShort(apdubuf, ISO7816.OFFSET_CDATA);
+ short keyLength = Util.getShort(apdubuf, offset);
+ offset+=2;
- short offset = 0;
+ byte anomalous = apdubuf[offset];
+
+ offset = 0;
switch (apdubuf[ISO7816.OFFSET_P1]) {
case P1_SETCURVE: {
- ecKeyGenerator.allocatePair(KeyPair.ALG_EC_FP, bitLen);
+ ecKeyGenerator.allocatePair(keyClass, keyLength);
+ 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();
-
- // If required, initialize curve parameters first
- ecKeyGenerator.setCustomCurve(KeyPair.ALG_EC_FP, bitLen, m_ramArray, (short) 0);
break;
}
case P1_GENERATEKEYPAIR: {
diff --git a/src/simpleapdu/CardMngr.java b/src/simpleapdu/CardMngr.java
index d778bc8..ae8bd55 100644
--- a/src/simpleapdu/CardMngr.java
+++ b/src/simpleapdu/CardMngr.java
@@ -105,7 +105,10 @@ public class CardMngr {
return true;
}
-
+
+ public boolean isConnected() {
+ return m_card != null;
+ }
public void DisconnectFromCard() throws Exception {
if (m_card != null) {
@@ -191,7 +194,7 @@ public class CardMngr {
if (responseAPDU.getSW1() == (byte) 0x61) {
CommandAPDU apduToSend = new CommandAPDU((byte) 0x00,
(byte) 0xC0, (byte) 0x00, (byte) 0x00,
- (int) responseAPDU.getSW1());
+ responseAPDU.getSW1());
responseAPDU = m_channel.transmit(apduToSend);
System.out.println(bytesToHex(responseAPDU.getBytes()));
diff --git a/src/simpleapdu/DirtyLogger.java b/src/simpleapdu/DirtyLogger.java
index 69e5e65..c06571b 100644
--- a/src/simpleapdu/DirtyLogger.java
+++ b/src/simpleapdu/DirtyLogger.java
@@ -1,14 +1,7 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
package simpleapdu;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
/**
*
@@ -17,6 +10,7 @@ import java.util.logging.Logger;
public class DirtyLogger {
FileOutputStream m_logFile;
boolean m_bOutputSystemOut = true;
+
public DirtyLogger(FileOutputStream logFile, boolean bOutputSystemOut) {
m_logFile = logFile;
m_bOutputSystemOut = bOutputSystemOut;
@@ -47,4 +41,8 @@ public class DirtyLogger {
} catch (IOException ex) {
}
}
+
+ void close() throws IOException {
+ m_logFile.close();
+ }
}
diff --git a/src/simpleapdu/SimpleAPDU.java b/src/simpleapdu/SimpleAPDU.java
index 44bf302..241ef4c 100644
--- a/src/simpleapdu/SimpleAPDU.java
+++ b/src/simpleapdu/SimpleAPDU.java
@@ -5,7 +5,13 @@ import applets.SimpleECCApplet;
import javacard.framework.ISO7816;
import javacard.security.CryptoException;
import javacard.security.KeyPair;
-import sun.java2d.pipe.SpanShapeRenderer;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
import javax.smartcardio.ResponseAPDU;
import java.io.FileNotFoundException;
@@ -18,13 +24,17 @@ import java.util.Arrays;
* @author Petr Svenda petr@svenda.com
*/
public class SimpleAPDU {
- static CardMngr cardManager = new CardMngr();
+ private CardMngr cardManager = new CardMngr();
+ private DirtyLogger systemOutLogger = null;
+
+ private CommandLineParser cliParser = new DefaultParser();
+ private Options opts = new Options();
+ private static final String cliHeader = "";
+ private static final String cliFooter = "";
private final static byte SELECT_ECTESTERAPPLET[] = {(byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, (byte) 0x0a,
(byte) 0x45, (byte) 0x43, (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x30, (byte) 0x31};
- static DirtyLogger m_SystemOutLogger = null;
-
private static final byte TESTECSUPPORTALL_FP[] = {(byte) 0xB0, (byte) 0x5E, (byte) 0x00, (byte) 0x00, (byte) 0x00};
private static final byte TESTECSUPPORTALL_F2M[] = {(byte) 0xB0, (byte) 0x5F, (byte) 0x00, (byte) 0x00, (byte) 0x00};
private static final byte TESTECSUPPORT_GIVENALG[] = {(byte) 0xB0, (byte) 0x71, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00};
@@ -38,8 +48,104 @@ public class SimpleAPDU {
private static final short INVALIDCURVEB_CORRUPTIONTYPE_OFFSET = 7;
private static final short INVALIDCURVEB_REWINDONSUCCESS_OFFSET = 9;
- private static final byte TESTECSUPPORT_GENERATEECCKEY[] = {(byte) 0xB0, (byte) 0x5a, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x00};
+ private static final byte TESTECSUPPORT_GENERATEECCKEY[] = {(byte) 0xB0, (byte) 0x5a, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
+ private static final short GENERATEECKEY_ALG_OFFSET = 5;
+ private static final short GENERATEECKEY_KEYLENGTH_OFFSET = 6;
+ private static final short GENERATEECKEY_ANOMALOUS_OFFSET = 8;
+
+
+ public void run(String[] args) {
+ try {
+ //parse cmd args
+ CommandLine cli = parseArgs(args);
+
+ //byte[] installData = new byte[10];
+ //byte[] AID = {(byte) 0x4C, (byte) 0x61, (byte) 0x62, (byte) 0x61, (byte) 0x6B, (byte) 0x41, (byte) 0x70, (byte) 0x70, (byte) 0x6C, (byte) 0x65, (byte) 0x74};
+ //cardManager.prepareLocalSimulatorApplet(AID, installData, SimpleECCApplet.class);
+
+ //do stuff
+ if (cli.hasOption("help")) {
+ HelpFormatter help = new HelpFormatter();
+ help.printHelp("SimpleAPDU", cliHeader, opts, cliFooter);
+ } else {
+ //open log(only when actually doing something)
+ String logFileName = cli.getOptionValue("output-file", String.format("ECTESTER_log_%d.log", System.currentTimeMillis()));
+ FileOutputStream stdoutStream = new FileOutputStream(logFileName);
+ systemOutLogger = new DirtyLogger(stdoutStream, true);
+
+ boolean fp = cli.hasOption("fp");
+ boolean f2m = cli.hasOption("f2m");
+ if (!fp && !f2m) {
+ fp = true;
+ f2m = true;
+ }
+ int genAmount = Integer.parseInt(cli.getOptionValue("generate", "0"));
+ int keyLength = Integer.parseInt(cli.getOptionValue("b", "192"));
+
+ if (cli.hasOption("generate")) {
+ //generate EC keys
+ if (fp) {
+ generateECKeys(genAmount, KeyPair.ALG_EC_FP, (short) keyLength, cli.hasOption("anomalous"));
+ }
+ if (f2m) {
+ generateECKeys(genAmount, KeyPair.ALG_EC_F2M, (short) keyLength, cli.hasOption("anomalous"));
+ }
+ } else if (cli.hasOption("test")) {
+ if (cli.hasOption("bit-length")) {
+ //test only one bitsize
+ if (fp) {
+ testSupportECFp((short) keyLength);
+ }
+ if (f2m) {
+ testSupportECFp((short) keyLength);
+ }
+ } else {
+ //test default bit sizes
+ testSupportECAll(fp, f2m);
+ testFPkeyGen((short) 10, EC_Consts.CORRUPTION_ONEBYTERANDOM, true);
+ }
+ } else {
+ systemOutLogger.println("You need to specify one of -t / -g [num] commands.");
+ }
+
+ //close log
+ systemOutLogger.close();
+ }
+
+ //disconnect
+ cardManager.DisconnectFromCard();
+ } catch (Exception ex) {
+ if (systemOutLogger != null) {
+ systemOutLogger.println("Exception : " + ex);
+ }
+ }
+ }
+ private CommandLine parseArgs(String[] args) throws ParseException {
+
+ opts.addOption("h", "help", false, "show this help");
+ opts.addOption(Option.builder("g")
+ .longOpt("generate")
+ .hasArg()
+ .optionalArg(true)
+ .argName("num")
+ .desc("generate EC keys").build());
+ opts.addOption("t", "test", false, "test EC support (default)");
+ opts.addOption(Option.builder("b")
+ .longOpt("bit-length")
+ .hasArg()
+ .argName("bits")
+ .desc("set EC bit size").build());
+ opts.addOption("f2m", "use EC over binary-fields");
+ opts.addOption("fp", "user EC over prime-fields (default)");
+ opts.addOption("s", "anomalous", false, "generate anomalous (non-prime order, small pubkey order) curves");
+ opts.addOption(Option.builder("o")
+ .longOpt("output-file")
+ .hasArg()
+ .argName("file")
+ .desc("output file to log to").build());
+ return cliParser.parse(opts, args);
+ }
static short getShort(byte[] array, int offset) {
return (short) (((array[offset] & 0xFF) << 8) | (array[offset + 1] & 0xFF));
@@ -50,190 +156,125 @@ public class SimpleAPDU {
array[offset] = (byte) ((value >> 8) & 0xFF);
}
- static void testFPkeyGen_setNumRepeats(byte[] apduArray, short numRepeats) {
- setShort(apduArray, INVALIDCURVEB_NUMREPEATS_OFFSET, numRepeats);
- }
-
- static void testFPkeyGen_setCorruptionType(byte[] apduArray, short corruptionType) {
- setShort(apduArray, INVALIDCURVEB_CORRUPTIONTYPE_OFFSET, corruptionType);
- }
-
- static void testFPkeyGen_rewindOnSuccess(byte[] apduArray, boolean bRewind) {
- apduArray[INVALIDCURVEB_REWINDONSUCCESS_OFFSET] = bRewind ? (byte) 1 : (byte) 0;
- }
+ private boolean ReconnnectToCard() throws Exception {
+ if (cardManager.isConnected()) {
+ cardManager.DisconnectFromCard();
+ }
- static CardMngr ReconnnectToCard() throws Exception {
- cardManager.DisconnectFromCard();
- if (cardManager.ConnectToCard()) {
+ boolean result = cardManager.ConnectToCard();
+ if (result) {
// Select our application on card
cardManager.sendAPDU(SELECT_ECTESTERAPPLET);
}
- return cardManager;
+ return result;
}
- static void testSupportECGivenAlg(byte[] apdu, CardMngr cardManager) throws Exception {
+ private void testFPkeyGen(short numRepeats, short corruptionType, boolean bRewind) throws Exception {
+ byte[] apdu = Arrays.copyOf(TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB, TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB.length);
+ setShort(apdu, INVALIDCURVEB_NUMREPEATS_OFFSET, numRepeats);
+ setShort(apdu, INVALIDCURVEB_CORRUPTIONTYPE_OFFSET, corruptionType);
+ apdu[INVALIDCURVEB_REWINDONSUCCESS_OFFSET] = bRewind ? (byte) 1 : (byte) 0;
+
+ ReconnnectToCard();
+ ResponseAPDU resp_fp_keygen = cardManager.sendAPDU(apdu);
+ ResponseAPDU resp_keygen_params = cardManager.sendAPDU(TESTECSUPPORTALL_LASTUSEDPARAMS);
+ PrintECKeyGenInvalidCurveB(resp_fp_keygen);
+ PrintECKeyGenInvalidCurveB_lastUserParams(resp_keygen_params);
+ }
+
+ private void testSupportECGivenAlg(short keyLength, byte keyClass) throws Exception {
+ byte[] apdu = Arrays.copyOf(TESTECSUPPORT_GIVENALG, TESTECSUPPORT_GIVENALG.length);
+ apdu[TESTECSUPPORT_ALG_OFFSET] = keyClass;
+ setShort(apdu, TESTECSUPPORT_KEYLENGTH_OFFSET, keyLength);
+
ReconnnectToCard();
ResponseAPDU resp = cardManager.sendAPDU(apdu);
//byte[] resp = cardManager.sendAPDUSimulator(apdu);
PrintECSupport(resp);
}
- static void testSupportECAll(CardMngr cardManager) throws Exception {
- byte[] testAPDU = Arrays.copyOf(TESTECSUPPORT_GIVENALG, TESTECSUPPORT_GIVENALG.length);
-
- testAPDU[TESTECSUPPORT_ALG_OFFSET] = KeyPair.ALG_EC_FP;
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 128);
- testSupportECGivenAlg(testAPDU, cardManager);
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 160);
- testSupportECGivenAlg(testAPDU, cardManager);
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 192);
- testSupportECGivenAlg(testAPDU, cardManager);
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 224);
- testSupportECGivenAlg(testAPDU, cardManager);
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 256);
- testSupportECGivenAlg(testAPDU, cardManager);
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 384);
- testSupportECGivenAlg(testAPDU, cardManager);
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 521);
- testSupportECGivenAlg(testAPDU, cardManager);
-
- testAPDU[TESTECSUPPORT_ALG_OFFSET] = KeyPair.ALG_EC_F2M;
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 113);
- testSupportECGivenAlg(testAPDU, cardManager);
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 131);
- testSupportECGivenAlg(testAPDU, cardManager);
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 163);
- testSupportECGivenAlg(testAPDU, cardManager);
- setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 193);
- testSupportECGivenAlg(testAPDU, cardManager);
-
+ private void testSupportECFp(short keyLength) throws Exception {
+ testSupportECGivenAlg(keyLength, KeyPair.ALG_EC_FP);
}
- public static void main(String[] args) throws FileNotFoundException, IOException {
- //parse cli args. Should be replaced with some cli parsing library code in the future.
- boolean genKeys = false;
- int genAmount = 0;
- boolean testAll = false;
- if (args.length > 0) {
- for (int i = 0; i < args.length; i++) {
- if (args[i].equals("-g")) {
- genKeys = true;
- if (args.length >= i + 1) {
- try {
- genAmount = Integer.parseInt(args[i + 1]);
- }catch (NumberFormatException ignored) {
- //is another param, genAmount = 0 by default
- genAmount = 0;
- }
+ private void testSupportECF2m(short keyLength) throws Exception {
+ testSupportECGivenAlg(keyLength, KeyPair.ALG_EC_F2M);
+ }
- }
- } else if (args[i].equals("-a")) {
- testAll = true;
- }
- }
+ private void testSupportECAll(boolean testFp, boolean testF2m) throws Exception {
+ if (testFp) {
+ testSupportECFp((short) 128);
+ testSupportECFp((short) 192);
+ testSupportECFp((short) 224);
+ testSupportECFp((short) 256);
+ testSupportECFp((short) 384);
+ testSupportECFp((short) 521);
}
- //by default do the test
- if (!genKeys && !testAll) {
- testAll = true;
+ if (testF2m) {
+ testSupportECF2m((short) 113);
+ testSupportECF2m((short) 131);
+ testSupportECF2m((short) 163);
+ testSupportECF2m((short) 193);
}
+ }
+ private void generateECKeys(int amount, byte keyClass, short keyLength, boolean anomalous) throws Exception {
+ if (cardManager.ConnectToCardSelect()) {
+ cardManager.sendAPDU(SELECT_ECTESTERAPPLET);
- String logFileName = String.format("ECTESTER_log_%d.log", System.currentTimeMillis());
- FileOutputStream systemOutLogger = new FileOutputStream(logFileName);
- m_SystemOutLogger = new DirtyLogger(systemOutLogger, true);
-
- try {
- if (testAll) {
- //byte[] installData = new byte[10];
- //byte[] AID = {(byte) 0x4C, (byte) 0x61, (byte) 0x62, (byte) 0x61, (byte) 0x6B, (byte) 0x41, (byte) 0x70, (byte) 0x70, (byte) 0x6C, (byte) 0x65, (byte) 0x74};
- //cardManager.prepareLocalSimulatorApplet(AID, installData, SimpleECCApplet.class);
- if (cardManager.ConnectToCard()) {
-
- // Test all default curves for both fields
- testSupportECAll(cardManager);
-
- // Test setting invalid parameter B of curve
- byte[] testAPDU = Arrays.copyOf(TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB, TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB.length);
- testFPkeyGen_setCorruptionType(testAPDU, EC_Consts.CORRUPTION_ONEBYTERANDOM);
- testFPkeyGen_setNumRepeats(testAPDU, (short) 10);
- testFPkeyGen_rewindOnSuccess(testAPDU, true);
-
- ReconnnectToCard();
- ResponseAPDU resp_fp_keygen = cardManager.sendAPDU(testAPDU);
- ResponseAPDU resp_keygen_params = cardManager.sendAPDU(TESTECSUPPORTALL_LASTUSEDPARAMS);
- PrintECKeyGenInvalidCurveB(resp_fp_keygen);
- PrintECKeyGenInvalidCurveB_lastUserParams(resp_keygen_params);
-
- cardManager.DisconnectFromCard();
- } else {
- m_SystemOutLogger.println("Failed to connect to card");
+ String keyFileName = String.format("ECKEYS_%s_%d.log", keyClass == KeyPair.ALG_EC_FP ? "fp" : "f2m", System.currentTimeMillis());
+ FileOutputStream keysFile = new FileOutputStream(keyFileName);
+
+ String message = "index;time;pubW;privS\n";
+ keysFile.write(message.getBytes());
+ byte[] gatherKeyAPDU = Arrays.copyOf(TESTECSUPPORT_GENERATEECCKEY, TESTECSUPPORT_GENERATEECCKEY.length);
+ // Prepare keypair object
+ gatherKeyAPDU[ISO7816.OFFSET_P1] = SimpleECCApplet.P1_SETCURVE;
+ 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);
+
+ // Generate new keypair
+ gatherKeyAPDU[ISO7816.OFFSET_P1] = SimpleECCApplet.P1_GENERATEKEYPAIR;
+ int counter = 0;
+ while (true) {
+ counter++;
+ long elapsed = -System.nanoTime();
+ respGather = cardManager.sendAPDU(gatherKeyAPDU);
+ elapsed += System.nanoTime();
+
+ byte[] data = respGather.getData();
+ int offset = 0;
+ String pubKeyW = "";
+ String privKeyS = "";
+ if (data[offset] == EC_Consts.TAG_ECPUBKEY) {
+ offset++;
+ short len = getShort(data, offset);
+ offset += 2;
+ pubKeyW = CardMngr.bytesToHex(data, offset, len, false);
+ offset += len;
+ }
+ if (data[offset] == EC_Consts.TAG_ECPRIVKEY) {
+ offset++;
+ short len = getShort(data, offset);
+ offset += 2;
+ privKeyS = CardMngr.bytesToHex(data, offset, len, false);
+ offset += len;
}
- }
-
- if (genKeys) {
- // Gather large number of ECC keypairs
- if (cardManager.ConnectToCardSelect()) {
- cardManager.sendAPDU(SELECT_ECTESTERAPPLET);
-
- String keyFileName = String.format("ECKEYS_%d.log", System.currentTimeMillis());
- FileOutputStream keysFile = new FileOutputStream(keyFileName);
-
- String message = "index;time;pubW;privS\n";
- keysFile.write(message.getBytes());
- byte[] gatherKeyAPDU = Arrays.copyOf(TESTECSUPPORT_GENERATEECCKEY, TESTECSUPPORT_GENERATEECCKEY.length);
- // Prepare keypair object
- gatherKeyAPDU[ISO7816.OFFSET_P1] = SimpleECCApplet.P1_SETCURVE;
- setShort(gatherKeyAPDU, (short) 5, (short) 192); // ecc length
- ResponseAPDU respGather = cardManager.sendAPDU(gatherKeyAPDU);
-
- // Generate new keypair
- gatherKeyAPDU[ISO7816.OFFSET_P1] = SimpleECCApplet.P1_GENERATEKEYPAIR;
- int counter = 0;
- while (true) {
- counter++;
- long elapsed = -System.nanoTime();
- respGather = cardManager.sendAPDU(gatherKeyAPDU);
- elapsed += System.nanoTime();
-
- byte[] data = respGather.getData();
- int offset = 0;
- String pubKeyW = "";
- String privKeyS = "";
- if (data[offset] == EC_Consts.TAG_ECPUBKEY) {
- offset++;
- short len = getShort(data, offset);
- offset += 2;
- pubKeyW = CardMngr.bytesToHex(data, offset, len, false);
- offset += len;
- }
- if (data[offset] == EC_Consts.TAG_ECPRIVKEY) {
- offset++;
- short len = getShort(data, offset);
- offset += 2;
- privKeyS = CardMngr.bytesToHex(data, offset, len, false);
- offset += len;
- }
- message = String.format("%d;%d;%s;%s\n", counter, elapsed / 1000000, pubKeyW, privKeyS);
- keysFile.write(message.getBytes());
+ message = String.format("%d;%d;%s;%s\n", counter, elapsed / 1000000, pubKeyW, privKeyS);
+ keysFile.write(message.getBytes());
- m_SystemOutLogger.flush();
- keysFile.flush();
+ this.systemOutLogger.flush();
+ keysFile.flush();
- //stop when we have enough keys, go on forever with 0
- if (counter >= genAmount && genAmount != 0)
- break;
- }
- }
+ //stop when we have enough keys, go on forever with 0
+ if (counter >= amount && amount != 0)
+ break;
}
-
-
- } catch (Exception ex) {
- m_SystemOutLogger.println("Exception : " + ex);
}
-
- systemOutLogger.close();
}
static String getPrintError(short code) {
@@ -278,12 +319,12 @@ public class SimpleAPDU {
MUST_FAIL
}
- static int VerifyPrintResult(String message, byte expectedTag, byte[] buffer, int bufferOffset, ExpResult expRes) {
+ private int VerifyPrintResult(String message, byte expectedTag, byte[] buffer, int bufferOffset, ExpResult expRes) {
if (bufferOffset >= buffer.length) {
- m_SystemOutLogger.println(" No more data returned");
+ systemOutLogger.println(" No more data returned");
} else {
if (buffer[bufferOffset] != expectedTag) {
- m_SystemOutLogger.println(" ERROR: mismatched tag");
+ systemOutLogger.println(" ERROR: mismatched tag");
assert (buffer[bufferOffset] == expectedTag);
}
bufferOffset++;
@@ -298,22 +339,21 @@ public class SimpleAPDU {
bHiglight = true;
}
if (bHiglight) {
- m_SystemOutLogger.println(String.format("!! %-50s%s", message, getPrintError(resCode)));
+ systemOutLogger.println(String.format("!! %-53s%s", message, getPrintError(resCode)));
} else {
- m_SystemOutLogger.println(String.format(" %-50s%s", message, getPrintError(resCode)));
+ systemOutLogger.println(String.format(" %-53s%s", message, getPrintError(resCode)));
}
}
return bufferOffset;
}
- static void PrintECSupport(ResponseAPDU resp) {
+ private void PrintECSupport(ResponseAPDU resp) {
PrintECSupport(resp.getData());
}
- static void PrintECSupport(byte[] buffer) {
-
- m_SystemOutLogger.println();
- m_SystemOutLogger.println("### Test for support and with valid and invalid EC curves");
+ private void PrintECSupport(byte[] buffer) {
+ systemOutLogger.println();
+ systemOutLogger.println("### Test for support and with valid and invalid EC curves");
int bufferOffset = 0;
while (bufferOffset < buffer.length) {
assert (buffer[bufferOffset] == SimpleECCApplet.ECTEST_SEPARATOR);
@@ -325,10 +365,10 @@ public class SimpleAPDU {
if (buffer[bufferOffset] == KeyPair.ALG_EC_F2M) {
ecType = "ALG_EC_F2M";
}
- m_SystemOutLogger.println(String.format("%-53s%s", "EC type:", ecType));
+ systemOutLogger.println(String.format("%-56s%s", "EC type:", ecType));
bufferOffset++;
short keyLen = getShort(buffer, bufferOffset);
- m_SystemOutLogger.println(String.format("%-53s%d bits", "EC key length (bits):", keyLen));
+ systemOutLogger.println(String.format("%-56s%d bits", "EC key length (bits):", keyLen));
bufferOffset += 2;
bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
@@ -346,18 +386,17 @@ public class SimpleAPDU {
bufferOffset = VerifyPrintResult("Set invalid field (may fail):", SimpleECCApplet.ECTEST_SET_INVALIDFIELD, buffer, bufferOffset, ExpResult.MAY_FAIL);
bufferOffset = VerifyPrintResult("Generate key with invalid field (fail si good):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDFIELD, buffer, bufferOffset, ExpResult.MUST_FAIL);
- m_SystemOutLogger.println();
+ systemOutLogger.println();
}
}
- static void PrintECKeyGenInvalidCurveB(ResponseAPDU resp) {
+ private void PrintECKeyGenInvalidCurveB(ResponseAPDU resp) {
PrintECKeyGenInvalidCurveB(resp.getData());
}
- static void PrintECKeyGenInvalidCurveB(byte[] buffer) {
-
- m_SystemOutLogger.println();
- m_SystemOutLogger.println("### Test for computation with invalid parameter B for EC curve");
+ private void PrintECKeyGenInvalidCurveB(byte[] buffer) {
+ systemOutLogger.println();
+ systemOutLogger.println("### Test for computation with invalid parameter B for EC curve");
int bufferOffset = 0;
while (bufferOffset < buffer.length) {
assert (buffer[bufferOffset] == SimpleECCApplet.ECTEST_SEPARATOR);
@@ -369,15 +408,15 @@ public class SimpleAPDU {
if (buffer[bufferOffset] == KeyPair.ALG_EC_F2M) {
ecType = "ALG_EC_F2M";
}
- m_SystemOutLogger.println(String.format("%-53s%s", "EC type:", ecType));
+ systemOutLogger.println(String.format("%-53s%s", "EC type:", ecType));
bufferOffset++;
short keyLen = getShort(buffer, bufferOffset);
- m_SystemOutLogger.println(String.format("%-53s%d bits", "EC key length (bits):", keyLen));
+ systemOutLogger.println(String.format("%-53s%d bits", "EC key length (bits):", keyLen));
bufferOffset += 2;
short numRepeats = getShort(buffer, bufferOffset);
bufferOffset += 2;
- m_SystemOutLogger.println(String.format("%-53s%d times", "Executed repeats before unexpected error: ", numRepeats));
+ systemOutLogger.println(String.format("%-53s%d times", "Executed repeats before unexpected error: ", numRepeats));
bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
while (bufferOffset < buffer.length) {
@@ -390,18 +429,22 @@ public class SimpleAPDU {
bufferOffset = VerifyPrintResult("Generate key with valid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEED);
}
- m_SystemOutLogger.println();
+ systemOutLogger.println();
}
}
- static void PrintECKeyGenInvalidCurveB_lastUserParams(ResponseAPDU resp) {
+ private void PrintECKeyGenInvalidCurveB_lastUserParams(ResponseAPDU resp) {
byte[] buffer = resp.getData();
short offset = 0;
- m_SystemOutLogger.print("Last used value of B: ");
+ systemOutLogger.print("Last used value of B: ");
while (offset < buffer.length) {
- m_SystemOutLogger.print(String.format("%x ", buffer[offset]));
+ systemOutLogger.print(String.format("%x ", buffer[offset]));
offset++;
}
+ }
+ public static void main(String[] args) throws FileNotFoundException, IOException {
+ SimpleAPDU app = new SimpleAPDU();
+ app.run(args);
}
}
--
cgit v1.3.1
From da1ee8381f2f7eaf1cfcd4941ec9f95287435625 Mon Sep 17 00:00:00 2001
From: J08nY
Date: Sun, 11 Dec 2016 14:20:00 +0100
Subject: Fixed some issues with errors in key generation, and their
propagation
---
!uploader/simpleECC.cap | Bin 18296 -> 18330 bytes
dist/SimpleAPDU.jar | Bin 470966 -> 471079 bytes
src/applets/ECKeyGenerator.java | 8 ++---
src/applets/SimpleECCApplet.java | 63 ++++++++++++++++++++-------------------
src/simpleapdu/SimpleAPDU.java | 11 +++++++
5 files changed, 47 insertions(+), 35 deletions(-)
(limited to 'src/applets/SimpleECCApplet.java')
diff --git a/!uploader/simpleECC.cap b/!uploader/simpleECC.cap
index 52fef17..886ce88 100644
Binary files a/!uploader/simpleECC.cap and b/!uploader/simpleECC.cap differ
diff --git a/dist/SimpleAPDU.jar b/dist/SimpleAPDU.jar
index 1e96259..be5599a 100644
Binary files a/dist/SimpleAPDU.jar and b/dist/SimpleAPDU.jar 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();
}
}
--
cgit v1.3.1
From 4debe5adb4bb486f488878e348ee7bcf386c43f2 Mon Sep 17 00:00:00 2001
From: J08nY
Date: Tue, 17 Jan 2017 02:55:31 +0100
Subject: major changes, ECTester rewrite, moved to valid package
reader: ECTester, mostly rewritten SimpleAPDU
- communication with applet now done through simpler
instructions: allocate, set, generate, ecdh, ecdsa
- moved to a valid Java package dir cz.crcs.ectester
- SimpleAPDU: renamed to ECTester
- CardMngr: seamlessly supports simulation vs real card
- DirtyLogger: takes a nullable String and creates file
- ECTester: currently only supports key generation,
curve testing under way
- supports external curve setting, example files
in data package
- tests can be done through files, to achieve a more
modular approach
- Util: static utility class
- ParamReader: reads curve domain parameters and keys from
simple csv-like human-readable files with hex strings
applet: ECTesterApplet, rewrite of SimpleECCApplet
- more granularity in instructions
- moved complexity over to the reader side
- ECKeyGenerator: now a class that takes KeyPair as param
- ECKeyTester: now a class that takes KeyPair as param
- EC_Consts: removed ecsp curves(now done externally),
removed unused methods
- ECTesterApplet: currently only tested instructions are:
allocate, set, generate
data: contains several curve and pubkey files in format supported
by ParamReader
- Prime field curves:
p,a,b,gx,gy,r,k
- Binary field curves:
e1,a,b,gx,gy,r,k or e1,e2,e3,a,b,gx,gy,r,k
- Public key:
wx,wy
- Private key:
s
- Key:
wx,wy,s
- all values are hex strings
---
!uploader/ectester.cap | Bin 0 -> 13349 bytes
!uploader/gppro_upload.bat | 2 +-
!uploader/gppro_upload.sh | 2 +-
!uploader/gppro_upload_emv.bat | 2 +-
!uploader/gppro_upload_emv.sh | 8 +-
README.md | 6 +-
build.xml | 22 +
dist/ECTester.jar | Bin 0 -> 190873 bytes
dist/SimpleAPDU.jar | Bin 471079 -> 0 bytes
dist/lib/commons-cli-1.3.1.jar | Bin 0 -> 52988 bytes
jcardsim-2.2.2-all.jar | Bin 394661 -> 0 bytes
jcbuild.xml | 20 +-
manifest.mf | 2 +
nbproject/project.properties | 17 +
src/applets/ECKeyGenerator.java | 276 ---
src/applets/ECKeyTester.java | 177 --
src/applets/EC_Consts.java | 2020 --------------------
src/applets/SimpleECCApplet.java | 1026 ----------
src/cz/crcs/ectester/applet/ECKeyGenerator.java | 381 ++++
src/cz/crcs/ectester/applet/ECKeyTester.java | 184 ++
src/cz/crcs/ectester/applet/ECTesterApplet.java | 427 +++++
src/cz/crcs/ectester/applet/EC_Consts.java | 1298 +++++++++++++
src/cz/crcs/ectester/data/ecsp128.csv | 7 +
src/cz/crcs/ectester/data/ecsp128_pub.csv | 2 +
src/cz/crcs/ectester/data/ecsp160.csv | 7 +
src/cz/crcs/ectester/data/ecsp160_pub.csv | 2 +
src/cz/crcs/ectester/data/ecsp192.csv | 7 +
src/cz/crcs/ectester/data/ecsp192_pub.csv | 2 +
src/cz/crcs/ectester/data/ecsp224.csv | 7 +
src/cz/crcs/ectester/data/ecsp224_pub.csv | 2 +
src/cz/crcs/ectester/data/ecsp256.csv | 7 +
src/cz/crcs/ectester/data/ecsp256_pub.csv | 2 +
src/cz/crcs/ectester/data/ecsp384.csv | 7 +
src/cz/crcs/ectester/data/ecsp384_pub.csv | 2 +
src/cz/crcs/ectester/data/ecsp521.csv | 7 +
src/cz/crcs/ectester/data/ecsp521_pub.csv | 2 +
src/cz/crcs/ectester/data/secp192k1.csv | 7 +
src/cz/crcs/ectester/data/secp192r1.csv | 7 +
src/cz/crcs/ectester/data/secp224r1.csv | 7 +
src/cz/crcs/ectester/data/secp256k1.csv | 7 +
src/cz/crcs/ectester/data/secp256r1.csv | 7 +
src/cz/crcs/ectester/data/secp384r1.csv | 7 +
src/cz/crcs/ectester/data/secp521r1.csv | 7 +
src/cz/crcs/ectester/data/sect163k1.csv | 9 +
src/cz/crcs/ectester/data/sect163r1.csv | 9 +
src/cz/crcs/ectester/data/sect163r2.csv | 9 +
src/cz/crcs/ectester/data/sect233k1.csv | 7 +
src/cz/crcs/ectester/data/sect233r1.csv | 7 +
src/cz/crcs/ectester/data/sect239k1.csv | 7 +
src/cz/crcs/ectester/data/sect283k1.csv | 9 +
src/cz/crcs/ectester/data/sect283r1.csv | 9 +
src/cz/crcs/ectester/data/sect409k1.csv | 7 +
src/cz/crcs/ectester/data/sect409r1.csv | 7 +
src/cz/crcs/ectester/data/sect571k1.csv | 9 +
src/cz/crcs/ectester/data/sect571r1.csv | 9 +
src/cz/crcs/ectester/reader/CardMngr.java | 289 +++
src/cz/crcs/ectester/reader/DirtyLogger.java | 55 +
src/cz/crcs/ectester/reader/ECTester.java | 489 +++++
.../crcs/ectester/reader/ISO7816_status_words.txt | 71 +
src/cz/crcs/ectester/reader/ParamReader.java | 134 ++
src/cz/crcs/ectester/reader/SimpleAPDU.java | 459 +++++
src/cz/crcs/ectester/reader/Util.java | 82 +
src/simpleapdu/CardMngr.java | 260 ---
src/simpleapdu/DirtyLogger.java | 48 -
src/simpleapdu/ISO7816_status_words.txt | 71 -
src/simpleapdu/SimpleAPDU.java | 460 -----
66 files changed, 4141 insertions(+), 4357 deletions(-)
create mode 100644 !uploader/ectester.cap
create mode 100644 dist/ECTester.jar
delete mode 100644 dist/SimpleAPDU.jar
create mode 100644 dist/lib/commons-cli-1.3.1.jar
delete mode 100644 jcardsim-2.2.2-all.jar
delete mode 100644 src/applets/ECKeyGenerator.java
delete mode 100644 src/applets/ECKeyTester.java
delete mode 100644 src/applets/EC_Consts.java
delete mode 100644 src/applets/SimpleECCApplet.java
create mode 100644 src/cz/crcs/ectester/applet/ECKeyGenerator.java
create mode 100644 src/cz/crcs/ectester/applet/ECKeyTester.java
create mode 100644 src/cz/crcs/ectester/applet/ECTesterApplet.java
create mode 100644 src/cz/crcs/ectester/applet/EC_Consts.java
create mode 100644 src/cz/crcs/ectester/data/ecsp128.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp128_pub.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp160.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp160_pub.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp192.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp192_pub.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp224.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp224_pub.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp256.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp256_pub.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp384.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp384_pub.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp521.csv
create mode 100644 src/cz/crcs/ectester/data/ecsp521_pub.csv
create mode 100644 src/cz/crcs/ectester/data/secp192k1.csv
create mode 100644 src/cz/crcs/ectester/data/secp192r1.csv
create mode 100644 src/cz/crcs/ectester/data/secp224r1.csv
create mode 100644 src/cz/crcs/ectester/data/secp256k1.csv
create mode 100644 src/cz/crcs/ectester/data/secp256r1.csv
create mode 100644 src/cz/crcs/ectester/data/secp384r1.csv
create mode 100644 src/cz/crcs/ectester/data/secp521r1.csv
create mode 100644 src/cz/crcs/ectester/data/sect163k1.csv
create mode 100644 src/cz/crcs/ectester/data/sect163r1.csv
create mode 100644 src/cz/crcs/ectester/data/sect163r2.csv
create mode 100644 src/cz/crcs/ectester/data/sect233k1.csv
create mode 100644 src/cz/crcs/ectester/data/sect233r1.csv
create mode 100644 src/cz/crcs/ectester/data/sect239k1.csv
create mode 100644 src/cz/crcs/ectester/data/sect283k1.csv
create mode 100644 src/cz/crcs/ectester/data/sect283r1.csv
create mode 100644 src/cz/crcs/ectester/data/sect409k1.csv
create mode 100644 src/cz/crcs/ectester/data/sect409r1.csv
create mode 100644 src/cz/crcs/ectester/data/sect571k1.csv
create mode 100644 src/cz/crcs/ectester/data/sect571r1.csv
create mode 100644 src/cz/crcs/ectester/reader/CardMngr.java
create mode 100644 src/cz/crcs/ectester/reader/DirtyLogger.java
create mode 100644 src/cz/crcs/ectester/reader/ECTester.java
create mode 100644 src/cz/crcs/ectester/reader/ISO7816_status_words.txt
create mode 100644 src/cz/crcs/ectester/reader/ParamReader.java
create mode 100644 src/cz/crcs/ectester/reader/SimpleAPDU.java
create mode 100644 src/cz/crcs/ectester/reader/Util.java
delete mode 100644 src/simpleapdu/CardMngr.java
delete mode 100644 src/simpleapdu/DirtyLogger.java
delete mode 100644 src/simpleapdu/ISO7816_status_words.txt
delete mode 100644 src/simpleapdu/SimpleAPDU.java
(limited to 'src/applets/SimpleECCApplet.java')
diff --git a/!uploader/ectester.cap b/!uploader/ectester.cap
new file mode 100644
index 0000000..f829de4
Binary files /dev/null and b/!uploader/ectester.cap differ
diff --git a/!uploader/gppro_upload.bat b/!uploader/gppro_upload.bat
index 0dd7864..cbfd5ae 100644
--- a/!uploader/gppro_upload.bat
+++ b/!uploader/gppro_upload.bat
@@ -1,6 +1,6 @@
gp.exe -delete 4543546573746572 -deletedeps -verbose
gp.exe -deletedeps -verbose -delete 4A43416C6754657374
-gp.exe -install simpleECC.cap -verbose
+gp.exe -install ectester.cap -verbose
diff --git a/!uploader/gppro_upload.sh b/!uploader/gppro_upload.sh
index faeaf24..47c7c97 100755
--- a/!uploader/gppro_upload.sh
+++ b/!uploader/gppro_upload.sh
@@ -2,6 +2,6 @@
java -jar gp.jar -delete 4543546573746572 -deletedeps -verbose
java -jar gp.jar -deletedeps -verbose -delete 4A43416C6754657374
-java -jar gp.jar -install simpleECC.cap -verbose
+java -jar gp.jar -install ectester.cap -verbose
diff --git a/!uploader/gppro_upload_emv.bat b/!uploader/gppro_upload_emv.bat
index a9b0b9c..737f8ac 100644
--- a/!uploader/gppro_upload_emv.bat
+++ b/!uploader/gppro_upload_emv.bat
@@ -3,6 +3,6 @@ gp.exe -deletedeps -verbose -emv -delete 4A43416C6754657374
gp.exe -deletedeps -verbose -emv -delete 4543546573746572
-gp.exe -install simpleECC.cap -verbose -emv -d
+gp.exe -install ectester.cap -verbose -emv -d
diff --git a/!uploader/gppro_upload_emv.sh b/!uploader/gppro_upload_emv.sh
index 9a51cd9..8f0c994 100755
--- a/!uploader/gppro_upload_emv.sh
+++ b/!uploader/gppro_upload_emv.sh
@@ -1,8 +1,8 @@
-java -jar gp.jar -deletedeps -verbose -emv -delete 4C6162616B417070
-java -jar gp.jar -deletedeps -verbose -emv -delete 4A43416C6754657374
-java -jar gp.jar -deletedeps -verbose -emv -delete 4543546573746572
+java -jar gp.jar --deletedeps --verbose -emv --delete 4C6162616B417070
+java -jar gp.jar --deletedeps --verbose -emv --delete 4A43416C6754657374
+java -jar gp.jar --deletedeps --verbose -emv --delete 4543546573746572
-java -jar gp.jar -install simpleECC.cap -verbose -emv -d
+java -jar gp.jar --install ectester.cap --verbose --emv -d
diff --git a/README.md b/README.md
index ad76fc1..c877a46 100644
--- a/README.md
+++ b/README.md
@@ -5,8 +5,8 @@ Tests support and behavior of smartcards with JavaCard platform with focus on El
Usage
------
-1. Upload simpleECC.cap using your favorite tool (e.g., [GlobalPlatformPro tool](https://github.com/martinpaljak/GlobalPlatform))
-2. Run `java -jar SimpleAPDU.jar`
+1. Upload ectester.cap using your favorite tool (e.g., [GlobalPlatformPro tool](https://github.com/martinpaljak/GlobalPlatform))
+2. Run `java -jar ectester.jar`
3. Inspect output log with annotated results
Following operations are tested:
@@ -17,7 +17,7 @@ Following operations are tested:
- Signature via ECDSA
- Behavior of card when invalid curves/points are provided (should fail)
-See `java -jar SimpleAPDU.jar -h` for more.
+See `java -jar ectester.jar -h` for more.
Example output
--------------
diff --git a/build.xml b/build.xml
index d788ca5..1fd4fcd 100644
--- a/build.xml
+++ b/build.xml
@@ -70,4 +70,26 @@
nbproject/build-impl.xml file.
-->
+
diff --git a/dist/ECTester.jar b/dist/ECTester.jar
new file mode 100644
index 0000000..2c14e00
Binary files /dev/null and b/dist/ECTester.jar differ
diff --git a/dist/SimpleAPDU.jar b/dist/SimpleAPDU.jar
deleted file mode 100644
index be5599a..0000000
Binary files a/dist/SimpleAPDU.jar and /dev/null differ
diff --git a/dist/lib/commons-cli-1.3.1.jar b/dist/lib/commons-cli-1.3.1.jar
new file mode 100644
index 0000000..c3e7a1f
Binary files /dev/null and b/dist/lib/commons-cli-1.3.1.jar differ
diff --git a/jcardsim-2.2.2-all.jar b/jcardsim-2.2.2-all.jar
deleted file mode 100644
index 0ac10c0..0000000
Binary files a/jcardsim-2.2.2-all.jar and /dev/null differ
diff --git a/jcbuild.xml b/jcbuild.xml
index 70d5370..9b1dabb 100644
--- a/jcbuild.xml
+++ b/jcbuild.xml
@@ -1,5 +1,5 @@
-
- * CDATA:
- * byte keyClass -> KeyPair.ALG_EC_FP or KeyPair.ALG_EC_F2\M
- * short keyLength
- * short fieldLength
- * short aLength
- * short bLength
- * short gxLength
- * short gyLength
- * short rLength
- * field -> FP: prime / F2M: three or one short representing the reduction polynomial
- * a
- * b
- * gx
- * gy
- * r
- * short k
- *
- * Response APDU format:
- * CDATA:
- * byte ECTEST_SEPARATOR
- * byte ECTEST_ALLOCATE_KEYPAIR
- * short sw
- * byte ECTEST_SET_EXTERNALCURVE
- * short sw
- * byte ECTEST_GENERATE_KEYPAIR_EXTERNALCURVE
- * short sw
- * byte ECTEST_ECDH_AGREEMENT_VALID_POINT
- * short sw
- * byte ECTEST_ECDH_AGREEMENT_INVALID_POINT
- * short sw
- * byte ECTEST_ECDSA_SIGNATURE
- * short sw
- *
- * @param apdu
- */
- void TestEC_SupportExternal(APDU apdu) {
- byte[] apdubuf = apdu.getBuffer();
- short len = apdu.setIncomingAndReceive();
-
- short offset = ISO7816.OFFSET_CDATA;
- byte keyClass = apdubuf[offset];
- ++offset;
- short keyLength = Util.getShort(apdubuf, offset);
- offset += 2;
-
- short dataLength = TestECSupportExternalCurve(keyClass, keyLength, apdubuf, offset, (short) 0);
-
- apdu.setOutgoingAndSend((short) 0, dataLength);
- }
-
-
- void TestEC_FP_GenerateInvalidCurve(APDU apdu) {
- byte[] apdubuf = apdu.getBuffer();
- short len = apdu.setIncomingAndReceive();
-
- short offset = ISO7816.OFFSET_CDATA;
- short repeats = Util.getShort(apdubuf, offset);
- offset += 2;
- short corruptionType = Util.getShort(apdubuf, offset);
- offset += 2;
- byte bRewindOnSuccess = apdubuf[offset];
- offset++;
-
- short dataOffset = 0;
-
- // FP
- dataOffset += TestECSupportInvalidCurve(KeyPair.ALG_EC_FP, (short) 160, apdubuf, dataOffset, repeats, corruptionType, bRewindOnSuccess);
-
- apdu.setOutgoingAndSend((short) 0, dataOffset);
- }
-
- short TestECSupportInvalidCurve(byte keyClass, short keyLen, byte[] buffer, short bufferOffset, short repeats, short corruptionType, byte bRewindOnSuccess) {
- short baseOffset = bufferOffset;
-
- short testFlags = FLAG_ECTEST_ALL;
-
- ecPubKey = null;
- ecPrivKey = null;
-
- buffer[bufferOffset] = ECTEST_SEPARATOR;
- bufferOffset++;
- buffer[bufferOffset] = keyClass;
- bufferOffset++;
- Util.setShort(buffer, bufferOffset, keyLen);
- bufferOffset += 2;
-
- short numExecutionsOffset = bufferOffset; // num executions to be stored later
- bufferOffset += 2;
-
- short sw;
-
- //
- // 1. Allocate KeyPair object
- //
- buffer[bufferOffset] = ECTEST_ALLOCATE_KEYPAIR;
- bufferOffset++;
- sw = SW_SKIPPED;
- if ((testFlags & FLAG_ECTEST_ALLOCATE_KEYPAIR) != (short) 0) {
- sw = ecKeyGenerator.allocatePair(keyClass, keyLen);
- if (sw == ISO7816.SW_NO_ERROR) {
- ecPrivKey = ecKeyGenerator.getPrivateKey();
- ecPubKey = ecKeyGenerator.getPublicKey();
- } else {
- testFlags = 0;
- }
-
- if (ecPubKey == null || ecPrivKey == null) {
- ecKeyGenerator.generatePair();
- ecPrivKey = ecKeyGenerator.getPrivateKey();
- ecPubKey = ecKeyGenerator.getPublicKey();
- }
- }
- Util.setShort(buffer, bufferOffset, sw);
- bufferOffset += 2;
-
-
- //
- // 2. Set invalid custom curve (many times)
- //
- sw = ecKeyGenerator.setCustomCurve(keyClass, keyLen, m_ramArray, (short) 0);
- ecPrivKey = ecKeyGenerator.getPrivateKey();
- ecPubKey = ecKeyGenerator.getPublicKey();
-
- m_lenB = ecPubKey.getB(m_ramArray2, (short) 0); //store valid B
-
- short startOffset = bufferOffset;
- short i;
- for (i = 0; i < repeats; i++) {
- if ((testFlags & FLAG_ECTEST_SET_INVALIDCURVE) != (short) 0) {
- if (bRewindOnSuccess == 1) {
- // if nothing unexpected happened, rewind bufferOffset back again
- bufferOffset = startOffset;
- }
-
- ecPubKey.getB(m_ramArray2, (short) 0); //store valid B
-
- // set invalid curve
- buffer[bufferOffset] = ECTEST_SET_INVALIDCURVE;
- bufferOffset++;
-
- // Supported types of invalid curve:
- // CORRUPTION_NONE = 0x01, valid parameter
- // CORRUPTION_FIXED = 0x02, first and last byte changed to a fixed value
- // CORRUPTION_FULLRANDOM = 0x03, completely random parameter data
- // CORRUPTION_ONEBYTERANDOM = 0x04, one random byte randomly changed
- // CORRUPTION_ZERO = 0x05, parameter competely zero
- // CORRUPTION_ONE = 0x06, parameter completely one
- sw = ecKeyGenerator.setCustomInvalidCurve(keyClass, keyLen, ECKeyGenerator.KEY_BOTH, EC_Consts.PARAMETER_B, corruptionType, m_ramArray, (short) 0);
- Util.setShort(buffer, bufferOffset, sw);
- bufferOffset += 2;
- if (sw != ISO7816.SW_NO_ERROR) {
- // if we reach this line, we are interested in value of B that caused incorrect response
- break; // stop execution, return B
- }
-
- // Gen key pair with invalid curve
-
- buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE;
- bufferOffset++;
- // Should fail
- sw = ecKeyGenerator.generatePair();
- Util.setShort(buffer, bufferOffset, sw);
- bufferOffset += 2;
-
- if (sw == ISO7816.SW_NO_ERROR) {
- // If this line is reached, we generated key pair - what should not happen
- buffer[bufferOffset] = ECTEST_DH_GENERATESECRET;
- bufferOffset++;
-
- ecPrivKey = ecKeyGenerator.getPrivateKey();
- ecPubKey = ecKeyGenerator.getPublicKey();
-
- sw = ecKeyTester.testECDH_validPoint(ecPrivKey, ecPubKey, m_ramArray, (short) 0, m_ramArray2, (short) 0);
- m_lenB = ecPubKey.getB(m_ramArray2, (short) 0); //store B
- //TODO: note, according to the previous version of this method, sw should get appended to the buffer only if sw != SW_NO_ERROR
- Util.setShort(buffer, bufferOffset, sw);
- bufferOffset += 2;
- break; //stop execution, return B
- }
-
- // Generate keypair with valid curve - to check that whole engine is not somehow blocked
- // after previous attempt with invalid curve
- //
- // set valid curve
- buffer[bufferOffset] = ECTEST_SET_VALIDCURVE;
- bufferOffset++;
- sw = ecKeyGenerator.setCustomCurve(keyClass, keyLen, m_ramArray, (short) 0);
-
- Util.setShort(buffer, bufferOffset, sw);
- bufferOffset += 2;
-
- // Gen key pair with valid curve
- buffer[bufferOffset] = ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE;
- bufferOffset++;
-
- sw = ecKeyGenerator.generatePair();
- Util.setShort(buffer, bufferOffset, sw);
- bufferOffset += 2;
- if (sw != ISO7816.SW_NO_ERROR) {
- break;
- }
-
- // If we reach this line => everything was as expected
- // Rewind offset in array back (no storage of info about expected runs)
- // bufferOffset = startOffset; done at beginning
- } else {
- Util.setShort(buffer, bufferOffset, SW_SKIPPED);
- bufferOffset += 2;
- }
- }
-
- // Set number of executed repeats
- Util.setShort(buffer, numExecutionsOffset, i);
-
- return (short) (bufferOffset - baseOffset);
- }
-
- //TODO: generalize invalid B setting to all curve params
- void TestECSupportInvalidCurve_lastUsedParams(APDU apdu) {
- byte[] apdubuf = apdu.getBuffer();
- apdu.setIncomingAndReceive();
-
- short offset = 0;
- Util.arrayCopyNonAtomic(m_ramArray2, (short) 0, apdubuf, offset, m_lenB);
- offset += m_lenB;
-
- apdu.setOutgoingAndSend((short) 0, offset);
- }
-
- void AllocateKeyPairReturnDefCurve(APDU apdu) {
- byte[] apdubuf = apdu.getBuffer();
- apdu.setIncomingAndReceive();
-
- short bitLen = Util.getShort(apdubuf, ISO7816.OFFSET_CDATA);
-
- // Note: all locations should happen in constructor. But here it is intentional
- // as we like to test for result of allocation
- ecKeyGenerator.allocatePair(KeyPair.ALG_EC_FP, bitLen);
-
- // If required, generate also new key pair
- if (apdubuf[ISO7816.OFFSET_P1] == (byte) 1) {
-
- // If required, initialize curve parameters first
- if (apdubuf[ISO7816.OFFSET_P2] == (byte) 2) {
- ecKeyGenerator.setCustomCurve(KeyPair.ALG_EC_FP, bitLen, m_ramArray, (short) 0);
- }
-
- // Now generate new keypair with either default or custom curve
- ecKeyGenerator.generatePair();
-
- short len;
- short offset = 0;
-
- // Export curve public parameters
- offset += 2; // reserve space for length
- len = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_FP, apdubuf, offset);
- Util.setShort(apdubuf, (short) (offset - 2), len);
- offset += len;
- offset += 2; // reserve space for length
- len = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_A, apdubuf, offset);
- Util.setShort(apdubuf, (short) (offset - 2), len);
- offset += len;
-
- offset += 2; // reserve space for length
- len = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_B, apdubuf, offset);
- Util.setShort(apdubuf, (short) (offset - 2), len);
- offset += len;
- offset += 2; // reserve space for length
- len = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PUBLIC, EC_Consts.PARAMETER_R, apdubuf, offset);
- Util.setShort(apdubuf, (short) (offset - 2), len);
- offset += len;
- /*
- offset += 2; // reserve space for length
- len = ecPubKey.getW(apdubuf, offset);
- Util.setShort(apdubuf, (short) (offset - 2), len);
- offset += len;
- */
- apdu.setOutgoingAndSend((short) 0, offset);
- }
- }
-
- void DeriveECDHSecret(APDU apdu) {
- byte[] apdubuf = apdu.getBuffer();
- short len = apdu.setIncomingAndReceive();
-
- // Assumption: proper EC keyPair is already allocated
- // If public key point is provided, then use it
- if (len == 0) {
- // if not provided, use build-in one (valid only for 192 only)
- Util.arrayCopyNonAtomic(EC192_FP_PUBLICW, (short) 0, apdubuf, ISO7816.OFFSET_CDATA, (short) EC192_FP_PUBLICW.length);
- len = (short) EC192_FP_PUBLICW.length;
- }
-
- // Generate fresh EC keypair
- ecKeyGenerator.generatePair();
- ecPrivKey = ecKeyGenerator.getPrivateKey();
-
- if (dhKeyAgreement == null) {
- dhKeyAgreement = KeyAgreement.getInstance(KeyAgreement.ALG_EC_SVDP_DH, false);
- }
- dhKeyAgreement.init(ecPrivKey);
- short secretLen = 0;
- // Generate and export secret
- secretLen = dhKeyAgreement.generateSecret(apdubuf, ISO7816.OFFSET_CDATA, len, m_ramArray, (short) 0);
- Util.arrayCopyNonAtomic(m_ramArray, (short) 0, apdubuf, (short) 0, secretLen);
-
- apdu.setOutgoingAndSend((short) 0, secretLen);
- }
-
- void GenerateAndReturnKey(APDU apdu) {
- byte[] apdubuf = apdu.getBuffer();
- apdu.setIncomingAndReceive();
-
- short offset = ISO7816.OFFSET_CDATA;
- byte keyClass = apdubuf[offset];
- offset++;
-
- short keyLength = Util.getShort(apdubuf, offset);
- offset += 2;
-
- byte anomalous = apdubuf[offset];
-
- offset = 0;
-
- switch (apdubuf[ISO7816.OFFSET_P1]) {
- case P1_SETCURVE: {
- ecKeyGenerator.allocatePair(keyClass, keyLength);
-
- 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();
- break;
- }
- case P1_GENERATEKEYPAIR: {
- // Assumption: proper EC keyPair is already allocated and initialized
- short sw = ecKeyGenerator.generatePair();
- if (sw != ISO7816.SW_NO_ERROR) {
- ISOException.throwIt(sw);
- }
- ecPubKey = ecKeyGenerator.getPublicKey();
- ecPrivKey = ecKeyGenerator.getPrivateKey();
-
- offset = 0;
- apdubuf[offset] = EC_Consts.TAG_ECPUBKEY;
- offset++;
- offset += 2; // reserve space for length
- 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 = ecKeyGenerator.exportParameter(ECKeyGenerator.KEY_PRIVATE, EC_Consts.PARAMETER_S, 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) {
- // Select proper attributes
- switch (bitLen) {
- case (short) 128: {
- ecKeyPair = ecKeyPair128;
- ecKeyPair = ecKeyPair128;
- ecPrivKey = ecPrivKey128;
- break;
- }
- case (short) 160: {
- ecKeyPair = ecKeyPair160;
- ecKeyPair = ecKeyPair160;
- ecPrivKey = ecPrivKey160;
- break;
- }
- case (short) 192: {
- ecKeyPair = ecKeyPair192;
- ecKeyPair = ecKeyPair192;
- ecPrivKey = ecPrivKey192;
- break;
- }
- case (short) 256: {
- ecKeyPair = ecKeyPair256;
- ecKeyPair = ecKeyPair256;
- ecPrivKey = ecPrivKey256;
- break;
- }
- default: {
- ISOException.throwIt((short) -1);
- }
- }
-
- // Allocate instance
- ecKeyPair = new KeyPair(algorithm, bitLen);
- ecKeyPair.genKeyPair();
- ecPubKey = (ECPublicKey) ecKeyPair.getPublic();
- // sometimes null is returned and previous one call to genKeyPair()
- // is required before we can get public key
- if (ecPubKey == null) {
- ecKeyPair.genKeyPair();
- }
- ecPubKey = (ECPublicKey) ecKeyPair.getPublic();
- ecPrivKey = (ECPrivateKey) ecKeyPair.getPrivate();
- // Set required EC parameters
- EC_Consts.setValidECKeyParams(ecPubKey, ecPrivKey, KeyPair.ALG_EC_FP, bitLen, m_ramArray);
- }
-
-
-*/
-
-}
-
diff --git a/src/cz/crcs/ectester/applet/ECKeyGenerator.java b/src/cz/crcs/ectester/applet/ECKeyGenerator.java
new file mode 100644
index 0000000..47f9c94
--- /dev/null
+++ b/src/cz/crcs/ectester/applet/ECKeyGenerator.java
@@ -0,0 +1,381 @@
+package cz.crcs.ectester.applet;
+
+import javacard.framework.ISO7816;
+import javacard.framework.ISOException;
+import javacard.framework.Util;
+import javacard.security.CryptoException;
+import javacard.security.ECPrivateKey;
+import javacard.security.ECPublicKey;
+import javacard.security.KeyPair;
+
+/**
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class ECKeyGenerator {
+
+ public static final byte KEY_PUBLIC = 0x01;
+ public static final byte KEY_PRIVATE = 0x02;
+ public static final byte KEY_BOTH = KEY_PUBLIC | KEY_PRIVATE;
+
+ private short sw = ISO7816.SW_NO_ERROR;
+
+ /**
+ * @param keyClass
+ * @param keyLength
+ * @return
+ */
+ public KeyPair allocatePair(byte keyClass, short keyLength) {
+ sw = ISO7816.SW_NO_ERROR;
+ KeyPair ecKeyPair = null;
+ try {
+ ecKeyPair = new KeyPair(keyClass, keyLength);
+
+ if (ecKeyPair.getPublic() == null || ecKeyPair.getPrivate() == null) {
+ try {
+ ecKeyPair.genKeyPair();
+ } catch (Exception ignored) {
+ }
+ }
+ } catch (CryptoException ce) {
+ sw = ce.getReason();
+ } catch (Exception e) {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ return ecKeyPair;
+ }
+
+ /**
+ * @param keypair
+ * @return
+ */
+ public short generatePair(KeyPair keypair) {
+ sw = ISO7816.SW_NO_ERROR;
+ try {
+ keypair.genKeyPair();
+ } catch (CryptoException ce) {
+ sw = ce.getReason();
+ } catch (Exception e) {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ return sw;
+ }
+
+ public short setCurve(KeyPair keypair, byte curve, byte[] buffer, short offset) {
+ return setCurve(keypair, curve, EC_Consts.PARAMETERS_ALL, buffer, offset);
+ }
+
+ public short setCurve(KeyPair keypair, byte curve, short params, byte[] buffer, short offset) {
+ return setCurve(keypair, KEY_BOTH, curve, params, buffer, offset);
+ }
+
+ public short setCurve(KeyPair keypair, byte key, byte curve, short params, byte[] buffer, short offset) {
+ byte alg = EC_Consts.getCurveType(curve);
+ sw = ISO7816.SW_NO_ERROR;
+
+ short length;
+ if (alg == KeyPair.ALG_EC_FP && (params & EC_Consts.PARAMETER_FP) != 0) {
+ length = EC_Consts.getCurveParameter(curve, EC_Consts.PARAMETER_FP, buffer, offset);
+ sw = setParameter(keypair, key, EC_Consts.PARAMETER_FP, buffer, offset, length);
+ } else if (alg == KeyPair.ALG_EC_F2M && (params & EC_Consts.PARAMETER_F2M) != 0) {
+ length = EC_Consts.getCurveParameter(curve, EC_Consts.PARAMETER_F2M, buffer, offset);
+ sw = setParameter(keypair, key, EC_Consts.PARAMETER_F2M, buffer, offset, length);
+ }
+ if (sw != ISO7816.SW_NO_ERROR) return sw;
+
+ //go through all params
+ short paramMask = EC_Consts.PARAMETER_A;
+ while (paramMask <= EC_Consts.PARAMETER_S) {
+ short masked = (short) (paramMask & params);
+ if (masked != 0) {
+ length = EC_Consts.getCurveParameter(curve, masked, buffer, offset);
+ sw = setParameter(keypair, key, masked, buffer, offset, length);
+ if (sw != ISO7816.SW_NO_ERROR) break;
+ }
+ paramMask = (short) (paramMask << 1);
+ }
+ return sw;
+ }
+
+ /**
+ * @param keypair
+ * @param corruptParams
+ * @param corruption
+ * @param buffer
+ * @param offset
+ * @return
+ */
+ public short corruptCurve(KeyPair keypair, short corruptParams, byte corruption, byte[] buffer, short offset) {
+ return corruptCurve(keypair, KEY_BOTH, corruptParams, corruption, buffer, offset);
+ }
+
+ /**
+ * @param keypair
+ * @param key
+ * @param corruptParams
+ * @param corruption
+ * @param buffer
+ * @param offset
+ * @return
+ */
+ public short corruptCurve(KeyPair keypair, byte key, short corruptParams, byte corruption, byte[] buffer, short offset) {
+ sw = ISO7816.SW_NO_ERROR;
+
+ //go through param bit by bit, and invalidate all selected params
+ short paramMask = EC_Consts.PARAMETER_FP;
+ while (paramMask <= EC_Consts.PARAMETER_S) {
+ short masked = (short) (paramMask & corruptParams);
+ if (masked != 0) {
+ short length = exportParameter(keypair, key, masked, buffer, offset);
+ EC_Consts.corruptParameter(corruption, buffer, offset, length);
+ sw = setParameter(keypair, key, masked, buffer, offset, length);
+ if (sw != ISO7816.SW_NO_ERROR) break;
+ }
+ paramMask = (short) (paramMask << 1);
+ }
+ return sw;
+ }
+
+ /**
+ * @param key
+ * @param param
+ * @param data
+ * @param offset
+ * @param length
+ * @return
+ */
+ public short setParameter(KeyPair keypair, byte key, short param, byte[] data, short offset, short length) {
+ sw = ISO7816.SW_NO_ERROR;
+ ECPublicKey ecPublicKey = (ECPublicKey) keypair.getPublic();
+ ECPrivateKey ecPrivateKey = (ECPrivateKey) keypair.getPrivate();
+
+ try {
+ switch (param) {
+ case EC_Consts.PARAMETER_FP: {
+ if ((key & KEY_PUBLIC) != 0) ecPublicKey.setFieldFP(data, offset, length);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldFP(data, offset, length);
+ break;
+ }
+ case EC_Consts.PARAMETER_F2M: {
+ if (length == 2) {
+ short i = Util.makeShort(data[offset], data[(short) (offset + 1)]);
+ if ((key & KEY_PUBLIC) != 0) ecPublicKey.setFieldF2M(i);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldF2M(i);
+ } else if (length == 6) {
+ short i1 = Util.makeShort(data[offset], data[(short) (offset + 1)]);
+ short i2 = Util.makeShort(data[(short) (offset + 2)], data[(short) (offset + 3)]);
+ short i3 = Util.makeShort(data[(short) (offset + 4)], data[(short) (offset + 5)]);
+ if ((key & KEY_PUBLIC) != 0) ecPublicKey.setFieldF2M(i1, i2, i3);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setFieldF2M(i1, i2, i3);
+ } else {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ break;
+ }
+ case EC_Consts.PARAMETER_A: {
+ if ((key & KEY_PUBLIC) != 0) ecPublicKey.setA(data, offset, length);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setA(data, offset, length);
+ break;
+ }
+ case EC_Consts.PARAMETER_B: {
+ if ((key & KEY_PUBLIC) != 0) ecPublicKey.setB(data, offset, length);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setB(data, offset, length);
+ break;
+ }
+ case EC_Consts.PARAMETER_G: {
+ if ((key & KEY_PUBLIC) != 0) ecPublicKey.setG(data, offset, length);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setG(data, offset, length);
+ break;
+ }
+ case EC_Consts.PARAMETER_R: {
+ if ((key & KEY_PUBLIC) != 0) ecPublicKey.setR(data, offset, length);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setR(data, offset, length);
+ break;
+ }
+ case EC_Consts.PARAMETER_K: {
+ short k = 0;
+ if (length > 2 || length <= 0) {
+ sw = ISO7816.SW_UNKNOWN;
+ break;
+ } else if (length == 2) {
+ k = Util.getShort(data, offset);
+ } else if (length == 1) {
+ k = data[offset];
+ }
+ if ((key & KEY_PUBLIC) != 0) ecPublicKey.setK(k);
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setK(k);
+ break;
+ }
+ case EC_Consts.PARAMETER_S:
+ if ((key & KEY_PRIVATE) != 0) ecPrivateKey.setS(data, offset, length);
+ break;
+ case EC_Consts.PARAMETER_W:
+ if ((key & KEY_PUBLIC) != 0) ecPublicKey.setW(data, offset, length);
+ break;
+ default: {
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
+ }
+ } catch (CryptoException ce) {
+ sw = ce.getReason();
+ } catch (Exception e) {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ return sw;
+ }
+
+ /**
+ * @param keypair
+ * @param params
+ * @param inBuffer
+ * @param inOffset
+ * @return
+ */
+ public short setExternalCurve(KeyPair keypair, short params, byte[] inBuffer, short inOffset) {
+ return setExternalCurve(keypair, KEY_BOTH, params, inBuffer, inOffset);
+ }
+
+ /**
+ * @param keypair
+ * @param key
+ * @param params
+ * @param inBuffer
+ * @param inOffset
+ * @return
+ */
+ public short setExternalCurve(KeyPair keypair, byte key, short params, byte[] inBuffer, short inOffset) {
+ sw = ISO7816.SW_NO_ERROR;
+
+ short paramMask = EC_Consts.PARAMETER_FP;
+ while (paramMask <= EC_Consts.PARAMETER_S) {
+ short masked = (short) (paramMask & params);
+ if (masked != 0) {
+ short paramLength = Util.getShort(inBuffer, inOffset);
+ inOffset += 2;
+ sw = setParameter(keypair, key, masked, inBuffer, inOffset, paramLength);
+ inOffset += paramLength;
+ if (sw != ISO7816.SW_NO_ERROR) break;
+ }
+ paramMask = (short) (paramMask << 1);
+ }
+ return sw;
+ }
+
+ /**
+ * @param key
+ * @param param
+ * @param outputBuffer
+ * @param outputOffset
+ * @return
+ */
+ public short exportParameter(KeyPair keypair, byte key, short param, byte[] outputBuffer, short outputOffset) {
+ sw = ISO7816.SW_NO_ERROR;
+ ECPublicKey ecPublicKey = (ECPublicKey) keypair.getPublic();
+ ECPrivateKey ecPrivateKey = (ECPrivateKey) keypair.getPrivate();
+
+ short length = 0;
+ try {
+ switch (param) {
+ case EC_Consts.PARAMETER_FP:
+ case EC_Consts.PARAMETER_F2M:
+ if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getField(outputBuffer, outputOffset);
+ if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getField(outputBuffer, outputOffset);
+ break;
+ case EC_Consts.PARAMETER_A:
+ if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getA(outputBuffer, outputOffset);
+ if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getA(outputBuffer, outputOffset);
+ break;
+ case EC_Consts.PARAMETER_B:
+ if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getB(outputBuffer, outputOffset);
+ if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getB(outputBuffer, outputOffset);
+ break;
+ case EC_Consts.PARAMETER_G:
+ if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getG(outputBuffer, outputOffset);
+ if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getG(outputBuffer, outputOffset);
+ break;
+ case EC_Consts.PARAMETER_R:
+ if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getR(outputBuffer, outputOffset);
+ if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getR(outputBuffer, outputOffset);
+ break;
+ case EC_Consts.PARAMETER_K:
+ if ((key & KEY_PUBLIC) != 0) Util.setShort(outputBuffer, outputOffset, ecPublicKey.getK());
+ if ((key & KEY_PRIVATE) != 0) Util.setShort(outputBuffer, outputOffset, ecPrivateKey.getK());
+ length = 2;
+ break;
+ case EC_Consts.PARAMETER_W:
+ if ((key & KEY_PUBLIC) != 0) length = ecPublicKey.getW(outputBuffer, outputOffset);
+ break;
+ case EC_Consts.PARAMETER_S:
+ if ((key & KEY_PRIVATE) != 0) length = ecPrivateKey.getS(outputBuffer, outputOffset);
+ break;
+ default:
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
+ } catch (CryptoException ce) {
+ sw = ce.getReason();
+ } catch (Exception e) {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ return length;
+ }
+
+ /**
+ * @param keypair
+ * @param key
+ * @param params
+ * @param buffer
+ * @param offset
+ * @return
+ */
+ public short exportParameters(KeyPair keypair, byte key, short params, byte[] buffer, short offset) {
+ sw = ISO7816.SW_NO_ERROR;
+
+ short length = 0;
+
+ short paramMask = EC_Consts.PARAMETER_FP;
+ while (paramMask <= EC_Consts.PARAMETER_S) {
+ short masked = (short) (paramMask & params);
+ if (masked != 0) {
+ short len = exportParameter(keypair, key, masked, buffer, (short) (offset + 2));
+ if (len == 0) {
+ paramMask = (short) (paramMask << 1);
+ continue;
+ }
+ Util.setShort(buffer, offset, len);
+ offset += len + 2;
+ length += len + 2;
+ }
+ paramMask = (short) (paramMask << 1);
+ }
+ return length;
+ }
+
+ /**
+ * Copies this KeyPairs curve parameters to another ECKeyGenerator.
+ *
+ * @param from
+ * @param to
+ * @param buffer
+ * @param offset
+ * @return
+ */
+ public short copyCurve(KeyPair from, KeyPair to, byte[] buffer, short offset) {
+ sw = ISO7816.SW_NO_ERROR;
+ try {
+ short param = EC_Consts.PARAMETER_FP;
+ while (param <= EC_Consts.PARAMETER_K) {
+ short paramLength = exportParameter(from, KEY_PUBLIC, param, buffer, offset);
+ setParameter(to, KEY_BOTH, param, buffer, offset, paramLength);
+ param = (short) (param << 1);
+ }
+ } catch (CryptoException ce) {
+ sw = ce.getReason();
+ } catch (Exception e) {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ return sw;
+ }
+
+ public short getSW() {
+ return sw;
+ }
+}
diff --git a/src/cz/crcs/ectester/applet/ECKeyTester.java b/src/cz/crcs/ectester/applet/ECKeyTester.java
new file mode 100644
index 0000000..72fa165
--- /dev/null
+++ b/src/cz/crcs/ectester/applet/ECKeyTester.java
@@ -0,0 +1,184 @@
+package cz.crcs.ectester.applet;
+
+
+import javacard.framework.ISO7816;
+import javacard.security.*;
+
+/**
+ * Class capable of testing ECDH/C and ECDSA.
+ * Note that ECDH and ECDHC output should equal, only the algorithm is different.
+ *
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class ECKeyTester {
+
+ private KeyAgreement ecdhKeyAgreement = null;
+ private KeyAgreement ecdhcKeyAgreement = null;
+ private Signature ecdsaSignature = null;
+
+ private short sw = ISO7816.SW_NO_ERROR;
+
+ public short allocateECDH() {
+ sw = ISO7816.SW_NO_ERROR;
+ try {
+ ecdhKeyAgreement = KeyAgreement.getInstance(KeyAgreement.ALG_EC_SVDP_DH, false);
+ } catch (CryptoException ce) {
+ sw = ce.getReason();
+ } catch (Exception e) {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ return sw;
+ }
+
+ public short allocateECDHC() {
+ sw = ISO7816.SW_NO_ERROR;
+ try {
+ ecdhcKeyAgreement = KeyAgreement.getInstance(KeyAgreement.ALG_EC_SVDP_DHC, false);
+ } catch (CryptoException ce) {
+ sw = ce.getReason();
+ } catch (Exception e) {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ return sw;
+ }
+
+ public short allocateECDSA() {
+ sw = ISO7816.SW_NO_ERROR;
+ try {
+ ecdsaSignature = Signature.getInstance(Signature.ALG_ECDSA_SHA, false);
+ } catch (CryptoException ce) {
+ sw = ce.getReason();
+ } catch (Exception e) {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ return sw;
+ }
+
+ private short testKA(KeyAgreement ka, ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
+ sw = ISO7816.SW_NO_ERROR;
+ short length = 0;
+ try {
+ ka.init(privateKey);
+ length = ka.generateSecret(pubkeyBuffer, pubkeyOffset, pubkeyLength, outputBuffer, outputOffset);
+ } catch (CryptoException ce) {
+ sw = ce.getReason();
+ } catch (Exception e) {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ return length;
+ }
+
+ private short testKA_validPoint(KeyAgreement ka, ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
+ return testKA(ka, privateKey, pubkeyBuffer, pubkeyOffset, pubkeyLength, outputBuffer, outputOffset);
+ }
+
+ private short testKA_invalidPoint(KeyAgreement ka, ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
+ pubkeyBuffer[(short)(pubkeyLength - 2)] += 0xcc;
+ pubkeyBuffer[(short)(pubkeyLength - 3)] += 0xcc;
+ short result = testKA(ka, privateKey, pubkeyBuffer, pubkeyOffset, pubkeyLength, outputBuffer, outputOffset);
+ pubkeyBuffer[(short)(pubkeyLength - 2)] -= 0xcc;
+ pubkeyBuffer[(short)(pubkeyLength - 3)] -= 0xcc;
+ return result;
+ }
+
+ public short testECDH(ECPrivateKey privateKey, byte[] pubkeyBuffer, short pubkeyOffset, short pubkeyLength, byte[] outputBuffer, short outputOffset) {
+ return testKA(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, pubkeyLength, outputBuffer, outputOffset);
+ }
+
+ /**
+ * Tests ECDH secret generation with given {@code privateKey} and {@code publicKey}.
+ * Uses {@code pubkeyBuffer} at {@code pubkeyOffset} for computations.
+ * Output should equal with ECDHC output.
+ * @param privateKey
+ * @param publicKey
+ * @param pubkeyBuffer
+ * @param pubkeyOffset
+ * @param outputBuffer
+ * @param outputOffset
+ * @return derived secret length
+ *
+ **/
+ public short testECDH_validPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
+ short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
+ return testKA_validPoint(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
+ }
+
+ public short testECDH_invalidPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
+ short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
+ return testKA_invalidPoint(ecdhKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
+ }
+
+
+ /**
+ * Tests ECDHC secret generation with given {@code privateKey} and {@code publicKey}.
+ * Uses {@code pubkeyBuffer} at {@code pubkeyOffset} for computations.
+ * Output should equal to ECDH output.
+ * @param privateKey
+ * @param publicKey
+ * @param pubkeyBuffer
+ * @param pubkeyOffset
+ * @param outputBuffer
+ * @param outputOffset
+ * @return ISO7816.SW_NO_ERROR on correct operation,
+ * exception reason otherwise
+ */
+ public short testECDHC_validPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
+ short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
+ return testKA_validPoint(ecdhcKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
+ }
+
+ public short testECDHC_invalidPoint(ECPrivateKey privateKey, ECPublicKey publicKey, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset) {
+ short length = publicKey.getW(pubkeyBuffer, pubkeyOffset);
+ return testKA_invalidPoint(ecdhcKeyAgreement, privateKey, pubkeyBuffer, pubkeyOffset, length, outputBuffer, outputOffset);
+ }
+
+ /**
+ * Uses {@code signKey} to sign data from {@code inputBuffer} at {@code inputOffset} with {@code inputOffset}.
+ * Then checks for correct signature length.
+ * Then tries verifying the data with {@code verifyKey}.
+ * @param signKey
+ * @param verifyKey
+ * @param inputBuffer
+ * @param inputOffset
+ * @param inputLength
+ * @param sigBuffer
+ * @param sigOffset
+ * @return signature length
+ */
+ public short testECDSA(ECPrivateKey signKey, ECPublicKey verifyKey, byte[] inputBuffer, short inputOffset, short inputLength, byte[] sigBuffer, short sigOffset) {
+ sw = ISO7816.SW_NO_ERROR;
+ short length = 0;
+ try {
+ ecdsaSignature.init(signKey, Signature.MODE_SIGN);
+ length = ecdsaSignature.sign(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset);
+
+ ecdsaSignature.init(verifyKey, Signature.MODE_VERIFY);
+ boolean correct = ecdsaSignature.verify(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset, length);
+ if (!correct) {
+ sw = ECTesterApplet.SW_SIG_VERIFY_FAIL;
+ }
+ } catch (CryptoException ce) {
+ sw = ce.getReason();
+ } catch (Exception e) {
+ sw = ISO7816.SW_UNKNOWN;
+ }
+ return length;
+ }
+
+ public KeyAgreement getECDH() {
+ return ecdhKeyAgreement;
+ }
+
+ public KeyAgreement getECDHC() {
+ return ecdhcKeyAgreement;
+ }
+
+ public Signature getECDSA() {
+ return ecdsaSignature;
+ }
+
+ public short getSW() {
+ return sw;
+ }
+
+}
diff --git a/src/cz/crcs/ectester/applet/ECTesterApplet.java b/src/cz/crcs/ectester/applet/ECTesterApplet.java
new file mode 100644
index 0000000..b461688
--- /dev/null
+++ b/src/cz/crcs/ectester/applet/ECTesterApplet.java
@@ -0,0 +1,427 @@
+/*
+ * PACKAGEID: 4C6162616B417070
+ * APPLETID: 4C6162616B4170706C6574
+ */
+package cz.crcs.ectester.applet;
+
+import javacard.framework.*;
+import javacard.security.ECPrivateKey;
+import javacard.security.ECPublicKey;
+import javacard.security.KeyPair;
+import javacard.security.RandomData;
+
+/**
+ * @author Petr Svenda petr@svenda.com
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class ECTesterApplet extends Applet {
+
+ // MAIN INSTRUCTION CLASS
+ public static final byte CLA_ECTESTERAPPLET = (byte) 0xB0;
+
+ //INSTRUCTIONS
+ public static final byte INS_ALLOCATE = (byte) 0x5a;
+ public static final byte INS_SET = (byte) 0x5b;
+ public static final byte INS_GENERATE = (byte) 0x5c;
+ public static final byte INS_ECDH = (byte) 0x5d;
+ public static final byte INS_ECDSA = (byte) 0x5e;
+
+ //PARAMETERS for P1 and P2
+ public static final byte KEYPAIR_LOCAL = (byte) 0x01;
+ public static final byte KEYPAIR_REMOTE = (byte) 0x02;
+ public static final byte KEYPAIR_BOTH = KEYPAIR_LOCAL | KEYPAIR_REMOTE;
+ public static final byte EXPORT_PUBLIC = (byte) 0x04;
+ public static final byte EXPORT_PRIVATE = (byte) 0x08;
+ public static final byte EXPORT_BOTH = EXPORT_PUBLIC | EXPORT_PRIVATE;
+ public static final byte EXPORT_ECDH = (byte) 0x10;
+ public static final byte EXPORT_SIG = (byte) 0x20;
+
+ //STATUS WORDS
+ public static final short SW_SIG_VERIFY_FAIL = (short) 0x0ee1;
+
+
+ private static final short ARRAY_LENGTH = (short) 0xff;
+ // TEMPORARRY ARRAY IN RAM
+ private byte ramArray[] = null;
+ private byte ramArray2[] = null;
+ // PERSISTENT ARRAY IN EEPROM
+ private byte dataArray[] = null; // unused
+
+
+ private RandomData randomData = null;
+
+ private KeyPair localKeypair = null;
+ private KeyPair remoteKeypair = null;
+ private ECKeyTester keyTester = null;
+ private ECKeyGenerator keyGenerator = null;
+
+ protected ECTesterApplet(byte[] buffer, short offset, byte length) {
+ if (length > 9) {
+ /*
+ short dataOffset = offset;
+ // shift to privilege offset
+ dataOffset += (short) (1 + buffer[offset]);
+ // finally shift to Application specific offset
+ dataOffset += (short) (1 + buffer[dataOffset]);
+ // go to proprietary data
+ dataOffset++;
+ */
+
+ ramArray = JCSystem.makeTransientByteArray(ARRAY_LENGTH, JCSystem.CLEAR_ON_RESET);
+ ramArray2 = JCSystem.makeTransientByteArray(ARRAY_LENGTH, JCSystem.CLEAR_ON_RESET);
+
+ dataArray = new byte[ARRAY_LENGTH];
+ Util.arrayFillNonAtomic(dataArray, (short) 0, ARRAY_LENGTH, (byte) 0);
+
+ randomData = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
+ EC_Consts.randomData = randomData;
+
+ keyGenerator = new ECKeyGenerator();
+ keyTester = new ECKeyTester();
+ keyTester.allocateECDH();
+ keyTester.allocateECDHC();
+ keyTester.allocateECDSA();
+ }
+ register();
+ }
+
+ public static void install(byte[] bArray, short bOffset, byte bLength) throws ISOException {
+ // applet instance creation
+ new ECTesterApplet(bArray, bOffset, bLength);
+ }
+
+ public void process(APDU apdu) throws ISOException {
+ // get the APDU buffer
+ byte[] apduBuffer = apdu.getBuffer();
+
+ // ignore the applet select command dispached to the process
+ if (selectingApplet())
+ return;
+
+ if (apduBuffer[ISO7816.OFFSET_CLA] == CLA_ECTESTERAPPLET) {
+ switch (apduBuffer[ISO7816.OFFSET_INS]) {
+ case INS_ALLOCATE:
+ insAllocate(apdu);
+ break;
+ case INS_SET:
+ insSet(apdu);
+ break;
+ case INS_GENERATE:
+ insGenerate(apdu);
+ break;
+ case INS_ECDH:
+ insECDH(apdu);
+ break;
+ case INS_ECDSA:
+ insECDSA(apdu);
+ break;
+ default:
+ // The INS code is not supported by the dispatcher
+ ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
+ break;
+ }
+ } else ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
+ }
+
+ /**
+ * Allocate local and remote keypairs.
+ * returns allocate SWs
+ *
+ * @param apdu P1 = byte keypair (KEYPAIR_* | ...)
+ * P2 =
+ * DATA = short keyLength
+ * byte keyClass
+ */
+ private void insAllocate(APDU apdu) {
+ apdu.setIncomingAndReceive();
+ byte[] apdubuf = apdu.getBuffer();
+
+ byte keypair = apdubuf[ISO7816.OFFSET_P1];
+ short keyLength = Util.getShort(apdubuf, ISO7816.OFFSET_CDATA);
+ byte keyClass = apdubuf[ISO7816.OFFSET_CDATA + 2];
+
+ short len = allocate(keypair, keyLength, keyClass, apdubuf, (short) 0);
+
+ apdu.setOutgoingAndSend((short) 0, len);
+ }
+
+ /**
+ * @param keypair which keypair to use, local/remote (KEYPAIR_* | ...)
+ * @param keyLength key length to set
+ * @param keyClass key class to allocate
+ * @param buffer apdu buffer
+ * @param offset offset into apdu buffer
+ * @return length of data written to the buffer
+ */
+ private short allocate(byte keypair, short keyLength, byte keyClass, byte[] buffer, short offset) {
+ short length = 0;
+ if ((keypair & KEYPAIR_LOCAL) != 0) {
+ localKeypair = keyGenerator.allocatePair(keyClass, keyLength);
+ Util.setShort(buffer, offset, keyGenerator.getSW());
+ length += 2;
+ }
+
+ if ((keypair & KEYPAIR_REMOTE) != 0) {
+ remoteKeypair = keyGenerator.allocatePair(keyClass, keyLength);
+ Util.setShort(buffer, (short) (offset + length), keyGenerator.getSW());
+ length += 2;
+ }
+
+ return length;
+ }
+
+ /**
+ * Sets curve parameters on local and remote keypairs.
+ * returns setCurve SWs, set params if export
+ *
+ * @param apdu P1 = byte keypair (KEYPAIR_* | ...)
+ * P2 = byte export (EXPORT_* | KEYPAIR_*)
+ * DATA = byte curve (EC_Consts.CURVE_*)
+ * short params (EC_Consts.PARAMETER_* | ...)
+ * short corruptedParams (EC_Consts.PARAMETER_* | ...)
+ * byte corruptionType (EC_Consts.CORRUPTION_*)
+ *
+ * if curveID = CURVE_EXTERNAL:
+ * [short param_length, byte[] param],
+ * for all params in params,
+ * in order: field,a,b,g,r,k,w,s
+ */
+ private void insSet(APDU apdu) {
+ apdu.setIncomingAndReceive();
+ byte[] apdubuf = apdu.getBuffer();
+
+ byte keypair = apdubuf[ISO7816.OFFSET_P1];
+ byte export = apdubuf[ISO7816.OFFSET_P2];
+ byte curve = apdubuf[ISO7816.OFFSET_CDATA];
+ short params = Util.getShort(apdubuf, (short) (ISO7816.OFFSET_CDATA + 1));
+ short corruptedParams = Util.getShort(apdubuf, (short) (ISO7816.OFFSET_CDATA + 3));
+ byte corruptionType = apdubuf[(short) (ISO7816.OFFSET_CDATA + 5)];
+
+ short len = 0;
+
+ if ((keypair & KEYPAIR_LOCAL) != 0)
+ len += set(localKeypair, curve, params, corruptedParams, corruptionType, apdubuf, (short) (ISO7816.OFFSET_CDATA + 6), (short) 0);
+ if ((keypair & KEYPAIR_REMOTE) != 0)
+ len += set(remoteKeypair, curve, params, corruptedParams, corruptionType, apdubuf, (short) (ISO7816.OFFSET_CDATA + 6), len);
+ if ((export & KEYPAIR_LOCAL) != 0)
+ len += export(localKeypair, export, params, apdubuf, len);
+ if ((export & KEYPAIR_REMOTE) != 0)
+ len += export(remoteKeypair, export, params, apdubuf, len);
+
+ apdu.setOutgoingAndSend((short) 0, len);
+ }
+
+ /**
+ * @param keypair KeyPair to set params on
+ * @param curve curve to set (EC_Consts.CURVE_*)
+ * @param params parameters to set (EC_Consts.PARAMETER_* | ...)
+ * @param corrupted parameters to corrupt (EC_Consts.PARAMETER_* | ...)
+ * @param corruption corruption type (EC_Consts.CORRUPTION_*)
+ * @param buffer buffer to read params from and write sw to
+ * @param inOffset input offset in buffer
+ * @param outOffset output offset in buffer
+ * @return length of data written to the buffer
+ */
+ private short set(KeyPair keypair, byte curve, short params, short corrupted, byte corruption, byte[] buffer, short inOffset, short outOffset) {
+ short sw = ISO7816.SW_NO_ERROR;
+
+ switch (curve) {
+ case EC_Consts.CURVE_default:
+ //default, dont set anything
+ break;
+ case EC_Consts.CURVE_external:
+ //external
+ sw = keyGenerator.setExternalCurve(keypair, params, buffer, inOffset);
+ break;
+ default:
+ //custom
+ sw = keyGenerator.setCurve(keypair, curve, params, ramArray, (short) 0);
+ break;
+ }
+
+ if (sw == ISO7816.SW_NO_ERROR)
+ sw = keyGenerator.corruptCurve(keypair, corrupted, corruption, ramArray, (short) 0);
+ Util.setShort(buffer, outOffset, sw);
+ return 2;
+ }
+
+ /**
+ * Generates the local and remote keypairs.
+ * returns generate SWs, pubkey and privkey if export
+ *
+ * @param apdu P1 = byte keypair (KEYPAIR_* | ...)
+ * P2 = byte export (EXPORT_* | KEYPAIR_*)
+ */
+ private void insGenerate(APDU apdu) {
+ apdu.setIncomingAndReceive();
+ byte[] apdubuf = apdu.getBuffer();
+
+ byte keypair = apdubuf[ISO7816.OFFSET_P1];
+ byte export = apdubuf[ISO7816.OFFSET_P2];
+
+ short len = 0;
+ if ((keypair & KEYPAIR_LOCAL) != 0)
+ len += generate(localKeypair, apdubuf, (short) 0);
+ if ((keypair & KEYPAIR_REMOTE) != 0)
+ len += generate(remoteKeypair, apdubuf, len);
+ if ((export & KEYPAIR_LOCAL) != 0)
+ len += export(localKeypair, export, (short) (EC_Consts.PARAMETER_W | EC_Consts.PARAMETER_S), apdubuf, len);
+ if ((export & KEYPAIR_REMOTE) != 0)
+ len += export(remoteKeypair, export, (short) (EC_Consts.PARAMETER_W | EC_Consts.PARAMETER_S), apdubuf, len);
+
+ apdu.setOutgoingAndSend((short) 0, len);
+ }
+
+ /**
+ * @param keypair KeyPair to generate
+ * @param buffer buffer to write sw to
+ * @param offset output offset in buffer
+ * @return length of data written to the buffer
+ */
+ private short generate(KeyPair keypair, byte[] buffer, short offset) {
+ short sw = keyGenerator.generatePair(keypair);
+ Util.setShort(buffer, offset, sw);
+
+ return 2;
+ }
+
+ /**
+ * @param keypair KeyPair to export from
+ * @param export which key to export from (EXPORT_PUBLIC | EXPORT_PRIVATE)
+ * @param params which params to export (EC_Consts.PARAMETER_* | ...)
+ * @param buffer buffer to export params to
+ * @param offset output offset in buffer
+ * @return length of data written to the buffer
+ */
+ private short export(KeyPair keypair, byte export, short params, byte[] buffer, short offset) {
+ short length = 0;
+
+ if ((export & EXPORT_PUBLIC) != 0) {
+ //export params from public
+ length += keyGenerator.exportParameters(keypair, ECKeyGenerator.KEY_PUBLIC, params, buffer, offset);
+ }
+
+ if ((export & EXPORT_PRIVATE) != 0) {
+ //export params from private
+ length += keyGenerator.exportParameters(keypair, ECKeyGenerator.KEY_PRIVATE, params, buffer, (short) (offset + length));
+
+ }
+ return length;
+ }
+
+ /**
+ * Does ECDH, between the pubkey specified in P1(local/remote) and the privkey specified in P2(local/remote).
+ * returns deriveSecret SW, if export != 0 => short secretlen, byte[] secret
+ *
+ * @param apdu P1 = byte pubkey (KEYPAIR_*)
+ * P2 = byte privkey (KEYPAIR_*)
+ * DATA = byte export (EXPORT_ECDH || 0)
+ * byte invalid (00 = valid, !00 = invalid)
+ */
+ private void insECDH(APDU apdu) {
+ apdu.setIncomingAndReceive();
+ byte[] apdubuf = apdu.getBuffer();
+
+ byte pubkey = apdubuf[ISO7816.OFFSET_P1];
+ byte privkey = apdubuf[ISO7816.OFFSET_P2];
+ byte export = apdubuf[ISO7816.OFFSET_CDATA];
+ byte invalid = apdubuf[(short) (ISO7816.OFFSET_CDATA + 1)];
+
+ short len = ecdh(pubkey, privkey, export, invalid, apdubuf, (short) 0);
+
+ apdu.setOutgoingAndSend((short) 0, len);
+ }
+
+ /**
+ * @param pubkey keypair to use for public key, (KEYPAIR_LOCAL || KEYPAIR_REMOTE)
+ * @param privkey keypair to use for private key, (KEYPAIR_LOCAL || KEYPAIR_REMOTE)
+ * @param export whether to export ECDH secret
+ * @param invalid whether to invalidate the pubkey before ECDH
+ * @param buffer buffer to write sw to, and export ECDH secret if (export & EXPORT_ECDH) != 0
+ * @param offset output offset in buffer
+ * @return length of data written to the buffer
+ */
+ private short ecdh(byte pubkey, byte privkey, byte export, byte invalid, byte[] buffer, short offset) {
+ short length = 0;
+
+ KeyPair pub = ((pubkey & KEYPAIR_LOCAL) != 0) ? localKeypair : remoteKeypair;
+ KeyPair priv = ((privkey & KEYPAIR_LOCAL) != 0) ? localKeypair : remoteKeypair;
+
+ short secretLength;
+ if (invalid != 0) {
+ secretLength = keyTester.testECDH_invalidPoint((ECPrivateKey) priv.getPrivate(), (ECPublicKey) pub.getPublic(), ramArray, (short) 0, ramArray2, (short) 0);
+ } else {
+ secretLength = keyTester.testECDH_validPoint((ECPrivateKey) priv.getPrivate(), (ECPublicKey) pub.getPublic(), ramArray, (short) 0, ramArray2, (short) 0);
+ }
+
+ Util.setShort(buffer, offset, keyTester.getSW());
+ length += 2;
+
+ if ((export & EXPORT_ECDH) != 0) {
+ Util.setShort(buffer, (short) (offset + length), secretLength);
+ length += 2;
+ Util.arrayCopyNonAtomic(ramArray2, (short) 0, buffer, (short) (offset + length), secretLength);
+ length += secretLength;
+ }
+
+ return length;
+ }
+
+ /**
+ * Does and ECDSA signature and verification on data provided, using the keypair in P1(local/remote).
+ * returns ecdsa SW, if export != 0 => short signature_length, byte[] signature
+ *
+ * @param apdu P1 = byte keypair (KEYPAIR_*)
+ * P2 = byte export (EXPORT_SIG || 0)
+ * DATA = short data_length (00 = random data generated, !00 = data length)
+ * byte[] data
+ */
+ private void insECDSA(APDU apdu) {
+ apdu.setIncomingAndReceive();
+ byte[] apdubuf = apdu.getBuffer();
+
+ byte keypair = apdubuf[ISO7816.OFFSET_P1];
+ byte export = apdubuf[ISO7816.OFFSET_P2];
+
+ short len = ecdsa(keypair, export, apdubuf, ISO7816.OFFSET_CDATA, (short) 0);
+
+ apdu.setOutgoingAndSend((short) 0, len);
+ }
+
+ /**
+ * @param keypair keypair to use for signing and verification (KEYPAIR_LOCAL || KEYPAIR_REMOTE)
+ * @param export whether to export ECDSA signature
+ * @param buffer buffer to write sw to, and export ECDSA signature if (export & EXPORT_SIG) != 0
+ * @param inOffset input offset in buffer
+ * @param outOffset output offset in buffer
+ * @return length of data written to the buffer
+ */
+ private short ecdsa(byte keypair, byte export, byte[] buffer, short inOffset, short outOffset) {
+ short length = 0;
+
+ short dataLength = Util.getShort(buffer, inOffset);
+ if (dataLength == 0) { //no data to sign
+ //generate random
+ dataLength = 32;
+ randomData.generateData(ramArray, (short) 0, dataLength);
+ } else {
+ Util.arrayCopyNonAtomic(buffer, (short) (inOffset + 2), ramArray, (short) 0, dataLength);
+ }
+
+ KeyPair sign = ((keypair & KEYPAIR_LOCAL) != 0) ? localKeypair : remoteKeypair;
+
+ short signatureLength = keyTester.testECDSA((ECPrivateKey) sign.getPrivate(), (ECPublicKey) sign.getPublic(), ramArray, (short) 0, dataLength, ramArray2, (short) 0);
+ Util.setShort(buffer, outOffset, keyTester.getSW());
+ length += 2;
+
+ if ((export & EXPORT_SIG) != 0) {
+ Util.setShort(buffer, (short) (outOffset + length), signatureLength);
+ length += 2;
+
+ Util.arrayCopyNonAtomic(ramArray2, (short) 0, buffer, (short) (outOffset + length), signatureLength);
+ length += signatureLength;
+ }
+
+ return length;
+ }
+}
diff --git a/src/cz/crcs/ectester/applet/EC_Consts.java b/src/cz/crcs/ectester/applet/EC_Consts.java
new file mode 100644
index 0000000..c70919c
--- /dev/null
+++ b/src/cz/crcs/ectester/applet/EC_Consts.java
@@ -0,0 +1,1298 @@
+package cz.crcs.ectester.applet;
+
+import javacard.framework.ISO7816;
+import javacard.framework.ISOException;
+import javacard.framework.Util;
+import javacard.security.KeyPair;
+import javacard.security.RandomData;
+
+/**
+ * @author Petr Svenda petr@svenda.com
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class EC_Consts {
+
+ private static byte[] EC_FP_P = null; //p
+ private static byte[] EC_A = null; //a
+ private static byte[] EC_B = null; //b
+ private static byte[] EC_G_X = null; //G[x,y]
+ private static byte[] EC_G_Y = null; //
+ private static byte[] EC_R = null; //n
+ private static short EC_K = 1; //h
+
+ private static byte[] EC_W_X = null; //Pubkey[x,y]
+ private static byte[] EC_W_Y = null;
+ private static byte[] EC_S = null; //Private
+
+ private static byte[] EC_F2M_F2M = null; //[short i1, short i2, short i3], f = x^m + x^i1 + x^i2 + x^i3 + 1
+
+
+ public static final short PARAMETER_FP = 0x0001;
+ public static final short PARAMETER_F2M = 0x0002;
+
+ public static final short PARAMETER_A = 0x0004;
+ public static final short PARAMETER_B = 0x0008;
+ public static final short PARAMETER_G = 0x0010;
+ public static final short PARAMETER_R = 0x0020;
+ public static final short PARAMETER_K = 0x0040;
+ public static final short PARAMETER_W = 0x0080;
+ public static final short PARAMETER_S = 0x0100;
+
+ public static final short PARAMETERS_NONE = 0x0000;
+ public static final short PARAMETERS_DOMAIN_FP = 0x007d;
+ /**
+ * FP,A,B,G,R,K
+ */
+ public static final short PARAMETERS_DOMAIN_F2M = 0x007e;
+ /**
+ * F2M,A,B,G,R,K
+ */
+ public static final short PARAMETERS_KEYPAIR = 0x0180;
+ public static final short PARAMETERS_ALL = 0x01ff;
+
+ public static RandomData randomData = null;
+
+
+ // secp128r1
+ public static final byte[] EC128_FP_P = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFD,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
+
+ public static final byte[] EC128_FP_A = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFD,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC};
+
+ public static final byte[] EC128_FP_B = new byte[]{
+ (byte) 0xE8, (byte) 0x75, (byte) 0x79, (byte) 0xC1,
+ (byte) 0x10, (byte) 0x79, (byte) 0xF4, (byte) 0x3D,
+ (byte) 0xD8, (byte) 0x24, (byte) 0x99, (byte) 0x3C,
+ (byte) 0x2C, (byte) 0xEE, (byte) 0x5E, (byte) 0xD3};
+
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC128_FP_G_X = new byte[]{
+ (byte) 0x16, (byte) 0x1F, (byte) 0xF7, (byte) 0x52,
+ (byte) 0x8B, (byte) 0x89, (byte) 0x9B, (byte) 0x2D,
+ (byte) 0x0C, (byte) 0x28, (byte) 0x60, (byte) 0x7C,
+ (byte) 0xA5, (byte) 0x2C, (byte) 0x5B, (byte) 0x86};
+
+ // second part of G uncompressed
+ public static final byte[] EC128_FP_G_Y = new byte[]{
+ (byte) 0xCF, (byte) 0x5A, (byte) 0xC8, (byte) 0x39,
+ (byte) 0x5B, (byte) 0xAF, (byte) 0xEB, (byte) 0x13,
+ (byte) 0xC0, (byte) 0x2D, (byte) 0xA2, (byte) 0x92,
+ (byte) 0xDD, (byte) 0xED, (byte) 0x7A, (byte) 0x83};
+ // Order of G
+ public static final byte[] EC128_FP_R = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x75, (byte) 0xA3, (byte) 0x0D, (byte) 0x1B,
+ (byte) 0x90, (byte) 0x38, (byte) 0xA1, (byte) 0x15};
+ // cofactor of G
+ public static final short EC128_FP_K = 1;
+
+ // secp160r1
+ public static final byte[] EC160_FP_P = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
+
+ public static final byte[] EC160_FP_A = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC};
+
+ public static final byte[] EC160_FP_B = new byte[]{
+ (byte) 0x1C, (byte) 0x97, (byte) 0xBE, (byte) 0xFC,
+ (byte) 0x54, (byte) 0xBD, (byte) 0x7A, (byte) 0x8B,
+ (byte) 0x65, (byte) 0xAC, (byte) 0xF8, (byte) 0x9F,
+ (byte) 0x81, (byte) 0xD4, (byte) 0xD4, (byte) 0xAD,
+ (byte) 0xC5, (byte) 0x65, (byte) 0xFA, (byte) 0x45};
+
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC160_FP_G_X = new byte[]{
+ (byte) 0x4A, (byte) 0x96, (byte) 0xB5, (byte) 0x68,
+ (byte) 0x8E, (byte) 0xF5, (byte) 0x73, (byte) 0x28,
+ (byte) 0x46, (byte) 0x64, (byte) 0x69, (byte) 0x89,
+ (byte) 0x68, (byte) 0xC3, (byte) 0x8B, (byte) 0xB9,
+ (byte) 0x13, (byte) 0xCB, (byte) 0xFC, (byte) 0x82};
+
+ // second part of G uncompressed
+ public static final byte[] EC160_FP_G_Y = new byte[]{
+ (byte) 0x23, (byte) 0xA6, (byte) 0x28, (byte) 0x55,
+ (byte) 0x31, (byte) 0x68, (byte) 0x94, (byte) 0x7D,
+ (byte) 0x59, (byte) 0xDC, (byte) 0xC9, (byte) 0x12,
+ (byte) 0x04, (byte) 0x23, (byte) 0x51, (byte) 0x37,
+ (byte) 0x7A, (byte) 0xC5, (byte) 0xFB, (byte) 0x32};
+ // Order of G
+ public static final byte[] EC160_FP_R = new byte[]{
+ (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x01, (byte) 0xF4, (byte) 0xC8,
+ (byte) 0xF9, (byte) 0x27, (byte) 0xAE, (byte) 0xD3,
+ (byte) 0xCA, (byte) 0x75, (byte) 0x22, (byte) 0x57};
+ // cofactor of G
+ public static final short EC160_FP_K = 1;
+
+
+ // secp192r1 from http://www.secg.org/sec2-v2.pdf
+ public static final byte[] EC192_FP_P = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
+ public static final byte[] EC192_FP_A = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC};
+ public static final byte[] EC192_FP_B = new byte[]{
+ (byte) 0x64, (byte) 0x21, (byte) 0x05, (byte) 0x19,
+ (byte) 0xE5, (byte) 0x9C, (byte) 0x80, (byte) 0xE7,
+ (byte) 0x0F, (byte) 0xA7, (byte) 0xE9, (byte) 0xAB,
+ (byte) 0x72, (byte) 0x24, (byte) 0x30, (byte) 0x49,
+ (byte) 0xFE, (byte) 0xB8, (byte) 0xDE, (byte) 0xEC,
+ (byte) 0xC1, (byte) 0x46, (byte) 0xB9, (byte) 0xB1};
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC192_FP_G_X = new byte[]{
+ (byte) 0x18, (byte) 0x8D, (byte) 0xA8, (byte) 0x0E,
+ (byte) 0xB0, (byte) 0x30, (byte) 0x90, (byte) 0xF6,
+ (byte) 0x7C, (byte) 0xBF, (byte) 0x20, (byte) 0xEB,
+ (byte) 0x43, (byte) 0xA1, (byte) 0x88, (byte) 0x00,
+ (byte) 0xF4, (byte) 0xFF, (byte) 0x0A, (byte) 0xFD,
+ (byte) 0x82, (byte) 0xFF, (byte) 0x10, (byte) 0x12};
+ // second part of G uncompressed
+ public static final byte[] EC192_FP_G_Y = new byte[]{
+ (byte) 0x07, (byte) 0x19, (byte) 0x2B, (byte) 0x95,
+ (byte) 0xFF, (byte) 0xC8, (byte) 0xDA, (byte) 0x78,
+ (byte) 0x63, (byte) 0x10, (byte) 0x11, (byte) 0xED,
+ (byte) 0x6B, (byte) 0x24, (byte) 0xCD, (byte) 0xD5,
+ (byte) 0x73, (byte) 0xF9, (byte) 0x77, (byte) 0xA1,
+ (byte) 0x1E, (byte) 0x79, (byte) 0x48, (byte) 0x11};
+ // Order of G
+ public static final byte[] EC192_FP_R = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0x99, (byte) 0xDE, (byte) 0xF8, (byte) 0x36,
+ (byte) 0x14, (byte) 0x6B, (byte) 0xC9, (byte) 0xB1,
+ (byte) 0xB4, (byte) 0xD2, (byte) 0x28, (byte) 0x31};
+ // cofactor of G
+ public static final short EC192_FP_K = 1;
+
+ // secp224r1 from http://www.secg.org/sec2-v2.pdf
+ public static final byte[] EC224_FP_P = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01};
+
+ public static final byte[] EC224_FP_A = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE};
+
+ public static final byte[] EC224_FP_B = new byte[]{
+ (byte) 0xB4, (byte) 0x05, (byte) 0x0A, (byte) 0x85,
+ (byte) 0x0C, (byte) 0x04, (byte) 0xB3, (byte) 0xAB,
+ (byte) 0xF5, (byte) 0x41, (byte) 0x32, (byte) 0x56,
+ (byte) 0x50, (byte) 0x44, (byte) 0xB0, (byte) 0xB7,
+ (byte) 0xD7, (byte) 0xBF, (byte) 0xD8, (byte) 0xBA,
+ (byte) 0x27, (byte) 0x0B, (byte) 0x39, (byte) 0x43,
+ (byte) 0x23, (byte) 0x55, (byte) 0xFF, (byte) 0xB4};
+
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC224_FP_G_X = new byte[]{
+ (byte) 0xB7, (byte) 0x0E, (byte) 0x0C, (byte) 0xBD,
+ (byte) 0x6B, (byte) 0xB4, (byte) 0xBF, (byte) 0x7F,
+ (byte) 0x32, (byte) 0x13, (byte) 0x90, (byte) 0xB9,
+ (byte) 0x4A, (byte) 0x03, (byte) 0xC1, (byte) 0xD3,
+ (byte) 0x56, (byte) 0xC2, (byte) 0x11, (byte) 0x22,
+ (byte) 0x34, (byte) 0x32, (byte) 0x80, (byte) 0xD6,
+ (byte) 0x11, (byte) 0x5C, (byte) 0x1D, (byte) 0x21};
+ // second part of G uncompressed
+ public static final byte[] EC224_FP_G_Y = new byte[]{
+ (byte) 0xBD, (byte) 0x37, (byte) 0x63, (byte) 0x88,
+ (byte) 0xB5, (byte) 0xF7, (byte) 0x23, (byte) 0xFB,
+ (byte) 0x4C, (byte) 0x22, (byte) 0xDF, (byte) 0xE6,
+ (byte) 0xCD, (byte) 0x43, (byte) 0x75, (byte) 0xA0,
+ (byte) 0x5A, (byte) 0x07, (byte) 0x47, (byte) 0x64,
+ (byte) 0x44, (byte) 0xD5, (byte) 0x81, (byte) 0x99,
+ (byte) 0x85, (byte) 0x00, (byte) 0x7E, (byte) 0x34};
+ // Order of G
+ public static final byte[] EC224_FP_R = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0xA2,
+ (byte) 0xE0, (byte) 0xB8, (byte) 0xF0, (byte) 0x3E,
+ (byte) 0x13, (byte) 0xDD, (byte) 0x29, (byte) 0x45,
+ (byte) 0x5C, (byte) 0x5C, (byte) 0x2A, (byte) 0x3D};
+ // cofactor of G
+ public static final short EC224_FP_K = 1;
+
+ // secp256r1 from http://www.secg.org/sec2-v2.pdf
+ public static final byte[] EC256_FP_P = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
+ public static final byte[] EC256_FP_A = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC};
+ public static final byte[] EC256_FP_B = new byte[]{
+ (byte) 0x5A, (byte) 0xC6, (byte) 0x35, (byte) 0xD8,
+ (byte) 0xAA, (byte) 0x3A, (byte) 0x93, (byte) 0xE7,
+ (byte) 0xB3, (byte) 0xEB, (byte) 0xBD, (byte) 0x55,
+ (byte) 0x76, (byte) 0x98, (byte) 0x86, (byte) 0xBC,
+ (byte) 0x65, (byte) 0x1D, (byte) 0x06, (byte) 0xB0,
+ (byte) 0xCC, (byte) 0x53, (byte) 0xB0, (byte) 0xF6,
+ (byte) 0x3B, (byte) 0xCE, (byte) 0x3C, (byte) 0x3E,
+ (byte) 0x27, (byte) 0xD2, (byte) 0x60, (byte) 0x4B};
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC256_FP_G_X = new byte[]{
+ (byte) 0x6B, (byte) 0x17, (byte) 0xD1, (byte) 0xF2,
+ (byte) 0xE1, (byte) 0x2C, (byte) 0x42, (byte) 0x47,
+ (byte) 0xF8, (byte) 0xBC, (byte) 0xE6, (byte) 0xE5,
+ (byte) 0x63, (byte) 0xA4, (byte) 0x40, (byte) 0xF2,
+ (byte) 0x77, (byte) 0x03, (byte) 0x7D, (byte) 0x81,
+ (byte) 0x2D, (byte) 0xEB, (byte) 0x33, (byte) 0xA0,
+ (byte) 0xF4, (byte) 0xA1, (byte) 0x39, (byte) 0x45,
+ (byte) 0xD8, (byte) 0x98, (byte) 0xC2, (byte) 0x96};
+ // second part of G uncompressed
+ public static final byte[] EC256_FP_G_Y = new byte[]{
+ (byte) 0x4F, (byte) 0xE3, (byte) 0x42, (byte) 0xE2,
+ (byte) 0xFE, (byte) 0x1A, (byte) 0x7F, (byte) 0x9B,
+ (byte) 0x8E, (byte) 0xE7, (byte) 0xEB, (byte) 0x4A,
+ (byte) 0x7C, (byte) 0x0F, (byte) 0x9E, (byte) 0x16,
+ (byte) 0x2B, (byte) 0xCE, (byte) 0x33, (byte) 0x57,
+ (byte) 0x6B, (byte) 0x31, (byte) 0x5E, (byte) 0xCE,
+ (byte) 0xCB, (byte) 0xB6, (byte) 0x40, (byte) 0x68,
+ (byte) 0x37, (byte) 0xBF, (byte) 0x51, (byte) 0xF5};
+ // Order of G
+ public static final byte[] EC256_FP_R = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xBC, (byte) 0xE6, (byte) 0xFA, (byte) 0xAD,
+ (byte) 0xA7, (byte) 0x17, (byte) 0x9E, (byte) 0x84,
+ (byte) 0xF3, (byte) 0xB9, (byte) 0xCA, (byte) 0xC2,
+ (byte) 0xFC, (byte) 0x63, (byte) 0x25, (byte) 0x51};
+ // cofactor of G
+ public static final short EC256_FP_K = 1;
+
+ // secp384r1 from http://www.secg.org/sec2-v2.pdf
+ public static final byte[] EC384_FP_P = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
+
+ public static final byte[] EC384_FP_A = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC};
+
+ public static final byte[] EC384_FP_B = new byte[]{
+ (byte) 0xB3, (byte) 0x31, (byte) 0x2F, (byte) 0xA7,
+ (byte) 0xE2, (byte) 0x3E, (byte) 0xE7, (byte) 0xE4,
+ (byte) 0x98, (byte) 0x8E, (byte) 0x05, (byte) 0x6B,
+ (byte) 0xE3, (byte) 0xF8, (byte) 0x2D, (byte) 0x19,
+ (byte) 0x18, (byte) 0x1D, (byte) 0x9C, (byte) 0x6E,
+ (byte) 0xFE, (byte) 0x81, (byte) 0x41, (byte) 0x12,
+ (byte) 0x03, (byte) 0x14, (byte) 0x08, (byte) 0x8F,
+ (byte) 0x50, (byte) 0x13, (byte) 0x87, (byte) 0x5A,
+ (byte) 0xC6, (byte) 0x56, (byte) 0x39, (byte) 0x8D,
+ (byte) 0x8A, (byte) 0x2E, (byte) 0xD1, (byte) 0x9D,
+ (byte) 0x2A, (byte) 0x85, (byte) 0xC8, (byte) 0xED,
+ (byte) 0xD3, (byte) 0xEC, (byte) 0x2A, (byte) 0xEF};
+
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC384_FP_G_X = new byte[]{
+ (byte) 0xAA, (byte) 0x87, (byte) 0xCA, (byte) 0x22,
+ (byte) 0xBE, (byte) 0x8B, (byte) 0x05, (byte) 0x37,
+ (byte) 0x8E, (byte) 0xB1, (byte) 0xC7, (byte) 0x1E,
+ (byte) 0xF3, (byte) 0x20, (byte) 0xAD, (byte) 0x74,
+ (byte) 0x6E, (byte) 0x1D, (byte) 0x3B, (byte) 0x62,
+ (byte) 0x8B, (byte) 0xA7, (byte) 0x9B, (byte) 0x98,
+ (byte) 0x59, (byte) 0xF7, (byte) 0x41, (byte) 0xE0,
+ (byte) 0x82, (byte) 0x54, (byte) 0x2A, (byte) 0x38,
+ (byte) 0x55, (byte) 0x02, (byte) 0xF2, (byte) 0x5D,
+ (byte) 0xBF, (byte) 0x55, (byte) 0x29, (byte) 0x6C,
+ (byte) 0x3A, (byte) 0x54, (byte) 0x5E, (byte) 0x38,
+ (byte) 0x72, (byte) 0x76, (byte) 0x0A, (byte) 0xB7};
+ // second part of G uncompressed
+ public static final byte[] EC384_FP_G_Y = new byte[]{
+ (byte) 0x36, (byte) 0x17, (byte) 0xDE, (byte) 0x4A,
+ (byte) 0x96, (byte) 0x26, (byte) 0x2C, (byte) 0x6F,
+ (byte) 0x5D, (byte) 0x9E, (byte) 0x98, (byte) 0xBF,
+ (byte) 0x92, (byte) 0x92, (byte) 0xDC, (byte) 0x29,
+ (byte) 0xF8, (byte) 0xF4, (byte) 0x1D, (byte) 0xBD,
+ (byte) 0x28, (byte) 0x9A, (byte) 0x14, (byte) 0x7C,
+ (byte) 0xE9, (byte) 0xDA, (byte) 0x31, (byte) 0x13,
+ (byte) 0xB5, (byte) 0xF0, (byte) 0xB8, (byte) 0xC0,
+ (byte) 0x0A, (byte) 0x60, (byte) 0xB1, (byte) 0xCE,
+ (byte) 0x1D, (byte) 0x7E, (byte) 0x81, (byte) 0x9D,
+ (byte) 0x7A, (byte) 0x43, (byte) 0x1D, (byte) 0x7C,
+ (byte) 0x90, (byte) 0xEA, (byte) 0x0E, (byte) 0x5F};
+
+ // Order of G
+ public static final byte[] EC384_FP_R = new byte[]{
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xC7, (byte) 0x63, (byte) 0x4D, (byte) 0x81,
+ (byte) 0xF4, (byte) 0x37, (byte) 0x2D, (byte) 0xDF,
+ (byte) 0x58, (byte) 0x1A, (byte) 0x0D, (byte) 0xB2,
+ (byte) 0x48, (byte) 0xB0, (byte) 0xA7, (byte) 0x7A,
+ (byte) 0xEC, (byte) 0xEC, (byte) 0x19, (byte) 0x6A,
+ (byte) 0xCC, (byte) 0xC5, (byte) 0x29, (byte) 0x73};
+ // cofactor of G
+ public static final short EC384_FP_K = 1;
+
+
+ // secp521r1 from http://www.secg.org/sec2-v2.pdf
+ public static final byte[] EC521_FP_P = new byte[]{
+ (byte) 0x01, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
+
+ public static final byte[] EC521_FP_A = new byte[]{
+ (byte) 0x01, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFC};
+
+ public static final byte[] EC521_FP_B = new byte[]{
+ (byte) 0x00, (byte) 0x51, (byte) 0x95, (byte) 0x3E,
+ (byte) 0xB9, (byte) 0x61, (byte) 0x8E, (byte) 0x1C,
+ (byte) 0x9A, (byte) 0x1F, (byte) 0x92, (byte) 0x9A,
+ (byte) 0x21, (byte) 0xA0, (byte) 0xB6, (byte) 0x85,
+ (byte) 0x40, (byte) 0xEE, (byte) 0xA2, (byte) 0xDA,
+ (byte) 0x72, (byte) 0x5B, (byte) 0x99, (byte) 0xB3,
+ (byte) 0x15, (byte) 0xF3, (byte) 0xB8, (byte) 0xB4,
+ (byte) 0x89, (byte) 0x91, (byte) 0x8E, (byte) 0xF1,
+ (byte) 0x09, (byte) 0xE1, (byte) 0x56, (byte) 0x19,
+ (byte) 0x39, (byte) 0x51, (byte) 0xEC, (byte) 0x7E,
+ (byte) 0x93, (byte) 0x7B, (byte) 0x16, (byte) 0x52,
+ (byte) 0xC0, (byte) 0xBD, (byte) 0x3B, (byte) 0xB1,
+ (byte) 0xBF, (byte) 0x07, (byte) 0x35, (byte) 0x73,
+ (byte) 0xDF, (byte) 0x88, (byte) 0x3D, (byte) 0x2C,
+ (byte) 0x34, (byte) 0xF1, (byte) 0xEF, (byte) 0x45,
+ (byte) 0x1F, (byte) 0xD4, (byte) 0x6B, (byte) 0x50,
+ (byte) 0x3F, (byte) 0x00};
+
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC521_FP_G_X = new byte[]{
+ (byte) 0x00, (byte) 0xC6, (byte) 0x85, (byte) 0x8E,
+ (byte) 0x06, (byte) 0xB7, (byte) 0x04, (byte) 0x04,
+ (byte) 0xE9, (byte) 0xCD, (byte) 0x9E, (byte) 0x3E,
+ (byte) 0xCB, (byte) 0x66, (byte) 0x23, (byte) 0x95,
+ (byte) 0xB4, (byte) 0x42, (byte) 0x9C, (byte) 0x64,
+ (byte) 0x81, (byte) 0x39, (byte) 0x05, (byte) 0x3F,
+ (byte) 0xB5, (byte) 0x21, (byte) 0xF8, (byte) 0x28,
+ (byte) 0xAF, (byte) 0x60, (byte) 0x6B, (byte) 0x4D,
+ (byte) 0x3D, (byte) 0xBA, (byte) 0xA1, (byte) 0x4B,
+ (byte) 0x5E, (byte) 0x77, (byte) 0xEF, (byte) 0xE7,
+ (byte) 0x59, (byte) 0x28, (byte) 0xFE, (byte) 0x1D,
+ (byte) 0xC1, (byte) 0x27, (byte) 0xA2, (byte) 0xFF,
+ (byte) 0xA8, (byte) 0xDE, (byte) 0x33, (byte) 0x48,
+ (byte) 0xB3, (byte) 0xC1, (byte) 0x85, (byte) 0x6A,
+ (byte) 0x42, (byte) 0x9B, (byte) 0xF9, (byte) 0x7E,
+ (byte) 0x7E, (byte) 0x31, (byte) 0xC2, (byte) 0xE5,
+ (byte) 0xBD, (byte) 0x66};
+
+ // second part of G uncompressed
+ public static final byte[] EC521_FP_G_Y = new byte[]{
+ (byte) 0x01, (byte) 0x18, (byte) 0x39, (byte) 0x29,
+ (byte) 0x6A, (byte) 0x78, (byte) 0x9A, (byte) 0x3B,
+ (byte) 0xC0, (byte) 0x04, (byte) 0x5C, (byte) 0x8A,
+ (byte) 0x5F, (byte) 0xB4, (byte) 0x2C, (byte) 0x7D,
+ (byte) 0x1B, (byte) 0xD9, (byte) 0x98, (byte) 0xF5,
+ (byte) 0x44, (byte) 0x49, (byte) 0x57, (byte) 0x9B,
+ (byte) 0x44, (byte) 0x68, (byte) 0x17, (byte) 0xAF,
+ (byte) 0xBD, (byte) 0x17, (byte) 0x27, (byte) 0x3E,
+ (byte) 0x66, (byte) 0x2C, (byte) 0x97, (byte) 0xEE,
+ (byte) 0x72, (byte) 0x99, (byte) 0x5E, (byte) 0xF4,
+ (byte) 0x26, (byte) 0x40, (byte) 0xC5, (byte) 0x50,
+ (byte) 0xB9, (byte) 0x01, (byte) 0x3F, (byte) 0xAD,
+ (byte) 0x07, (byte) 0x61, (byte) 0x35, (byte) 0x3C,
+ (byte) 0x70, (byte) 0x86, (byte) 0xA2, (byte) 0x72,
+ (byte) 0xC2, (byte) 0x40, (byte) 0x88, (byte) 0xBE,
+ (byte) 0x94, (byte) 0x76, (byte) 0x9F, (byte) 0xD1,
+ (byte) 0x66, (byte) 0x50};
+
+ // Order of G
+ public static final byte[] EC521_FP_R = new byte[]{
+ (byte) 0x01, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFA,
+ (byte) 0x51, (byte) 0x86, (byte) 0x87, (byte) 0x83,
+ (byte) 0xBF, (byte) 0x2F, (byte) 0x96, (byte) 0x6B,
+ (byte) 0x7F, (byte) 0xCC, (byte) 0x01, (byte) 0x48,
+ (byte) 0xF7, (byte) 0x09, (byte) 0xA5, (byte) 0xD0,
+ (byte) 0x3B, (byte) 0xB5, (byte) 0xC9, (byte) 0xB8,
+ (byte) 0x89, (byte) 0x9C, (byte) 0x47, (byte) 0xAE,
+ (byte) 0xBB, (byte) 0x6F, (byte) 0xB7, (byte) 0x1E,
+ (byte) 0x91, (byte) 0x38, (byte) 0x64, (byte) 0x09};
+
+ // cofactor of G
+ public static final short EC521_FP_K = 1;
+
+ //sect163r1 from http://www.secg.org/sec2-v2.pdf
+ // [short i1, short i2, short i3] f = x^163 + x^i1 + x^i2 + x^i3 + 1
+ public static final byte[] EC163_F2M_F = new byte[]{
+ (byte) 0x00, (byte) 0x07,
+ (byte) 0x00, (byte) 0x06,
+ (byte) 0x00, (byte) 0x03
+ };
+
+ public static final byte[] EC163_F2M_A = new byte[]{
+ (byte) 0x07, (byte) 0xB6, (byte) 0x88, (byte) 0x2C,
+ (byte) 0xAA, (byte) 0xEF, (byte) 0xA8, (byte) 0x4F,
+ (byte) 0x95, (byte) 0x54, (byte) 0xFF, (byte) 0x84,
+ (byte) 0x28, (byte) 0xBD, (byte) 0x88, (byte) 0xE2,
+ (byte) 0x46, (byte) 0xD2, (byte) 0x78, (byte) 0x2A,
+ (byte) 0xE2
+ };
+
+ public static final byte[] EC163_F2M_B = new byte[]{
+ (byte) 0x07, (byte) 0x13, (byte) 0x61, (byte) 0x2D,
+ (byte) 0xCD, (byte) 0xDC, (byte) 0xB4, (byte) 0x0A,
+ (byte) 0xAB, (byte) 0x94, (byte) 0x6B, (byte) 0xDA,
+ (byte) 0x29, (byte) 0xCA, (byte) 0x91, (byte) 0xF7,
+ (byte) 0x3A, (byte) 0xF9, (byte) 0x58, (byte) 0xAF,
+ (byte) 0xD9
+ };
+
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC163_F2M_G_X = new byte[]{
+ (byte) 0x03, (byte) 0x69, (byte) 0x97, (byte) 0x96,
+ (byte) 0x97, (byte) 0xAB, (byte) 0x43, (byte) 0x89,
+ (byte) 0x77, (byte) 0x89, (byte) 0x56, (byte) 0x67,
+ (byte) 0x89, (byte) 0x56, (byte) 0x7F, (byte) 0x78,
+ (byte) 0x7A, (byte) 0x78, (byte) 0x76, (byte) 0xA6,
+ (byte) 0x54
+ };
+
+ // second part of G uncompressed
+ public static final byte[] EC163_F2M_G_Y = new byte[]{
+ (byte) 0x00, (byte) 0x43, (byte) 0x5E, (byte) 0xDB,
+ (byte) 0x42, (byte) 0xEF, (byte) 0xAF, (byte) 0xB2,
+ (byte) 0x98, (byte) 0x9D, (byte) 0x51, (byte) 0xFE,
+ (byte) 0xFC, (byte) 0xE3, (byte) 0xC8, (byte) 0x09,
+ (byte) 0x88, (byte) 0xF4, (byte) 0x1F, (byte) 0xF8,
+ (byte) 0x83
+ };
+
+ // order of G
+ public static final byte[] EC163_F2M_R = new byte[]{
+ (byte) 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x48,
+ (byte) 0xAA, (byte) 0xB6, (byte) 0x89, (byte) 0xC2,
+ (byte) 0x9C, (byte) 0xA7, (byte) 0x10, (byte) 0x27,
+ (byte) 0x9B
+ };
+
+ // cofactor of G
+ public static final short EC163_F2M_K = 2;
+
+ //sect233r1 from http://www.secg.org/sec2-v2.pdf
+ // [short i1, short i2, short i3] f = x^233 + x^i1 + 1
+ public static final byte[] EC233_F2M_F = new byte[]{
+ (byte) 0x00, (byte) 0x4a
+ };
+
+ public static final byte[] EC233_F2M_A = new byte[]{
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x01
+ };
+
+ public static final byte[] EC233_F2M_B = new byte[]{
+ (byte) 0x00, (byte) 0x66, (byte) 0x64, (byte) 0x7E,
+ (byte) 0xDE, (byte) 0x6C, (byte) 0x33, (byte) 0x2C,
+ (byte) 0x7F, (byte) 0x8C, (byte) 0x09, (byte) 0x23,
+ (byte) 0xBB, (byte) 0x58, (byte) 0x21, (byte) 0x3B,
+ (byte) 0x33, (byte) 0x3B, (byte) 0x20, (byte) 0xE9,
+ (byte) 0xCE, (byte) 0x42, (byte) 0x81, (byte) 0xFE,
+ (byte) 0x11, (byte) 0x5F, (byte) 0x7D, (byte) 0x8F,
+ (byte) 0x90, (byte) 0xAD
+ };
+
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC233_F2M_G_X = new byte[]{
+ (byte) 0x00, (byte) 0xFA, (byte) 0xC9, (byte) 0xDF,
+ (byte) 0xCB, (byte) 0xAC, (byte) 0x83, (byte) 0x13,
+ (byte) 0xBB, (byte) 0x21, (byte) 0x39, (byte) 0xF1,
+ (byte) 0xBB, (byte) 0x75, (byte) 0x5F, (byte) 0xEF,
+ (byte) 0x65, (byte) 0xBC, (byte) 0x39, (byte) 0x1F,
+ (byte) 0x8B, (byte) 0x36, (byte) 0xF8, (byte) 0xF8,
+ (byte) 0xEB, (byte) 0x73, (byte) 0x71, (byte) 0xFD,
+ (byte) 0x55, (byte) 0x8B
+ };
+
+ // second part of G uncompressed
+ public static final byte[] EC233_F2M_G_Y = new byte[]{
+ (byte) 0x01, (byte) 0x00, (byte) 0x6A, (byte) 0x08,
+ (byte) 0xA4, (byte) 0x19, (byte) 0x03, (byte) 0x35,
+ (byte) 0x06, (byte) 0x78, (byte) 0xE5, (byte) 0x85,
+ (byte) 0x28, (byte) 0xBE, (byte) 0xBF, (byte) 0x8A,
+ (byte) 0x0B, (byte) 0xEF, (byte) 0xF8, (byte) 0x67,
+ (byte) 0xA7, (byte) 0xCA, (byte) 0x36, (byte) 0x71,
+ (byte) 0x6F, (byte) 0x7E, (byte) 0x01, (byte) 0xF8,
+ (byte) 0x10, (byte) 0x52
+ };
+
+ // order of G
+ public static final byte[] EC233_F2M_R = new byte[]{
+ (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x13,
+ (byte) 0xE9, (byte) 0x74, (byte) 0xE7, (byte) 0x2F,
+ (byte) 0x8A, (byte) 0x69, (byte) 0x22, (byte) 0x03,
+ (byte) 0x1D, (byte) 0x26, (byte) 0x03, (byte) 0xCF,
+ (byte) 0xE0, (byte) 0xD7
+ };
+
+ // cofactor of G
+ public static final short EC233_F2M_K = 2;
+
+ //sect283r1 from http://www.secg.org/sec2-v2.pdf
+ // [short i1, short i2, short i3] f = x^283 + x^i1 + x^i2 + x^i3 + 1
+ public static final byte[] EC283_F2M_F = new byte[]{
+ (byte) 0x00, (byte) 0x0c,
+ (byte) 0x00, (byte) 0x07,
+ (byte) 0x00, (byte) 0x05
+ };
+
+ public static final byte[] EC283_F2M_A = new byte[]{
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
+ };
+
+ public static final byte[] EC283_F2M_B = new byte[]{
+ (byte) 0x02, (byte) 0x7B, (byte) 0x68, (byte) 0x0A,
+ (byte) 0xC8, (byte) 0xB8, (byte) 0x59, (byte) 0x6D,
+ (byte) 0xA5, (byte) 0xA4, (byte) 0xAF, (byte) 0x8A,
+ (byte) 0x19, (byte) 0xA0, (byte) 0x30, (byte) 0x3F,
+ (byte) 0xCA, (byte) 0x97, (byte) 0xFD, (byte) 0x76,
+ (byte) 0x45, (byte) 0x30, (byte) 0x9F, (byte) 0xA2,
+ (byte) 0xA5, (byte) 0x81, (byte) 0x48, (byte) 0x5A,
+ (byte) 0xF6, (byte) 0x26, (byte) 0x3E, (byte) 0x31,
+ (byte) 0x3B, (byte) 0x79, (byte) 0xA2, (byte) 0xF5
+ };
+
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC283_F2M_G_X = new byte[]{
+ (byte) 0x05, (byte) 0xF9, (byte) 0x39, (byte) 0x25,
+ (byte) 0x8D, (byte) 0xB7, (byte) 0xDD, (byte) 0x90,
+ (byte) 0xE1, (byte) 0x93, (byte) 0x4F, (byte) 0x8C,
+ (byte) 0x70, (byte) 0xB0, (byte) 0xDF, (byte) 0xEC,
+ (byte) 0x2E, (byte) 0xED, (byte) 0x25, (byte) 0xB8,
+ (byte) 0x55, (byte) 0x7E, (byte) 0xAC, (byte) 0x9C,
+ (byte) 0x80, (byte) 0xE2, (byte) 0xE1, (byte) 0x98,
+ (byte) 0xF8, (byte) 0xCD, (byte) 0xBE, (byte) 0xCD,
+ (byte) 0x86, (byte) 0xB1, (byte) 0x20, (byte) 0x53
+ };
+
+ // second part of G uncompressed
+ public static final byte[] EC283_F2M_G_Y = new byte[]{
+ (byte) 0x03, (byte) 0x67, (byte) 0x68, (byte) 0x54,
+ (byte) 0xFE, (byte) 0x24, (byte) 0x14, (byte) 0x1C,
+ (byte) 0xB9, (byte) 0x8F, (byte) 0xE6, (byte) 0xD4,
+ (byte) 0xB2, (byte) 0x0D, (byte) 0x02, (byte) 0xB4,
+ (byte) 0x51, (byte) 0x6F, (byte) 0xF7, (byte) 0x02,
+ (byte) 0x35, (byte) 0x0E, (byte) 0xDD, (byte) 0xB0,
+ (byte) 0x82, (byte) 0x67, (byte) 0x79, (byte) 0xC8,
+ (byte) 0x13, (byte) 0xF0, (byte) 0xDF, (byte) 0x45,
+ (byte) 0xBE, (byte) 0x81, (byte) 0x12, (byte) 0xF4
+ };
+
+ // order of G
+ public static final byte[] EC283_F2M_R = new byte[]{
+ (byte) 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xEF, (byte) 0x90,
+ (byte) 0x39, (byte) 0x96, (byte) 0x60, (byte) 0xFC,
+ (byte) 0x93, (byte) 0x8A, (byte) 0x90, (byte) 0x16,
+ (byte) 0x5B, (byte) 0x04, (byte) 0x2A, (byte) 0x7C,
+ (byte) 0xEF, (byte) 0xAD, (byte) 0xB3, (byte) 0x07
+ };
+
+ // cofactor of G
+ public static final short EC283_F2M_K = 2;
+
+ //sect409r1 from http://www.secg.org/sec2-v2.pdf
+ // [short i1, short i2, short i3] f = x^409 + x^i1 + 1
+ public static final byte[] EC409_F2M_F = new byte[]{
+ (byte) 0x00, (byte) 0x57
+ };
+
+ public static final byte[] EC409_F2M_A = new byte[]{
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
+ };
+
+ public static final byte[] EC409_F2M_B = new byte[]{
+ (byte) 0x00, (byte) 0x21, (byte) 0xA5, (byte) 0xC2,
+ (byte) 0xC8, (byte) 0xEE, (byte) 0x9F, (byte) 0xEB,
+ (byte) 0x5C, (byte) 0x4B, (byte) 0x9A, (byte) 0x75,
+ (byte) 0x3B, (byte) 0x7B, (byte) 0x47, (byte) 0x6B,
+ (byte) 0x7F, (byte) 0xD6, (byte) 0x42, (byte) 0x2E,
+ (byte) 0xF1, (byte) 0xF3, (byte) 0xDD, (byte) 0x67,
+ (byte) 0x47, (byte) 0x61, (byte) 0xFA, (byte) 0x99,
+ (byte) 0xD6, (byte) 0xAC, (byte) 0x27, (byte) 0xC8,
+ (byte) 0xA9, (byte) 0xA1, (byte) 0x97, (byte) 0xB2,
+ (byte) 0x72, (byte) 0x82, (byte) 0x2F, (byte) 0x6C,
+ (byte) 0xD5, (byte) 0x7A, (byte) 0x55, (byte) 0xAA,
+ (byte) 0x4F, (byte) 0x50, (byte) 0xAE, (byte) 0x31,
+ (byte) 0x7B, (byte) 0x13, (byte) 0x54, (byte) 0x5F
+ };
+
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC409_F2M_G_X = new byte[]{
+ (byte) 0x01, (byte) 0x5D, (byte) 0x48, (byte) 0x60,
+ (byte) 0xD0, (byte) 0x88, (byte) 0xDD, (byte) 0xB3,
+ (byte) 0x49, (byte) 0x6B, (byte) 0x0C, (byte) 0x60,
+ (byte) 0x64, (byte) 0x75, (byte) 0x62, (byte) 0x60,
+ (byte) 0x44, (byte) 0x1C, (byte) 0xDE, (byte) 0x4A,
+ (byte) 0xF1, (byte) 0x77, (byte) 0x1D, (byte) 0x4D,
+ (byte) 0xB0, (byte) 0x1F, (byte) 0xFE, (byte) 0x5B,
+ (byte) 0x34, (byte) 0xE5, (byte) 0x97, (byte) 0x03,
+ (byte) 0xDC, (byte) 0x25, (byte) 0x5A, (byte) 0x86,
+ (byte) 0x8A, (byte) 0x11, (byte) 0x80, (byte) 0x51,
+ (byte) 0x56, (byte) 0x03, (byte) 0xAE, (byte) 0xAB,
+ (byte) 0x60, (byte) 0x79, (byte) 0x4E, (byte) 0x54,
+ (byte) 0xBB, (byte) 0x79, (byte) 0x96, (byte) 0xA7
+ };
+
+ // second part of G uncompressed
+ public static final byte[] EC409_F2M_G_Y = new byte[]{
+ (byte) 0x00, (byte) 0x61, (byte) 0xB1, (byte) 0xCF,
+ (byte) 0xAB, (byte) 0x6B, (byte) 0xE5, (byte) 0xF3,
+ (byte) 0x2B, (byte) 0xBF, (byte) 0xA7, (byte) 0x83,
+ (byte) 0x24, (byte) 0xED, (byte) 0x10, (byte) 0x6A,
+ (byte) 0x76, (byte) 0x36, (byte) 0xB9, (byte) 0xC5,
+ (byte) 0xA7, (byte) 0xBD, (byte) 0x19, (byte) 0x8D,
+ (byte) 0x01, (byte) 0x58, (byte) 0xAA, (byte) 0x4F,
+ (byte) 0x54, (byte) 0x88, (byte) 0xD0, (byte) 0x8F,
+ (byte) 0x38, (byte) 0x51, (byte) 0x4F, (byte) 0x1F,
+ (byte) 0xDF, (byte) 0x4B, (byte) 0x4F, (byte) 0x40,
+ (byte) 0xD2, (byte) 0x18, (byte) 0x1B, (byte) 0x36,
+ (byte) 0x81, (byte) 0xC3, (byte) 0x64, (byte) 0xBA,
+ (byte) 0x02, (byte) 0x73, (byte) 0xC7, (byte) 0x06
+ };
+
+ // order of G
+ public static final byte[] EC409_F2M_R = new byte[]{
+ (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xE2,
+ (byte) 0xAA, (byte) 0xD6, (byte) 0xA6, (byte) 0x12,
+ (byte) 0xF3, (byte) 0x33, (byte) 0x07, (byte) 0xBE,
+ (byte) 0x5F, (byte) 0xA4, (byte) 0x7C, (byte) 0x3C,
+ (byte) 0x9E, (byte) 0x05, (byte) 0x2F, (byte) 0x83,
+ (byte) 0x81, (byte) 0x64, (byte) 0xCD, (byte) 0x37,
+ (byte) 0xD9, (byte) 0xA2, (byte) 0x11, (byte) 0x73
+ };
+
+ // cofactor of G
+ public static final short EC409_F2M_K = 2;
+
+ //sect571r1 from http://www.secg.org/sec2-v2.pdf
+ // [short i1, short i2, short i3] f = x^571 + x^i1 + x^i2 + x^i3 + 1
+ public static final byte[] EC571_F2M_F = new byte[]{
+ (byte) 0x00, (byte) 0x0a,
+ (byte) 0x00, (byte) 0x05,
+ (byte) 0x00, (byte) 0x02,
+ };
+
+ public static final byte[] EC571_F2M_A = new byte[]{
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
+ };
+
+ public static final byte[] EC571_F2M_B = new byte[]{
+ (byte) 0x02, (byte) 0xF4, (byte) 0x0E, (byte) 0x7E,
+ (byte) 0x22, (byte) 0x21, (byte) 0xF2, (byte) 0x95,
+ (byte) 0xDE, (byte) 0x29, (byte) 0x71, (byte) 0x17,
+ (byte) 0xB7, (byte) 0xF3, (byte) 0xD6, (byte) 0x2F,
+ (byte) 0x5C, (byte) 0x6A, (byte) 0x97, (byte) 0xFF,
+ (byte) 0xCB, (byte) 0x8C, (byte) 0xEF, (byte) 0xF1,
+ (byte) 0xCD, (byte) 0x6B, (byte) 0xA8, (byte) 0xCE,
+ (byte) 0x4A, (byte) 0x9A, (byte) 0x18, (byte) 0xAD,
+ (byte) 0x84, (byte) 0xFF, (byte) 0xAB, (byte) 0xBD,
+ (byte) 0x8E, (byte) 0xFA, (byte) 0x59, (byte) 0x33,
+ (byte) 0x2B, (byte) 0xE7, (byte) 0xAD, (byte) 0x67,
+ (byte) 0x56, (byte) 0xA6, (byte) 0x6E, (byte) 0x29,
+ (byte) 0x4A, (byte) 0xFD, (byte) 0x18, (byte) 0x5A,
+ (byte) 0x78, (byte) 0xFF, (byte) 0x12, (byte) 0xAA,
+ (byte) 0x52, (byte) 0x0E, (byte) 0x4D, (byte) 0xE7,
+ (byte) 0x39, (byte) 0xBA, (byte) 0xCA, (byte) 0x0C,
+ (byte) 0x7F, (byte) 0xFE, (byte) 0xFF, (byte) 0x7F,
+ (byte) 0x29, (byte) 0x55, (byte) 0x72, (byte) 0x7A
+ };
+
+ // G in compressed form / first part of ucompressed
+ public static final byte[] EC571_F2M_G_X = new byte[]{
+ (byte) 0x03, (byte) 0x03, (byte) 0x00, (byte) 0x1D,
+ (byte) 0x34, (byte) 0xB8, (byte) 0x56, (byte) 0x29,
+ (byte) 0x6C, (byte) 0x16, (byte) 0xC0, (byte) 0xD4,
+ (byte) 0x0D, (byte) 0x3C, (byte) 0xD7, (byte) 0x75,
+ (byte) 0x0A, (byte) 0x93, (byte) 0xD1, (byte) 0xD2,
+ (byte) 0x95, (byte) 0x5F, (byte) 0xA8, (byte) 0x0A,
+ (byte) 0xA5, (byte) 0xF4, (byte) 0x0F, (byte) 0xC8,
+ (byte) 0xDB, (byte) 0x7B, (byte) 0x2A, (byte) 0xBD,
+ (byte) 0xBD, (byte) 0xE5, (byte) 0x39, (byte) 0x50,
+ (byte) 0xF4, (byte) 0xC0, (byte) 0xD2, (byte) 0x93,
+ (byte) 0xCD, (byte) 0xD7, (byte) 0x11, (byte) 0xA3,
+ (byte) 0x5B, (byte) 0x67, (byte) 0xFB, (byte) 0x14,
+ (byte) 0x99, (byte) 0xAE, (byte) 0x60, (byte) 0x03,
+ (byte) 0x86, (byte) 0x14, (byte) 0xF1, (byte) 0x39,
+ (byte) 0x4A, (byte) 0xBF, (byte) 0xA3, (byte) 0xB4,
+ (byte) 0xC8, (byte) 0x50, (byte) 0xD9, (byte) 0x27,
+ (byte) 0xE1, (byte) 0xE7, (byte) 0x76, (byte) 0x9C,
+ (byte) 0x8E, (byte) 0xEC, (byte) 0x2D, (byte) 0x19
+ };
+
+ // second part of G uncompressed
+ public static final byte[] EC571_F2M_G_Y = new byte[]{
+ (byte) 0x03, (byte) 0x7B, (byte) 0xF2, (byte) 0x73,
+ (byte) 0x42, (byte) 0xDA, (byte) 0x63, (byte) 0x9B,
+ (byte) 0x6D, (byte) 0xCC, (byte) 0xFF, (byte) 0xFE,
+ (byte) 0xB7, (byte) 0x3D, (byte) 0x69, (byte) 0xD7,
+ (byte) 0x8C, (byte) 0x6C, (byte) 0x27, (byte) 0xA6,
+ (byte) 0x00, (byte) 0x9C, (byte) 0xBB, (byte) 0xCA,
+ (byte) 0x19, (byte) 0x80, (byte) 0xF8, (byte) 0x53,
+ (byte) 0x39, (byte) 0x21, (byte) 0xE8, (byte) 0xA6,
+ (byte) 0x84, (byte) 0x42, (byte) 0x3E, (byte) 0x43,
+ (byte) 0xBA, (byte) 0xB0, (byte) 0x8A, (byte) 0x57,
+ (byte) 0x62, (byte) 0x91, (byte) 0xAF, (byte) 0x8F,
+ (byte) 0x46, (byte) 0x1B, (byte) 0xB2, (byte) 0xA8,
+ (byte) 0xB3, (byte) 0x53, (byte) 0x1D, (byte) 0x2F,
+ (byte) 0x04, (byte) 0x85, (byte) 0xC1, (byte) 0x9B,
+ (byte) 0x16, (byte) 0xE2, (byte) 0xF1, (byte) 0x51,
+ (byte) 0x6E, (byte) 0x23, (byte) 0xDD, (byte) 0x3C,
+ (byte) 0x1A, (byte) 0x48, (byte) 0x27, (byte) 0xAF,
+ (byte) 0x1B, (byte) 0x8A, (byte) 0xC1, (byte) 0x5B
+ };
+
+ // order of G
+ public static final byte[] EC571_F2M_R = new byte[]{
+ (byte) 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ (byte) 0xE6, (byte) 0x61, (byte) 0xCE, (byte) 0x18,
+ (byte) 0xFF, (byte) 0x55, (byte) 0x98, (byte) 0x73,
+ (byte) 0x08, (byte) 0x05, (byte) 0x9B, (byte) 0x18,
+ (byte) 0x68, (byte) 0x23, (byte) 0x85, (byte) 0x1E,
+ (byte) 0xC7, (byte) 0xDD, (byte) 0x9C, (byte) 0xA1,
+ (byte) 0x16, (byte) 0x1D, (byte) 0xE9, (byte) 0x3D,
+ (byte) 0x51, (byte) 0x74, (byte) 0xD6, (byte) 0x6E,
+ (byte) 0x83, (byte) 0x82, (byte) 0xE9, (byte) 0xBB,
+ (byte) 0x2F, (byte) 0xE8, (byte) 0x4E, (byte) 0x47
+ };
+
+ // cofactor of G
+ public static final short EC571_F2M_K = 2;
+
+
+ // getCorruptCurveParameter PARAMETER_CORRUPTION TYPES
+ public static final byte CORRUPTION_NONE = (byte) 0x00;
+ public static final byte CORRUPTION_FIXED = (byte) 0x01;
+ public static final byte CORRUPTION_FULLRANDOM = (byte) 0x02;
+ public static final byte CORRUPTION_ONEBYTERANDOM = (byte) 0x03;
+ public static final byte CORRUPTION_ZERO = (byte) 0x04;
+ public static final byte CORRUPTION_ONE = (byte) 0x05;
+
+
+ // Supported embedded curves, getCurveParameter
+ public static final byte CURVE_default = (byte) 0;
+ public static final byte CURVE_external = (byte) 0xff;
+
+ // SECP recommended curves over FP
+ public static final byte CURVE_secp128r1 = (byte) 1;
+ public static final byte CURVE_secp160r1 = (byte) 2;
+ public static final byte CURVE_secp192r1 = (byte) 3;
+ public static final byte CURVE_secp224r1 = (byte) 4;
+ public static final byte CURVE_secp256r1 = (byte) 5;
+ public static final byte CURVE_secp384r1 = (byte) 6;
+ public static final byte CURVE_secp521r1 = (byte) 7;
+
+ public static final byte FP_CURVES = (byte) 7;
+
+ // SECP recommended curves over F2M
+ public static final byte CURVE_sect163r1 = (byte) 8;
+ public static final byte CURVE_sect233r1 = (byte) 9;
+ public static final byte CURVE_sect283r1 = (byte) 10;
+ public static final byte CURVE_sect409r1 = (byte) 11;
+ public static final byte CURVE_sect571r1 = (byte) 12;
+
+ public static final byte F2M_CURVES = (byte) 12;
+
+ public static final short[] FP_SIZES = new short[]{128, 160, 192, 224, 256, 384, 521};
+ public static final short[] F2M_SIZES = new short[]{163, 233, 283, 409, 571};
+
+ public static byte getCurve(short keyLength, byte keyClass) {
+ if (keyClass == KeyPair.ALG_EC_FP) {
+ switch (keyLength) {
+ case (short) 128:
+ return CURVE_secp128r1;
+ case (short) 160:
+ return CURVE_secp160r1;
+ case (short) 192:
+ return CURVE_secp192r1;
+ case (short) 224:
+ return CURVE_secp224r1;
+ case (short) 256:
+ return CURVE_secp256r1;
+ case (short) 384:
+ return CURVE_secp384r1;
+ case (short) 521:
+ return CURVE_secp521r1;
+ default:
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
+ } else if (keyClass == KeyPair.ALG_EC_F2M) {
+ switch (keyLength) {
+ case (short) 163:
+ return CURVE_sect163r1;
+ case (short) 233:
+ return CURVE_sect233r1;
+ case (short) 283:
+ return CURVE_sect283r1;
+ case (short) 409:
+ return CURVE_sect409r1;
+ case (short) 571:
+ return CURVE_sect571r1;
+ default:
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
+ } else {
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
+ return 0;
+ }
+
+ public static short getCurveParameter(byte curve, short param, byte[] outputBuffer, short outputOffset) {
+ byte alg = getCurveType(curve);
+ switch (curve) {
+ case CURVE_secp128r1: {
+ EC_FP_P = EC128_FP_P;
+ EC_A = EC128_FP_A;
+ EC_B = EC128_FP_B;
+ EC_G_X = EC128_FP_G_X;
+ EC_G_Y = EC128_FP_G_Y;
+ EC_R = EC128_FP_R;
+ EC_K = EC128_FP_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ case CURVE_secp160r1: {
+ EC_FP_P = EC160_FP_P;
+ EC_A = EC160_FP_A;
+ EC_B = EC160_FP_B;
+ EC_G_X = EC160_FP_G_X;
+ EC_G_Y = EC160_FP_G_Y;
+ EC_R = EC160_FP_R;
+ EC_K = EC160_FP_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ case CURVE_secp192r1: {
+ EC_FP_P = EC192_FP_P;
+ EC_A = EC192_FP_A;
+ EC_B = EC192_FP_B;
+ EC_G_X = EC192_FP_G_X;
+ EC_G_Y = EC192_FP_G_Y;
+ EC_R = EC192_FP_R;
+ EC_K = EC192_FP_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ case CURVE_secp224r1: {
+ EC_FP_P = EC224_FP_P;
+ EC_A = EC224_FP_A;
+ EC_B = EC224_FP_B;
+ EC_G_X = EC224_FP_G_X;
+ EC_G_Y = EC224_FP_G_Y;
+ EC_R = EC224_FP_R;
+ EC_K = EC224_FP_K;
+ EC_S = null;
+ break;
+ }
+ case CURVE_secp256r1: {
+ EC_FP_P = EC256_FP_P;
+ EC_A = EC256_FP_A;
+ EC_B = EC256_FP_B;
+ EC_G_X = EC256_FP_G_X;
+ EC_G_Y = EC256_FP_G_Y;
+ EC_R = EC256_FP_R;
+ EC_K = EC256_FP_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ case CURVE_secp384r1: {
+ EC_FP_P = EC384_FP_P;
+ EC_A = EC384_FP_A;
+ EC_B = EC384_FP_B;
+ EC_G_X = EC384_FP_G_X;
+ EC_G_Y = EC384_FP_G_Y;
+ EC_R = EC384_FP_R;
+ EC_K = EC384_FP_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ case CURVE_secp521r1: {
+ EC_FP_P = EC521_FP_P;
+ EC_A = EC521_FP_A;
+ EC_B = EC521_FP_B;
+ EC_G_X = EC521_FP_G_X;
+ EC_G_Y = EC521_FP_G_Y;
+ EC_R = EC521_FP_R;
+ EC_K = EC521_FP_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ case CURVE_sect163r1: {
+ EC_F2M_F2M = EC163_F2M_F;
+ EC_A = EC163_F2M_A;
+ EC_B = EC163_F2M_B;
+ EC_G_X = EC163_F2M_G_X;
+ EC_G_Y = EC163_F2M_G_Y;
+ EC_R = EC163_F2M_R;
+ EC_K = EC163_F2M_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ case CURVE_sect233r1: {
+ EC_F2M_F2M = EC233_F2M_F;
+ EC_A = EC233_F2M_A;
+ EC_B = EC233_F2M_B;
+ EC_G_X = EC233_F2M_G_X;
+ EC_G_Y = EC233_F2M_G_Y;
+ EC_R = EC233_F2M_R;
+ EC_K = EC233_F2M_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ case CURVE_sect283r1: {
+ EC_F2M_F2M = EC283_F2M_F;
+ EC_A = EC283_F2M_A;
+ EC_B = EC283_F2M_B;
+ EC_G_X = EC283_F2M_G_X;
+ EC_G_Y = EC283_F2M_G_Y;
+ EC_R = EC283_F2M_R;
+ EC_K = EC283_F2M_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ case CURVE_sect409r1: {
+ EC_F2M_F2M = EC409_F2M_F;
+ EC_A = EC409_F2M_A;
+ EC_B = EC409_F2M_B;
+ EC_G_X = EC409_F2M_G_X;
+ EC_G_Y = EC409_F2M_G_Y;
+ EC_R = EC409_F2M_R;
+ EC_K = EC409_F2M_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ case CURVE_sect571r1: {
+ EC_F2M_F2M = EC571_F2M_F;
+ EC_A = EC571_F2M_A;
+ EC_B = EC571_F2M_B;
+ EC_G_X = EC571_F2M_G_X;
+ EC_G_Y = EC571_F2M_G_Y;
+ EC_R = EC571_F2M_R;
+ EC_K = EC571_F2M_K;
+ EC_W_X = null;
+ EC_W_Y = null;
+ EC_S = null;
+ break;
+ }
+ default:
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
+ short length = 0;
+ switch (param) {
+ case PARAMETER_FP:
+ if (alg == KeyPair.ALG_EC_FP) {
+ length = Util.arrayCopyNonAtomic(EC_FP_P, (short) 0, outputBuffer, outputOffset, (short) EC_FP_P.length);
+ }
+ break;
+ case PARAMETER_F2M:
+ if (alg == KeyPair.ALG_EC_F2M) {
+ length = Util.arrayCopyNonAtomic(EC_F2M_F2M, (short) 0, outputBuffer, outputOffset, (short) EC_F2M_F2M.length);
+ }
+ break;
+ case PARAMETER_A:
+ length = Util.arrayCopyNonAtomic(EC_A, (short) 0, outputBuffer, outputOffset, (short) EC_A.length);
+ break;
+ case PARAMETER_B:
+ length = Util.arrayCopyNonAtomic(EC_B, (short) 0, outputBuffer, outputOffset, (short) EC_B.length);
+ break;
+ case PARAMETER_G:
+ length = toX962(outputBuffer, outputOffset, EC_G_X, (short) 0, (short) EC_G_X.length, EC_G_Y, (short) 0, (short) EC_G_Y.length);
+ break;
+ case PARAMETER_R:
+ length = Util.arrayCopyNonAtomic(EC_R, (short) 0, outputBuffer, outputOffset, (short) EC_R.length);
+ break;
+ case PARAMETER_K:
+ length = 2;
+ Util.setShort(outputBuffer, outputOffset, EC_K);
+ break;
+ case PARAMETER_W:
+ if (EC_W_X == null || EC_W_Y == null) {
+ return 0;
+ }
+ length = toX962(outputBuffer, outputOffset, EC_W_X, (short) 0, (short) EC_W_X.length, EC_W_Y, (short) 0, (short) EC_W_Y.length);
+ break;
+ case PARAMETER_S:
+ if (EC_S == null) {
+ return 0;
+ }
+ length = Util.arrayCopyNonAtomic(EC_S, (short) 0, outputBuffer, outputOffset, (short) EC_S.length);
+ break;
+ default:
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ }
+ return length;
+ }
+
+ public static short getCorruptCurveParameter(byte curve, short param, byte[] outputBuffer, short outputOffset, byte corruptionType) {
+ short length = getCurveParameter(curve, param, outputBuffer, outputOffset);
+ if (length <= 0) {
+ return length;
+ }
+ corruptParameter(corruptionType, outputBuffer, outputOffset, length);
+ return length;
+ }
+
+ public static void corruptParameter(byte corruption, byte[] buffer, short offset, short length) {
+ switch (corruption) {
+ case CORRUPTION_NONE:
+ break;
+ case CORRUPTION_FIXED:
+ if (length >= 1) {
+ buffer[offset] = (byte) 0xcc;
+ buffer[(short) (offset + length - 1)] = (byte) 0xcc;
+ }
+ break;
+ case CORRUPTION_FULLRANDOM:
+ randomData.generateData(buffer, offset, length);
+ break;
+ case CORRUPTION_ONEBYTERANDOM:
+ short first = Util.getShort(buffer, (short) 0); // save first two bytes
+
+ randomData.generateData(buffer, (short) 0, (short) 2); // generate position
+ short rngPos = Util.getShort(buffer, (short) 0); // save generated position
+
+ Util.setShort(buffer, (short) 0, first); // restore first two bytes
+
+ if (rngPos < 0) { // make positive
+ rngPos = (short) -rngPos;
+ }
+ rngPos %= length; // make < param length
+
+ byte original = buffer[rngPos];
+ do {
+ randomData.generateData(buffer, rngPos, (short) 1);
+ } while (original == buffer[rngPos]);
+ break;
+ case CORRUPTION_ZERO:
+ Util.arrayFillNonAtomic(buffer, offset, length, (byte) 0);
+ break;
+ case CORRUPTION_ONE:
+ Util.arrayFillNonAtomic(buffer, offset, length, (byte) 1);
+ break;
+ default:
+ ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
+ /* //TODO implement CORRUPT_B_LASTBYTEINCREMENT somehow
+ case CORRUPT_B_LASTBYTEINCREMENT:
+ m_ramArray2[(short) (m_lenB - 1)] += 1;
+ // Make sure its not the valid byte again
+ if (m_ramArray[(short) (m_lenB - 1)] == m_ramArray2[(short) (m_lenB - 1)]) {
+ m_ramArray2[(short) (m_lenB - 1)] += 1; // if yes, increment once more
+ }
+ break;
+ }
+ */
+ }
+ }
+
+ public static byte getCurveType(byte curve) {
+ return curve <= FP_CURVES ? KeyPair.ALG_EC_FP : KeyPair.ALG_EC_F2M;
+ }
+
+ public static short toX962(byte[] outputBuffer, short outputOffset, byte[] xBuffer, short xOffset, short xLength, byte[] yBuffer, short yOffset, short yLength) {
+ short size = 1;
+ size += xLength;
+ size += yLength;
+
+ short offset = outputOffset;
+ outputBuffer[offset] = 0x04;
+ offset += 1;
+
+ offset = Util.arrayCopyNonAtomic(xBuffer, xOffset, outputBuffer, offset, xLength);
+ Util.arrayCopyNonAtomic(yBuffer, yOffset, outputBuffer, offset, yLength);
+ return size;
+ }
+
+}
diff --git a/src/cz/crcs/ectester/data/ecsp128.csv b/src/cz/crcs/ectester/data/ecsp128.csv
new file mode 100644
index 0000000..29cfe3b
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp128.csv
@@ -0,0 +1,7 @@
+cfba21fd0483b1f300fa2506a5a566ef,
+36d9a5acac27a008e36cbe3e9f103fde,
+a67cf5fa09fb1db902068c87046ae21e,
+47d78391a4b9fff6a0db1292f9cd0e6a,
+9aed9c92f8bb3dbd42402165a270bd6f,
+cfba21fd0483b1f333d61a5af6ada2c7,
+1
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp128_pub.csv b/src/cz/crcs/ectester/data/ecsp128_pub.csv
new file mode 100644
index 0000000..ee1f34d
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp128_pub.csv
@@ -0,0 +1,2 @@
+63901e122761d9c16565b2f38e991f71,
+b9d99fbc3154a96ca23ecff770cbbe4f
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp160.csv b/src/cz/crcs/ectester/data/ecsp160.csv
new file mode 100644
index 0000000..49824d9
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp160.csv
@@ -0,0 +1,7 @@
+dc13490ff9857b111f44c0500770a6457e683223,
+a3ecd7d51e79d72d2700184c795aa8a6b8e66573,
+8ac43592905f995cb13f3694317bf470adafb645,
+5f8e88afc117c722859fe8e55647bca69ba82150,
+93e6dcaee271e9f2838c98b7d06eccc5d7c800e5,
+dc13490ff9857b111f446ef4a6d1e1715f6a6dff,
+1
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp160_pub.csv b/src/cz/crcs/ectester/data/ecsp160_pub.csv
new file mode 100644
index 0000000..e2e164e
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp160_pub.csv
@@ -0,0 +1,2 @@
+59c9c3c8aef29f1c1c500cafb4726da6086e6eb0,
+d695a76005eddb26afd40ee20904778bb3497bb1
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp192.csv b/src/cz/crcs/ectester/data/ecsp192.csv
new file mode 100644
index 0000000..ccb5537
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp192.csv
@@ -0,0 +1,7 @@
+ce714cc3a15ce7e5dab068c9a1f8be00aad480abccaeefc3,
+597c781f64c33eb8ef919c415911518ea323be88b9437caf,
+f81585a1b18f233d70add7ee1342d2035c386a92e3ab8320,
+150ff0a40deac6462b5987418617fdeeb6bfd76d4d60a067,
+843d577371c5dce122c2ff206b2f42fa0b842b49bdaf990f,
+ce714cc3a15ce7e5dab068c9a30bc92915bd8662ae882887,
+1
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp192_pub.csv b/src/cz/crcs/ectester/data/ecsp192_pub.csv
new file mode 100644
index 0000000..ec7f822
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp192_pub.csv
@@ -0,0 +1,2 @@
+17047f91dbe33032c9d09bd29ceadd8a09ccc32ac6309541,
+6a726de54fbd59cfc352e838b337fa005a97180816135e6a
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp224.csv b/src/cz/crcs/ectester/data/ecsp224.csv
new file mode 100644
index 0000000..894e669
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp224.csv
@@ -0,0 +1,7 @@
+eed4c3d98f1c9b9518f116263db770366877d12df6a9cf08b96dd4bb,
+8d4dddb0317d6a6bf9a4dbbed3a43fa21f79869c5ab9729d239e9282,
+46873614be3dffc9218082322210c0616140286f2d160503c1a9250d,
+961bbb1fc9955a71c91a50aedcd2f14fccb660af992b0030b9c90b36,
+1c00f6d0bd405dd7d3016fb8c0c75e4ecec70fe61237f6d24008a5fd,
+eed4c3d98f1c9b9518f116263db821c36a06adae17162ad3162f68c3,
+1
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp224_pub.csv b/src/cz/crcs/ectester/data/ecsp224_pub.csv
new file mode 100644
index 0000000..0999b99
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp224_pub.csv
@@ -0,0 +1,2 @@
+cfd92aea0f79190c48ca703eb8a9baa7099a23bb39578261fe4d0f04,
+257a3d98de44bd25404977a4ac7fc56d3d4e827f085b7cf5247524c4
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp256.csv b/src/cz/crcs/ectester/data/ecsp256.csv
new file mode 100644
index 0000000..17387a6
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp256.csv
@@ -0,0 +1,7 @@
+c9a803b1eaf849f1c02cfd1dbfac68623985c88b37103b338ae11d2597ee8445,
+4841c5775a24a884ca36ec362b44645a2f60b25d002c4fc1d9f139870fe0cc71,
+1b097456751f3534190dae568f80a2c6ff55dddfe072a7dc6467a4b6476b6880,
+a1fd34a27afb1340b8e4a7db2a5ec5a1432c6dc8555af9f78fca2cf740cab2b7,
+98419c698cab6c7dbb53eb2751417b52ccded4680c5e09543f93c7886c3a173e,
+c9a803b1eaf849f1c02cfd1dbfac6863128c5b1fc5acd5b5e0fc0a7311fb5b1d,
+1
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp256_pub.csv b/src/cz/crcs/ectester/data/ecsp256_pub.csv
new file mode 100644
index 0000000..a8f0492
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp256_pub.csv
@@ -0,0 +1,2 @@
+75fce70968862d53e29548aad70582514e960d8128bd3c5f8c4dbe2cf8dad653,
+55aa4b7d3882fb0a83bd00c9c3bae17f1024d64aec67e1db38ef671e6350beae
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp384.csv b/src/cz/crcs/ectester/data/ecsp384.csv
new file mode 100644
index 0000000..9acae3f
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp384.csv
@@ -0,0 +1,7 @@
+d0df6c96cff7081be80d22b005758a2e2f046e15fe020ef886e21b492ac57257a923144bcad989ab6341bd3b700f914b,
+45c64503be019afd3462b361ad2b2a3bca0aeccc5494a624fb632455e62b4f0c98f944fa97c37811da039823cd77c906,
+d85583f7f11ad23ec75ed5a414153a06d6640936b8103f5df691fa95cf2afa78f3ea5addc225b144964048c9f7592ae4,
+2b1341d12dff4f9cf9427c4752962b4c2bdc8fbcd80652516c421cc523212a01ea63c79d6e9a9c84933e353e212416ec,
+ce416c6e75fa9fd205ed48fc4e3099cbb1d6ed031b7ddbff1d634eb97a83d9b780cfd4dedfdd2c7604d143196c08d933,
+d0df6c96cff7081be80d22b005758a2e2f046e15fe020ef7664ed51d7701c86bf2a1e9f3002c26fe002314c3c92f1ca9,
+1
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp384_pub.csv b/src/cz/crcs/ectester/data/ecsp384_pub.csv
new file mode 100644
index 0000000..4eb4a96
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp384_pub.csv
@@ -0,0 +1,2 @@
+a4bd575bf20300b0cf8a2f41dd5a03e908966a4229a5f22f5c190d3641ac2d32b7b24a63482cbbcd0c2257f834834ef1,
+38d51c8f9e90592f567e81d0e4855e79731b5797857a4c7dc270653bc9f0c31e84693007b09cebf710d5ae3237303949
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp521.csv b/src/cz/crcs/ectester/data/ecsp521.csv
new file mode 100644
index 0000000..1d36bd7
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp521.csv
@@ -0,0 +1,7 @@
+01d3df430924956e210a605b4dbf4a2e909d7a801658978c88ffd68dcc817f5cc79cf188d9ee82d1a51c44cbd31e9cc5b816d76d5b1312b005f7b68919e275dac99f,
+00401639f36f2ee45fc164ea3e1f14f4803fd7a77ffdfb392c3f8fe95d1aea331467f4618d59aeee49d5d7c70caf320f7dd1ac166114f562413449991d3aa1a2c49e,
+004a26a8c47fce204ba953015fa86708c0de720f27523988b097e774168c15f7a215aaf18a5f1b9579ab3db935d45be14c9a87b71170396909b14d06f7a09975b3a6,
+01c880ae0a355a52791fc9600fd8b35726e9d799101489161c8f90a9c6631d09b3cb347584837d9deb8566a9c5846aded0d01eb947b4affd34e8ea7dbe733cbedafa,
+00050f12672f163f19d5d493eb82ef777b0213dd4e0cf75a9b99724fbdb54b0cc4e037bf86a48bac28467bdd936c314ce13f6ec7ec69ea09ae4f5444df4b2a117a66,
+01d3df430924956e210a605b4dbf4a2e909d7a801658978c88ffd68dcc817f5cc7ba0838717c1947f93cfdd3ed87ec2c2df181c7ada553346ec1495732a1e7ffe9b3,
+1
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/ecsp521_pub.csv b/src/cz/crcs/ectester/data/ecsp521_pub.csv
new file mode 100644
index 0000000..0e3f320
--- /dev/null
+++ b/src/cz/crcs/ectester/data/ecsp521_pub.csv
@@ -0,0 +1,2 @@
+002844df0f31f46a40e6c7006cde99155bd5d18d0e4150178a8e307d6aec08fd02d466c03c49b49c2654b7c9a32d88ca014016a7eddd44217be915505d228efb9389,
+0105921e2172c3050ba4c9d2e744fc5b7b5e8451751e6780c6de88229497be7d23550beefa0cb7fafebb4dd9fad1244c6733befe5a97710f0dc56dc08d9d9df9d846
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/secp192k1.csv b/src/cz/crcs/ectester/data/secp192k1.csv
new file mode 100644
index 0000000..c408b5e
--- /dev/null
+++ b/src/cz/crcs/ectester/data/secp192k1.csv
@@ -0,0 +1,7 @@
+FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFEE37,
+00000000 00000000 00000000 00000000 00000000 00000000,
+00000000 00000000 00000000 00000000 00000000 00000003,
+DB4FF10E C057E9AE 26B07D02 80B7F434 1DA5D1B1 EAE06C7D,
+9B2F2F6D 9C5628A7 844163D0 15BE8634 4082AA88 D95E2F9D,
+FFFFFFFF FFFFFFFF FFFFFFFE 26F2FC17 0F69466A 74DEFD8D,
+1
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/secp192r1.csv b/src/cz/crcs/ectester/data/secp192r1.csv
new file mode 100644
index 0000000..29305b0
--- /dev/null
+++ b/src/cz/crcs/ectester/data/secp192r1.csv
@@ -0,0 +1,7 @@
+FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFFFF FFFFFFFF,
+FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFFFF FFFFFFFC,
+64210519 E59C80E7 0FA7E9AB 72243049 FEB8DEEC C146B9B1,
+188DA80E B03090F6 7CBF20EB 43A18800 F4FF0AFD 82FF1012,
+07192B95 FFC8DA78 631011ED 6B24CDD5 73F977A1 1E794811,
+FFFFFFFF FFFFFFFF FFFFFFFF 99DEF836 146BC9B1 B4D22831,
+1
\ No newline at end of file
diff --git a/src/cz/crcs/ectester/data/secp224r1.csv b/src/cz/crcs/ectester/data/secp224r1.csv
new file mode 100644
index 0000000..f8f592e
--- /dev/null
+++ b/src/cz/crcs/ectester/data/secp224r1.csv
@@ -0,0 +1,7 @@
+FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 00000000 00000000 00000001,
+FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFFFF FFFFFFFF FFFFFFFE,
+B4050A85 0C04B3AB F5413256 5044B0B7 D7BFD8BA 270B3943 2355FFB4,
+B70E0CBD 6BB4BF7F 321390B9 4A03C1D3 56C21122 343280D6 115C1D21,
+BD376388 B5F723FB 4C22DFE6 CD4375A0 5A074764 44D58199 85007E34,
+FFFFFFFF FFFFFFFF FFFFFFFF FFFF16A2 E0B8F03E 13DD2945 5C5C2A3D,
+1
diff --git a/src/cz/crcs/ectester/data/secp256k1.csv b/src/cz/crcs/ectester/data/secp256k1.csv
new file mode 100644
index 0000000..19eeef7
--- /dev/null
+++ b/src/cz/crcs/ectester/data/secp256k1.csv
@@ -0,0 +1,7 @@
+FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000007,
+79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798,
+483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8,
+FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141,
+1
diff --git a/src/cz/crcs/ectester/data/secp256r1.csv b/src/cz/crcs/ectester/data/secp256r1.csv
new file mode 100644
index 0000000..060f5aa
--- /dev/null
+++ b/src/cz/crcs/ectester/data/secp256r1.csv
@@ -0,0 +1,7 @@
+FFFFFFFF 00000001 00000000 00000000 00000000 FFFFFFFF FFFFFFFF FFFFFFFF,
+FFFFFFFF 00000001 00000000 00000000 00000000 FFFFFFFF FFFFFFFF FFFFFFFC,
+5AC635D8 AA3A93E7 B3EBBD55 769886BC 651D06B0 CC53B0F6 3BCE3C3E 27D2604B,
+6B17D1F2 E12C4247 F8BCE6E5 63A440F2 77037D81 2DEB33A0 F4A13945 D898C296,
+4FE342E2 FE1A7F9B 8EE7EB4A 7C0F9E16 2BCE3357 6B315ECE CBB64068 37BF51F5,
+FFFFFFFF 00000000 FFFFFFFF FFFFFFFF BCE6FAAD A7179E84 F3B9CAC2 FC632551,
+1
diff --git a/src/cz/crcs/ectester/data/secp384r1.csv b/src/cz/crcs/ectester/data/secp384r1.csv
new file mode 100644
index 0000000..08472ae
--- /dev/null
+++ b/src/cz/crcs/ectester/data/secp384r1.csv
@@ -0,0 +1,7 @@
+FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFFFF 00000000 00000000 FFFFFFFF,
+FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFFFF 00000000 00000000 FFFFFFFC,
+B3312FA7 E23EE7E4 988E056B E3F82D19 181D9C6E FE814112 0314088F 5013875A C656398D 8A2ED19D 2A85C8ED D3EC2AEF,
+AA87CA22 BE8B0537 8EB1C71E F320AD74 6E1D3B62 8BA79B98 59F741E0 82542A38 5502F25D BF55296C 3A545E38 72760AB7,
+3617DE4A 96262C6F 5D9E98BF 9292DC29 F8F41DBD 289A147C E9DA3113 B5F0B8C0 0A60B1CE 1D7E819D 7A431D7C 90EA0E5F,
+FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF C7634D81 F4372DDF 581A0DB2 48B0A77A ECEC196A CCC52973,
+1
diff --git a/src/cz/crcs/ectester/data/secp521r1.csv b/src/cz/crcs/ectester/data/secp521r1.csv
new file mode 100644
index 0000000..9e0f5d3
--- /dev/null
+++ b/src/cz/crcs/ectester/data/secp521r1.csv
@@ -0,0 +1,7 @@
+01FF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF,
+01FF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFC,
+0051 953EB961 8E1C9A1F 929A21A0 B68540EE A2DA725B 99B315F3 B8B48991 8EF109E1 56193951 EC7E937B 1652C0BD 3BB1BF07 3573DF88 3D2C34F1 EF451FD4 6B503F00,
+00C6 858E06B7 0404E9CD 9E3ECB66 2395B442 9C648139 053FB521 F828AF60 6B4D3DBA A14B5E77 EFE75928 FE1DC127 A2FFA8DE 3348B3C1 856A429B F97E7E31 C2E5BD66,
+0118 39296A78 9A3BC004 5C8A5FB4 2C7D1BD9 98F54449 579B4468 17AFBD17 273E662C 97EE7299 5EF42640 C550B901 3FAD0761 353C7086 A272C240 88BE9476 9FD16650,
+01FF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFA 51868783 BF2F966B 7FCC0148 F709A5D0 3BB5C9B8 899C47AE BB6FB71E 91386409,
+1
diff --git a/src/cz/crcs/ectester/data/sect163k1.csv b/src/cz/crcs/ectester/data/sect163k1.csv
new file mode 100644
index 0000000..6e5142e
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect163k1.csv
@@ -0,0 +1,9 @@
+0007,
+0006,
+0003,
+00 00000000 00000000 00000000 00000000 00000001,
+00 00000000 00000000 00000000 00000000 00000001,
+02 FE13C053 7BBC11AC AA07D793 DE4E6D5E 5C94EEE8,
+02 89070FB0 5D38FF58 321F2E80 0536D538 CCDAA3D9,
+04 00000000 00000000 00020108 A2E0CC0D 99F8A5EF,
+2
diff --git a/src/cz/crcs/ectester/data/sect163r1.csv b/src/cz/crcs/ectester/data/sect163r1.csv
new file mode 100644
index 0000000..879f79b
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect163r1.csv
@@ -0,0 +1,9 @@
+0007,
+0006,
+0003,
+07 B6882CAA EFA84F95 54FF8428 BD88E246 D2782AE2,
+07 13612DCD DCB40AAB 946BDA29 CA91F73A F958AFD9,
+03 69979697 AB438977 89566789 567F787A 7876A654,
+00 435EDB42 EFAFB298 9D51FEFC E3C80988 F41FF883,
+03 FFFFFFFF FFFFFFFF FFFF48AA B689C29C A710279B,
+2
diff --git a/src/cz/crcs/ectester/data/sect163r2.csv b/src/cz/crcs/ectester/data/sect163r2.csv
new file mode 100644
index 0000000..eb7d453
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect163r2.csv
@@ -0,0 +1,9 @@
+0007,
+0006,
+0003,
+00 00000000 00000000 00000000 00000000 00000001,
+02 0A601907 B8C953CA 1481EB10 512F7874 4A3205FD,
+03 F0EBA162 86A2D57E A0991168 D4994637 E8343E36,
+00 D51FBC6C 71A0094F A2CDD545 B11C5C0C 797324F1,
+04 00000000 00000000 000292FE 77E70C12 A4234C33,
+2
diff --git a/src/cz/crcs/ectester/data/sect233k1.csv b/src/cz/crcs/ectester/data/sect233k1.csv
new file mode 100644
index 0000000..b85fef0
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect233k1.csv
@@ -0,0 +1,7 @@
+004A,
+0000 00000000 00000000 00000000 00000000 00000000 00000000 00000000,
+0000 00000000 00000000 00000000 00000000 00000000 00000000 00000001,
+0172 32BA853A 7E731AF1 29F22FF4 149563A4 19C26BF5 0A4C9D6E EFAD6126,
+01DB 537DECE8 19B7F70F 555A67C4 27A8CD9B F18AEB9B 56E0C110 56FAE6A3,
+80 00000000 00000000 00000000 00069D5B B915BCD4 6EFB1AD5 F173ABDF,
+4
diff --git a/src/cz/crcs/ectester/data/sect233r1.csv b/src/cz/crcs/ectester/data/sect233r1.csv
new file mode 100644
index 0000000..d0c6f14
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect233r1.csv
@@ -0,0 +1,7 @@
+004A,
+0000 00000000 00000000 00000000 00000000 00000000 00000000 00000001,
+0066 647EDE6C 332C7F8C 0923BB58 213B333B 20E9CE42 81FE115F 7D8F90AD,
+00FA C9DFCBAC 8313BB21 39F1BB75 5FEF65BC 391F8B36 F8F8EB73 71FD558B,
+0100 6A08A419 03350678 E58528BE BF8A0BEF F867A7CA 36716F7E 01F81052,
+0100 00000000 00000000 00000000 0013E974 E72F8A69 22031D26 03CFE0D7,
+2
diff --git a/src/cz/crcs/ectester/data/sect239k1.csv b/src/cz/crcs/ectester/data/sect239k1.csv
new file mode 100644
index 0000000..d01bb08
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect239k1.csv
@@ -0,0 +1,7 @@
+009E,
+0000 00000000 00000000 00000000 00000000 00000000 00000000 00000000,
+0000 00000000 00000000 00000000 00000000 00000000 00000000 00000001,
+29A0 B6A887A9 83E97309 88A68727 A8B2D126 C44CC2CC 7B2A6555 193035DC,
+7631 0804F12E 549BDB01 1C103089 E73510AC B275FC31 2A5DC6B7 6553F0CA,
+2000 00000000 00000000 00000000 005A79FE C67CB6E9 1F1C1DA8 00E478A5,
+4
diff --git a/src/cz/crcs/ectester/data/sect283k1.csv b/src/cz/crcs/ectester/data/sect283k1.csv
new file mode 100644
index 0000000..cc62698
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect283k1.csv
@@ -0,0 +1,9 @@
+000C,
+0007,
+0005,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001,
+0503213F 78CA4488 3F1A3B81 62F188E5 53CD265F 23C1567A 16876913 B0C2AC24 58492836,
+01CCDA38 0F1C9E31 8D90F95D 07E5426F E87E45C0 E8184698 E4596236 4E341161 77DD2259,
+01FFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFE9AE 2ED07577 265DFF7F 94451E06 1E163C61,
+4
diff --git a/src/cz/crcs/ectester/data/sect283r1.csv b/src/cz/crcs/ectester/data/sect283r1.csv
new file mode 100644
index 0000000..27e2ff2
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect283r1.csv
@@ -0,0 +1,9 @@
+000C,
+0007,
+0005,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001,
+027B680A C8B8596D A5A4AF8A 19A0303F CA97FD76 45309FA2 A581485A F6263E31 3B79A2F5,
+05F93925 8DB7DD90 E1934F8C 70B0DFEC 2EED25B8 557EAC9C 80E2E198 F8CDBECD 86B12053,
+03676854 FE24141C B98FE6D4 B20D02B4 516FF702 350EDDB0 826779C8 13F0DF45 BE8112F4,
+03FFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFEF90 399660FC 938A9016 5B042A7C EFADB307,
+2
diff --git a/src/cz/crcs/ectester/data/sect409k1.csv b/src/cz/crcs/ectester/data/sect409k1.csv
new file mode 100644
index 0000000..aeb2e76
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect409k1.csv
@@ -0,0 +1,7 @@
+0057,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001,
+0060F05F 658F49C1 AD3AB189 0F718421 0EFD0987 E307C84C 27ACCFB8 F9F67CC2 C460189E B5AAAA62 EE222EB1 B35540CF E9023746,
+01E36905 0B7C4E42 ACBA1DAC BF04299C 3460782F 918EA427 E6325165 E9EA10E3 DA5F6C42 E9C55215 AA9CA27A 5863EC48 D8E0286B,
+7FFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFE5F 83B2D4EA 20400EC4 557D5ED3 E3E7CA5B 4B5C83B8 E01E5FCF,
+4
diff --git a/src/cz/crcs/ectester/data/sect409r1.csv b/src/cz/crcs/ectester/data/sect409r1.csv
new file mode 100644
index 0000000..5ac19dc
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect409r1.csv
@@ -0,0 +1,7 @@
+0057,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001,
+0021A5C2 C8EE9FEB 5C4B9A75 3B7B476B 7FD6422E F1F3DD67 4761FA99 D6AC27C8 A9A197B2 72822F6C D57A55AA 4F50AE31 7B13545F,
+015D4860 D088DDB3 496B0C60 64756260 441CDE4A F1771D4D B01FFE5B 34E59703 DC255A86 8A118051 5603AEAB 60794E54 BB7996A7,
+0061B1CF AB6BE5F3 2BBFA783 24ED106A 7636B9C5 A7BD198D 0158AA4F 5488D08F 38514F1F DF4B4F40 D2181B36 81C364BA 0273C706,
+01000000 00000000 00000000 00000000 00000000 00000000 000001E2 AAD6A612 F33307BE 5FA47C3C 9E052F83 8164CD37 D9A21173,
+2
diff --git a/src/cz/crcs/ectester/data/sect571k1.csv b/src/cz/crcs/ectester/data/sect571k1.csv
new file mode 100644
index 0000000..7d5fdf4
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect571k1.csv
@@ -0,0 +1,9 @@
+000A,
+0005,
+0002,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001,
+026EB7A8 59923FBC 82189631 F8103FE4 AC9CA297 0012D5D4 60248048 01841CA4 43709584 93B205E6 47DA304D B4CEB08C BBD1BA39 494776FB 988B4717 4DCA88C7 E2945283 A01C8972,
+0349DC80 7F4FBF37 4F4AEADE 3BCA9531 4DD58CEC 9F307A54 FFC61EFC 006D8A2C 9D4979C0 AC44AEA7 4FBEBBB9 F772AEDC B620B01A 7BA7AF1B 320430C8 591984F6 01CD4C14 3EF1C7A3,
+02000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 131850E1 F19A63E4 B391A8DB 917F4138 B630D84B E5D63938 1E91DEB4 5CFE778F 637C1001,
+4
diff --git a/src/cz/crcs/ectester/data/sect571r1.csv b/src/cz/crcs/ectester/data/sect571r1.csv
new file mode 100644
index 0000000..850f0b9
--- /dev/null
+++ b/src/cz/crcs/ectester/data/sect571r1.csv
@@ -0,0 +1,9 @@
+000A,
+0005,
+0002,
+00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001,
+02F40E7E 2221F295 DE297117 B7F3D62F 5C6A97FF CB8CEFF1 CD6BA8CE 4A9A18AD 84FFABBD 8EFA5933 2BE7AD67 56A66E29 4AFD185A 78FF12AA 520E4DE7 39BACA0C 7FFEFF7F 2955727A,
+0303001D 34B85629 6C16C0D4 0D3CD775 0A93D1D2 955FA80A A5F40FC8 DB7B2ABD BDE53950 F4C0D293 CDD711A3 5B67FB14 99AE6003 8614F139 4ABFA3B4 C850D927 E1E7769C 8EEC2D19,
+037BF273 42DA639B 6DCCFFFE B73D69D7 8C6C27A6 009CBBCA 1980F853 3921E8A6 84423E43 BAB08A57 6291AF8F 461BB2A8 B3531D2F 0485C19B 16E2F151 6E23DD3C 1A4827AF 1B8AC15B,
+03FFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF E661CE18 FF559873 08059B18 6823851E C7DD9CA1 161DE93D 5174D66E 8382E9BB 2FE84E47,
+2
diff --git a/src/cz/crcs/ectester/reader/CardMngr.java b/src/cz/crcs/ectester/reader/CardMngr.java
new file mode 100644
index 0000000..d7a5c5f
--- /dev/null
+++ b/src/cz/crcs/ectester/reader/CardMngr.java
@@ -0,0 +1,289 @@
+package cz.crcs.ectester.reader;
+
+import com.licel.jcardsim.io.CAD;
+import com.licel.jcardsim.io.JavaxSmartCardInterface;
+import java.util.List;
+import java.util.Scanner;
+import javacard.framework.AID;
+
+import javax.smartcardio.*;
+
+/**
+ * @author Petr Svenda petr@svenda.com
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class CardMngr {
+ private CardTerminal m_terminal = null;
+ private CardChannel m_channel = null;
+ private Card m_card = null;
+
+ // Simulator related attributes
+ private CAD m_cad = null;
+ private JavaxSmartCardInterface m_simulator = null;
+
+ private boolean simulate = false;
+
+ private final byte selectCM[] = {
+ (byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, (byte) 0x07, (byte) 0xa0, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x18, (byte) 0x43, (byte) 0x4d};
+
+ public static final byte OFFSET_CLA = 0x00;
+ public static final byte OFFSET_INS = 0x01;
+ public static final byte OFFSET_P1 = 0x02;
+ public static final byte OFFSET_P2 = 0x03;
+ public static final byte OFFSET_LC = 0x04;
+ public static final byte OFFSET_DATA = 0x05;
+ public static final byte HEADER_LENGTH = 0x05;
+
+ public static final short DATA_RECORD_LENGTH = (short) 0x80; // 128B per record
+ public static final short NUMBER_OF_RECORDS = (short) 0x0a; // 10 records
+
+ public CardMngr() {
+ this(false);
+ }
+
+ public CardMngr(boolean simulate) {
+ this.simulate = simulate;
+ }
+
+ public boolean connectToCard() throws CardException {
+ if (simulate)
+ return true;
+
+ // TRY ALL READERS, FIND FIRST SELECTABLE
+ List