Skip to content

Commit

Permalink
Merge pull request #199 from crocs-muni/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
petrs authored Jul 22, 2019
2 parents 900be98 + df6184d commit 86ea4d0
Show file tree
Hide file tree
Showing 39 changed files with 1,711 additions and 469 deletions.
17 changes: 12 additions & 5 deletions AlgTest_JClient/src/algtestjclient/AlgTestJClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ public class AlgTestJClient {
/* Arguments for choosing which AlgTest version to run. */
public static final String ALGTEST_SINGLEPERAPDU = "AT_SINGLEPERAPDU"; // for 'New' AlgTest
public static final String ALGTEST_PERFORMANCE = "AT_PERFORMANCE"; // for performance testing
public static final String ALGTEST_USE_SIMULATOR = "JCARDSIM"; // use simulator instead of real card

public static final String ALGTEST_USE_SIMULATOR = "JCARDSIM"; // use simulator instead of real card
public static final String ALGTEST_USE_CUSTOM_AID = "AID="; // use custom AID for applet (if default one is changed for some reason)

/**
* Version 1.7.9 (22.07.2019)
* - no changes, updating version with card applet
*/
public final static String ALGTEST_JCLIENT_VERSION_1_7_9 = "1.7.9";
/**
* Version 1.7.8 (18.05.2019)
* Version 1.7.8 (18.05.2019)
* - no changes, updating version with card applet
*/
public final static String ALGTEST_JCLIENT_VERSION_1_7_8 = "1.7.8";
//public final static String ALGTEST_JCLIENT_VERSION_1_7_8 = "1.7.8";
/**
* Version 1.7.7 (17.04.2019)
* - removed testing for high-power mode for SIM cards as some cards stop responding till reset
Expand Down Expand Up @@ -149,12 +155,13 @@ public class AlgTestJClient {
/**
* Current version
*/
public final static String ALGTEST_JCLIENT_VERSION = ALGTEST_JCLIENT_VERSION_1_7_8;
public final static String ALGTEST_JCLIENT_VERSION = ALGTEST_JCLIENT_VERSION_1_7_9;

public final static int STAT_OK = 0;

// If required to be run with simulator, run as: >java -cp "AlgTestJClient.jar;jcardsim-3.0.5-SNAPSHOT.jar" -noverify algtestjclient.AlgTestJClient JCARDSIM
static boolean USE_JCARDSIM = false;

/**
* @param args the command line arguments
*/
Expand Down
38 changes: 34 additions & 4 deletions AlgTest_JClient/src/algtestjclient/CardMngr.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,11 @@ public boolean ConnectToCard(CardTerminal targetReader, StringBuilder selectedRe
}
}

if (selectedATR != null) { selectedATR.append(getATR()); }
if (selectedReader != null) { selectedReader.append(getTerminalName()); }
if (usedProtocol != null) { usedProtocol.append(getProtocol()); }

if (cardFound) {
if (selectedATR != null) { selectedATR.append(getATR()); }
if (selectedReader != null) { selectedReader.append(getTerminalName()); }
if (usedProtocol != null) { usedProtocol.append(getProtocol()); }
return true;
}
}
Expand Down Expand Up @@ -690,6 +691,17 @@ public int GetJCSystemInfo(StringBuilder pValue, FileOutputStream pFile) throws
int ramDeselectSize = (temp[7] << 8) + (temp[8] & 0xff);
int maxCommitSize = (temp[9] << 8) + (temp[10] & 0xff);

int apduInBlockSize = -1;
int apduOutBlockSize = -1;
int apduProtocol = -1;
int apduNAD = -1;
if (temp.length > 11) {
apduInBlockSize = (temp[11] << 8) + (temp[12] & 0xff);
apduOutBlockSize = (temp[13] << 8) + (temp[14] & 0xff);
apduProtocol = temp[15];
apduNAD = temp[16];
}



String message;
Expand Down Expand Up @@ -717,8 +729,26 @@ public int GetJCSystemInfo(StringBuilder pValue, FileOutputStream pFile) throws
pFile.write(message.getBytes());
pValue.append(message);

// APDU properties
message = String.format("\r\n%s;%dB;", Utils.GetAlgorithmName(SingleModeTest.JCSYSTEM_STR[7]), apduInBlockSize);
m_SystemOutLogger.println(message);
pFile.write(message.getBytes());
pValue.append(message);
message = String.format("\r\n%s;%dB;", Utils.GetAlgorithmName(SingleModeTest.JCSYSTEM_STR[8]), apduOutBlockSize);
m_SystemOutLogger.println(message);
pFile.write(message.getBytes());
pValue.append(message);
message = String.format("\r\n%s;%dB;", Utils.GetAlgorithmName(SingleModeTest.JCSYSTEM_STR[9]), apduProtocol);
m_SystemOutLogger.println(message);
pFile.write(message.getBytes());
pValue.append(message);
message = String.format("\r\n%s;%dB;", Utils.GetAlgorithmName(SingleModeTest.JCSYSTEM_STR[10]), apduNAD);
m_SystemOutLogger.println(message);
pFile.write(message.getBytes());
pValue.append(message);


message += "\r\n";
message = "\r\n";

pFile.write(message.getBytes());
pValue.append(message);
Expand Down
4 changes: 3 additions & 1 deletion AlgTest_JClient/src/algtestjclient/SingleModeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ public class SingleModeTest {

public static final String JCSYSTEM_STR[] = {"javacard.framework.JCSystem", "JCSystem.getVersion()[Major.Minor]#&le;2.1",
"JCSystem.isObjectDeletionSupported#2.2.0", "JCSystem.MEMORY_TYPE_PERSISTENT#2.2.1", "JCSystem.MEMORY_TYPE_TRANSIENT_RESET#2.2.1",
"JCSystem.MEMORY_TYPE_TRANSIENT_DESELECT#2.2.1", "JCSystem.getMaxCommitCapacity()#2.1"};
"JCSystem.MEMORY_TYPE_TRANSIENT_DESELECT#2.2.1", "JCSystem.getMaxCommitCapacity()#2.1",
"APDU.getInBlockSize()#2.1", "APDU.getOutBlockSize()#2.1", "APDU.getProtocol()#2.1", "APDU.getNAD()#2.1"};

public static final String RAWRSA_1024_STR[] = {"Variable RSA 1024 - support for variable public exponent. If supported, user-defined fast modular exponentiation can be executed on the smart card via cryptographic coprocessor. This is very specific feature and you will probably not need it",
"Allocate RSA 1024 objects", "Set random modulus", "Set random public exponent", "Initialize cipher with public key with random exponent", "Use random public exponent"};
Expand Down Expand Up @@ -268,6 +269,7 @@ public class SingleModeTest {
"000108A000000062020804#3.0.4", // javacardx.framework.string
"000107A0000000620209#2.2.2", // javacardx.apdu
"000108A000000062020901#3.0.5" // javacardx.apdu.util
// org.
};

public static final Map<String, String> PACKAGE_AID_NAMES_STR;
Expand Down
Binary file added AlgTest_JavaCard/AlgTest_v1.7.9_jc222.cap
Binary file not shown.
62 changes: 50 additions & 12 deletions AlgTest_JavaCard/src/AlgTest/AlgPerformanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,33 @@ KeyPair getKeyPairObject(byte typeRSA, short keyPairIndex, short keyLength) {
return tmp;
}

KeyPair getECCKeyPairObject(byte typeECC, short keyLength) {
KeyPair keyPair = null;
// Try two alternatives for EC key construction - new KeyPair() emit exception on some cards
try {
// Make KeyPair object first, then initialize curve
keyPair = new KeyPair(typeECC, keyLength);
EC_Consts.ensureInitializedECCurve(typeECC, keyLength, keyPair, m_ram1);
} catch (Exception e) {
// Make public and private keys first, then initialize curve and finally create KeyPair
ECPrivateKey ecPrivKey;
ECPublicKey ecPubKey;
if (typeECC == KeyPair.ALG_EC_FP) {
ecPrivKey = (ECPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PRIVATE, keyLength, false);
ecPubKey = (ECPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PUBLIC, keyLength, false);
}
else if (typeECC == KeyPair.ALG_EC_F2M) {
ecPrivKey = (ECPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_F2M_PRIVATE, keyLength, false);
ecPubKey = (ECPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_F2M_PUBLIC, keyLength, false);
} else {
return null;
}
EC_Consts.setECKeyParams(ecPubKey, ecPrivKey, typeECC, keyLength, m_ram1);
keyPair = new KeyPair(ecPubKey, ecPrivKey);
}
return keyPair;
}

void prepare_class_Key(APDU apdu) {
byte[] apdubuf = apdu.getBuffer();
m_testSettings.parse(apdu);
Expand Down Expand Up @@ -686,7 +713,7 @@ short prepare_Key(APDU apdu, TestSettings testSet, byte bSetKeyValue) {
if (bSetKeyValue == Consts.TRUE){
m_keyPair1 = getKeyPairObject((byte) m_testSettings.keyClass, (short) 1, m_testSettings.keyLength);
//m_keyPair1 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
//m_keyPair1.genKeyPair();
//m_keyPair1.genKeyPair();
m_key1 = m_keyPair1.getPublic();
m_rsapublic_key = (RSAPublicKey) m_keyPair1.getPublic();
m_keyPair2 = getKeyPairObject((byte) m_testSettings.keyClass, (short) 2, m_testSettings.keyLength);
Expand All @@ -711,16 +738,18 @@ short prepare_Key(APDU apdu, TestSettings testSet, byte bSetKeyValue) {
case JCConsts.KeyBuilder_TYPE_EC_F2M_PRIVATE: // no break
case JCConsts.KeyBuilder_TYPE_EC_FP_PRIVATE:
if (bSetKeyValue == Consts.TRUE) {
m_keyPair1 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
EC_Consts.ensureInitializedECCurve((byte) m_testSettings.keyClass, m_testSettings.keyLength, m_keyPair1, m_ram1);
m_keyPair1 = getECCKeyPairObject((byte) m_testSettings.keyClass, m_testSettings.keyLength);
//m_keyPair1 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
//EC_Consts.ensureInitializedECCurve((byte) m_testSettings.keyClass, m_testSettings.keyLength, m_keyPair1, m_ram1);
m_keyPair1.genKeyPair(); // TODO: use fixed key value to shorten time required for key generation?
m_key1 = m_keyPair1.getPrivate();
m_ecprivate_key = (ECPrivateKey) m_keyPair1.getPrivate();
m_ecpublic_key = (ECPublicKey) m_keyPair1.getPublic();
m_keyInv1 = m_ecpublic_key;

m_keyPair2 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
EC_Consts.ensureInitializedECCurve((byte) m_testSettings.keyClass, m_testSettings.keyLength, m_keyPair2, m_ram1);

m_keyPair2 = getECCKeyPairObject((byte) m_testSettings.keyClass, m_testSettings.keyLength);
//m_keyPair2 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
//EC_Consts.ensureInitializedECCurve((byte) m_testSettings.keyClass, m_testSettings.keyLength, m_keyPair2, m_ram1);
m_keyPair2.genKeyPair(); // TODO: use fixed key value to shorten time required for key generation?
m_key2 = m_keyPair2.getPrivate();
m_ecprivate_key2 = (ECPrivateKey) m_keyPair2.getPrivate();
Expand Down Expand Up @@ -763,15 +792,17 @@ short prepare_Key(APDU apdu, TestSettings testSet, byte bSetKeyValue) {
case JCConsts.KeyBuilder_TYPE_EC_F2M_PUBLIC: // no break
case JCConsts.KeyBuilder_TYPE_EC_FP_PUBLIC:
if (bSetKeyValue == Consts.TRUE){
m_keyPair1 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
EC_Consts.ensureInitializedECCurve((byte) m_testSettings.keyClass, m_testSettings.keyLength, m_keyPair1, m_ram1);
m_keyPair1 = getECCKeyPairObject((byte) m_testSettings.keyClass, m_testSettings.keyLength);
//m_keyPair1 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
//EC_Consts.ensureInitializedECCurve((byte) m_testSettings.keyClass, m_testSettings.keyLength, m_keyPair1, m_ram1);
m_keyPair1.genKeyPair();
m_key1 = m_keyPair1.getPublic();
m_ecpublic_key = (ECPublicKey) m_keyPair1.getPublic();
m_ecprivate_key = (ECPrivateKey) m_keyPair1.getPrivate();
m_keyInv1 = m_ecprivate_key;
m_keyPair2 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
EC_Consts.ensureInitializedECCurve((byte) m_testSettings.keyClass, m_testSettings.keyLength, m_keyPair2, m_ram1);
m_keyPair2 = getECCKeyPairObject((byte) m_testSettings.keyClass, m_testSettings.keyLength);
//m_keyPair2 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
//EC_Consts.ensureInitializedECCurve((byte) m_testSettings.keyClass, m_testSettings.keyLength, m_keyPair2, m_ram1);
m_keyPair2.genKeyPair();
m_key2 = m_keyPair2.getPublic();
m_ecpublic_key2 = (ECPublicKey) m_keyPair2.getPublic();
Expand Down Expand Up @@ -1468,8 +1499,14 @@ void prepare_class_KeyPair(APDU apdu) {
m_testSettings.parse(apdu);

try {
m_keyPair1 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
// Make sure that for EC, we will have initailized curve
if (((byte) m_testSettings.keyClass == KeyPair.ALG_EC_FP) || ((byte) m_testSettings.keyClass == KeyPair.ALG_EC_F2M)) {
m_keyPair1 = getECCKeyPairObject((byte) m_testSettings.keyClass, m_testSettings.keyLength);
}
else {
m_keyPair1 = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
}
/*
// Make sure that for EC, we will have initialized curve
switch (m_testSettings.keyClass) {
case JCConsts.KeyPair_ALG_EC_F2M: // no break
case JCConsts.KeyPair_ALG_EC_FP:
Expand All @@ -1478,6 +1515,7 @@ void prepare_class_KeyPair(APDU apdu) {
default:
// do nothing
}
*/
apdubuf[(short) (ISO7816.OFFSET_CDATA)] = SUCCESS;
apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, (byte)1);
}
Expand Down
54 changes: 44 additions & 10 deletions AlgTest_JavaCard/src/AlgTest/AlgSupportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,39 @@ void TestSupportedModeSingle(APDU apdu) {
case (byte) 0x19: // no break
case (byte) 0x1C: { // no break - TODO remove this constant, unused
try {
offset++; m_keyPair1 = new KeyPair(algorithmClass, algorithmParam1);

if ((algorithmClass == KeyPair.ALG_EC_FP) || (algorithmClass == KeyPair.ALG_EC_F2M)) {
// Key pair for ECC need to be initialized by proper curve for some cards in some cases
EC_Consts.ensureInitializedECCurve(algorithmClass, algorithmParam1, m_keyPair1, m_ramArray);
}

m_keyPair1.genKeyPair();
apdubuf[(short) (ISO7816.OFFSET_CDATA + offset)] = SUPP_ALG_SUPPORTED;
offset++;
m_keyPair1 = null;
if ((algorithmClass == KeyPair.ALG_EC_FP) || (algorithmClass == KeyPair.ALG_EC_F2M)) {
// Try two alternatives for EC key construction - new KeyPair() emit exception on some cards
try {
// Make KeyPair object first, then initialize curve
m_keyPair1 = new KeyPair(algorithmClass, algorithmParam1);
EC_Consts.ensureInitializedECCurve(algorithmClass, algorithmParam1, m_keyPair1, m_ramArray);
} catch (Exception e) {
// Make public and private keys first, then iniatlize curve and finally create KeyPair
ECPrivateKey ecPrivKey = null;
ECPublicKey ecPubKey = null;
if (algorithmClass == KeyPair.ALG_EC_FP) {
ecPrivKey = (ECPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PRIVATE, algorithmParam1, false);
ecPubKey = (ECPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PUBLIC, algorithmParam1, false);
}
if (algorithmClass == KeyPair.ALG_EC_F2M) {
ecPrivKey = (ECPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_F2M_PRIVATE, algorithmParam1, false);
ecPubKey = (ECPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_F2M_PUBLIC, algorithmParam1, false);
}
if ((ecPrivKey != null) && (ecPubKey != null)) {
EC_Consts.setECKeyParams(ecPubKey, ecPrivKey, algorithmClass, algorithmParam1, m_ramArray);
m_keyPair1 = new KeyPair(ecPubKey, ecPrivKey);
}
}
}
else{
// Other non-ECC keypairs
m_keyPair1 = new KeyPair(algorithmClass, algorithmParam1);
}

m_keyPair1.genKeyPair();
apdubuf[(short) (ISO7816.OFFSET_CDATA + offset)] = SUPP_ALG_SUPPORTED;
}
catch (CryptoException e) {apdubuf[(short) (ISO7816.OFFSET_CDATA + offset)] = (byte) (e.getReason() + SUPP_ALG_EXCEPTION_CODE_OFFSET); }
break;
Expand All @@ -202,12 +226,13 @@ void TestSupportedModeSingle(APDU apdu) {
catch (CryptoException e) { apdubuf[(short) (ISO7816.OFFSET_CDATA + offset)] = (byte) (e.getReason() + SUPP_ALG_EXCEPTION_CODE_OFFSET);}
break;
}
case Consts.CLASS_AEADCIPHER: {
try { offset++; m_object = javacardx.crypto.AEADCipher.getInstance(algorithmClass, false); apdubuf[(short) (ISO7816.OFFSET_CDATA + offset)] = SUPP_ALG_SUPPORTED;}
catch (CryptoException e) { apdubuf[(short) (ISO7816.OFFSET_CDATA + offset)] = (byte) (e.getReason() + SUPP_ALG_EXCEPTION_CODE_OFFSET);}
break;
}
*/
/**/
}
// ENDING 0xFF
offset++;
Expand Down Expand Up @@ -247,6 +272,15 @@ void JCSystemInfo(APDU apdu) {
}
Util.setShort(apdubuf, offset, JCSystem.getMaxCommitCapacity());
offset = (short)(offset + 2);
// APDU information
Util.setShort(apdubuf, offset, APDU.getInBlockSize());
offset = (short) (offset + 2);
Util.setShort(apdubuf, offset, APDU.getOutBlockSize());
offset = (short) (offset + 2);
apdubuf[offset] = APDU.getProtocol();
offset++;
apdubuf[offset] = apdu.getNAD();
offset++;

apdu.setOutgoingAndSend((byte) 0, offset);
}
Expand Down
2 changes: 1 addition & 1 deletion AlgTest_JavaCard/src/AlgTest/EC_Consts.java
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ public class EC_Consts {
public static final short EC163_F2M_K = 2;
*/

private static void setECKeyParams(ECPublicKey ecPubKey, ECPrivateKey ecPrivKey, byte ecClass, short ecLength, byte[] auxBuffer) {
static void setECKeyParams(ECPublicKey ecPubKey, ECPrivateKey ecPrivKey, byte ecClass, short ecLength, byte[] auxBuffer) {
if (ecClass == KeyPair.ALG_EC_FP) {
// Select proper courve parameters
boolean bParamReady = false;
Expand Down
Loading

0 comments on commit 86ea4d0

Please sign in to comment.